Sunteți pe pagina 1din 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 user
details
6 Presentation Application process

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
IP v 4 a p p lic a tio n IP v 6 a p p lic a tio n
A F _ IN E T A F _ IN E T 6
s o c k a d d r_ in { } s o c k a d d r_ in 6 { }

tc p - m - tra c e - tra c e -
p in g a p p l. a p p l. a p p l. a p p l. p in g
dum p ro u te d ro u te ro u te

A P I

TC P U D P

IC M P

3 2 - b it 1 2 8 - b it IC M P v
IG M P IP v 4 IP v 6
a d d re s s e s a d d re s s e s 6

AR P,
R AR P

B PF, d a ta -
D LPI lin k

F ig u re 2 . 1 O v e rv ie w o f T C P / IP p ro to 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
• Three-Way Handshake
c lie n t s e rv e r
socket,bind,listen
socket a c c p e t ( b lo c k s )
c o n n e c t ( b lo c k s )
( a c tio n o p e n )

c o n n e c t re tu rn s

a c c e p t re tu rn s
r e a d ( b lo c k s )

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

• SYN segment
• ACK

UNIX Network Programming 17


TCP Header

0 15 16 32
1 6 - b it s o u rc e p o rt n u m b e r 1 6 - b it d e s tin a tio n p o rt n u m b e r

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 te s
U A P R S F
4 - b it h e a d e r re s e rv e d
R C S S Y I 1 6 - b it w in d o w s iz e
le n g th ( 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 rg e n t p o in te r

o p tio n ( if a n y )

d a ta ( if a n y )

TC P H eader

UNIX Network Programming 18


Encapsulation
u s e r d a ta

a p p lic a t io n

Appl
u s e r d a ta
header

TC P

TC P
a p p lic a tio n d a ta
header

TC P segm ent IP

IP TC P
a p p lic a tio n d a ta
header header

IP d a ta g ra m E th e rn e t
d riv e r

E th e rn e t IP TC P E th e rn e t
a p p lic a tio n d a ta
header header header tra ile r
E th e rn e t
14 20 20 4
E th e rn e t f ra m e
4 6 to 1 5 0 0 b y te s

E n c a p s u la t io n o f d a ta a s it g o s e d o w n th e p ro to c o l s ta c k s

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 rv e r

close
( a c t iv e c lo s e ) FIN M

( p a s s iv e c lo s e )
r e a d re tu rn s 0
1
a ck M +
close
c o n n e c t re tu rn s
FIN N

ack N +
1

F ig u re 2 . 3 P a c 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 n is c lo s e d .

• 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 ta rtin g p o in t

C LO SED

2.5 TCP Co a p p l: p a s s iv e o p e n

nnection Estab
s e n d : < n o th in g >

lishment and T L IS T E N
p a s s iv e o p e n

ermination (con
re c v : S Y N
t) S Y N _R C V D
send: SYN , AC K
S YN _S E N T
a p p l: c lo s e
o r tim e o u t
s im u lta n e o u s o p e n a c tiv e o p e n

re c v : F IN
E S T A B L IS H E D C L O S E _ W A IT
send: AC K
d a ta tra n s f e r s ta te
re c v : c lo s e
send: F IN
- State s im u lta n e o u s c lo s e
re c v : A C K
transition
re c v : F IN
F IN _ W A IT _ 1 C L O S IN G LA S T_A C K
send: AC K s e n d : < n o th in g >

diagram re c v : A C K re c v : A C K
p a s s iv e c lo s e

s e n d : < n o th in g > s e n d : < n o th in g >

re c v : F IN
F IN _ W A IT _ 2 T IM E _ W A IT
send: AC K
2 M S L tim e o u t

a c tiv e c lo s e

F ig u re 2 . 4 T C P s t a t e t r a n s it io n d ia g ra m
UNIX Network Programming 22
2.5 TCP Connection Establishment and Termination (cont)
c lie n t s e rv e r
• Watching the Packets socket
c o n n e c t ( b lo c k s )
socket,bind,listen
LISTEN(passive open)
a c c p e t ( b lo c k s )
( a c tio n o p e n )
S YN _S E N T

ESTABLISHED
c o n n e c t re tu rn s

<client forms request> ESTABLISHED


a c c e p t re tu rn s
write r e a d ( b lo c k s )
r e a d ( b lo c k s )

r e a d re tu rn s
<server processes request>

write
r e a d ( b lo c k s )

r e a d re tu rn s

close
( a c tiv e c lo s e )
F IN _ W A IT _ 1
C L O S E _ W A IT
( p a s s iv e c lo s e )
r e a d re tu rn s 0

close
F IN _ W A IT _ 2 LAST_AC K

T IM E _ W A IT

C LO SED

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

UNIX Network Programming 23


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

• There are two reason for TIME_WAIT state


– to implement TCP’s full-duplex connection termination reliab
ly
– 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 tio n re q u e s t to
s e rv e r c lie n t
2 0 6 . 6 2 . 2 2 6 . 3 5 , p o rt 2 1
lis te 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 re 2 . 8 C o n n e c t io n re q u e s t f ro m c lie n t to s e rv 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 rv e r c lie n t
lis te n in g s o c k e t (* .2 1 , * .* ) {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
on 2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
e cti
fork nn
co

s e rv e r
( c h ild )
c o n n e c te d
socket {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 re 2 . 9 C o n c u rre n t s e r v e r h a s c h ild h a n d le c lie n t


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 rv e r c lie n t 1
lis te 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 }
n n
fork co

s e rv e r
c lie n t 2
( c h ild 1 )
c o n n e c te d
socket {2 0 6 .6 2 .2 2 6 .3 5 .2 1 , {1 9 8 .6 9 .1 0 .2 .1 5 0 0 ,
ion
1 9 8 .6 9 .1 0 .2 .1 5 0 0 } ect 2 0 6 .6 2 .2 2 6 .3 5 .2 1 }
nn
co

s e rv e r
( c h ild 2 )
c o n n e c te d
socket {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 re 2 . 1 0 S e c o n d c lie n t c o n n e c t io n w it h s a m e s e r v e r

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

S-ar putea să vă placă și