Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? In other words, how can I apply port multiplexing in C++?

like image 608
SkypeMeSM Avatar asked Oct 07 '10 02:10

SkypeMeSM


People also ask

Does UDP have multiplexing?

Multiplexing and Demultiplexing services are provided in almost every protocol architecture ever designed. UDP and TCP perform the demultiplexing and multiplexing jobs by including two special fields in the segment headers: the source port number field and the destination port number field.

How is TCP stream communication and UDP datagram communication done using sockets?

Bind both sockets to the server address. Initialize a descriptor set for select and calculate a maximum of 2 descriptors for which we will wait. Call select and get the ready descriptor(TCP or UDP) Handle new connection if the ready descriptor is of TCP OR receive datagram if the ready descriptor is of UDP.

Can TCP connect to UDP?

TCP and UDP ports are not related to each other. TCP ports are interpreted by the TCP stack, while the UDP stack interprets UDP ports. Ports are a way of multiplexing the connection so that multiple devices can connect to a node.

Is UDP socket bidirectional?

Datagram sockets allow processes to use UDP to communicate. A datagram socket supports bidirectional flow of messages. A process on a datagram socket can receive messages in a different order from the sending sequence and can receive duplicate messages. Record boundaries in the data are preserved.


1 Answers

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.

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. 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. Bind and listen on <newport>.

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.

like image 54
jweyrich Avatar answered Sep 23 '22 14:09

jweyrich