全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

Linux bind的核心执行函数

时间:2023-07-31 10:51

人气:

作者:admin

标签: 系统  函数 

导读:...

bind的核心执行函数

bind系统调用的核心函数调用流程如下:

SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
|
sock- >ops- >bind(sock,
            (struct sockaddr *)
            &address, addrlen);
|
inet_bind
|
inet_csk_get_port

中间的流程暂且不看,本文主要分析最重要的函数:inet_csk_get_port,从该函数出发了解本地绑定端口如何管理。

以下是原函数,通过下文章节逐步分段分析

int inet_csk_get_port(struct sock *sk, unsigned short snum)
{
 bool reuse = sk- >sk_reuse && sk- >sk_state != TCP_LISTEN;
 struct inet_hashinfo *hinfo = sk- >sk_prot- >h.hashinfo;
 int ret = 1, port = snum;
 struct inet_bind_hashbucket *head;
 struct net *net = sock_net(sk);
 struct inet_bind_bucket *tb = NULL;
 kuid_t uid = sock_i_uid(sk);

 if (!port) {
  head = inet_csk_find_open_port(sk, &tb, &port);
  if (!head)
   return ret;
  if (!tb)
   goto tb_not_found;
  goto success;
 }
 head = &hinfo- >bhash[inet_bhashfn(net, port,
       hinfo- >bhash_size)];
 spin_lock_bh(&head- >lock);
 inet_bind_bucket_for_each(tb, &head- >chain)
  if (net_eq(ib_net(tb), net) && tb- >port == port)
   goto tb_found;
tb_not_found:
 tb = inet_bind_bucket_create(hinfo- >bind_bucket_cachep,
         net, head, port);
 if (!tb)
  goto fail_unlock;
tb_found:
 if (!hlist_empty(&tb- >owners)) {
  if (sk- >sk_reuse == SK_FORCE_REUSE)
   goto success;

  if ((tb- >fastreuse > 0 && reuse) ||
      sk_reuseport_match(tb, sk))
   goto success;
  if (inet_csk_bind_conflict(sk, tb, true, true))
   goto fail_unlock;
 }
success:
 if (hlist_empty(&tb- >owners)) {
  tb- >fastreuse = reuse;
  if (sk- >sk_reuseport) {
   tb- >fastreuseport = FASTREUSEPORT_ANY;
   tb- >fastuid = uid;
   tb- >fast_rcv_saddr = sk- >sk_rcv_saddr;
   tb- >fast_ipv6_only = ipv6_only_sock(sk);
   tb- >fast_sk_family = sk- >sk_family;
#if IS_ENABLED(CONFIG_IPV6)
   tb- >fast_v6_rcv_saddr = sk- >sk_v6_rcv_saddr;
#endif
  } else {
   tb- >fastreuseport = 0;
  }
 } else {
  if (!reuse)
   tb- >fastreuse = 0;
  if (sk- >sk_reuseport) {
   if (!sk_reuseport_match(tb, sk)) {
    tb- >fastreuseport = FASTREUSEPORT_STRICT;
    tb- >fastuid = uid;
    tb- >fast_rcv_saddr = sk- >sk_rcv_saddr;
    tb- >fast_ipv6_only = ipv6_only_sock(sk);
    tb- >fast_sk_family = sk- >sk_family;
#if IS_ENABLED(CONFIG_IPV6)
    tb- >fast_v6_rcv_saddr = sk- >sk_v6_rcv_saddr;
#endif
   }
  } else {
   tb- >fastreuseport = 0;
  }
 }
 if (!inet_csk(sk)- >icsk_bind_hash)
  inet_bind_hash(sk, tb, port);
 WARN_ON(inet_csk(sk)- >icsk_bind_hash != tb);
 ret = 0;

fail_unlock:
 spin_unlock_bh(&head- >lock);
 return ret;
}
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信