Networking/Sockets

From WhitixDoc

Jump to: navigation, search
Source reference
net/socket.c
socket.c handles socket creation, destruction and various other operations.

One of the core objects in the Whitix network stack is the socket. It abstracts the idea of a network connection between two computers or two programs, and several operations can be performed on a socket. It is used internally by the network stack to represent a connection; the userspace programs only deal with a file descriptor.

Contents

Overview

Data structure

/* Taken from Whitix 0.1 source. */
struct Socket
{
	int state;
	int family;
	int type;
	struct SocketAddr addr;
	struct SocketOps* socketOps;
 
	/* Since a server socket and server's children sockets are used for
	 * entirely different purposes, we can optimize structure size by
	 * using a union.
	 */
	union {
		struct {
			struct ListHead recvPackets, sendPackets;
		};
 
		struct ListHead childSocks;
	};
	struct ListHead next;
	struct Socket* parent;
	struct File* file;
	struct NetDevice* device;
	void* protoInfo;
};

Operations

Creating a socket

Sockets are created by the SocketCreate function, (line 93) which returns a Socket* for use in later socket functions. The prototype is:

struct Socket* SocketCreate(
    int domain,
    int type
)
The call graph of SocketCreate. A dark gray background signifies that the function is in the socket subsystem, and a rhombus denotes a function pointer.
The call graph of SocketCreate. A dark gray background signifies that the function is in the socket subsystem, and a rhombus denotes a function pointer.

domain can be either PF_LOCAL or PF_INET (local or internet sockets), and type depends on the socket family (see family-specific documentation for more information on the parameter).

SocketCreate first checks whether the address family has been registered (that is, if socket operations exist for that particular family). It then allocates a socket structure from the socket cache, and fills in basic information about the socket (including its domain, type and parent) and initialises its lists: sendPackets and recvPackets.

At this point, family-specific socket creation can begin. This depends on the socket family; for example, the local socket code sets the private field of the socket to a custom data structure, which contains the Socket* that the socket will be attached to.

Attaching to a file descriptor

Call graph of SysSocketCreate. Functions with a dark gray background are part of the network subsystem.
Call graph of SysSocketCreate. Functions with a dark gray background are part of the network subsystem.

The system call SysSocketCreate returns a file descriptor for use in later socket system calls. However, SocketCreate only returns a Socket*, and not a file descriptor. After calling SocketCreate,SysCreateSocket performs two more operations to link the socket to the file descriptor:

  • Allocating a free file descriptor. SysSocketCreate calls VfsGetFreeFd to allocate a file descriptor.
  • Link socket to file structure. SysSocketCreate calls SocketCreateFile, which performs the actual linking of the socket to the file structure.

SysSocketCreate then returns the file descriptor allocated by VfsGetFreeFd.

SocketCreateFile

SocketCreateFile takes two parameters, the file structure and the socket structure. (line 251) The function sets up the file structure, allocating an empty vNode and assigning sockFileOps to the file's operations. The socket structure becomes the file's extraInfo field, and socketSuperBlock becomes the node's superblock, so that the FreeVNode operation can be handled appropriately.

Connecting a socket

The SysSocketConnect system call retrieves a Socket* using SocketGet, and calls the family-specific connect operation for that socket. See the family-specific documentation for more information.

System call interface

See the system call list for a list of network system calls
Personal tools