You are on page 1of 32

UNIX Network Programming

2nd Edition

UNIX Network Programming 1


Chapter 1. Introduction

• Contents
– Introduction
– A Simple Daytime Client
– Error Handling: Wrapper Functions
– A Simple Daytime Server
– OSI Model
– Unix Standards

UNIX Network Programming 2


1.1 Introduction
• Client / Server
Communication link
Client Server

Figure 1.1 Network application : client and server

Client
...
Client Server
...
Client
Figure 1.2 Server handling multiple clients at the same time.
UNIX Network Programming 3
1.1 Introduction (continued)
• Example : Client and Server on the same Ethernet
communication using TCP
User Application protocol
Web Web Application layer
process Client server

TCP protocol
TCP TCP transport layer

Protocol stack
within kernel
IP IP protocol IP network layer

Ethernet Ethernet protocol Ethernet datalink layer


driver driver
Actual flow between client and server

Ethernet
Figure 1.3 Client and server on the same Ethernet communicating using TCP

UNIX Network Programming 4


1.1 Introduction (continued)
• Example : Client and Server on different LANs connected
through WAN.
client server
application application

Host Host
with with
TCP/IP TCP/IP

LAN LAN

router router

WAN
router router router router

Figure 1.4 Client and server on different LANs connected through a WAN

UNIX Network Programming 5


1.2 A Simple Daytime Client
1 #include “unp.h”

int
main (int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;

if ( argc != 2) err_quit(“usage: a.out <IPaddress>”);

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)


err_sys(“socket error”);
2 bzero( &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit(“inet_pton error for %s”,argv[1]);
3 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys(“connect error”);

while ( (n = read(sockfd, recvline, MAXLINE)) > 0 ) {


4 recvline[n] = 0; /* null termicate */
if ( fputs(recvline, stdout) == EOF)
err_sys(“fputs error”);
}
if ( n < 0 ) err_sys ( “read error “);
5 exit(0);
}

UNIX Network Programming 6


1.3 protocol Independence
• #include "unp.h"
• int main(int argc, char **argv)
• {
• int sockfd, n;
• struct sockaddr_in6 servaddr;
• char recvline[MAXLINE + 1];

• if (argc != 2) err_quit("usage: a.out <IPaddress>");

• if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0)


• err_sys("socket error");

• bzero(&servaddr, sizeof(servaddr));
• servaddr.sin6_family = AF_INET6;
• servaddr.sin6_port = htons(13); /* daytime server */
• if (inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr) <= 0)
• err_quit("inet_pton error for %s", argv[1]);

• if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)


• err_sys("connect error");

• while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {


• recvline[n] = 0; /* null terminate */
• if (fputs(recvline, stdout) == EOF)
• err_sys("fputs error");
• }
• if (n < 0)
• err_sys("read error");

• exit(0);
• }

UNIX Network Programming 7


1.4 Error Handling: Wrapper Functions
• Since terminating on an error is the common case, we can shorten our program
by defining a wrapper function that performs the actual function call, tests the
return value, and terminates on an error.

• sockfd = Socket(AF_INET, SOCK_STREAM, 0);


int
Socket(int family, int type, int protocol)
{
int n;
if( (n = socket( family, type, protocol)) < 0 )
err_sys(“socket error”);
return (n);
}

Figure 1.7 Our wrapper function for the socket function

• Unix errno Value

UNIX Network Programming 8


1.5 A Simple Daytime Server
#include “unp.h”
#include <time.h>

int
main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;

listenfd = Socket(AF_INET, SOCK_STREAM, 0);


1 bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13); /* daytime server */
2
Bind(listenfd, (SA *) *servaddr, sizeof(servaddr) );

Listen(listenfd, LISTENQ);

3 for( ; ; ) {
connfd = Accept(listenfd, (SA *) NULL, NULL) ;

ticks = time(NULL);
snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks) );
4 Write(connfd, buff, strlen(buff) );

Close(connfd);
}
}

UNIX Network Programming 9


