Networking UDP: User Datagram Protocol UDP encapsulation UDP

advertisement
UDP: User Datagram Protocol
• RFC 768 [Postel 1980]: about three
pages.
• provides no reliability
Networking
– it sends the datagram to the IP layer, but
there is no guarantee that:
• it will reach its destination
• it will reach unspoiled its destination
UDP - checksum
UDP encapsulation
32-bit source IP address
32-bit destination IP address
IP datagram
UDP datagram
IP
header
UDP
header
8-bit
protocol(17)
16-bit UDP
length
16-bit source port
number
16-bit
destination
port number
16-bit UDP length
16-bit UDP
checksum
zero
UDP
data
pseudo
header*
header
data
(*) not transmitted, only used for checksum calculations
IP: Internet Protocol
UDP fragmentation
4-bit
4-bit
header
version length
IP datagram
IP
header
20 bytes
UDP
header
8-bit type of
service
(TOS)
3-bit
flags
16-bit identification
UDP data (1473 bytes)
8-bit time to
live
(TTL)
8 bytes
16-bit total length (in bytes)
8-bit protocol
13-bit fragment offset
16-bit header checksum
20 bytes
32-bit source IP address
32-bit destination IP address
IP
header
20 bytes
UDP
header
8 bytes
packet
1472 bytes
IP
header
20 bytes
options (if any)
1 byte
packet
data
IP Datagram
1
format
TFTP
Trivial File Transfer protocol
• uses UDP as its transport mechanism
• mainly used to bootstrap diskless systems
• RFC 1350[Sollins 1992] is the official
spec.
IP
header
UDP
header
opcode
(2 bytes)
read
write
01
read
request
01
some-file
0 octet 0
ack
04 01
filename
0
data
03
block
#
ack
04
block
#
error
05
error
#
mode
0
octet: binary/raw
ascii: convert nl to cr/nl
2 bytes
• lock-step protocol
the protocol
null terminated string
02
– RFC 2347, 2348, 2349 specify newer
extensions.
client
TFTP message
data 0 to 512 bytes
null terminated message
TFTP ...
server
• is a stop and wait protocol
data
03 01
data
• each data-block has a block number
– used in the acknowledge response
03
02
data
• lost packets are detected with timeout and
retransmission implemented on the sender side.
• has no checksum / data integrity check
– handled by the UDP layer
03
04
nn
< block size
why are protocols so difficult?
RRQ
time out
ACK 1
ACK 1
the Fix
data 1
data 2
data 2
ACK 2
ACK 2
• has no security
nn
•ignore duplicate ACKs
data 3
data 3
time out
The sorcerer's apprentice syndrome
2
DNS
tftp extensions
IP
header
opcode
(2 bytes)
1=RRQ
2=WRQ
UDP
header
The Domain Name System
• Server
TFTP message
– manage a distributed data base
– process queries/requests
filename
0
mode
0
option1 0 value1 0 option2 0 value2 0
• Client:
– does queries
– uses the resolver library functions
6=OACK option1 0 value1 0 option2 0 value2 0
• ie: gethostbyname(...), gethostbyaddr(...)
4=ACK
0
DNS basics
unnamed root
arpa
edu
DNS Zones
.
...
com
org
il
ac
in-addr
• a zone is a subtree of the DNS tree that is
administered separately.
• each zone needs at least one nameserver.
• each zone needs at least one
administrator.
huji
cse
DNS Message Format
Zones ...
• Primary name server
0
15 16
31
– obtains its data locally
identification
flags
• Secondary name server
# of questions
# of answer RRs
– obtains its data from the primary
# of authority RRs # of additional RRs
questions
answers
authority
additional information
12 bytes
header
variable
length
fields
3
format ...
•identification: set by the client and
returned by the server.
•flags:
DNS - Summary
• essential when host is connected to the
internet.
• hierarchical tree that forms the DNS name
space.
• all DNS queries and responses have the
same message format.
QR opcode AA TC RD RA MBZ rcode
4
3
4
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/*
| daytime server - RFC 867
*/
main(int cc, char **vv)
{
struct sockaddr_in sin;
char buf[BUFSIZ];
int
sfd;
while(1) {
int
time_t
len = sizeof(sin);
if(recvfrom(sfd, buf, 1, 0,
Clients & Servers
• Client:
– in general, an application that initiates a peer-to-peer
communication.
– usually invoked by the 'end user'
• Server:
(struct sockaddr *)&sin, &len)
< 0) {
perror("recvfrom");
continue;
}
time(&clock);
strcpy(buf, ctime(&clock));
if(sendto(sfd, buf, strlen(buf), 0,
(struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("sendto");
}
if((sfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
bzero(&sin, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = htons(13);
if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind");
exit(1);
}
len;
clock;
}
}
Concurrent Vs. Iterative
• concurrent-server
– handles multiple requests at one time.
• iterative-server
– process one request at a time.
– waits for incoming requests from a client.
– performs necessary work and
– probably returns a result.
4
Connection [oriented|less]
types of server/client
• connectionless:
– UDP - User Datagram Protocol
– the burden of the data integrity is on the application.
iterative
connectionless
iterative
connectionoriented
concurrent
connectionless
concurrent
connectionoriented
• connection-oriented:
– TCP - Transport Control Protocol
– the application is free to deal with higher things.
Server types
• iterative, connectionless
– the most common
• usually stateless
• trivial amount of processing
• iterative, connection-oriented
– less common
• trivial amount of data but
• need relaible transport
server types ...
• concurrent, connectionless
– very uncommon
• a process is created for each request
• tfptd is such a server
• concurrent, connection-oriented
– the most common
• reliable transport
• usually used by long living activities
reliable
TCP - Transmission Control Protocol
• connection oriented
– exactly two end points.
• no broadcast/multicast
– the two applications must establish a connection with
each other before data can be exchanged.
• reliable
• byte stream
– 8-bit bytes with no interpretation
• data is broken up into best size chunks
– the unit of information passed by TCP to IP is called a segment.
• each segment sent has a timer
– when the timer expires before an acknowledgment is received, the
segment is retransmitted.
• when data is received, an acknowledgment is sent
– but not immediately.
• the data and header have a checksum
– a segment with bad/invalid checksum is dropped, the sender times
out and retransmits
– there is no record boundaries.
5
reliable ...
TCP encapsulation
• preserves sequence
– IP datagrams can arrive out of order
IP datagram
– segments are resequenced if necessary
• drops duplicates
TCP segment
– since IP datagrams can get duplicated
IP
header
• flow control
TCP
header
TCP
data
– each end of the connection has a finite amount of buffer space.
– the receiving side allows the other end to send as much data as
it has buffer for.
TCP Header
16-bit source port
number
TCP Header ...
16-bit destination port
number
32-bit sequence number
32-bit acknowledgment number
4-bit
header
length
6-bit
flags
16-bit TCP checksum
20
bytes
16-bit window size
max
60
bytes
16-bit urgent pointer
options (if any)
• each segment contains a source and
destination port number.
• together with the source and destination
IP number from the IP header we get an
unique identification of each connection.
• socket: IP address + port number
• socket pair: source + destination sockets.
data (if any)
TCP Header ...
flags
Description
URG
the urgent pointer is valid
ACK
the acknowledgment is valid
PSH
the receiver should pass this data ASAP
RST
Reset the connection
SYN
Synchronous sequence number to init
connection
FIN
the sender has finished sending data
connection establishment
1. the client dials a #
2. the server answers, Hello?
3. who's calling?
6
Connection Establishment
the three way handshake
1. the client sends a SYN segment specifying the
port # of the server it wants to connect to, and
its ISN - Initial Sequence Number
2. the server responds with its own SYN segment
containing its ISN. The server also ACKs the
client's SYN by ACKing the client's ISN+1
3. the client must ACK this SYN from the server
by ACKing the server's ISN+1.
client
ISN + 2
ack
isn'+
1
segment 3
display
last byte that can be sent
before an ack is received
segment 2
•isn: initial sequence number
–incremented by 1 every 4
microseconds - actually by
64,000 every 1/2 sec.
–incremented on each
connection by 64,000
TCP - Interactive data flow
current window
ready to be sent
isn
'
- isn
SYN +1
isn
ack
client
keystroke
data stream
sent but not acked
SYN
-
segment 1
Segments, Streams and Sequence numbers
last byte successfully sent
acknowledged
server
server
data byte
yte
data b
f
o
k
c
a
e
ta byt
of da
echo
server
echo
ack of
echoed
byte
7
Download