Transcript document

The Application Layer
Application Services
(Telnet, FTP, e-mail, WWW)
Reliable Stream
Unreliable Transport
Transport (TCP)
Service (UDP)
Connectionless Packet Delivery Service
(IP)
The Client-Server Model
• Commonly used model for application
interaction over a TCP/IP internet
– Server: a program that offers a service that can
be reached over a network
• Examples: web server, file server
– Client: a program that requests a service from a
server
• Examples: ping, browser
The Client-Server Model (cont)
Example: UDP Echo Server
• Server process attaches to port 7 (echo port)
• While (TRUE) do
– Wait for a user datagram to arrive
– Swap the source and destination IP addresses
and port numbers
– Return the user datagram to the original sender
Example: UDP Echo Client
• Obtain an unused UDP port
• Create a user datagram
– Source and destination IP addresses
– Source and destination port numbers
• Send the user datagram to the echo server
• Await the server’s reply
Differences Between
Clients and Servers
Client
- Requests service
- Usually terminates after
using a server a finite
number of times
- Obtains an arbitrary,
unused port for
communication
Server
- Provides service
- Usually accepts requests
and provides responses
indefinitely
- Listens for requests on a
well-known, reserved port
Servers - Master and Slaves
• Can receive multiple concurrent requests
• Each request can take considerable time to
process
• Most complex servers have two parts:
– A single master program that accepts new
requests
– A set of slaves that are responsible for handling
individual requests
The Master-Slave Server
Algorithm
• Master:
– Open a well-known port for requests
– While (TRUE) do
• Waits for a client request
• Start an independent, concurrent slave to handle the request
• Slave:
– Process the request given by the master
– Send reply to the client
– Exit
Servers - Must be well Written!
• Servers are usually more complex and
harder to write than clients:
– Handle multiple, concurrent requests
– Enforce authorization and protection
– Must handle errors/malformed requests
• The Oracle of Bacon at Virginia
• The Internet worm
Alternatives to the
Client-Server Model
• Consider ARP:
– Follows the client-server model
– Demand driven
• Consider NNTP (Network News Transport
Protocol):
– Does not follow the client-server model
– Uses precollection
Alternatives to the
Client-Server Model
• Precollection
– Advantages
• Speed
• Mask network/hardware failures
– Disadvantages
• Uses processor time and network bandwidth
whether anyone uses the service or not
How Programmers use TCP/IP
• Example: UNIX
– Application programs interact with the
operating system by making system calls
– Standard I/O operations:
• Open
• Read/write
• Close
– Network I/O: has own system calls and library
routines
Using UNIX Sockets for
Network I/O
• Socket = abstraction of the port concept:
– Application programs request that the operating
system create a socket when one is needed
– O.S. returns a small integer (socket descriptor)
that the program uses to reference the socket
– Application program can then use read and
write system calls on the socket
– Program closes the socket when finished using
it
Creating a Socket
• The socket system call:
int sd;
int pf;
int type;
int protocol;
// socket descriptor
// protocol family (one of PF_INET,
// PF_PUP, PF_APPLETALK,
// PF_UNIX, etc.)
// type of service (one of SOCK_RAW,
// SOCK_DGRAM, SOCK_STREAM)
// specific protocol in pf
sd = socket(pf, type, protocol)
// create a new socket
Binding a Socket to a
Source Address
• Some programs (mainly servers) will want a
specific (reserved) address
• Some programs (mainly clients) don’t care
what address they use and will allow the
O.S. to choose one
• Uses the bind system call
Binding a Socket to a
Source Address (cont)
• The bind system call:
int sd;
struct sockaddr addr;
int len;
bind(sd,addr,len)
// socket descriptor
// structure specifying source
// address
// length (in bytes) of
// addr struct
// bind socket to source address
Binding a Socket to a
Source Address (cont)
• The sockaddr struct:
0
16
ADDRESS FAMILY
31
ADDRESS OCTETS 0-1
ADDRESS OCTETS 2-5
ADDRESS OCTETS 6-9
ADDRESS OCTETS 10-13
• Address family field: determines the format of the
remaining address octets
Binding a Socket to an
Internet Source Address
• The sockaddr_in struct for PF_INET:
0
16
ADDRESS FAMILY(2)
31
PROTOCOL PORT
IP ADDRESS
UNUSED (ZERO)
UNUSED (ZERO)
Binding a Socket to an
Internet Source Address (cont)
• The bind system call:
int sd;
// socket descriptor
struct sockaddr_in addr; // structure specifying source
// address
int len;
// length (in bytes) of
// addr struct
bind(sd,addr,len)
// bind socket to source IP
// address
Connecting a Socket to a
Destination Address
• The connect system call:
int sd;
struct sockaddr addr;
int len;
// socket descriptor
// structure specifying dest addr
// length (in bytes) of
// addr struct
connect(sd,addr,len)
// connect socket to
// dest address
• Can also use a sockaddr_in struct for dest address
Sending Data Through a Socket
• The write system call:
int sd;
void *buffer;
int len;
// socket descriptor
// address of the data to be sent
// number of bytes to send
write(sd,buffer,len)
// send data through socket
Receiving Data Through a Socket
• The read system call:
int sd;
void *buffer;
int len;
// socket descriptor
// address in memory at which
// to store the data
// number of bytes to receive
read(sd,buffer,len)
// receive data through socket
Closing a Socket
• The close system call:
int sd;
// socket descriptor
close(sd)
// close socket
Obtaining Local
Socket Addresses
• The getsockname system call:
int sd;
struct sockaddr *addr;
int *len;
// socket descriptor
// address structure to be filled
// pointer to integer that will
// contain the length of the
// address
getsockname(sd, addr, len); // obtain local socket address
Obtaining and Setting
Socket Options
• The getsockopt system call:
int sd;
int level;
int optionid;
void *optionval;
int *len;
// socket descriptor
// option for socket or protocol?
// which specific option?
// where to place the requested
// value
// length of the optionval
getsockopt(sd, level, optionid, optionval, len); // obtain
// socket opt
Obtaining and Setting
Socket Options (cont)
• Values for level (from <netinet/in.h>):
–
–
–
–
–
SOL_SOCKET
IPPROTO_IP
IPPROTO_ICMP
IPPROTO_TCP
IPPROTO_UDP
option for the socket
option for IP
option for ICMP
option for TCP
option for UDP
Obtaining and Setting
Socket Options (cont)
• Values for optionid (from <sys/socket.h>):
–
–
–
–
–
–
–
–
SO_TYPE
SO_SNDBUF
SO_RCVBUF
SO_DEBUG
SO_ACCEPTCONN
SO_BROADCAST
SO_REUSEADDR
SO_KEEPALIVE
socket type
send buffer size
receive buffer size
debugging info available?
socket listening enabled?
broadcast supported?
address reuse allowed?
keep alive after close?
Obtaining and Setting
Socket Options (cont)
• The setsockopt system call:
int sd;
int level;
int optionid;
void *optionval;
int *len;
// socket descriptor
// option for socket or protocol?
// which specific option?
// option value
// length of the option value
setsockopt(sd, level, optionid, optionval, len); // set option
// value
Socket Options for Servers
• The listen system call:
int sd;
int length;
// socket descriptor
// length of request queue
listen(sd, length)
// set socket request queue
// length
• Can only be used for SOCK_STREAM sockets
Servers: Accepting Connections
• The accept system call:
int sd;
struct sockaddr *name;
int *len;
// socket descriptor
// address of client
// length of address struct
newsock = accept(sd, addr, len) // accept connection
• A new socket is created that connects to the client
• Server handles request, sends reply, closes newsock
Servers That Provide
Multiple Services
• The select system call:
int ndesc;
void *indesc;
void *outdesc;
void *excdesc;
int *timeout;
// check descriptors 0...ndesc-1
// descriptors to check for input
// descriptors to check for output
// descriptors to check for exceptions
// how long to wait for a connection
nready = select(ndesc, indesc, outdesc, excdesc, timeout)
// determine which descriptors are
// ready for I/O
Servers That Provide
Multiple Services (cont)
• The select system call:
nready = select(ndesc, indesc, outdesc, excdesc, timeout)
• Returns the number of descriptors from the
specified set that are ready for I/O
• A process can use select to communicate over
more than one socket at a time
• Typically each socket provides a distinct service
Miscellaneous (Useful)
System Calls
• The gethostname system call:
char *name;
int length;
// buffer to store name
// size of buffer in bytes
gethostname(name, length)
// get name of host
• Defined in <netdb.h> include file
• Process can learn host it’s running on
Miscellaneous (Useful)
System Calls (cont)
• The network byte order conversion routines:
– Network-to-host (short), ntohs, convert a short
int from network byte order to host byte order
– Network-to-host (long), ntohl, convert a long
int from network byte order to host byte order
– Host-to-network (short), htons, convert a short
int from network byte order to host byte order
– Host-to-network (long), htonl, convert a long
int from network byte order to host byte order
Miscellaneous (Useful)
System Calls (cont)
• Testing htons:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
main(int argc, char **argv) {
if (argc != 2) exit(-1);
printf("%d\n",atoi(argv[1]));
printf("%d\n",htons(atoi(argv[1])));
}
Miscellaneous (Useful)
System Calls (cont)
• The gethostbyname system call:
struct hostent *h;
char *name;
// hostent structure
// host name
h = gethostbyname(name); // fill in hostent with
// info about name
Miscellaneous (Useful)
System Calls (cont)
• The hostent structure (defined in <netdb.h>):
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
/* official name of host */
/* alias list */
/* host address type */
/* length of address */
/* list of addresses from
name server */
Datagram Socket Example:
Receiver
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
main() {
int sock, len;
struct sockaddr_in name;
char buf [1024];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) exit(-1);
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = 0;
if (bind(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1);
len = sizeof(name);
if (getsockname(sock, (struct sockaddr *) &name, &len)) exit(-1);
printf("Receiver listening on port %d\n",ntohs(name.sin_port));
if (read(sock, buf, 1024) < 0) exit(-1);
printf("%s\n",buf);
close(sock);
}
Datagram Socket Example:
Sender
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
main(int argc, char **argv) {
int sock;
struct sockaddr_in name;
struct hostent *hp, *gethostbyname();
if (argc != 3) exit(-1);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) exit(-1);
hp = gethostbyname("prime.cs.ohiou.edu");
if (hp == 0) exit(-1);
bcopy(hp->h_addr, &name.sin_addr, hp->h_length);
name.sin_family = AF_INET;
name.sin_port = htons(atoi(argv[1]));
if (connect(sock, (struct sockaddr *) &name, sizeof(name))) exit(-1);
if (write(sock, argv[2], (strlen(argv[2])+1)) < 0) exit(-1);
close(sock);
}
Summary
• The client-server model is widely-used for application
interaction over a TCP/IP internet
– Server: a program that offers a service that can be reached over a
network
• Examples: web server, file server
– Client: a program that requests a service from a server
• Examples: ping, browser
• Application programs typically interact with the
networking software by using sockets and system calls:
–
–
–
–
Socket – create a socket
Bind – bind a socket to a port
Read/write – get/put data in a socket
Etc.