2

I have understood that UDP sockets are fully identified by destination IP and destination port. The IPs are in the IP datagram´s header yes, but when the datagram arrive at its destination, only the payload is sent to the upper-layer protocol. If two hosts send a UDP segment to a host with the same IP and port, how does the socket know which IP to send the response to, since the payload does not contain the source IP and the socket isn´t identified with source IP?

Ramriez
  • 21
  • 1
  • 2
  • 1
    https://manpages.debian.org/stretch/manpages-dev/recvfrom.2.en.html#recvfrom%28%29 – A.B May 04 '18 at 20:50
  • my doubt is same, how transport layer comes to know about source IP which sent the UDP packet at this layer, does IP layer passes UDP packet + source IP ? – dgfjxcv Mar 29 '20 at 05:05
  • Yes, the source IP of the packet is passed to the transport layer and the transport layer decodes the source port from the UDP header. A UNIX sockets application would us use recvfrom() to receive arriving datagrams and get the source IP and port in the src_addr structure. – etherfish Mar 29 '20 at 07:34
  • why data length is not passed from ip too, as data length can be easily calculated from ip datagrams – dgfjxcv Mar 29 '20 at 08:51

1 Answers1

0

TCP packets need to be wrapped in IP the same way that UDP datagrams are. Both have port numbers but not addresses for delivery.

For UDP/IP or TCP/IP, you need a 5-tuple to identify a connection:

  1. Protocol. (TCP, UDP)
  2. Source IP address.
  3. Source port.
  4. Target IP address.
  5. Target port.

A poor analogy: think about sending several physical packages. UDP alone would be just writing someone's apartment number without any street address, which of course does not work. Adding IP would mean writing destination and return addresses and adding postage, so it can be sent. (TCP would mean a fancier postal rate that allows package tracking to ensure delivery, and numbers the packages so their order is known.)

John Mahowald
  • 32,050
  • 2
  • 19
  • 34