Escolar Documentos
Profissional Documentos
Cultura Documentos
Networks
EE / CS 6713
Implementation
Dr. Amir Qayyum
1
Elements of a Protocol
Implementation
Outline
Service Interface
Process Model
Common Subroutines
Example Protocol
Network Software
Major factors for runaway success of the Internet:
most functionalities provided by software running on
general-purpose computers
new services can be added readily with just a small matter of
programming
Socket API
Use sockets as abstract endpoints of
communication
Issues
Creating & identifying sockets
Sending & receiving data
Mechanisms
UNIX system calls and library routines
socket
process
6
Socket API
Creating a socket
int socket(int domain, int type, int protocol)
domain (family) = AF_INET, PF_UNIX, AF_OSI
type = SOCK_STREAM, SOCK_DGRAM
protocol = TCP, UDP, UNSPEC
return value is a handle for the newly created
socket
Sockets (cont)
Passive Open (on server)
int bind(int socket, struct sockaddr *addr, int
addr_len)
int listen(int socket, int backlog)
int accept(int socket, struct sockaddr *addr, int
addr_len)
Sockets (cont)
Sending Messages
int send(int socket, char *msg, int mlen, int flags)
Receiving Messages
int recv(int socket, char *buf, int blen, int flags)
Protocol-to-Protocol Interface
A protocol interacts with a lower level protocol like
an application interacts with underlying network
Why not using available network APIs for PPI ?
Inefficiencies built into the socket interface
application programmer tolerate them to simplify their task
inefficiency at one level
protocol implementers do not tolerate them
inefficiencies at several layers of protocols
10
Process Model
avoid context switches
Buffer Model
avoid data copies
11
Process Model
procedure
call
inter-process
communication
(a)
Process-per-Protocol
(b)
Process-per-Message
12
IP
Buffer Model
Application process
buffer copy
buffer copy
send()
deliv er()
Topmost protocol
14
Message Library
Add header
m
Strip header
m
abcdefg
abcdefg
m
defg
+ hdr = abc
xyzabcdefg
15
m1
abcdefg
Reassemble messages
m2
abcd
efg
defg
abc
abcdefg
16
Network Programming
Things to learn
Internet protocols (IP, TCP, UDP, )
Sockets API (Application Programming Interface)
OSI Model
application layers
Internet Protocols
FTP
HTTP
NV
UDP
TCP
transport
network
TFTP
IP
data link
FDDI
Ethernet
ATM
physical
18
Programming
learn to use Internet for
communication (with
transport
focus on implementation
of networking concepts)
network
data link
physical
Principles
and concepts learn to build network
from ground up
19
Socket Programming
Reading: Stevens 2nd edition, Chapter 1-6
Sockets API: a transport layer service interface
Outline
Client-server model
TCP connections
UDP services
Addresses and data
Sockets API
Example of use
21
Client-Server Model
Asymmetric relationship
Server/daemon
well-known name
waits for contact
process requests, sends
replies
Client
server
client
client
client
initiates contact
waits for response
22
Client-Server Model
Bidirectional communication channel
Service models
sequential: server processes only one clients requests at
a time
concurrent: server processes multiple clients requests
simultaneously
hybrid: server maintains multiple connections, but
processes requests sequentially
TCP Connections
Transmission Control Protocol, at OSI transport
layer
Recall: each protocol provides service interface
Aspects of TCP service
transfers a stream of bytes (interpreted by application)
connection-oriented
set up connection before communicating
tear down connection when done
Flow control
prevents senders from wasting bandwidth
reduces global congestion problems
TCP Connections
TCP connection setup via 3-way handshake
J and K are sequence numbers for messages
SYN J
client
SYN K
ACK J+1
ACK K+1
server
Hmmm
RTT is
important!
26
TCP Connections
TCP connection teardown (4 steps) (either client
or server can initiate connection teardown)
active close
FIN J
ACK J+1
passive close
closes connection
client
FIN K
ACK K+1
server
Hmmm
Latency
matters!
27
UDP Services
User Datagram Protocol, at OSI transport layer
Thin layer over IP
Aspects of services
29
Endianness
Machines on Internet have different endianness
little-endian (Intel, DEC): least significant byte of word
stored in lowest memory address
big-endian (Sun, SGI, HP): most significant byte...
network byte order is big-endian
use of network byte order
imperative for some data (e.g., IP addresses)
good form for all binary data (e.g., application-specific)
ASCII/Unicode are acceptable alternatives
30
Endianness
16- / 32-bit conversion (for platform independence)
int m, n; // int32
short int s, t; // int16
m = ntohl(n)
s = ntohs(t)
n = htonl(m)
t = htons(s)
/* 32-bit IP address */
/* e.g., AF_INET */
/* TCP / UDP port */
/* IP address */
32
Address Conversion
All binary values used and returned by these functions
are network byte ordered
struct hostent* gethostbyname (const char* hostname);
Address Conversion
in_addr_t inet_addr (const char* strptr);
Sockets API
35
Error model
standardization of return value
0 on success, -1 on failure
NULL on failure for routines returning pointers
errno variable
36
Client-Server Connection
Talk to
mail.yahoo.com,
mail.yahoo.com
my-machine
port b
I am
mail.yahoo.com,
port b
I accept
connections
client
server
I will talk to
my-machine,
port a
37
Client-Server Connection
Talk to
mail.yahoo.com,
mail.yahoo.com
my-machine
port b
1. socket()
2. bind()
4. socket()
5. connect()
client
7. send() / sendto()
8. recv() / recvfrom()
9. close() / shutdown()
I am
mail.yahoo.com,
port b
I accept
connections
server
6. accept()
3. listen()
I will talk to
my-machine,
port a
38
46
51
Advanced Sockets
Managing multiple connections
fork()/exec(): multiple server processes
pthread_create(): multi-threaded server process
(no calls): event-based server process
Example of Use
Taken from Beejs Guide to Network
Programming (see the course web page)
Client-server example using TCP
For each client
Server forks new process to handle connection
Sends Hello, world
55
56
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on
new_fd */
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
/* automatically fill with my IP (w/o Beejs bug) */
61
information */
if (argc != 2) {
fprintf (stderr, usage: client hostname\n); exit(1);
}
if ((he = gethostbyname (argv[1])) == NULL) {
/* get the host info */
TCP Connection
socket
bind
listen
socket
connect
client
connect
completes
SYN J
SYN K
ACK J+1
ACK K+1
connection added to
incomplete queue
server
connection moved
to complete queue
accept
66
TCP Connection
socket
connect
client
socket
bind
listen
server
accept
write
read
read
close
write
close
67
UDP Connection
socket
socket
sendto
client
bind
server
recvfrom
sendto
recvfrom
close
68