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

2014年(39)

2013年(13)

2012年(5)

我的朋友

分类: C/C++

2014-10-10 15:57:41

内核版本:2.6.18


点击(此处)折叠或打开

  1. /* The inetsw table contains everything that inet_create needs to
  2.  * build a new socket.
  3.  */
  4. static struct list_head inetsw[SOCK_MAX]


点击(此处)折叠或打开

  1. /* Upon startup we insert all the elements in inetsw_array[] into
  2.  * the linked list inetsw.
  3.  */
  4. static struct inet_protosw inetsw_array[] =
  5. {
  6.         {
  7.                 .type = SOCK_STREAM,
  8.                 .protocol = IPPROTO_TCP,
  9.                 .prot = &tcp_prot,
  10.                 .ops = &inet_stream_ops,
  11.                 .capability = -1,
  12.                 .no_check = 0,
  13.                 .flags = INET_PROTOSW_PERMANENT |
  14.              INET_PROTOSW_ICSK,
  15.         },

  16.         {
  17.                 .type = SOCK_DGRAM,
  18.                 .protocol = IPPROTO_UDP,
  19.                 .prot = &udp_prot,
  20.                 .ops = &inet_dgram_ops,
  21.                 .capability = -1,
  22.                 .no_check = UDP_CSUM_DEFAULT,
  23.                 .flags = INET_PROTOSW_PERMANENT,
  24.        },
  25.         

  26.        {
  27.                .type = SOCK_RAW,
  28.                .protocol = IPPROTO_IP,    /* wild card */
  29.                .prot = &raw_prot,
  30.                .ops = &inet_sockraw_ops,
  31.                .capability = CAP_NET_RAW,
  32.                .no_check = UDP_CSUM_DEFAULT,
  33.                .flags = INET_PROTOSW_REUSE,
  34.        }
  35. }


点击(此处)折叠或打开

  1. /* Register the socket-side information for inet_create. */
  2.     for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  3.         INIT_LIST_HEAD(r); //初始化表头

  4.     for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  5.         inet_register_protosw(q); //把inetsw[q->type]放到q->list上



点击(此处)折叠或打开

  1. #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))

  2. void inet_register_protosw(struct inet_protosw *p)
  3. {
  4.     struct list_head *lh;
  5.     struct inet_protosw *answer;
  6.     int protocol = p->protocol;
  7.     struct list_head *last_perm;

  8.     spin_lock_bh(&inetsw_lock);

  9.     if (p->type >= SOCK_MAX)
  10.         goto out_illegal;

  11.     /* If we are trying to override a permanent protocol, bail. */
  12.     answer = NULL;
  13.     last_perm = &inetsw[p->type];
  14.     list_for_each(lh, &inetsw[p->type]) {
  15.         answer = list_entry(lh, struct inet_protosw, list);

  16.         /* Check only the non-wild match. */
  17.         if (INET_PROTOSW_PERMANENT & answer->flags) {//固定协议
  18.             if (protocol == answer->protocol)//不允许覆盖固定协议
  19.                 break;
  20.             last_perm = lh;
  21.         }

  22.         answer = NULL;
  23.     }
  24.     if (answer)
  25.         goto out_permanent;

  26.     /* Add the new entry after the last permanent entry if any, so that
  27.      * the new entry does not override a permanent entry when matched with
  28.      * a wild-card protocol. But it is allowed to override any existing
  29.      * non-permanent entry. This means that when we remove this entry, the
  30.      * system automatically returns to the old behavior.
  31.      */
  32.     list_add_rcu(&p->list, last_perm);
  33. out:
  34.     spin_unlock_bh(&inetsw_lock);

  35.     synchronize_net();

  36.     return;

  37. out_permanent:
  38.     printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  39.      protocol);
  40.     goto out;

  41. out_illegal:
  42.     printk(KERN_ERR
  43.      "Ignoring attempt to register invalid socket type %d.\n",
  44.      p->type);
  45.     goto out;
  46. }


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