LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-16-2020, 09:40 AM   #1
babaliaris
Member
 
Registered: Jul 2018
Location: Greece
Distribution: Arch Linux
Posts: 37

Rep: Reputation: Disabled
Question How does Linux knows in which socket to inject an incoming package???


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!
 
Old 02-16-2020, 10:38 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,803

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
inject is not the right word here. The ports are always connected directly to the program which handles it. Either by reading or writing into it. As far as I know the kernel knows which port is used by which app. You can use netstat (for example) to see it.
The communication (sockets) are represented by file descriptors, so the application will read and write actually file descriptors.
The file descriptor points to a communication channel which itself knows the local/remote port and ip address information (among others). Also it knows if the packages is a reply or a request and can handle the real data to be sent/received. But it depends on the type of the channel.

Here you can find some basic details of the tcp protocol
https://en.wikipedia.org/wiki/Transm...ntrol_Protocol
and another one about udp: https://en.wikipedia.org/wiki/User_Datagram_Protocol
 
1 members found this post helpful.
Old 02-16-2020, 11:58 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I've always found this to be good reading, https://beej.us/guide/bgnet/

To be honest, your questions are a bit off because you make a correct statement about a binding, and then you ask how the system knows what to do when traffic comes in on a port that it's listening on.

In the code, the network port knows the receive function to call for traffic on a given port, because that's how it was set up. This is dynamic, but also there is a default if there wasn't something set up for the port.

I'd actually suggest asking this one on one with your instructor because the two of you can look at actual sample code and discuss at the same time.
 
Old 02-16-2020, 02:18 PM   #4
babaliaris
Member
 
Registered: Jul 2018
Location: Greece
Distribution: Arch Linux
Posts: 37

Original Poster
Rep: Reputation: Disabled
Thank you both for the references!


Quote:
Originally Posted by rtmistler View Post
I'd actually suggest asking this one on one with your instructor because the two of you can look at actual sample code and discuss at the same time.
There have been 3 years since I had this class. Now I have graduated
 
Old 02-16-2020, 02:42 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by pan64 View Post
inject is not the right word here.
Neither is "Package". It's "Packet".

Anyway, you might want to buy a copy of "Linux Networking Internals" if you're curious about how the TCP protocol (that's the layer that routes data to ports and therefore sockets) is implemented in the kernel.

I just did a quick google and I didn't really find any good free alternatives.

Last edited by dugan; 02-16-2020 at 02:47 PM.
 
2 members found this post helpful.
Old 02-16-2020, 03:01 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Well I recommend you search for source examples of TCP and UDP client-server. There are plenty.

Read the manual pages for the system calls used, and also refer to the See Also sections in those man pages.

You say you know a lot about sockets. Great! Expand that knowledge with study, I've given you a great reference, that used to be the defacto guide. May still be. Either case, using Linux and gcc, you have all you need to be able to compile example code, and also create your own test code.
 
1 members found this post helpful.
Old 02-16-2020, 04:33 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
These might or might not be helpful:

https://ops.tips/blog/how-linux-creates-sockets/

https://www.linuxjournal.com/article/7660

https://linux-kernel-labs.github.io/...etworking.html

https://viveksubbarao.wordpress.com/...ns-internally/

Also, as the professor doesn't seem to be using the same words as everyone else (for one thing, "regular" isn't a technical description of a type of socket), I might suggest that actually writing some working sample programs would be a good first step towards understanding more.

Last edited by dugan; 02-16-2020 at 04:42 PM.
 
1 members found this post helpful.
Old 02-18-2020, 11:30 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by babaliaris View Post
Plus regular sockets can have the same port number, so the port is useless.
If we're talking about TCP? Generally speaking, that's not true...

The host and port at the other end of socket connection are indeed set when you connect, and stored in the kernel. You can see it with ss.
 
Old 02-20-2020, 09:44 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As well as the beej guide mentioned above, the books mentioned here are classics https://en.wikipedia.org/wiki/TCP/IP_Illustrated & he wrote a quite a few other good ones.
 
1 members found this post helpful.
  


Reply

Tags
linux, posix, sockets



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 14 02-23-2021 08:35 PM
"Google Knows When You Are Sleeping, Knows When You're Awake" frankbell General 15 02-20-2019 10:24 PM
Modify UDP receive socket buffer size for an open socket (not at system level, but socket level) barz_83_LQ Linux - Networking 2 11-27-2017 07:56 PM
Connecting client socket to server socket only once in socket programming srinietrx Programming 5 08-20-2017 11:53 AM
How do I inject drivers into a Linux image? genghiskhano Linux - Software 3 03-21-2009 08:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration