[問題] 取得網路裝置

看板C_and_CPP (C/C++)作者 (可樂)時間12年前 (2013/08/16 12:01), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/1
我想抓網卡的 mac address 和監視流量 在網路上看到一個範例 #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> void main(void) { struct ifreq ifr; int sockfd; sockfd = socket(AF_INET, SOCK_DGRAM, 0); bzero(&ifr, sizeof(ifr)); strcpy(ifr.ifr_name, "eth0"); ioctl(sockfd, SIOCGIFFLAGS, &ifr); if (ifr.ifr_flags & IFF_UP) { printf("eth0 is up!"); } close(sockfd); } 編譯會出現 gcc test3.c -o test3.o test3.c:8:6: warning: return type of ‘main’ is not ‘int’ test3.c: In function ‘main’: test3.c:10:15: error: storage size of ‘ifr’ isn’t known test3.c:14:2: warning: implicit declaration of function ‘bzero’ test3.c:37:22: error: ‘IFF_UP’ undeclared (first use in this function) test3.c:37:22: note: each undeclared identifier is reported only once for each function it appears in test3.c:10:15: warning: unused variable ‘ifr’ 兩個error看起來是找不到<net/if.h> 可是我確定 /usr/include/net/if.h file還在 而且昨天編譯沒有問題。今天突然就不行了。 請問有人知道原因嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.154.115

08/16 16:23, , 1F
你直接打開那個檔就會發現需要#define __USE_MISC不過
08/16 16:23, 1F

08/16 16:24, , 2F
我不知道原因
08/16 16:24, 2F
文章代碼(AID): #1I3QGF2Y (C_and_CPP)
文章代碼(AID): #1I3QGF2Y (C_and_CPP)