03

Document Sample
03 Powered By Docstoc
					Computer Networks with
Internet Technology
William Stallings

Chapter 03
Traditional Applications
Traditional Applications
• Terminal Access – Telnet
• File Transfer – FTP
• Electronic Mail
  — SMTP
  — MIME

  — POP3
Telnet
• IETF STD 8 (RFC 854 and RFC 855)
• Remote Terminal Access
            Original ARPANET environment
Network Virtual Terminal Concept
Current Use of Telnet
• Original environment for Telnet little relevance
  today
• Still used and included in the TCP/IP suite
• Available on PCs for use over the Internet
  —PC includes Telnet software
  —Telnet protocol and translation between PC
   keyboard/display and NVT
  —Not GUI
• Services available include United States Library
  of Congress
  —locis.loc.gov
Telnet Software
•   Telnet on Windows XP/2000                            tlntsvr.exe




•   puTTY
    —   http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
    — A free implementation of Telnet and SSH for Win32
      and Unix platforms
    — SSH (Secure Shell), TCP Port: 22
        •   Establishing a secure channel between a local and a
            remote computer
PuTTY
File Transfer - FTP
• File Transfer Protocol, RFC 959
• TCP Ports: 20: Data, 21: Control
• Objectives:
  —Promote sharing of files
  —Encourage indirect or implicit use of remote computers
  —Shield user from variations in file storage systems
  —Transfer data reliably and efficiently
• Anonymous FTP
  —User: anonymous
  —Password: guest or your e-mail address
FTP Model
FTP User Commands
!        delete       literal     prompt         send
?        debug         ls         put           status
append   dir            mdelete   pwd            trace
ascii    disconnect    mdir        quit          type
bell     get            mget        quote         user
binary    glob         mkdir        recv        verbose
bye       hash         mls         remotehelp
cd        help          mput        rename
close    lcd            open        rmdir
C:\>ftp ftp.ncnu.edu.tw
Connected to ftp4.ncnu.edu.tw.
220 Welcome to NCNU FTP service.
User (ftp4.ncnu.edu.tw:(none)): anonymous
331 Please specify the password.
Password:
230 Login successful.
ftp> dir
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x 2 503       503       4096 Feb 03 2006 Application
drwxr-xr-x 8 503       503       4096 Apr 05 2006 Documentation
drwxr-xr-x 17 503       503      4096 Oct 04 05:20 FreeBSD
drwxr-xr-x 19 503       503      4096 Feb 05 2006 JavaDownload
-rw-r--r-- 1 503     503       5327 Apr 22 04:04 index.html
226 Directory send OK.
ftp: 684 bytes received in 0.00Seconds 684000.00Kbytes/sec.
ftp> ascii
200 Switching to ASCII mode.
ftp> get index.html
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for index.html (5327 bytes).
226 File send OK.
ftp: 5327 bytes received in 0.01Seconds 355.13Kbytes/sec.
ftp> bye
221 Goodbye.
Electronic Mail
• Most heavily used application on any network
• Simple Mail Transfer Protocol (SMTP)
  —TCP/IP
  —Delivery of simple text messages
• Multi-purpose Internet Mail Extension (MIME)
  —Delivery of other types of data
  —Voice, images, video clips
SMTP
• RFC 821
• Not concerned with format of messages or data
   — Covered in RFC 822 (see later)
• SMTP uses info written on envelope of mail
   — Message header
• Does not look at contents
   — Message body
• Except:
   — Standardize message character set to 7 bit ASCII
   — Add log info to start of message
      • Shows path taken
Basic Operation
• Mail created by user agent program (mail client)
  —Message consists of:
     • Header containing recipient’s address and other info
     • Body containing user data
• Messages queued and sent as input to SMTP
  sender program
  —Typically a server process (daemon on UNIX)
Mail Message Contents
• Each queued message has:
  —Message text
     • RFC 822 header with message envelope and list of recipients
     • Message body, composed by user
  —A list of mail destinations
     •   Derived by user agent from header
     •   May be listed in header
     •   May require expansion of mailing lists
     •   May need replacement of mnemonic names with mailbox
         names
• If BCCs (Blind Carbon Copies) indicated, user
  agent needs to prepare correct message format
SMTP Sender
• Takes message from queue
• Transmits to proper destination host
   — Via SMTP transaction
   — Over one or more TCP connections to port 25
• Host may have multiple senders active
• Host should be able to create receivers on demand
• When delivery complete, sender deletes destination
  from list for that message
• When all destinations processed, message is deleted
Optimization
• If message destined for multiple users on a
  given host, it is sent only once
  —Delivery to users handled at destination host
• If multiple messages ready for given host, a
  single TCP connection can be used
  —Saves overhead of setting up and dropping
   connection
Possible Errors
•   Host unreachable
•   Host out of operation
•   TCP connection fail during transfer
•   Sender can re-queue mail
    —Give up after a period
• Faulty destination address
    —User error
    —Target user changed address
    —Redirect if possible
    —Inform user if not
SMTP Protocol - Reliability
• Used to transfer messages from sender to
  receiver over TCP connection
• Attempts to provide reliable service
• No guarantee to recover lost messages
• No end to end acknowledgement to originator
• Error indication delivery not guaranteed
• Generally considered reliable
SMTP Receiver
• Accepts arriving message
• Places in user mailbox or copies to outgoing
  queue for forwarding
• Receiver must:
  —Verify local mail destinations
  —Deal with errors
     • Transmission
     • Lack of disk space
• Sender responsible for message until receiver
  confirm complete transfer
  —Indicates mail has arrived at host, not user
SMTP Forwarding
• Mostly direct transfer from sender host to
  receiver host
• May go through intermediate machine via
  forwarding capability
  —Sender can specify route
  —Target user may have moved
Conversation
• SMTP limited to conversation between sender
  and receiver
• Main function is to transfer messages
• Rest of mail handling beyond scope of SMTP
  —May differ between systems
Figure 3.9 SMTP Mail Flow
SMTP System Overview
• Commands and responses between sender and
  receiver
• Initiative with sender
    —Establishes TCP connection
•   Sender sends commands to receiver
•   e.g. HELO<SP><domain><CRLF>
•   Each command generates exactly one reply
•   e.g. 250 requested mail action ok; completed
SMTP Replies
• Leading digit indicates category
  —Positive completion reply (2xx)
  —Positive intermediate reply (3xx)
  —Transient negative completion reply (4xx)
  —Permanent negative completion reply (5xx)
Operation Phases
• Connection setup
• Exchange of command-response pairs
• Connection termination
Connection Setup
• Sender opens TCP connection with receiver
• Once connected, receiver identifies itself
   —220 <domain> service ready
• Sender identifies itself
   —HELO
• Receiver accepts sender’s identification
   —250 OK
• If mail service not available, step 2 above
  becomes:
   —421 service not available
Mail Transfer
• Sender may send one or more messages to receiver
• MAIL command identifies originator
   — Gives reverse path to used for error reporting
   — Receiver returns 250 OK or appropriate fail/error message
• One or more RCPT commands identifies recipients for
  the message
   — Separate reply for each recipient
• DATA command transfers message text
   — End of message indicated by line containing just period (.)
Closing Connection
•   Two steps
•   Sender sends QUIT and waits for reply
•   Then initiate TCP close operation
•   Receiver initiates TCP close after sending reply
    to QUIT
   SMTP Commands




<SP> = space
<CRLF> = carriage return, Line feed
        ASCII codes: 13 (0D), 10 (0A)
Sample SMTP Exchange
• S: HELO pc-ycc
  R: 250 euler.im.ncnu.edu.tw Hello …, pleased
     to meet you
• S: MAIL FROM: Smith@Alpha.ARPA
  R: 250 OK
• S: RCPT TO: Jones@Beta.ARPA
  R: 250 OK
• S: RCPT TO: Green@Beta.ARPA
  R: 550 No such user here
