40
M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

Embed Size (px)

Citation preview

Page 1: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer

Programming TCP/IP

Page 2: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

2

Some Books Andrew S Tanenbaum

Computer Networks 5th Ed, Prentice Hall, 2013. James Kurose and Keith Ross

Computer Networking 2nd Ed, Addison-Wesley, 2010. William Stallings

Data & Computer Communications 9th Ed, Prentice Hall, 2010.

Page 3: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

3

Layered Architecture

Networking software is designed in a layered fashion: start with the services offered by the underlying hardware, and add a sequence of software layers, each providing a higher (more abstract) level of service.

The services provided by high layers are implemented using services provided by low layers.

This is of course the general principal of building any complex software system.

Page 4: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

4

Layered Architecture

Application programs

Process-to-Process channels

Host-to-host connectivity

Hardware

Request/

reply

Message stream

L3

L2

L1

L0

Application programs

Host-to-host connectivity

Hardware

Page 5: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

5

Layered Architecture

A given layer implements one (or more) protocol. Each protocol defines two interfaces: Service interface: defines operations provided by this protocol for layer L

i+1

Peer-to-peer interface: defines messages exchanged with peer (between Li)

Note: entities at the same level are called peer entities

Li+1

Li

Protocol

Peer-to-peer interface

Peer-to-peer communication

Service interface

Protocol

Page 6: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

6

Layered Architecture

At the hardware level peer-to-peer communication is directly over a link

At higher level (say Li), L

i to L

i communication is only conceptual; in reality

it happens by each Li making use of the services of L

i-1. A technique known

as encapsulation is used for using lower level services:

Application program

data

RRP, data

RRP

HHP

RRP

Application program

HHP

data

HHP RRP Data

RRP, data

Page 7: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

7

Layered Architecture

Let us consider how application on host 1 sends a message to an application on host 2 (in practice this would be equivalent to the application making a procedure call to Request Reply Protocol software module): RRP attaches some control information (RRP specific header

information) to data so that its peer RRP will know what to do when the data is received by it. This combined message is sent to local Host-to-Host protocol.

HHP attaches HHP specific header, and the entire message is sent to host 2

So each layer attaches a header (encapsulates the message) as the message ‘goes down’.

At host 2, each layer removes its header, performs header specific processing and passes the message ‘up’.

Page 8: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

8

Services and Protocols

Services: A set of primitives provided to the layer above (Li+1). A definition visible to the user layer.

Protocols: A set of rules governing the format and meaning of the frames, packets

or messages exchanged by peer entities. An implementation hidden from the user layer.

Page 9: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

9

ISO Model Layers

Application

Presentation

Session

Transport

Network

Data Link

Physical

Layer 7

Layer 6

Layer 5

Layer 4

Layer 3

Layer 2

Layer 1

Page 10: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

10

ISO Model Overview

1. Physical Layer: Network hardware Mechanical and electrical connections.

2. Data Link Layer: Managed the transmission of data across the physical network Framing, data transparency and error control.

3. Network Layer: These protocols define how addresses are assigned and how data is

forwarded from one network to another Routing

4. Transport Layer: Provides reliable, transparent transfer of data between end points. End to end Error recovery and flow control

Page 11: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

11

ISO Model Overview (ctd)

5. Session Layer: Provides the control structure for communication between applications. Establishing, manages and terminates dialogues between application

entities. Specification for security details

6. Presentation Layer: Provides independence to the application process from differences in

data representation

7. Application Layer: Each protocol specifies how a particular application uses the network. The protocol specifies how an application program on one computer

makes a request and how the application on another machine responds

Page 12: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

12

Services Provided by Link to Network Layer Unacknowledged connectionless service

No attempt (in data link layer) to recover lost frames Okay for low error rates Used by many LANs

Acknowledged connectionless service If not acknowledged a frame can be sent again

Lost acknowledge => frame sent more than once Connection-oriented service

Reliable bit stream Frames numbered:

All frames received once in correct order Three phases:

Connect (variables and counters initialized) One or more frames transmitted Release (resources freed)

Page 13: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

13

Network Layer

Overview The network layer has to get frames all the way from source to destination,

possibly over more than one link Design goals:

