在 ICMP 套接字上接收数据

发布于 2024-07-26 12:17:47 字数 306 浏览 7 评论 0原文

当在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人九事 2024-08-02 12:17:47

ICMP 是不同于 TCP 和 UDP 的协议,这是由 IP 标头。 当您使用 IPPROTO_ICMP 打开套接字时,您是在告诉套接字仅传输和接收带有协议字段设置为 ICMP 的 IP 标头的数据包。

同样,使用 IPPROTO_TCPIPPROTO_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 or IPPROTO_UDP respond only to packets whose IP headers contain a protocol field that is set to TCP or UDP, respectively.

朦胧时间 2024-08-02 12:17:47

您可以检查 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.

陌生 2024-08-02 12:17:47

收到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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文