我正在用C linux实现原始套接字。我是套接字编程的新手,所以在用于Internet地址的数据类型方面有一些问题。
我想知道在下面的代码中,ip_addres(在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;
}发布于 2014-04-17 19:07:16
是的,你是对的。您可以返回指向源ip地址的指针,并且目标ip地址是ip报头中的源ip地址的4个字节(32位)。因此,您可以在源ip地址的地址上加上4,得到目的ip地址。
char *src_ip;
src_ip =解析器(packet_buffer,len);
CreateIPHeader(src_ip,src_ip+4);
https://stackoverflow.com/questions/23130371
复制相似问题