Services should be independent from subnet technologies Transport layer should not have to know about number, type and topology of

subnets Transport layer network addresses should be standardized

Routing is the main function of the network layer

Types of Service: Connectionless:

Complexity in transport layer (the host) The Internet

Connection oriented: (The carrier’s choice)

Page 14: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

14

Organizational Philosophies Virtual circuits

Normally used with connection oriented subnets Route established at setup time all traffic uses this route circuit terminated when connection shutdown

Datagrams A packet in an unreliable data transfer No route worked out in advance (even if connection oriented) successive packets may follow different routes

Issue Datagram Virtual Circuitcircuit setup none requiredaddressing full source & destination short vc numberroute state info none each vc needs table space

routing each packet routed independently

route for all packets chosen at startup

router failure some packet might be lost

all vcs through router fail

congestion control difficult easy if enough buffer space

Page 15: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

15

OSI v TCP/IP

Application

Presentation

Session

Transport

Network

Data Link

Physical

Application

Internet Protocol IP

Network layer of the Internet

OSI TCP/IP

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol)

Page 16: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

16

TCP / IP Background

TCP/IP (Transmission Control Protocol / Internet Protocol) is the Internet’s communications standard

It is a complete suite of protocols and includes network, transport and application layers.

It is used in many Unix systems and explains why Unix is used widely throughout the Internet. Also, Unix is a sophisticated multi-tasking operating system.

It is now universal. It is the protocol of the Internet. Systems which originally used proprietary protocols for communications

such as Novell Netware (IPX/SPX) and Microsoft Windows (Netbeui) can now also use TCP/IP.

Page 17: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

17

TCP/IP Protocol Suite

TCP/IP components:

IP

Network Access

TCP UDP

ARP / RARP

SMTP Telnet FTP Other Applications SNMPApplications

Transport

Network

Data Link +Physical

Page 18: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

18

TCP/IP Applications FTP

File Transfer Protocol. Telnet

Remote terminal access. SMTP

Simple Mail Transfer Protocol. SNMP

Simple Network Management Protocol. DNS

Domain Name Service. NNTP

Network News Transfer Protocol. HTTP

HyperText Transfer Protocol.

Page 19: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

19

TCP and UDP Layers TCP

Transmission Control Protocol. A reliable connection-oriented protocol

Reliable - Error free, with no losses or duplicates and in the same order as it was submitted

Fragments an incoming byte stream into messages for IP layer. Reassembles received messages to create an output stream Manages flow control Equivalent to OSI layer 4 (transport)

UDP User Datagram Protocol An unreliable connectionless protocol Data sent and delivered as messages No sequencing or flow control Low overhead Equivalent to OSI layer 4 (transport)

Page 20: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

20

TCP & UDP Segments

TCP segment:

UDP segment:

32 bits

Source port Destination port

Sequence number

Acknowledgment Number

Header length & Flags Window size

Checksum Urgent pointer

Options (0 or more)

Data (optional with padding)

32 bits

Source port Destination port

UDP length UDP checksum

Data (optional with padding)

Page 21: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

21

TCP Port Numbers

Ports are used in TCP to name the ends of logical connections that carry conversations.

For the purpose of providing services to unknown callers, well-known ports are defined. For example: 21 FTP 23 Telnet 25 SMTP 161 SNMP

Page 22: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

22

IP and ARP Layers IP

Internet Protocol. An unreliable connectionless best effort IP packet delivery service. Message fragmentation and reassembly. Routing. Equivalent to OSI layer 3 (network).

ARP / RARP (Reverse) Address Resolution Protocol Maps IP addresses onto data link layer addresses such as Ethernet card

addresses: 193.63.32.233 to 008002B39DD10

Its functions are defined as part of TCP/IP but its implementation is dependent on the network type.

TCP/IP does not define what happens below the IP layer.

Page 23: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

23

IP Datagram and TCP/IP Packet Format

IP datagram format:

TCP/IP packet structure:

32 bits

Total length

Identification

Options (0 or more)

Header checksum

Source IP Address

Destination IP address

Data (optional with padding)

Type of serviceVersion HLen

ProtocolTime to live

Fragment offset & flags

Application DataTCP/UDP Header

IP HeaderLAN Header LAN Trailer

Page 24: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

