| 1 |
//go:build ignore |
| 2 |
|
| 3 |
// scan_generate generates zscan_rdata.go which houses all the parseXXX methods. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"flag" |
| 10 |
"fmt" |
| 11 |
"go/ast" |
| 12 |
"log" |
| 13 |
"slices" |
| 14 |
"strings" |
| 15 |
|
| 16 |
"codeberg.org/miekg/dns/internal/generate" |
| 17 |
) |
| 18 |
|
| 19 |
var skip = []string{"TSIG", "NULL", "TXT", "DELEG", "SVCB", "HIP", "LOC", |
| 20 |
"CSYNC", "RRSIG", "NSEC", "NSEC3", "GPOS", "RFC3597", "DSYNC", "NSEC3PARAM", |
| 21 |
"TKEY", "URI", "NINFO", "EUI48", "CAA", "HINFO", "ISDN", "NAPTR", "UINFO", "EUI64", |
| 22 |
} |
| 23 |
|
| 24 |
var hdr = ` |
| 25 |
// Code generated by "go run clone_generate.go"; DO NOT EDIT. |
| 26 |
|
| 27 |
package dns |
| 28 |
|
| 29 |
import ( |
| 30 |
"net/netip" |
| 31 |
|
| 32 |
"codeberg.org/miekg/dns/rdata" |
| 33 |
"codeberg.org/miekg/dns/internal/dnsstring" |
| 34 |
"codeberg.org/miekg/dns/internal/dnslex" |
| 35 |
) |
| 36 |
|
| 37 |
` |
| 38 |
|
| 39 |
const out = "zscan_rdata.go" |
| 40 |
|
| 41 |
func main() { |
| 42 |
flag.Parse() |
| 43 |
specs, err := generate.StructTypeSpecs("rdata/rdata.go") |
| 44 |
if err != nil { |
| 45 |
log.Fatalf("Failed to generate %s: %v", out, err) |
| 46 |
} |
| 47 |
|
| 48 |
b := &bytes.Buffer{} |
| 49 |
b.WriteString(hdr) |
| 50 |
|
| 51 |
Specs: |
| 52 |
for _, spec := range specs { |
| 53 |
rrname := spec.Name.Name |
| 54 |
if slices.Contains(skip, rrname) { |
| 55 |
continue |
| 56 |
} |
| 57 |
|
| 58 |
fmt.Fprintf(b, "func parse%[1]s(rd *rdata.%[1]s, c *dnslex.Lexer, o string) (err error) {\n", rrname) |
| 59 |
strct := spec.Type.(*ast.StructType) |
| 60 |
|
| 61 |
blank := func() {} |
| 62 |
next := func() { fmt.Fprintf(b, "l, _ := c.Next()\n") } |
| 63 |
for _, field := range strct.Fields.List { |
| 64 |
fieldname := field.Names[0].String() |
| 65 |
fieldtype := "" |
| 66 |
if id, ok := field.Type.(*ast.Ident); ok { |
| 67 |
fieldtype = id.Name |
| 68 |
} |
| 69 |
tag := "" |
| 70 |
if field.Tag != nil { |
| 71 |
tag = strings.Trim(field.Tag.Value, "`") |
| 72 |
} |
| 73 |
|
| 74 |
blank() |
| 75 |
|
| 76 |
if tag != `dns:"base64"` && tag != `dns:"hex"` { |
| 77 |
next() |
| 78 |
} |
| 79 |
|
| 80 |
next = func() { fmt.Fprintf(b, "l, _ = c.Next()\n") } |
| 81 |
|
| 82 |
switch tag { |
| 83 |
case `dns:"-"`: |
| 84 |
continue |
| 85 |
case "": |
| 86 |
switch fieldtype { |
| 87 |
case "string": |
| 88 |
fmt.Fprintf(b, "rd.%s = l.Token\n", fieldname) |
| 89 |
fmt.Fprintf(b, `if l.Value == dnslex.Error || rd.%s == "" {`+"\n", fieldname) |
| 90 |
case "uint8": |
| 91 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.AtoiUint8(l.Token)\n", fieldname) |
| 92 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 93 |
if fieldname == "Algorithm" { |
| 94 |
fmt.Fprintf(b, "var ok bool\n") |
| 95 |
fmt.Fprintf(b, "rd.%s, ok = upperLookup(l.Token, StringToAlgorithm)\n", fieldname) |
| 96 |
fmt.Fprintf(b, "if !ok {\n") |
| 97 |
fmt.Fprintf(b, `return &ParseError{err: "bad %s Algorithm", lex: l}`+"\n", rrname) |
| 98 |
fmt.Fprintf(b, "}\n") |
| 99 |
} |
| 100 |
case "uint16": |
| 101 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.AtoiUint16(l.Token)\n", fieldname) |
| 102 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 103 |
case "uint32": |
| 104 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.AtoiUint32(l.Token)\n", fieldname) |
| 105 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 106 |
case "uint64": |
| 107 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.AtoiUint64(l.Token)\n", fieldname) |
| 108 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 109 |
} |
| 110 |
|
| 111 |
case `dns:"ttl"`: |
| 112 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.AtoiUint32(l.Token)\n", fieldname) |
| 113 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 114 |
{ |
| 115 |
fmt.Fprintf(b, "var ok bool\n") |
| 116 |
fmt.Fprintf(b, "rd.%s, ok = stringToTTL(l.Token)\n", fieldname) |
| 117 |
fmt.Fprintf(b, "if !ok {\n") |
| 118 |
fmt.Fprintf(b, `return &ParseError{err: "bad %s %s", lex: l}`+"\n", rrname, fieldname) |
| 119 |
} |
| 120 |
fmt.Fprintf(b, "}\n") |
| 121 |
fmt.Fprintf(b, "}\n") |
| 122 |
|
| 123 |
continue |
| 124 |
|
| 125 |
case `dns:"node"`: |
| 126 |
fmt.Fprintf(b, "rd.%s, err = dnsstring.ToNodeID(l.Token)\n", fieldname) |
| 127 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil {\n") |
| 128 |
|
| 129 |
case `dns:"base64"`: |
| 130 |
fallthrough |
| 131 |
case `dns:"hex"`: |
| 132 |
// should be the last, no error handling, return the error |
| 133 |
fmt.Fprintf(b, `rd.%s, err = remainder(c, "bad %s %s")`+"\n", fieldname, rrname, fieldname) |
| 134 |
fmt.Fprintf(b, "return err\n") |
| 135 |
fmt.Fprintf(b, "}\n\n") |
| 136 |
|
| 137 |
continue Specs |
| 138 |
|
| 139 |
case `dns:"a"`: |
| 140 |
fmt.Fprintf(b, "rd.%s, err = netip.ParseAddr(l.Token)\n", fieldname) |
| 141 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil || !rd.%s.Is4(){\n", fieldname) |
| 142 |
case `dns:"aaaa"`: |
| 143 |
fmt.Fprintf(b, "rd.%s, err = netip.ParseAddr(l.Token)\n", fieldname) |
| 144 |
fmt.Fprintf(b, "if l.Value == dnslex.Error || err != nil || !rd.%s.Is6(){\n", fieldname) |
| 145 |
|
| 146 |
case `dns:"mname"`: |
| 147 |
fallthrough |
| 148 |
case `dns:"cname"`: |
| 149 |
fallthrough |
| 150 |
case `dns:"name"`: |
| 151 |
fmt.Fprintf(b, "rd.%s = dnsutilAbsolute(l.Token, o)\n", fieldname) |
| 152 |
fmt.Fprintf(b, `if l.Value == dnslex.Error || rd.%s == "" {`+"\n", fieldname) |
| 153 |
// rest of the error handling is done below |
| 154 |
} |
| 155 |
fmt.Fprintf(b, `return &ParseError{err: "bad %s %s", lex: l}`+"\n", rrname, fieldname) |
| 156 |
fmt.Fprintf(b, "}\n") |
| 157 |
|
| 158 |
blank = func() { fmt.Fprintln(b, "\nc.Next() // dnslex.Blank\n") } |
| 159 |
} |
| 160 |
fmt.Fprintln(b, "return toParseError(dnslex.Discard(c))") |
| 161 |
fmt.Fprint(b, "}\n\n") |
| 162 |
} |
| 163 |
|
| 164 |
generate.Write(b, out) |
| 165 |
} |