1
votes

Note: the question was edited.

I have few questions on Berkeley Socket Programming:

  1. Is it possible to change a socket address after binding? If so- What are the C commands to do so?

    2.According to https://www.cs.cmu.edu/~srini/15-441/F01.full/www/assignments/P2/htmlsim_split/node18.html, when a socket is bind to INADDR_ANY, it receives packets from all interfaces, but when a packet is sent,(using sent command) it sends through a single NIC with the default IP.

    If I understand correctly- if a server has two active NICs, with different IPs, then a socket with the INADDR_ANY parameter can receive a packet with dst IP=x and send a packet with src IP=y, where x is not y. This can cause problems, for example in TCP connections, where the socket in the other destination is will receive the packet, but will drop it due as the dest IP is not the one expected.

    Is it true? And if so- does it means that server programs are NOT using INADDR_ANY where there are (two or more) active NICs with different IPs?

  2. Suppose the NIC of the default IP causes bottleneck. Can we change the socket options so that the packets will be send through another NICs(and not the previous NIC)? Can we do so if the NICs have the default IP address?

  3. Can we send packets through one NIC, and set the IP destination to another NIC? (I.e, NIC1 will only send packets, and NIC2 will only receive packets )

1
TCP or UDP or other?Ben Voigt
INADDR_ANY is just for listening on all interfaces. Multiple NICs on different subnets will only send through the NIC where target is reachable. Multiple NICs on the same subnet may send out on either NIC. On your question number 3, the best solution is to use bonded (or teamed) interface with 802.3ad. On your question 4, the answer is NO. If a packet arrives at NIC1 will you ignore it? Forward it? It just doesn't make sense. Again you might be looking for interface bonding.alvits
Very hard to keep up with all these edits. Please try to get it all right at once. For example I think you still have a confusion between source and destination in several places.user207421

1 Answers

3
votes

Is it possible to change a socket address after created?

Sockets don't have an IP address to change when created. They get one when bound.

If so- What are the C commands to do so?

The Berkeley Sockets API functions to do so are bind() and connect().

When a socket is bind to INADDR_ANY, it receives packets from all interfaces, but sends through a single NIC with the default IP.

No. It sends packets via whichever NIC is necessary to reach the target in each case.

Your cited source draws a distinction without a difference. A socket bound to INADDR_ANY receives packets from any interface. There is no difference between 'any' and 'all' as far as INADDR_ANY is concerned. It is far easier to understand as 'any'.

If I understand correctly- if a server has two active NICs, with different IPs, then a socket with the INADDR_ANY parameter can receive a packet with dst IP=x and send a packet with src IP=y, where x is not y

No. It sends the packet with the same source address the client originally connected to. That's part of what defines the connection.

This can cause problems, for example in TCP connections, where the socket in the other destination is will receive the packet, but will drop it due as the dest IP is not the one expected.

No. The destination in the packet is the client's IP address. Otherwise it wouldn't even get there. This is just nonsense. If you mean the source IP, see above.

Is it true?

No.

And if so- does it means that server programs are NOT using INADDR_ANY where there are (two or more) active NICs with different IPs?

No. INADDR_ANY means exactly what it says. Any IP address: any NIC.

Suppose the NIC of the default IP causes bottleneck. Can we change the socket options so that the packets will be send through another NICs (and not the previous NIC)?

No, but you can alter the IP routing tables.

Can we do so if the NICs have the default IP address?

Only one of them can have the default IP address. The question doesn't make sense.

Can we send packets through one NIC, and set the destination to another NIC?

Only if you're sending to yourself. Otherwise the question doesn't make sense.

(from your citation) When sending, a socket bound with INADDR_ANY binds to the default IP address, which is that of the lowest-numbered interface

I hope this refers to whatever simulator is being described. If it is meant to be a description of how TCP works it is wrong.