1.7 OSI Model
application
7 Application details
user
6 Presentation process
Application
5 Session Sockets
4 Transport TCP | | UDP XTI
3 Network IPv4, IPv6 kernel
communication
2 Datalink Device driver details
1 Physical and Hardware
OSI Model Internet protocol
suite
Figure 1.14 Layers on OSI model and Internet protocol suite

– Why do both sockets and XTI provide the interface from the upper three layers of the
OSI model into the transport layer?
• First, the upper three layers handle all the details of the application and The lower four
layers handle all the communication details.
• Second, the upper three layers is called a user process while the lower four layers are
provided as part of the operating system kernel.

UNIX Network Programming 10


1.10 Unix Standards
• POSIX
– Potable Operating System Interface
– a family of standards being developed by IEEE
• The Open Group
– an international consortium of vendors and end-user
customers from industry, government, and academia.
• IETF (Internet Engineering Task Force)
– a large open international community of network designers,
operators, vendors, and researchers concerned with the
evolution of the Internet architecture and the smooth
operation of the internet.

UNIX Network Programming 11


Chapter 2. The Transport Layer :
TCP and UDP
• Contents
– Introduction
– The Big Picture
– UDP: User Datagram Protocol
– TCP: Transmission Control Protocol
– TCP Connection Establishment and Termination
– TIME_WAIT State
– Port Numbers
– TCP Port Numbers and Concurrent Servers
– Buffer Sizes and Limitations

UNIX Network Programming 12


2.1 Introduction
• Overview of the TCP / IP protocol
• Transport layer : TCP & UDP

UNIX Network Programming 13


2.2 The Big Picture
I P v 4 a p p lic a t io n I P v 6 a p p lic a t io n
A F _ I N E T A F _ I N E T 6
s o c k a d d r _ in { } s o c k a d d r _ in 6 { }

t c p - m - t r a c e - t r a c e -
p in g a p p l. a p p l. a p p l. a p p l. p in g
d u m p r o u t e d r o u t e r o u t e

A P I

T C P U D P

I C M P

3 2 - b it 1 2 8 - b it I C M P v
I G M P I P v 4 I P v 6
a d d r e s s e s a d d r e s s e s 6

A R P ,
R A R P

B P F , d a t a -
D L P I lin k

F ig u r e 2O . v1 e r v i e w o f T C P / I P p r o t o c o ls

UNIX Network Programming 14


2.3 UDP : User Datagram Protocol

• The application writes a datagram to a UDP socket, which is


encapsulated as either a IPv4 of a IPv6 datagram, which is sent
to its destination.

• UDP provides a connectionless service.


• Each UDP datagram has a length and we can consider a
datagram as a record.

• RFC 768[Postel 1980]

UNIX Network Programming 15


2.4 TCP: Transmission Control Protocol

• Provides connections between clients and servers.


• Provides reliability.
– RTT ( round-trip-time)
• TCP also sequences the data by associating a sequence number
with every byte that it sends.

• TCP provides flow control.


– window

• A TCP connection is also full-duplex.

UNIX Network Programming 16


2.5 TCP Connection Establishment and Termination

c lie n t s e r v e r
s o c k e t , b i n d , l i s t e n
s o c k e t a c c p ( eb t l o c k s )
c o n n e( bc tl o c k s )
SYN J
( a c t io n o p e n )

1
ac k J +
S Y N K,
c o n n e rc e t t u r n s

ac k K+1
a c c e pr et t u r n s
r e a( db l o c k s )

F ig u r e T 2 C . 2P t h r e e - w a y h a n d s h a k e

UNIX Network Programming 17


TCP Header

0 1 5 1 6 3 2
1 6 - b it s o u r c e p o r t n u m b e 1r 6 - b it d e s t in a t io n p o r t n u m b

3 2 - b it s e q u e n c e n u m b e r

3 2 - b it a c k n o w le d g m e n t n u m b e r 2 0 b y t e
4 - b i t h e a d r e rs e r v e dU A P R S F
R C S S Y I 1 6 - b it w in d o w s iz e
le n g t h ( 6 b it ) G K H T N N

1 6 - b it T C P c h e c k s u m 1 6 - b it u r g e n t p o in t e r

o p t io n ( if a n y )

d a t a ( if a n y )

T C P H e a d e r

