cmu ssd8: networks tools and ftp protocols

58
CMU SSD8: Networks Tools and FTP Protocols Tan Yi Software College Northwestern Polytechnical University

Upload: osma

Post on 22-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

CMU SSD8: Networks Tools and FTP Protocols. Tan Yi Software College Northwestern Polytechnical University. Network Tools ( 常用的网络工具 ). Netstat Ping/tracert Nslookup. netstat. displays the contents of various net- work-related data structures. The options have the following meaning: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMU SSD8:  Networks Tools and FTP Protocols

CMU SSD8: Networks Tools and FTP Protocols

Tan Yi

Software College

Northwestern Polytechnical University

Page 2: CMU SSD8:  Networks Tools and FTP Protocols

Network Tools ( 常用的网络工具 )

• Netstat• Ping/tracert• Nslookup

Page 3: CMU SSD8:  Networks Tools and FTP Protocols

netstat

• displays the contents of various net- work-related data structures.

• The options have the following meaning: – -a With the default display, show the state of

all sockets; normally sockets used by server processes are not shown.

– -s Show per-protocol statistics.

Page 4: CMU SSD8:  Networks Tools and FTP Protocols

Ping/tracert

• ICMP• Traceroute

Page 5: CMU SSD8:  Networks Tools and FTP Protocols

FTP 文件传输协议(File Transfer Protocol)

Reference:

RFC 959

Page 6: CMU SSD8:  Networks Tools and FTP Protocols

FTP 目标 Objectives( 摘录于 RFC 959)

• promote sharing of files

• encourage indirect use of remote computers

• shield user from variations in file storage

• transfer data reliably and efficiently

• “FTP, although usable directly by a user at a terminal, is designed mainly for use by programs”

Page 7: CMU SSD8:  Networks Tools and FTP Protocols
Page 8: CMU SSD8:  Networks Tools and FTP Protocols

HTTP 与 FTP 的比较

• 都是文件传输协议, TCP• FTP 使用两个并行的 TCP 连接 control connectio

n, data connection.• FTP 的控制信息是 out-of-band• HTTP 是带内 in-band 传输信息

Page 9: CMU SSD8:  Networks Tools and FTP Protocols

The FTP Model

Page 10: CMU SSD8:  Networks Tools and FTP Protocols

• FTP client ( 客户机 ) 首先与 server( 服务器 ) 在21 号端口上建立一个用于控制的 TCP 连接。

• FTP client

Page 11: CMU SSD8:  Networks Tools and FTP Protocols

Control and Data Connections

• Control functions (commands) and reply codes are transferred over the control connection.

• All data transfer takes place over the data connection.

• The control connection must be “up” while data transfer takes place.

Page 12: CMU SSD8:  Networks Tools and FTP Protocols

Control Connection

• The control connection is the “well known” service. Port 21

• The control connection uses the TELNET protocol.

• Commands and replies are all line oriented text (default is ASCII).

Page 13: CMU SSD8:  Networks Tools and FTP Protocols

Standard Connection Model

ControlControl

DataDataAAAA BBBB

Page 14: CMU SSD8:  Networks Tools and FTP Protocols

Alternative Connection Model

ControlControl

DataData

AA

BBBB CCCC

ControlControl

Page 15: CMU SSD8:  Networks Tools and FTP Protocols

Access Control Commands

USER specify user 用户名PASS specify password 口令CWD change directory 换目录CDUP change directory to parent

上一级目录QUIT logout 退出

Page 16: CMU SSD8:  Networks Tools and FTP Protocols

Transfer Parameter Commands

PORT publish local data port

PASV server should listen

TYPE establish data representation

MODE establish transfer mode

STRU establish file structure

Page 17: CMU SSD8:  Networks Tools and FTP Protocols

Service Commands

RETR retrieve file

SORT send file

STOU send file and save as unique

APPE send file and append

ABOR abort prev. service command

PWD print working directory

LIST transfer list of files over data link

Page 18: CMU SSD8:  Networks Tools and FTP Protocols

FTP Replies

• All replies are sent over control connection.

• Replies are a single line containing– 3 digit status code (sent as 3 numeric chars).– text message.

• The FTP spec. includes support for multiline text replies.

Page 19: CMU SSD8:  Networks Tools and FTP Protocols

FTP Reply Status Code example

• 331 Username OK, password required• 125 Data connection already open; transfer

starting• 425 Can’t open data connection• 452 Error writing file

Page 20: CMU SSD8:  Networks Tools and FTP Protocols

FTP Reply Status Code

First digit of status code indicates type of reply:‘1’: Positive Preliminary Reply (got it, but wait).

‘2’: Positive Completion Reply (success).

‘3’: Positive Intermediate Reply (waiting for more information).

‘4’: Transient Negative Completion (error - try again).

‘5’: Permanent Negative Reply (error - can’t do).

Page 21: CMU SSD8:  Networks Tools and FTP Protocols

FTP Reply Status Code

