1 CS 352- Spring 2010 TA: Tuan Phan Email: tphan@cs.rutgers.edutphan@cs.rutgers.edu 732-445-6450...

Post on 16-Jan-2016

242 views 0 download

transcript

1

CS 352- Spring 2010TA: Tuan Phan

Email: tphan@cs.rutgers.edu732-445-6450 (ext 9644) :

Just leaving msg( prefer using Email)

Recitation: Th 12:15 – 1:10 PM @ SEC 204Office Hour: F 11:00 – 12:00 @ CORE 340

Extra: Email to setup appointment on Tuesday afternoon.

Slides for recitation, Useful Links: SAKAI

1. Account on Cereal/iLab

2. Socket Programming with JavaPage 163, text book.

1. Review Java I/O http://java.sun.com/docs/books/tutorial/essential/io/

1. Project 1: Contact with Tracker

2. Get familiar with Wireshark, Eclipse, WinSCP & other tools.

Last Homeworks

TCP Socket Programming• Server

String clientSetence, capitalizedSentence;

welcomeSock = ServerSocket();

while(true) {

connSock = welcomeSocket.accept();

inFromClient= new BufferedReader( new InputStreamReader(connSock.getInputStream()));

outToClient=new DataOutputStream(connSocket.getOutputStream());

clientSetence = inFromClient.readLine();

capitalizedSentence = clientSetence.toUpperCase()+’\n’;

outToClient.writeBytes(capitalizedSentence);

}

• Write to Socket

outToClient.write(byte[] b, int off, int len);

outToClient.flush();

• Read from Socket:

inFromClient .readInt();

inFromClient .readByte();

int inFromclient.read(bytes[] b);

int inFromclient.read(bytes[] b, int offset, int len);

3

1. Read the input TorrentFile , and store information into a class- Use TorrentInfo.java & Bencoder2.java

1. Contact Tracker using HTTP_GET

2. Analyze the HTTP response from tracker to find the peer.

3. Implement hand-shake protocol between peer-to-peer.

4. Request piece and receive piece. Verify hash for each piece.

5. Write to a *.GIF file.

Project Challenges

Contact with Tracker• info_hash - The 20 byte(160-bit) SHA1 hash of the bencoded form

of the info value from the metainfo file.• peer_id - A string of length 20 which this downloader uses as its id.• ip – optional • port - The port number this peer is listening on. Common behavior is

6881-6889.• uploaded - in base-10 ascii.• downloaded - The total amount downloaded so far, encoded in

base-10 ascii.• left - The number of bytes this peer still has to downloaded,

encoded in base-10 ascii. This key is important - If you do not specify how much you have left to download, the tracker assumes you are a seed and will not return any seeds in the peer list.

• event = started , completed , or stopped

5

● Notation <field_name: size in bytes: value>

– Handshake:

<pstrlen:1:19><pstr:19:’BitTorrent protocol’> <reserved:8:’0..0’><info_hash:20:’from Torrent.file’><peer_id:??:’??’>

– General Messages:<length prefix:4:?><msg_ID:1:?><payload:?:?>

– Keep-alive: <prefix:4:0x0000>

– Interested: <prefix:4:0x0001><msg_ID:1:2>

– Uninterested: <prefix:4:0x0001><msg_ID:1:3>

– Have: <prefix:4:0x0005><msg_ID:1:4><index:4:?>

– Request Piece: <prefix:4:0x000D><msg_ID:1:6><index:4:?><begin:4:?><length:4:?>

● <index>: an integer (encoded as a 4-byte big-endian) – 0_based piece index

● <begin>: 0_based byte offset within the piece

● <length>: default = 2^14 = 16384 bytes

– Piece: <prefix:4:0x0009+X><msg_ID:1:7><index:4:?><begin:4:?><block:?:?>

• X is the size of payload

Download from a peer

● Download and Verify All Pieces

– SHA-1 Hash (160 bits)

MessageDigest.getInstance("SHA-1");

byte[] hash = sha.digest(bytes);

● Save to File

– FileOutputStream.write(byte[])

Verify Pieces & Save File

Debug• How do I know that my message is in a correct

format?– Wireshack + standard Torrent Client– Filtering:

• ip.addr == 128.6.157.250

• How do I know that my program forgets to process an incoming packet?– Wireshack

8

9

Sample Question 1: OSI LAYERING

• Fill in the boxes with the function (1-7) and protocol (A-G) belonging to them.

Layer Typical Functions Example Protocol

Application

Presentation

Session

Transport

Network

DataLink

Physical

10

Sample Question 1(cont.)

• Functions:1 Provides reliable end-to-end communication.2 Represent bytes as different voltages.3 Implement communication between two applications of the same type.5 Groups several user-level connections into a single entity.6 Provides reliable transfer of information between two adjacent nodes.7 Decides the route a packet will take across the network.4 Transform between big and little endian representations.

• Protocols:A TelnetB EthernetC User Datagram ProtocolD Category-5 Twisted PairE Internet ProtocolF Point-to-Point ProtocolG Tranmission Control Protocol

11

Internet Architecture

FTP HTTP RTP TFTP

TCP UDP

IP

Ethernet 802.11 PPP

CAT-5 Single-ModeFiber

RS-232

12

Network programming in JavaTCP/IP stack

Application Protocol

Transport Protocols (UDP and TCP)

ApplicationLayer

TransportLayer

NetworkLayer

Host-to-Net Layer

Host A Host B

ApplicationLayer

TransportLayer

NetworkLayer

Host-to-Net Layer

