原始套接字问题:TCP 数据包是否传递到原始套接字?
根据Unix网络编程第一卷,“接收到的UDP数据包和接收到的TCP数据包永远不会传递到原始套接字。如果进程想要读取包含UDP或TCP数据包的IP数据报,则必须在数据链路层读取数据包”...
但与此相反,在创建原始套接字时有一个 IPPROTO_TCP 协议选项,在我看来,它正好服务于这个目的。有人可以指出我在理解这一点时可能犯的任何错误吗?
According to Unix Network Programming Vol1, "Received UDP packets and received TCP packets are never passed to a raw socket. If a process wants to read IP datagrams containing UDP or TCP packets, the packets must be read at the datalink layer"...
But contrary to this, there is a IPPROTO_TCP protocol option in creating raw sockets which appears to me as serving this exact purpose. Could anyone please point out any mistakes I might be making in understanding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建原始套接字时,您可以使用 IPPROTO_TCP 等协议选项指定要绑定到的协议:UDP、TCP 或 ICMP。但是,此选项仅确定您要打开的套接字类型,因此该端口上收到的数据将转发到您的应用程序。因此,如果您设置 IPPROTO_TCP 并在端口 5000 上打开原始套接字,您的应用程序将收到发送到端口 5000 的原始 TCP 数据包,但不会收到发送到端口 5000 的原始 UDP 数据包。
即使数据包保证是 TCP,套接字也不会收到 发送到端口 5000 的原始 TCP 数据包。执行任何正常的 TCP 处理(syn、ack、重新排序等),您只需获取原始 IP 数据包以及表示 TCP 标头的一大块二进制数据。对于普通的 TCP 套接字,您接收到的数据是嵌入在 TCP 标头中的数据。对于原始 TCP 套接字,数据仍然是嵌入在链路层标头中的所有内容,因此您将看到 IP 标头,然后是 TCP 标头,然后是收到的每个数据包的有效负载数据。
有关更多信息,请查看本教程:
原始套接字的 C 语言简要编程教程
When You create a raw socket, you can specify which protocol to bind to, UDP, TCP, or ICMP using the IPPROTO_TCP,etc protocol options. However, this option only determines what type of socket you are opening and therefore what data receeived on that port will be forwarded to your application. SO if you set IPPROTO_TCP and open a raw socket on port 5000, your application will receive raw TCP packets sent to port 5000, but not raw UDP packets sent to port 5000.
Even though the packets are guaranteed to be TCP, the socket will not do any of the normal TCP processing (syn,ack, reordering, etc), you just get the raw IP packets with a chunk of binary data representing the TCP headers. With a normal TCP socket, the data you receive is the data embedded inside the TCP headers. With a Raw TCP socket, the data is still everything embedded in the link layer headers, so you will see the IP Header, followed by the TCP Header, followed by the payload data for each packet received.
For more information, check out this tutorial:
A brief programming tutorial in C for raw sockets