cmu ssd8: networks tools and ftp protocols
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 PresentationTRANSCRIPT
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: – -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.
Ping/tracert
• ICMP• Traceroute
FTP 文件传输协议(File Transfer Protocol)
Reference:
RFC 959
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”
HTTP 与 FTP 的比较
• 都是文件传输协议, TCP• FTP 使用两个并行的 TCP 连接 control connectio
n, data connection.• FTP 的控制信息是 out-of-band• HTTP 是带内 in-band 传输信息
The FTP Model
• FTP client ( 客户机 ) 首先与 server( 服务器 ) 在21 号端口上建立一个用于控制的 TCP 连接。
• FTP client
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.
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).
Standard Connection Model
ControlControl
DataDataAAAA BBBB
Alternative Connection Model
ControlControl
DataData
AA
BBBB CCCC
ControlControl
Access Control Commands
USER specify user 用户名PASS specify password 口令CWD change directory 换目录CDUP change directory to parent
上一级目录QUIT logout 退出
Transfer Parameter Commands
PORT publish local data port
PASV server should listen
TYPE establish data representation
MODE establish transfer mode
STRU establish file structure
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
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.
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
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).
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.
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.
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
TFTPTrivial File Transfer Protocol
References:
RFC 783, 1350
TFTP Usage and Design
• Transfer files between processes.
• Minimal overhead (no security).
• Designed for UDP, although could be used with many transport protocols.
TFTP Usage and Design (cont.)
• Easy to implement
• Small - possible to include in firmware
• Often uses to bootstrap workstations and network devices.
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
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
DisklessWorkstation
I need the file named boot-128.113.45.211
TFTP Request (Broadcast)
Diskless Workstation Booting 3
The request for instructions
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
TFTP Protocol
5 message types:– Read request– Write request– Data– ACK (acknowledgment)– Error
Messages
• Each is an independent UDP Datagram
• Each has a 2 byte opcode 操作字 (1st 2 bytes)
• The rest depends on the opcode.
Message Formats
OPCODE 0 0FILENAME MODE
BLOCK# DATA
BLOCK#
OPCODE
OPCODE
OPCODE BLOCK# ERROR MESSAGE 0
2 bytes 2 bytes
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!
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!
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.
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
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
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
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.
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
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".
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)
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.
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.
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.
RRQ (read request)
• Client sends RRQ• Server sends back data chunk #0• Client acks chunk #0• Server sends data chunk #1• ...
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!
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!
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!
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.
Timeout & RetransmissionParameters
• Reasonable Values:– wait no more than 5 seconds– retransmit no more than 5 times
• If no response - give up!
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.
Building Messages
• The messages are built in memory.
• Entire message is given to sendto.
• Opcode, block# are binary, network byte order, 2-byte integers.
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.
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);
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!