CAN201 W3
This’s note is about the lecture and lab of Week2 CAN201.
The lecture III - Application Layer
.
- Domain Name System (DNS)
- P2P Applications
- Socket Programming 套接字编程
Lecture
DNS
aka domain name system
- Application-layer protocol: DNS是一个应用层协议
- C/S architeture client-server architeture 不是p2p的
- UDP (port53) 传输方式(无保障 unreliable) 方便提取 不需要握手🤝
- hosts, name servers communicate to resolve names (name/address translation)
- Distributed database implemented in
hierarchy
of many name servers 分布式数据库在多个名称服务器的层次结构中实现 分层化命名可以减小重名的可能性 分布数据库解决名字到ip解析的关系,方便维护
services, structure
DNS services
- Hostname to IP address translation (A) 最重要的就是翻译功能
- Host aliasing (cname)
- canonical, alias names 别名问题
- Mail server aliasing (mx)
- Load distribution
- Replicated Web servers: many IP addresses correspond ot one name
Why not centralize DNS? 为什么DNS要用分布数据库?
- Single point of failure 单点故障
- Traffic volume 交通量
- Distant centralized database 远程集中式数据库
- Maintenance 维护
a distributed, hierarchical database
Client wants IP for www.amazon.com:
- Client queries root server to find com DNS server
- Client queries .com DNS server to get amazon.com DNS server
- Client queries amazon.com DNS server to get IP address for www.amazon.com
root name servers
- contacted by local name server that can not resolve name
- root name server:
- contacts authoritative name server if name mapping not known
- get mapping
- returns mapping to local name server
域名的每一个点都是一层,根据这个点的内容来寻找对应的位置。
TLD, authoritative servers
Top-level domain (TLD) servers: 顶级域名服务器
- Responsible for com, org, net, edu, aero, jobs, museums, and all top-level country domains, e.g.: cn, uk, fr, ca, jp
- Network Solution maintains servers for .com TLD
- Educause for .edu TLD (https://net.educause.edu/)
Authoritative DNS servers: 权威DNS服务器
- Organization’s own DNS servers, providing authoritative hostname to IP mappings for organization’s named hosts 组织自己的DNS服务器,为组织的命名主机提供权威主机名到IP映射
- Can be maintained by organization or service provider 可由组织或服务提供商维护
Local DNS name server
- Does not strictly belong to hierarchy
- Each ISP (residential ISP, company, university) has one
- Also called “default name server”
- When host makes DNS query, query is sent to its local DNS server
- Has local cache of recent name-to-address translation pairs (but may be out of date!) 本地缓存最近的名称-地址转换对
- Acts as proxy, forwards query into hierarchy 充当代理,将查询转发到层次结构
DNS name resolution example
两种查询方法
- Host at XJTLU wants IP address for www.feimax.com
- Iterated query 迭代查询
- contacted server replies with name of server to contact
- “I don’t know this name, but ask this server”
- 相当于不知道之后指路,让用户去问其他服务器
- Recursive query 递归查询
- Puts burden of name resolution on contacted name server
- Heavy load at upper levels of hierarchy
- 一条路走到底,再走回来
caching, updating records
缓存,升级记录
- Once (any) name server learns mapping, it caches mapping
- Cache entries timeout (disappear) after some time (TTL) 缓存条目超时后一段时间
- TLD servers typically cached in local name servers 经常会缓存顶级服务器,所以根服务器不常被访问
- thus root name servers not often visited
- Cached entries may be out-of-date
- if name host changes IP address, may not be known Internet-wide until all TTLs expire 如果主机名更改了IP地址,可能在所有ttl过期之前都不知道
- Update/notify mechanisms proposed IETF standard 更新/通知机制提议的IETF标准
- RFC 2136
DNS records
DNS: distributed database storing resource records (RR)
RR format: (name, value, type, ttl)
Loop up a domain name
There’re several ways to deal with.
- Use
nslookup
command. - Use
whois
command.
DNS protocol, message
Query and reply messages, both with same message format. 查询和回复消息,都使用相同的消息格式。
Inserting records into DNS
- Example: new startup “feimax.com”
- Register name feimax.com at DNS register
- Normally, you don’t need to set up the NS record
- Insert A record for the IP address of your host
- Insert MX record for email
Domain name is a scarce resource! 稀缺资源
Pure P2P architecture
- no always-on server
- arbitrary end systems directly communicate
- peers change IP addresses
Examples: file distribution, streaming, VoIP
File distribution: client-server vs P2P
Q: how much time to distribute file (size F) from one server to N peers?
Peer upload/download capacity is limited resource.
Example:
client upload rate = u, F/u = 1hour, u_s = 10u, d_min >= u_s
File distribution time: client-server
- server transmission: must sequentially send (upload) N file copies:
- time to send one copy: F/u_s
- time to send N copies: NF/u_s 发送N个file的大小再除服务器的带宽
- client: each client must download file copy
- d_min = min client download rate
- max client download time: F/d_min 最慢的那个用户下载文件所用的时间
后来会线性增长
File distribution time: P2P
- server transmission: must sequentially send (upload) at least one file copies:
- time to send one copy: F/us 服务器也是有文件的,假如网络里没有人参与p2p那就是服务器直接向唯一的用户分发。
- client: each client must download file copy
- min client download time: F/dmin
- clients: as total must download NF bits
- max upload rate (limiting max download rate) is us + Sui 所有的客户端也可以作为服务器上传
P2P file distribution: BitTorrent 文件分发:比特流
- File divided into 256Kb chunks 文件分成碎片
- Peers in torrent send/receive file chunks
- Peer joining torrent:
- has no chunks, but will accumulate them over time from other peers
- registers with tracker to get list of peers, connects to subset of peers
- While downloading, peer uploads chunks to other peers
- Peer may change peers with whom it exchanges chunks
- Peers may come and go
- Once peer has entire file, it may leave or remain in torrent
BitTorrent: requesting, sending file chunks
Requesting chunks:
- at any given time, different peers have different subsets of file chunks
- Periodically, Alice asks each peer for list of chunks that they have
- Alice requests missing chunks from peers, rarest first
Sending chunks: tit for tat
- Alice sends chunks to those four peers currently sending her chunks at highest rate
- other peers are choked by Alice (do not receive chunks from her)
- re-evaluate top 4 every 10 secs
- every 30 secs: randomly select another peer, starts sending chunks
- “optimistically unchoke” this peer
- newly chosen peer may join top 4
Higher upload rate: find better trading partners, get file faster!
Socket Programming 套接字编程
Two socket types for two transport services
- UDP: unreliable datagram
- TCP: reliable, byte stream-oriented
Application Example:
- client reads a line of characters (data) from its keyboard and sends data to server
- server receives the data and converts characters to uppercase
- server sends modified data to client
- client receives modified data and displays line on its screen
Socket programming with UDP
UDP: no “connection” between client & server
- No handshaking before sending data
- Sender explicitly attaches IP destination address and port # to each packet
- Receiver extracts sender IP address and port # from received packet
UDP: transmitted data may be lost or recieved out-of-order
Application viewpoint
- UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server
详细见ppt
Lab
- File Operation
- Text file – Write / Read
- Binary file – Write /Read
- Networking Programming Using Socket
- UDP Server and Client
- TCP Server and Client
Open
open
method is exploit to operate files:
1 | f = open(file='filename.xxx', mode='r') |
File open modes
1 | ‘r’ open for reading (default) |
Write
1 | f.write(Str) # Write a str to a file |
Read
1 | f.read(n) # read n chars(including \n), n=-1 means read all |
Text file appending
1 | f = open('/Users/fei/lesson1.txt', 'a') # open the file |
Binary file read
1 | f.read(n) # read n bytes(including \n), n=-1 means read all |
Binary file write
1 | f.write(bytes) # Write some bytes to a file |
Binary file operation is very powerful, but we don’t go deep today.
Socket programming
UDP
1 | # Server side |
1 | # Client side |
TCP
1 | # Server side |
1 | # Client Side |
Lab
TCP
1 | # Server side |
UDP
1 | # Server side |
Reference
- XJTLU slides (Week3)