C ++ UDP套接字端口多路复用 [英] C++ UDP Socket port multiplexing
问题描述
如何在C ++中创建客户端UDP套接字,以便它可以在另一个应用程序正在监听的端口上进行监听?换句话说,如何在C ++中应用端口多路复用?
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? In other words, how can I apply port multiplexing in 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
好的,在这里,我建议您丢弃嗅探器,并使用 MITM 技术.
Okay, here I suggest you to discard sniffers, and use a MITM technique.
您需要依靠PREROUTING防火墙规则将数据包转移到"代理"应用程序.假设UDP,Linux,iptables和"代理"在同一主机上运行,则实际上是"代理"要做的事情:
You'll need to rely on a PREROUTING firewall rule to divert the packets to a "proxy" application. Assuming UDP, Linux, iptables, and the "proxy" running on the same host, here's what the "proxy" actually needs to do:
1..将防火墙规则添加到转移数据包中(如果需要,请手动执行):
1. Add the firewall rule to divert the packets (do it manually, if you prefer):
iptables -t nat -A PREROUTING -i <iface> -p <proto> --dport <dport>
-j REDIRECT --to-port <newport>
2.绑定并收听<newport>
.
3..中继2个端点(客户端和原始目的地)之间的所有流量.如果要在其他主机上运行"代理",请使用
3. Relay all the traffic between the 2 endpoints (client, and original destination). If you're running the "proxy" on a different host, use getsockopt with SO_ORIGINAL_DST
to retrieve the original destination address.
这听起来可能很棘手,但是...是的,因为那有点棘手:-) 如果我的假设有所不同,请查阅您的防火墙文档.
It might sound tricky, but... yeah, that's because it's a bit tricky :-) Consult your firewall documentation if my assumption diverges.
这篇关于C ++ UDP套接字端口多路复用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!