我正在 C linux 中实现原始套接字。我是套接字编程新手,因此对用于 Internet 地址的数据类型有一些问题。
我想知道下面的代码中 ip_addres(in main) 的数据类型应该是什么。我认为它需要是指针,因为我需要返回两个地址。然后这两个地址将被传递到下一个函数中,如下所示。
main()
{
int len,raw_socket;
struct iphdr *ip_header;
unsigned char *packet_buffer[2048];
len=recvfrom(raw_socket,packet_buffer,2048,...);
ip_addres=parseipheader(packet_buffer,len); /*I want this function to return ip address of destination and source*/
ip_header=CreateIPHeader(source_ip,destination_ip);
}
parseipheader(unsigned char *packet,int len)
{
struct iphdr *ip_header;
ip_header=(struct ip_header *)(packet+sizeof(struct ethhdr));
return ip_addresses;
}
struct iphdr* CreateIPHeader(source_ip,destination_ip)
{
struct iphdr *ip_header;
return ip_header;
}
最佳答案
是的,你是对的。您可以返回指向源 IP 地址的指针,目标 IP 地址是 ip header 中源 IP 地址的 4 个字节(32 位)。所以你可以在源ip地址的基础上加上4来得到目的ip地址。
char *src_ip;
src_ip = parseipheader(packet_buffer,len);
CreateIPHeader(src_ip,src_ip+4);
关于c - 在C语言中解析原始套接字编程中的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130371/