Linux网络

2019-12-11 转载TCP/IP编程常用C语言头文件

2020-02-06  本文已影响0人  阿群1986

网络编程的头文件(这里所有的头文件都在/usr/include目录下面)

经常被一些头文件搞大,不知到到哪个头文件去找结构。这里做个总结

————————————————
版权声明:本文为CSDN博主「雷锋不谢」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012566181/article/details/38562475

网络编程的头文件(这里所有的头文件都在/usr/include目录下面)
经常被一些头文件搞大,不知到到哪个头文件去找结构。这里做个总结。

IPv4头部

Linux平台下有两个IPv4结构体,分别是

TCP头部

两个

UDP头部

ICMP头部

ARP头部

MAC头

其他

值得说明的是,netinet/if_ether.h中定义了struct ether_arp ,它是带有MAC头的整个arp包的定义。

#include <net/if.h>
#define IF_NAMESIZE 16
unsigned int if_nametoindex(const char *ifname);
char *if_indextoname(unsigned int ifindex, char *ifname);
struct ifreq ifr;

strcpy(ifr.ifr_name, "eth0");
ioctl(sockfd, SIOCGIFINDEX, &ifr);
#include <linux/if_packet.h>
struct sockaddr_ll{
    unsigned short sll_family; /* = AF_PACKET */
    unsigned short sll_protocol; /* 物理层的协议 */
    int sll_ifindex; /* 接口号 */
    unsigned short sll_hatype; /* 报头类型 */
    unsigned char sll_pkttype; /* 分组类型 */
    unsigned char sll_halen; /* 地址长度 */
    unsigned char sll_addr[8]; /* 物理层地址 */
};

以上就是各个数据包的头部格式,这在编写构造自己的包时候很有用。
另外,还有些头文件。

#include <linux/udp.h>
//udp
struct udphdr
{
    u_int16_t source;
    u_int16_t dest;
    u_int16_t len;
    u_int16_t check;
};
 
//........................................................................
#include <linux/icmp.h>
struct icmphdr
{
    u_int8_t type;//0--reques,8---reply,11----timeout;
    u_int8_t code;
    u_int16_t checksum;
    ........
};
..........................................................................
#include <linux/ip.h>
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;
    __be16    tot_len;
    __be16    id;
    __be16    frag_off;
    __u8    ttl;
    __u8    protocol; // 1--icmp 6---tcp 17---udp
    __sum16    check;
    __be32    saddr;
    __be32    daddr;
    /*The options start here. */
};

//.........................................................................
#include <linux/tcp.h>
struct tcphdr {
    __be16    source;
    __be16    dest;
    __be32    seq;
    __be32    ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u16    res1:4,
        doff:4,
        fin:1,
        syn:1,
        rst:1,
        psh:1,
        ack:1,
        urg:1,
        ece:1,
        cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
    __u16    doff:4,
        res1:4,
        cwr:1,
        ece:1,
        urg:1,
        ack:1,
        psh:1,
        rst:1,
        syn:1,
        fin:1;
#else
#error    "Adjust your <asm/byteorder.h> defines"
#endif    
    __be16    window;
    __sum16    check;
    __be16    urg_ptr;
}
 
//.........................................................................
#include <linux/if_ether.h>
struct ethhdr
{
    unsigned char h_dest[6];
    unsigned char h_source[6];
    __be16       h_proto;//0x0806---arp,0x0800--ip,0x0835---rarp.
}

//.........................................................................
#include <linux/if_arp.h>
struct arphdr
{
    unsigned short hard_type;//硬件类型,通常0x0001(以太网)
    unsigned short protocol;//协议类型,通常0x0800(IP)
    unsigned char hard_length;
    .....
    unsigned short operation_type;//操作类型,1,ARP请求,2,ARP应答
    //参考http://baike.baidu.com/view/121586.htm?fr=aladdin
}

————————————————
版权声明:本文为CSDN博主「雷锋不谢」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012566181/article/details/38562475

上一篇 下一篇

猜你喜欢

热点阅读