在 ICMP 套接字上接收数据
当在 ICMP 套接字上接收时(SOCK_RAW 和 IPPROTO_ICMP),因为 ICMP协议中没有“端口”的概念,如何实现 应用程序确定收到的数据包不是其他数据包的一部分 TCP/UDP/任何套接字传输也发生在 同时?
例如,假设您有一个具有 2 个线程的应用程序。 线程1 建立一个TCP服务器套接字,并不断地从一个TCP服务器接收数据 连接的客户端。 线程2不断发送echo请求包 使用 ICMP 套接字 (ping) 到同一客户端,然后接收回显 回复。 什么是阻止线程2接收TCP之一 数据包代替?
When receiving on an ICMP socket, (SOCK_RAW with IPPROTO_ICMP), since
there is no concept of "port" in the ICMP protocol, how can an
application determine that a received packet is not part of some other
TCP/UDP/whatever socket transmission that is also happening at the
same time?
For example, suppose you have an application with 2 threads. Thread 1
sets up a TCP server socket, and continuously receives data from a
connected client. Thread 2 continuously sends echo request packets
(ping) to the same client using an ICMP socket, and then receives echo
replys. What is to prevent Thread 2 from receiving one of the TCP
packets instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ICMP 是不同于 TCP 和 UDP 的协议,这是由 IP 标头。 当您使用
IPPROTO_ICMP
打开套接字时,您是在告诉套接字仅传输和接收带有协议字段设置为 ICMP 的 IP 标头的数据包。同样,使用
IPPROTO_TCP
或IPPROTO_UDP
打开的套接字仅响应其 IP 标头包含分别设置为 TCP 或 UDP 的协议字段的数据包。ICMP is a different protocol from TCP and UDP, as determined by the protocol field in the IP header. When you open a socket with
IPPROTO_ICMP
, you're telling the socket to transmit and receive only packets with IP headers whose protocol field is set to ICMP.Similarly, sockets opened with
IPPROTO_TCP
orIPPROTO_UDP
respond only to packets whose IP headers contain a protocol field that is set to TCP or UDP, respectively.您可以检查 ICMP 标头的类型并查看其是否为 ICMP 回显响应(类型 0)。 同样在 ICMP 中,响应将包含您最初发送的请求。
You can check the ICMP header for the type and see if its ICMP Echo Response (Type 0). Also in ICMP, the response will contain the request you had sent in the first place.
收到UDP& TCP 数据包从未传递到原始套接字。 如果进程想要读取包含 UDP 或 TCP 数据包的 IP 数据报,则必须在数据链路层读取数据包。 检查此链接
http://aschauf.landshut.org/fh/linux/udp_vs_raw/ ch01s03.html
如果数据包没有在第 2 层缓存,则由内核处理。
如果数据包是 icmp 协议并且是 echo 请求或时间戳请求或地址掩码请求类型,那么它完全由内核处理,否则它将传递到 RAW SOCKETS。
另一种情况是,所有具有内核不理解的协议字段的数据报都被传递到原始套接字,仅对它们进行基本的 ip 处理
。最后,如果数据报以片段形式到达,则不会将任何内容传递到原始套接字,直到所有片段都到达并重新组装。
如果您想了解更多信息,请阅读本书。
Received UDP & TCP packets never passed to raw sockets . IF a process wants to read IP datagram containing UDP or TCP packets the packets must be read at data link layer . check this link
http://aschauf.landshut.org/fh/linux/udp_vs_raw/ch01s03.html
if the packet is not cached at layer 2 then it is processed by kernel .
And if the packet is of icmp protocol and it is of type echo request or timestamp request or address mask request then it is entirely processed by kernel otherwise it will passed to RAW SOCKETS.
Another one all datagrams with a protocol field that the kernel does not understand are passed to raw sockets only basic processing of ip is done on them
At last if datagram arrives in fragments then nothing is passed to raw sockets until all fragments are arived and reassembled .
If you want to learn more, then read this book.