Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set source address when sending using and UDP socket

I've two pc using VRRP for redundancy. So every PC (Linux) has a physical and a Virtual IP address.

I've a software (C++) with a client/server architecture with UDP protocol. The software bind the listen socket on "0.0.0.0" and use a new socket every time it needs to send some data to the other party. With wireshark I saw that when it sends data the source IP is the phisycal one... How can I set the source address of the sending socket to the Virtual one??

NOTE: Whit ifconfig I see only eth0 with the physical address...

like image 297
Napoleone1981 Avatar asked Apr 22 '11 12:04

Napoleone1981


People also ask

Does UDP have source address?

It's true that neither UDP or TCP headers include a source address, but that's because they don't have to: source address is already included in the header of the encapsulating transport protocol, which in this case would be the IP header.

Does UDP need IP address?

UDP Broadcast is an automatic method that can be used without manually entering the IP address of all Audia/Nexia devices. TCP can be used only if the exact IP addresses are known and can be entered manually.

What does connect () do on a UDP socket?

Datagram (UDP) sockets. The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.

Can UDP send and receive on same socket?

TCP vs UDP Once connected, a TCP socket can only send and receive to/from the remote machine. This means that you'll need one TCP socket for each client in your application. UDP is not connection-based, you can send and receive to/from anyone at any time with the same socket.


1 Answers

When the kernel needs to send something through a socket it performs these steps

  • if the socket is bound, use that source address
  • is the socket is not bound, it looks around for interfaces and selects a source address

So you need to bind(2) your socket to your desired address. For more information: "Source Address Selection" in chapter "IP Routing" of "Guide to IP Layer Network Administration with Linux".

like image 103
cnicutar Avatar answered Nov 15 '22 04:11

cnicutar