UNIX Network Programming 18


Encapsulation
u s e r d a t a

a p p lic a t io n

A p p l
u s e r d a t a
h e a d e r

T C P

T C P
a p p lic a t io n d a t a
h e a d e r

T C P s e g m e n t I P

I P T C P
a p p lic a t io n d a t a
h e a d e r h e a d e r

I P d a t a g r a m E t h e r n e t
d r iv e r

E t h e r n e t I P T C P E t h e r n e t
a p p lic a t io n d a t a
h e a d e r h e a d e r h e a d e r t r a i le r
E t h e r n e t
1 4 2 0 2 0 4
E t h e r n e t f r a m e
4 6 t o 1 5 0 0 b y t e s

E n c a p s u la t io n o f d a t a a s it g o s e d o w n t h e p r

UNIX Network Programming 19


2.5 TCP Connection Establishment and Termination (cont)

• TCP Options
– MSS option
• With this option the TCP sending the SYN announces its maximum segment
size, the maximum amount of data that it is willing to accept in each TCP
segment, on this connection.

– Window Scale option

– Timestamp option

UNIX Network Programming 20


2.5 TCP Connection Establishment and Termination (cont)
• TCP Connection Termination

c lie n t s e r v e r

c l o s e
( a c t iv e c lo s e ) F IN M

( p a s s iv e c lo s e )
r e a rd e t u r0 n s
1
ac k M +
c l o s e
c o n n e rc e t t u r n s
F IN N

ack N +
1

F ig u r e P2 a. 3c k e t s e x c h a n g e d w h e n a T C P c o n n e c t io

• half-close : Between steps 2 and 3 it is possible for data to flow from the end doing the passive close to
the end doing active close.

UNIX Network Programming 21


s t a r t in g p o in t

2.5 TCP C L O S E D

Connection a p p l: p a s s iv e o p e n

ap end
s e n d : < n o t h in g >

pl
:a :S
s
Establishment

ct
ive N
CK L I S T E N
N ,A

op
SY
and

Y
:

en
nd p a s s iv e o p e n
se T
N; RS
SY v:
v: c
Termination c re
re
r e c v : S Y N a p p l: c lo s e
S Y N _ R C V D S Y N _ S E N T
s e n d : S Y N , K K A C o r t im e o u t
(cont) se re
nd cv:
s im u l t a n e o u s o p e n AC
,
a c t iv e o p e n
:< AC S YN C K
no K : :A
cv
th
in re end
g> s
r e c v : F IN
E S T A B L I S H E D C L O S E _ W A I T
s e n d : A C K
d a t a t r a n s f e r s t a t e
se r e c v : c lo s e
: clo N
pl FI s e n d : F I N
- State ap
se
n d:
s im u lt a n e o u s c lo s e

transition r e c v : F I N r e c v : A C K
F I N _ W A I T _ 1 C L O S I N G L A S T _ A C K
s e n d : A C K s e n d : < n o t h in

diagram re
cv
: p a s s iv e c lo s e
r e c v : A C sK en FIN r e c v : A C K
,
s e n d : < n o tdh: Ai n AgC > s e n d : < n o t h in g >
CK K

r e c v : F I N
F I N _ W A I T _ 2 T I M E _ W A IT
s e n d : A C K
2 M S L t im e o u t

a c t iv e c lo s e

F ig u r e 2T . C 4 P s t a t e t r a n s it io n d ia g r a m
UNIX Network Programming 22
2.5 TCP Connection Establishment and Termination (cont)
c lie n t s e r v e r
• Watching the Packets s o c k e t
c o n n e( cb t l o c k s )
SY N J , m
s o c k e t , b i n d , l i s t e n
L I S T E N ( p a s s i v e
a c c p (e b t l o c k s )
o p e n )

( a c t io n o p e n ) ss = 146
0
S Y N _ S E N T

1024
, mss =
N K , ack J + 1
SY
E S T A B L I S H E D
c o n n e cr et t u r n s
ac k K+1
< c l i e n t f o r m s r e q u e s t > E S T A B L I S H E D
a c c e pr te t u r n s
w r i t e r e a ( db l o c k s )
r e a ( db l o c k s ) data(req
uest)

