Reseau: Linux socket cheatSheet
Reseau: Linux socket cheatSheet

Linux, langage C

Directory

todo

1. Interaction client serveur

2. Port and Service Functions

Unix provides the following functions to fetch service name from the /etc/services file.

struct servent *getservbyname(char *name, char *proto)

This call takes a service name and a protocol name and returns the corresponding port number for that service.

struct servent *getservbyport(int port, char *proto)

This call takes a port number and a protocol name and returns the corresponding service name.

3. Byte Ordering Functions

unsigned short htons (unsigned short hostshort) 

This function converts 16-bit (2-byte) quantities from host byte order to network byte order.

unsigned long htonl (unsigned long hostlong) 

This function converts 32-bit (4-byte) quantities from host byte order to network byte order.

unsigned short ntohs (unsigned short netshort) 

This function converts 16-bit (2-byte) quantities from network byte order to host byte order.

unsigned long ntohl (unsigned long netlong) 

This function converts 32-bit quantities from network byte order to host byte order.

4. IP Address Functions

int inet_aton (const char *strptr, struct in_addr *addrptr) 

This function call converts the specified string,in the Internet standard dot notation, to a network address, and stores the address in the structure provided. The converted address will be in Network Byte Order (bytes ordered from left to right). It returns 1 if the string is valid and 0 on error.

in_addr_t inet_addr (const char *strptr)

This function call converts the specified string, in the Internet standard dot notation, to an integer value suitable for use as an Internet address. The converted address will be in Network Byte Order (bytes ordered from left to right). It returns a 32-bit binary network byte ordered IPv4 address and INADDR_NONE on error.

char *inet_ntoa (struct in_addr inaddr) 

This function call converts the specified Internet host address to a string in the Internet standard dot notation.

5. Socket Core Functions

int socket (int family, int type, int protocol) 

This call returns a socket descriptor that you can use in later system calls or it gives you -1 on error.

int connect (int sockfd, struct sockaddr *serv_addr, int addrlen) 

The connect function is used by a TCP client to establish a connection with a TCP server. This call returns 0 if it successfully connects to the server, otherwise it returns -1.

int bind(int sockfd, struct sockaddr *my_addr,int addrlen) 

The bind function assigns a local protocol address to a socket. This call returns 0 if it successfully binds to the address, otherwise it returns -1.

int listen(int sockfd, int backlog) 

The listen function is called only by a TCP server to listen for the client request. This call returns 0 on success, otherwise it returns -1.

int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen) 

The accept function is called by a TCP server to accept client requests and to establish actual connection. This call returns a non-negative descriptor on success, otherwise it returns -1.

int send(int sockfd, const void *msg, int len, int flags) 

The send function is used to send data over stream sockets or CONNECTED datagram sockets. This call returns the number of bytes sent out, otherwise it returns -1.

int recv (int sockfd, void *buf, int len, unsigned int flags) 

The recv function is used to receive data over stream sockets or CONNECTED datagram sockets. This call returns the number of bytes read into the buffer, otherwise it returns -1 on error.

int sendto (int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen) 

The sendto function is used to send data over UNCONNECTED datagram sockets. This call returns the number of bytes sent, otherwise it returns -1 on error.

int recvfrom (int sockfd, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen) 

The recvfrom function is used to receive data from UNCONNECTED datagram sockets. This call returns the number of bytes read into the buffer, otherwise it returns -1 on error.

int close (int sockfd) 

The close function is used to close a communication between the client and the server. This call returns 0 on success, otherwise it returns -1.

int shutdown (int sockfd, int how) 

The shutdown function is used to gracefully close a communication between the client and the server. This function gives more control in comparison to close function. It returns 0 on success, -1 otherwise.

int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) 

This function is used to read or write multiple sockets.

6. Socket Helper Functions

int write (int fildes, const void *buf, int nbyte) 

The write function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned.

int read (int fildes, const void *buf, int nbyte) 

The read function attempts to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf. Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned.

int fork (void) 

The fork function creates a new process. The new process, called the child process, will be an exact copy of the calling process (parent process).

void bzero (void *s, int nbyte) 

The bzero function places nbyte null bytes in the string s. This function will be used to set all the socket structures with null values.

int bcmp (const void *s1, const void *s2, int nbyte) 

The bcmp function compares the byte string s1 against the byte string s2. Both the strings are assumed to be nbyte bytes long.

