Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316811
  • 博文数量: 57
  • 博客积分: 146
  • 博客等级: 入伍新兵
  • 技术积分: 769
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-29 14:57
文章分类
文章存档

2014年(39)

2013年(13)

2012年(5)

我的朋友

分类: C/C++

2014-10-09 17:10:27

内核版本:2.6.18


点击(此处)折叠或打开

  1. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  2.         printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  3.     if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  4.         printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  5.     if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  6.         printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  7. #ifdef CONFIG_IP_MULTICAST
  8.     if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  9.         printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  10. #endif

点击(此处)折叠或打开

  1. #ifdef CONFIG_IP_MULTICAST
  2. static struct net_protocol igmp_protocol = {
  3.     .handler =    igmp_rcv, //icmp协议接收处理函数
  4. };
  5. #endif

  6. static struct net_protocol tcp_protocol = {
  7.     .handler =    tcp_v4_rcv,
  8.     .err_handler =    tcp_v4_err,
  9.     .gso_send_check = tcp_v4_gso_send_check,
  10.     .gso_segment =    tcp_tso_segment,
  11.     .no_policy =    1,
  12. };

  13. static struct net_protocol udp_protocol = {
  14.     .handler =    udp_rcv,
  15.     .err_handler =    udp_err,
  16.     .no_policy =    1,
  17. };

  18. static struct net_protocol icmp_protocol = {
  19.     .handler =    icmp_rcv,
  20. }
inet_add_protocol()作用:把igmp_protocol,icmp_protocol,tcp_protocol,udp_protocol放到inet_protos(全局hash table)上。

源代码:

点击(此处)折叠或打开

  1. int inet_add_protocol(struct net_protocol *prot, unsigned char protocol)
  2. {
  3.     int hash, ret;

  4.     hash = protocol & (MAX_INET_PROTOS - 1);//计算hash

  5.     spin_lock_bh(&inet_proto_lock);
  6.     if (inet_protos[hash]) {
  7.         ret = -1;
  8.     } else {
  9.         inet_protos[hash] = prot;
  10.         ret = 0;
  11.     }
  12.     spin_unlock_bh(&inet_proto_lock);

  13.     return ret;
  14. }






阅读(2361) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~