简体   繁体   English

为什么原始套接字将所有数据包发送回环?

[英]Why raw socket sends all packets to loopback?

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. 当我在Debian 7上运行它时,此代码始终通过127.0.0.1接口发送所有数据包。 But i want it sends it via 192.168.0.103. 但我希望它通过192.168.0.103发送它。 Where am i wrong? 我哪里错了?

I searched the solution in Google until past 16 hours but didn't find the answer :(. 直到过去16个小时,我都在Google中搜索了该解决方案,但没有找到答案:(。

Sure, i can fix it via routing table. 当然,我可以通过路由表对其进行修复。 But ping and traceroute works perfect. 但是ping和traceroute可以完美地工作。 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);

I don't think you're setting ifr correctly. 我认为您没有正确设置ifr

You need to call ioctl(rawsocket, SIOCGIFINDEX, &ifr) to set the index of the device. 您需要调用ioctl(rawsocket, SIOCGIFINDEX, &ifr)来设置设备的索引。 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 : 这是Linux的IPv4和IPv6原始套接字的C语言示例的代码片段:

// 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. 希望他们不会过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM