最新消息:

ioctl获取所有网卡信息

C/C++ admin 4595浏览 0评论

/* 获得网卡名 */
struct ifreq *ifr, *ifend;
struct ifconf ifc;
struct ifreq ifs[16];
int sockfd;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
ifc.ifc_len = sizeof(ifs);
ifc.ifc_req = ifs;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
writelog(g_log, “[error] %s(%d)-%s ioctl(): %s”,__FILE__,__LINE__,__FUNCTION__,strerror(errno) );
} else {
ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
if (ifr->ifr_addr.sa_family == AF_INET) {
if ( strncmp(ifr->ifr_name,”lo”,strlen(“lo”)) ) {
printf(“[dev]%sn”,ifr->ifr_name);
}
}
}
}

转载请注明:爱开源 » ioctl获取所有网卡信息

您必须 登录 才能发表评论!