Introduction to Sockets

29
Titolo presentazione sottotitolo Milano, XX mese 20XX Piattaforme Software per la Rete Socket Programming A.A. 2016/17 Federico Reghenzani

Transcript of Introduction to Sockets

Page 1: Introduction to Sockets

Titolo presentazionesottotitolo

Milano, XX mese 20XX

Piattaforme Software per la Rete

Socket Programming

A.A. 2016/17Federico Reghenzani

Page 2: Introduction to Sockets

Federico Reghenzani 2/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Outline

1) Introduction to Sockets

2) UDP communication

3) TCP communication

4) RAW communication

5) Advanced Socket Programming

Page 3: Introduction to Sockets

Introduction to Sockets

Page 4: Introduction to Sockets

Federico Reghenzani 4/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Sockets

● What is a socket?

– A socket is a logical endpoint of a communication link

● What is a socket in real-life?

– A file descriptor → a number provided by the kernel representing an (abstract) file

Page 5: Introduction to Sockets

Federico Reghenzani 5/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket (0/4)

int socket(int domain, int type, int protocol)

Page 6: Introduction to Sockets

Federico Reghenzani 6/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket (1/4)

int socket(int domain, int type, int protocol)

The domain argument – called also socket family – specify the protocol family to be used for communication.

Possible values are:

● AF_UNIX: local communication

● AF_INET: IPv4 protocol family

● AF_INET6: IPv6 protocol family

● AF_NETLINK: Netlink protocol

● …

we will use this

Page 7: Introduction to Sockets

Federico Reghenzani 7/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket (2/4)

int socket(int domain, int type, int protocol)

The type argument specify the communication semantics.

Possible values are:

● SOCK_STREAM: sequenced, reliable, two-way, connection-based

● SOCK_DGRAM: unreliable, connectionless

● SOCK_SEQPACKET: similar to SOCK_STREAM, but it cannot be fragmented

● SOCK_RAW: raw protocol access

● …

often used with AF_UNIX, we will not see this

Page 8: Introduction to Sockets

Federico Reghenzani 8/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket (3/4)

int socket(int domain, int type, int protocol)

The protocol argument can be used to force a specific protocol.

Possible values are:

● 0: leave the OS to select the best protocol for domain & type specified

● A constant representing a protocol variant, e.g. IPPROTO_ICMP, IPPROTO_UDPLITE

Page 9: Introduction to Sockets

Federico Reghenzani 9/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket (4/4)

int socket(int domain, int type, int protocol)

It returns:

● -1 on error (check the errno variable)

● > 0: the file descriptor

Page 10: Introduction to Sockets

Federico Reghenzani 10/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Getting a socket – Examples

How to…

● get a TCP socket:

● get a UDP socket:

● get a RAW socket that uses ICMP communication:

int my_sock = socket(AF_INET, SOCK_STREAM, 0);

int my_sock = socket(AF_INET, SOCK_DGRAM, 0);

int my_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

Page 11: Introduction to Sockets

UDP communication

Page 12: Introduction to Sockets

Federico Reghenzani 12/29

Dipartimento di Elettronica, Informazione e Bioingegneria

UDP Communication schema

socket()

Server Client

bind()

recvfrom()

socket()

sentdo()

sendto()

recvfrom()

close()close()

Page 13: Introduction to Sockets

Federico Reghenzani 13/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Sockets Addresses

● Socket addresses depend on used protocolstruct sockaddr { sa_family_t sa_family; char sa_data[14];};

struct sockaddr_in { sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr;};

struct in_addr { uint32_t s_addr;};

Page 14: Introduction to Sockets

Federico Reghenzani 14/29

Dipartimento di Elettronica, Informazione e Bioingegneria

bind()

● This operation “assigns a name to a socket”, i.e. request the assignment of an address to a socket

● Usually used in server-side

● It may fail if the address is already assigned to another process

int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen)

0: success, 1: error0: success, 1: errorFile descriptor of the socket

The address to bindThe size of theprevious parameter

Page 15: Introduction to Sockets

Federico Reghenzani 15/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Send/Receive datagrams

int sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)

● Send the message message of size (in bytes) length through socket socket to address dest_addr that has a length of dest_len.

int recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t address_len)

● Receive a message from socket socket and put it into the location pointed by buffer of maximum length of length and set the source address in the address struct of length address_len.

Page 16: Introduction to Sockets

TCP Communication

Page 17: Introduction to Sockets

Federico Reghenzani 17/29

Dipartimento di Elettronica, Informazione e Bioingegneria

TCP Communication schema

socket()

Server Client

bind()

socket()

connect()

send() / recv() send() / recv()

close()close()

listen()

accept()

Page 18: Introduction to Sockets

Federico Reghenzani 18/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Listen and accept connections

int listen(int sockfd, int backlog)

● Set the socket sockfd as a passive socket, able to queue a maximum of backlog pending connections

● It extracts the first connection in the queue of pending connections. If empty, it waits.

● When a connection is extracted, a new socket is created and returned.

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

Page 19: Introduction to Sockets

Federico Reghenzani 19/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Connect to a server

● Used by the client to connect the socket to the server specified by the address addr

● It returns 0 on success, -1 on error

int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen)

Page 20: Introduction to Sockets

Federico Reghenzani 20/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Send/Receive in TCP

int sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)

int send(int socket, const void *message, size_t length, int flags)

int recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t address_len)

int recv(int socket, void *buffer, size_t length, int flags)

Page 21: Introduction to Sockets

RAW Communication

Page 22: Introduction to Sockets

Federico Reghenzani 22/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Why RAW sockets?

● Deal with “control-protocols” that uses layer 3

– e.g. ICMP

● Implement new transport-layer protocol in user-space

● Security-related applications

– Traffic inspection, port scanners, etc.

● You need the root privileges to create a raw socket

Page 23: Introduction to Sockets

Federico Reghenzani 23/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Sending RAW data

● You have to…

– Create the IP Header (you can delegate this to the OS)

– Create the any other headers (e.g. TCP)

– Calculate all the necessary checksums

● Bugs may cause in the best option a lost message, but also network problems

Page 24: Introduction to Sockets

Advanced Socket Programming

Page 25: Introduction to Sockets

Federico Reghenzani 25/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Serving multiple clients (1/3)

● How can a server deal with multiple simultaneous TCP connections?

● If the connection duration is short, they can be managed sequentially provided a sufficient connection queue size

– PRO: easy to develop

– CONS: waiting time in the queue may be too long, doesn’t work with long connections, doesn’t scale well

● Solution: multi-threading

Page 26: Introduction to Sockets

Federico Reghenzani 26/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Serving multiple clients (2/3)

● Standard multi-thread server:

– One thread continues to perform accept() waiting for new clients (the “listening thread”)

– When a new client is connected, create a new thread that manages only that client

● PRO: enables multiple persistent connections, reduces the response time

● CONS: cost of creating new threads, high number of clients means high number of threads (DoS attack?)

Page 27: Introduction to Sockets

Federico Reghenzani 27/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Serving multiple clients (3/3)

● Multi-thread server from thread pool:

– One thread continues to perform accept() waiting for new clients (the “listening thread”)

– When a new client is connected, a thread from the pool is woken up and it starts managing the client

– If no thread free avaiable, the clients will wait in the queue or the thread-pool may accordingly scale

● PRO/CONS: a balanced solution from the previouses

Page 28: Introduction to Sockets

Federico Reghenzani 28/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Non-blocking operations

● All operations we have seen are blocking, i.e. they do not return until the operation is complete

– e.g. the accept() call blocks until a new connection arrives

● The OS preempts the process and puts it into I/O waiting state

● You can specify the O_NONBLOCK flag to the socket file descriptor (via the fctnl() call) or specify SOCK_NONBLOCK at socket creation as bitwise or with the type:

int my_sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);

Page 29: Introduction to Sockets

Federico Reghenzani 29/29

Dipartimento di Elettronica, Informazione e Bioingegneria

Other socket options

● Several others socket options are configurable via the setsockopt() call:

● For example:

– Send/Receive timeout

– Send/Receive buffer size

– Sharing a port with another process during bind()

– …

int setsockopt(int sockfd, int level, int option_name,const void* option_value, socklen_t option_len)