Creating sockets
In C language:
It is a means of communication, originally implemented by the University of Berkeley, between applications.
When using the "socket library" in C language, you should include the header files corresponding to this system library and the constants used.
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
The first step is to create a socket via the system call:
int socket (int domaine, int type, int protocole);
- the first parameter is the domain. It is an integer which can take as its value one of the following constants:
- AF_INET: to work with IPv4 as network layer.
- AF_INET6: to work with IPv6 as network layer.
- AF_PACKET: to operate directly above the link layer (layer 2) without using the implementation of a network layer (layer 3) of the kernel.
- AF_UNIX: to work with unix sockets and have communication limited to processes residing on the same machine.
In some cases, this constant is replaced by the synonym AF_LOCAL, which incidentally belongs to Posix terminology.
- The second parameter is the type. It is an integer which can take as its value one of the following constants:
- SOCK_STREAM: the dialogue takes place in connected mode, with flow control from one end of the communication to the other.
- SOCK_DGRAM: communication takes place without connection, by transmission of data packets (i.e. by exchange of "datagram").
- SOCK_RAW: the socket will be used to communicate raw with the indicated protocol. This is what to use with the AF_PACKET domain value.
- The third parameter is the transport protocol used. It is an integer which can take as its value one of the following constants:
- NULL: if the default protocol is chosen for the type of communication indicated.
- IPPROTO_TCP: indicates the TCP protocol. This is the default if the type is SOCK_STREAM.
- IPPROTO_UDP: indicates the UDP protocol. This is the default if the type is SOCK_DGRAM.
- IPPROTO_ICMP: indicates the ICMP protocol. This is one of two values to choose when the domain is AF_INET (or AF_INET6) and the type is SOCK_RAW.
- IPPROTO_RAW: indicates direct communication with the network layer of the indicated domain. This is one of two values to choose when the domain is AF_INET (or AF_INET6) and the type is SOCK_RAW.
- In the case of the AF_PACKET domain, the value of the protocol field will be indicated in the Ethernet frame. If you want to receive all frame types, you will use htons(ETH_P_ALL).
- The returned value is a positive or null integer corresponding to the number of the descriptor allocated by the socket that has just been created or a negative value to indicate an error:
- EMFILE: constant corresponding to a full file table in the kernel.
- ENFILE: constant corresponding to the fact that the limit of the total number of open files on the system has been reached.
- ENOMEM: constant corresponding to the fact that there is not enough space to allocate the necessary buffers. The socket cannot be created until enough resources are freed.
- EINVAL: constant corresponding to a non-existent domain.
- EPROTONOSUPPORT: constant corresponding to a type inconsistent with the protocol or the domain
- EACCES: constant corresponding to the absence of authorization to create a socket of the requested type (for example AF_INET and SOCK_RAW).
Notes:
- In this course, we will only use TCP or UDP internet sockets which are therefore created via one of the following two instructions:
fd_tcp = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
fd_udp = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- We have only reserved the socket management memory in the kernel, no communication has yet been initiated.
- There is a library equivalent to the "socket library" for Windows: The "Winsock library" which basically works on the same principle.
- In languages like PHP or Python, there are libraries interfacing with system functions.
$socket_tcp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$socket_udp = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)