Network Programming
Network Programming
2
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2
Internet
● US Military
○ DARPANet
○ Build a network capable of communicating while withstanding failures
● TCP/IP designed with reliability and fault tolerance
● What about security?
3
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Protocol Suite
4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4
TCP/IP Protocol Stack
5
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5
Layers
[Link]
6
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Layers
[Link]
7
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7
Layers
[Link]
8
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8
Physical and Datalink Layer
9
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9
MAC Address
10
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10
Network Layer
11
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11
Network Layer
12
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12
Network Layer
13
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13
Internet Protocol
● Universal protocol that all devices use to transmit data over the
Internet
● IP address: An address that identifies a device on the Internet
○ IPv4 is 32 bits, typically written as 4 decimal octets, e.g. [Link]
○ IPv6 is 128 bits, typically written as 8 groups of 2 hex bytes:
fe80::83a:1a1f:6871:2efb
● Unique addresses that help nodes make decisions on where to
forward the packet
14
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14
Reliability
15
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15
Transport Layer
16
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16
Transport Layer
● Provides resilience to
○ Data corruption
■ Payload checksum
○ Data loss
■ Sequence numbers, timeouts + retransmits
○ Out-of-order delivery
■ Sequence numbers
○ Congestion
■ Flow control, AIMD
● UDP is suitable when error checking is not necessary or performed in the
application
○ Avoids overhead of such processing at the network level
○ Time-sensitive applications often use UDP
17
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17
Application Layer
18
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18
Naming and Identification
19
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
DNS
● What is a socket?
○ To the kernel, a socket is an endpoint of communication
○ To an application, a socket is a file descriptor that lets the application
read/write from/to the network
● Using the FD abstraction lets you reuse code & interfaces
○ Clients and servers communicate with each other by reading from and
writing to socket descriptors
● The main distinction between regular file I/O and socket I/O is
how the application “opens” the socket descriptors
open_listen
fd
open_client
fd
terminal 3. Exchange
Client / read socket read data
socket write
Server
socket read
Session socket
terminal
write
write
EOF
close socket read
5. Drop client
4. Disconnect client
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27
Start client Start server
Client Server
getaddrinfo getaddrinfo
socket socket
open_listenfd
open_clientfd bind
listen
Connection
request
connect accept
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28
Echo Client-Server
int main(int argc, char **argv)
{
int clientfd;
char *host, *port, buf[MAXLINE];
rio_t rio;
host = argv[1];
port = argv[2];
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio, clientfd);
while (Fgets(buf, MAXLINE, stdin) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
Rio_readlineb(&rio, buf, MAXLINE);
Fputs(buf, stdout);
}
Close(clientfd);
exit(0);
}
struct sockaddr {
uint16_t sa_family; /* Protocol family */
char sa_data[14]; /* Address data. */
};
sa_family
Family Specific
struct sockaddr_in {
uint16_t sin_family; /* Protocol family (always AF_INET) */
uint16_t sin_port; /* Port num in network byte order */
struct in_addr sin_addr; /* IP addr in network byte order */
unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */
};
sin_port sin_addr
AF_INET 0 0 0 0 0 0 0 0
sa_family
sin_family
Family Specific
NULL
ai_addr
ai_next
NULL
ai_addr
NULL
listen
Connection
request
connect accept
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33
Sockets Interface: socket
⬛ Clients and servers use the socket function to create a
socket descriptor:
int socket(int domain, int type, int protocol)
⬛ Example:
int clientfd = socket(AF_INET, SOCK_STREAM, 0); Protocol specific!
listen
Connection
request
connect accept
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35
Sockets Interface: bind
⬛ A server uses bind to ask the kernel to associate the
server’s socket address with a socket descriptor:
int bind(int sockfd, SA *addr, socklen_t addrlen);
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37
Sockets Interface: listen
⬛ Kernel assumes that descriptor from socket function is an
active socket that will be on the client end
⬛ A server calls the listen function to tell the kernel that a
descriptor will be used by a server rather than a client:
int listen(int sockfd, int backlog);
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 39
Sockets Interface: accept
⬛ Servers wait for connection requests from clients by
calling accept:
int accept(int listenfd, SA *addr, int *addrlen);
rio_readlin
rio_writen
Client / eb
Server
Await connection
Session rio_readline
rio_writen
b request from
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 41
Sockets Interface: connect
⬛ A client establishes a connection with a server by calling
connect:
int connect(int clientfd, SA *addr, socklen_t addrlen);
Connection
listenfd
request 2. Client makes connection request by
Client Server calling and blocking in connect
clientfd
listenfd
3. Server returns connfd from
Client Server accept. Client returns from connect.
clientfd connfd
Connection is now established between
clientfd and connfd
⬛ Connected descriptor
▪ End point of the connection between client and server
▪ A new descriptor is created each time the server accepts a
connection request from a client
▪ Exists only as long as it takes to service client
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 45
Client Server
getaddrinfo getaddrinfo
socket socket
open_listenfd
open_clientfd bind
listen
Connection
request
connect accept
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 46
Sockets Helper: open_clientfd
⬛ Establish a connection with a server
int open_clientfd(char *hostname, char *port) {
int clientfd;
struct addrinfo hints, *listp, *p;
NULL
ai_addr
ai_next
NULL
ai_addr
NULL
⬛ Clients: walk this list, trying each socket address in turn, until the calls to
socket and connect succeed.
⬛ Servers: walk the list calling socket, listen, bind for all addresses, then use
select to accept connections on any of them (beyond our scope)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 48
Sockets Helper: open_clientfd (cont)
/* Walk the list for one that we can successfully connect to */
for (p = listp; p; p = p->ai_next) {
/* Create a socket descriptor */
if ((clientfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) < 0)
continue; /* Socket failed, try the next */
/* Clean up */
Freeaddrinfo(listp);
if (!p) /* All connects failed */
return -1;
else /* The last connect succeeded */
return clientfd;
} csapp.c
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 49
Client Server
getaddrinfo getaddrinfo
socket socket
open_listenfd
open_clientfd bind
listen
Connection
request
connect accept
rio_readlin
Client / rio_writen
eb
Server
Session rio_readline
Await connection
rio_writen request from
b
next client
EOF rio_readlin
close
eb
close
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 50
Sockets Helper: open_listenfd
⬛ Create a listening descriptor that can be used to accept
connection requests from clients.
int open_listenfd(char *port)
{
struct addrinfo hints, *listp, *p;
int listenfd, optval=1;
csapp.c
A production server would not break out of the loop on the first success.
We do that for simplicity only.
⬛ Usage:
▪ linux> telnet <host> <portnumber>
▪ Creates a connection with a server running on <host> and
listening on port <portnumber>
[Link]
⬛ HTTP standard requires that each text line end with “\r\n”
⬛ Blank line (“\r\n”) terminates request and response headers 59
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
HTTP Requests
⬛ HTTP request is a request line, followed by zero or more
request headers
⬛ Response line:
<version> <status code> <status msg>
▪ <version> is HTTP version of the response
▪ <status code> is numeric status
▪ <status msg> is corresponding English text
▪ 200 OK Request was handled without error
▪ 301 Moved Provide alternate URL
▪ 404 Not foundServer couldn’t find the file
⬛ Response headers: <header name>: <header data>
▪ Provide additional information about response
▪ Content-Type: MIME type of content in response body
▪ Content-Length: Length of content in response body
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 61