raw socket으로 packet을 수신해 보려는 중인데요
글쓴이: study / 작성시간: 수, 2021/11/17 - 11:48오후
아래에 code를 올렸습니다.
간단하게 raw socket을 열고, interface를 binding한 후에
binding 된 interface로 packet을 받아보려고 했는데, 뭔가 문제가 있는가봅니다.
실행을 하면, 잠시 아무일도 없다가 아래와 같은 출력이 보이네요.
code는 아래와 같구요
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <unistd.h> #include <errno.h> #include <net/ethernet.h> #include <linux/if_packet.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/time.h> #include <sys/select.h> int32_t main(int32_t argc, int8_t *argv[]) { int32_t sock; int8_t buf[1522]; int32_t ret; int32_t bytes; sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sock == -1) { printf("socket open failed\n"); return 1; } ret = setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, "ens193", (strlen("ens193")+1)); if (ret == -1) { printf("interface binding failed\n"); close(sock); return 1; } while(1) { bytes = recvfrom(sock, buf, 1522, 0, NULL, NULL); if (bytes < 0) { printf("error in recvfrom\n"); exit; } printf("bytes = %d\n", bytes); } close(sock); return 0; }
출력은 아래와 같습니다.
# ./a.out [잠시 아무일도 없다가] bytes = 60 bytes = 42 bytes = 134 bytes = 118 bytes = 118 bytes = 118 bytes = 118 bytes = 118 bytes = 118 bytes = 66 ... ... ...
Forums:
댓글 달기