Lecture 2 Overview - University of Otago · • Local socket address: local IP address and port number! • Remote socket address: only for TCP sockets! • Protocol: TCP, UDP! TELE402

Post on 21-May-2020

6 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Lecture 2 Overview!•  This Lecture!–  Protocol layering!–  Source: Comer’s book (Chapter 10)!– TCP and UDP!–  Source: Chapter 2 of Stevens� book!–  Sockets introduction!–  Source: Chapter 3 of Stevens� book!

•  Next Lecture!– Elementary TCP sockets!–  Source: Chapter 4 of Stevens� book!– TCP Client-Server example!–  Source: Chapter 5 of Stevens� book!

TELE402 2012 Lecture 2 Introduction of TCP/IP 1

Protocol Layering (1)!•  Why do we need communication protocols?!!

!Allow one to specify or understand communication without knowing the ! details of the network hardware!

•  What problems might arise when machines communicate over a network?!–  Hardware failure –  Network congestion –  Packet delay or loss –  Data corruption –  Data duplication or inverted arrivals

TELE402 2012 Lecture 2 Introduction of TCP/IP 2

Protocol Layering (2)!•  Is it possible to design a single protocol which handles

all problems occurred during data communication? ! might be possible, but very difficult !•  Layered design approach!–  Not new to communication protocol design

TELE402 2012 Lecture 2 Introduction of TCP/IP 3

Computer system!

hardware'

operating system!

application software!

Programming languages!

Unstructured (COBOL) !

structured (C) !

Object-oriented (C++)!

Component-based (NesC)!

Layered Models for Communication Systems !

TELE402 2012 Lecture 2 Introduction of TCP/IP 4

OSI Model!

Physical'

Presenta0on'

Session'

Transport'

Network'

Data'Link'

Applica0on'

TCP/IP Model!

Hardware'

Transport'

Internet'

Network'Interface'

Applica0on'

The Protocol Layering Principle!

!

!

•  Pros and Cons!–  Modularity, simplicity, interoperability, robustness, security,

cost effective –  Complexity, process time, memory usage, prevention from

optimization

TELE402 2012 Lecture 2 Introduction of TCP/IP 5

Layered protocols are designed so that layer n at the destination receives exactly the same object sent by layer n at the source

Multiplexing/Demultiplexing!•  Multiplexing and demultiplexing occur at almost every

protocol layer!

•  Example: multiplexing and demultiplexing at the transport layer

TELE402 2012 Lecture 2 Introduction of TCP/IP 6

multiplexer! demultiplexer!

IP! IP!

processes! processes!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 7!

The TCP/IP Protocol Suit! (The Internet Protocol Suite)!

Example: Client and Server (1)!•  Simple model!– One server, multiple clients!

•  How to make applications robust?!

TELE402 2012 Lecture 2 Introduction of TCP/IP 8

client' server'link!

protocol!

client'

server'client'

client'

…!

…!

Example: Client and Server (2)!•  Local Area Network (LAN) Scenario!

TELE402 2012 Lecture 2 Introduction of TCP/IP 9

Example: Client and Server (3)!•  Wide Area Network (WAN) Scenario!

TELE402 2012 Lecture 2 Introduction of TCP/IP 10

How to Develop the Program?!•  Socket Programming!•  What is a socket?!

–  Sockets represent endpoints in a line of communication.!–  A socket is a software component characterized by a unique

combination of !•  Local socket address: local IP address and port number!•  Remote socket address: only for TCP sockets!•  Protocol: TCP, UDP!

TELE402 2012 Lecture 2 Introduction of TCP/IP 11

telephone'

network'

Without the telephone network, each endpoint of a telephone line is nothing more than a plastic box.!

Socket Programming!

TELE402 2012 Lecture 2 Introduction of TCP/IP 12

•  Socket Address: the combination of an IP address and a port number (a 16-bit unsigned integer, ranging from 0 to 65535).!

•  Socket API: an application programming interface, usually provided by the operating system.

A Simple TCP Daytime Client!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 13!

