Title: Unix Network Programming
1Unix Network Programming
- Prof. Nelson Fonseca
- nfonseca_at_ic.unicamp.br
- http//www.ic.unicamp.br/nfonseca
2Cleint-Server
Application protocol
Client
Server
3Client Server on different LANs connected through
a WAN
Applic. Client
Applic. Server
Host with TCP/IP
Host with TCP/IP
LAN
LAN
router
router
WAN
router
router
router
router
4Overview of TCP/IP
5Programming with sockets
- API Sockets
- First in BSD4.1 UNIX,1981
- Explicitly created and terminated by the
applications - client/server
- dois tipos de serviço de transporte via API
Sockets - Datagram, non-reliable delivery
- Byte flow, reliable delivery
6TCP sockets
- Socket a port used for the communication
between two remote processes (UDP ou TCP) - Serviço TCP reliable data transfer, a pipe
between the two remote processes
Created and owned by the application
Created and owned by the application
Controlled by the Operating system
Controlled by the operating system
internet
7 UDP User Datagram Protocol RFC 768
- no luxuary
- Best effort service,
- UDP segments can be
- lost
- Delivered out of order
- no connection
- There is no connection setup
- Each message is proccessed individually
- Why use UDP?
- No connection setup overhead
- stateless
- Small header
- No congestion control
- No flow control
8 UDP
- Used in real time applications
- Loss tolerant
- Delay sensitive
- Other protocols
- DNS
- SNMP
- Any functionality needs to be added at the
application layer
32 bits
Source port
Destination port
checksum
length
Application layer data
UDP segment format
9 TCP RFCs 793, 1122, 1323, 2018, 2581
- Point to point
- One sender, one receive
- Ordered flow of bytes, reliable delivery
- Non-structured messages
- pipelined
- Transmission window controlled by flow control
and by congestion control - Buffers at the sendder and at the receiver
- transmissão full duplex
- MSS Maximum segment size
- Connection oriented
- Handshaking
- Flow controlled
10 TCP segment
11TCPsequence and ack number
B
A
send C
Seq42, ACK79, data C
Seq79, ACK43, data C
Seq43, ACK80
cenário simples de telnet
12 TCP reliable data transfer
event data received from application above
create, send segment
wait for event
event timer timeout for segment with seq y
wait for event
retransmit segment
event ACK received, with ACK y
ACK processing
13 TCP retransmission scenarios
Host A
Host B
Host A
Host B
Seq92, 8 bytes data
Seq92, 8 bytes data
Seq100, 20 bytes data
ACK100
Temp.p/ Seq92
Time out
X
ACK100
ACK120
Timeout Seq100
perda
Seq92, 8 bytes data
Seq92, 8 bytes data
ACK120
ACK100
Early timeout
Duplicated ACKs
14TCP options
- MSS Maximum Segment Size
- Window scale option
- Maximum window that can be advertized 65535 bytes
- High speed connection ( gt 45Mbits/s) and long
propagation delays sattelite (long fat pipe)-
high bandwidth-delay product - Scale factor 0 to 14 bits
- Maximum size 65535 x 214 1GB
- Timestamp
15Packet exchange
16 TCP connection termination
client
server
close
FIN
ACK
close
FIN
ACK
TimeWait
fechada
17 TCP connection management
18TCP state transition diagram
19TCP state transition diagram
20TIME_WAIT state
- Twice the Maximum Segment Lifetime (2 MSL)
- Recommended MSL 2 minutes
- IP packet maximum 255 hops (8 bits TTL)
- TTL avoids routing loop
- Time-wait allow
- Reliable connection termination
- Expiration of late dupliacates
21Port numbers
- Well-knows ports (0 1023)
- Accessible only by superusers in the UNIX system
Dynamic or private IANA ports
Well known ports IANA
Registred IANA ports
1 1023 1024
49151 49152 65535
Portas Reservadas BSD
BSD Ephemeral ports
BSD servers (nonpriviled)
1 1023 1024 3000
3001
65535
rresvport
Solaris Ephemeral ports
513-1023
32768
65535
22Socket pair
- ltlocal IP address, local TCP port, remote IP
address, remote TCP portgt
12.106.32.254 192.168.42.1
206.168.112.219
Client1
Server
connection
206.168.112.2191500, 12.106.32.25421
21,
listening socket
Server (child1)
fork
Client2
12.106.32.25421, 206.168.112.2191500
Connected socket
connection
206.168.112.2191501, 12.106.32.25421
Server (child 2)
12.106.32.25421, 206.168.112.2191501
Connected socket
23Connection establishment
24Buffer sizes and limitations
- Maximum size of IPv4 datagram 65535 bytes
including the header - Maximum size of IPv6 datagram 65575 bytes
including the header - IPv6 option Jumbo Payload 32 bits, MTU must
support
25Buffer sizes and limitations
- Minimum MTU
- IPv4 68 bytes IPv6 576 bytes
- Path MTU minimum MTU in a path
- Path MTU discovery
- Minimum reassembly buffer size (minimum datagram
size) - IPv4 576 bytes, IPv6 1500 bytes
26Steps and buffers limitations involved when
application writes to a TCP socket
Application
Application Buffer)
write
User process
Kernel
TCP
Socket sender buffer (SO_SNDBUF)
Segmentos TCP do tamanho do MSS
MSS normally MTU 40 (IPv4) ou MTU 60 (IPv6)
IP
MTU-sized IPv4 or IPv6 packets
Output queue
link
27Buffer sizes and limitations
- Kernel copies data from application buffer to
socket buffer - If no space is available, process is blocked
until the transfer of last byte - A suuccessful write does not mean remote process
received the data
28Steps and buffers limitations involved when
application writes to a UDP socket
Application
Application buffer
sendto
User process
Kernel
UDP
Socket send buffer (SO_SNDBUF)
UDP datagram
IP
MTU-sized IPv4 or IPv6 packets
Output queue
link
- Sucessful write datagram was placed at link
output queue -
29Buffer sizes and limitations