网络层如何为数据包找到路由
针对 www.stackoverflow.com 的 HTTP 应用程序请求。 该消息被传递到传输层。传输层添加其标头并将数据包发送到互联网层。 互联网层无法看到 www.stackoverflow.com,因为它只能访问传输层附加的标头。那么Internet层如何决定这个请求数据包的路由呢?
IP 标头中的目标地址字段是如何填充的,因为只有应用层和传输层知道该字段。 (应用层与互联网层和传输层没有交互,在其标头中提及端口号。)
An HTTP application request for www.stackoverflow.com.
This message is passed to Transport layer. Transport layer adds its header and sends the packet to Internet Layer.
The Internet Layer cannot see www.stackoverflow.com as it can only access the header which was appended by Transport Layer. Then how can Internet Layer decide route for this request packet.
How is the destination address field in IP header is filled, as only Application Layar and Transport Layer know about that field. (Application layer has no interaction with Internet Layer and Transport Layer mention port number in its Header.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应用层已经通过 DNS 从 URL 检索到主机的 IP 地址。 IP 地址以及来自应用层的其他数据被向下发送到传输层,传输层将数据打包,然后将其发送到互联网层,然后继续。
The application layer would have already retrieved the IP address of the host from the URL via DNS. The IP address as well as other data from the Application layer are sent down to the Transport layer which packetizes the data and then send it down to the Internet layer and then it goes.
应用程序(在本例中为浏览器)执行了一些操作,最终调用了 getaddrinfo 库函数或等效函数,这使得系统解析器在 DNS 中查找名称并返回一组 IP 地址。
应用程序以某种方式选择了其中一种(有标准方法可以做到这一点,但可爱的是有多少标准方法)并使用
connect
系统调用来建立连接,这在内核正在努力获取到该 IP 地址的连接。最终创建以该目标地址和本地地址作为源的 IP 数据包,下一个协议设置为 TCP,并且 TCP 标头中的 SYN 位处于打开状态。路径上的每个路由器都会查阅其表并转发数据包。
TCP 魔法发生,SYN+ACK 返回,然后建立一个连接,通过该连接发生 HTTP 魔法,然后加载页面。
The application, in this case the browser, did something that ended up calling the
getaddrinfo
library function or something equivalent, which made the system's resolver look up the name in the DNS and return a set of IP addresses.The application somehow chose one of those (there's standard ways to do this, but the lovely thing is how many standard ways) and used the
connect
system call to make the connection, which started the transport layer in the kernel working on getting a connection to that IP address.That ends up creating IP packets with that destination address and the local address as the source, next protocol set to TCP and the SYN bit on in the TCP header. Each router on the path consults its tables and forwards the packet.
TCP magic happens, a SYN+ACK comes back, then there's a connection, over which HTTP magic happens, and the page loads.
rfc791 IP - 寻址
名称、地址和路由之间存在区别 [4] 。名字表明了我们所寻求的东西。地址表明它在哪里。路线指示如何到达那里。互联网协议主要处理地址。高层(即主机到主机或应用程序)协议的任务是进行名称到地址的映射。互联网模块将互联网地址映射到本地网络地址。较低层(即本地网络或网关)过程的任务是进行从本地网络地址到路由的映射。地址的长度固定为四个八位字节(32 位)。
了解更多:http://www.faqs.org/rfcs/rfc791.html#ixzz0buBJkVEI
更高级别(即主机到主机或应用程序)协议的任务是进行从名称到地址的映射???
rfc791 IP - Addressing
A distinction is made between names, addresses, and routes [4]. A name indicates what we seek. An address indicates where it is. A route indicates how to get there. The internet protocol deals primarily with addresses. It is the task of higher level (i.e., host-to-host or application) protocols to make the mapping from names to addresses. The internet module maps internet addresses to local net addresses. It is the task of lower level (i.e., local net or gateways) procedures to make the mapping from local net addresses to routes. Addresses are fixed length of four octets (32 bits).
Read more: http://www.faqs.org/rfcs/rfc791.html#ixzz0buBJkVEI
It is the task of higher level (i.e., host-to-host or application) protocols to make the mapping from names to addresses ???
如果你想知道实际的IP头是如何获取地址的。当创建套接字时,它发生在内核中。在本例中是一个 TCP 套接字,检查
man 7 ip
数据不是从 TCP 数据包继承的,尽管数据包含在 TCP 标头的校验和中。
If you want to know how the actual IP header gets the address. It occurs in the Kernel, when a socket is created. In this case a TCP socket, Check out
man 7 ip
The data is not inherited from the TCP packet, though the data is included in the checksum of the TCP header.