• 2nd digit indicates function groupings.‘0’: Syntax (problem with command syntax).

‘1’: Information (reply to help or status cmds).

‘2’: Connections (problem with a connection).

‘3’: Authentication (problem with login).

‘4’: Unspecified.

‘5’: File system (related to file system).

• 3rd digit indicates specific problem within function group.

Page 22: CMU SSD8:  Networks Tools and FTP Protocols

Data Transfer Modes

• STREAM: file is transmitted as a stream of bytes.

• BLOCK: file is transmitted as a series of blocks preceded by headers containing count and descriptor code (EOF, EOR, restart marker).

• COMPRESSED: uses a simple compression scheme - compressed blocks are transmitted.

Page 23: CMU SSD8:  Networks Tools and FTP Protocols

RFC 959

• The RFC includes lots more information and many details including:– parameters for commands– lists of reply status codes– protocol state diagrams– support for a variety of file structures– sample sessions

Page 24: CMU SSD8:  Networks Tools and FTP Protocols

TFTPTrivial File Transfer Protocol

References:

RFC 783, 1350

Page 25: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Usage and Design

• Transfer files between processes.

• Minimal overhead (no security).

• Designed for UDP, although could be used with many transport protocols.

Page 26: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Usage and Design (cont.)

• Easy to implement

• Small - possible to include in firmware

• Often uses to bootstrap workstations and network devices.

Page 27: CMU SSD8:  Networks Tools and FTP Protocols

Diskless Workstation Booting 1The call for help

DisklessWorkstation

Help! I don't know who I am!My Ethernet address is:4C:23:17:77:A6:03

RARP

Page 28: CMU SSD8:  Networks Tools and FTP Protocols

Diskless Workstation Booting 2

The answer from the all-knowing

DisklessWorkstation

I know all! You are to be know as: 128.113.45.211

RARP REPLY

RARPServer

Page 29: CMU SSD8:  Networks Tools and FTP Protocols

DisklessWorkstation

I need the file named boot-128.113.45.211

TFTP Request (Broadcast)

Diskless Workstation Booting 3

The request for instructions

Page 30: CMU SSD8:  Networks Tools and FTP Protocols

Diskless Workstation Booting 4

The dialog

DisklessWorkstation

TFTP File Transfer

TFTPServer

here is part 1

I got part 1

here is part 2

boot file

Page 31: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Protocol

5 message types:– Read request– Write request– Data– ACK (acknowledgment)– Error

Page 32: CMU SSD8:  Networks Tools and FTP Protocols

Messages

• Each is an independent UDP Datagram

• Each has a 2 byte opcode 操作字 (1st 2 bytes)

• The rest depends on the opcode.

Page 33: CMU SSD8:  Networks Tools and FTP Protocols

Message Formats

OPCODE 0 0FILENAME MODE

BLOCK# DATA

BLOCK#

OPCODE

OPCODE

OPCODE BLOCK# ERROR MESSAGE 0

2 bytes 2 bytes

Page 34: CMU SSD8:  Networks Tools and FTP Protocols

Read Request

0202 filenamefilename 00 modemode 00

2 byte opcode2 byte opcodenetwork byte ordernetwork byte order

null terminated ascii stringnull terminated ascii stringcontaining name of filecontaining name of file

null terminated ascii stringnull terminated ascii stringcontaining transfer modecontaining transfer mode

variable length fields!variable length fields!

Page 35: CMU SSD8:  Networks Tools and FTP Protocols

Write Request

0202 filenamefilename 00 modemode 00

2 byte opcode2 byte opcodenetwork byte ordernetwork byte order

null terminated ascii stringnull terminated ascii stringcontaining name of filecontaining name of file

null terminated ascii stringnull terminated ascii stringcontaining transfer modecontaining transfer mode

variable length fields!variable length fields!

Page 36: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Data Packet

0303 block #block # data 0 to 512 bytesdata 0 to 512 bytes

2 byte opcode2 byte opcodenetwork byte ordernetwork byte order

2 byte block number2 byte block numbernetwork byte ordernetwork byte order

all data packets have 512 bytesall data packets have 512 bytesexcept the last one.except the last one.

Page 37: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Acknowledgment

0404 block #block #

2 byte opcode2 byte opcodenetwork byte ordernetwork byte order

2 byte block number2 byte block numbernetwork byte ordernetwork byte order

Page 38: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Error Packet

0505 errcodeerrcode errstringerrstring

2 byte opcode2 byte opcodenetwork byte ordernetwork byte order

2 byte error code2 byte error codenetwork byte ordernetwork byte order

null terminated ascii error stringnull terminated ascii error string

00

Page 39: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Error Codes

0 - not defined

1 - File not found

2 - Access violation

3 - Disk full

4 - Illegal TFTP operation

5 - Unknown port

6 - File already exists

7 - No such user

Page 40: CMU SSD8:  Networks Tools and FTP Protocols

TFTP transfer modes