void bcopy (const void *s1, void *s2, int nbyte) 

The bcopy function copies nbyte bytes from the string s1 to the string s2. Overlapping strings are handled correctly.

void *memset(void *s, int c, int nbyte) 

The memset function is also used to set structure variables in the same way as bzero.

7. Linux Socket: Structures

sockaddr

Contient des informations sur le socket

struct sockaddr {
   unsigned short   sa_family;
   char             sa_data[14];
};

Structure d’adresse générique qui sera passé dans la majorité des appels de fonction.

description des membres:

attribut valeur description
sa_family AF_INET
AF_UNIX
AF_NS
AF_IMPLIK
représente une famille d’adresses (ipv4/6). On utilise généralement AF_INET (protocole IP)
sa_data Protocol-specific Adress les 14 bytes de ce champ sont interprété en fonction du type d’adresse.

sockaddr_in

struct sockaddr_in {
   short int            sin_family;
   unsigned short int   sin_port;
   struct in_addr       sin_addr;
   unsigned char        sin_zero[8];
};

Permet de référencer (pointer) les divers éléments du socket

description des membres:

attribut valeur description
sa_family AF_INET
AF_UNIX
AF_NS
AF_IMPLIK
représente une famille d’adresses (ipv4/6). On utilise généralement AF_INET (protocole IP)
sin_port Service Port numéro de port sur 16 bits (Network Byte Order)
sin_addr IP Address adresse IP sur 32 bits (Network Byte Order)
sin_zero ne s’utilise pas/plus valeur à set sur NULL

in_addr

struct in_addr {
   unsigned long s_addr;
};

Cette structure est uniquement utilisée dans la structure précédente et contient le netid/hostid sur 32 bits

description des membres:

attribut valeur description
s_addr service port adresse IP sur 32 bits (Network Byte Order)

hostent

struct hostent {
   char  *h_name; 
   char **h_aliases; 
   int    h_addrtype;  
   int    h_length;    
   char **h_addr_list
	
// define h_addr  h_addr_list[0]
};

Contient des informations sur l’hôte

description des membres:

attribut valeur description
h_name url nom de domaine de l’hôte
h_aliases TI liste d’alias de l’hôte
h_addrtype AF_INET représente une famille d’adresses (ipv4/6). On utilise généralement AF_INET (protocole IP)
h_lenght 4 longeur de l’ip (byte). 4 pour IP
h_addr_list in_addr liste de pointeurs (pour le protocol IP)

note: h_addr est définis comme étant h_addr_list[0] pour assurer une retro compatibilité

servent

struct servent {
   char   *s_name; 
   char  **s_aliases; 
   int     s_port;  
   char   *s_proto;
};

Contient des informations sur les service et les ports associés

description des membres:

attribut valeur description
s_name http nom officiel du serveur. (SMTP, FTP, POP3,…)
s_aliases ALIAS liste d’alias du service. Le plus souvent sera set à NULL
s_port 80 numéro de port associé (ex. Si HTTP => 80)
s_proto TCP
UDP
protocole utilisé

8. exemple fork()

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main(int arc, char **argv) {
	printf("--beginning of program\n");

	int counter = 0;
	sleep(2);
	printf("creating child\n");
	pid_t pid = fork(); 

	if (pid == 0) {
		// child process
		for (int i = 0; i < 10; ++i) {
			sleep(3);
			// printf("child process: counter = %d\n", ++counter);
		}
	}
	else if (pid > 0) {
		// parent process
		for (int j = 0; j < 10; ++j) {
			sleep(3);
			// printf("parent process: counter = %d\n", ++counter);

			if (j == 5) {
				printf("Exit parent process... child alone.\n");
				printf("Child will display a message when he is done (21sec).\n");
				printf("user prompt should be available:\n");
				exit(0); 
			}
		}
	}
	else {
		// fork failed
		printf("fork() failed!\n");
		return 1;
	}

	printf("--end of child process\n");

	return 0;
}

8. divers

strcmp

int strcmp(const char *str1, const char *str2)

Parameters:

  • str1 – This is the first string to be compared.
  • str2 – This is the second string to be compared.

Return Value:

  • if Return value < 0 then it indicates str1 is less than str2.
  • if Return value > 0 then it indicates str2 is less than str1.
  • if Return value = 0 then it indicates str1 is equal to str2.