�include "unp.h”! int! main(int argc, char **argv)! {! int sockfd, n;! struct sockaddr_in6 servaddr;! char recvline[MAXLINE + 1];! if (argc != 2)! err_quit("usage: a.out <IPaddress>");! if ( (sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) err_sys("socket error");! bzero(&servaddr, sizeof(servaddr));! servaddr.sin6_family = AF_INET6;! servaddr.sin6_port = htons(13);/* daytime server */! if (inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr) <= 0)! err_quit("inet_pton error for %s", argv[1]);! if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error");! while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {! recvline[n] = 0; /* null terminate */! if (fputs(recvline, stdout) == EOF)! err_sys("fputs error");! }! if (n < 0) err_sys("read error");! exit(0);!}!!!

Create'a'TCP'socket'(socket)'

Specify server’s IP address and port !

Connect to the server (connect)!

Send request or receive reply!(send & recv)!

Terminate program (close socket) !

A Simple TCP Server!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 14!

�include"unp.h"!#include<time.h>!int main(int argc, char **argv)!{! intlistenfd, connfd;! struct sockaddr_inservaddr;! charbuff[MAXLINE];! time_tticks;! listenfd = Socket(AF_INET, SOCK_STREAM, 0);! bzero(&servaddr, sizeof(servaddr));! servaddr.sin_family= AF_INET;! servaddr.sin_addr.s_addr = htonl(INADDR_ANY);! servaddr.sin_port= htons(13); /* daytime server */!! Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));! Listen(listenfd, LISTENQ);! for ( ; ; ) {! connfd = Accept(listenfd, (SA *) NULL, NULL);! ticks = time(NULL);! snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));! Write(connfd, buff, strlen(buff));! Close(connfd);! }!}!!!

Create'a'TCP'socket'(socket)'

Specify server’s IP address and port !

Bind socket with local port (Bind)!

Receive or reply (send & recv)!Terminate connection (Close) !

Convert the socket to listening socket (Listen)!

Accept client connection (Accept)!

Discovering Details of Your Local Network!

TELE402 2012 Lecture 2 Introduction of TCP/IP 15

•  To find out interfaces: netstat -ni •  To find out routing table: netstat -rn •  To find out details of an interface: ifconfig •  To discover hosts on a LAN: ping

TCP/IP vs OSI!

TELE402 2012 Lecture 2 Introduction of TCP/IP 16

Unix standards!•  Unix has different standards, which have different

networking APIs!•  POSIX!–  Portable Operating System Interface!–  Developed by IEEE!–  Linux follows this standard!

•  BSD!–  Berkeley Software Distribution!–  Socket API originated from BSD!

•  We won’t pay much attention to the differences, but will mention them when necessary.!

TELE402 2012 Lecture 2 Introduction of TCP/IP 17

TCP/IP Protocol Suite!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 18!

IP Packet Format !

TELE402 2012! Lecture 2 Introduction of TCP/IP! 19!

User Datagram Protocol (UDP)!•  A simple transport-layer protocol!–  RFC 786!

•  UDP datagram is encapsulated into an IP packet!•  A connection-less service!•  No reliability guaranteed!–  Datagram may get lost!–  Once delivered to applications, data in the datagram is error-

free (Why?)!•  UDP data can be considered as records which have

boundaries according to datagram!

TELE402 2012 Lecture 2 Introduction of TCP/IP 20

Format of UDP messages!•  Each UDP message is called a user datagram

•  Why does UDP have checksum in addition to IP checksum?!

•  The UDP checksum is optional; a value of zero means the checksum is not computed. !

•  Why is UDP created in addition to IP?!

!!

TELE402 2012 Lecture 2 Introduction of TCP/IP 21

UDP'SOURCE'PORT' UDP'DESTINATION'PORT'

UDP'MESSAGE'LENGTH' UDP'CHECKSUM'

DATA'

…'

0! 16! 31!

Header!

UDP Pseudo-Header!

•  UDP checksum covers both the pseudo-header and the UPD datagram. Why?!

•  The PROTO field contains the IP protocol type code (17 for UDP)!•  The ZERO field is an octet of zeros for padding, not transmitted.!•  Name registration for UDP ports!–  Use program name id to find the local port?!–  Use process id to replace the local port?!

TELE402 2012 Lecture 2 Introduction of TCP/IP 22

ZERO' UDP'CHECKSUM'

SOURCE'IP'ADDRESS'

0! 16! 31!

DESTINATION'IP'ADDRESS'

PROTO'

Reliable Delivery Service!•  Stream oriented!–  In order delivery!

•  Virtual circuit connection!– Both parties should be ready and responsive to loss of

