简体   繁体   English

C ++ UDP套接字端口多路复用

[英]C++ UDP Socket port multiplexing

How can I create a client UDP socket in C++ so that it can listen on a port which is being listened to by another application? 如何在C ++中创建客户端UDP套接字,以便它可以在另一个应用程序正在监听的端口上进行监听? In other words, how can I apply port multiplexing in C++? 换句话说,如何在C ++中应用端口多路复用?

I want to listen on only one port 我只想在一个端口上收听

You can do that with a sniffer. 您可以使用嗅探器来做到这一点。 Just ignore the packets from different ports. 只是忽略来自不同端口的数据包。

I might need to stop it from sending out some particular packets, because my program will send it instead of the original application 我可能需要阻止它发出某些特定的数据包,因为我的程序将发送它而不是原始应用程序

Okay, here I suggest you to discard sniffers, and use a MITM technique. 好的,在这里我建议您丢弃嗅探器,并使用MITM技术。

You'll need to rely on a PREROUTING firewall rule to divert the packets to a " proxy " application. 您将需要依靠PREROUTING防火墙规则数据包转移到“ 代理 ”应用程序。 Assuming UDP, Linux, iptables, and the " proxy " running on the same host, here's what the " proxy " actually needs to do: 假设UDP,Linux,iptables和“ 代理 ”在同一主机上运行,​​则“ 代理 ”实际上需要执行的操作:

1. Add the firewall rule to divert the packets (do it manually, if you prefer): 1.添加防火墙规则以转移数据包(如果愿意,请手动执行):

iptables -t nat -A PREROUTING -i <iface> -p <proto> --dport <dport>
    -j REDIRECT --to-port <newport>

2. Bind and listen on <newport> . 2.绑定并听<newport>

3. Relay all the traffic between the 2 endpoints (client, and original destination). 3.在2个端点(客户端和原始目的地)之间中继所有流量。 If you're running the " proxy " on a different host, use getsockopt with SO_ORIGINAL_DST to retrieve the original destination address. 如果要在其他主机上运行“ 代理 ”, SO_ORIGINAL_DST getsockoptSO_ORIGINAL_DST一起使用以检索原始目标地址。

It might sound tricky, but... yeah, that's because it's a bit tricky :-) Consult your firewall documentation if my assumption diverges. 这听起来可能很棘手,但是...是的,这是因为它有点棘手:-)如果我的假设有所不同,请查阅防火墙文档。

This is just packet sniffing like tcpdump or snoop , open up a raw socket and pull everything from the wire and filter as you require. 这只是像tcpdumpsnoop这样的数据包嗅探,打开一个原始套接字,并根据需要从电线和过滤器中拉出所有东西。 You will probably want to use libpcap to make things a little easier. 您可能希望使用libpcap使事情变得容易一些。

Without administrator or super-user privileges you will need the target application to open ports with SO_REUSEADDR and SO_REUSEPORT as appropriate for the platform. 没有管理员或超级用户特权,您将需要目标应用程序使用适合平台的SO_REUSEADDRSO_REUSEPORT打开端口。 The caveat being you can only receive broadcast and multicast packets, unicast packets are delivered to the first open socket. 需要注意的是,您只能接收广播和多播数据包,单播数据包将传递到第一个打开的套接字。

This is not multiplexing - that term is reserved for handling I/O on multiple channels in the same process and where things like select(2) and poll(2) are most useful. 不是多路复用-该术语保留用于在同一过程中处理多个通道上的I / O,而select(2)poll(2)类的东西最有用。

What you are asking for is multicast . 您要的是多播 Here is the basic example. 是基本示例。

Note that IP reserves a special range of addresses (aka groups) for multicasting. 请注意,IP为多播保留了一个特殊的地址范围(即组)。 These get mapped to special ethernet addresses. 这些被映射到特殊的以太网地址。 The listener(s) would have to join the multicast group, while sender does not have to, it just sends as usual. 侦听器必须加入多播组,而发送者不必加入 ,而只是照常发送。

Hope this helps. 希望这可以帮助。

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

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