• “netascii” : for transferring text files.– all lines end with \r\n (CR,LF).– provides standard format for transferring text files.– both ends responsible for converting to/from netascii f

ormat.

• “octet” : for transferring binary files.– no translation done.

Page 41: CMU SSD8:  Networks Tools and FTP Protocols

NetAscii Transfer Mode

Unix - end of line marker is just '\n'

• receiving a file– you need to remove '\r' before storing data.

• sending a file– you need to replace every '\n' with "\r\n" before

sending

Page 42: CMU SSD8:  Networks Tools and FTP Protocols

Lost Data Packets - Original Protocol

Specification• Sender uses a timeout with restransmission.

– sender could be client or server.

• Duplicate data packets must be recognized and ACK retransmitted.

• This original protocol suffers from the "sorcerer’s apprentice syndrome".

Page 43: CMU SSD8:  Networks Tools and FTP Protocols

Sorcerer’s Apprentice Syndrome

send DATA[n]

(time out)retransmit DATA[n]

receive ACK[n]send DATA[n+1]

receive ACK[n] (dup)send DATA[n+1]

(dup)

...

receive DATA[n]send ACK[n]

receive DATA[n] (dup)send ACK[n] (dup)

receive DATA[n+1] send ACK[n+1]

receive DATA[n+1] (dup)send ACK[n+1] (dup)

Page 44: CMU SSD8:  Networks Tools and FTP Protocols

The Fix

• Sender should not resend a data packet in response to a duplicate ACK.

• If sender receives ACK[n] - don’t send DATA[n+1] if the ACK was a duplicate.

Page 45: CMU SSD8:  Networks Tools and FTP Protocols

Concurrency

• TFTP servers use a "well known address" (UDP port number).

• How would you implement a concurrent server?– forking (alone) may lead to problems!– Can provide concurrency without forking, but it

requires lots of bookkeeping.

Page 46: CMU SSD8:  Networks Tools and FTP Protocols

TFTP Concurrency

• According to the protocol, the server may create a new udp port and send the initial response from this new port.

• The client should recognize this and send all subsequent messages to the new port.

Page 47: CMU SSD8:  Networks Tools and FTP Protocols

RRQ (read request)

• Client sends RRQ• Server sends back data chunk #0• Client acks chunk #0• Server sends data chunk #1• ...

Page 48: CMU SSD8:  Networks Tools and FTP Protocols

WRQ (write request)

• Client sends WRQ• Server sends back ack #0• Client data chunk #1 (the first chunk!)• Server acks data chunk #1• …

there is no data chunk #0!

Page 49: CMU SSD8:  Networks Tools and FTP Protocols

When is it over?

• There is no length of file field sent!

• All data messages except the last one contain 512 bytes of data.– message length is 2 + 2 + 512 = 516

• The last data message might contain 0 bytes of data!

Page 50: CMU SSD8:  Networks Tools and FTP Protocols

Issues

What if more than 65535 chunks are sent?– 65536 blocks x 512 bytes/block = 33,554,432 bytes.

• The RFC does not address this issue!• Remember that the network can duplicate

packets!

Page 51: CMU SSD8:  Networks Tools and FTP Protocols

Timeouts

• Set up an alarm to go off after a few seconds.• Call recvfrom (or recv or read).

– Check for error and EINTR– Can also set a flag in the SIGALRM signal handler.

Page 52: CMU SSD8:  Networks Tools and FTP Protocols

Timeout & RetransmissionParameters

• Reasonable Values:– wait no more than 5 seconds– retransmit no more than 5 times

• If no response - give up!

Page 53: CMU SSD8:  Networks Tools and FTP Protocols

Avoiding Sorcerer's Apprentice Syndrome

• Sender should timeout and retransmit.• Sender should ignore duplicate ACKs.

– don't retransmit data!

• Receiver should transmit ACK whenever data is received.– could be duplicate ACK, that's OK.

Page 54: CMU SSD8:  Networks Tools and FTP Protocols

Building Messages

• The messages are built in memory.

• Entire message is given to sendto.

• Opcode, block# are binary, network byte order, 2-byte integers.

Page 55: CMU SSD8:  Networks Tools and FTP Protocols

Suggestions

• Write a function that builds a message.buildmsg(char *buf, int op, int block, ...

– one place in the code to worry about network byte order!

• Write a function that extracts fields from a message.

Page 56: CMU SSD8:  Networks Tools and FTP Protocols

Stuffing binary values into a buffer

short int opcode; char *buffer;

Using memcpy - need to convert to NBO first:

tmp = htons(opcode);

memcpy(buffer, (char *) &tmp, 2);

Page 57: CMU SSD8:  Networks Tools and FTP Protocols

Advanced Stuffing Techniques

(a great name for a band!)

Stuffing NBO short in to a buffer:*((short int *) buffer) = htons(opcode);

Extracting a NBO short from a bufferopcode = ntohs( *((short int *) buffer+2));

doesn't have to be the beginning of the buffer!

Page 58: CMU SSD8:  Networks Tools and FTP Protocols