data!

•  Buffered transfer!–  For retransmission!

•  Unstructured stream!•  Full duplex!•  Acknowledgment and retransmission!

TELE402 2012 Lecture 2 Introduction of TCP/IP 23

Transmission Control Protocol (TCP)!•  RFC 793!•  A connection-oriented service!–  A connection is established before exchanging data!

•  Reliability is guaranteed (?)!–  Acknowledgment is used to confirm the reception of data;

Round Trip Time (RTT) is used for retransmission!–  Data are sequenced byte by byte!

•  Flow control is used to adapt to the speed difference between the sender and receiver.!–  The sliding window mechanism !

•  A TCP connection is full-duplex just like UDP!

TELE402 2012 Lecture 2 Introduction of TCP/IP 24

Connection establishment!•  Three-way handshake!

TELE402 2012 Lecture 2 Introduction of TCP/IP 25

TCP options!•  MSS option: Maximum Segment Size!–  With this option the TCP sending the SYN announces the

maximum amount of data that it is willing to accept in each TCP segment!

•  Window scale option!–  The maximum window that either TCP can advertise to the

other TCP is 65535 (16 bits for window size)!•  Timestamp option!–  New option needed for high-speed connections to prevent

possible data corruption caused by lost packets that then reappear. No worries for network programmers.!

TELE402 2012 Lecture 2 Introduction of TCP/IP 26

Connection termination!

TELE402 2012 Lecture 2 Introduction of TCP/IP 27

TCP state transition!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 28!

Packet exchange!

TELE402 2012! Lecture 2 Introduction of TCP/IP! 29!

TIME_WAIT state!•  Why need TIME_WAIT state?!–  To implement TCP’s full-duplex connection termination

reliably!–  To allow old duplicate segments to expire in the network!

•  The time to remain in this state is 2*MSL!–  MSL is Maximum Segment Lifetime (the maximum amount

of time that any given IP datagram can live in an Internet)!–  The recommended value for MSL is 2 minutes in RFC 1122,

though BSD used a value of 30 seconds!–  So the time for TIME_WAIT state is between 1 and 4

minutes!

TELE402 2012 Lecture 2 Introduction of TCP/IP 30

Port numbers!•  Well-known ports!–  0-1024!–  Controlled and assigned by IANA (Internet Assigned

Number Authority)!•  Registered ports!–  1024-49151!–  Not controlled by IANA, but IANA registers and lists the

uses of these ports as a convenience to the community!•  Dynamic (or private) ports!–  49152-65535, also called ephemeral ports!

•  Reserved (privileged) ports in Unix, 0-1024!

TELE402 2012 Lecture 2 Introduction of TCP/IP 31

Concurrent servers and port!•  Socket pair!–  A 4-tuple for a TCP connection, which uniquely identifies

the TCP connection!–  local IP address, local TCP port, foreign IP address, and

foreign TCP port!

TELE402 2012 Lecture 2 Introduction of TCP/IP 32

Concurrent servers and port (cont.)!

TELE402 2012 Lecture 2 Introduction of TCP/IP 33

Concurrent servers and port (cont.)!

TELE402 2012 Lecture 2 Introduction of TCP/IP 34

IP Limitations!•  Maximum size of IP datagrams!–  IPv4, 65535 bytes, including IPv4 header!–  IPv6, 65575 bytes, including the 40-byte IPv6 header!

•  Many networks have an MTU (Maximum Transmission Unit)!–  Ethernet 1500 bytes!

•  Path MTU!–  the smallest MTU in the path between two hosts!

•  Fragmentation is needed if an IP datagram exceeds the link MTU!

TELE402 2012 Lecture 2 Introduction of TCP/IP 35

IP Limitations (cont.)!•  The DF (Don’t fragment) bit can be set for non-

fragmentation!•  Minimum reassembly buffer size!–  the minimum datagram size that we are guaranteed any

implementation must support!–  IPv4, 576 bytes; IPv6, 1500 bytes!

•  Maximum Segment Size (MSS) for TCP!–  Announce to the peer TCP the maximum amount of TCP data

that the peer can send per segment!–  MSS is normally set to the interface MTU minus the fixed

sizes of the IP and TCP headers!

TELE402 2012 Lecture 2 Introduction of TCP/IP 36

TCP Output!

TELE402 2012 Lecture 2 Introduction of TCP/IP 37

