Debian Codegraph @

json.go

raw
1 package dns
2
3 import (
4 "encoding/hex"
5 "encoding/json"
6 "fmt"
7
8 "codeberg.org/miekg/dns/dnsjson"
9 "golang.org/x/crypto/cryptobyte"
10 )
11
12 // MarshalJSON returns the JSON (RFC 8427) representation of [RR] as defined in [dnsjson.RR]. If more than one
13 // [RR] is given it is assumed this represents a [dnsjson.RRset].
14 func MarshalJSON(rrs ...RR) ([]byte, error) {
15 buf := dnsjson.Pool.Get()
16 defer dnsjson.Pool.Put(buf)
17
18 jrr := &dnsjson.RR{
19 Name: rrs[0].Header().Name,
20 TTL: rrs[0].Header().TTL,
21 TypeName: typeToString(RRToType(rrs[0])),
22 ClassName: classToString(rrs[0].Header().Class),
23 }
24
25 switch len(rrs) {
26 case 1:
27 if l := rrs[0].Len(); cap(buf) < l {
28 buf = make([]byte, l)
29 }
30
31 off, err := zpack(rrs[0], buf, 0, nil)
32 if err != nil {
33 return nil, err
34 }
35 jrr.RdataHex = hex.EncodeToString(buf[:off])
36 default:
37 jrr.RRset = make([]dnsjson.RRset, len(rrs))
38 for i, rr := range rrs {
39 if l := rr.Len(); cap(buf) < l {
40 buf = make([]byte, l)
41 }
42
43 off, err := zpack(rr, buf, 0, nil)
44 if err != nil {
45 return nil, err
46 }
47 jrr.RRset[i].RdataHex = hex.EncodeToString(buf[:off])
48 }
49 }
50
51 return json.Marshal(jrr)
52 }
53
54 // UnmarshalJSON returns the [RR] from the JSON (RFC 8427) object. If class (CLASS, or CLASSname) is not set, [ClassINET] is assumed.
55 func UnmarshalJSON(data []byte) ([]RR, error) {
56 jrr := &dnsjson.RR{}
57 if err := json.Unmarshal(data, jrr); err != nil {
58 return nil, err
59 }
60 rrs := make([]RR, 1)
61 if len(jrr.RRset) > 0 {
62 rrs = make([]RR, len(jrr.RRset))
63 }
64
65 var ok bool
66 newfn := func() RR { return nil }
67 switch {
68 case jrr.Type > 0:
69 newfn, ok = TypeToRR[jrr.Type]
70 case jrr.TypeName != "":
71 newfn, ok = TypeToRR[StringToType[jrr.TypeName]]
72 }
73 if !ok {
74 return nil, fmt.Errorf("bad RR type")
75 }
76
77 class := uint16(0)
78 switch {
79 case jrr.Class > 0:
80 class = jrr.Class
81 case jrr.ClassName != "":
82 class, _ = StringToClass[jrr.ClassName]
83 default:
84 class = ClassINET
85 }
86
87 buf := dnsjson.Pool.Get()
88 defer dnsjson.Pool.Put(buf)
89
90 switch len(rrs) {
91 case 1:
92 rrs[0] = newfn()
93
94 rrs[0].Header().Name = jrr.Name
95 rrs[0].Header().TTL = jrr.TTL
96 rrs[0].Header().Class = class
97
98 if l := hex.DecodedLen(len(jrr.RdataHex)); cap(buf) < l {
99 buf = make([]byte, l)
100 }
101
102 n, err := hex.Decode(buf, []byte(jrr.RdataHex))
103 if err != nil {
104 return nil, err
105 }
106 if err := zunpack(rrs[0], cryptobyte.String(buf[:n]), nil); err != nil {
107 return nil, err
108 }
109 default:
110 for i := range rrs {
111 rrs[i] = newfn()
112
113 rrs[i].Header().Name = jrr.Name
114 rrs[i].Header().TTL = jrr.TTL
115 rrs[i].Header().Class = class
116
117 if l := hex.DecodedLen(len(jrr.RRset[i].RdataHex)); cap(buf) < l {
118 buf = make([]byte, l)
119 }
120
121 n, err := hex.Decode(buf, []byte(jrr.RRset[i].RdataHex))
122 if err != nil {
123 return nil, err
124 }
125 if err := zunpack(rrs[i], cryptobyte.String(buf[:n]), nil); err != nil {
126 return nil, err
127 }
128 }
129 }
130
131 return rrs, nil
132 }