2013-04-10 4 views
0

패키지를 보내고받는 프로그램을 작성하고 있습니다. ICMP 프로토콜을 통해 데이터를 전송하는 데는 문제가 없지만 호스트 IP 나 ICMP 코드와 같은 정보는 거의 얻지 못하는 문제가 있습니다.받은 패키지의 ICMP에 대한 정보를 얻는 방법은 무엇입니까?

코드 8 ('에코 요청') (작동)과 내 컴퓨터에서 코드 0 ('에코 응답') 또는 코드 11 ('시간 초과')으로 패키지를 보냅니다. Wireshark에서 확인 했어.

받은 패키지에서 ICMP에 관한 정보를 얻는 방법을 모르겠습니다. 내 프로그램의 일부 :

socklen_t addrlen = sizeof(connection); 
if (recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen) == -1) { 
    perror("recv"); 
} else { 
    ip_reply = (struct iphdr*) buffer; 
    printf("ID: %d\n", ntohs(ip_reply->id)); 
    printf("TTL: %d\n", ip_reply->ttl); 
} 

수신 된 호스트의 IP 및 ICMP 코드에 대한 정보를 갖고 싶습니다.

'iphdr'구조 안에 'saddr'및 'daddr'이라는 필드가 있다는 것을 알고 있습니다. 그러나 '_be32'유형이 있습니다. 나는 그것을 'char *'로 변환하는 방법을 모른다. 사전 :

답변

1
#include <netinet/ip_icmp.h> 

... 
... 
... 
your recvfrom 
... 

// first, verify the ip packet contains a ICMP 
if (ip_reply->protocol == 1) 
{ 
    // ok, it contains ICMP data 
    printf("Received ICMP message from IP: %s\n", inet_ntoa(connection.sin_addr)); 

    // make the icmphdr struct point to the right offset 
    struct icmphdr *icmp = (struct icmphdr *)((uint32_t *)ip_reply + ip_reply->ihl); 

    // print what you want 
    printf("ICMP type: %u\n", icmp->type); 
    printf("ICMP code: %u\n", icmp->code); 
} 
+0

에서

감사 : – Simon

+0

국제 인도법로 교체 –

+0

덕분에 많은 "오류는 '구조체 iphdr이'라는 이름의 멤버는 'hdrlen 없다'있다!" 그것은 작동합니다! :) – Simon