UDP Output!

TELE402 2012 Lecture 2 Introduction of TCP/IP 38

Socket Introduction!•  Socket API is the popular fundamental

networking API in Unix/Linux!•  BSD socket released in 1983.!•  POSIX socket API!–  POSIX.1g, approved in 2000.!

TELE402 2012 Lecture 2 Introduction of TCP/IP 39

Socket Address Structure!

•  Posix.1g definition!

TELE402 2012 Lecture 2 Introduction of TCP/IP 40

Posix.1g Data Types!

TELE402 2012 Lecture 2 Introduction of TCP/IP 41

Generic Socket Address!•  Generic socket address structure is proposed to deal

with addresses of supported protocol families!–  Defined in the <sys/socket.h> header!

TELE402 2012 Lecture 2 Introduction of TCP/IP 42

Using the generic socket address!

Function prototype:! int bind(int, struct sockaddr *, socklen_t)!!Using bind:! struct sockaddr_in serv;! /* fill in serv{} */! bind(sockdf, (struct sockaddr *) &serv, sizeof(serv));!

TELE402 2012 Lecture 2 Introduction of TCP/IP 43

IPv6 Socket Address!

TELE402 2012 Lecture 2 Introduction of TCP/IP 44

Various socket addresses!

TELE402 2012 Lecture 2 Introduction of TCP/IP 45

Value-Result Arguments!

•  connect(sockfd, &serv, sizeof(serv));!

TELE402 2012 Lecture 2 Introduction of TCP/IP 46

Value-Result arguments (cont.)!

•  accept(sockfd, &from, &len);!

TELE402 2012 Lecture 2 Introduction of TCP/IP 47

Byte order!•  Little-endian and big-endian!

TELE402 2012 Lecture 2 Introduction of TCP/IP 48

Byte ordering functions!

•  uint16_t htons(uint16_t host16bitvalue);!•  uint32_t htonl(uint32_t host32bitvalue);!•  uint16_t ntohs(uint16_t net16bitvalue);!•  uint32_t ntohl(uint32_t net32bitvalue);!

TELE402 2012 Lecture 2 Introduction of TCP/IP 49

Byte manipulation functions!

•  Berkeley-derived!–  void bzero(void *dest, size_t nbytes);!–  void bcopy(const void *src, void *dest, !! !size_t nbytes);!–  int bcmp(const void &ptr1, const void *ptr2, !! !size_t nbytes);!

TELE402 2012 Lecture 2 Introduction of TCP/IP 50

Byte manipulation functions (cont)!•  ANSI C!–  void *memset(void *dest, int c, size_t len);!–  void *memcpy(void *dest, const void *src, !! !size_t nbytes);!–  int memcmp(const void *ptr1, const void *ptr2, !! !size_t nbytes);!

TELE402 2012 Lecture 2 Introduction of TCP/IP 51

Address conversion functions!

•  Traditional functions!–  int inet_aton(const char *strptr, !! !struct in_addr *addrptr);!–  char *inet_ntoa(struct in_addr inaddr);!

–  inet_addr (deprecated)!

TELE402 2012 Lecture 2 Introduction of TCP/IP 52

Address conversion functions (continued)!

•  int inet_pton(int family, const char *strptr,!!!void *addrptr);!•  const char *inet_ntop(int family, !!!const void *addrptr, !!!char *strptr, size_t len);!•  #define INET_ADDRSTRLEN 16!•  #define INET6_ADDRSTRLEN 46!

TELE402 2012 Lecture 2 Introduction of TCP/IP 53

Address conversion functions!•  How to make functions independent of protocols?!

TELE402 2012 Lecture 2 Introduction of TCP/IP 54

More functions!

TELE402 2012 Lecture 2 Introduction of TCP/IP 55

More functions (cont.)!

TELE402 2012 Lecture 2 Introduction of TCP/IP 56

Questions!•  What is the Protocol Layering Principle? What are the

advantages and disadvantages of protocol layering?!•  What defines a TCP connection?!•  What is the difference between TCP segment size and TCP

window size?!•  How can the delay, bandwidth, load, and packet loss affect TCP

retransmission?!•  Explain the relationship between RTT, bandwidth, and window

size. !•  What are passive open connection and active open connection?!

TELE402 2012 Lecture 2 Introduction of TCP/IP 57

top related