NetworkLayer

Host-to-Net Layer

NetworkLayer

Host-to-Net Layer

IPIP IP

Data

Data

TCP/UDP header

Data

TCP/UDP header

IP header

13

Sample Question2: ENCAPSULATION

Fit the following headers in the correct order in the frame provided. UDP, Ethernet, DNS and IP.

If the application wants to send a payload (data) of 400 bytes to the destination and the headers are of the sizes given below, what will be the size of the entire frame as presented to the IP layer at the destination?

UDP: 50bytes, Ethernet:40bytes, DNS:20 byes, IP:30bytes

14

Why DNS?

• On Internet, each machine has an unique IP address, e.g, 100.100.1.201.

• The IP addresses are used to communicate between 2 machines.

• Human is familiar with names: www.google.com, mail.yahoo.com .

• Need a mechanism to convert from name to IP address: DNS(Domain Name Service)

DNS ProtocolExample

remus

ns-lcsr

a.root-servers.net

yale.edu

cs.yale.edu

1

2

3

4

8

7

6

5

Scenario:

remus tries toresolve an IP address forvenus.cs.yale.eduusing a recursive query

DNS ProtocolAnother Example

remus

ns-lcsr

a.root-servers.net

yale.edu

cs.yale.edu

1 2

Scenario:

remus tries toresolve an IP address forvenus.cs.yale.eduusing an iterative query

3 4

5 6

7 8

Some servers do not supportRecursive queries

17

Sample Question 8: DNS • Consider the internet in the figure below, in which zones are indicated with a

dashed line. There is only one DNS server per zone and it happens to have the same name as the highest node in each zone: yale.edu, cs.yale.edu, rutgers.edu, cs.rutgers.edu and root-servers.net.

• The only servers supporting recursive querying are cs.yale.edu and cs.rutgers.edu.

• For each of the queries below, list in order all the DNS servers contacted by the resolver (located in the OS of the machine running the query). Assume there is no caching performed at any level of the hierarchy.

18

Sample Question 8: DNS(cont.)• A. (4 points) A machine called lab1.bio.yale.edu exists in the biology

department at Yale, and a user on eden.rutgers.edu launches this query: "nslookup lab1.bio.yale.edu’’ .

• B. (4 points) At the prompt of paul.cs.rutgers.edu somebody launches this query: "nslookup lab1.bio.yale.edu" .

• C. (4 points) Later, lab1.bio.yale.edu is assigned a new IP (but keeps the same name) and is physically moved in the science building onto a local Ethernet with other machines such as electron.eng.yale.edu and theorem.eng.yale.edu. At the prompt of eden.rutgers.edu you launch this query: "nslookup lab1.bio.yale.edu".

• D. (4 points) In the science building, on lab1.bio.yale.edu, somebody queries: "nslookup paul.cs.rutgers.edu".

• E. (4 points) Follow the same scenario as in part (B), but this time indicate, in order, all the queries involved in the process. Use this notation to represent a query: rutgers.edu -> rootservers.net to indicate that rutgers.edu is generating/forwarding a query to rootservers.net.

19

Build your own network

• 3 CS students live together -> How about a small network connecting to Internet??? What equipments do they need to buy?? Is it very expensive ???

• System Admin of a small company want to build a network that connect 20 machines in the company. The office locates in 2 floors of a building. What equipments does he need to buy?

• HUB, switch, router … how to distinguish them?

• How to “touch” the network theories in class??– TCP/IP, DNS, HTTP– Packet capturing !!!

20

ETHERNET

• TOPOLOGY: Bus & Tree

• BUS TOPOLOGY

Host

T-Connector/TransceiverCoaxial Cable

Terminator

NIC

Send

Transfer !!

Receive

Receive

21

ETHERNET – Tree Topology

HUB

Backbone HUB

SWITCH

BA C X Y Z

Tsend

forward

Crossover cable CAT 5-

UTP cable

straight through

cable

22

Straight through cable & crossover cable

Fig1: Typical RJ45 Connector

Figure 2: 8 wires in RJ45 that is labeled from 1 to 8

Figure 3: Wiring for straight thru and crossover cables

23

MAC address

• Network Interface Card (NIC)– Each NIC has its own MAC address , it is

stored on the chip!!

• Length of MAC : 48 bites = 6 bytes• For examples: 00:C1:71:01:AB:F0

– Prefix: Company ID: 3Com, Netgear …– Postfix: Serial Number

• Command to show MAC: ifconfig, ipconfig

24

Ethernet Frame Structure

• Type(2 bytes) specifies the network layer protocol: IP or Novel IPX or ARP or RARP…

• CRC: check sum

• Broadcast:– MAC Broadcast Address = FF:FF:FF:FF:FF:FF

• Host A sends a msg to host B:– MAC dest = MAC B, MAC src = MAC A

MAC Dest. address

MAC Source address Type DATA …. CRC

25

HUB vs. SWITCH

HUB

MAC A -> MAC C

SWITCH

BA C A B C

send

DATA

Ethernet Frame

forward

B can CAPTURE the packet from A to C !!!!

send

SWITCH can learn the MACs of all nodes

B CAN’T capture the packet from A to C !!!!

26

How to build your own network!

• NICs

• HUB or SWITCH !!!

• 2 machines can be connected by using crossover cable!!!

• Assign IP address– Static– Dynamic

Crossover cable

Homeworks

• Re-answer the sample questions today

• Implement downloading pieces from the given peer.

• Learn to debug your BitTorrent client with Wireshark.

27