Transport Protocols Overview
-
Upload
siobhan-crofton -
Category
Documents
-
view
41 -
download
0
description
Transcript of Transport Protocols Overview
Transport Protocols OverviewTransport Protocols Overview
Transport services and protocolsTransport services and protocols
provide logical communication between app processes running on different hosts
transport protocols run in end systems send side: breaks app
messages into segments, passes to network layer
rcv side: reassembles segments into messages, passes to app layer
more than one transport protocol available to apps Internet: TCP and UDP
application
transportnetworkdata linkphysical
application
transportnetworkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysical
networkdata linkphysicalnetwork
data linkphysical
logical end-end transport
TCP versus UDPTCP 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)
TCP OverviewTCP Overview
full duplex data: MSS: maximum segment
size
connection-oriented: flow controlled: point-to-point:
reliable, in-order byte steam: no โmessage boundariesโ
pipelined: TCP congestion and flow
control set window size
send & receive buffers
send() recv()
send() ๋ send buffer ์ ์ผ๋ค๋ ์๋ฏธ์ด์ง ,๋ณด๋๋ค๋ ์๋ฏธ๊ฐ ์๋๋ค .send() ๋ send buffer ์ ์ผ๋ค๋ ์๋ฏธ์ด์ง ,๋ณด๋๋ค๋ ์๋ฏธ๊ฐ ์๋๋ค .
TCP segment structureTCP segment structure
source port # dest port #
32 bits
applicationdata
(variable length)
sequence number
acknowledgement numberReceive window
Urg data pnterchecksum
FSRPAUheadlen
notused
Options (variable length)
URG: urgent data (generally not used)
ACK: ACK #valid
PSH: push data now(generally not used)
RST, SYN, FIN:connection estab(setup, teardown
commands)
# bytes rcvr willingto accept
countingby bytes of data(not segments!)
Internetchecksum
(as in UDP)
Multiplexing/demultiplexingMultiplexing/demultiplexing
application
transport
network
link
physical
P1 application
transport
network
link
physical
application
transport
network
link
physical
P2P3 P4P1
host 1 host 2 host 3
= process= socket
delivering received segmentsto correct socket
Demultiplexing at rcv host:gathering data from multiplesockets, enveloping data with header (later used for demultiplexing)
Multiplexing at send host:
Connectionless demux (cont)Connectionless demux (cont)
DatagramSocket serverSocket = new DatagramSocket(6428);
ClientIP:B
P2
client IP: A
P1P1P3
serverIP: C
SP: 6428
DP: 9157
SP: 9157
DP: 6428
SP: 6428
DP: 5775
SP: 5775
DP: 6428
SP provides โreturn addressโ
Connection-oriented demux (cont)Connection-oriented demux (cont)
ClientIP:B
P1
client IP: A
P1P2P4
serverIP: C
SP: 9157
DP: 80
SP: 9157
DP: 80
P5 P6 P3
D-IP:CS-IP: A
D-IP:C
S-IP: B
SP: 5775
DP: 80
D-IP:CS-IP: B
Sockets IntroductionSockets Introduction
Protocols and their ImplementationProtocols and their Implementation Protocol Implementation
๋ณดํต transport layer ์ดํ protocol ๋ค์ kernel (OS) ๋ด์ ์์ฃผํ๋ค . Why?
Link layer ๋ H/W ์ ์ด๋ฅผ ์ปดํจํฐ์ ์ฅ์ฐฉํ๊ธฐ ์ํ S/W (์ : Ethernet driver) ๋ก ๊ตฌํ๋๋ค .
Application protocol ์ user process ( ์ฆ , application) ์ผ๋ก ๊ตฌํ๋๋ค .
Client-server model Internet ์์ ๋ง์ application
protocol ์ ์ด model ์ ๊ฐ์ ํ๊ณ ์๋ค .
Server ๋ ์ฌ๋ฌ client ์ ์์ฒญ์ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด์ผ ํ๋ค .
Peer-to-peer model Peer ๋ด์ client ์ server ๋ชจ๋
๊ฐ์ง๊ณ ์๋ model ๋ก ์๊ฐํ ์ ์๋ค .
S/W
H/W EthernetAdapter
EthernetAdapter
Socket APISocket API API: Application Program Interface
User process ๊ฐ kernel ์ฌ์ด์ interface ๋ฅผ API ๋ผ๊ณ ํ๋ฉฐ , ํํ system call ์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค .
Function call vs. system call Socket API is an API for communications
๊ฐ์ฅ ๋๋ฆฌ ์ฐ์ด๋ Communication API ์ฌ๋ฌ protocol suite ์ ๋ํด generic API ์ ๊ณต Transport, network, link layer ์ ์๋น์ค๋ฅผ ๋ฐ์ ์ ์๋ API ๋ ์ ๊ณต
What is a socket?What is a socket? A handle (or descriptor) to a data structure
Not a pointer! The data structure lives in the kernel. UNIX/LINUX ์์๋ socket ๋ file ์ฒ๋ผ ์ฌ์ฉ ๊ฐ๋ฅ sockfd = socket(PF_INET, SOCK_STREAM, 0);
user-level library of functions send(sockfd, ...), recv(sockfd, โฆ), โฆ
a single system call via trap (special instruction causing software interrrupt) sys_socketcall(SEND, sockfd, โฆ)
File, device
socket
Data Structure in Kernel (BSD)Data Structure in Kernel (BSD)
ํ๋ก์ธ์ค์์๋ handle (descriptor) ๋ก socket structure ๋ฅผ ์ฐพ์ ์ ์๋ค .
Socket (file) ์ ๋ํด read, write, select, close ๋ฑ์ ์ผ๋ฐ์ ์ธ file operation ์ socket ํน์ฑ์ ๋ง๊ฒ ์คํํ ์ ์๋ค .
Incoming packet ์ header ๋ฅผ ๋ณด๊ณ ํด๋น๋๋ socket ์ ์ฐพ์ ์ ์๋ค . TCP: (local addr, local port,
foreign addr, foreign port) Connected UDP socket ์ธ
๊ฒฝ์ฐ์๋ ํด๋น
UDP: (local addr, local port)
UDP socket creationsockfd=socket(PF_INET, SOCK_DGRAM, 0);
sockfd
Transport Protocol
Sending and Receiving via SocketSending and Receiving via Socket
Address family
Local addr Foreign addr
Local port Foreign port
Recv bufferRecv buffer Send bufferSend buffer
Client App
Server App
Transport Protocol
Address family
Local addr Foreign addr
Local port Foreign port
Recv bufferRecv buffer Send bufferSend buffer
Socket API(Operations)
A TCP connection
Socketdescriptor
Socket Address StructuresSocket Address Structures
IPv4 SA structure
Generic SA structure SA structure ์ pointer ๋ฅผ casting ํ ๋๋ง ์ฌ์ฉ
IPv4 Socket Address StructureIPv4 Socket Address Structure SA structure itself are
used only on a given host, not used for communication
POSIX datatypes
always set to 0
Elementary Elementary Sockets APISockets API
TCP Client-Server Procedure TCP Client-Server Procedure 12.106.32.254192.168.42.1
206.168.112.219203.253.70.44
foreignlocal
????
foreignlocal
21150012.106.32.254206.168.112.219
foreignlocal
?21?0
foreignlocal
?21?0
foreignlocal
150021206.168.112.21912.106.32.254
Listening socket (=3)
Connected socket (=4)
FIN
TCP segment
foreignlocal
????
INADDR_ANY
socket(): Create a Socketsocket(): Create a Socket
Socket descriptor is a file descriptor in UNIX systems, but is not a file descriptor in Windows
field local foreign
addr ? ?
port ? ?
TCP socket
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */SOCKET socket (int family, int type, int protocol);Returns: socket descriptor on success, -1 (UNIX) or INVALID_SOCKET (Windows) on failure
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int connect (SOCKET s, const struct sockaddr *peer, int peer_len);Returns: 0 on success, -1 (UNIX) or nonzero (Windows) on failure
connect(): Establish the Connectionconnect(): Establish the Connection
Kernel ์ ๋ฏธ์ฌ์ฉ ์ค์ธ port ๋ฒํธ๋ฅผ ํํ๊ณ , forwarding table์ loopkup ํ์ฌ peer ๋ก ์ ์กํ interface ์ IP address ๋ฅผ source(local) IP ์ฃผ์๋ก ๊ฒฐ์ ํ๋ค .
3-way handshake Error ๊ฐ ๋ฐ์ํ ์ด์
extern int errno; /* UNIX */ GetLastError(); /* Windows */ No response to SYN: connection timeout (ETIMEDOUT)
๋ค์ 3 ๋ฒ retry ํ , ์ด 75 ์ด ์ง๋๋ฉด error return Response is RST: no such server process (ECONNREFUSED) Receive ICMP unreachable error message: host unreachable
(EHOSTUNREACH or ENETUNREACH) connection ์ด ์คํจํ๋ฉด , socket ์ ๋ ์ด์ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก
close() ํด์ผ ํ๋ค .
field local foreign
addr 206.168.112.219 12.106.32.254
port 1500 13
TCP socket - client
Send: Put the message (of length len in buf) into the TCP(socket) send buffer
Blocked if not enough space in TCP send buffer
Recv: Get a message(<= bufsize) from the TCP(socket) receive buffer
Blocked until TCP receive buffer becomes non-empty
n-byte message ๋ฅผ send()ํ๋ค๊ณ ํด์ , ์์ ์ธก์์ recv() ํ ๋ n-byte ๋ฅผ ๋ฐ๋ ๊ฒ ์๋๋ค .
send(), recv(): TCP Data Transmissionsend(), recv(): TCP Data Transmission
field local foreign
addr 206.168.112.219 12.106.32.254
port 1500 13
TCP socket - client
field local foreign
addr 12.106.32.254 206.168.112.219
port 13 1500
TCP connected socket - server
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* 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 (>0), 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
bind(): Set My Addressbind(): Set My Address
If port # = 0, the kernel chooses a port (ephemeral port) If wildcard address(INADDR_ANY), the kernel chooses IP addr
Example: IPv4struct sockaddr_in servaddr;sockfd = socket(AF_INET, SOCK_STREAM, 0);bzero(&servaddr, sizeof(servaddr));seraddr.sin_family = AF_INET;seraddr.sin_addr.s_addr = htonl(INADDR_ANY);/* wild card */seraddr.sin_port = htons(13); /* Daytime server */bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
Example: IPv6struct sockaddr_in6 servaddr;seraddr.sin6_addr.s_addr =in6addr_any; /* wild card */
TCP listening socket - serverfield local foreign
addr * (0) ?
port 13 ?
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int bind (SOCKET s, const struct sockaddr *myaddr, int addrlen);Returns: 0 on success, -1 (UNIX) or SOCKET_ERROR (Windows) on error
listen(): Convert to the Listening Socketlisten(): Convert to the Listening Socket
Listening socket: connection request ๋ฅผ accept ํ ์ ์๋ socket ๋์์ ์ฌ๋ฌ ๊ฐ์ 3-way handshaking ํ ์ ์๊ฒ queue ๊ฐ ํ์ํจ Queue size ๋ backlog( ๋ณดํต 5) ๋ก ์ ์
์ฐธ๊ณ : backlog ์ ์๋ ์ค์ queue size ์ ์ผ์นํ์ง ์๊ณ , OS ๋ง๋ค ๋ค๋ฅด๋ค .
Data ๋ฅผ ์ก์์ ํ ์ ์์ TCP state transition: CLOSED LISTEN TCP listening socket - server
field local foreign
addr * (0) ?
port 13 ?
queue
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int listen (SOCKET s, int backlog);Returns: 0 on success, -1 (UNIX) or SOCKET_ERROR (Windows) on error
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */SOCKET accept (SOCKET s, struct sockaddr *cliaddr, int *addrlen);Returns: a connected socket on success, -1 (UNIX) or INVALID_SOCKET (Windows) on failure
accept(): Accept a Connection Request accept(): Accept a Connection Request and create the Connected Socketand create the Connected Socket
field local foreign
addr * (0) ?
port 13 ?
blocked if the completed connection queue is empty
If the identity of client is not interested, use NULL pointer instead of cliaddr, addrlen
field local foreign
addr 12.106.32.254 206.168.112.219
port 13 1500
TCP listening socket - server
TCP connected socket - server
close(): close the socket and terminate the close(): close the socket and terminate the TCP connection if no more usedTCP connection if no more used
Socket send buffer ์ data ๊ฐ ๋จ์ ์์ผ๋ฉด ๋ณด๋ธ๋ค Reference_count-- ๋ง์ผ , ์ด socket ์ ๋ค๋ฅธ ํ๋ก์ธ์ค์ ๊ณต์ ํ๊ณ ์๋ค๋ฉด (if reference_count >
0), return ๋ ์ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด (if reference_count == o), ์ ์์ ์ธ TCP connection
termination (graceful termination) ์ด ์ด๋ฃจ์ด์ง๋ค . ์ฆ , FIN, ACK ๋ฅผ ๊ตํํ๋ค . Close ๋๋ฉด , ๋ ์ด์ send/recv ๋ถ๊ฐ๋ฅ
#include <unistd.h> /* UNIX */#include <winsock2.h> /* Windows */int close (SOCKET s); /* UNIX */int closesocket (SOCKET s); /* Windows */Returns: 0 on success, -1 on error
Client ๊ฐ server ์๊ฒ request ๋ฅผ ๋ชจ๋ ๋ณด๋๋ค๊ณ ์๋ฆฌ๊ณ ์ถ๋ค . ( ์ฆ , FIN์ ๋ณด๋ด๊ณ ์ถ๋ค .) ๊ทธ๋ฌ๋ , server ๋ก ๋ถํฐ์ response๋ ๊ณ์ ๋์ฐฉํ ๊ฒ์ด๊ณ , ์ด๋ฅผ recv()ํด์ผ ํ๋ค . ๋ฐฉ๋ฒ์ ?
close close versusversus shutdown shutdown Close
decrease reference count close the socket only if count == 0 terminates both directions of data
transfer: reading and writing Cannot read after close
Shutdown initiate TCPโs normal termination
regardless of reference count terminates one direction (half of
connection)
SHUT_RD (SD_RECEIVE, Windows): read-half of the connection is closed Any data currently in socket receive buffer is discarded Any data received after this call is acknowledged and then silently discarded
SHUT_WR (SD_SEND, Windows): write-half of the connection is closed(half-close) Any data currently in socket send buffer will be sent
SHUT_RDWR (SD_BOTH, Windows): read-half and write-half of connection are both closed SHUT_RD + SHUT_WR
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int shutdown (SOCKET s, int howto);Returns: 0 on success, -1 on error
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int getsockname (SOCKET s, struct sockaddr *localaddr, int *addrlen);int getpeername (SOCKET s, struct sockaddr *peeraddr, int *addrlen);Returns: 0on success, -1 on error
Finding Protocol Addresses from socketsFinding Protocol Addresses from sockets
When is getsockname function required ? client ๊ฐ connect ํ ์์ ์ IP
addr/port # ๋ฅผ ์๊ณ ์ ํ ๋ 0 ์ธ port # ๋ก bind ํ ํ assign
๋ local port # ๋ฅผ ์๊ณ ์ ํ ๋ socket ์ address family ๋ฅผ
์๊ณ ์ ํ ๋ server ๊ฐ wildcard IP addr ๋ฅผ
bind ํ์ฌ connection ์ด ์ด๋ฃจ์ด์ง ํ (accept ๊ฐ return ๋ ํ ) local IP addr ๋ฅผ ์๊ณ ์ ํ ๋
When is getpeername function required ? server ๊ฐ accept ํ child
process ๊ฐ fork ๋๊ณ exec๋๋ฉด client ๋ฅผ ์ ์ ์์ (์ , inetd)
field local foreign
addr 206.168.112.219 12.106.32.254
port 1500 13
#include <sys/socket.h> /* UNIX */#include <winsock2.h> /* Windows */int recvfrom (SOCKET s, void *buf, size_t bufsize, int flags,
struct sockaddr *from, int *fromlen);int sendto (SOCKET s, const void *buf, size_t len, int flags,
const struct sockaddr *to, int tolen);Returns: datagram length on success, -1 on failure
UDP Datagram TransmissionUDP Datagram Transmission
No connection is needed. Recvfrom: ๋ฉ์์ง๋ฅผ recv ํ๊ณ ์ก์ ์ (from) ํ์ธ
If 0 returns : datagram of length 0 (???) If no interest in senders address
from : NULL, addrlen : NULL Recv ํ ๋ ๋ง๋ค ์ก์ ์๊ฐ ๋ค๋ฅผ ์ ์๋ค .
Sendto: ๋ฉ์์ง๋ฅผ ์์ ์ (to) ์๊ฒ send Send ํ ๋ ๋ง๋ค ์์ ์๊ฐ ๋ค๋ฅผ ์ ์๋ค .
May Lost via UDP Socket Comm.May Lost via UDP Socket Comm. Lost datagram due to
lost in network socket receive buffer overflow
UDP has no flow control
Lost ๋์ง ์์๋ค๋ฉด , UDP ๋ฅผ ํตํ ์ก์์ ์ message ๊ฒฝ๊ณ๊ฐ ์ ์ง๋๋ค . ์ก์ ํ ๋ฉ์์ง๋ ํฌ๊ธฐ๊ฐ ์ ์ง๋ ์ฑ๋ก ์์ ๋๋ค . ์์ธ์ ์ผ๋ก , ์์ ๋ ๋ฉ์ง์ง๊ฐ ์งค๋ฆด ์ ์๋ค .
(truncated) ์์ ๋ ๋ฉ์์ง์ ๋นํธ์๋ฌ๊ฐ ์์ ์ ์๋ค .
Connected UDP SocketConnected UDP Socket Call connect only to communication with exactly one peer
Kernel just records IP address and port # of the peer Connected UDP socket
No need to specify the destination IP addr and port # for output operation write, send instead of sendto
No need to verify received response read, recv instead of recvfrom
Asynchronous errors are returned Connected UDP socket provides better performance
Unconnected UDP socket: make a temporary connection(1/3 overhead) May connect multiple times for a UDP socket by specifying a new IP
addr and port #
ํ๋์ ์ง์ ๋ ์๋์ UDP ํต์ ํ ๋๋TCP ์ฒ๋ผ connect() ํ์ฌ send(), recv()ํ๋ ํธ์ด ์ข๋ค .
Miscellaneous FunctionsMiscellaneous Functions
Value-Result ArgumentsValue-Result Arguments
SA structure are always passed by reference
process to kernel: bind, connect, sendto
kernel to process: accept, recvfrom, getsockname,getpeername
struct sockaddrstruct sockaddr
Byte OrderingByte Ordering
Host Byte Order Network Byte Order
ํ๋กํ ์ฝ์์ ๊ทธ๋๋ก header ์ ์ฃ์ ์ ์๋๋กNetwork address ์ port ๋ ํญ์ network byte order ๋ก ์ ์ฅ๋์ด์ผ ํ๋ค .๊ทธ๋ฌ๋ , ์ปดํจํฐ ์ข ๋ฅ์ ๋ฐ๋ผ byte ์ ์ฅ ์์๊ฐ ๋ค๋ฅผ ์ ์๋ค .
0000 0001 0000 0010
0000 0001 0000 0010
un.c[0]
un.c[0]
un.c[1]
un.c[1]
Byte Manipulation FunctionsByte Manipulation Functions
4.3BSD
ANSI C: a = b; style !!
IPv4 Address ConversionIPv4 Address Conversion Functions only for IPv4, not for IPv6 Dotted decimal(a string) 32-bit IP address
What about 255.255.255.255?
32-bit IP address Dotted decimal
Not reentrant, not thread-safe Function ๋ด๋ถ์์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๊ณต๊ฐ์ static memory ๋ก ์ก์์ ,
function call ๋ ๋ง๋ค ๊ฐ์ ์์ญ์ ์ฐ๊ธฐ ๋๋ฌธ
IPv4/IPv6 Address ConversionIPv4/IPv6 Address Conversion Functions for IPv6: work with both IPv6 and IPv4