【问题标题】:parsing ip address in raw socket programming in C在C中的原始套接字编程中解析IP地址
【发布时间】:2014-06-01 13:28:20
【问题描述】:

我正在 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;
}

【问题讨论】:

    标签: c linux sockets ip-address


    【解决方案1】:

    是的,你是对的。您可以返回指向源 IP 地址的指针,目标 IP 地址是 ip header 中源 IP 地址的 4 个字节(32 位)。所以你可以在源IP地址的地址上加4得到目的IP地址。

    char *src_ip;

    src_ip = parseipheader(packet_buffer,len);

    创建IPHeader(src_ip,src_ip+4);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2011-02-04
      • 2011-10-01
      • 1970-01-01
      相关资源
      最近更新 更多