Documente Academic
Documente Profesional
Documente Cultură
N e
t w o r k I P L a y e r
( I P v 4 , I P v 6 , I P ( XI P ) v 4 , I P v 6 )
D
t a li n a k
( E t h e r n e t , T o k e n
D e v ic e D r i v e r a n d
R in g , A T M , P P P )
H a r d w a r e
P h y s ic a l
( t w is t e d p a ir , N I C )
( V . 2 4 , 8 0 2 . 3 ,
E t h e r n e t R J 4 5 )
O S I M o d e l
I n t e r n e t P r o t o c o l S u it e
( T a n n e n b a u m , 1 9 8 8 )
Protocol Communication
f t p C l ie n t f t p d S e r v e r
T C P T r a n s p o r t T C P T r a n s p o r t
I P L a y e r , I P v 4 I P L a y e r , I P v 4
E t e r n e t C o n t r o ll e r , E t e r n e t C o n t r o l le r ,
3 C o m E t h e r l in k 3 3 C o m E t h e r lin k 3
D r iv e r D r i v e r
H U B H U B
R o u t e r
Common Protocols
Application ICMP UDP TCP
Ping
Traceroute
DHCP
NTP
SNMP
SMTP
Telnet
FTP
HTTP
NNTP
DNS
NFS
Sun RPC
T r a n s p o r t
T C P T r a n s p o r t H
A p p l ic a t io n
e a d e r
d a t a
I P T r a n s p o r t
I P L a y e r , I P v 4 H
A p p lic a t io n
e a d He re a d e r
d a t a
E t e r n e t C o n t r o l le r ,
E t h e r n I Pe t T r a n s p o r t
3 C o m E t h e r l i n k H 3 e a d H e e r a d H e er a d Ae rp p l i c a t i o n d a t a
D r i v e r
The Hardware (Ethernet) Layer
• Responsible for transfering frames (units of data)
between machines on the same physical network
P a c k e t t y p e
( m a g ic n u m b e r
f o r p r o t o c o l:
D e s t i n a t Si o o n u r c e D a t a g r a mC y c l i c
P r e a m b le 0 x 8 0 0 = I P ,
A d d r e s sA d d r e s s ( T H E D RA eT d A u ) n d a n
( b it s e q u e n c e ) 0 x 6 0 0 3 =
( 1 9 2 . 3 2 ( . 1 6 9 5 2 . 1. 3 ) 2 . 6 3 . 5 ) ( u p t o 1 2 k C bh ie t sc ) k
D e c n e t ,
0 x 8 0 9 B =
A p p le t a lk )
6 4 b it s 4 8 b it s 4 8 b it s 1 6 b it s v a r ia b l e3 2 b it s
The IP Layer
• The IP layer allows packets to be sent over gateways to
machines not on the physical network
• Addresses used are IP addresses, 32-bit numbers divided
into a network address (used for routing) and a host
address
• The IP protocol is connectionless, implying:
– gateways route discrete packets independently and
irrespective of other packets
– packets from one host to another may be routed
differently (and may arrive at different times)
– non-guaranteed delivery
IP Datagram Format
• Packets may be broken up, or fragmented, if original data
is too large for a single packet (Maximum Transmission
Unit is currently 12k bits, or 1500 Bytes)
• Packets have a Time To Live, number of seconds/rounds
it can bounce around aimlessly among routers until it’s
killed
F r a g m e n t a t io n
I n f o r m a t i o n P ( ri fo t o c o l S o u r c eD e s t i n a t i o n D a t a g r a m
L e n g t h T im e
P r e a m b le it 's t o o b i g f o (r T a C n PC , h e c k s Au md d r e s A s d d r e s sO p t i o n ( s T H E D A T A
o f d a t a T o L iv e
e t h e r n e t f r a mU eD P ) ( 1 9 2 . 3 2 ( . 1 6 9 3 2 . 5. 3 ) 2 . 6 5 . 1 ) ( u p t o 1 2 k b
b u f f e r )
2 b y t 2e s b y t e 4s b y t e s1 b y t1 e b y t 2e b y t e s4 b y t e s 4 b y t e s v a r ia b le v a r ia b le
The Transport Layer
• Unix has two common transports
– User Datagram Protocol
• record protocol
• connectionless, broadcast
• Metaphor: Postal Service
– Transmission Control Protocol
• byte stream protocol
• direct connection-oriented
• Metaphor: Phone Service circa 1945
– “Sarah, this is Andy, get me Barney please.”
The Transport Layer:
UDP Protocol
• Connectionless, in that no long term connection
exists between the client and server. A
connection exists only long enough to deliver a
single packet and then the connection is severed.
• No guaranteed delivery (“best effort”)
• Fixed size boundaries, sent as a single “fire and
forget message”. Think announcement.
• No built-in acknowledgement of receipt
The Transport Layer:
UDP Protocol
• No built-in order of delivery, random delivery
• Unreliable, since there is no acknowledgement of
receipt, there is no way to know to resend a lost
packet
• Does provide checksum to guarantee integrity of
packet data
• Fast and Efficient
The Transport Layer:
TCP Protocol
• TCP guarantees delivery of packets in order of
transmission by offering acknowledgement and
retransmission: it will automatically resend after
a certain time if it does not receive an ACK
• TCP promises sequenced delivery to the
application layer, by adding a sequence number
to every packet. Packets are reordered by the
receiving TCP layer before handing off to the
application layer. This also aides in handling
“duplicate” packets.
The Transport Layer:
TCP Protocol
• Pure stream-oriented connection, it does not care
about message boundaries
• A TCP connection is full duplex (bidirectional),
so the same socket can be read and written to (cf.
half duplex pipes)
• Provides a checksum that guarantees packet
integrity
TCP’s Positive Acknowledgement
with Retransmission
• TCP offers acknowledgement and retransmission: it will
automatically resend after a certain time if it does not receive an ACK
• TCP offers flow control, which uses a “sliding window” (in the TCP
header) will allow a limited number of non-ACKs on the net during a
given interval of time. This increases the overall bandwidth
efficiency. This window is dynamically manged by the recipient TCP
layer.
t im 1 e t t im 2 e t t i m 3 e t t im 4 e t t i m 5 e t t im 6 e t t i m 7 e t
A C K n e v e r
A K C
r e c e i v e d f o r r e c e i v e 2d , T i m e r
P a c k e t 1 i s s e n t P a c k e t 1 t i m e s o u t A C K r e c e i v e
T i m 1 se t r a r t e d P a c k e t 2 s e n t a n d P a c k e t 2 R E S E N T P a c k e t 2
T i m 2 se t r a r t e d a s P a c k e t 3 a n d
T i m3 es tr a r t e d
P a c k e t 1 is V o
r t e x P a c k e t 3 i s
r e c e i v e d , A C K r e c e i v e d , A C K
s e n t O f s e n t
D e a t h
How to Reuse Addresses
• Local ports are locked from rebinding for a period of time (usually a
couple of minutes based on the TIME_WAIT state) after a process
closes them. This is to ensure that a temporarily “lost” packet does
not reappear, and then be delivered to a reincarnation of a listening
server. But when coding and debugging a client server app, this is
bothersome. The following code will turn this feature off:
int yes = 1;
server = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(server, SOL_SOCKET,
SO_REUSEADDR, &yes, sizeof(int)) < 0)
{
perror(“setsockopt SO_REUSEADDR");
exit(1);
}
TCP Datagram Format
• Source and Destination addresses
• Sequence Number tells what byte offset within
the overall data stream this segment applies
• Acknowledgement number lets the recipient set
what packet in the sequence was received ok.
D a t a g r a m
S o u r Dc ee s t i n aS t e i o q n u e A n c c k e n o w l e d g We m i n e d n o t w U r g e n t
F la g s C h e c k s u m O p t i o ( nT s H E D A T
P o r t P o r t N u m b e rN u m b e r S iz e P o in t e r
( u p t o 1 2 k
2 b y t e2s b y t e 4s b y t e s 4 b y t e 2s b y 2t e bs y t e 2 s b y t e 2 s b y tv e a s r i a b lv e a r i a b le
Socket Domain Families
• There are several significant socket domain families:
– Internet Domain Sockets (AF_INET)
• implemented via IP addresses and port numbers
– Unix Domain Sockets (AF_UNIX)
• implemented via filenames (think “named pipe”)
– Novell IPX (AF_IPX)
– AppleTalk DDS (AF_APPLETALK)
– Example:
~mark/pub/51081/sockets/linux/socketpairs.c
Creating a Socket
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
C r e a t e s o c k e t
b i n d a p o r t
s o c k e t
t o t h e • Connection-
C L I E N T oriented socket
l i s t e n f o r in c o m
c o n n e c t io n s
in g
C r e a t e s o c k e t connections
• Client-Server
a c c e p t a n
i n c o m i n g
c o n n e c t t o view
s e r v e r 's
p o r t
c o n n e c t i o n
r e a d f r o m t h e w r it e t o t h e
c o n n e c t i o n c o n n e c t i o n
lo o p l o o p
w r it e t o t h e r e a d f r o m t h e
c o n n e c t i o n c o n n e c t i o n
c l o s e c o n n e c t io n
Server Side Socket Details
S E R V E R
C r e a t e
in t s o c k e t ( i n t d o m
s o c k e t
a in , in t t y p e , in t p r o
s o c k s f od c = k e t ( P F _ I N E T , S O C K _ S T R E A
b in d a p o r t it no tt h eb i n d ( in t s o c k f d , s t r u c t s o c k a d d r * s
s o c k e t
b in d ( s o c k f d , & s e r v e r , s i z e o f ( s e r v e r ) )
li s t e n f o r i n c i no m t i nl i g st e n ( i n t s o c k f d , i n t n u m _ q u e u e d
c o n n e c t i o n s
lis t e n ( s o c k f d , 5 ) ;
a c c e p t a n
i n c o m in g
i n t a c c e p t ( in t s o c k f d , s t r u c t s o c k a d d r
c o n n e c t i o nn e w f d = a c c e p t ( s o c k f d , & c li e n t , s i z e o
r e a d f r o m ti hn e t r e a d ( i n t s o c k f d , v o i d * b u f f e r , s iz e
c o n n e c t i o n
r e a d ( n e w f d , b u f f e r , s i z e o f ( b u f f e r ) ) ;
w r i t e t o t h ei n t w
r it e ( in t s o c k f d , v o id * b u f f e r , s i z e
c o n n e c t i o n
w r i t e ( n e w f d , b u f f e r , s iz e o f ( b u f f e r ) ) ;
Client Side Socket Details
C L I E N T
C r e a t e
i n t s o c k e t ( i n t d o m
s o c k e t
a i n , i n t t y p e , i n t p r
s o c k s f od c = k e t ( P F _ I N E T , S O C K _ S T R E
c o n n e c t t o Sin e rt v ec r o n n e c t ( i n t s o c k f d , s t r u c t s o c k a d
s o c k e t
c o n n e c t ( s o c k f d , & s e r v e r , s iz e o f ( s e r
w r it e t o t h ei n t wr i t e ( i n t s o c k f d , v o i d * b u f f e r , s iz
c o n n e c t io n
w r it e ( s o c k f d , b u f f e r , s i z e o f ( b u f f e r ) ) ;
r e a d f r o m ti hn e t r e a d ( i n t s o c k f d , v o id * b u f f e r , s iz
c o n n e c t io n
r e a d ( s o c k f d , b u f f e r , s i z e o f ( b u f f e r ) ) ;
Setup for an Internet Domain
Socket
struct sockaddr_in {
sa_family_t sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
unsigned char pad[...];
};