Lecture 7 Berkeley Socket Programming
Metaphors • Postal Service – Address • Name, Street, City, State, Zip Code
– – – – – – –
Return Address Network of Post Offices Local Post Offices Lost Mail Indeterminacy of Order No confirmation of delivery for regular mail No failed delivery notification
Metaphors (continued) • Toll call from one hotel room to another (circa 1945), (or, London today) – call down to local hotel operator – tell her the area code and number to call of the remote hotel – hotel operator calls long distance operator, who is listening for incoming calls – long distance operator calls remote hotel – remote hotel operator picks up, as she has been listening for calls, and routes the call to your friend’s room – You and your friend are now talking directly
You Already Use Sockets • echo (7), telnet (25), ftp (21), ssh (22) – telnet calcna.ab.ca echo • cat /etc/services | grep [telnet | ssh | ftp | echo | etc.] • daytime (13) (telnet time.mit.edu 13) • email (SMTP) (port 25): (telnet direct to SMTP server) – telnet laime.cs.uchicago.edu 25 – MAIL FROM:
[email protected] – RCPT TO:
[email protected] – DATA – [write something here, and end with a period on a line] – . – QUIT
The Fundamentals • The Computer Systems Research Group (CSRG) at the University of California Berkeley gave birth to the Berkeley Socket API (along with its use of the TCP/IP protocol) with the 4.2BSD release in 1983. – A Socket is comprised of: • a 32-bit node address (IP address or FQDN) • a 16-bit port number (like 7, 21, 13242) – Example: 192.168.31.52:1051 • The 192.168.31.52 host address is in “IPv4 dottedquad” format, and is a decmial representation of the hex network address 0xc0a81f34
Port Assignments (less /etc/services) • Ports 0 through 1023 are reserved, priveledged ports, defined by TCP and UDP well known port assignments • Ports 1024 through 49151 are ports registered by the IANA (Internet Assigned Numbers Authority), and represent second tier common ports (socks (1080), WINS (1512), kermit (1649), https (443)) • Ports 49152 through 65535 are ephemeral ports, available for temporary client usage
Protocol Stacks A
p
( T
e
p li c a t io n l n e t , f t p , e
P
r e s e n t ( M I D I , H E B C D I S e s s io ( R P C , N A p p le t a lk T r a n ( T C P N ( I P D
t i o T M C ) n e t b i , D E
s p o r t , U D P
e
t w o r k v 4 , I P v 6
t a li n ( E t h e r n e R in g , A T P h y s ic ( V . 2 4 , 8 E t h e r n e ( T
a
a
O S I M a n n e n
, ( T
o C )
A
p e
s , n
p li c a t i o n ln e t , f t p , e
e
t c . )
t )
T r a n ( T C P
s p o r t , U D P
I P L a y e r I P ( XI P ) v 4 , I P v 6
)
)
k t , T o D M , P a l ( 0 2 . 3 t R J 4
o b
,
n L
t c . )
d a
e u
k e n e v ic e D r i v e r a n d P P ) H a r d w a r e t w is t e d p a ir , N I C ) , 5 )
l I n t e r n e t P m , 1 9 8 8 )
r o
t o
c o
l
S
u
it e
Protocol Communication f t p
T
C
P
I P
E 3
C
T
L
a
l ie
n
r a
n
y e
r ,
t
f t p
s p
o
I P
v 4
r t
T
U
P
I P
t e r n e t C o n t r o ll e r , C o m E t h e r l in k 3 D r iv e r
H
C
E 3
B
L
o
u
t e
r
S
e
T
r a
a
y e
r v e
n
r ,
r
s p
o
I P
v 4
r t
t e r n e t C o n t r o l le r , C o m E t h e r lin k 3 D r i v e r
H R
d
U
B
Common Protocols Application Ping Traceroute DHCP NTP SNMP SMTP Telnet FTP HTTP NNTP DNS NFS Sun RPC
ICMP
UDP
TCP
ICMP: Internet Control Message Protocol UDP: User Datagram Protocol TCP: Transmission Control Protocol
Data Encapsulation • Application puts data out through a socket • Each successive layer wraps the received data with its own header: f t p
T
C
I P
E 3
C
P
L
T
a
l i e
n
r a
n
y e
r ,
t
A
s p
o
I P
v 4
r t
T H
H
p p lic a t io n
d a t a
r a n s p o r t 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 A p p lic a t io n e a d He re a d e r
d a t a
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 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 r i v e r
d a t a
The Hardware (Ethernet) Layer • Responsible for transfering frames (units of data) between machines on the same physical network
D e r e a m b le A d ( b it s e q u e n ( 1 9 P
6
4
b it s
s t d c 2 .
4 8
P a c k e ( m a g ic f o r p r o i n a t Si o o n u r c e 0 x 8 0 0 r e s sA d d r e s s e ) 0 x 6 0 0 3 2 ( . 1 6 9 5 2 . 1. 3 ) 2 . 6 3 . 5 ) D e c n 0 x 8 0 9 A p p le b it s
4
8
b
it s
1 6
t
t y p n u m t o c o D a = I P ( T H 3 = ( u p e t , B = t a lk )
b it s
e b e r l: t a g r a mC y c l i c , E D RA eT d A u ) n d t o 1 2 k C bh ie t sc ) k
v a r ia b
l e3 2
b it s
a n
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
P
r e a
2
b
F r a g m I n f o r m L e n g t h m b le it 's t o o o f d a t a e t h e r n b u f f e
y t 2e s b y t e 4s
b y t e
e n a t T b i T e t r )
t a t io i o n P ( ri im e g f o (r T o L iv f r a mU
s1
b y t1 e
n 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 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 e 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 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 A
P T
a c k e t 1 i s i m 1 se t r a r t e d
e
t
t im
K r e c e i v e d P a c k e t 1 P a c k e t 2 s e n t T i m 2 se t r a r t e d
s e n t
P a c k e t 1 r e c e i v e d , s e n t
3
C
is A
C
K
V
4
e
t
f o r a n d
o
r t e x O f D e a t h
t i m A C r e c e t i m P a c k a s P T i m3
5
e
t
t im
6
K n e v e r i v e 2d , T i m e r e s o u t e t 2 R E S E N a c k e t 3 a n d es tr a r t e d
e
t
t i m
A
C
T
P a c k e t 3 r e c e i v e d , s e n t
P
C
e
t
K r e c e i v e a c k e t 2
i s A
7
K
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.
S
2
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 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
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
D a t a g r a m t i o ( nT s H E D A T ( u p t o 1 2 k
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);
• domain is one of the Address Families (AF_INET, AF_UNIX, etc.) • type defines the communication protocol semantics, usually defines either: – SOCK_STREAM: connection-oriented stream (TCP) – SOCK_DGRAM: connectionless, unreliable (UDP)
• protocol specifies a particular protocol, just set this to 0 to accept the default (PF_INET, PF_UNIX) based on the domain
S C
E
R
r e a t e
V
E
R
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
C
L
I E
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
a c c e p t a n i n c o m i n g c o n n e c t i o n
c o n n e c t p o r t
r e a d f r o m t h e c o n n e c t i o n
N
T
• Connectionoriented socket connections • Client-Server view
s o c k e t
t o
s e r v e r 's
w r it e t o t h e 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 c o n n e c t i o n c l o s 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 io n
Server Side Socket Details S C
E
R
r e a t e
V
E
R in t s o c k e t ( i n t d o m s o c k s f od c = k e t ( P F _ I N
s o c k e t
b in d a p o r t s o c k e t
it no tt h eb b in d
li s t e n f o r i n c i no m t c o n n e c t i o n s
n
t
t e n ( i n t s o c k f d n ( s o c k f d , 5 ) ;
e
r e a d f r o m ti hn e c o n n e c t i o n
w t
r e
w r i t e t o t h ei c o n n e c t i o n
w
n
a
t
c c e p t ( in t f d = a c c e
r e a d ( i n t a d ( n e w f d w
in t S O
t y p e , in C K _ S T
t
p R
r o E A
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 f d , & s e r v e r , s i z e o f ( s e r v e r ) )
i nl i g s
lis t e
a c c e p t a n i i n c o m in g c o n n e c t i o nn
a in , E T ,
r it e ( in t r i t e ( n e w f d
,
i n
t
n
u
m
_
q
u
e
u
e
d
s o c k f d , s t r u c t s o c k a d d p t ( s o c k f d , & c li e n t , s i z e
s o c k f d , v o i d , b u f f e r , s i z e
* b u f f e r , s iz e o f ( b u f f e r ) ) ;
s o c k f d , v o id , b u f f e r , s iz e
* b u f f e r , s i z e o f ( b u f f e r ) ) ;
r o
Client Side Socket Details C C
L
I E
r e a t e
N
T i n t s o c k e t ( i n t d o m s o c k s f od c = k e t ( P F _ I N
s o c k e t
c o n n e c t t o s o c k e t
a E
i n , T ,
i n t S O
t y p e , i n C K _ S T
t p r R E
Si
n 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 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 c o n n e c t io n
n
w
r e a d f r o m ti hn e c o n n e c t io n
r e
t
r i t e ( i n t s o c k f d , v o i d * b u f f e r , s iz 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 ) ) ; t
w
r e a d ( i n t s o c k f d , v o id * b u f f e r , s iz 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[...]; }; • sin_family is set to Address Family AF_INET • sin_port is set to the port number you want to bind to • sin_addr is set to the IP address of the machine you are binding to (struct in_addr is a wrapper struct for an unsigned long). INADDR_ANY supports all interfaces (since a given machine may have multiple interface cards) • ignore padding
Setup for A Unix Domain Socket struct sockaddr_un { sa_family_t sun_family; char sun_path[UNIX_PATH_MAX]; }; • sun_family is set to Address Family AF_UNIX • sun_path is set to a UNIX pathname in the filesystem
Reading From and Writing To Stream Sockets • Sockets, like everything else, are like files: – low level IO: • read() system call • write() system call – higher level IO: • int recv(int socket, char *buf, int len, int flags); – blocks on read – returns 0 when other connection has terminated • int send(int socket, char *buf, int len, int flags); – returns the number of bytes actually sent • where flags may be one of: – MSG_DONTROUTE (don’t route out of localnet) – MSG_OOB (out of band data (think interruption)) – MSG_PEEK (examine, but don’t remove from stream)
Closing a Socket Session • int close(int socket); – closes read/write IO, closes socket file descriptor • int shutdown( int socketfd, int how); – where how is: • 0: no more receives allowed • 1: no more sends are allowed • 2: disables both receives and sends (but doesn’t close the socket, use close() for that) • Example: hangserver.c (hangman game)
Host and Network Byte Ordering • Different computer architectures store numbers differently: – Little Endian architectures (like VAX, Intel) store the least significant byte first • This means that within a (2-byte) word, the least significant byte is stored first, that is, at the lowest byte address
– Big Endian architectures (like Sun Sparc, Motorola 68000) store the most significant byte appearing first • This means that within a (2-byte) word, the most significant byte is stored first, that is, at the lowest byte address
• examples: ~mark/pub/51081/byteorder/linux/endian.sh and ~mark/pub/51081/byteorder/solaris/endian.sh
Why This Matters • TCP/IP mandates that big-endian byte ordering be used for transmitting protocol information • This means that little-endian machines will need to convert ip addresses and port numbers into big-endian form in order to communicate successfully • Note that big-endian architectures don’t actually have to do anything, because they already meet the specification
What’s To Be Done About It? • Several functions are provided to allow you to easily convert between host and network byte ordering, and they are: – to translate 32-bit numbers (i.e. IP addresses): • unsigned long htonl(unsigned long hostlong); • unsigned long ntohl(unsigned long netlong); – to translate 16-bit numbers (i.e. Port numbers): • unsigned short htons(unsigned short hostshort); • unsigned short ntohs(unsigned short netshort);
UDP Clients and Servers • Connectionless clients and servers create a socket using SOCK_DGRAM instead of SOCK_STREAM • Connectionless servers do not call listen() or accept(), and usually do not call connect() • Since connectionless communications lack a sustained connection, several methods are available that allow you to specify a destination address with every call: – sendto(sock, buffer, buflen, flags, to_addr, tolen); – recvfrom(sock, buffer, buflen, flags, from_addr, fromlen);
• Examples: daytimeclient.c, mytalkserver.c, mytalkclient.c
Servicing Multiple Clients • Two main approaches: – forking with fork() – selecting with select() • fork() approach forks a new process to handle each incoming client connection, essentially to act as a “miniserver” dedicated to each new client: – must worry about zombies created when parent loops back to accept() a new client (ignore SIG_CHILD signal) – inefficient • A better approach would be to have a single process handle all incoming clients, without having to spawn separate child “server handlers”. Enter select().
select() int select(int numfiledescs, fd_set readfdsset, fd_set writefdsset, fd_set errorfdsset, struct timeval * timeout);
• The select() system call provides a way for a single server to wait until a set of network connections has data available for reading • The advantage over fork() here is that no multiple processes are spawned • The downside is that the single server must handle state management on its own for all its new clients
select() (continued) • select() will return if any of the descriptors in readfdsset and writefdsset of file descriptors are ready for reading or writing, respectively, or, if any of the descriptors in errorfdsset are in an error condition • The FD_SET(int fd, fd_set *set) function will add the file descriptor fd to the set set • The FD_ISSET(int fd, fd_set *set) function will tell you if filedesc fd is in the modified set set • select() returns the total number of descriptors in the modified sets • If a client closes a socket whose file descriptor is in one of your watched sets, select() will return, and your next recv() will return 0, indicating the socket has been closed
Setting the timeval in select() • If you set the timeout to 0, select() times out immediately • If you set the timeout to NULL, select() will never time out, and will block indefinitely until a filedes is modified • If you don’t care about a particular file descriptor set, just set it to NULL in the call: select (max, &readfds, NULL, NULL, NULL); – Here we only care about reading, and we want to block indefinitely until we do have a file descriptor ready to be read • examples: multiserver.c, multiclient.c
Miscellaneous Socket Functions • int getpeername(int sockfd, struct sockaddr * addr, int *addrlen); – this tells you the hostname of the REMOTE connection • int gethostname(char * hostname, size_t size); – this tells you the hostname of your LOCAL connection • int inet_aton(const char * string_address, &(addr.sin_addr)); – converts the const ip string_address (“192.168.3.1”) into an acceptable numeric form • addr.sin_addr = inet_addr(“192.168.3.1”); – does the same thing
More Miscellaneous Functions • struct hostent *gethostbyname(const char *hostname); – Does a DNS lookup and returns a pointer to a hostent structure that contains the host name, aliases, address type (AF_INET, etc.), length, and an array of IP addresses for this host (hostent.h_addr_list[0] is usually the one) (cf. /etc/nsswitch.conf) struct hostent { char *h_name; /*DNS host name*/ char **h_aliases; /*alias list*/ int h_addrtype; /* “AF_INET”, etc*/ int h_length; /* length of addr*/ char **h_addr_list; /*list of IP adds*/ };
And a Few More struct servent * getservbyname(const char *servicename, const char *protocol) struct servent * getservbyport(int port, const char *protocol) • example: serventptr = getservbyname(“daytime”, “udp”); struct servent { char * s_name; /*official service name*/ char **s_aliases; /* alias list */ int s_port; /*port num*/ char *s_proto; /* protocol: “tcp”, “udp”*/ };