TUN インターフェイスを通過するパケットを傍受しようとしています。後で使用できるように、生のパケット情報を読み取り可能な情報に変換したいと考えています。
私は次のコードを使用しています:
int main(){
char tun_name[IFNAMSIZ];
char data[1500];
int nread = 0;
int tun_fd = 0;
/* Connect to the device */
strcpy(tun_name, "tun1");
tun_fd = tun_alloc(tun_name); /* tun interface, no Ethernet headers*/
if(tun_fd < 0){
perror("Allocating interface");
exit(1);
}
/* Now read data coming from the kernel */
int i=0;
int count=0;
char src[INET_ADDRSTRLEN];
u_int8_t protocol;
while(1) {
count ++;
nread = read(tun_fd, data, sizeof(data));
if(nread < 0) {
perror("Reading from interface");
close(tun_fd);
exit(1);
}
struct ip *iphdr = (struct ip *) data;
/* Do whatever with the data */
printf("Packet N° %d\n", count);
printf("Read %d bytes from device %s\n", nread, tun_name);
protocol = iphdr->ip_p;
inet_ntop(AF_INET, &(iphdr->ip_src), src, INET_ADDRSTRLEN);
printf("\nProtocol: %d", protocol);
printf("\nIP source address: %s", src);
printf("\n\n");
}
return 0;
}
パケットのプロトコルと ip src アドレスを読み取れないようです。私は奇妙な結果を得ています。
助けてください??
ありがとうございました!