赞
踩
udp是不可靠、无连接的协议,不可靠是指不能检查到数据包是否安全到达对端,但应用程序可以做保证数据包到达的机制,udp是无连接的协议说明udp的开销小、数据包传输效率高,如果传输的数据小,创建连接的开销、保证数据包可靠发送需要做的工作比数据本身还有多,那么udp是一种好的选择。udp协议头包含有四部分:
(1)、源端口:16位表示取值范围是1-65535。
(2)、目的端口:也是16位。
(3)、长度:长度是16位表示,指udp数据包的整体长度,udp数据包最小是8个字节,所以它能发送的最大负载长度是65535-8。
(4)、校验和:udp的校验和用16位表示,是检验协议头和负载数据。
1、UDP协议头数据结构
udp协议头结构体是struct udphdr,结构体元素包括:源端口、目的端口、udp报文整体长度、数据包校验和。结构体定义在include/linux/udp.h文件中。
- struct udphdr {
- __be16 source; //源端口
- __be16 dest; //目的端口
- __be16 len; //数据包长度
- __sum16 check; //校验和
- };
2、UDP控制缓冲区
在Socket BUffer的sk_buff结构体中有一个控制缓冲区,提供给tcp/udp协议头栈中各层协议存放私有数据,udp存放私有数据的结构体是struct udp_skb_cb,定义在include/net/udp.h中
- struct udp_skb_cb {
- union {
- struct inet_skb_parm h4; //ipv4
- #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
- struct inet6_skb_parm h6; //ipv6
- #endif
- } header;
- __u16 cscov; //udp校验和
- __u8 partial_cov; //udp部分校验和
- };
- //访问缓冲区
- #define UDP_SKB_CB(__skb) ((struct udp_skb_cb *)((__skb)->cb))
h4、h6:分别是ipv4、ipv6的选项信息。
cscov:整个udp数据包的校验和。
partial_cov:部分数据的校验和。
udp缓冲区只能通过UDP_SKB_CB宏来访问。
3、udp套接字结构体
udp套接字结构体是struct udp_sock是描述了udp协议的专业特性,struct udp_sock包含了struct inet_sock,struct inet_sock是所有AF_INET地址族域套接字专用数据结构。struct udp_sock在struct inet_sock的基础是扩展了udp数据包需要的全部管理、控制信息。
- struct udp_sock {
- /* inet_sock has to be the first member */
- struct inet_sock inet;
- #define udp_port_hash inet.sk.__sk_common.skc_u16hashes[0] //struct inet_sock的数据域
- #define udp_portaddr_hash inet.sk.__sk_common.skc_u16hashes[1]
- #define udp_portaddr_node inet.sk.__sk_common.skc_portaddr_node
- int pending; /* Any pending frames ? 当前是否有等待的数据包 */
- unsigned int corkflag; /* Cork is required 是否要阻塞套接字*/
- __u16 encap_type; /* Is this an Encapsulation socket? 是否是封装套接字*/
- /*
- * Following member retains the information to create a UDP header
- * when the socket is uncorked.
- */
- __u16 len; /* total length of pending frames 等待发送数据包的长度*/
- /*
- * Fields specific to UDP-Lite.
- */
- __u16 pcslen; //轻套接字等待发送的数据包长度
- __u16 pcrlen; //轻套接字等待接受的数据包长度
- /* indicator bits used by pcflag: */
- #define UDPLITE_BIT 0x1 /* set by udplite proto init function */
- #define UDPLITE_SEND_CC 0x2 /* set via udplite setsockopt */
- #define UDPLITE_RECV_CC 0x4 /* set via udplite setsocktopt */
- __u8 pcflag; /* marks socket as UDP-Lite if > 0 轻套接字标志 */
- __u8 unused[3];
- /*
- * For encapsulation sockets.
- */
-
- int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); //封装套接字的接受函数
- };

4、udp协议和套接字层的接口
udp协议和套接字层有接口结构是struct proto,定义在net/ipv4/udp.c中,主要是管理套接字和接受发送数据包的处理函数,udp接受数据包时要确定是把数据包分配给那个套接字,以便把数据包放入套接字的接受队列中提供用户读取。udp上所有打开的套接字由udp_v4_hash函数注册到struct sock *udp_hash[UDP_TABLE_SIZE]哈希链表中,端口号就是查询哈希表的哈希值。udp和套接字层的接口struct proto udp_prot定义在net/ipv4/udp.c文件中。
- struct proto udp_prot = {
- .name = "UDP",
- .owner = THIS_MODULE,
- .close = udp_lib_close, //关闭套接字
- .connect = ip4_datagram_connect, //初始化一个连接
- .disconnect = udp_disconnect, //断开套接字
- .ioctl = udp_ioctl,
- .destroy = udp_destroy_sock,
- .setsockopt = udp_setsockopt,
- .getsockopt = udp_getsockopt,
- .sendmsg = udp_sendmsg, //发送数据包到网络层接口
- .recvmsg = udp_recvmsg, //接受应用层数据
- .sendpage = udp_sendpage,
- .backlog_rcv = __udp_queue_rcv_skb,
- .hash = udp_lib_hash,
- .unhash = udp_lib_unhash,
- .rehash = udp_v4_rehash,
- .get_port = udp_v4_get_port,
- .memory_allocated = &udp_memory_allocated,
- .sysctl_mem = sysctl_udp_mem,
- .sysctl_wmem = &sysctl_udp_wmem_min,
- .sysctl_rmem = &sysctl_udp_rmem_min,
- .obj_size = sizeof(struct udp_sock),
- .slab_flags = SLAB_DESTROY_BY_RCU,
- .h.udp_table = &udp_table,
- #ifdef CONFIG_COMPAT
- .compat_setsockopt = compat_udp_setsockopt,
- .compat_getsockopt = compat_udp_getsockopt,
- #endif
- };

upd和套接字层的接口实现了对数据的收发、管理,在AF_INET协议族初始化的过程中完成注册,注册函数是int proto_register(struct proto *prot, int alloc_slab),初始化函数是inet_init在net/ipv4/af_inet.c文件中。
- static int __init inet_init(void)
- {
- struct sk_buff *dummy_skb;
- struct inet_protosw *q;
- struct list_head *r;
- int rc = -EINVAL;
-
- ...
-
-
- //注册tcp协议实例
- rc = proto_register(&tcp_prot, 1);
- if (rc)
- goto out_free_reserved_ports;
- //注册udp协议
- rc = proto_register(&udp_prot, 1);
- if (rc)
- goto out_unregister_tcp_proto;
-
- ...
-
- }

5、udp协议和IP层之间的接口
udp协议和IP层之间的接口由struct net_protoco结构体描述,也是定义了一系列函数指针,主要的函数是接受IP层的数据包udp_rcv和处理ICMP错误信息函数udp_err。
- static const struct net_protocol udp_protocol = {
- .handler = udp_rcv, //接受IP层数据包函数
- .err_handler = udp_err, //icmp错误处理函数
- .gso_send_check = udp4_ufo_send_check,
- .gso_segment = udp4_ufo_fragment,
- .no_policy = 1,
- .netns_ok = 1,
- };
也是在inet_init()函数中调用inet_add_protocol注册的。
- static int __init inet_init(void)
- {
-
- ...
-
- //注册传输层的处理函数到inet_protos全局数组中
- if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
- //注册udp处理函数
- if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
- printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
- ...
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。