Question

I have a very simple code below. When i run it on Debian 7, this code always sends all packets via 127.0.0.1 interface. But i want it sends it via 192.168.0.103. Where am i wrong?

I searched the solution in Google until past 16 hours but didn't find the answer :(.

Sure, i can fix it via routing table. But ping and traceroute works perfect. Why my code doesn't work?

int rawSocket;

char buffer[PACKET_LENGTH];

struct IPHeader *ipHeader = (struct IPHeader *) buffer;
struct UDPHeader *udpHeader = (struct UDPHeader *) (buffer + sizeof(struct IPHeader));

struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;

memset(buffer, 0, PACKET_LENGTH);

rawSocket = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (rawSocket < 0) {
    printf("socket() error");
    exit(-1);
}

sin.sin_family = AF_INET;
din.sin_family = AF_INET;

sin.sin_port = htons(atoi(srcPort));
din.sin_port = htons(atoi(dstPort));

sin.sin_addr.s_addr = inet_addr(realSrcIP);
din.sin_addr.s_addr = inet_addr(dstIP);

ipHeader->iph_ihl = 5;
ipHeader->iph_ver = 4;
ipHeader->iph_tos = 16; // Low delay
ipHeader->iph_len = sizeof(struct IPHeader) + sizeof(struct UDPHeader);
ipHeader->iph_ident = htons(54321);
ipHeader->iph_ttl = 64; // hops
ipHeader->iph_protocol = 17; // UDP
// spoof please lol =P
ipHeader->iph_sourceip = inet_addr(srcIP);
ipHeader->iph_destip = inet_addr(dstIP);

udpHeader->udph_srcport = htons(atoi(srcPort));
udpHeader->udph_destport = htons(atoi(dstPort));
udpHeader->udph_len = htons(sizeof(struct UDPHeader));
ipHeader->iph_chksum = csum((unsigned short *)buffer, sizeof(struct IPHeader) + sizeof(struct UDPHeader));

if(setsockopt(rawSocket, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
    perror("setsockopt() error");
    exit(-1);
}

struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
if (setsockopt(rawSocket, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
    printf("HUJ\n");
}

int errorCode = sendto(rawSocket, buffer, ipHeader->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin));
if(errorCode < 0)
{
    perror("sendto() error");
    exit(-1);
}

close(rawSocket);
Was it helpful?

Solution

I don't think you're setting ifr correctly.

You need to call ioctl(rawsocket, SIOCGIFINDEX, &ifr) to set the index of the device. The name is not enough. Your index is left as zero which is the first device which happens to be loopback.

Here's a snippet of code from C Language Examples of IPv4 and IPv6 Raw Sockets for Linux:

// Use ioctl() to look up interface index which we will use to
// bind socket descriptor sd to specified interface with setsockopt() since
// none of the other arguments of sendto() specify which interface to use.
memset (&ifr, 0, sizeof (ifr));
snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
if (ioctl (sd, SIOCGIFINDEX, &ifr) < 0) {
   perror ("ioctl() failed to find interface ");
   return (EXIT_FAILURE);
}

There may be other problems (your code is too big) try running/modifying the examples in the link above. Hopefully they are not outdated.

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