简体   繁体   English

绑定如何在内核空间内部工作?

[英]How bind works internally in kernel space?

Can anyone help me in tracing bind() system call in socket programming. 谁能帮我跟踪套接字编程中的bind()系统调用。 I would like to know what happens when bind() is called, in kernel space. 我想知道在内核空间中调用bind()时会发生什么。 Like which are the structures it updates and what functions are invoked in lower level 就像它更新哪些结构以及在较低级别调用哪些功能

The bind(2) system call just configures the local side's address parameters that a socket will use once you have connected (or sendto(2) ). bind(2)系统调用仅配置您连接(或sendto(2) )后套接字将使用的本地地址参数。 If you don't use it, the kernel selects defaults for it, depending on the underlying protocol. 如果您不使用它,则内核会根据基础协议为其选择默认值。

The exact procedure bind(2) follows depends on the protocol family you are working on, as bind will behave differently depending if you are using PF_UNIX , PF_INET , PF_PACKET , PF_XNS , etc. 遵循的确切过程bind(2)取决于您正在使用的协议系列,因为bind的行为会有所不同,具体取决于您是否使用PF_UNIXPF_INETPF_PACKETPF_XNS等。

For example, in Unix sockets, you'll get your socket associated to an inode in the filesystem (an inode that supports unix sockets, of course), so clients have a path to connect to (in Unix sockets, addresses are paths in the filesystem). 例如,在Unix套接字中,您将使套接字与文件系统中的一个索引节点关联(当然,一个索引节点支持unix套接字),因此客户端具有连接的路径(在Unix套接字中,地址是文件系统)。 In TCP/IP sockets, you can fix the local IP address or the local IP port your socket can listen on (to accept connections) or you can force a IP address and/or port to connect from, to a server. 在TCP / IP套接字中,您可以固定套接字可以侦听(接受连接)的本地IP地址或本地IP端口,也可以强制IP地址和/或端口从其连接到服务器。

For a deeper understanding of networking sockets internals, I recommend you reading the excellent book from WR Stevens "TCP/IP Illustrated Vol 2. The implementation," describing the implementation of BSD sockets in NET2. 为了更深入地了解网络套接字内部,我建议您阅读WR Stevens的优秀著作“ TCP / IP Illustrated Vol2。实现”,其中介绍了NET2中BSD套接字的实现。 It's old, but still the best explanation ever made. 它很旧,但仍然是有史以来最好的解释。 For a good introduction of the BSD socket system calls use, there's also an excellent book (for a long time it was indeed also the best system call reference for BSD unix system calls) by WRStevens: "UNIX network programming, Vol 1 (2ND Ed): The sockets API." 为了更好地介绍BSD套接字系统调用的用法,还有WRStevens撰写的一本非常不错的书(很长一段时间以来,它的确是BSD unix系统调用的最佳系统调用参考): “ UNIX网络编程,第1卷(第2版Ed ):套接字API。” Both are two jewels everyone should have available at work. 两者都是每个人都应该在工作中获得的两个珠宝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM