structural view of networks ◦ physical layer: wire, radio, etc. ◦ data link layer: protocol...

48

Upload: susanna-cobb

Post on 12-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking
Page 2: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Structural view of networks◦ Physical layer: wire, radio, etc.◦ Data link layer: protocol principles, etc◦ Network layer: routing, internetworking◦ Transport layer: protocols (tcp), etc◦ Application layer: email, web, class project

We will use one common method at the transport/network level (TCP/IP via sockets)

application

transport

network

link

physical

Page 3: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Internetwork: a network of networks

Uses packet switching to send data between machines on this “internet”

Home network

Institutional network

Mobile network

Global ISP

Regional ISP

Page 4: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Each machine has a unique address on the network (IP address)

Internet Protocol (IP): format of packets used to create these envelops of data◦ Like mailing a multipage letter by putting each page into a separately mailed envelop

◦ May not arrive in correct order

Page 5: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

1. Hosts (e.g., computers and other devices) are mapped to a set of 32-bit* IP addresses.◦ 147.129.181.14

2. The set of IP addresses is mapped to a set of identifiers called Internet domain names.◦ 147.129.56.50 is mapped to www.ithaca.edu

3. A process on one Internet host can communicate with a process on another Internet host over a connection.

* this is IPv4. There is a new IP standard iPv6 that uses 64-bit addresses. See http://en.wikipedia.org/wiki/IPv6

Page 6: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Windows NT/2000/XP/Win 7◦ Click on start, run, type cmd and at the prompt type ipconfig

◦ Type exit to close the command prompt window.

You’ll see something like:

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :IP Address. . . . . . . . . . . . : 192.168.1.101Subnet Mask . . . . . . . . . . : 255.255.255.0Default Gateway . . . . . . . : 192.168.1.1

Page 7: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

From the Finder, under Applications -> Utilities launch Network Utility. ◦ in OS X 9.x look in /System/Library/CoreServices/Applications

◦ Select the correct ethernet interface (normally Ethernet 1 (en0)).

◦ Information and utilities are available from the tabs above.

From the Finder, choose apple->System Preferences. ◦ Then click on “Network” and in the resulting window choose “ethernet”

Or, start a terminal window. ◦ Type “ifconfig –a” ◦ Look under “en0”

Page 8: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

For Linux or Unix users, to view their IP address or network information, users must have administrator or root privileges.

1. Open the Linux or Unix shell if you are utilizing a GUI interface for your Linux or Unix machine.

2. From the prompt, type ifconfig eth0 and press enter. You’ll see:

eth0 Link encap:Ethernet HWaddr 00:A0:24:72:EB:0A inet addr:10.10.10.2 Bcast:10.0.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5569799 errors:32 dropped:32 overruns:0 frame:6 TX packets:3548292 errors:0 dropped:0 overruns:0 carrier:3 Collisions:14 Interrupt:18 Base address:0xda00

As seen from the above example, users will commonly see the network settings for all their network devices when running the "ifconfig" command. First in the above example we have the network settings for the "lo" or "local loopback", next is the actual network settings of your network adapter.

Page 9: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

ping◦ “a computer network administration utility used to test the reachability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer”

Open a terminal window (Mac) or do run->cmd (Windows)

A228536:~ barr$ ping www.ithaca.edu

PING www.ithaca.edu (147.129.56.50): 56 data bytes

64 bytes from 147.129.56.50: icmp_seq=0 ttl=62 time=1.236 ms

64 bytes from 147.129.56.50: icmp_seq=1 ttl=62 time=1.269 ms

64 bytes from 147.129.56.50: icmp_seq=2 ttl=62 time=1.309 ms

Page 10: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

The whois utility looks up records in the databases maintained by several Network Information Centers (NICs).

whois -h hostname domainName e.g. whois ithaca.eduOr, if you know of a whois server like whois.educause.net (stores edu

names):

whois -h whois.educause.net ithaca.edu

Page 11: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Barr$ whois ithaca.edu

Domain Name: ITHACA.EDU

Registrant:

Ithaca College

Network & Communication Services

110 Phillips Hall

Ithaca, NY 14850-7028

UNITED STATES

Administrative Contact:

Bernie Rhoades

Director, Network & Communication Services

Ithaca College

Information Technology Services

110 Phillips Hall

Ithaca, NY 14850-7028

UNITED STATES

(607) 274-1694

[email protected]

Technical Contact:

William W. Weeks

Network Services Manager

Ithaca College

Information Technology Services 110 Phillips Hall Ithaca, NY 14850-7028 UNITED STATES (607) 274-1311 [email protected]

Name Servers: RESOLVER1.ITHACA.EDU 147.129.1.2 RESOLVER2.ITHACA.EDU 147.129.94.4 RESOLVER3.ITHACA.EDU 208.81.240.2

Domain record activated: 18-Jun-1990Domain record last updated: 24-Sep-2007Domain expires: 31-Jul-2009

Page 12: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Transmission Control Protocol (TCP): ◦Used to create reliable byte-streaming communication

◦Ensures that messages are passed error-free from one machine to another on an internet

◦Divides data into discrete messages before passing them on to the IP layer

◦At the receiving end, protocol used to “reconstruct” the packets correctly

Page 13: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Applications utilize TCP/IP communication by having the sender and the receiver create end points called Sockets

Sockets are created and maintained by the Operating System (not the network stack)

Page 14: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Created in the early 80’s as part of the original Berkeley distribution of Unix that contained an early version of the Internet protocols.

Provides a user-level interface to the network.

Underlying basis for all Internet applications.

Based on client/server programming model.

Page 15: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

What is a socket?◦ To the kernel, a socket is an endpoint of communication.

◦ To an application, a socket is a file descriptor that lets the application read/write from/to the network. In Unix all I/O devices, including networks, are modeled as files.

Clients and servers communicate with each other by reading from and writing to socket descriptors.

A socket is not a physical device!◦ your computer may have one physical Ethernet connection◦ and many sockets

Page 16: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Each socket has a socket number (address)◦ IP address of the host computer◦ Port number local to that host to which the socket is attached Port numbers below 1024 are reserved (e.g. for email and FTP)

Some addresses are “well known”, Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server

You must choose a port not already in use

Page 17: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

To use TCP service a connection must be explicitly established between a socket on the sending machine and a socket on the receiving machine

A socket may be used for multiple connections at the same time.

A computer (and/or application) may have multiple open socket connections

Page 18: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Computer

Physical connection (e.g.

ethernet, RS

232)

OSapplication

Port 80

Port 5678

socket

IP: 147.129.30.2

Page 19: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Client Server

socket socket

bind

listen

rio_readlineb

rio_writenrio_readlineb

rio_writen

Connectionrequest

rio_readlineb

close

closeEOF

Await connectionrequest fromnext client

open_listenfd

open_clientfd

acceptconnect

Page 20: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

use netstat.exe program (ships with windows, mac, and linux).

Start ”Command Prompt" using run-> cmd on Windows type netstat -na and then press Enter, use command "more" to stop when your listing contains to much lines (netstat -na | more).

Each line of netstat's listing contains these information:

* Protocol type - it can be TCP or UDP. Two sockets can use the same port number when they use different protocols.

