ICMP 패킷 캡쳐 프로그램인데 잡지를 못해요 ㅠㅠ 도와주세요
# include unistd.h
# include sys/socket.h
# include sys/types.h
# include string.h
# include netinet/in.h
# include stdio.h
# include stdlib.h
#include netinet/ip.h
#include netinet/ip_icmp.h
int main(){
int sockfd,retval,n;
socklen_t clilen; //소켓 길이 , 크기
struct sockaddr_in cliaddr, servaddr; // 소켓 주소 , 서버 주소
char buf[10000];
int i;
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); //ICMP 인자
if (sockfd < 0){ // 소켓 지정 번호
perror("뜨면 안됌:");
exit(1);
}
clilen = sizeof(struct sockaddr_in);
while(1){
printf("메시지를 읽어오는 중n");
n=recvfrom(sockfd,buf,10000,0,(struct sockaddr *)&cliaddr,&clilen);
printf(" rec'd %d bytesn",n);
struct iphdr *ip_hdr = (struct iphdr *)buf;
printf("IP header is %d bytes.n", ip_hdr->ihl*4);
for (i = 0; i < n; i++) {
printf("%02X%s", (uint8_t)buf[i], (i + 1)%16 ? " " : "n");
}
printf("n");
struct icmphdr *icmp_hdr = (struct icmphdr *)((char *)ip_hdr + (4 * ip_hdr->ihl));
printf("ICMP msgtype=%d, code=%d", icmp_hdr->type, icmp_hdr->code);
printf("Checksum=%04xn",ntohs(icmp_hdr->checksum));
printf("Identifier=%dn",icmp_hdr->un.echo.id);
printf("Seq=%dn",icmp_hdr->un.echo.sequence);
}
패킷을 보내면 잡아서 출력해야 하는데 메시지를 받는중 이라고만 돌고 메시지 출력이 안되요
뭘 잘못한 거 같은데.. 어떤게 실수인지 모르겠습니다 ㅠㅠ
받는 쪽의 port 번호 설정은 어떻게 하셨나요?
받는 쪽의 port 번호 설정은 어떻게 하셨나요?
구글 검색해 보면 받는 예제가 있을 겁니다. 거기서부터 시작해 보세요.
받는 쪽 PORT 설정을 따로 해야하나요?
따로 안했는데 저 상태로도 원래 받을 수 있지 않나요.. 뭐가 문젠지 진짜 모르겠어서
여길 한번 참고해 보세요.
여길 한번 참고해 보세요.
https://stackoverflow.com/questions/4888285/send-an-udp-packet-and-receive-an-icmp-response-from-router-in-c
댓글 달기