Re: strange behavior when the client bind its UDP socket to a local address



Hello,

David Schwartz a écrit :
On Jan 16, 5:04 am, Christophe Lohr <christophe.l...@xxxxxxxxxxxxxxxx>
wrote:

Server part:
$ nc -u -l -p 5555

Client part:
$ nc -u -s 192.168.75.62 localhost 5555

Sniffing lo:
# tshark -i lo
192.168.75.62 -> 127.0.0.1 UDP Source port: 39293 Destination
port: 5555
192.168.75.62 -> 192.168.75.62 UDP Source port: 5555 Destination
port: 39293
192.168.75.62 -> 192.168.75.62 ICMP Destination unreachable (Port
unreachable)

So, why is the second packet from 192.168.75.62 to 192.168.75.62 ???

Probably because the server used the default source address selected by the stack instead of using the destination address in the received packet.

Because 192.168.75.62 is "closer" to 192.168.75.62 than 127.0.0.1 is.
Normally, the networking stack will try to pick the closest source
interface address it can.

From my observations of the behaviour of the Linux IPv4 stack, I did not see any evidence that the default source adresse selection is based on "closeness" to the destination address. AFAICS, the default source address selection works as follows :

- If the destination is local (on the host), then use the same address as source. This is what is happening here.
- If the route covering the destination specifies an explicit source address, use that source address. This is usually what happens for directly attached subnets.
- Otherwise, use the primary address of the output interface specified in the route covering the destination, if it exists.
- Otherwise, when the output interface has no address, use the primary address of the first non-loopback interface.

(and not from 127.0.0.1 to 192.168.75.62)

Why would it deliberately pick a worse address?

Because the client sent a datagram to 127.0.0.1 and expects datagrams from the same address, not 192.168.75.62.

Although UDP is connectionless, it is rather common that bidirectional UDP communications use source and destination adresses in the same way as TCP does. At least it is common enough so that this is what the UDP connection tracking in netfilter expects.

Now a last comment. As a client, when nc sends a UDP datagram to address A it expects to receive datagrams from the same address A. However as a server, when nc receives a UDP datagram on address A it may send datagrams from a different address. This looks like a bug to me.
.



Relevant Pages

  • Re: Missing UDP packets
    ... After the datagram reaches the destination computer, UDP moves the ... Switch to TCP if you need to be sure of receiving the data. ...
    (comp.lang.labview)
  • How to extract the Destination address from a UDP package
    ... Is there anyway to extract the Destination address from a UDP package. ... the hope that something similar can be done outside BSD systems. ... datagram without resorting to the RAW packet format. ...
    (perl.beginners)
  • Re: strange behavior when the client bind its UDP socket to a local address
    ... received datagram just because the source address isn't what 'nc' ... Here is what netstat prints when the nc client is ... address that matches the received destination address. ... The nc server socket has the local addresse 127.7.8.9 instead of ...
    (comp.os.linux.networking)
  • Re: Newby: is an UDP socket full-duplex?
    ... > or client. ... You send datagrams that are addressed to a destination (IP and ... I use the same UDP socket I read data from... ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to extract the Destination address from a UDP package
    ... I am working on an Ubuntu box with perl version 5.8.8 installed. ... Does anyone know how to get the destination address from a UDP ... datagram without resorting to the RAW packet format. ...
    (perl.beginners)