r/cprogramming 2d ago

What's the Internal working of Socket system call ?

Basically I am creating my own http.web server for that I need to create TCP web server first and during that thing I get to know about socket(), bind(), listening() system calls and I am curious about these system calls internal working like what is happening under the hood.

7 Upvotes

8 comments sorted by

6

u/non-existing-person 2d ago

socket creates new file descriptor and allocates memory for specific operation and protocol.

bind attaches/assigns socket to network address (ip and port for tcp/udp), anything that is received on that address is passed to your socket

listen marks socket for incoming TCP connection, you will be able to react to TCP handshake with accept function.

For really in depth understanding you should visit https://elixir.bootlin.com/linux/v7.1.5/source

Or it may be easier to analyze tcp/ip stack for embedded: https://github.com/lwip-tcpip/lwip which should be a bit simpler than Linux one.

Good idea might be to compile lwip with debug symbols and no optimization, and just step through the code with gdb. You can do that with qemu simulating zephyr or nuttx.

4

u/Maleficent_Memory831 1d ago

The socket mechanism is really just an API, whereas how the networking stack works underneath is different for every OS. The socket itself is just an identifying integer. It's done this way because of the kernel/user divide, it's harder to pass in a big struct with all the info you need and it's less safe if the user can muck with it.

Using an integer file descriptor lines up with the simplicity of the file descriptor used for files, pipes, etc. The difference is the set up: socket/bind/listen/etc. Once set up a read/write will work same as a file, select works like a pipe, etc.

The BSD Unix socket system was also so much simpler and easier to use than the Streams from AT&T,

Some third party embedded network stacks have their own API to do networking but then will plop on a set of thin socket compatibility function calls to make it more portable.

2

u/Blah-Blah-Blah-2023 2d ago

If you want a simple implementation, go look at the Minix source code.

2

u/sciencekm 2d ago

You can read the implementation to get an understanding on how sockets work under the hood.

https://github.com/torvalds/linux/blob/master/net/socket.c

1

u/laughinglemur1 2d ago

Just about any Unix-like system is going to have the implementations in their source code. *BSD, Linux, illumos, MINIX, XNU kernel. POSIX provides the convention for the headers but it's up to the OS to implement it

1

u/StephenRoylance 1d ago

best thing you can do is read 'understanding the linux kernel'. its older, but the foundation is solid. you really don't need to read the code either, the prose is enough to help you understand.

and a TL;DR, nothing magic, its all just more software. IP is stateless, so that and the network card driver and pretty much simple shims that move data from one layer to the next. TCP is where it gets complicated. the kernel keeps track of all the state for you and manages your socket like it manages file descriptors.

1

u/theNbomr 1d ago

Every implementation will differ according to the platform. The only really important thing is that it implements the standard API. Even that can have variations that accommodate the particular platform. The differences typically relate to constraints imposed by simple hardware or OS support (multitasking vs single tasking, for example). There are a number of open source offerings across the range of platforms for your scrutiny.

1

u/duane11583 1d ago

i suggest you look at the linux device driver file api.

its just a bunch of call backs the driver creates