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

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

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

————————————————

版权声明:本文为CSDN博主「雷锋不谢」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/u012566181/article/details/38562475

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

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

IPv4头部

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

netinet/ip.h 中定义了两套结构体,分别是struct iphdr 和 struct ip;

linux/ip.h 中定义的结构体struct iphdr;

这三个都是一样的,看个人喜好,我个人喜换netinet/ip.h下面的struct iphdr;

TCP头部

两个

netinet/tcp.h 中定义的结构体 struct tcphdr;

linux/tcp.h 中定义的结构体 struct tcphdr;

任选,不过网络编程的C语言开发者一般在netinet这个地下选,只有内核开发者才去用linux目录。

UDP头部

netinet/udp.h struct udphdr;

linux/udp.h struct udphdr;

ICMP头部

netinet/ip_icmp.h struct icmphdr

linux/icmp.h struct icmphdr

ARP头部

net/if_arp.h struct arphdr // 注意:不是netinet目录了,改成net/if_arp.h!

linux/if_arp.h struct arphdr

MAC头

linux/if_ether.h 中定义了结构体 struct ethhdr

netinet/if_ether.h

原则上在netinet/if_ether.h 文件中没有定义struct ethhdr,但是,在这个文件中,它引用了上面的文件linux/if_ether.h,所以也可也引用这个文件来定义 struct ethhdr.这样就统一了。

其他

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

#include

#define IF_NAMESIZE 16

unsigned int if_nametoindex(const char *ifname);

char *if_indextoname(unsigned int ifindex, char *ifname);

if_nametoindex()查询name字符串"eth0""eth1"对应的interface index序号

if_indextoname()根据ifindex序号查询字符串名称

struct ifreq ifr;

strcpy(ifr.ifr_name, "eth0");

ioctl(sockfd, SIOCGIFINDEX, &ifr);

#include

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]; /* 物理层地址 */

};

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

另外,还有些头文件。

netinet/in.h 定义了ip地址结构 struct sockaddr_in (struct in_addr作为sockaddr_in 的成员) 还有函数 htons等,以及一些宏。

在编写用户应用程序时,是少不了这个头文件的(因为要构造ip地址呀)

arpa/inet.h 提供IPv4/v6地址转换函数 inet_ntoa() inet_aton() inet_pton() inet_ntop() 等等。

sys/socket.h 定义了一些列的socket API函数 如 socket(),bind() listen() , send() ,sendmsg(),setsockopt(),等等。所以这个头文件和netinet/in.h头文件是用户程序不可缺少的。

......................................................................

#include

//udp

struct udphdr

{

u_int16_t source;

u_int16_t dest;

u_int16_t len;

u_int16_t check;

};

//........................................................................

#include

struct icmphdr

{

u_int8_t type;//0--reques,8---reply,11----timeout;

u_int8_t code;

u_int16_t checksum;

........

};

..........................................................................

#include

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 "

#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

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 defines"

#endif

__be16 window;

__sum16 check;

__be16 urg_ptr;

}

//.........................................................................

#include

struct ethhdr

{

unsigned char h_dest[6];

unsigned char h_source[6];

__be16 h_proto;//0x0806---arp,0x0800--ip,0x0835---rarp.

}

//.........................................................................

#include

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值