1

While studying computer networks as the course subject, my concept was that operating system distinguishes a packet based on the destination port and delivers it to application which is running on that port.

Later I came to know that we can connect to two different destinations (DestinationIP:DestinationPort) using the same source(SourceIP:SourcePort).

tcp        0      0 192.168.1.5:60000       199.7.57.72:80          ESTABLISHED 1000       196102      10179/firefox
tcp        0      0 192.168.1.5:60000       69.192.3.67:443         ESTABLISHED 1000       200361      10179/firefox
tcp        0      0 192.168.1.5:60000       69.171.234.18:80        ESTABLISHED 1000       196107      10179/firefox
tcp        0      0 192.168.1.5:60000       107.21.19.182:22        ESTABLISHED 1000       196399      10722/ssh
tcp        0      0 192.168.1.5:60000       69.171.234.18:443       ESTABLISHED 1000       201792      10179/firefox
tcp        0      0 192.168.1.5:60000       69.192.3.34:443         ESTABLISHED 1000       200349      10179/firefox
tcp        0      0 127.0.0.1:4369          127.0.0.1:51889         ESTABLISHED 129        12036       1649/epmd
tcp        0      0 192.168.1.5:60000       69.192.3.58:443         ESTABLISHED 1000       200352      10179/firefox
tcp        0      0 192.168.1.5:60000       74.125.236.88:80        ESTABLISHED 1000       200143      10179/firefox
tcp        0      0 192.168.1.5:60000       174.122.92.78:80        ESTABLISHED 1000       202935      10500/telnet
tcp        0      0 192.168.1.5:60000       74.125.236.87:80        ESTABLISHED 1000       201600      10179/firefox

Going little more into depths, I came to know that if an application uses bind() system call to bind a socket descriptor with a particular IP and port combination, then we can't use the same port again. Otherwise if a port is not binded to any socket descriptor, we can use the same port and IP combination again to connect to a different destination.

I read in the man page of bind() syscall that

   bind() assigns the address specified to by addr to the socket referred to by the file descriptor sockfd.

My question are:

  1. When we don't call bind() syscall generally while writing a client program then how does the OS automatically selects the port number.

  2. When two different applications use the same port and IP combination to connect to two different servers and when those servers reply back, how does the OS finds out that which packet needs to be redirected to which application.

1
  • Ah yes. You figured a copy-paste is waaay too easy so why not put up a screenshot ?
    – cnicutar
    Jan 20, 2013 at 12:11

3 Answers 3

2

When we don't call bind() syscall generally while writing a client program then how does the OS automatically selects the port number

The OS picks a random unused port (not necessarily the "next" one).

how does the OS finds out that which packet needs to be redirected to which application

Each TCP connection is identified by a 4-tuple: (src_addr, src_port, dst_addr, dst_port) which is unique and thus enough to identify where each segment belongs.

EDIT

When we don't call bind() syscall generally while writing a client program then how does the OS automatically selects the port number.

Sometime before "connecting" in the case of a TCP socket. For example, Linux has the function inet_csk_get_port to get an unused port number. Look for inet_hash_connect in tcp_v4_connect.

14
  • 2
    Also not necessarily 'a random port', but one within a specific range (on most Linuxes, by default, between 32768 and 61000).
    – robertklep
    Jan 20, 2013 at 12:16
  • @robertklep The word "random" does not conflict with the actual port being bound in a range.
    – cnicutar
    Jan 20, 2013 at 12:17
  • @robertklep: Yes you are correct, the port is not random port. The port range can be seen using the command: $ sysctl net.ipv4.ip_local_port_range Jan 20, 2013 at 12:18
  • But my question is while writing client application, after socket() syscall we use connect() syscall, then where actually the OS have come in between and alloted that random port. I want to know in terms of programming. Jan 20, 2013 at 12:21
  • @Cratylus: What do you mean by available port. My screenshot clearly shows that same port is utilized again and again. Jan 20, 2013 at 12:23
0

For 1: OS just picks the next available port.
For 2: It is done based on the dst port. Client applications will connect to same server over different client ports

0

I think for a client program OS maintains a table with socket fd(opened by client) and server IP+port after establishment of TCP connection.So whenever server replies back, OS can pick up the socket fd against the particular server IP+PORT and data is written onto the socket fd. So server reply can be available to the client on this particular socket fd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.