标签:eth1 soc find mtu inet_ntoa add flags amp request
TR069终端程序需要获取wan口的状态信息,这里采用一种方式,使用socket函数打开一个描述符,使用ioctl函数获取相应的参数。ifconfig的内部实现也是这样做的,具体可以参看ifocnfig源码。
如要获取wan口的信息,则首先需要知道wan口对应的网卡名是什么,这里假定为eth0,因为具体的获取需要看具体设备的配置,所以这里就默认为eth0。
int s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { return -1; }
网卡信息需要使用 struct ifreq ifr; 结构体,该结构体在net/if.h文件中。
struct ifreq { # define IFHWADDRLEN 6 # define IFNAMSIZ IF_NAMESIZE union { char ifrn_name[IFNAMSIZ]; /* Interface 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 int ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[IFNAMSIZ]; /* Just fits the size */ char ifru_newname[IFNAMSIZ]; __caddr_t ifru_data; } ifr_ifru; }; # 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 */
这次需要获取MTU信息,因此可以使用
strcpy(ifr.ifr_name,"eth0"); if (ioctl(s,SIOCGIFMTU, &ifr) < 0) { return -1; }
该操作过后,所保存的信息就存在 mtu = ifr.ifr_map; 结构体元素中了,默认值为1500。
值得注意的是,每次操作都需要重新给网卡名赋值,在结构体中有如下的注释:
/* 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. */
例如要获取Mac地址与IP地址,则可以通过如下的代码实现:
unsigned char mac[6];
unsigned long ip;
strcpy(ifr.ifr_name, "eth0"); if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) { close(s); return -1; } memcpy(mac, ifr.ifr_hwaddr.sa_data, sizeof(mac)); sprintf(wanIf->MACAddress, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); strcpy(ifr.ifr_name, "eth0"); if (ioctl(s, SIOCGIFADDR, &ifr) < 0) { ip = 0; } else { ip = *(unsigned long*)&ifr.ifr_broadaddr.sa_data[2]; strcpy(wanIf->ExternalIPAddress, inet_ntoa(*(struct in_addr*)&ip)); }
ifconfig命令修改MTU:
ifconfig ${Interface} mtu ${SIZE} up ifconfig eth1 mtu 9000 up
具体的ioctl函数在网络获取参数的用法可以参考这篇博文http://www.cnblogs.com/minuse/p/3592567.html?utm_source=tuicool&utm_medium=referral 。
标签:eth1 soc find mtu inet_ntoa add flags amp request
原文地址:http://www.cnblogs.com/fogcell/p/6670364.html