发送icmp原始套接字

时间:2015-06-13 13:23:34

标签: c sockets networking icmp raw-sockets

所以我得到了关于编程整个TCP / IP堆栈(包括ARP和ICMP)的任务。到目前为止,我被困在ICMP。根据Wireshark的说法,我发送的每个数据包似乎都是格式错误的(IP标头不是,只有ICMP)。

我不能使用现有的结构,所以我必须自己创建一些。

这是我的icmp_header_t结构:

typedef struct {
   uint8_t type;
   uint8_t code;
   uint16_t checksum;
   } icmp_header_t;

我认为这可能与我的校验和计算有关。 这是校验和计算:

uint16_t csum (uint16_t *buffer, int length){
    unsigned long sum;

        // initialize sum to zero and loop until length (in words) is 0
        for (sum=0; length>1; length-=2) // sizeof() returns number of bytes, we're interested in number of words
            sum += *buffer++;   // add 1 word of buffer to sum and proceed to the next

        // we may have an extra byte
        if (length==1)
            sum += (unsigned char)*buffer;

        sum = (sum >> 16) + (sum & 0xFFFF);  // add high 16 to low 16
        sum += (sum >> 16);          // add carry
        return (uint16_t) ~sum;
}

这就是我构建标题的方式: construct_icmp_header(icmp_head, ICMP_ECHO, 0, 0);

我如何计算校验和:icmp_head->checksum = csum((uint16_t *) icmp_head, sizeof(icmp_header_t));

这是我的一个icmp数据包的hexdump:

一个是正确的:

有人会对此有所了解吗?

编辑:所以我解决了。这是一个缺少的领域,淬火(现在由id和seq取代)。

0 个答案:

没有答案
相关问题