---
# System prepended metadata

title: Domain Name Server

---

# Domain Name Server
https://www.rfc-editor.org/rfc/rfc1035



![](https://i.imgur.com/4G7PcbA.png)

![](https://i.imgur.com/IdCLJPX.png)
上圖: 用zone transfer protocol of DNS查看updates

DNS要做的事:
- 從local file system讀master file，來得到關於zone的資訊
- 回答從別的resolver來的queries
- 檢查updates

![](https://i.imgur.com/0PuFryz.png)

#### 大小限制:
- labels <= 63 bytes
- names <= 255 bytes
- TTL: signed 32 bit number, positive
- UDP訊息 <= 512 bytes

#### Name space 定義:
- domain name的總長不能多於255 bytes
- 每個label為一個byte來表示長度(後面還有幾個)byte，後面放label
- domain name表示長度的byte中，前兩個bit為0，剩下 6 bits要限制label頂多有63 bytes

## RR (Resource Record)
### 格式
![RR format](https://i.imgur.com/vmK0GW5.png)
- NAME: 擁有者名字，即擁有resouce record的node名字。
- TYPE: 包含其中一個RR TYPE code的兩個bytes。
- CLASS: 包含其中一個RR CLASS code的兩個bytes。
- TTL: 
- RDLENGTH: unsigned 16 bit int，表示RDATA有幾個octets
- RDATA: 變動長度，一串bytes，描述resorce。會根據resource record的type和class而有不同格式。

### TYPE values
以下僅列出需要實作的。
```
A     :  1, a host address in IPv4
AAAA  :  ?, a host address in IPv6
NS    :  2, an authoritative name server 
CNAME :  5, the canonical name for an alias
SOA   :  6, marks the start of a zone of authority
MX    : 15, mail exchange
TXT   : 16,, text strings 
```

### QTYPE values
QTYPES在query的問題區出現，是TYPE的superset，所以所有TYPES都是合法的QTYPEs。

```
AXFR            252 要求傳輸一個完整的zone

MAILB           253 要求和mailbox相關的records (MB, MG or MR)

MAILA           254 要求mail agent RRs (Obsolete - see MX)

*               255 要求所有record
```
### CLASS values
```
IN              1 the Internet
CS              2 the CSNET class (Obsolete - used only for examples in some obsolete RFCs)
CH              3 the CHAOS class
HS              4 Hesiod [Dyer 87]
```
### QCLASS values
QCLASS在query的問題區出現，是CLASS的superset，所以所有CLASS都是合法的QCLASS。
`*               255 any class`
## 標準RR

### CNAME RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                     CNAME                     /
    /                                               /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
放owner的canonical或primary name，是別名(alias)。

???
CNAME RRs cause no additional section processing, but name servers may
choose to restart the query at the canonical name in certain cases.  See
the description of name server logic in [RFC-1034] for details.
???

### MX RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                  PREFERENCE                   |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                   EXCHANGE                    /
    /                                               /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

- PREFERENCE: 16 bit int，放同個owner給這個RR的preference，數字越低順序越前面。
- EXCHNANGE: 一個 <domain-name>，表示一個host願意做為給owner名字mail exchange的人。
> MX records cause type A additional section processing for the hostspecified by EXCHANGE.  The use of MX RRs is explained in detail in [RFC-974].
  
### NS RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                   NSDNAME                     /
    /                                               /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- NSDNAME: 一個<domain-name>，用來表示要當特定class和domain的authoritive。

NS records cause both the usual additional section processing to locate
a type A record, and, when used in a referral, a special search of the
zone in which they reside for glue information.

The NS RR states that the named host should be expected to have a zone
starting at owner name of the specified class.  Note that the class may
not indicate the protocol family which should be used to communicate
with the host, although it is typically a strong hint.  For example,
hosts which are name servers for either Internet (IN) or Hesiod (HS)
class information are normally queried using IN class protocols.0
    
### SOA RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                     MNAME                     /
    /                                               /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                     RNAME                     /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    SERIAL                     |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    REFRESH                    |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                     RETRY                     |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    EXPIRE                     |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    MINIMUM                    |
    |                                               |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    

### TXT RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    /                   TXT-DATA                    /
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    
    
### A RDATA 格式
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    ADDRESS                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+