Chương 2 (tiếp). Giao thức tầng ứng dụng (application layer)

1a. http client thiết lập liên kết TCP với http server (process) tại địa chỉ www.uct2.edu.vn, cổng 80 (ngầm định với http server). 2. http client gửi http request message (bao gồm cả URL) tới TCP connection socket

pdf22 trang | Chia sẻ: lylyngoc | Lượt xem: 1621 | Lượt tải: 2download
Bạn đang xem trước 20 trang tài liệu Chương 2 (tiếp). Giao thức tầng ứng dụng (application layer), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chương 2 (tiếp). Giao thức tầng ứng dụng (application layer) Trần Quang Hải Bằng Computer Networking and Internet (course of 2009) Faculty of Information Technology University of Communication and Transport (Unit 2) Office location: Administration building, Block D3, Room 6 Office phone: 38962018 Cell phone: N/A Email: bangtqh@hotmail.com Chapter 2. The Application Layer 224/8-25/10/2009 Discussion Web programming with ASP. Visual Basic programming. § Object oriented programming. § Event driven programming. § Component driven programming. TCP Socket programming. § Simple web server. Chapter 2. The Application Layer 324/8-25/10/2009 Winsock Chapter 2. The Application Layer 424/8-25/10/2009 HTTP example 1a. http client thiết lập liên kết TCP với http server (process) tại địa chỉ www.uct2.edu.vn, cổng 80 (ngầm định với http server). 2. http client gửi http request message (bao gồm cả URL) tới TCP connection socket 1b. http server chấp nhận kết nối rồi thông báo với client. 3. http server nhận request message, tạo ra http response message có chứa các đối tượng được yêu cầu rồi gửi vào socket. time Giả sử người dùng truy cập URL: www.uct2.edu.vn/index.htm (trang web có text và 10 hình ảnh jpeg) 0. http server tại máy phục vụ www.uct2.edu.vn chờ yêu cầu kết nối TCP tại cổng 80. Chapter 2. The Application Layer 524/8-25/10/2009 5. http client nhận response message có chứa html file, hiển thị html. Sau đó, phân tích html file, tìm URL của 10 hình ảnh jpeg trong tài liệu. 6. Bước 1-5 được lặp lại với từng hình ảnh. 4. http server ngắt liên kết. time Chapter 2. The Application Layer 624/8-25/10/2009 Ch2. The Application Layer 2.1. Một số khái niệm và nguyên tắc. 2.2. Web & Hyper Text Transfer Protocol. 2.3. Web design and HTTP, Web programming. 2.4. File Transfer Protocol. 2.5. Electronic Mail Protocols. 2.6. Domain Name System. Chapter 2. The Application Layer 724/8-25/10/2009 2.4. FTP - File Transfer Protocol n Truyền/tải tệp (to/from remote host). n Client/server model ¤ Client: đưa ra yêu cầu truyền tải. ¤ Server = remote host. n FTP: RFC 959 file transfer FTP server FTP user interface FTP client local file system remote file system user at host Chapter 2. The Application Layer 824/8-25/10/2009 FTP: control & data connections n FTP sử dụng TCP. n FTP sử dụng đồng thời 2 liên kết TCP tại 2 cổng: ¤ TCP control connection, port 21: trao đổi các thông điệp điều khiển (commands, responses…). ¤ TCP data connection, port 20: truyền tải tệp. n FTP lưu giữ trạng thái client trong phiên làm việc (state vs. HTTP is stateless). FTP client FTP server TCP control connection port 21 TCP data connection port 20 Chapter 2. The Application Layer 924/8-25/10/2009 FTP: Quá trình trao đổi/truyền tải FTP server nghe tại cổng 21. FTP client yêu cầu kết nối với FTP server qua TCP tại cổng 21. Gửi user & password để đăng nhập. FTP server chấp nhận, liên kết điều khiển (control connection) được thiết lập. Quá trình trao đổi có thể bắt đầu. Khi server nhận được lệnh truyền tệp, nó mở liên kết dữ liệu (data connection) tới client, tệp được truyền qua liên kết này. Sau khi truyền xong một tệp, server ngắt liên kết dữ liệu (mỗi liên kết chỉ sử dụng để truyền một tệp). client server TCP control connection port 21 TCP data connection port 20 Chapter 2. The Application Layer 1024/8-25/10/2009 FTP commands, responses Sample commands: n sent as ASCII text over control channel n USER username n PASS password n LIST return list of file in current directory n RETR filename retrieves (gets) file n STOR filename stores (puts) file onto remote host Sample return codes n status code and phrase (as in HTTP) n 331 Username OK, password required n 125 data connection already open; transfer starting n 425 Can’t open data connection n 452 Error writing file Lệnh (commands), phúc đáp (responses) được truyền dạng ASCII Chapter 2. The Application Layer 1124/8-25/10/2009 FTP clients & servers n Command-based client: ¤ Windows FTP command. ¤ Linux FTP command. n GUI clients ¤ Windows Commander. ¤ CuteFTP, WS_FTP… n Microsoft FTP Service n … file transfer FTP server FTP user interface FTP client local file system remote file system user at host Chapter 2. The Application Layer 1224/8-25/10/2009 Ch2. The Application Layer 2.1. Một số khái niệm và nguyên tắc. 2.2. Web & Hyper Text Transfer Protocol. 2.3. Web design and HTTP, Web programming. 2.4. File Transfer Protocol. 2.5. Electronic Mail Protocols. 2.6. Domain Name System. Chapter 2. The Application Layer 1324/8-25/10/2009 2.5. Electronic Mail Ba thành phần chính: n User agents (mail clients) ¤ Soạn, đọc thư (messages). ¤ Vd: Outlook, Eudora, Netscape Messenger... n Mail servers ¤ Lưu trữ, xử lý thư. ¤ Vd: Exchange, MDeamon… n Protocols ¤ Simple Mail Transfer Protocol (SMTP). ¤ Post Office Protocol (POP). ¤ Internet Mail Access Protocol (IMAP). ¤ HTTP. user mailbox outgoing message queue mail server user agent user agent user agent mail server user agent user agent mail server user agent SMTP SMTP SMTP Chapter 2. The Application Layer 1424/8-25/10/2009 Alice Bob 1) Alice sử dụng UA soạn thư, đ/c gửi tới “to”: bob@yahoo.com. 2) Alice’s UA gửi thư đến mail server của Alice, bức thư được xếp vào hàng đợi (message queue). 3) Mail server của Alice mở liên kết TCP tới mail server của Bob. 4) Mail server của Alice gửi thư qua liên kết TCP. 5) Mail server của Bob cất thư nhận được vào hòm thư của Bob. 6) Bob dùng UA để lấy thư từ server về rồi đọc thư. user agent mail server mail server user agent 1 2 3 4 5 6 uct2.edu.vn yahoo.com Outlook Yahoo! Mail Chapter 2. The Application Layer 1524/8-25/10/2009 SMTP (Simple Mail Transfer Protocol) n Sử dụng liên kết TCP (port 25) để gửi mails: ¤ Từ sender’s mail client tới sender’s mail server. ¤ Từ sender’s mail server tới receiver’s mail server. n Ba pha (three ways handshake): ¤ Bắt tay (handshaking/greeting). ¤ Trao đổi messages. ¤ Kết thúc. n Command/response: ¤ commands: ASCII. ¤ responses: status code & phrase. Chapter 2. The Application Layer 1624/8-25/10/2009 SMTP: sample interaction (C: client; S: server) S: 220 yahoo.com C: HELO uct2.edu.vn S: 250 Hello uct2.edu.vn, pleased to meet you C: MAIL FROM: S: 250 alice@uct2.edu.vn... Sender ok C: RCPT TO: S: 250 bob@yahoo.com ... Recipient ok C: DATA S: 354 Enter mail, end with "." on a line by itself C: Hi Bob! C: Would you like to play tennis this evening? C: . S: 250 Message accepted for delivery C: QUIT S: 221 yahoo.com closing connection Chapter 2. The Application Layer 1724/8-25/10/2009 SMTP: more n SMTP sử dụng persistent connection: một liên kết, nhiều thông điệp. n Thông điệp (kể cả nội dung thư) phải được mã hoá dạng ASCII 7 bit (US-ASCII). n Đánh dấu kết thúc msg bởi CRLF.CRLF n Một số dữ liệu nhị phân (vd: picture) có thể chứa CRLF.CRLF cần mã hoá để đảm bảo không có dữ liệu nào trùng với ký hiệu kết thúc message (vd: base64). n So sánh với HTTP: ¤ HTTP: pull protocol SMTP: push protocol. ¤ Cùng sử dụng ASCII (command/req/resp/status code). ¤ HTTP: mỗi obj chứa trong một msg SMTP: cho phép nhiều obj (text, picture…) trong một msg (multipart msg). Chapter 2. The Application Layer 1824/8-25/10/2009 Mail message format SMTP: giao thức trao đổi email msg (exchange msgs). RFC 822: standard for text message format: n Header lines, vd: ¤ To: ¤ From: ¤ Subject: Khác với smtp commands (MAIL FROM, RCPT TO,…)! n Body ¤ The “message”. ¤ ASCII header body blank line Chapter 2. The Application Layer 1924/8-25/10/2009 MIME (Multipurpose Internet Mail Extension) n RFC 822: text msg, gặp khó khăn với multimedia. n MIME: RFC 2045, RFC 2056. n So với RFC 822, MIME thêm vào các header lines để mô tả nội dung của email msg. From: alice@uct2.edu.vn To: bob@yahoo.com Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data ..... ......................... ......base64 encoded data multimedia data type, subtype, parameter declaration method used to encode data MIME version encoded data Chapter 2. The Application Layer 2024/8-25/10/2009 MIME: Content types Content-type: type/subtype; parameters Chapter 2. The Application Layer 2124/8-25/10/2009 MIME: Encoding types Content-Transfer-Encoding: Encoding type Chapter 2. The Application Layer 2224/8-25/10/2009 From: alice@uct2.edu.vn To: bob@yahoo.com Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=StartOfNextPart --StartOfNextPart Content-Transfer-Encoding: quoted-printable Content-Type: text/plain Dear Bob, Please find a picture of a crepe. --StartOfNextPart Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data ..... ......................... ......base64 encoded data --StartOfNextPart MIME: Multiple objects mail message Chapter 2. The Application Layer 2324/8-25/10/2009 The received mail message n Server thêm vào dòng header mỗi khi nhận thư: ¤ Received: ¤ Return-path: n Mail có thể được chuyển qua nhiều server khác nhau (forward). Received: from hamburger.edu by sushi.jp; 12 Oct 98 15:30:01 GMT Received: from crepes.fr by hamburger.edu; 12 Oct 98 15:27:39 GMT From: alice@crepes.fr To: bob@hamburger.edu Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data ....... ........................................ .......base64 encoded data Chapter 2. The Application Layer 2424/8-25/10/2009 Hiển thị full headers với Y! Mail 1.Mail Options 2.General Preference 3.Messages: Show all… Chapter 2. The Application Layer 2524/8-25/10/2009 Mail access protocol n SMTP: giao thức gửi mail tới server. n Mail access protocol: giao thức nhận mail từ server. ¤ POP (Post Office Protocol): RFC 1939. TCP Port 110. ¤ IMAP (Internet Mail Access Protocol): RFC 1730. TCP port 143. ¤ HTTP: Hotmail, Yahoo!... user agent sender’s mail server user agent SMTP SMTP access protocol receiver’s mail server Chapter 2. The Application Layer 2624/8-25/10/2009 POP3 protocol [1] Authorization phase n client commands: ¤ user: declare username ¤ pass: password n server responses ¤ +OK ¤ -ERR [2] Transaction phase, client: n list: list message numbers n retr: retrieve message by number n dele: delete [3] Update phase: n Quit n những thay đổi (dele) được cập nhật S: +OK POP3 server ready C: user bob S: +OK C: pass hungry S: +OK user successfully logged on C: list S: 1 498 S: 2 912 S: . C: retr 1 S: S: . C: dele 1 C: retr 2 S: S: . C: dele 2 C: quit S: +OK POP3 server signing off [1] [2] [3] Chapter 2. The Application Layer 2724/8-25/10/2009 POP3 (more) and IMAP More about POP3 n Chế độ “download and delete” (xem vd trước): ¤ Msg bị xoá khi thoát. ¤ Không thể nhận msg lần nữa. n “Download-and-keep”: ¤ Msg được giữ lại server. ¤ Có thể nhận msg nhiều lần từ nhiều client. n Không lưu trạng thái giữa các phiên làm việc (stateless). IMAP4 n Lưu trữ toàn bộ msg tại server. n Cho phép người dùng tổ chức lưu trữ trên server (tạo thư mục, di chuyển msg…) n Lưu trạng thái giữa các phiên (state): ¤ Thư mục tự tạo. ¤ Các msgs đã di chuyển. Chapter 2. The Application Layer 2824/8-25/10/2009 Ch2. The Application Layer 2.1. Một số khái niệm và nguyên tắc. 2.2. Web & Hyper Text Transfer Protocol. 2.3. Web design and HTTP, Web programming. 2.4. File Transfer Protocol. 2.5. Electronic Mail Protocols. 2.6. Domain Name System. Chapter 2. The Application Layer 2924/8-25/10/2009 2.6. DNS (Domain Name System) n Địa chỉ IP (32 bit) được sử dụng để xác định một nút trong mạng TCP/IP: 210.245.22.125 khó nhớ! Sử dụng tên (host name) song song với IP address. n DNS (RFC 1034, RFC 1035, RFC 2136): ¤ Chuyển host name IP address. From: N.V.Toan To: Phòng ðT Phòng ðT => D3 HTTP (Internet Explorer) www.uct2.edu.vn => 210.245.22.125 DNS client www.uct2.edu.vn 210.245.22.125 DNS servers Mr Toan Ms Phuong Chapter 2. The Application Layer 3024/8-25/10/2009 DNS services DNS không tương tác trực tiếp với users mà cung cấp các dịch vụ cho các chương trình khác (HTTP, FTP…) thuộc tầng ứng dụng: n Host name IP (hostname to IP resolve). n Host aliasing. ¤ Một máy tính có tên chính thức và có thể có bí danh (alias). n Mail server aliasing. ¤ @uct2.edu.vn mail server address = ? n Load distribution (phân tán tải) ¤ Một dịch vụ web (cnn.com, yahoo.com…) có thể được cung cấp tại nhiều server có IP khác nhau để phân tán tải do lượng người sử dụng cao. Chapter 2. The Application Layer 3124/8-25/10/2009 Internet domain name space n Domain name (tên miền): ¤ Miền = tập hợp các nút mạng có cùng một nút cha trong sơ đồ trên. ¤ Vd: ¤ uct2.edu.vn; yahoo.com; yale.edu. ¤ Sub-domain: cs.yale.edu; n Host name: www.uct2.edu.vn; mail.uct2.edu.vn ¤ vn: Vietnam. ¤ edu: education. ¤ uct2: University of Commnication & Transport – unit 2. ¤ www: máy chủ world wide web. ¤ mail: máy chủ mail. vn Chapter 2. The Application Layer 3224/8-25/10/2009 Domain name servers Vd: client muốn biết IP của www.amazon.com thì: n Tìm địa chỉ của com DNS servers hỏi Root DNS servers. n amazon.com com DNS servers. n www.amazon.com amazon.com DNS servers. Root DNS Servers com DNS servers org DNS servers edu DNS servers poly.edu DNS servers umass.edu DNS servers yahoo.com DNS servers amazon.com DNS servers pbs.org DNS servers Chapter 2. The Application Layer 3324/8-25/10/2009 DNS: Root name servers 13 root name servers worldwide b USC-ISI Marina del Rey, CA l ICANN Los Angeles, CA e NASA Mt View, CA f Internet Software C. Palo Alto, CA (and 17 other locations) i Autonomica, Stockholm (plus 3 other locations) k RIPE London (also Amsterdam, Frankfurt) m WIDE Tokyo a Verisign, Dulles, VA c Cogent, Herndon, VA (also Los Angeles) d U Maryland College Park, MD g US DoD Vienna, VA h ARL Aberdeen, MD j Verisign, ( 11 locations) n Khi local name server không thể chuyển host name IP, gửi yêu cầu tới root srv. n root name server: ¤ Liên lạc với authoritative name server nếu chưa thể ánh xạ đ/c ngay. ¤ Lấy kết quả ánh xạ gửi về từ authoritative name server. ¤ Trả kết quả về cho local name server. Chapter 2. The Application Layer 3424/8-25/10/2009 TLD and Authoritative Servers n Top-level domain (TLD) servers: chịu trách nhiệm về các miền com, org, net, edu… và uk, fr, ca, jp… ¤ Network Solutions: quản lý com TLD servers. ¤ Educause: quản lý edu TLD servers. n Authoritative DNS servers: ¤ Mỗi host trong mạng muốn được biết đến phải được khai báo với một authoritiative DNS server. ¤ DNS servers của các tổ chức ánh xạ hostname IP cho các hosts của tổ chức đó (vd: web, mail…). ¤ Có thể được quản lý bởi các tổ chức hay nhà cung cấp dịch vụ. Chapter 2. The Application Layer 3524/8-25/10/2009 Local name server n Thuộc quản lý của ISP (ISP, company, university…). n Khi một host muốn biết IP của một host nào đó, trước tiên, nó “hỏi” local name server. n Không nhất thiết phải nằm trong kiến trúc DNS servers (độc lập). n “Gần” máy tính của người sử dụng nhất nên thường được “hỏi thăm” đầu tiên mỗi khi có truy vấn DNS. n Xem Windows Local DNS: ipconfig Chapter 2. The Application Layer 3624/8-25/10/2009 DNS example 1 Tình huống: host surf.eurecom.fr cần đ/c IP của gaia.cs.umass.edu 1. Liên lạc với local DNS server: dns.eurecom.fr 2. dns.eurecom.fr liên lạc với root name server (nếu cần) 3. root name server liên lạc với authoritative name server: dns.umass.edu (nếu cần) requesting host surf.eurecom.fr gaia.cs.umass.edu root name server Authorititive name server dns.umass.edu local name server dns.eurecom.fr 1 2 3 4 5 6 Chapter 2. The Application Layer 3724/8-25/10/2009 DNS example 2 Root name server có thể: n Không biết authoritative name server (ans). n Biết intermediate name server (ins) ¤ Từ ins có thể tìm ra ans requesting host surf.eurecom.fr gaia.cs.umass.edu root name server local name server dns.eurecom.fr 1 2 3 4 5 6 authoritative name server dns.cs.umass.edu intermediate name server dns.umass.edu 7 8 Chapter 2. The Application Layer 3824/8-25/10/2009 DNS example 3 recursive query: n A hỏi B, B hỏi C, C hỏi D,… rồi trả lời ngược lại. n Server được hỏi chịu tải nặng gấp đôi? iterative query: n DNS server được hỏi gợi ý hỏi một server khác. n “I don’t know this name, but ask this server” requesting host surf.eurecom.fr gaia.cs.umass.edu root name server local name server dns.eurecom.fr 1 2 3 4 5 6 authoritative name server dns.cs.umass.edu intermediate name server dns.umass.edu 7 8 iterated query Chapter 2. The Application Layer 3924/8-25/10/2009 DNS: caching & updating n Mỗi khi DNS server nhận được yêu cầu tra cứu IP, IP mà nó tìm được sẽ được lưu lại phục vụ cho những lần tra cứu tiếp theo. ¤ Timeout: thời gian hết hạn (sau một khoảng thời gian, bản ghi cache tương ứng bị xoá). ¤ TLD servers thường được cache tại local name servers. n Cơ chế cập nhật/thông báo được đưa ra bởi IETF: ¤ RFC 2136 ¤ Chapter 2. The Application Layer 4024/8-25/10/2009 DNS records n Type=A ¤ name = hostname ¤ value = IP address n Type=NS ¤ name = domain name ¤ value = IP address của authoritative name server cho domain này n Type=CNAME ¤ name = “bí danh” (alias). www.ibm.com ¤ value = tên thật. servereast.backup2.ibm.com n Type=MX ¤ name = domain name ¤ value = tên của mail server DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) Chapter 2. The Application Layer 4124/8-25/10/2009 DNS protocol, messages n Msg header (12 bytes) n Identification: số 16 bit reply msg sử dụng cùng id với req msg. n Flags: nhiều bit, mỗi bit có một ý nghĩa: ¤ Query (0) hay reply (1) ¤ Authoritative DNS. ¤ recursion desired: client mong muốn DNS server tra cứu đệ quy. ¤ recursion available: server hỗ trợ tra cứu đệ quy. DNS Protocol: Query/reply messages có cùng format. Chapter 2. The Application Layer 4224/8-25/10/2009 DNS messages Name, type fields for a query RRs in reponse to query records for authoritative servers additional “helpful” info that may be used Chapter 2. The Application Layer 4324/8-25/10/2009 Summary √ √ √ √ ? ? Telnet! Terminal Emulation Web programming TCP Socket Programming RTSP? Chapter 2. The Application Layer 4424/8-25/10/2009 Bài tập tự làm và thảo luận n Tìm hiểu các câu lệnh cơ bản của FTP, SMTP, POP3. (Nộp báo cáo sau 1 tuần) n Viết mail client đơn giản (không bắt buộc).
Tài liệu liên quan