标签:des blog os 使用 io ar 2014 问题 cti
最近在做项目过程中,由于在多线程中使用system函数,有时候出现莫名程序异常终止,最后决定替换所有的system函数,但是对于设置mac地址这个函数试了很多次都没有成功;今天在此总结下原因:
1. 编写的关闭/打开网卡函数没有延时;(一般情况设置开关网卡可能需要初始化,所以如果开关连续可能没有初始化成功,你可以试试在嵌入式Linux命令行下快速的开关网卡,也是没有反应的)
2. 类型转换问题;(由于为了便捷,使用sscanf提取mac字符串,如果使用8bit提取%x,转换出错,最后只能使用32bit,再一个个赋值即可)
#include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <net/if.h> #include <netinet/in.h> typedef signed char INT8S; typedef unsigned char INT8U; typedef signed short INT16S; typedef unsigned short INT16U; typedef signed int INT32S; typedef unsigned int INT32U; //add by lightd, 2014-06-04 //============================================================================ //Function: ifconfig_ethx_down_API //Description: 关闭本地指定网卡 - eg: ifconfig eth0 down //Input: //Output: //Return: //Others: None //============================================================================ INT8S ifconfig_ethx_down_API(const INT8U *interface_name) { INT32S sock_fd; struct ifreq ifr; int selector; //传入参数合法性检测 if(interface_name == NULL) { fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__); return -1; } //禁止关闭回环 if(strncmp((char *)interface_name, (char *)"lo", 2) == 0) { fprintf(stdout, "%s:%d: You can't pull down interface lo!", __FUNCTION__, __LINE__); return 0; } sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if(sock_fd < 0) { fprintf(stdout, "%s:%d: socket failed!", __FUNCTION__, __LINE__); return -2; } sprintf(ifr.ifr_name, "%s", interface_name); if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) { fprintf(stdout, "%s:%d: ioctl failed 1!", __FUNCTION__, __LINE__); return -3; } selector = IFF_UP; ifr.ifr_flags &= ~selector; if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) { fprintf(stdout, "%s:%d: ioctl failed 2!", __FUNCTION__, __LINE__); return -4; } close( sock_fd ); return 0; } //add by lightd, 2014-06-04 //============================================================================ //Function: ifconfig_ethx_up_API //Description: 打开本地指定网卡 - eg: ifconfig eth0 up //Input: //Output: //Return: //Others: None //============================================================================ INT8S ifconfig_ethx_up_API(const INT8U *interface_name) { INT32S sock_fd; struct ifreq ifr; int selector; //传入参数合法性检测 if(interface_name == NULL) { fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__); return -1; } sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if(sock_fd < 0) { fprintf(stdout, "%s:%d: create socket failed!", __FUNCTION__, __LINE__); return -2; } sprintf(ifr.ifr_name, "%s", interface_name); if(ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) { fprintf(stdout, "%s:%d: ioctl error 1", __FUNCTION__, __LINE__); return -3; } selector = (IFF_UP | IFF_RUNNING); ifr.ifr_flags |= selector; if(ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) { fprintf(stdout, "%s:%d: ioctl error 2", __FUNCTION__, __LINE__); return -4; } close( sock_fd ); return 0; } //add by lightd, 2014-06-04 //============================================================================ //Function: SetLocalMACAddr_API //Description: 设置本地指定网卡的MAC //Input: //Output: //Return: //Others: None //Test result: 测试结果 - 连续调用20次不出错 //============================================================================ INT8S SetLocalMACAddr_API(const INT8U *interface_name, const INT8U *str_macaddr) { int ret; int sock_fd; struct ifreq ifr; INT32U mac2bit[6]; //传入参数合法性检测 if(interface_name == NULL || str_macaddr == NULL) { fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__); return -1; } //提取mac格式 sscanf((char *)str_macaddr, "%02X:%02X:%02X:%02X:%02X:%02X", (INT8U *)&mac2bit[0], (INT8U *)&mac2bit[1], (INT8U *)&mac2bit[2], (INT8U *)&mac2bit[3], (INT8U *)&mac2bit[4], (INT8U *)&mac2bit[5]); sock_fd = socket(PF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) { perror("socket error"); return -2; } //设置mac前,必须关闭对应的网卡 - 否则出错 ret = ifconfig_ethx_down_API( interface_name ); if(ret < 0) { fprintf(stdout, "%s:%d: close eth0 error", __FUNCTION__, __LINE__); return -3; } sleep(1); //等待网卡关闭OK sprintf(ifr.ifr_ifrn.ifrn_name, "%s", interface_name); ifr.ifr_ifru.ifru_hwaddr.sa_family = 1; ifr.ifr_ifru.ifru_hwaddr.sa_data[0] = mac2bit[0]; ifr.ifr_ifru.ifru_hwaddr.sa_data[1] = mac2bit[1]; ifr.ifr_ifru.ifru_hwaddr.sa_data[2] = mac2bit[2]; ifr.ifr_ifru.ifru_hwaddr.sa_data[3] = mac2bit[3]; ifr.ifr_ifru.ifru_hwaddr.sa_data[4] = mac2bit[4]; ifr.ifr_ifru.ifru_hwaddr.sa_data[5] = mac2bit[5]; ret = ioctl(sock_fd, SIOCSIFHWADDR, &ifr); if (ret != 0) { perror("set mac address erorr"); return -4; } close( sock_fd ); ret = ifconfig_ethx_up_API( interface_name ); if(ret < 0) { fprintf(stdout, "%s:%d: open eth0 error!", __FUNCTION__, __LINE__); return -5; } sleep(2); //等待网卡打开OK return 0; } //add by lightd, 2014-06-04 //============================================================================ //Function: GetLocalMACAddr_API //Description: 获取本地指定网卡的MAC //Input: //Output: //Return: //Others: None //============================================================================ INT8S GetLocalMACAddr_API(const INT8U *interface_name, INT8U *str_macaddr) { INT32S sock_fd; struct ifreq ifr_mac; //传入参数合法性检测 if(interface_name == NULL || str_macaddr == NULL) { fprintf(stdout, "%s:%d: args invalid!", __FUNCTION__, __LINE__); return -1; } sock_fd = socket( AF_INET, SOCK_STREAM, 0 ); if( sock_fd == -1) { perror("create socket failed"); sprintf((char *)str_macaddr, "00:00:00:00:00:00"); return -2; } //指定网卡 memset(&ifr_mac, 0, sizeof(ifr_mac)); sprintf(ifr_mac.ifr_name, "%s", interface_name); //获取指定网卡的mac地址 if( (ioctl( sock_fd, SIOCGIFHWADDR, &ifr_mac)) < 0 ) { perror("mac ioctl error"); sprintf((char *)str_macaddr, "00:00:00:00:00:00"); return -3; } close( sock_fd ); sprintf((char *)str_macaddr,"%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)ifr_mac.ifr_hwaddr.sa_data[0], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[1], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[2], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[3], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[4], (unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]); printf("local mac:<%s> \n", str_macaddr); return 0; } int main(void) { INT8U str_macaddr[20]; memset(str_macaddr, 0, sizeof(str_macaddr)); GetLocalMACAddr_API("eth0", str_macaddr); fprintf(stdout, "1 mac: %s\n", str_macaddr); //ifconfig_ethx_down_API("eth0"); //system("ifconfig eth0 down"); //usleep(500000); //system("ifconfig eth0 up"); //usleep(500000); //sleep(1); //10ms //ifconfig_ethx_down_API("eth0"); //sleep(1); SetLocalMACAddr_API("eth0", "08:00:11:22:33:44"); //ifconfig_ethx_up_API("eth0"); //ifconfig_ethx_up_API("eth0"); //sleep(2); memset(str_macaddr, 0, sizeof(str_macaddr)); GetLocalMACAddr_API("eth0", str_macaddr); fprintf(stdout, "2 mac: %s\n", str_macaddr); system("ping 200.200.200.100"); //usleep(50000); //ifconfig_ethx_down_API("eth0"); //ifconfig_ethx_up_API("eth0"); //memset(str_macaddr, 0, sizeof(str_macaddr)); //GetLocalMACAddr_API("eth0", str_macaddr); fprintf(stdout, "2 mac: %s\n", str_macaddr); return 0; }
linux 设置网络API --- 开/关网卡、设置/获取Mac地址
标签:des blog os 使用 io ar 2014 问题 cti
原文地址:http://blog.csdn.net/dl0914791011/article/details/38959471