Recently in the process of doing the project, due to the use of the system function in multi-threading, sometimes inexplicable program abnormal termination, and finally decided to replace all the system functions, but for the setting of MAC address this function tried many times did not succeed;
1. The Write off/Open network card function has no delay, (General setting switch network card may need to be initialized, so if the switch may not be initialized successfully, you can try to quickly switch the network card under the embedded Linux command line, is also unresponsive)
2. Type conversion problem; (for convenience, use SSCANF to extract the Mac string, if using 8bit extract%x, conversion error, finally can only use 32bit, and then assign values)
#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: Turn off local specified network adapter-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; Incoming parameter legality detection if (interface_name = = NULL) {fprintf (stdout, "%s:%d:args invalid!", __function__, __line__); return-1;} Turn off loopback 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__, __l INE__); return-3; }selector = iff_up; Ifr.ifr_flags &= ~selector; if (IOCTL (SOCK_FD, Siocsifflags, &IFR) < 0) {fprintf (stdout, "%s:%d:ioctl failed 2!", __function__, __l INE__); return-4; }close (SOCK_FD); return 0;} Add by LIGHTD, 2014-06-04//============================================================================// Function:ifconfig_ethx_up_api//description: Open local specified network card-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; Incoming parameter legality detection 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: Set mac//input for locally specified NIC://output://return://others: none//test Result: test Result-20 consecutive calls without error//=========================================================================== =int8s Setlocalmacaddr_api (const int8u *interface_name, const int8u *str_macaddr) {int ret; int sock_fd; struct Ifreq IFR; INT32U mac2bit[6];//incoming parameter legality detection if (interface_name = = NULL | | str_macaddr = = NULL) {fprintf (stdout, "%s:%d:args invalid!", __ FUNCTION__, __line__); return-1;} Extract Mac Format 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; }//before setting up your Mac, you must close the corresponding network card-otherwise error RET = IFCONFIG_ETHX_DOWN_API (interface_name), if (Ret < 0) {fprintf (stdout, "%s:%d:clos E eth0 error ", __function__, __line__); return-3;} Sleep (1); Wait for the NIC to close 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__, __li ne__); return-5;} Sleep (2); Wait for the network card to open OK return 0; }//add by LIGHTD, 2014-06-04//============================================================================// Function:getlocalmacaddr_api//description: Get mac//input of locally specified NIC://output://return://others:none//============ ================================================================int8s GetlocalmacaDdr_api (const int8u *interface_name, int8u *str_macaddr) {int32s sock_fd; struct Ifreq Ifr_mac; Incoming parameter legality detection 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; }//Specify NIC memset (&ifr_mac, 0, sizeof (IFR_MAC)); sprintf (Ifr_mac.ifr_name, "%s", interface_name); Gets the MAC address of the specified network card 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 Setup Network API---on/Guan, set/Get MAC address