在IP = 127.0.0.1和interface ='enp0s3'上创建过滤器的问题

问题描述 投票:0回答:1

端口= 1,IP = 127.0.0.1,接口= enp0s3。您好朋友,我的问题是funkcion pcap_loop()不想工作,如果我更改127.0.0.0上的IP并在“ lo”接口上连接IP,则以后funkcion不能正常工作。我认为错误在这里

char filter_exp[40];
sprintf(filter_exp, "port %d", port)

但我不确定。如果您能帮助我,我将不胜感激。

int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);

    if ( sock == -1 )
        {
                printf("Error while creating socket\n");
                exit(-1);
        }

    char datagram[4096] , source_ip[32] , *data , *pseudogram;
    memset (datagram, 0, 4096);

    struct iphdr *iph = (struct iphdr *) datagram;

    struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct iphdr));
    struct sockaddr_in sin;
    struct pseudo_header psh;

    data = datagram + sizeof(struct iphdr) + sizeof(struct tcphdr);
    strcpy(data , "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    strcpy(source_ip , IP);
    sin.sin_family = AF_INET;
        sin.sin_port = htons(port);
        sin.sin_addr.s_addr = inet_addr(IP);

    iph->ihl = 5;
    iph->version = 4;
    iph->tos = 0;
    iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr) + strlen(data);
    iph->id = htonl(54321);
    iph->frag_off = 0x00;
    iph->ttl = 0xFF;
    iph->protocol = IPPROTO_TCP;
    iph->check  = 0;
    iph->saddr = inet_addr(IP);
    iph->daddr = sin.sin_addr.s_addr;

    iph->check = csum((unsigned short *) datagram, iph->tot_len);

    tcph->source = htons(1234);
    tcph->dest = htons(port);
    tcph->seq = 0x0;
    tcph->ack_seq = 0x0;
    tcph->doff = 5;
    tcph->fin = 0;
    tcph->syn = 1;
    tcph->rst = 0;
    tcph->psh = 0;
    tcph->ack = 0;
    tcph->urg = 0;
    tcph->window = htons(155);
    tcph->check = 0;
    tcph->urg_ptr = 0;
    tcph->res1 = 0;
    tcph->cwr = 0;
    tcph->ece = 0;

    psh.source_address = inet_addr(IP);
    psh.dest_address = sin.sin_addr.s_addr;
    psh.placeholder = 0;
    psh.protocol = IPPROTO_TCP;
    psh.tcp_length = htons(sizeof(struct tcphdr) + strlen(data));

    int psize = sizeof(struct pseudo_header) + sizeof(struct tcphdr) + strlen(data);
    pseudogram = malloc(psize);

    memcpy(pseudogram , (char*) &psh , sizeof (struct pseudo_header));
    memcpy(pseudogram + sizeof(struct pseudo_header) , tcph , sizeof(struct tcphdr) + strlen(data));

    tcph->check = csum( (unsigned short*) pseudogram , psize);

    int one = 1;

    if ( setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one)) < 0 )
    {
        perror("Error while setting socket options");
        exit(1);
    }

    char errbuf[PCAP_ERRBUF_SIZE];
    struct bpf_program fp;
    char filter_exp[40];
    sprintf(filter_exp, "port %d", port);
    bpf_u_int32 mask;
    bpf_u_int32 net;

        if ( setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one)) < 0 )
        {
                perror("Error while setting socket options");
                exit(-1);
        }

    if ( pcap_lookupnet(interface->ifa_name, &net, &mask, errbuf) == -1 )
    {
        fprintf(stderr, "Can't get netmask for device %s\n", interface->ifa_name);
        net = 0;
        mask = 0;
    }
    handle = pcap_open_live(interface->ifa_name, BUFSIZ, 1, 1000, errbuf);
    if ( handle == NULL )
    {
        fprintf(stderr, "Couldn't open device %s: %s\n", interface->ifa_name, errbuf);
        exit(1);
    }
    if ( pcap_compile(handle, &fp, filter_exp, 0, net) == -1 )
    {
        fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
        exit(1);
    }
    if ( pcap_setfilter(handle, &fp) == -1 )
    {
        fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
        exit(1);
    }
    if ( sendto(sock, datagram, iph->tot_len, 0, (struct sockaddr *) &sin, sizeof(sin)) < 0 )
    {
        printf("Sending packet failed\n");
        exit(1);
    }

    printf("%d/tcp ", port);

    //Here crash
    pcap_loop(handle, 1, callBack, NULL);

    pcap_close(handle);
    printf("\n"
c pcap libpcap
1个回答
0
投票

“ enp0s3”是以太网接口,根据this question on a forum

您不应该从以太网接口上的127.0.0.1获得ANY流量;计算机将通过“环回”接口将流量发送给自身,该接口在Linux上是“ lo”。

© www.soinside.com 2019 - 2024. All rights reserved.