1
votes

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.

3
Ah yes. You figured a copy-paste is waaay too easy so why not put up a screenshot ?cnicutar

3 Answers

2
votes

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.

0
votes

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
votes

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.