• S: DATA
  R: 354 Start mail input; end with <CRLF>.<CRLF>
  S: Blah blah blah…
  S: etc. etc. etc.
  S: <CRLF>.<CRLF>
  R: 250 OK
• S: QUIT
Format for Text Messages
RFC 822
• Message viewed as having envelope and
  contents
• Envelope contains information required to
  transmit and deliver message
• Message is sequence of lines of text
  —Uses general memo framework
  —Header usually keyword followed by colon followed
   by arguments
 RFC 822 Headers
Received: By OpenMail Mailer;Mon, 16 Oct 2006 09:32:54 +0800 (CST)
From: "Yen-Cheng Chen" <ycchen@ncnu.edu.tw>
Reply-To: ycchen@ncnu.edu.tw
Subject: RFC 822 Header Example
Message-ID: <1160962374.7261.ycchen@ncnu.edu.tw>
To: ycchen@im.ncnu.edu.tw,"Yen-Cheng Chen" <ycchen@ncnu.edu.tw>
Date: Mon, 16 Oct 2006 09:32:54 +0800 (CST)
MIME-Version: 1.0
Return-Path: ycchen@ncnu.edu.tw
Disposition-Notification-To: "Yen-Cheng Chen" <ycchen@ncnu.edu.tw>
X-Priority: 1
X-MSMail-Priority: High
Content-Type: text/plain; charset=big5
Content-Transfer-Encoding: quoted-printable

Dear Professor Chen:
…
smtpClient.java                    http:///ycchen.im.ncnu.edu.tw/net2011/smtpClient.java


import java.io.*;
import java.net.*;
public class smtpClient {
     public static void main(String[] args) {
          Socket smtpSocket = null;
          DataOutputStream os = null;
          DataInputStream is = null;
          BufferedReader br = null;
          String responseLine;
          try {
                smtpSocket = new Socket("smtp.ncnu.edu.tw", 25);
                os = new DataOutputStream(smtpSocket.getOutputStream());
                is = new DataInputStream(smtpSocket.getInputStream());
                br = new BufferedReader(new InputStreamReader(is));
          } catch (UnknownHostException e) {
                System.err.println("Don't know about host: hostname");
          } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: smtp.ncnu.edu.tw");
          }
        if (smtpSocket != null && os != null && is != null) {
             try {
                   os.writeBytes("HELO\n");
                   os.writeBytes("MAIL FROM: sender@ncnu.edu.tw\n");
                   os.writeBytes("RCPT TO: receiver@ncnu.edu.tw\n");
                   os.writeBytes("DATA\n");
                   os.writeBytes("From: sender@ncnu.edu.tw\n");
                   os.writeBytes("To: receiver@ncnu.edu.tw\n");
                   os.writeBytes("Subject: A test mail\n\n");
                   os.writeBytes("Dear Professor Chen,\n\n"); // message body
                   os.writeBytes("How are you?\n"); // message body
                   os.writeBytes("Best regards,\n\nFred\n"); // message body
                   os.writeBytes("\r\n.\r\n");
                   os.writeBytes("quit\n");
                   while ((responseLine = br.readLine()) != null) {
                         System.out.println("Server: " + responseLine);
                   }
                   os.close();
                   br.close();
                   is.close();
                   smtpSocket.close();
             } catch (UnknownHostException e) {
                   System.err.println("Trying to connect to unknown host: " + e);
             } catch (IOException e) {
                   System.err.println("IOException: " + e);
             }
        }
    }
}
C:\Java>javac smtpClient.java
C:\Java>java smtpClient
Server: 220 sunday.ncnu.edu.tw ESMTP Service(NCNU EMail Ver. 0.3) ready Mon, 02 Oct
2006 10:32:32 +0800 (CST)
Server: 250 sunday.ncnu.edu.tw
Server: 250 Sender <sender@ncnu.edu.tw> OK
Server: 250 Recipient <receiver@ncnu.edu.tw> OK
Server: 354 Please start mail input.
Server: 250 Mail queued for delivery.
Server: 221 Closing connection. Good bye.
Multipurpose Internet Mail
Extension (MIME)
• Extension to RFC822
• SMTP can not transmit executables
   — Uuencode and other schemes are available
       • Not standardized
