Question

In C I bind a datagram socket (AF_INET, SOCK_DGRAM) to INADDR_ANY. I then periodically use this socket to send and receive datagrams, and monitor the flow of packets with pcap. The problem is, I can't tell whether a packet is incoming or outgoing using pcap.

The transmission/receiving and the pcap monitoring are running in separate threads, and for synchronisation reasons they can't communicate. I only want to track the incoming packets, not the ones being sent, so does anyone have an idea as to how I can do that?

I thought already of testing the destination ip address, but I can't figure out any way to get my local ip. the machine this is running on doesn't have a static ip, much less an assigned domain name, and it seems that getsockname doesn't work on sockets bound to INADDR_ANY. Also tried using ioctl(sockfd, SIOCGIFCONF, &buffer), which didn't work either - sets buffer.ifc_len=0.

Was it helpful?

Solution

Found a solution. I can get my own ip using this:

char *command = malloc(100);

sprintf(command,"ifconfig %s|grep -o \"inet addr:[^ ]\"|grep -o -e \"[0-9]\.[0-9]\.[0-9]\.[0-9]*\"",device);

char path[1035];

FILE *fp;

fp = popen(command,"r");

fgets(path, 1034, fp);

pclose(fp);

my_ip = malloc(sizeof(char)*(1+strlen(path)));

memcpy(my_ip, path, strlen(path)-1);

my_ip[strlen(path)-1] = 0;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top