0
TUN 인터페이스를 통과하는 패킷을 가로 채려고합니다. 원시 패킷 정보를 읽을 수있는 정보로 변환하여 나중에 사용할 수 있습니다.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 주소를 읽을 수없는 것 같다. 나는 결과가 흐려지고있다.
도와 주시겠습니까 ??
감사합니다. TUN 인터페이스의 생성, 여기서
int tun_alloc(char *dev) {
struct ifreq ifr;
int fd, err;
char *clonedev = "/dev/net/tun";
/* open the clone device */
if((fd = open(clonedev, O_RDWR)) < 0) {
return fd;
}
/* preparation of the struct ifr, of type "struct ifreq" */
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN; /* IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI */
if (*dev) {
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
}
/* try to create the device */
if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
: 여기
tun_fd = tun_alloc(tun_name); /* tun interface, no Ethernet headers*/
는 함수 tun_alloc()의 정의이다
IPv6가없는 한 작동합니다. 이 경우 커널이 tun 패킷에 추가 한 처음 4 바이트를 요청할 수 있습니다. 이 바이트는 플래그와 프로토콜 (INET/INET6)을 포함합니다. –