Download NwksTCPIP

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Piggybacking (Internet access) wikipedia , lookup

SIP extensions for the IP Multimedia Subsystem wikipedia , lookup

Net bias wikipedia , lookup

CAN bus wikipedia , lookup

Lag wikipedia , lookup

Distributed firewall wikipedia , lookup

Asynchronous Transfer Mode wikipedia , lookup

Network tap wikipedia , lookup

Remote Desktop Services wikipedia , lookup

AppleTalk wikipedia , lookup

Airborne Networking wikipedia , lookup

Computer network wikipedia , lookup

Wake-on-LAN wikipedia , lookup

IEEE 1355 wikipedia , lookup

Deep packet inspection wikipedia , lookup

TCP congestion control wikipedia , lookup

Real-Time Messaging Protocol wikipedia , lookup

Cracking of wireless networks wikipedia , lookup

Zero-configuration networking wikipedia , lookup

UniPro protocol stack wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Internet protocol suite wikipedia , lookup

Transcript
Programming TCP/IP
M J Brockway; based on notes by Adrian Robson and Gerhard Fehringer
Some Books

Andrew S Tanenbaum


James Kurose and Keith Ross


Computer Networks 5th Ed, Prentice Hall, 2013.
Computer Networking 2nd Ed, Addison-Wesley, 2010.
William Stallings

Data & Computer Communications 9th Ed, Prentice Hall, 2010.
2
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.
3
Layered Architecture
Application programs
Process-to-Process channels
L3
L2
Application programs
Request/
reply
Message
stream
Host-to-host connectivity
L1
Host-to-host connectivity
Hardware
L0
Hardware
4
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 Li+1
 Peer-to-peer interface: defines messages exchanged with peer (between Li)
 Note: entities at the same level are called peer entities
Service
interface
Li+1
Li
Protocol
Protocol
Peer-to-peer
interface
Peer-to-peer
communication
5
Layered Architecture


At the hardware level peer-to-peer communication is directly over a link
At higher level (say Li), Li to Li communication is only conceptual; in reality
it happens by each Li making use of the services of Li-1. A technique known
as encapsulation is used for using lower level services:
Application
program
Application
program
data
data
RRP
RRP
RRP, data
RRP, data
HHP
HHP
HHP
RRP Data
6
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-toHost 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’.
7
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.
8
ISO Model Layers
Layer 7
Application
Layer 6
Presentation
Layer 5
Session
Layer 4
Transport
Layer 3
Network
Layer 2
Data Link
Layer 1
Physical
9
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
10
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
11
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)
12
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)
13
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
circuit setup
addressing
route state info
Datagram
none
full source & destination
none
Virtual Circuit
required
short vc number
each vc needs table space
routing
each packet routed
independently
some packet might be
lost
difficult
route for all packets chosen at
startup
all vcs through router fail
router failure
congestion control
easy if enough buffer space
14
OSI v TCP/IP
OSI
TCP/IP
Application
Presentation
Application
Transport
TCP (Transmission Control
Protocol) and UDP (User
Datagram Protocol)
Network
Internet Protocol IP
Data Link
Network layer of the
Internet
Session
Physical
15
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.
16
TCP/IP Protocol Suite

TCP/IP components:
Applications
Transport
Network
Data Link +
Physical
SMTP
Telnet
Other Applications
FTP
TCP
SNMP
UDP
ARP / RARP
IP
Network Access
17
TCP/IP Applications

FTP


Telnet


Domain Name Service.
NNTP


Simple Network Management Protocol.
DNS


Simple Mail Transfer Protocol.
SNMP


Remote terminal access.
SMTP


File Transfer Protocol.
Network News Transfer Protocol.
HTTP

HyperText Transfer Protocol.
18
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)
19
TCP & UDP Segments
TCP 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)
UDP segment:
32 bits
Source port
Destination port
UDP length
UDP checksum
Data (optional with padding)
20
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, wellknown ports are defined. For example:




21
23
25
161
FTP
Telnet
SMTP
SNMP
21
IP and ARP Layers

IP






ARP / RARP




Internet Protocol.
An unreliable connectionless best effort IP packet delivery service.
Message fragmentation and reassembly.
Routing.
Equivalent to OSI layer 3 (network).
(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.
22
IP Datagram and TCP/IP Packet Format

IP datagram format:
32 bits
Version
HLen
Type of service
Total length
Identification
Time to live
Fragment offset & flags
Protocol
Header checksum
Source IP Address
Destination IP address
Options (0 or more)
Data (optional with padding)

TCP/IP packet structure:
LAN Header
IP Header
TCP/UDP
Header
Application Data
LAN Trailer
23
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.
24
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 dotteddecimal 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...
25
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.
26
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.
27
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 (!) .
28
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.
uk
co
ac
demon
ncl
unn
durh
unn.ac.uk
29
Domain Name Servers
Network Manager
Terminal User
Server
Client
User Applic
Server
process
name
IP addr
OS
OS
1
Server
Applic
Protocol
2
5
Client
Application
Protocol
TCP/IP
OS
Domain
Name
Server
Resolver
TCP/IP
Name
DBase
TCP/IP
6
IP address used with
TCP connection or UDP packet
3
domain name
IP address
UDP packets
4
30
Domain Name Server Operation
1.
2.
3.
4.
5.
6.
User issues a request to the client application protocol.
The client protocol passes the name to the name resolver.
The name resolver passes name to a domain name server.
The domain name server returns IP address to the name resolver.
The name resolver returns IP address to client.
The client communicates with the applications server using IP
address.
31
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.
32
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>.
33
Sockets in C

#include <sys/socket.h>

Functions

int socket(…);


int gethostname(char *name, int namelen);


translates a host name to an ip address
int bind(int s, struct sockaddr *name, int
namelen);


creates a new socket
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)
34
Sockets in C

#include <sys/socket.h>

Functions

void listen (socket_id s, int backlog );


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);


causes server to start listening for requests for a connection
d = socket id; reads from/writes to a socket
int close(int d);

close socket (d = socket id)
35
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

36
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.
37
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


(Assuming return value indicates connection successful) use
socket I/O functions to send data to/receive data from the server


The port number is same at both ends: it identifies the virtual circuit
between the client and the server.
According to protocol
Close the socket when finished.
38
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.
39
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
40