• Can not transmit text including international characters
  (e.g. â, å, ä, è, é, ê, ë)
   — Need 8 bit ASCII
• Servers may reject mail over certain size
• Translation between ASCII and EBCDIC not standard
• SMTP gateways to X.400 can not handle none text data
  in X.400 messages
• Some SMTP implementations do not adhere to standard
   — CRLF, truncate or wrap long lines, removal of white space, etc.
Overview of MIME
• Five new message header fields
  —MIME version
  —Content type
  —Content transfer encoding
  —Content Id
  —Content Description
• Number of content formats defines
• Transfer encoding defined
Content Types
• Text
   — Plain
• Multipart
   — Mixed, Parallel, Alternative, Digest
• Message
   — RFC 822, Partial, External-body
• Image
   — jpeg, gif
• Video
   — mpeg
• Audio
   — Basic
• Application
   — Postscript
   — octet stream
multipart/mixed
...
From: "Yen-Cheng Chen" <ycchen@ncnu.edu.tw>
To: <ycchen@im.ncnu.edu.tw>
Subject: test mail!
Date: Wed, 22 May 2002 14:27:07 +0800
MIME-Version: 1.0
Content-Type: multipart/mixed;
       boundary="----=_NextPart_000_0047_01C2019E.69F32C40"
Status:
This is a multi-part message in MIME format.
------=_NextPart_000_0047_01C2019E.69F32C40
Content-Type: text/plain; charset="big5"
Content-Transfer-Encoding: 7bit
Start of Body!
Yen-Cheng
------=_NextPart_000_0047_01C2019E.69F32C40
Content-Type: text/plain; name="a.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
       filename="a.txt“
This is the first and only one line of the file.
------=_NextPart_000_0047_01C2019E.69F32C40--
Content-Transfer-Encoding
• Reliable delivery across wide range of environments
• Content transfer encoding field
   — Six values
   — Three (7bit, 8bit, binary) no encoding done
       • Provide info about nature of data
• Quoted-printable
   — Data largely printable ASCII characters
   — Non-printing characters represented by hex code
• Base64
   — Maps arbitrary binary input onto printable output
• X-token
   — Named nonstandard encoding
 Content-Transfer-Encoding
  • Content-Transfer-Encoding: base64
                          24 bits


  0 0
            0 0
                         0 0
                                       0 0


Radix-64          0~63  A~Za~z0~9+/



                        32 bits
