我基本上创建了一个简单的程序,它在其中创建自定义 UDP 数据包并将其发送到目标 IP 地址。
这是代码 UDPraw.c:
// ----rawudp.c------
// Must be run by root lol! Just datagram, no payload/data
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
// The packet length
#define PCKT_LEN 8192
// Can create separate header file (.h) for all headers' structure
// The IP header's structure
struct ipheader {
unsigned char iph_ihl:5, iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned char iph_flag;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
// UDP header's structure
struct udpheader {
unsigned short int udph_srcport;
unsigned short int udph_destport;
unsigned short int udph_len;
unsigned short int udph_chksum;
};
// total udp header length: 8 bytes (=64 bits)
// Function for checksum calculation. From the RFC,
// the checksum algorithm is:
// "The checksum field is the 16 bit one's complement of the one's
// complement sum of all 16 bit words in the header. For purposes of
// computing the checksum, the value of the checksum field is zero."
unsigned short csum(unsigned short *buf, int nwords)
{ //
unsigned long sum;
for(sum=0; nwords>0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum &0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[]){
int sd;
// No data/payload just datagram
char buffer[PCKT_LEN];
// Our own headers' structures
struct ipheader *ip = (struct ipheader *) buffer;
struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));
// Source and destination addresses: IP and port
struct sockaddr_in sin, din;
int one = 1;
const int *val = &one;
memset(buffer, 0, PCKT_LEN);
if(argc != 5){
printf("- Invalid parameters!!!\n");
printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(-1);
}
// Create a raw socket with UDP protocol
sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if(sd < 0){
perror("socket() error");
// If something wrong just exit
exit(-1);
} else
printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
// The source is redundant, may be used later if needed
// The address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// Port numbers
sin.sin_port = htons(atoi(argv[2]));
din.sin_port = htons(atoi(argv[4]));
// IP addresses
sin.sin_addr.s_addr = inet_addr(argv[1]);
din.sin_addr.s_addr = inet_addr(argv[3]);
// Fabricate the IP header or we can use the
// standard header structures but assign our own values.
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16; // Low delay
ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
ip->iph_ident = htons(54321);
ip->iph_ttl = 64; // hops
ip->iph_protocol = 17; // UDP
// Source IP address, can use spoofed address here!!!
ip->iph_sourceip = inet_addr(argv[1]);
// The destination IP address
ip->iph_destip = inet_addr(argv[3]);
// Fabricate the UDP header. Source port number, redundant
udp->udph_srcport = htons(atoi(argv[2]));
// Destination port number
udp->udph_destport = htons(atoi(argv[4]));
udp->udph_len = htons(sizeof(struct udpheader));
// Calculate the checksum for integrity
ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader));
// Inform the kernel do not fill up the packet structure. we will build our own...
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0){
perror("setsockopt() error");
exit(-1);
} else
printf("setsockopt() is OK.\n");
// Send loop, send for every 2 second for 100 count
printf("Trying...\n");
printf("Using raw socket and UDP protocol\n");
printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));
int count;
for(count = 1; count <=20; count++){
if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0){
perror("sendto() error");
exit(-1);
} else {
printf("Count #%u - sendto() is OK.\n", count);
sleep(2);
}
}
close(sd);
return 0;
}
仅供引用:我以 root 用户身份登录。
在编译代码时,我只是收到这些警告:
custompacket.c: In function ‘main’:
custompacket.c:69:5: warning: incompatible implicit declaration of built-in function ‘memset’ [enabled by default]
custompacket.c:73:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
custompacket.c:82:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
custompacket.c:128:6: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
custompacket.c:139:20: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
custompacket.c:143:10: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
除此之外它编译得很好。
现在我基本上做的是将程序运行为 ./rawtcp [源地址] [源端口] [目标地址] [目标端口]
我也运行了wireshark来嗅探数据包,但是它没有出现。否则,如果我做一个简单的 ping。 ping echo 请求和回复会在wireshark 中弹出。
如果您能告诉我我在这里做错了什么,我将非常感激。
我是否使用 write()
其他 sendto()
以及如何使用?
非常感谢所有回复:)
最佳答案
首先,由于缺少 header 而引起的警告在这里大多无关紧要,尽管包含正确的 header 文件以获得正确的函数签名始终很重要,否则在依赖默认值时可能会导致更微妙的错误隐式函数声明。
在您的struct ipheader
中,存在一些问题。首先,iph_ihl
的位字段大小应该是4
,而不是5
,因为IP头的版本和IHL字段都是4位长的。这可能只是一个错字。
其次,您在结构中包含 8 位的 iph_flag
和 16 位的 iph_offset
。然而,这两个字段都应位于 IP header 中相同的 16 位内。标志字段应为 3 位,分段偏移字段应为 13 位。由于在您的代码中,您将这些保留为 memset
中的默认归零值,因此您可以从结构中完全删除 flags
字段,而只拥有 16 位 iph_offset
字段。
另一个问题是您对这种逐字节数据打包的结构的一般使用。 C 编译器可以自由地在结构体字段之间添加任何他们喜欢的填充,以实现数据对齐目的。因此,要以这种方式使用结构并具有任何可靠性,您必须依赖于特定于编译器的扩展,特别是 GCC 和 Clang 支持的 #pragma pack
。
/* Set alignment of all fields in the struct to 1, to pack them as tightly as
* possible with no padding. */
#pragma pack(push, 1)
struct ipheader {
/* ... */
};
struct udpheader {
/* ... */
};
/* Reset back to normal. */
#pragma pack(pop)
值得注意的是,结构中位域的对齐/填充的确切行为也是实现定义的,但是它确实按照您在我的带有 GCC 的 Linux 机器上的程序的预期工作,并且可能在很多情况下工作设置。
除此之外,这一行还有另一个错误:
ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
与 header 中的其他 16 位字段一样,您应该使用 htons()
设置它们,以在小端机器上将它们转换为大端:
ip->iph_len = htons(sizeof(struct ipheader) + sizeof(struct udpheader));
iph_chksum
也是如此:
ip->iph_chksum = htons(csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader)));
通过这些修复,数据包确实按照我的计算机上的预期显示在 Wireshark 中,但值得注意的是,您没有设置 UDP header 的校验和。
结构体方法的替代方法是使用unsigned char
数组,然后通过位移位和其他位操作操作手动填写字段。这样,您甚至不必使用 htons 来交换字节顺序,因为无论如何您都将手动填充每个字节。例如:
unsigned char ip_header[160];
ip_header[0] = ihl & (ip_ver << 4);
ip_header[2] = (unsigned char) (ip_len >> 8);
ip_header[3] = (unsigned char) (ip_len & 0xff);
关于c - 为什么 Sendto() 不在 c 中发送数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48322157/