Chapters 1&2

advertisement
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
Client
Communication link
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
process
Web
Client
Application protocol
Web
server
Application layer
TCP protocol
TCP
TCP
transport layer
Protocol stack
within kernel
IP
IP protocol
IP
network layer
Ethernet
driver
Ethernet protocol
Ethernet
driver
datalink layer
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
application
server
application
Host
with
TCP/IP
Host
with
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>”);
2
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys(“socket error”);
3
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]);
4
if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys(“connect error”);
5
while ( (n = read(sockfd, recvline, MAXLINE)) > 0 ) {
recvline[n] = 0;
/* null termicate */
if ( fputs(recvline, stdout) == EOF)
err_sys(“fputs error”);
}
if ( n < 0 ) err_sys ( “read error “);
exit(0);
UNIX Network Programming
}
6
1.3 protocol Independence
•
•
•
•
•
•
#include "unp.h"
int main(int argc, char **argv)
{
int
struct sockaddr_in6
char
sockfd, n;
servaddr;
recvline[MAXLINE + 1];
•
if (argc != 2)
•
•
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");
•
•
err_quit("usage: a.out <IPaddress>");
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) ;
4
ticks = time(NULL);
snprintf(buff, sizeof(buff), “%.24s\r\n”, ctime(&ticks) );
Write(connfd, buff, strlen(buff) );
Close(connfd);
}
}
UNIX Network Programming
9
1.7 OSI Model
7 Application
6 Presentation
5
Session
4
Transport
3
Network
2
Datalink
1
Physical
OSI Model
application
details
user
process
Application
Sockets
TCP | | UDP
IPv4, IPv6
Device driver
and Hardware
XTI
kernel
communication
details
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
IPv4 application
AF_INET
sockaddr_in{ }
tcpdump
mrouted
ping
traceroute
IPv6 application
AF_INET6
sockaddr_in6{ }
appl.
appl.
appl.
appl.
traceroute
ping
API
TCP
UDP
ICMP
IGMP
IPv4
32- bit
addresses
128- bit
addresses
IPv6
ICMPv
6
ARP,
RARP
BPF,
DLPI
datalink
Figure 2.1 Overview of TCP/IP protocols
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
client
socket
connect(blocks)
(action open)
server
socket,bind,listen
accpet(blocks)
S YN J
1
a ck J +
S YN K ,
connect returns
a ck K + 1
accept returns
read(blocks)
Figure 2.2 TCP three- way handshake
• SYN segment
• ACK
UNIX Network Programming
17
TCP Header
0
15
16
16- bit source port number
32
16- bit destination port number
32- bit sequence number
32- bit acknowledgment number
4- bit header
length
reserved
(6bit)
U
R
G
A
C
K
P
S
H
R
S
T
S
Y
N
F
I
N
16- bit TCP checksum
20 bytes
16- bit window size
16- bit urgent pointer
option (if any)
data (if any)
TCP Header
UNIX Network Programming
18
Encapsulation
user data
application
Appl
header
user data
TCP
TCP
header
application data
TCP segment
IP
header
TCP
header
IP
application data
Ethernet
driver
IP datagram
Ethernet
header
IP
header
TCP
header
14
20
20
application data
Ethernet
trailer
4
Ethernet
Ethernet frame
46 to 1500 bytes
Encapsulation of data as it gose down the protocol stacks
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
client
close
(active close)
server
FIN M
1
a ck M +
connect returns
(passive close)
read returns 0
close
FIN N
a ck N +
1
Figure 2.3 Packets exchanged when a TCP connection is closed.
• 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
starting point
2.5 TCP
appl: passive open
send: < nothing>
r
:
ecv
en
; s
N
SY
d:
N ,A
SY
S
v: R
rec
CK
LISTEN
passive open
en
op
ive N
ct
Y
:a :S
pl
a p en d
s
Connection
Establishment
and
Termination
CLOSED
T
recv: SYN
SYN_RCVD
(cont)
re
se
n d cv:
AC
:<
K
no
thi
ng
>
send: SYN, ACK
K
AC
,
YN
K
: S : AC
v
c
re
nd
se
recv: FIN
ESTABLISHED
send: ACK
simultaneous open
appl: close
or timeout
SYN_SENT
active open
CLOSE_WAIT
data transfer state
- State
transition
diagram
FIN_WAIT_1
se
clo
:
l
IN
p
ap
:F
d
n
se
recv : FIN
send: ACK
re
simultaneous close
CLOSING
cv
:F
se
I
recv : ACK
nd N, A
:A
C
send: < nothing>
CK K
FIN_WAIT_2
recv : close
send: FIN
recv : FIN
send: ACK
LAST_ACK
recv: ACK
send: < nothing>
passive close
recv : ACK
send: < nothing>
TIME_WAIT
2MSL timeout
active close
Figure 2.4 TCP state transition diagram
UNIX Network Programming
22
2.5 TCP Connection Establishment and Termination (cont)
• Watching the Packets
client
socket
connect(blocks)
(action open)
SYN_SENT
ESTABLISHED
connect returns
server
S YN J , m
s s = 146
0
102 4
, m ss =
ck J + 1
a
,
K
N
SY
a ck K + 1
<client forms request>
write
read(blocks)
socket,bind,listen
LISTEN(passive open)
accpet(blocks)
data(req
uest)
ESTABLISHED
accept returns
read(blocks)
read returns
<server processes request>
read returns
ly)
data (rep
est
qu
re
of
k
ac
write
read(blocks)
ack of re
ply
close
(active close)
FIN_WAIT_1
FIN M
1
a ck M +
FIN_WAIT_2
FIN N
CLOSE_WAIT
(passive close)
read returns 0
close
LAST_ACK
TIME_WAIT
a ck N +
1
CLOSED
Figure 2.5 Packet exchange for TCP connection
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
206.62.226.35
206.62.226.66
server
listening socket
client
206.62.226.35, port 21
{198.69.10.2.1500,
206.62.226.35.21}
Connection request from client to server
206.62.226.35
206.62.226.66
198.69.10.2
server
client
(*.21, *.*)
{198.69.10.2.1500,
206.62.226.35.21}
fork
connected
socket
connection request to
(*.21, *.*)
Figure 2.8
listening socket
198.69.10.2
e
nn
co
on
cti
server
(child)
{206.62.226.35.21,
198.69.10.2.1500}
Figure 2.9
Concurrent server has child handle client
UNIX Network Programming
26
2.8 TCP port Numbers
and Concurrent Servers
listening socket
206.62.226.35
206.62.226.66
198.69.10.2
server
client1
(*.21, *.*)
fork
connected
socket
connected
socket
server
(child1)
{206.62.226.35.21,
198.69.10.2.1500}
nn
co
ion
ect
{198.69.10.2.1500,
206.62.226.35.21}
client2
nn
co
ion
ect
{198.69.10.2.1500,
206.62.226.35.21}
server
(child2)
{206.62.226.35.21,
198.69.10.2.1501}
Figure 2.10
Second client connection with same server
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
Related documents
Download