Quoted-printable
Content-Type: text/plain; charset="big5"
Content-Transfer-Encoding: quoted-printable


   一年=365天
   =A4@=A6~=3D365=A4=D1

   暨南大學
   =BA[=ABn=A4j=BE=C7

   X=Y
   X=3DY
Discussion
•   Mail Relay
•   Junk mail
•   Virus Spreading via mails
•   Mail Security: authentication, privacy, integrity,…
•   MIME applications
    — .mht (mht file format for Web pages)
    — MMS, WAP Push Messages, …
• Developing mail applications
POP3
Post Office Protocol - Version 3
• RFC 1939 / Std 53
• To retrieve emails from server (POP3 server)
• Three states
   —Authorization State
   —Transaction State
   —Update State
POP3 Commands
• Authorization State    • Update State
    — USER username        — QUIT
    — PASS password
    — APOP name digest
•   Transaction State
    —   STAT
    —   LIST [msgNo]
    —   RETR msgNo
    —   DELE msgNo
    —   NOOP
    —   RSET
    —   TOP msgNo line
    —   UIDL [msgNo]
S:   +OK POP3 server ready <1896.697170952@dbc.mtview.ca.us>
C:   USER mrose
S:   +OK User name accepted, password please
C:   PASS pwd999
S:   +OK Mailbox open, 2 messages (320 octets)
C:   STAT
S:   +OK 2 320
C:   LIST
S:   +OK 2 messages (320 octets)
S:   1 120
S:   2 200
S:   .
C:   RETR 1
S:   +OK 120 octets
S:   <the POP3 server sends message 1>
S:   .
C:   DELE 1
S:   +OK message 1 deleted
C:   QUIT
S:   +OK dewey POP3 server signing off (maildrop empty)
delSpam.java (1/4)               http://ycchen.im.ncnu.edu.tw/net2011/delSpam.java

import java.io.*;                               C:\java>javac delSpam.java
import java.net.*;
public class delSpam {
                                                C:\java>java delSpam password
   public static void main(String[] args) {
        Socket popSocket = null;
        DataOutputStream os = null;
        DataInputStream is = null;
        BufferedReader br = null;
        String responseLine;
        try {
            popSocket = new Socket(“pop3.ncnu.edu.tw", 110);
            os = new DataOutputStream(popSocket.getOutputStream());
            is = new DataInputStream(popSocket.getInputStream());
            br = new BufferedReader(new InputStreamReader(is));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: pop.ncnu.edu.tw");
        }
delSpam.java (2/4)                              yourUserName@ncnu.edu.tw

   if (popSocket != null && os != null && is != null) {
      try {
          System.out.println("Server: "+ br.readLine());
          os.writeBytes("user yourUserName\n");
          System.out.println("Server: "+ br.readLine());
          os.writeBytes("pass "+ args[0]+"\n");
          System.out.println("Server: "+ br.readLine());
          os.writeBytes("stat\n");
          String num[] = new String[3];
          num = (br.readLine()).split("\\s");
          System.out.println("Number of mails: " + num[1]);
          int No = Integer.parseInt(num[1]);
delSpam.java (3/4)
         for (int i=1;i<=No;i++) {
             os.writeBytes("top " + i+ " 0\n");
             responseLine = br.readLine();                垃圾信件(unsolicited email)
             boolean isDel = false;
             while (!responseLine.equals(".")) {
                  if (responseLine.startsWith("Subject: [SPAM ")) {
                  isDel= true;
                  }
                  if (responseLine.startsWith("Subject")) {
                       System.out.println(i+": " + responseLine.substring(9,
                       Math.min(38, responseLine.length())));
                  }
                  if (responseLine.startsWith("To")) {
                       System.out.println(i+": " + responseLine);
                  }
                  responseLine = br.readLine();
             }
             if (isDel) {
                  os.writeBytes("dele " + i+ "\n");
                  System.out.println("Server: "+ br.readLine());
             }
     }
delSpam.java (4/4)

                   os.writeBytes("quit\n");
                   System.out.println("\nServer: " + br.readLine());
                   os.close();
                   br.close();
                   is.close();
                   popSocket.close();
               } catch (UnknownHostException e) {
                   System.err.println("Trying to connect to unknown host: " + e);
               } catch (IOException e) {
                   System.err.println("IOException: " + e);
               }
           }
       }
   }
C:\Java>java delSpam mypassword
Server: +OK POP3 mcu.edu.tw 2004.89 server ready
Server: +OK User name accepted, password please
Server: +OK Mailbox open, 6 messages
Number of mails: 6
1: To: slchang@mcu.edu.tw
1: [SPAM MAIL]
Server: +OK Message deleted
2: [SPAM MAIL] =?Big5?B?pnCqR7F6
2: To: yencheng@mcu.edu.tw
Server: +OK Message deleted
3: [SPAM MAIL] =?iso-2022-jp?B?G
3: To: <yencheng@mcu.edu.tw>
Server: +OK Message deleted
4: To: cyfan@mcu.edu.tw
4: [SPAM MAIL] LV
Server: +OK Message deleted
5: To: yencheng@mcu.edu.tw
5: [SPAM MAIL] fancy
Server: +OK Message deleted
6: [SPAM MAIL]
6: To: hjchen@mcu.edu.tw
Server: +OK Message deleted

Server: +OK Sayonara
gmail 用戶端設定
http://support.google.com/mail/bin/answer.py?hl=zh-Hant&answer=13287

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:11/5/2012
language:English
pages:58