Title: Effective TCP/IP Programming
1Effective TCP/IP Programming
2Understand IPv4 Addressing
3IPv4 originally Classful Addressing
- Special address
- Host ID 0 network address
- Network ID 0, host ID 0
- i.e 0.0.0.0 means this network
- 127.x.y.z looped back
- Host ID all 1s broadcasting
- NAT address
- 10/8
- 172.16/12
- 192.168/12
- Broadcast address
- 255.255.255.255
- Limited broadcast
- router ??? ??? ??
- 190.50.255.255
- Network-directed broadcast
- ? ?????? ?? ???? ?? broadcast
- 190.50.1.255/24
- Subnet-directed broadcast
IPv4 address? host? address ? ??? interface?
address??.
4Subnet
223.1.0.0/16
223.1.1.0/24
223.1.2.0/24
223.1.1.1
223.1.2.1
223.1.1.2
223.1.2.9
223.1.1.4
223.1.2.2
223.1.1.3
223.1.3.27
- Network address? IP address mask
- Network ID
subnet
223.1.3.2
223.1.3.1
223.1.3.0/24
network consisting of 3 subnets
- CIDR (Classless Inter-Domain Routing)
- Subnetting suppernetting
5Develop and Use Application Skeletons
6Making UNIX/Windows Compatible
UNIX bsd/skel.h
Windows win/skel.h
wincompat.c
Window?? socket? ???? call??
7TCP Server Skeleton
mclab.hufs.ac.kr or 203.254.68.114 or
-- server
http or 80
tcpserver.skel
8TCP Server Skeleton - Contd
Usage myserver local_addr local_name
local_port service Example myserver
15000 myserver localhost http
9TCP Client Skeleton
tcpclient.skel
Usage myclient peer_addr peer_name
peer_port service Example myclient
203.253.70.5 15000 myclient
www.hufs.ac.kr http
10UDP Server Client Skeleton
udpserver.skel
udpclient.skel
11Build Your Own Library and Use It !
etcp.h
12TCP Client Server Starting Functions
Host name or IP addr or (my addr for
server)
http or 80
13UDP Client Server Starting Functions
14Remember thatTCP is a Stream Protocol
15TCP is a Stream Protocol
- No message boundary, just a byte stream
- TCP application?? ??? message? send()??? ??
recevier application?? ? message? ? ???? recv()??
?? ???. - Message? send()??? TCP segment? ??, ??? ??? ???.
(buffering ?? ??) - Recv()?? ? byte ??? ???.
- If you want to read exactly n bytes ??
- If you want to make a record ???
- Use end-of-record mark. e.g) new line
- Handle variable records (using fixed header)
???? ?? user buffer? ?? ?, ??? ?? ? ?? ??
include ltsys/socket.hgt / UNIX / include
ltwinsock2.hgt / Windows / int recv (SOCKET s,
void buf, size_t bufsize, int flags) int read
(SOCKET s, void buf, size_t bufsize) / UNIX
/ Returns of bytes read (gt0), 0 if received
FIN and no more data, -1 on failure int send
(SOCKET s, const void buf, size_t len, int
flags) int write (SOCKET s, const void buf,
size_t len) / UNIX / Returns of bytes
transferred on success, -1 on failure
Socket send buffer? ??? ??? ??
16Use End-of-record mark read a line
lib/readline.c
17Read n bytes and Read a variable-length record
lib/readvrec
Network byte order
lib/readn.c
Header size
include etcp.h int readn (SOCKET s, char buf,
size_t len) Returns of bytes read,
-1 on failure int readvrec (SOCKET s, char buf,
size_t len) Returns of bytes read ,
-1 on failure
18Example Client/Server using variable records
vrs.c
vrc.c
19Dont Underestimate the Performance of TCP
20TCP versus UDP
- TCP
- connection-oriented
- reliable
- byte stream
- Application typically concurrent server
- SMTP(Simple Mail Transfer Protocol)
- Telnet
- FTP
- HTTP
- NNTP(Network News TP)
- UDP
- connectionless
- unreliable
- datagram
- Applications typically iterative server
- SNMP(Simple Network Management Protocol)
- TFTP(Trivial FTP)
- BOOTP(Bootstrap Protocol)
- DHCP(Bootstrap Protocol)
21TCP? ??? Optimize ?? ??.
- ???? TCP segment ?? ?? 30 instructions
(excluding checksum) - ACK? piggy-back
- ??? UDP? ???? ??.
- But, a single request-response? ???
transaction??? TCP? - Connection set-up RTT ??
- Connection release ??? RTT ??
22A UDP Source and Sink
?? 5,000?? datagram? buffering? ?? ??. ?? ??,
????? ???? socket receiver buffer? ?? ??? ??? ??
?? ????(?? KB).
udpsource.c
udpsink.c
Set UDP socket receive buffer size
Record of length 0 i.e. end-of-record mark
- UDP datagram? lost? ? ??!!
- Network congestion (no congestion cotrol)
- Recv buffer overflow(no flow control)
23A TCP Source and Sink
tcpsink.c
tcpsource.c
Options -s sndsz -b sndbufsz -c blks
Set TCP receive buffer size
Set TCP send buffer size
24Comparison of TCP and UDP Performance
- LAN?? UDP? TCP?? 20 ?? ??? ????
- ???, UDP??? lost? ?? ??? (no flow control)
- Loopback interface(?? host ?)??? TCP? UDP ?? ??
?? ???? - Local host? MTU? 16,384 B (BSD)
- Ethernet? MTU? 1,500 B
25Avoid Reinventing TCP
- Any reasonably robust UDP application must
provide - Error recovery reTx a request if not received a
response within RTO - Sequencing ensure that replies are matched
correctly to requests - Flow control if servers reply can consist of
multiple datagrams, prohibit overflow of clients
recv buffer - Cause to rewrite TCP
- TCP? kernel?? ???? ??? application?? reliable
protocol? ???? ??? ?? ???.
26When to Use UDP instead of TCP
- Adv. Of UDP
- supports broadcasting and multicasting
- no overhead for connection setup or teardown
- UDP requires 2 packets to exchange a request and
a reply - TCP requires about 10 packets to exchange
assuming new TCP connection is established for
each request-reply exchange - Features of TCP that are not provided by UDP
- positive ACK, reTx of lost packet, duplicate
packet detection, sequencing of packets - windowed flow control
- slow start and congestion avoidance
- Recommendation of UDP Usage
- must be used for broadcast or multicast
applications - desired level of error control must be added
- can be used for simple request-reply applications
- error detection must be needed
- should not be used for bulk data transfer
27Realize that TCP is a Reliable Protocol, Not
Infallible Protocol
28TCP is a Reliable Protocol, Not Infallible
Protocol
- 2 peer?? connection? ???? ? TCP? ordered and
uncorrupted delivery? ????. - Application? ??? ????? ??? ???? ?? ? ??, ??? ????
delivery ?? ??? ??? ????. - TCP? data? ???? ?? peer TCP? ?? ???? ?? ?? (ACK?
?? ??) ??, 2?? ?? ??? ??? ?? ???? ?? ???? ?? ? ?
?? - ??? ???? ??? ????? ???? ? heart beat mechanism ??
?? - Application? send()/recv()? error return ??? ??,
?? ????? ?? ??. ? failure ?? - ?? ???? ?? (?? connection? ???? ?? ??)
- Network outage (due to router or link failure)
- Peer app crashes
- Peer host crashes
29Network Outage
- Inside TCP
- Segment ?? ? ACK ? ???, 12? ReTx?? (? 9? ??)
- ??? ACK? ?? ???, set socket pending error
(ETIMEOUT) - Inside IP/ICMP
- IP datagram? forwarding? ? ???(router? link ???
??), ICMP host unreachable/network unreachable
message? source? ???. - ? ???? Source IP? ???, set socket pending error
(ENETUNREACH/EHOSTUNREACH) - Socket Pending Error
- Send() returns on failure ? send buffer? ?? ??
??? ?? - ?? ?? ???? peer?? ??? ? ??? ?? ??? ? ? ??.
- Kernel? ?? ?? error? ????, ???? socket? pending
?? ??? - Socket API call? ???? ?, error return??? errno?
????.
30Peer App Crashes
- When peer app crashes, Local app is
- In recv() return 0
- ???? ??? ??
- In send() normal return
- But, sent data is lost.Local connection? ??
close.Error is pending. - Send()/recv() error return (ECONNRESET)
- Send()/recv() rrror return (EPIPE)
- Peer app crashes(killed)
- Call exit(), implicitly
- Call close() in exit()
- TCP send FIN
FIN
data
No connection ! Not delivered to peer app
RESET
31Ways to Detect Various TCP Condition
32Peer app crashes an example
tcprw.c
count.c
killed
killed
33Peer Host Crashes
- Local app
- Connection set-up
- Send() normal return
- But sent data is lost
- Error is pending (ETIMEOUT) after retransmitting
12 times(9 min) - or error is pending (EHOSTUNREACH or ENETUNREACH)
by ICMP
- Peer host crash
- No TCP there, so no TCP response
data
No TCP/IP Protocol !
34Remember that TCP/IP is Not Polled
- No notification when connectivity is lost
35Heartbeats
- ???? ?? ?? ???? peer? ?? ?? ??? ? ? ??
- ?? ?????? ???? ???? ??? lost?(????? ? ???? ???)
- ??? ??? ??? ??? ?? ??? ????? check? ?? ? ?
hearbeat ?? - C/S? ???? msg type? ???? ??
- Heartbeat msg? ??? type? ??
- C/S? byte stream?? ???? ??
- Hearbeat ? data? ??? ? ??
- Hearbeat? ?? ?? TCP connection ???? ??
- Or, Hearbeat? ?? OOB msg? ??
- (send/recv() ?? flag? OOB? ??)
36Hearbeat Client msg type
heartbeat.h
hb_client.c
37Heartbeat Server- msg type
hb_server.c
38Hearbeat Client separate connection
hb_client2.c
39Hearbeat Server separate connection
hb_server2.c
40Be Prepared for Rude Behavior from a Peer
41- Checking for client termination
- Checking for valid input
- UNIX utility program ? 6 43 ? ????? ???
random input?? ???? ? ???
42Read a line
lib/readline.c
len
0
bufptr
bufx
Buffering
cnt
bp
b
From socket
C string? \0? append?? ? Line gt 2 bytes
43Consider Letting inetd Launch Your Application
44inetd daemon
- Problems starting with /etc/rc(without inet
daemon) - All the servers contains nearly identical startup
code - Each daemon takes a slot in process table, but
asleep most of time - /etc/inetd.conf file specifies the services that
the superserver inetd is to handle
45Steps performed by inetd
dup2(sockfd, 0) dup2(sockfd, 1) dup2(sockfd,
2) close(sockfd)
Open descriptor?? fork? copy?? Exec ??? ????.
Exec?? Peer? ? ? ?? ????
46Consider Using Two TCP Connections
47Concurrent Input/Output processes
- One-connection architecture
- Xout?? send? ? pending error? xin?? recv?? ? ? ??
- Xin ? mp ? xout ?? ?? ?? ?
- Two connection architecture
- ??? connection? ?? socket pending error? ???
testing ?? - Using select() readability
48Example
xout1.c
49Making Your Applications Event Driven
50Making Your Apps Event Driven
- Types of events in protocols and networked
application - Packet arrival
- User input
- Timeout
- API call
- ??, API? UNIX domain socket(??? socket
descriptor?)??? select()? API call? ????? ?? ?? - ??, select()? timeout? ??? ??
- How to support multiple timeouts using select() ?
- You can make it !
select()? ?? ?? (Ready or timeout? )
51Timers in UNIX/Linux
- Using SIGALRM signal ? ??
- Interval timer ??? 10 msec ??? ??
- select() ?? 10 msec ??? ??
signal(SIGALRM, handler) // or
sigaction() alarm(seconds) // set timeout
timer void handler(in signo) . // actions
in timeout event
include ltsys/time.hgt int getitimer(ITIMER_REAL,
struct itimerval value) int setitimer(int
which, const struct itimerval value, struct
itimerval ovalue) struct itimerval
struct timeval it_interval /
next value / struct timeval
it_value / current value /
struct timeval long
tv_sec / seconds /
long tv_usec / microseconds /
52Multiple Timeouts using select()
include etcp.h int tselect( int maxfdp1,
fd_set readset, fd_set writeset, fd_set
exceptset ) Returns of ready events, 0
if no remaining events, -1 on error unsigned int
timeout( void (handler)(void ), void arg, int
msec ) Returns timer ID to be used in
untimeout call void untimeout( unsigned int
timerid )
Examples
/ call retransmit(packet) after 1500 msec
(timeout) / rxtimer timeout(retransmit, (void
) packet, 1500 ) UItimer timeout(useridle,
NULL, 30000) n tselect(maxfdp1, readset,
writeset, exceptset ) // if received ACK,
untimeout(rxtimer) // reset the timer
53Tselect() source from etcp
lib/tselect.c
54(No Transcript)
55(No Transcript)
56(No Transcript)
57Internet Checksum Algorithm