3 // types_generate.go is meant to run with go generate. It will use
4 // go/{importer,types} to track down all the RR struct types. Then for each type
5 // it will generate conversion tables (TypeToRR and TypeToString) and banal
6 // methods (len, Header, copy) based on the struct tags. The generated source is
7 // written to ztypes.go, and is meant to be checked into git.
22 var skipLen = map[string]struct{}{
30 // Code generated by "go run types_generate.go"; DO NOT EDIT.
41 var TypeToRR = template.Must(template.New("TypeToRR").Parse(`
42 // TypeToRR is a map of constructors for each RR type.
43 var TypeToRR = map[uint16]func() RR{
44 {{range .}}{{if ne . "RFC3597"}} Type{{.}}: func() RR { return new({{.}}) },
49 var typeToString = template.Must(template.New("typeToString").Parse(`
50 // TypeToString is a map of strings for each RR type.
51 var TypeToString = map[uint16]string{
52 {{range .}}{{if ne . "NSAPPTR"}} Type{{.}}: "{{.}}",
53 {{end}}{{end}} TypeNSAPPTR: "NSAP-PTR",
58 var headerFunc = template.Must(template.New("headerFunc").Parse(`
59 {{range .}} func (rr *{{.}}) Header() *RR_Header { return &rr.Hdr }
64 // getTypeStruct will take a type and the package scope, and return the
65 // (innermost) struct if the type is considered a RR type (currently defined as
66 // those structs beginning with a RR_Header, could be redefined as implementing
67 // the RR interface). The bool return value indicates if embedded structs were
69 func getTypeStruct(t types.Type, scope *types.Scope) (*types.Struct, bool) {
70 st, ok := t.Underlying().(*types.Struct)
74 if st.Field(0).Type() == scope.Lookup("RR_Header").Type() {
77 if st.Field(0).Anonymous() {
78 st, _ := getTypeStruct(st.Field(0).Type(), scope)
85 // Import and type-check the package
86 pkg, err := importer.Default().Import("github.com/miekg/dns")
90 // Collect constants like TypeX
91 var numberedTypes []string
92 for _, name := range scope.Names() {
93 o := scope.Lookup(name)
94 if o == nil || !o.Exported() {
97 b, ok := o.Type().(*types.Basic)
98 if !ok || b.Kind() != types.Uint16 {
101 if !strings.HasPrefix(o.Name(), "Type") {
104 name := strings.TrimPrefix(o.Name(), "Type")
105 if name == "PrivateRR" {
108 numberedTypes = append(numberedTypes, name)
111 // Collect actual types (*X)
112 var namedTypes []string
113 for _, name := range scope.Names() {
114 o := scope.Lookup(name)
115 if o == nil || !o.Exported() {
118 if st, _ := getTypeStruct(o.Type(), scope); st == nil {
121 if name == "PrivateRR" {
125 // Check if corresponding TypeX exists
126 if scope.Lookup("Type"+o.Name()) == nil && o.Name() != "RFC3597" {
127 log.Fatalf("Constant Type%s does not exist.", o.Name())
130 namedTypes = append(namedTypes, o.Name())
134 b.WriteString(packageHdr)
137 fatalIfErr(TypeToRR.Execute(b, namedTypes))
139 // Generate typeToString
140 fatalIfErr(typeToString.Execute(b, numberedTypes))
142 // Generate headerFunc
143 fatalIfErr(headerFunc.Execute(b, namedTypes))
146 fmt.Fprint(b, "// len() functions\n")
147 for _, name := range namedTypes {
148 if _, ok := skipLen[name]; ok {
151 o := scope.Lookup(name)
152 st, isEmbedded := getTypeStruct(o.Type(), scope)
156 fmt.Fprintf(b, "func (rr *%s) len(off int, compression map[string]struct{}) int {\n", name)
157 fmt.Fprintf(b, "l := rr.Hdr.len(off, compression)\n")
158 for i := 1; i < st.NumFields(); i++ {
159 o := func(s string) { fmt.Fprintf(b, s, st.Field(i).Name()) }
161 if _, ok := st.Field(i).Type().(*types.Slice); ok {
165 case `dns:"cdomain-name"`:
166 o("for _, x := range rr.%s { l += domainNameLen(x, off+l, compression, true) }\n")
167 case `dns:"domain-name"`:
168 o("for _, x := range rr.%s { l += domainNameLen(x, off+l, compression, false) }\n")
170 o("for _, x := range rr.%s { l += len(x) + 1 }\n")
172 log.Fatalln(name, st.Field(i).Name(), st.Tag(i))
178 case st.Tag(i) == `dns:"-"`:
180 case st.Tag(i) == `dns:"cdomain-name"`:
181 o("l += domainNameLen(rr.%s, off+l, compression, true)\n")
182 case st.Tag(i) == `dns:"domain-name"`:
183 o("l += domainNameLen(rr.%s, off+l, compression, false)\n")
184 case st.Tag(i) == `dns:"octet"`:
185 o("l += len(rr.%s)\n")
186 case strings.HasPrefix(st.Tag(i), `dns:"size-base64`):
188 case st.Tag(i) == `dns:"base64"`:
189 o("l += base64.StdEncoding.DecodedLen(len(rr.%s))\n")
190 case strings.HasPrefix(st.Tag(i), `dns:"size-hex:`): // this has an extra field where the length is stored
191 o("l += len(rr.%s)/2\n")
192 case strings.HasPrefix(st.Tag(i), `dns:"size-hex`):
194 case st.Tag(i) == `dns:"hex"`:
195 o("l += len(rr.%s)/2 + 1\n")
196 case st.Tag(i) == `dns:"any"`:
197 o("l += len(rr.%s)\n")
198 case st.Tag(i) == `dns:"a"`:
199 o("l += net.IPv4len // %s\n")
200 case st.Tag(i) == `dns:"aaaa"`:
201 o("l += net.IPv6len // %s\n")
202 case st.Tag(i) == `dns:"txt"`:
203 o("for _, t := range rr.%s { l += len(t) + 1 }\n")
204 case st.Tag(i) == `dns:"uint48"`:
206 case st.Tag(i) == "":
207 switch st.Field(i).Type().(*types.Basic).Kind() {
217 o("l += len(rr.%s) + 1\n")
219 log.Fatalln(name, st.Field(i).Name())
222 log.Fatalln(name, st.Field(i).Name(), st.Tag(i))
225 fmt.Fprintf(b, "return l }\n")
229 fmt.Fprint(b, "// copy() functions\n")
230 for _, name := range namedTypes {
231 o := scope.Lookup(name)
232 st, isEmbedded := getTypeStruct(o.Type(), scope)
236 fmt.Fprintf(b, "func (rr *%s) copy() RR {\n", name)
237 fields := []string{"rr.Hdr"}
238 for i := 1; i < st.NumFields(); i++ {
239 f := st.Field(i).Name()
240 if sl, ok := st.Field(i).Type().(*types.Slice); ok {
241 t := sl.Underlying().String()
242 t = strings.TrimPrefix(t, "[]")
243 if strings.Contains(t, ".") {
244 splits := strings.Split(t, ".")
245 t = splits[len(splits)-1]
247 // For the EDNS0 interface (used in the OPT RR), we need to call the copy method on each element.
249 fmt.Fprintf(b, "%s := make([]%s, len(rr.%s));\nfor i,e := range rr.%s {\n %s[i] = e.copy()\n}\n",
251 fields = append(fields, f)
254 fmt.Fprintf(b, "%s := make([]%s, len(rr.%s)); copy(%s, rr.%s)\n",
256 fields = append(fields, f)
259 if st.Field(i).Type().String() == "net.IP" {
260 fields = append(fields, "copyIP(rr."+f+")")
263 fields = append(fields, "rr."+f)
265 fmt.Fprintf(b, "return &%s{%s}\n", name, strings.Join(fields, ","))
266 fmt.Fprintf(b, "}\n")
270 res, err := format.Source(b.Bytes())
277 f, err := os.Create("ztypes.go")
283 func fatalIfErr(err error) {