How does Linux knows in which socket to inject an incoming package???
by babaliaris from LinuxQuestions.org on (#4ZEP4)
I know a lot about sockets, but in the os class the professor never told us all the details and I can't find them by googling them.
I believe it's because I don't know exactly what information does a network package carries as overhead except for the IP of the receiver.
Ok, let's say computer 1 has a program A which creates a binding socket at port 80. Now computer 2 has a program B which creates a regular socket that sends a connection request at computer 1. This request contains the following extras:
1) The binding port number 80.
2) The IP address of the receiver (computer 1)
3) The IP address of the sender-itself (computer 2)
?) Should I put information saying this is a connection request?
?) Should I put something which says, next time you send me a request
this is the socket which I must use to inject this package.
Computer 1 receives that request, somehow knows that this is a connection request and probably uses the port number which was extracted from the package and this is how it knows that this package needs to be injected to the binding socket 80 that program A created, correct?
Now, program A creates a new regular socket to be used for the connection.
program B's regular socket does not know anything yet about program A's newly created regular socket.
Now let's assume program A's newly created regular socket sends that information to program B's regular socket. When this package reaches Cumputer 2 how does the OS know that this package needs to be injected to the program B's regular socket?
And after that, when program B replies through it's regular socket when that package reaches computer 1 how does the OS knows that this information needs to be injected through program's A new regular socket?
Do packages carry a PID or something? But still, this is not enough because a process can have multiple regular sockets. Plus regular sockets can have the same port number, so the port is useless. Only binding sockets have unique port numbers that can be used to identify which socket to be used to inject the package.
So, what exactly happens behind the scene? Does anyone know?
Thank you!


I believe it's because I don't know exactly what information does a network package carries as overhead except for the IP of the receiver.
Ok, let's say computer 1 has a program A which creates a binding socket at port 80. Now computer 2 has a program B which creates a regular socket that sends a connection request at computer 1. This request contains the following extras:
1) The binding port number 80.
2) The IP address of the receiver (computer 1)
3) The IP address of the sender-itself (computer 2)
?) Should I put information saying this is a connection request?
?) Should I put something which says, next time you send me a request
this is the socket which I must use to inject this package.
Computer 1 receives that request, somehow knows that this is a connection request and probably uses the port number which was extracted from the package and this is how it knows that this package needs to be injected to the binding socket 80 that program A created, correct?
Now, program A creates a new regular socket to be used for the connection.
program B's regular socket does not know anything yet about program A's newly created regular socket.
Now let's assume program A's newly created regular socket sends that information to program B's regular socket. When this package reaches Cumputer 2 how does the OS know that this package needs to be injected to the program B's regular socket?
And after that, when program B replies through it's regular socket when that package reaches computer 1 how does the OS knows that this information needs to be injected through program's A new regular socket?
Do packages carry a PID or something? But still, this is not enough because a process can have multiple regular sockets. Plus regular sockets can have the same port number, so the port is useless. Only binding sockets have unique port numbers that can be used to identify which socket to be used to inject the package.
So, what exactly happens behind the scene? Does anyone know?
Thank you!