24

IP Address structure IP uses unique 32 bit addresses to specify sources and target hosts on an

Internet. IP addresses are normally represented in dotted decimal format, with each

block describing 8 bits, so these are the same: 193.63.32.233 11000001 00111111 00100000 11101001

Each IP address has two components: A network (or subnet) prefix – the higher bits. A host identifier identifying an individual host or interface to a router, within a

network. Hosts on the same network must have IP addresses with the same prefix, but

different host Ids. Hosts with different network prefixes might be connected by a router (eg, with two

interfaces with network prefixes agreeing with the two hosts.

Page 25: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

25

IP Address structure Originally the network ID had to be 8 or 16 or 24 bytes (class A, B, C

addresses) This classful addressing became obsolete as it was not flexible, scalable enough There are only 214 = 16384 class B addresses (starting 10...) – to few for an ISP to proved;

but if the ISP provides class C addresses, these only allow 256 hosts: not enough for an organisation.

The more modern approach: CIDR with VLSM Classless Inter-domain Routing VLMS = variable length subnet masking. A range of IP addresses (as supplied by an ISP, for example) is denoted a.b.c.d/x in dotted-

decimal notation: a, b, c,d are integers 0-255 and x is the number of bits forming the network prefix.

Thus eg 192.168.100.0/24 denotes a range of addresses with the 24-bit network prefix 192.168.100 (= 11000000 10101000 01100100) – like the old class C, able to accommodate 256 hosts.

An old class B address became a.b.0.0/16; an old class A address a.0.0.0/8 But in-between options are possible...

Page 26: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

26

IP Address structure

An ISP might provide the range 223.1.240.0/20 NB 240 => 20 bits' network prefix followed by 12 0-bits Accommodation for up to 212 = 4000 hosts

The organisation using these might split the range up into four subnets 223.1.240.0/22, 223.1.244.0/22, 223.1.248.0/22, 223.1.252.0/22 each with

1000 hosts capacity. These all agree on their top 20 bits (check: convert the numbers to hex or

binary) These four sub-networks would need to be connected by routers within the

organisation. Hosts on the same subnet must have addresses agreeing on their top 22

bits: including routers connected to the subnet.

Page 27: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

27

IP Address structure

When configuring a the IP address of a host to be a.b.c.d/x you might specify a subnet mask to express the x: Eg 223.1.252.3 within the subnet 223.1.252.0/22 woud need subnet mask

255.255.252.0 in binary, 11111111 11111111 11111100 00000000:

22 1-bits to specify the network address. Two hosts on this subnet must have the same address ANDed with the

mask.

Page 28: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

28

Domain Names

To avoid referring to individual numerical IP addresses the concept of domain and host names was developed.

Domain and host names are mapped to IP addresses. For example: cougar.unn.ac.uk maps to 193.63.32.233 There is no logical relationship between the parts of an IP address and its

domain name.

The mapping is done using Domain Name Resolution, normally with the help of a Domain Name Server. Domain name servers often have to be specified as raw IP addresses.

Because without a DNS we cannot resolve the domain name of the DNS (!) .

Page 29: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

29

Domain Name Hierarchy Top level domains:

Generic: e.g. edu or com - most of the USA. Countries: e.g. uk, nl or jp - most of the rest of the world.

Each domain controls how its subdomains are organised and allocated: To create a new domain permission is required from the domain which will

contain it. Nominet UK is responsible for all uk domain names

(http://www.nic.uk/) Naming follows organisational boundaries not physical network.

ac

ncl unn

unn.ac.uk

uk

co

durhdemon

Page 30: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

30

Domain Name Servers

Terminal User

Server Client

Network Manager

Serverprocess

OS OS

TCP/IP TCP/IP TCP/IP

Server ApplicProtocol

Client ApplicationProtocol

Resolver

User Applic

name

IP addr

Domain Name Server

domain name

IP addressIP address used with

TCP connection or UDP packet

OS

NameDBase

UDP packets

1

2

3

4

5

6

Page 31: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

31

Domain Name Server Operation

1. User issues a request to the client application protocol.

2. The client protocol passes the name to the name resolver.

3. The name resolver passes name to a domain name server.

4. The domain name server returns IP address to the name resolver.

5. The name resolver returns IP address to client.

6. The client communicates with the applications server using IP address.

Page 32: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

32

Programming TCP/IP

In particular, programming at upper levels of the TCP/IP protocol stack. Facilities provided at the TCP/UDP level for application programs to use As application programmers, we can ignore the lower levels – IP, etc These functions are intended primarily for enabling application programs

to communicate with one another over the internet… …but they are useful for developing any kind of …

Distributed Application Any application which consists of multiple programs, running as

separate processes, possibly on separate machines. Example: A graphical user interface written in Java could communicate

by TCP with a high-performance "engine" written in C. Example: A networked game: players are TCP clients, the game engine is

a server process.

Page 33: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

33

TCP and Sockets

We shall ignore the connectionless protocol UDP for now. TCP provides a virtual circuit between two programs based on a

software entity called a socket. Plays a role similar to a file-handle:

First, the socket is connected to a remote host (compare with opening a file) Then data is input/output through the socket. When complete, the socket is closed – the connection is broken.

Implementation Java offers an object-oriented implementation based on the class

java.net.Socket C provides the socket class and a library of socket functions prototyped

in <sys/socket.h>.

Page 34: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

34

Sockets in C

#include <sys/socket.h>

Functions int socket(…);

creates a new socket int gethostname(char *name, int namelen);

translates a host name to an ip address int bind(int s, struct sockaddr *name, int namelen);

binds a socket to a specific address (and port) int connect(int s, struct sockaddr *addr, int *addrlen);

used by client to request a socket connection to a remote address (and port)

Page 35: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

35

Sockets in C

#include <sys/socket.h>

Functions void listen (socket_id s, int backlog );

causes server to start listening for requests for a connection int accept(int s, struct sockaddr *addr, int *addrlen);

used by server to accept a connection request from a client int read(int d, char *buf, int nbytes); int write(int d, char *buf, int nb);

d = socket id; reads from/writes to a socket int close(int d);

close socket (d = socket id)

Page 36: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

36

TCP Server Logic

Fix the port number Create a socket for the server Start listening on the socket for requests to connect Repeat –

Wait for a request for a connection. Accept function returns id of a new socket which will manage the

connection; also gets the name of the client host. Spin off a new process or thread to serve the client If a new process was created with fork, the parent will

close its copy of the new socket before going back to listening on the old sock for a connection request;

the child process will close the “spare copy of” the old socket before commencing communication with client using new socket

Page 37: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

37

TCP Server Logic

Serving the client A subroutine running in a new thread or process Uses socket returned by Accept function Uses socket I/O functions to send data to / receive data from client

according to protocol When finished, closes the socket.

Page 38: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

38

TCP Client Logic

Specify server name and port number to which we wish to connect

Create a socket Bind this socket to host name and port number Request a connection to the server host / port

The port number is same at both ends: it identifies the virtual circuit between the client and the server.

(Assuming return value indicates connection successful) use socket I/O functions to send data to/receive data from the server According to protocol

Close the socket when finished.

Page 39: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

39

Nagle’s Algorithm and TCP_NODELAY

Nagel’s algorithm (RFC896) An algorithm for coalescing a number of small outgoing messages, and sending

them all at once. As long as there is a sent packet for which the sender has received no

acknowledgment, the sender should keep buffering its output until it has a full packet's worth of output, so that output can be sent all at once.

Interacts badly with TCP delayed acknowledgments, introduced into TCP in the early 1980s, but by a different group. With both algorithms enabled, applications which do two successive writes to a TCP connection, followed by a read, experience a constant delay of up to 500 milliseconds.

TCP implementations provide an interface to disable the Nagle algorithm, typically called the TCP_NODELAY option. Use this if your application needs to send small packets very frequently.

Page 40: M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer Programming TCP/IP

40

Setting TCP_NODELAY

In C (UNIX, Winsock) int flag = 1; int result = setsockopt(sock, // socket affected IPPROTO_TCP, // set option at TCP level TCP_NODELAY, // name of option

(char *) &flag, sizeof(int)); // length of option value if (result < 0) ... handle the error ...

In Java sock.setTcpNoDelay(true);

Reference http://en.wikipedia.org/wiki/Nagle%27s_algorithm