тема закрыта, даную проблему решил через libpcap
хотя почему я не мог отлавливать ICMP пакеты в предыдущем коде, я так и не понял
#include <pcap.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <netinet/tcp.h>
#include <ctype.h>
struct my_ip {
u_int8_t ip_vhl;
#define IP_V(ip) (((ip)->ip_vhl & 0xf0) >> 4)
#define IP_HL(ip) ((ip)->ip_vhl & 0x0f)
u_int8_t ip_tos;
u_int16_t ip_len;
u_int16_t ip_id;
u_int16_t ip_off;
#define IP_DF 0x4000
#define IP_MF 0x2000
#define IP_OFFMASK 0x01fff
u_int8_t ip_ttl;
u_int8_t ip_p;
u_int16_t ip_sum;
struct in_addr ip_src, ip_dst;
};
typedef struct _ihdr {
char i_type;
char i_code;
unsigned short i_cksum;
unsigned short i_id;
unsigned short i_seq;
unsigned long timestamp;
}IcmpHeader;
typedef struct _icmpdata {
char operation;
long port;
} IcmpData;
#define HEXDUMP_BYTES_PER_LINE 16
#define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
#define HEXDUMP_HEXSTUFF_PER_SHORT 5
#define HEXDUMP_HEXSTUFF_PER_LINE (HEXDUMP_HEXSTUFF_PER_SHORT*HEXDUMP_SHORTS_PER_LINE)
u_short handle_ethernet(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
struct ether_header *eth;
eth=(struct ether_header *) packet;
return ntohs(eth->ether_type);
}
void handle_ip(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
const struct my_ip* ip;
IcmpData *icmpdata;
IcmpHeader *icmphdr;
char ipport[20];
int port;
ip=(struct my_ip *)(packet+sizeof(struct ether_header));
if (ip->ip_p==IPPROTO_ICMP)
{
icmphdr = (IcmpHeader*)(packet + 20 + sizeof(struct ether_header));
icmpdata = (IcmpData*)(packet + sizeof(IcmpHeader) + 20 + sizeof(struct ether_header));
if (icmpdata->operation=='S')
{
sprintf(ipport, "%s:%i\0", inet_ntoa(ip->ip_src), icmpdata->port);
printf("%s\n",ipport);
}
}
}
void callback(u_char *args, const struct pcap_pkthdr* pkthdr, const u_char* packet)
{
u_int16_t etype=handle_ethernet(args, pkthdr, packet);
if(etype==ETHERTYPE_IP) handle_ip(args, pkthdr, packet);
}
int main(int argc, char *argv[])
{
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct bpf_program fp;
struct in_addr addr;
bpf_u_int32 maskp;
bpf_u_int32 netp;
if((dev=pcap_lookupdev(errbuf))==NULL)
{
printf("%s\n", errbuf); exit(1);
}
pcap_lookupnet(dev, &netp, &maskp, errbuf);
if((descr=pcap_open_live(dev, BUFSIZ, 1, 0, errbuf))==NULL)
{
printf("pcap_open_live(): %s\n", errbuf); exit(1);
}
if(pcap_compile(descr, &fp, argv[2], 0, netp)==-1)
{
printf("Error pcap_compile()\n"); exit(1);
}
if(pcap_setfilter(descr, &fp)==-1)
{
printf("Error pcap_setfilter\n"); exit(1);
}
printf("DEV: %s\n", dev);
addr.s_addr=netp;
printf("NET: %s\n", inet_ntoa(addr));
addr.s_addr=maskp;
printf("MASK: %s\n", inet_ntoa(addr));
pcap_loop(descr, -1, callback, NULL);
pcap_close(descr);
return 0;
}