Lecture3.pptx

Download Report

Transcript Lecture3.pptx

CSE 30264
Computer Networks
Prof. Aaron Striegel
Department of Computer Science & Engineering
University of Notre Dame
Lecture 3 – January 14, 2010
Implementing Network Software
Outline
Sockets
Example
Homework 2
Spring 2010
CSE 30264
2
Communication Paradigms
Spring 2010
CSE 30264
3
Stream Paradigm (TCP)
• Stream
– Sequence of bytes flows from one
application program to another
– Think of it like a tube of data
• Bi-directional
– Streams in both directions
– Client to server, server to client
firefo
x
Port: 7583
129.74.200.5
• No boundaries
IIS
Port: 80
129.74.120.75
– Streaming protocol does not
understand boundaries
– Stream of bits
• No size limits*
– One byte -> large blocks of data
– * From the application perspective
firefo
x
129.74.200.5, 7583
IIS
129.74.120.75, 80
• Protocol selects delivery size
– Combine smaller blocks
– Divide large blocks
Spring 2010
CSE 30264
4
Message Paradigm (UDP)
• Message paradigm
– Network accepts and delivers messages
– Datagram service
Shoutcast Port: 8000
Port: 42083
firefox
• Messages are self-contained
– Protocol simply forwards
– No re-assembly, highly aware of packetization
– N bytes in a message, N bytes
received on the other side
– Size limit on messages
62.74.89.71
129.74.200.10
• May be uni-directional or bi-directional
• Spray and pray – no guarantees
– May be lost
– Delivered out-of-order
– May be duplicated
Shoutcast
Destination
129.74.200.10,
42083
• More burden on programmer
Spring 2010
CSE 30264
5
Sockets
Operating
System
• Socket programming
– Adapter that gets an app
onto the network
– API – Application Programming
Interface
– Give me a TCP socket,
hold the aggregation
Spring 2010
CSE 30264
Socket
Network
Stack
Device
Drivers
Physical
Adapter
6
Socket Operations
• Dependent on type of socket
– Different ops for TCP vs. UDP
• Supported operations
– Specify local/remote communication endpoints
• Who / where -> IP address / port
– Initiate a connection
• Ring the telephone
– Wait for a connection
• Server
– Send/receive data
– Handle incoming data
– Terminate connection gracefully
– Handle connection termination from remote site
– Abort communication
– Handle error conditions or connection abort
– Allocate and release local resources
Spring 2010
CSE 30264
Not to be confused
with Sprockets
7
Socket Communication
• Very similar to file I/O
– File I/O
– Open, close, read, write, seek, fcntl, ...
• Network communication
– File descriptor for network comms
• Socket is just another file handle
– Is a network connection really a file?
• Initiate, respond to aborted connection
• Socket API was born
– Nominally the same between C and C++
– Similar beyond C, C++
• Java, Perl, PHP, C#
Spring 2010
CSE 30264
8
Upcoming Programming
• Homework 2 - Implement a UDP client
– Connect to a server
– Send a message
– Receive a message
• Project 1 – Implement Client / Server
– Small scale TCP
• Google is your friend
– Just document where you get help from
– C UDP Socket Tutorial
http://www.cs.ucsb.edu/~almeroth/classes/W01.176B/hw2/examples/udp-client.c
Spring 2010
CSE 30264
9
Refresh -> Client-Server Model
•
•
•
•
Server listens for requests from clients
Server: passive open
Client: active open
request
Example:
response
–
–
–
–
–
–
file server
web server
print server
mail server
name server
X window server
Spring 2010
SERVER
CLIENTS
CSE 30264
10
Overview - TCP
SERVER
socket()
bind()
CLIENT
listen()
socket()
accept()
blocks until connection from client
connection establishment
read()
data request
connect()
write()
process request
write()
Spring 2010
data reply
CSE 30264
read()
11
Overview - UDP
SERVER
socket()
CLIENT
socket()
recvfrom()
data
sendto()
sendto()
data
recvfrom()
Spring 2010
CSE 30264
12
Socket Creation
int socket(int family,
int type,
int protocol);
• Return value:
– Socket descriptor
– Use for all subsequent operations
Spring 2009
CSE 30264
13
Socket Creation
#include <sys/types.h>
#include <sys/socket.h>
int s;
s = socket (PF_INET, SOCK_STREAM, 0);
Default
Protocol Family –
Internet
Spring 2010
CSE 30264
Socket Type –
Streaming Socket
14
Socket Creation
• Family
– PF_INET, PF_UNIX, ...
– Also may be listed as AF_INET (Address Family)
• Socket Types
– Datagram
UDP SOCK_DGRAM
– Reliable stream TCP SOCK_STREAM
– Raw
IP
SOCK_RAW
• Must be root to use
• Protocol
– Typically 0 = default protocol for type
Spring 2010
CSE 30264
15
Endpoint Addresses
• Different protocols may have different representations.
• TCP/IP uses combination of IP address and port.
• Sockets offer different ‘address families’
– TCP/IP uses a single address representation (AF_INET).
• PF_INET, AF_INET often confused
– Use same numeric value.
– Most code
#define PF_INET AF_INET (Linux)
Bind for the local one
Two addresses
Spring 2010
Connect to the remote one
CSE 30264
16
Socket Addresses
(1)
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
/* sys/socket.h */
/* address family */
/* addressing information */
(2)
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct sin_addr sin_addr;
}
/* AF_INET */
/* network byte order */
/* network address */
Two options
(1) is most common, (2) is actually the recommended approach
Spring 2010
CSE 30264
17
Refresher - C
•
•
•
•
•
•
•
Types – char, short, long
Pointers
size
struct
typecasting
Memset
Endian-ness
Spring 2010
CSE 30264
See board in class /
wiki for more examples
18
Binding the Local Address
int bind(int s,
struct sockaddr *addr,
int addresslen);
• Socket has no notion of endpoint addresses when
created
– Neither local nor remote
• Necessary on the server side
– Need to specify the local endpoint
• Not always necessary for the client
– May just need to send data towards a destination
– Let OS auto-assign
Spring 2010
CSE 30264
19
Binding the Local Address
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int s;
struct sockaddr_in sin;
s = socket(PF_INET, SOCK_DGRAM, 0);
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (6000);
/* 0 -> let system choose */
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* allow any interface */
bind (s, (struct sockaddr *)&sin, sizeof(sin));
Spring 2010
CSE 30264
20
Breaking the Code Down
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (6000);
/* 0 -> let system choose */
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* allow any interface */
Take the struct and initialize it to zeroes
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (6000);
/* 0 -> let system choose */
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* allow any interface */
Set the address family to Internet
Spring 2010
CSE 30264
21
Breaking the Code Down
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (6000);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
htons – Host to
network short
The port should be set to 6000
memset((char *)&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (6000);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
htonl – Host to
network long
Just use a local address, I don’t care which one
Spring 2010
CSE 30264
22
Converting a String
• What if we need to specify an address?
– inet_addr(char *)
– Takes a string with the dot notation, converts to a
four byte IPv4 value
inet_addr(“129.74.56.5”)
inet_addr(argv[1])
Spring 2010
CSE 30264
23
Breaking the Code Down
bind (s, (struct sockaddr *)&sin, sizeof(sin));
Bind our socket s to the information in the socket
address structure sin and oh by the way, here is the
size of that struct too
Typecast – oh yeah!
Spring 2010
CSE 30264
24
netstat
• HW 1
– netstat –ra
• Routing table
• Shows socket
usage
Spring 2010
CSE 30264
25
setsockopt()/getsockopt()
• May need to tweak the socket
– How long to wait to purge a socket?
• Default is two MSL (Maximum Segment Lengths)
• If not properly terminated, bind() will return EADDRINUSE.
setsockopt(int s, int level, int optname,
const void *optval, int optlen);
getsockopt(int s, int level, int optname,
void *optval, int *optlen);
• Before bind:
setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt));
• Allows re-use of port in use.
Spring 2010
CSE 30264
26
Why reuse ports?
• What if your code
crashes?
• Otherwise, you wait …
wait … wait … wait
• When we write servers,
you will get a range of
ports to use
• Rogue programs
– ps –A –f
kill XXXX (XXXX is PID)
Spring 2010
CSE 30264
27
What do we have so far?
• socket()
– Creates the local “handle”
– Identifier for all socket operations
• bind()
– Allows us to pick the endpoint address
• Port number
• Potentially address on a multi-interface machine
– Must be done on the server
• setsocketopt, getsocketopt
– Change socket options
Spring 2010
CSE 30264
28
Contrast – TCP, UDP
SERVER
TCP Only
socket()
TCP, UDP
bind()
CLIENT
listen()
socket()
accept()
blocks until connection from client
connection establishment
read()
data request
connect()
write(), sendto()
process request
write()
Spring 2010
data reply
CSE 30264
read(), recvfrom()
Technically,
it is possible
to use connect
with UDP
29
Overview - UDP
SERVER
socket()
CLIENT
socket()
recvfrom()
data
sendto()
sendto()
data
recvfrom()
Spring 2010
CSE 30264
30
connect()
int connect(int s,
struct sockaddr *addr,
int namelen);
• Client issues connect() to
– Establish remote address and port
– Establish connection
• Fails if host not listening to port
Spring 2010
CSE 30264
31
Class Exercise
• Sketch the code for a connect call to the following
location:
IP Address: 192.168.4.6
Port: 89
Spring 2010
CSE 30264
32
UDP – Send / Receive Data
• UDP -> Spray and Pray
– sendto -> Send a message
– recvfrom -> Listen on the port for a message
• Key parameters
–
–
–
–
–
Socket
Buffer pointer
Buffer size (bytes)
Socket address
Size of socket address struct
Spring 2010
CSE 30264
33
recvfrom
ssize_t recvfrom(int socket,
void *buffer,
size_t length,
int flags,
struct sockaddr *address,
socklen_t *address_len);
Socket
Pointer to a pre-allocated buffer
Size of the buffer (to avoid overflow)
Flags (usually 0)
Address struct – where the packet came from
Length of the address
Spring 2010
CSE 30264
May be NULL
34
sendto
ssize_t sendto(int socket,
const void *message,
size_t length,
int flags,
const struct sockaddr *dest_addr,
socklen_t dest_len);
Socket
Pointer to a pre-allocated buffer
Size of the buffer (to avoid overflow)
Flags (usually 0)
Address struct – where the packet came from
Length of the address
Spring 2010
CSE 30264
35
In-Class Derivation
• Write a small snippet of code to send a packet via
UDP
Spring 2010
CSE 30264
36
Where next?
• UDP – protocol is up to programmer
– How many messages?
– Who talks when?
– What type of message?
Homework 2: Write a simple UDP client
Read a small text file
Send to the server
Read / display the response
Spring 2010
CSE 30264
37
Revisiting TCP – Server Side
SERVER
TCP Only
socket()
TCP, UDP
bind()
CLIENT
listen()
socket()
accept()
blocks until connection from client
connection establishment
read()
data request
connect()
write(), sendto()
process request
write()
Spring 2010
data reply
CSE 30264
read(), recvfrom()
38
listen()
int listen(int s, int backlog);
• Only for stream sockets.
• Socket being listened to can’t be used for client.
• listen() does not wait for connections, but needed
to receive connection attempts.
• Completes 3-way handshake.
Spring 2010
CSE 30264
39
listen()
• Allows backlog pending connection requests
(SYN, completed 3WH) while waiting for
accept().
• Queue full: SYN requests are silently dropped.
• “SYN-flood”: send fake TCP SYN requests to fill
queue.
Spring 2010
CSE 30264
40
TCP Preview
client
port=64444
server
port=9
active open
connect()
SYN j
(64444, 9)
listen()
SYN k
ACK j+1
(9, 64444)
ACK k+1
active close
close()
(64444, 9)
FIN
ACK
Spring 2010
CSE 30264
41
accept()
int accept(int s,
struct sockaddr *addr,
int *addrlen);
• By connection-oriented server, after listen().
• Can’t preview connection: accept and close.
• Returns a new socket.
• Returns address of client.
Spring 2010
CSE 30264
42
Sending/Receiving
• TCP
– read/write: send/receive, no explicit address
– send/recv: send/receive, flags
• UDP
–
–
–
–
sendto: specify destination explicitly
recvfrom: also receive address of peer
sendmsg: msghdr data structure, scatter/gather
recvmsg: msghdr data structure, scatter/gather
Spring 2010
CSE 30264
43
Example: TCP Client
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int s, n;
struct sockaddr_in sin;
char msg[80] = “Hello, World!”;
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror(“socket”); return(-1);
}
Spring 2010
CSE 30264
44
Example: TCP Client
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[2]));
sin_addr.s_addr = inet_addr(argv[1]);
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
perror(“connect”); return -1;
}
if (write(s, msg, strlen(msg)+1) < 0) {
perror(“write”); return -1;
}
if ((n = read(s, msg, sizeof(msg))) < 0) {
perror(“read”); return -1;
}
printf(“%d bytes: %s\n”, n, msg);
if (close(s) < 0) { perror(“close”); return -1; }
return 0;
}
Spring 2010
CSE 30264
45
Single-Threaded Server
int server;
server = initialize();
while (1) {
wait_for_request(server);
read_request;
send_answer;
}
Spring 2010
CSE 30264
46
Example: TCP Server
int main(int argc, char *argv[]) {
int s, t, n;
struct sockaddr_in sin;
char *r;
char buf[100];
int sinlen;
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror(“socket); return -1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(13333);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind (s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
perror(“bind”); return -1;
}
if (listen(s, 5) < 0) { perror(“listen”); return -1;}
Spring 2010
CSE 30264
47
Example: TCP Server
for (;;) {
sinlen = sizeof(sin);
if ((t = accept(s, (struct sockaddr *) &sin, &sinlen)) < 0) {
perror(“accept”); return -1;
}
if (read(t, &buf, 100) < 0) { perror (“read”); return -1;}
r = gettime();
if (write(t, r, strlen(r)) < 0) { perror (“write”); return -1; }
if (close(t) < 0) { perror(“close”); return -1; }
}
if (close(s) < 0) { perror(“close”); return -1; }
}
Spring 2010
CSE 30264
48
Other Socket Functions
• The socket API contains a variety of support
functions
–
–
–
–
–
–
getpeername
gethostname
setsockopt
getsockopt
gethostbyname
gethostbyaddr
Spring 2010
CSE 30264
49
What About Threaded Servers?
• The socket API works well with concurrent servers
• Implementations of the socket API adhere to the following inheritance
principle:
– each new thread that is created inherits a copy of all open sockets from the
thread that created it
– the socket implementation uses a reference count mechanism to control
each socket
– when a socket is first created
• the system sets the socket’s reference count to 1
• and the socket exists as long as the reference count remains positive.
– when a program creates an additional thread
• the thread inherits a pointer to each open socket the program owns
• and the system increments the reference count of each socket by 1
– when a thread calls close
• the system decrements the reference count for the socket
• if the reference count has reached zero, the socket is removed
Spring 2010
CSE 30264
50