All writings · tech · Linux networking

Understanding BSD Sockets

In the process of understanding the entire Egress and Ingress path a network packet follows in a Linux based machine, it is crucial to understand how the BSD Sockets Interface works.

Prerequisites

When we think about how computers communicate, it all boils down to process exchaning information, which is known as Inter Process Communication. Whether these processes are on the same machine or on different ones, they need to follow a specific set of steps to successfully transfer data.

If the processes are on the same machine, they use local mechanisms like shared memory or Unix domain Sockets. Examples include system services like Docker or Systemd communicating internally. However when the processes are on different machines- like when a web browser talks to a web server- the data has to travel across networks, which is a different series of steps.

In either case, all communication follows a specific route:

To manage this complexity, Linux provides various layers of abstraction, starting from the BSD socket API- which serves as the interface for user space applications- to the Network Interface Card (NIC), which handles the actual transmission of data.

Understanding these layers is important, so let's start with the absolute basics, Sockets.

Sockets

Think of a socket as a virtual plug that allows data to flow between two processes. It’s like a two-way pipe connecting applications, allowing them to exchange information. These processes can either be on the same machine or on different machines across a network.

Sockets act as communication endpoints. Each socket has a unique address (such as an IP address) and a port number. This combination helps uniquely identify the socket, ensuring that data reaches the correct destination.

Here’s a simple analogy: Imagine a phone call. Each person (process) has a unique phone number (address), and the phone line (socket) connects them, allowing conversation (data exchange) in both directions.

Depending on how they operate, sockets can be:

Sockets provide an abstraction over the complexities of data transfer. They provide a universal interface (API) for applications to talk to each other, whether they are communicating locally or over the internet.

BSD Socket API: A Unified Approach to Network Communication

When it comes to two processes communicating, it can happen via various means i.e. Bluetooth, Internet, Local (processes running on the same machine) etc.

The way Bluetooth communication works is totally different from the internet communication, but they overall do similar things. Things like sending, receiving messages, establishing connection etc. This is where Interfaces come very handy.

The BSD Socket API serves as a universal interface for network communication. It abstracts the complexities of different protocols and provides a standard and consistent way for applications to send and receive data, regardless of the transport medium.

The actual implementation details of the interface depend on the specific type of communication, which in turn depends on the Address Family.

In the Linux world, implementation of these different communication types is grouped into Address Families. Each address family related to a particular protocol. For Example:

Address Families

Address families define the type of addressing a socket can communicate with. Each family has a different type of address representation and how the socket communicates over the network.

Some common address families in the BSD socket API:

When creating a socket, you specify the address family along with the socket type and protocol. This determines the socket’s behavior and the communication rules for communication. For instance, an AF_INET socket use SOCK_STREAM for TCP and SOCK_DGRAM for UDP.


Type of BSD Sockets

There are two types of BSD sockets, depending on the type of communication between two processes.


How does it Work

The API operates by defining a socket- an endpoint for communication. Through this interface, applications interact with the Transport and Network layers of the Networking stack (OSI model).

The BSD Socket API looks something like this, along with an overview of what each method is supposed to do, with an example in C:

When you create a socket using the BSD socket API, you specify the address family to define the kind of protocol and addressing system that socket will use.

Example:

int sockfd = socket(AF_INET, SOCK_STREAM, 0)

In the above example, we are creating a TCP socket for IPv4. Some of the common combinations are as follows.

Conclusion: Why Understanding BSD sockets is Crucial

Understanding the BSD socket interface is foundational for anyone working with network applications and systems. Many user-space libraries/frameworks interact directly with this API.

For Example: When you do app.listen(:8080, callback) on an express server, internally it triggers the listen() system call.

This knowledge becomes essential in scenarios like:

Ultimately, mastering these concepts bridges the gap between theory and practice, empowering you to design robust, efficient, and scalable networked applications and understanding how to interact with the networking stack using eBPF. Whether you're working on low-level optimizations or developing high-level APIs, understanding this foundational layer ensures you have the tools to succeed.

Next we will be looking into the Egress and Ingress path implementation.


Originally published at prateeksingh.life/writings/tech/linux-networking/BSD-socket-API. Save to Instapaper.