ICMP数据包的自定义IP标头不起作用

时间:2017-07-03 21:07:35

标签: c sockets networking ip raw-sockets

我正在尝试使用自定义IP标头制作ICMP数据包 当我禁用IP_HDRINCL以使用默认IP标头并删除所有与IP标头相关的代码时,它可以工作(我检查了下面代码的校验和以及使用默认标头的代码,ICMP数据包肯定是有效的在下面的代码中)。

当尝试使用我自己的IP头时出现问题,我没有收到任何ICMP数据包,表明数据包没有正确传输或出现问题。

我正在使用Ubuntu 16.04并使用标记-std=c11 -Wall -Wextra -pedantic

编译GCC
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>

#define PACKETSIZE 256

// Packet struct
struct packet {
    struct iphdr ip; // IP Header
    struct icmphdr hdr; // ICMP Header
    char msg[PACKETSIZE - sizeof(struct icmphdr) - sizeof(struct iphdr)]; // Message
};

// Checksum function
unsigned short checksum(void *b, int len) { 
    unsigned short *buf = b;
    unsigned int sum = 0;
    unsigned short result;

    for (sum = 0; len > 1; len -= 2) {
        sum += *buf++;
    }
    if (len == 1) sum += *(unsigned char*)buf;

    sum = (sum >> 16) + (sum & 0xFFFF);
    sum += (sum >> 16);
    result = ~sum;

    return result;
}

int main(int argc, char* argv[])
{   
    // Enough arguments
    if (argc < 2) {
        printf("usage: ./tracert <server>\n");
        return EXIT_FAILURE;
    }

    // Variables
    struct hostent *hname;
    struct sockaddr_in addr;
    unsigned int i;
    int sockfd, seq = 1;
    struct packet pckt;
    socklen_t len;
    char buf[1024];

    // Get host from domain
    hname = gethostbyname(argv[1]);
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = hname->h_addrtype;
    addr.sin_port = htons(6969);
    addr.sin_addr.s_addr = *(long *)hname->h_addr_list[0]; 

    // Create ICMP RAW socket
    sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if (sockfd < 0) {
        printf("error on socket creation\n");
        return EXIT_FAILURE;;
    }

    if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &seq, sizeof(seq)) < 0) {
        printf("error on default ip header settings\n");
        return EXIT_FAILURE;
    }

    // Loop and receive/send packets
    while (1) {
    // Make packet
        memset(&pckt, 0, sizeof(pckt));

        // IP Header
        pckt.ip.version = 4;
        pckt.ip.ihl = 5;
        pckt.ip.tot_len = htons(sizeof(pckt));
        pckt.ip.ttl = 255;
        pckt.ip.protocol = IPPROTO_ICMP;
        pckt.ip.saddr = inet_addr("192.168.1.1");
        pckt.ip.daddr = addr.sin_addr.s_addr;
        pckt.ip.check = checksum(&pckt, sizeof(struct iphdr));

        // ICMP Header
        pckt.hdr.type = ICMP_ECHO;
        pckt.hdr.un.echo.id = 0;
        for (i = 0; i < sizeof(pckt.msg) - 1; i++) {
            pckt.msg[i] = i+'0';
        }
        pckt.msg[i] = 0;
        pckt.hdr.un.echo.sequence = seq;
        pckt.hdr.checksum = checksum(&pckt.hdr, sizeof(struct icmphdr) + sizeof(pckt.msg));

        // Send packet
        if (sendto(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr, sizeof(addr)) <= 0) {
            printf("error on sending packet\n");
            return EXIT_FAILURE;
        }

        // Receive packet
        len = sizeof(addr);
        memset(buf, 0, sizeof(buf));

        if (recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len) > 0) {
            printf("packet received\n");
            // Do more stuff here later...
        }

        // The while is there for later, for now I just want to send one packet
        return EXIT_SUCCESS;
    }

    close(sockfd);
    return EXIT_SUCCESS;
}

0 个答案:

没有答案