r e a dr e t u r n s
< s e r v e r p r o c e s s e s r e q u e s t >

w r i t e
y)
data (repl r e a ( db l o c k s )
k of request
ac
r e a dr e t u r n s

ack of re
ply

c l o s e
( a c t iv e c lo s e )
F IN M
F I N _ W A I T _ 1
C L O S E _ W A I T
( p a s s iv e c lo s e )
1 r e a dr e t u r 0 n s
ac k M +
c l o s e
F I N _ W A I T _ 2
F IN N L A S T _ A C K

T I M E _ W A I T
ac k N +
1

C L O S E D

F ig u r e P2 a. 5c k e t e x c h a n g e f o r T C P c o n n e c t io n

UNIX Network Programming 23


2.6 TIME_WAIT State
• The end that performs the active close is the end that remains in
the TIME_WAIT state=>because that end is the one that might
have to retransmit the final ACK.

• There are two reason for TIME_WAIT state


– to implement TCP’s full-duplex connection termination
reliably
– to allow old duplicate segments to expire in the network

UNIX Network Programming 24


2.7 Port Numbers

UNIX Network Programming 25


2.8 TCP port Numbers
and Concurrent Servers
2 0 6 . 6 2 . 2 2 6 . 3 5
2 0 6 . 6 2 . 2 2 6 . 6 6 1 9 8 . 6 9 . 1 0 . 2

c o n n e c t io n r e q u e s t t o
s e r v e r c lie n t
2 0 6 . 6 2 . 2 2 6 . 3 5 , p o r t 2 1
lis t e n in g s o c k (e* t . 2 1 , * . * ) { 1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 0 ,
2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 }

F ig u r e 2 C . o8 n n e c t i o n r e q u e s t f r o m c lie n t t o s e r

2 0 6 . 6 2 . 2 2 6 . 3 5
2 0 6 . 6 2 . 2 2 6 . 6 6 1 9 8 . 6 9 . 1 0 . 2

s e r v e r c lie n t
lis t e n in g s o c k (e* t . 2 1 , * . * ) { 1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 0 ,
n 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 }
tio
nec
f o r k n
co

s e r v e r
( c h ild )
c o n n e c t e d
s o c k e t { 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 ,
1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 0 }

F ig u r e 2C . o9 n c u r r e n t s e r v e r h a s c h ild h a n d le c lie
UNIX Network Programming 26
2.8 TCP port Numbers
and Concurrent Servers
2 0 6 . 6 2 . 2 2 6 . 3 5
2 0 6 . 6 2 . 2 2 6 . 6 6 1 9 8 . 6 9 . 1 0 . 2

s e r v e r c lie n t 1
lis t e n in g s o c k (e* t . 2 1 , * . * ) { 1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 0 ,
n
ectio 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 }
f o r k nn
co

s e r v e r
c lie n t 2
( c h il d 1 )
c o n n e c t e d
s o c k e t { 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 , { 1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 0 ,
n
1 9 8 . 6 9 . 1 0 . 2 . 1 5 0nec0tio } 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 }
on
c

s e r v e r
( c h il d 2 )
c o n n e c t e d
s o c k e t { 2 0 6 . 6 2 . 2 2 6 . 3 5 . 2 1 ,
1 9 8 . 6 9 . 1 0 . 2 . 1 5 0 1 }

F ig u r e 2 S. 1e 0c o n d c lie n t c o n n e c t io n w it h s a m e

UNIX Network Programming 27


2.9 Buffer Sizes and Limitations
• Maximum size of IPv4 => 65535 byte
• Maximum size of IPv6 => 65575 byte
• MTU(maximum transmit unit) => fragmentation
• DF (don’t fragment)
• IPv4 and IPv6 define a minimum reassembly buffer size.
• TCP has MSS(maximum segment size)

UNIX Network Programming 28


TCP output

UNIX Network Programming 29


UDP output

UNIX Network Programming 30


2.10 standard internet service

Notice: TCP and UDP port number is same.

UNIX Network Programming 31


2.11protocol usage by common internet Application

UNIX Network Programming 32