如何将原始套接字绑定到特定端口? [英] How to bind a Raw Socket to a specific port?

查看:1491
本文介绍了如何将原始套接字绑定到特定端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个编程任务。的分配是实现一个客户端,网络仿真器,和服务器。客户端通过数据包发送到网络仿真器,和网络仿真器传递给服务器。反之也适用。对于分配的prerequisite的是,我只可以使用原始套接字。所以,我会创造我自己的IP和UDP头。我已经使用Wireshark测试了我的包。他们是正确的,并以正确的格式(它正确地读)。

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. I have tested my packets with wireshark. They are all correct and in the proper format(it reads them properly).

另一个要求是仿真器,客户机和服务器都必须将它们绑定到特定端口。现在,我不知道如何原始套接字绑定到特定端口。我所有的原始套接字收到它们被绑定到主机地址的所有通信。根据手册页和其他地方上网,包括UNIX网络编程由理查德·史蒂文斯,这是他们应该如何工作。我的老师没有回应我的任何电子邮件,我可能就不能问他,直到Tuesday.I看到在我面前的两个选项。首先,我可以使用libpcap的从特定设备,然后输出到我的原始套接字进行过滤。我觉得这是出路的范围,为我们的任务虽然。或后我从套接字接收他们,我可以过滤。这显然​​有很大的开销通过内核,因为所有的数据包被复制/移动。至少,这是我的理解(请随时纠正我,如果我错了)。

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. 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. 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?

感谢您的时间。

-

推荐答案

借助手册页为原料( 7)说:

一个原始套接字可以绑定使用bind(2)调用特定的本地地址。如果它不与指定IP协议绑定所有分组被接收。除了一个RAW套接可以绑定到使用SO_BINDTODEVICE特定的网络设备;看到插座(7)。

A raw socket can be bound to a specific local address using the bind(2) call. If it isn't bound all packets with the specified IP protocol are received. In addition a RAW socket can be bound to a specific network device using SO_BINDTODEVICE; see socket(7).

编辑:你不能在原始套接字绑定到特定端口,因为口是TCP和UDP,IP不是一个概念。看头示为这三个协议和应成为显而易见:你在一个较低的水平,其中,端口的概念是不知道工作

You cannot bind a raw socket to a specific port because "port" is a concept in TCP and UDP, not 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.

这篇关于如何将原始套接字绑定到特定端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