порт = 1, IP = 127.0.0.1, интерфейс = enp0s3. Здравствуйте, друзья, моя проблема в том, что funkcion pcap_l oop () не хочет работать, я не понимаю b c, если я изменяю IP на 127.0.0.0 и интерфейс на 'lo' позже функционирует нормально. Я думаю, что ошибка здесь
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"