0

When reading C source code, I see the socket function used to open a connection. When I search for the definition of socket in the Linux kernel source code (version 3 and above) using grep, I can find more calls to socket but not the definition.

Where is socket defined? Is it defined in the source code of the ethernet card?

David Winiecki
  • 4,093
  • 2
  • 37
  • 39

2 Answers2

2

The socket() function is not a kernel function, it is a libc one.

If you want to study socket() internals get the code of the glibc (or any implementation of the standard C library), not the kernel code.

If you plan to go even deeper than that and study how the kernel implements the sockets mechanism look for the system call sys_socketcall().

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Ole
  • 19
  • 1
  • 2
    That's not exactly true - socket() is a kernel call rather than a libc one, it's manual page is in section 2 (kernel) rather than section 3 (libc) and as it's behavior is defined by the kernel rather than the C library, it is to kernel code where one should be looking. All that's in libc is a few lines of code implementing the platform-specific syscall mechanism needed to invoke it, and some traps for oddly configured systems. – Chris Stratton Dec 30 '11 at 16:29
0

I realize this thread is ancient, but I actually had to go look this up because I'm working on a security stack that does 10Gb/s and needed to know how this was implemented. I was able to find the source here: https://github.com/torvalds/linux/blob/master/net/socket.c

Grant Curell
  • 1,321
  • 2
  • 16
  • 32