关于DNS协议的具体内容可以查看RFC1035,如果英语不太好我推荐下面这个人的译文:
http://blog.csdn.net/tigerjibo/article/details/6827736 翻译的足够详尽了。
那么来看下Go的实现:
package main
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"strings"
)
type DNSHeader struct {
ID uint16
Flag uint16
QuestionCount uint16
AnswerRRs uint16 //RRs is Resource Records
AuthorityRRs uint16
AdditionalRRs uint16
}
func (header *DNSHeader) SetFlag(QR uint16, OperationCode uint16, AuthoritativeAnswer uint16, Truncation uint16, R