本文實例為大家分享了C語言實現linux網卡檢測的精簡代碼,供大家參考,具體內容如下
萬能的網絡,通過getifaddrs可以大大減少編碼量,獲得 C語言實現linux網卡檢測-改進版 同樣的效果。
#include <stdlib.h>#include <stdio.h>#include <string.h> #include <sys/socket.h>#include <sys/ioctl.h>#include <linux/if.h>#include <ifaddrs.h>#include <arpa/inet.h> int c_ifaddrs_netlink_status(const char *if_name ){ struct ifaddrs *ifa = NULL, *ifList; if (getifaddrs(&ifList) < 0) { return -1; } for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) { if(ifa->ifa_addr->sa_family == AF_INET) { if(strcmp(ifa->ifa_name, if_name) ==0) { if(!(ifa->ifa_flags & IFF_UP)) { printf("DEVICE_DOWN/r/n"); freeifaddrs(ifList); return 1; } if(!(ifa->ifa_flags & IFF_RUNNING)) { printf("DEVICE_UNPLUGGED/r/n"); freeifaddrs(ifList); return 2; } printf("DEVICE_LINKED/r/n"); freeifaddrs(ifList); return 3; } } } printf(stderr, "DEVICE_NONE/r/n"); freeifaddrs(ifList); return 0;} int main(int argc, char* argv[]){ int i=0; if(argc != 2) { fprintf(stderr, "usage: %s <ethname>/r/n", argv[0]); return -1; } i = c_ifaddrs_netlink_status(argv[1]); fprintf(stderr,"c_ifaddrs_netlink_status if_status = %d/n", i ); return 0;}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答