[基礎知識說明]linux
結構原型:網絡
/*
* Interface request structure used for socket
* ioctl's. All interface ioctl's must have parameter
* definitions which begin with ifr_name. The
* remainder may be interface specific.
*/數據結構
struct ifreq
{
#define IFHWADDRLEN 6
union
{
char ifrn_name[IFNAMSIZ]; /* if name, e.g. "en0" */
} ifr_ifrn;
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ]; /* Just fits the size */
char ifru_newname[IFNAMSIZ];
void __user * ifru_data;
struct if_settings ifru_settings;
} ifr_ifru;
};socket
#define ifr_name ifr_ifrn.ifrn_name /* interface name */
#define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
#define ifr_addr ifr_ifru.ifru_addr /* address */
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
#define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
#define ifr_flags ifr_ifru.ifru_flags /* flags */
#define ifr_metric ifr_ifru.ifru_ivalue /* metric */
#define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
#define ifr_map ifr_ifru.ifru_map /* device map */
#define ifr_slave ifr_ifru.ifru_slave /* slave device */
#define ifr_data ifr_ifru.ifru_data /* for use by interface */
#define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
#define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
#define ifr_qlen ifr_ifru.ifru_ivalue /* Queue length */
#define ifr_newname ifr_ifru.ifru_newname /* New name */
#define ifr_settings ifr_ifru.ifru_settings /* Device/proto settings*/spa
基本介紹: 指針
ifreq結構定義在/usr/include/net/if.h,用來配置ip地址,激活接口,配置MTU等接口信息的。其中包含了一個接口的名字和具體內容——(是個共用體,有多是IP地址,廣播地址,子網掩碼,MAC號,MTU或其餘內容)。ifreq包含在ifconf結構中。而ifconf結構一般是用來保存全部接口的信息的。code
舉例說明:blog
在Linux系統中,ifconfig命令是經過ioctl接口與內核通訊,例如,當系統管理員輸入以下命令來改變接口eth0的MTU大小:接口
ifconfig eth0 mtu 1250 ip
ifconfig命令首先打開一個socket,而後經過系統管理員輸入的參數初始化一個數據結構,並經過ioctl調用將數據傳送到內核。SIOCSIFMTU是命令標識符。
struct ifreq data;
fd = socket(PF_INET, SOCK_DGRAM, 0);
< ... initialize "data" ...>
err = ioctl(fd, SIOCSIFMTU, &data);
[舉例應用]
/** * * \brief 經過廣播socket獲取系統eth0當前IP地址 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIpBySocket(char* pAddr) { if ( pAddr == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFADDR,&ifr) < 0) { perror("ioctl error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_addr); strcpy(pAddr, inet_ntoa(sin->sin_addr)); close(sock); } return RET_OK; }
/** * * \brief 經過廣播socket獲取系統eth0當前子網掩碼 * * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMaskBySocket(char* pMask) { if ( pMask == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFNETMASK,&ifr) < 0) { perror("ioctl error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_netmask); strcpy(pMask, inet_ntoa(sin->sin_addr)); close(sock); } return RET_OK; }
/** * * \brief 經過廣播socket獲取系統eth0當前IP地址和子網掩碼 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIpMaskBySocket(char* pAddr, char* pMask) { if ( pAddr == NULL || pMask == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFADDR,&ifr) < 0) { perror("ioctl SIOCGIFADDR error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_addr); strcpy(pAddr, inet_ntoa(sin->sin_addr)); } if(ioctl(sock,SIOCGIFNETMASK,&ifr) < 0) { perror("ioctl SIOCGIFNETMASK error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_netmask); strcpy(pMask, inet_ntoa(sin->sin_addr)); } close(sock); return RET_OK; }
/** * * \brief 經過ioctl獲取系統eth0 Mac地址 * * \param pMask 存儲 Mac地址的內存地址,不小於18個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMacBySocket(char* pMac) { if ( pMac == NULL ) { return RET_ERR_PARAM; } struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFHWADDR,&ifr) < 0) { perror("ioctl SIOCGIFHWADDR error\n"); close(sock); return RET_ERROR; } else { sprintf(pMac, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]); } close(sock); return RET_OK; }
參考H CPP文件
/* * ===================================================================================== * * Filename: ipconfig.h * * Description: 基於ifconfig命令,獲取、配置系統當前網絡配置參數。 * * Version: 1.0 * Created: 11/23/2012 01:42:16 PM HKT * Revision: none * Compiler: gcc * * Author: * Company: * * ===================================================================================== */ #ifndef IPCONFIG_INC #define IPCONFIG_INC bool IsIpValid (const char* pAddr); bool IsMaskValid (const char* pNetMask); bool IsMacValid (const char* pMac); bool IsGatewayValid(const char* pGateway); int GetSysIp (char* pAddr); int GetSysMask (char* pMask); int GetSysIpMask(char* pAddr, char* pMask); int GetSysMac (char* pMac); int GetSysGateway(char* pGateway); int GetSysBroadcast(const char* pAddr, const char* pMask, char* pBc); int GetSysIpBySocket (char* pAddr); int GetSysMaskBySocket (char* pMask); int GetSysIpMaskBySocket(char* pAddr, char* pMask); int GetSysMacBySocket (char* pMac); int SetSysIp (const char* pAddr); int SetSysMask (const char* pNetMask); int SetSysMac (const char* pMac); int SetSysIpMask(const char* pAddr, const char* pNetMask); int SetSysGateway(const char* pGateway); #endif /* ----- #ifndef IPCONFIG_INC ----- */
/* * ===================================================================================== * * Filename: ipconfig.cpp * * Description: 基於ifconfig命令,獲取、配置系統當前網絡配置參數。 * * Version: 1.0 * Created: 11/23/2012 11:35:52 AM HKT * Revision: none * Compiler: gcc * * Author: (), * Company: * * ===================================================================================== */ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #ifdef __linux__ #include <unistd.h> #endif #include <sys/file.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <net/if.h> #include <sys/ioctl.h> #include "RcsFile.h" #include "lederror.h" static const unsigned int IPADDR_LEN = 15;//IP地址長度 static const unsigned int MACADDR_LEN = 17;//MAC地址長度 /** * \brief 判斷字符是否爲十六進制表示字符,即0~f。 */ bool IsHexChar(char hex) { return ( ( hex >= '0' && hex <= '9' ) || ( hex >= 'a' && hex <= 'f' ) || ( hex >= 'A' && hex <= 'F' ) ); } int HexCharToInt(char hex) { int val = -1; if( hex >= '0' && hex <= '9' ) { val = hex - '0'; } else if( hex >= 'a' && hex <= 'f' ) { val = hex - 'a' + 10; } else if( hex >= 'A' && hex <= 'F' ) { val = hex - 'A' + 10; } return val; } /** * \brief 判斷輸入ip是否有效 * * \return true 有效 * false 無效 */ bool IsIpValid(const char* pAddr) { if ( NULL == pAddr ) { return false; } struct in_addr addr; if ( inet_aton ( pAddr, &addr ) == 0 ) { return false; } char* destIp = ( char* ) inet_ntoa ( addr ); if ( 0 != strcmp ( pAddr, destIp ) ) { return false; } return true; } /** * \brief 判斷輸入netmask字符串是否有效 * * \param pNetMask 表示netmask的字符串指針 * * \return true 有效 * \return false 無效 */ bool IsMaskValid(const char* pNetMask) { bool ret = false; if ( !IsIpValid(pNetMask) ) { return ret; } unsigned int mask = ntohl( inet_addr(pNetMask) ); //最高8位必須全爲0,且不能爲0xffffff if ( mask < 0xff || mask == 0xffffffff ) { return ret; } for ( int i = 23; i >= 0; --i ) { if ( (mask & (1 << i)) > 0 ) { continue; } if ( (mask << (8 + 23 - i)) > 0 ) { ret = false; break; } ret = true; break; } return ret; } /** * \brief 判斷輸入mac字符串是否有效 * * \param pMac 表示mac地址的字符串指針 * * \return true 有效 * \return false 無效 */ bool IsMacValid(const char* pMac) { if ( NULL == pMac || strlen ( pMac ) != MACADDR_LEN ) { return false; } //MAC的第一個字節必須爲偶數 if( HexCharToInt(*(pMac+1)) % 2 != 0 ) { return false; } for ( unsigned int index = 0; index < MACADDR_LEN; ++index ) { if ( 0 == ( ( index + 1 ) % 3 ) ) //不檢查間隔爲3的字符 { continue; } //字符必須爲十六進制表示字符,即0~f。 char val = ( * ( pMac + index ) ); if ( !IsHexChar(val) ) { return false; } } return true; } /** * \brief 判斷輸入網關字符串是否有效 * * \param pGateway 表示網關地址的字符串指針 * * \return true 有效 * \return false 無效 */ bool IsGatewayValid(const char* pGateway) { return IsIpValid(pGateway); } /** * * \brief 獲取系統eth0當前IP地址 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIp(char* pAddr) { const char FILE_NAME[] = "/tmp/ipaddr.txt"; const char SCRIPT[] = "ifconfig eth0 | grep inet | cut -d: -f2 | cut -d' ' -f1 > /tmp/ipaddr.txt"; FILE* handle = NULL; char* delimiter = NULL; if ( pAddr == NULL ) { return RET_ERR_PARAM; } if ( system(SCRIPT) != 0 ) { fprintf(stderr, "Execute command '%s'error: %s\n", SCRIPT, strerror(errno)); goto FAILED; } if ( (handle = fopen(FILE_NAME, "r")) != NULL ) { fgets(pAddr, IPADDR_LEN-1, handle); } else { fprintf(stderr, "Open file %s error: %s\n", FILE_NAME, strerror(errno)); goto FAILED; } fclose(handle); if ( (delimiter = strchr(pAddr, '\x0a')) != NULL ) { *delimiter = '\0'; } unlink(FILE_NAME); return RET_OK; FAILED: unlink(FILE_NAME); return RET_ERROR; } /** * * \brief 獲取系統eth0當前子網掩碼 * * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMask(char* pMask) { const char FILE_NAME[] = "/tmp/netmask.txt"; const char SCRIPT[] = "ifconfig eth0 | grep Mask | cut -dk -f2 | cut -d: -f2 > /tmp/netmask.txt"; FILE* handle = NULL; char* delimiter = NULL; if ( pMask == NULL ) { return RET_ERR_PARAM; } if ( system(SCRIPT) != 0 ) { fprintf(stderr, "Execute command '%s'error: %s\n", SCRIPT, strerror(errno)); goto FAILED; } if ( (handle = fopen(FILE_NAME, "r")) != NULL ) { fgets(pMask, IPADDR_LEN+1, handle); } else { fprintf(stderr, "Open file %s error: %s\n", FILE_NAME, strerror(errno)); goto FAILED; } fclose(handle); if ( (delimiter = strchr(pMask, '\x0a')) != NULL ) { *delimiter = '\0'; } unlink(FILE_NAME); return RET_OK; FAILED: unlink(FILE_NAME); return RET_ERROR; } /** * * \brief 獲取系統eth0當前IP地址和子網掩碼 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIpMask(char* pAddr, char* pMask) { const char FILE_NAME[] = "/tmp/ipmask.txt"; const char SCRIPT[] = "ifconfig eth0 | grep -i mask > /tmp/ipmask.txt"; char info[128] = {0}; char *delimiter = NULL; FILE *handle = NULL; if ( pAddr == NULL || pMask == NULL ) { return RET_ERR_PARAM; } if ( system(SCRIPT) != 0 ) { fprintf(stderr, "Execute command '%s'error: %s\n", SCRIPT, strerror(errno)); goto FAILED; } if ( (handle = fopen(FILE_NAME, "r")) != NULL ) { fread(info, 128, 1, handle); char* ipbegin = strstr(info, "addr:"); if( ipbegin ) { ipbegin += 5; sscanf(ipbegin, "%s", pAddr); } char* maskbegin = strstr(info, "Mask:"); if( maskbegin ) { maskbegin += 5; sscanf(maskbegin, "%s", pMask); } fclose(handle); } else { fprintf(stderr, "Open file %s error: %s\n", FILE_NAME, strerror(errno)); goto FAILED; } if ( (delimiter = strchr(pAddr, '\x0a')) != NULL ) { *delimiter = '\0'; } if ( (delimiter = strchr(pMask, '\x0a')) != NULL ) { *delimiter = '\0'; } unlink(FILE_NAME); return RET_OK; FAILED: unlink(FILE_NAME); return RET_ERROR; } /** * * \brief 獲取系統eth0 Mac地址 * * \param pMask 存儲 Mac地址的內存地址,不小於18個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMac(char* pMac) { const char FILE_NAME[] = "/tmp/ipmac.txt"; const char SCRIPT[] = "ifconfig eth0 | grep HWaddr|tr -s ' '|cut -d ' ' -f 5 > /tmp/ipmac.txt"; FILE* handle = NULL; char* delimiter = NULL; if ( pMac == NULL ) { return RET_ERR_PARAM; } if ( system(SCRIPT) != 0 ) { fprintf(stderr, "Execute command '%s'error: %s\n", SCRIPT, strerror(errno)); goto FAILED; } if ( (handle = fopen(FILE_NAME, "r")) != NULL ) { fgets(pMac, MACADDR_LEN+1, handle); } else { fprintf(stderr, "Open file %s error: %s\n", FILE_NAME, strerror(errno)); goto FAILED; } fclose(handle); if ( (delimiter = strchr(pMac, '\x0a')) != NULL ) { *delimiter = '\0'; } unlink(FILE_NAME); return RET_OK; FAILED: unlink(FILE_NAME); return RET_ERROR; } /** * \brief 獲取系統網關地址。 * * \return RET_OK 成功 * \return RET_ERROR 失敗 */ int GetSysGateway(char* pGateway) { const char FILE_NAME[] = "/tmp/ipgateway.txt"; const char SCRIPT[] = "route | grep default | cut -d' ' -f10 | cut -d' ' -f1 > /tmp/ipgateway.txt"; FILE* handle = NULL; char* delimiter = NULL; if ( pGateway == NULL ) { return RET_ERR_PARAM; } if ( system(SCRIPT) != 0 ) { fprintf(stderr, "Execute command '%s'error: %s\n", SCRIPT, strerror(errno)); goto FAILED; } if ( (handle = fopen(FILE_NAME, "r")) != NULL ) { fgets(pGateway, IPADDR_LEN+1, handle); } else { fprintf(stderr, "Open file %s error: %s\n", FILE_NAME, strerror(errno)); goto FAILED; } fclose(handle); if ( (delimiter = strchr(pGateway, '\x0a')) != NULL ) { *delimiter = '\0'; } unlink(FILE_NAME); return RET_OK; FAILED: unlink(FILE_NAME); return RET_ERROR; } int SetSysIp(const char* pAddr) { if ( !IsIpValid(pAddr) ) { fprintf(stderr, "Input ip %s is invalid\n", pAddr); return RET_ERROR; } char localip[80] = {0}; GetSysIp(localip); if ( strcmp(localip, pAddr) == 0 ) { return RET_OK; } fprintf(stderr, "[%s]: ip is %s\n", __FUNCTION__, pAddr); sprintf(localip, "ifconfig eth0 %s up ", pAddr); if ( system(localip) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", localip, strerror(errno)); return RET_ERROR; } return ModifyRcs("LED_IP",pAddr); } int SetSysMask(const char* pMask) { if ( !IsMaskValid(pMask) ) { fprintf(stderr, "Input netmask %s is invalid\n", pMask); return RET_ERROR; } char netmask[80] = {0}; GetSysIp(netmask); if ( strcmp(netmask, pMask) == 0 ) { return RET_OK; } fprintf(stderr, "[%s]: netmask is %s\n", __FUNCTION__, pMask); sprintf(netmask, "ifconfig eth0 netmask %s up ", pMask); if ( system(netmask) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", netmask, strerror(errno)); return RET_ERROR; } return ModifyRcs("LED_MASK",pMask); } int SetSysMac(const char* pMac) { if ( !IsMacValid(pMac) ) { fprintf(stderr, "Input mac addr %s is invalid\n", pMac); return RET_ERROR; } char mac[80] = {0}; GetSysMac(mac); if ( strcmp(mac, pMac) == 0 ) { return RET_OK; } fprintf(stderr, "[%s]: mac addr is %s\n", __FUNCTION__, pMac); //shut down network memset(mac, 0, sizeof(mac)); sprintf(mac, "ifconfig eth0 down"); if ( system(mac) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", mac, strerror(errno)); return RET_ERROR; } //config mac address memset(mac, 0, sizeof(mac)); sprintf(mac, "ifconfig eth0 hw ether %s", pMac); if ( system(mac) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", mac, strerror(errno)); return RET_ERROR; } //start network memset(mac, 0, sizeof(mac)); sprintf(mac, "ifconfig eth0 up"); if ( system(mac) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", mac, strerror(errno)); return RET_ERROR; } return ModifyRcs("LED_MAC",pMac); } /** * \brief 設置ip和mask。 * * \return RET_OK 成功 * \return RET_ERROR 失敗 */ int SetSysIpMask(const char* pAddr, const char* pNetMask) { //判斷ip和mask的有效性 if ( (!IsIpValid(pAddr)) || (!IsMaskValid(pNetMask)) ) { fprintf(stderr, "Input ip %s or netmask %s is invalid\n", pAddr, pNetMask); return RET_ERROR; } //設置ip和netmask char script[80] = {0}; sprintf(script, "ifconfig eth0 %s netmask %s up ", pAddr, pNetMask); if ( system(script) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", script, strerror(errno)); return RET_ERROR; } ModifyRcs("LED_IP",pAddr); return ModifyRcs("LED_MASK",pNetMask); } /** * \brief 設置gateway。 * * \return RET_OK 成功 * \return RET_ERROR 失敗 */ int SetSysGateway(const char* pGateway) { if( pGateway && strlen(pGateway) == 0 ) { //把網關刪除,設爲空 } else { //判斷gateway的有效性 if ( (!IsGatewayValid(pGateway)) ) { fprintf(stderr, "Input Gateway %s is invalid\n", pGateway); return RET_ERROR; } } //設置gateway char script[80] = {0}; sprintf(script, "route del default "); if ( system(script) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", script, strerror(errno)); } if( strlen(pGateway) ) { sprintf(script, "route add default gw %s ", pGateway); if ( system(script) != 0 ) { fprintf(stderr, "Execute command '%s' error: %s\n", script, strerror(errno)); return RET_ERROR; } } return ModifyRcs("LED_GATEWAY",pGateway); } /** * \brief 根據IP和子網掩碼獲取本網段廣播地址。 */ int GetSysBroadcast(const char* pAddr, const char* pMask, char* pBc) { if( !pAddr || !pMask || !pBc ) { return RET_ERR_PARAM; } if( !IsIpValid(pAddr) || !IsMaskValid(pMask) ) { return RET_ERR_PARAM; } unsigned int ip = ntohl( inet_addr(pAddr) ); unsigned int mask = ntohl( inet_addr(pMask) ); unsigned int bc = (ip & mask) | (~mask); struct in_addr bcAddr; bcAddr.s_addr = htonl(bc); char *tmp = inet_ntoa(bcAddr); memcpy(pBc, tmp, strlen(tmp)); return RET_OK; } /** * * \brief 經過廣播socket獲取系統eth0當前IP地址 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIpBySocket(char* pAddr) { if ( pAddr == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFADDR,&ifr) < 0) { perror("ioctl error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_addr); strcpy(pAddr, inet_ntoa(sin->sin_addr)); close(sock); } return RET_OK; } /** * * \brief 經過廣播socket獲取系統eth0當前子網掩碼 * * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMaskBySocket(char* pMask) { if ( pMask == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFNETMASK,&ifr) < 0) { perror("ioctl error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_netmask); strcpy(pMask, inet_ntoa(sin->sin_addr)); close(sock); } return RET_OK; } /** * * \brief 經過廣播socket獲取系統eth0當前IP地址和子網掩碼 * * \param pAddr 存儲IP地址的內存地址,不小於16個字節。 * \param pMask 存儲子網掩碼的內存地址,不小於16個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysIpMaskBySocket(char* pAddr, char* pMask) { if ( pAddr == NULL || pMask == NULL ) { return RET_ERR_PARAM; } struct sockaddr_in *sin; struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFADDR,&ifr) < 0) { perror("ioctl SIOCGIFADDR error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_addr); strcpy(pAddr, inet_ntoa(sin->sin_addr)); } if(ioctl(sock,SIOCGIFNETMASK,&ifr) < 0) { perror("ioctl SIOCGIFNETMASK error\n"); close(sock); return RET_ERROR; } else { sin = (struct sockaddr_in *)&(ifr.ifr_netmask); strcpy(pMask, inet_ntoa(sin->sin_addr)); } close(sock); return RET_OK; } /** * * \brief 經過ioctl獲取系統eth0 Mac地址 * * \param pMask 存儲 Mac地址的內存地址,不小於18個字節。 * * \Return 0 成功 * else 失敗 */ int GetSysMacBySocket(char* pMac) { if ( pMac == NULL ) { return RET_ERR_PARAM; } struct ifreq ifr; int sock = socket(AF_INET,SOCK_DGRAM,0); if(sock <= 0) { perror("socket error!\n"); return RET_ERROR; } strcpy(ifr.ifr_name,"eth0"); if(ioctl(sock,SIOCGIFHWADDR,&ifr) < 0) { perror("ioctl SIOCGIFHWADDR error\n"); close(sock); return RET_ERROR; } else { sprintf(pMac, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[5]); } close(sock); return RET_OK; }