struct ip 和struct iphdr的差别 , <netinet/*.h>和<linux/*.h>

11 篇文章 0 订阅

(1)struct ip 和struct iphdr

struct ip {
#if BYTE_ORDER == LITTLE_ENDIAN 
    u_char  ip_hl:4,        /* header length */
        ip_v:4;         /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN 
    u_char  ip_v:4,         /* version */
        ip_hl:4;        /* header length */
#endif
    u_char  ip_tos;         /* type of service */
    short   ip_len;         /* total length */
    u_short ip_id;          /* identification */
    short   ip_off;         /* fragment offset field */
#define IP_DF 0x4000            /* dont fragment flag */
#define IP_MF 0x2000            /* more fragments flag */
    u_char  ip_ttl;         /* time to live */
    u_char  ip_p;           /* protocol */
    u_short ip_sum;         /* checksum */
    struct  in_addr ip_src,ip_dst;  /* source and dest address */
};
struct iphdr

struct iphdr {
    #if defined(__LITTLE_ENDIAN_BITFIELD)
        __u8    ihl:4,
                version:4;
    #elif defined (__BIG_ENDIAN_BITFIELD)
        __u8    version:4,
                ihl:4;
    #else
        #error  "Please fix <asm/byteorder.h>"
    #endif
         __u8   tos;
         __u16  tot_len;
         __u16  id;
         __u16  frag_off;
         __u8   ttl;
         __u8   protocol;
         __u16  check;
         __u32  saddr;
         __u32  daddr;
         /*The options start here. */
};

struct ip and struct iphdr are two different definitions of the same underlying structure, brought in from different places.

struct ip is defined in <netinet/ip.h>, which is a reasonably standard header on UNIX systems.

struct iphdr is defined in <linux/ip.h>. This header (and structure) are Linux-specific, and will not be present in other operating systems.

If you're not sure which one to use, use struct ip; code which uses this structure is more likely to be portable to non-Linux systems.


struct icmp and struct icmphdr are a messier situation:

  • <netinet/icmp.h> defines both struct icmp and struct icmphdr.
  • <linux/icmp.h> also defines struct icmphdr, with a similar structure (but, as usual, different field names) as the definition from <netinet/icmp.h>.

First: Don't include <linux/icmp.h> unless you have a very good reason. You cannot include both headers -- they will conflict -- and most software will expect the netinet definition.

Second: struct icmphdr is, as the name implies, the header. struct icmp defines the contents of a structured ICMP message, like a destination unreachable message.

(2)<netinet/*.h> 和 <linux/*.h>

The linux/*.h headers were really meant for internal kernel use and if Linux were being created today, these files would not even exist under /usr/include. But early on, a lot of the userspace libc (libc4 and libc5 at the time) relied on Linux headers to define types, constants, structures, etc. for use in userspace, so netinet/in.h contained just #include <linux/in.h> or similar, and the lovely tradition got started. Today the only headers in the linux tree that should be used for userspace apps are some things related to supporting specific hardware at a low level, like the Linux console, framebuffer, video4linux, etc.

In short, you should use netinet/in.h (the standard header specified by POSIX) and pretend you never saw linux/in.h. :-)













  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要经过TCP/IP协议栈发送802.1Q报文,需要在数据包中添加802.1Q VLAN标签,并将其发送到网络接口。以下是一个简单的示例程序,演示如何使用TCP/IP协议栈发送带有802.1Q VLAN标签的数据包: ```c++ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <arpa/inet.h> #include <net/ethernet.h> #include <net/if.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <netinet/udp.h> #include <netinet/ip_icmp.h> #include <linux/if_packet.h> #include <linux/if_ether.h> #include <linux/if_vlan.h> int main() { int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sock < 0) { perror("socket"); exit(1); } char ifname[IFNAMSIZ] = "eth0"; struct ifreq ifr; strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) { perror("ioctl"); exit(1); } struct sockaddr_ll addr; memset(&addr, 0, sizeof(addr)); addr.sll_family = AF_PACKET; addr.sll_ifindex = ifr.ifr_ifindex; // 构造802.1Q VLAN标签 struct vlan_tag { uint16_t tci; uint16_t ether_type; } __attribute__((packed)); struct vlan_tag vlan; vlan.tci = htons(0x100); vlan.ether_type = htons(ETH_P_IP); // 构造IP数据包 char ip_packet[ETH_FRAME_LEN]; struct iphdr *iph = (struct iphdr *)ip_packet; iph->version = 4; iph->ihl = 5; iph->tos = 0; iph->tot_len = htons(sizeof(struct iphdr) + sizeof(struct udphdr)); iph->id = htons(0); iph->frag_off = htons(0); iph->ttl = 64; iph->protocol = IPPROTO_UDP; iph->check = htons(0); iph->saddr = inet_addr("192.168.1.100"); iph->daddr = inet_addr("192.168.1.1"); // 构造UDP数据包 char udp_packet[ETH_FRAME_LEN]; struct udphdr *udph = (struct udphdr *)udp_packet; udph->source = htons(1234); udph->dest = htons(5678); udph->len = htons(sizeof(struct udphdr)); udph->check = htons(0); // 将IP头和UDP数据拷贝到数据包中 memcpy(ip_packet + sizeof(struct iphdr), udph, sizeof(struct udphdr)); // 将802.1Q VLAN标签和IP/UDP数据拷贝到数据包中 char packet[ETH_FRAME_LEN]; memcpy(packet, "\xff\xff\xff\xff\xff\xff", ETH_ALEN); memcpy(packet + ETH_ALEN, "\x00\x11\x22\x33\x44\x55", ETH_ALEN); memcpy(packet + 2 * ETH_ALEN, &vlan, sizeof(struct vlan_tag)); memcpy(packet + 2 * ETH_ALEN + sizeof(struct vlan_tag), ip_packet, sizeof(struct iphdr) + sizeof(struct udphdr)); // 发送数据包 if (sendto(sock, packet, 2 * ETH_ALEN + sizeof(struct vlan_tag) + sizeof(struct iphdr) + sizeof(struct udphdr), 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("sendto"); exit(1); } close(sock); return 0; } ``` 该程序使用socket API创建了一个PF_PACKET类型的socket,并将其绑定到网络接口上。然后,程序构造了一个带有802.1Q VLAN标签的IP/UDP数据包,并将其封装成一个以太网帧。在构造802.1Q VLAN标签时,程序使用了vlan_tag结构体来定义标签格式,并将其添加到数据包的头部。最后,程序调用sendto函数将数据包发送到目标MAC地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值