[問題] 取得網路裝置
我想抓網卡的 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
08/16 16:23, 1F
→
08/16 16:24, , 2F
08/16 16:24, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章