简体   繁体   English

如何将Raw Socket绑定到特定端口?

[英]How to bind a Raw Socket to a specific port?

I am currently working on a programming assignment. 我目前正在从事编程工作。 The assignment is to implement a client,network emulator, and server. 分配是实现客户端,网络仿真器和服务器。 The client passes packets to a network emulator, and the network emulator passes to the server. 客户端将数据包传递给网络仿真器,网络仿真器传递给服务器。 Vice-versa applies as well. 反之亦然。 The prerequisite for the assignment is that I may only use raw sockets. 分配的先决条件是我只能使用原始套接字。 So I will create my own IP and UDP headers. 所以我将创建自己的IP和UDP标头。 I have tested my packets with wireshark. 我用wireshark测试了我的数据包。 They are all correct and in the proper format(it reads them properly). 它们都是正确的并且格式正确(它可以正确读取)。

Another requirement is that the emulator, client and server all have specific ports they must be bound to. 另一个要求是模拟器,客户端和服务器都有必须绑定的特定端口。 Now, I do not understand how to bind a raw socket to a specific port. 现在,我不明白如何将原始套接字绑定到特定端口。 All my raw sockets receive all traffic on the host address they are bound to. 我的所有原始套接字都接收它们绑定的主机地址上的所有流量。 According to man pages, and everywhere else on the internet, including "Unix Network Programming" by Richard Stevens, this is how they are supposed to work. 根据手册页和互联网上的其他地方,包括理查德史蒂文斯的“Unix网络编程”,这就是他们应该如何工作的。 My teacher has not responded to any of my emails and I probably will not be able to ask him until Tuesday.I see two options in front of me. 我的老师没有回复我的任何电子邮件,我可能要到周二才能问他。我看到前面有两个选项。 First I can use libpcap to filter from a specific device and then output to my raw socket. 首先,我可以使用libpcap从特定设备进行过滤,然后输出到我的原始套接字。 I feel this is way out of scope for our assignment though. 我觉得这超出了我们的任务范围。 Or I can filter them after I receive them from the socket. 或者我可以在从套接字接收它们后过滤它们。 This apparently has a lot of overhead because all the packets are being copied/moved through the kernel. 这显然有很多开销,因为所有数据包都是通过内核复制/移动的。 At least, that is my understanding(please feel free to correct me if i'm wrong). 至少,这是我的理解(如果我错了,请随时纠正我)。

So my question is: Is their an option or something I can set for this? 所以我的问题是:他们是一个选项还是我可以为此设置的东西? Where the raw socket will bind to a port? 原始套接字将绑定到端口的位置? Have I missed something obvious? 我错过了明显的东西吗?

Thank you for your time. 感谢您的时间。

-- -

The man page for raw(7) says: raw(7)手册页说:

A raw socket can be bound to a specific local address using the bind(2) call. 可以使用bind(2)调用将原始套接字绑定到特定的本地地址。 If it isn't bound all packets with the specified IP protocol are received. 如果未绑定,则接收具有指定IP协议的所有数据包。 In addition a RAW socket can be bound to a specific network device using SO_BINDTODEVICE; 此外,可以使用SO_BINDTODEVICE将RAW套接字绑定到特定网络设备; see socket(7). 见socket(7)。

Edit: You cannot bind a raw socket to a specific port because "port" is a concept in TCP and UDP, not IP. 编辑:您不能将原始套接字绑定到特定端口,因为“端口”是TCP和UDP中的概念,而不是IP。 Look at the header diagrams for those three protocols and it should become obvious: you are working at a lower level, where the concept of port is not known. 看看这三个协议的标题图,它应该变得很明显:你在较低级别工作,其中端口的概念是未知的。

I would think you're expected to filter the packets in your software. 我认为你应该过滤软件中的数据包。 It sounds like the exercise is to learn what the different components of the IP stack do by recreating a simplified piece of it in user space. 听起来这个练习是通过在用户空间中重新创建它的简化部分来了解IP堆栈的不同组件。 Normally in the kernel, the IP code would process all packets, verify the IP headers, reassemble fragments, and check the protocol field. 通常在内核中,IP代码将处理所有数据包,验证IP头,重新组合片段,并检查协议字段。 If the protocol field is 17 (udp), then it passes it to the UDP code (every UDP packet). 如果协议字段是17(udp),则它将其传递给UDP代码(每个UDP数据包)。 It's up to the UDP code to then validate the UDP header and determine if any applications are interested in them based on the destination port. 然后由UDP代码验证UDP标头,并根据目标端口确定是否有任何应用程序对它们感兴趣。

I imagine your project is expected to more or less mimic this process. 我想你的项目预计会或多或少地模仿这个过程。 Obviously none of it will be as efficient as doing it in the kernel, but since the assignment is to write (part of) an IP stack in user-space, I'd guess efficiency isn't the point of the exercise. 显然,它没有一个像在内核中那样高效,但由于分配是在用户空间中写入(部分)IP栈,我认为效率不是练习的重点。

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

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