* Local Address - local IP address which is used. In most cases it is 0.0.0.0 (any = not bound to the specific IP address. It can accept connection from any PC's interface). When IP address is specified then only connection from interface with the same IP address are accepted.

Local Address field contains port number which is used to accept connection or receive UDP data.

* Foreign Address - address of PC on remote side. It will be displayed when connection is established.

* State - state of socket. You can see this states:

LISTENING - socket waits for connection (or data in case of UDP socket)

ESTABLISHED - connection to the remote side is established

CLOSE_WAIT, TIME_WAIT - socket waits for connection close or other type of negotiation.

To determine whether desired port number is already in use you must check Local Address field on all lines.

On Windows:

Page 21: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Example line:

TCP 0.0.0.0:25 0.0.0.0:0 LISTENING

Some application which uses port 25 (SMTP server port) is running.

Page 22: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

On Linux:

Read the /etc/services file. If you want the list of open ports, try netstat -nlp◦ Arguments: n = show network addresses as numbers, l = show listening sockets, p = show pid and name of program

Page 23: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

On Macs:

Read the /etc/services file. If you want the list of open ports, try netstat -n◦ The port follows the IP address

Page 24: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Networked applications may be structured any number of ways

One popular structure is client-server Server:◦ Usually unique◦ Provides a service ◦ Runs continuously

Clients◦ Usually many◦ Send request for service, receive result from server

◦ Connect and disconnect as needed

Page 25: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Clientprocess

Serverprocess

1. Client sends request

2. Server handlesrequest

3. Server sends response4. Client handlesresponse

Resource

Page 26: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Web server(port 80)

Client host

Server host 128.2.194.242

Echo server(port 7)

Service request for128.2.194.242:80(i.e., the Web

server)

Web server(port 80)

Echo server(port 7)

Service request for128.2.194.242:7

(i.e., the echo server)

Kernel

Kernel

Client

Client

Page 27: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Servers are long-running processes (daemons in Unix).◦ Created at boot-time (typically) by the init process (process 1)

◦ Run continuously until the machine is turned off. Each server waits for requests to arrive on a well-known port associated with a particular service.◦ Port 7: echo server◦ Port 23: telnet server◦ Port 25: mail server◦ Port 80: HTTP server◦ See

http://www.answersthatwork.com/Download_Area/ATW_Library/Networking/Network__2-List_of_Common_TCPIP_port_numbers.pdf

A machine that runs a server process is also often referred to as a “server.”

Page 28: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Web server (port 80)◦ Resource: files/compute cycles (CGI programs)◦ Service: retrieves files and runs CGI programs on behalf of the client

FTP server (20, 21)◦ Resource: files◦ Service: stores and retrieve files

Telnet server (23)◦ Resource: terminal◦ Service: proxies a terminal on the server machine

Mail server (25)◦ Resource: email “spool” file◦ Service: stores mail messages in spool file

See /etc/services for a comprehensive list of the services available on a Linux machine.

Page 29: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Your web browser is the client Computer containing the pages you are trying to access runs the web server

Your computer requests the page Server sends the page Many other people may be requesting the same page

Note: these are distinct applications. Your browser (client) never acts as a server

Page 30: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

As you develop networked applications:◦ Is the client-server model a good model?

Minimizes network communication? Minimizes computational redundancy?

If so:◦ Which program will be on the server side (e.g. provide a service)?

◦ Which must be on the client side?

Page 31: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

python provides built-in support for TCP/IP network communication

There are methods for connecting a client and methods for connecting a server

Page 32: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Within a Client application, create a socket and connect to a specific server:

#Socket client example in python

import socket   #for sockets

#create an AF_INET, STREAM socket (TCP)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Address Family : AF_INET (this is IP version 4 or IPv4)Type : SOCK_STREAM (this means connection oriented TCP protocol)

Page 33: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Better to do error checking:#handling errors in python socket programs

 

import socket   #for sockets

import sys  #for exit

 

try:

    #create an AF_INET, STREAM socket (TCP)

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

except (socket.error, msg):

    print ('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1])

    sys.exit()

Page 34: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

To connect to a remote server need 2 things, IP address and port number. ◦ need to know the IP address of the remote server◦ Here we used the ip address of google.com as a sample.host = 'www.google.com' try:    remote_ip = socket.gethostbyname( host ) except socket.gaierror:    #could not resolve    print ('Hostname could not be resolved. Exiting’)    sys.exit()     print ('Ip address of ' + host + ' is ' + remote_ip)

method to obtain an IP address from a host name

method to obtain an IP address from a host name

Page 35: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Now connect to google on port 80 (the well known http port)

#Connect to remote serverport = 80s.connect((remote_ip , port)) print ('Socket Connected to ' + host + ' on ip ' + remote_ip)

Page 36: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Now we can send and receive messages◦ sendall method sends bytes through the socket◦ recv method receives a number of bytes

#Send some data to remote servermessage = "GET / HTTP/1.1\r\n\r\n" try : #Set the whole string s.sendall(bytes(message, 'UTF-8'))except socket.error: #Send failed print ('Send failed') sys.exit() print ('Message send successfully’)#Now receive datareply = s.recv(4096) print (reply)

must convert string to bytesmust convert string to bytes

Page 37: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Servers do the following actions:1. Open a socket 2. Bind to a address(and port).

3. Listen for incoming connections. 4. Accept connections 5. Read/Send

try:    #create an AF_INET, STREAM socket (TCP)    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)except (socket.error, msg):    print ('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error: ' + msg[1])    sys.exit();

open a socket for the server just as we did for the client

open a socket for the server just as we did for the client

Page 38: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Next must bind the socket to an address and a port

By doing this we ensure that all incoming data which is directed towards this port number on this machine is received by this application.

To bind the socket use the following method:◦ s.bind(hostname, port number)

Page 39: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Ways of specifying arguments to bind.

to find the name of the host that the program is running on use:◦ hostname = socket.gethostname()

to create a “server” socket that is only visible within the same machine use ◦ s.bind(('localhost', 80)) or ◦ s.bind(('127.0.0.1', 80))

To specify that the socket is reachable by any address the machine happens to have use◦ s.bind(('', 80))

Page 40: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

To bind the socket to an address and a port

import socketimport sys HOST = ''   # Symbolic name meaning all available interfacesPORT = 8888 # Arbitrary non-privileged port

try:    s.bind((HOST, PORT))except socket.error , msg:    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]    sys.exit()     print 'Socket bind complete'

the server socket will listen on port 8888 (not a well known port) on all interfaces

the server socket will listen on port 8888 (not a well known port) on all interfaces

Page 41: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

The next step is to make the socket listen for incoming requests

We must put the socket into listening mode:◦ s.listen(x)

The parameter x is called the backlog. ◦ It controls the number of incoming connections that are kept "waiting" if the program is already busy.

◦ x =10 means that if 10 connections are already waiting to be processed, then the 11th connection request shall be rejected.

Page 42: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

The final step is for the socket to accept incoming requests

When a request to this machine on the correct port comes in, the socket must accept the request:◦ (conn, addr) = s.accept()

The result conn is a new socket with which we can communicate with the client.

The result addr is an array. ◦first element is the IP of the client◦second element is the port the client is using

Page 43: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

(conn, addr) = s.accept()#display client informationprint ('Connected with ' + addr[0] + ':' +str(addr[1]))

This displays the client addr

Page 44: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Now can send & receive over the connection:

#now keep talking with the clientdata = conn.recv(1024)print ("Server received: " + data)conn.sendall("Server says: Hello client!")

Page 45: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

When finished, close the connection & socket:

conn.close()s.close()

Page 46: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

See Class web site: ◦ Course Resources -> Program Examples->find_ip.py

◦ Course Resources -> Program Examples->client.py

◦ Course Resources -> Program Examples->server.py

Page 47: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

Calculating area:◦ Server calculates area given radius◦ Client gets radius from user, requests area calculation

Page 48: Structural view of networks ◦ Physical layer: wire, radio, etc. ◦ Data link layer: protocol principles, etc ◦ Network layer: routing, internetworking

server

radius

clientclient

radius

Area