forked from miekg/unbound
/
unbound.go
308 lines (273 loc) · 8.82 KB
/
unbound.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Package unbound implements a wrapper for libunbound(3).
// Unbound is a DNSSEC aware resolver, see https://unbound.net/
// for more information. It's up to the caller to configure
// Unbound with trust anchors. With these anchors a DNSSEC
// answer can be validated.
//
// The method's documentation can be found in libunbound(3).
// The names of the methods are in sync with the
// names used in unbound, but the underscores are removed and they
// are in camel-case, e.g. ub_ctx_resolv_conf becomes u.ResolvConf.
// Except for ub_ctx_create() and ub_ctx_delete(),
// which become: New() and Destroy().
//
// Basic use pattern:
// u := unbound.New()
// defer u.Destroy()
// u.ResolvConf("/etc/resolv.conf")
// u.AddTaFile("trustanchor")
// r, e := u.Resolve("miek.nl.", dns.TypeA, dns.ClassINET)
//
// The asynchronous functions are implemented using goroutines. This
// means the following functions are not useful in Go and therefor
// not implemented: ub_fd, ub_wait, ub_poll, ub_process and ub_cancel.
//
// Unbound's ub_result has been modified. An extra field has been added
// named 'Rr' which is a []dns.RR.
//
// The Lookup* functions of the net package are re-implemented in this package.
package unbound
/*
#cgo LDFLAGS: -lunbound
#include <stdlib.h>
#include <stdio.h>
#include <unbound.h>
int array_elem_int(int *l, int i) { return l[i]; }
char * array_elem_char(char **l, int i) { if (l == NULL) return NULL; return l[i]; }
char * new_char_pointer() { char *p = NULL; return p; }
struct ub_result *new_ub_result() {
struct ub_result *r;
r = calloc(sizeof(struct ub_result), 1);
return r;
}
*/
import "C"
import (
"github.com/miekg/dns"
"os"
"unsafe"
)
type Unbound struct {
ctx *C.struct_ctx
}
// Results is Unbound's ub_result adapted for Go.
type Result struct {
Qname string // Text string, original question
Qtype uint16 // Type code asked for
Qclass uint16 // Class code asked for
Data [][]byte // Slice of rdata items,
Rr []dns.RR // The RR encoded from the Data, Qclass, Qtype and Qname (not in Unbound)
CanonName string // Canonical name of result
Rcode int // Additional error code in case of no data
AnswerPacket *dns.Msg // Full network format answer packet
HaveData bool // True if there is data
NxDomain bool // True if nodata because name does not exist
Secure bool // True if result is secure
Bogus bool // True if a security failure happened
WhyBogus string // String with error if bogus
}
// UnboundError is an error returned from Unbound, it wraps both the
// return code and the error string as returned by ub_strerror.
type UnboundError struct {
Err string
code int
}
func (e *UnboundError) Error() string {
return e.Err
}
func newError(i int) error {
if i == 0 {
return nil
}
e := new(UnboundError)
e.Err = errorString(i)
e.code = i
return e
}
func errorString(i int) string {
return C.GoString(C.ub_strerror(C.int(i)))
}
// New wraps Unbound's ub_ctx_create.
func New() *Unbound {
u := new(Unbound)
u.ctx = C.ub_ctx_create()
return u
}
// Destroy wraps Unbound's ub_ctx_delete.
func (u *Unbound) Destroy() {
C.ub_ctx_delete(u.ctx)
}
// ResolvConf wraps Unbound's ub_ctx_resolvconf.
func (u *Unbound) ResolvConf(fname string) error {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
i := C.ub_ctx_resolvconf(u.ctx, cfname)
return newError(int(i))
}
// SetOption wraps Unbound's ub_ctx_set_option.
func (u *Unbound) SetOption(opt, val string) error {
copt := C.CString(opt)
defer C.free(unsafe.Pointer(copt))
cval := C.CString(val)
defer C.free(unsafe.Pointer(cval))
i := C.ub_ctx_set_option(u.ctx, copt, cval)
return newError(int(i))
}
// GetOption wraps Unbound's ub_ctx_get_option.
func (u *Unbound) GetOption(opt string) (string, error) {
copt := C.CString(opt)
defer C.free(unsafe.Pointer(copt))
cval := C.new_char_pointer()
defer C.free(unsafe.Pointer(cval))
i := C.ub_ctx_get_option(u.ctx, C.CString(opt), &cval)
return C.GoString(cval), newError(int(i))
}
// Config wraps Unbound's ub_ctx_config.
func (u *Unbound) Config(fname string) error {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
i := C.ub_ctx_config(u.ctx, cfname)
return newError(int(i))
}
// SetFwd wraps Unbound's ub_ctx_set_fwd.
func (u *Unbound) SetFwd(addr string) error {
caddr := C.CString(addr)
defer C.free(unsafe.Pointer(caddr))
i := C.ub_ctx_set_fwd(u.ctx, caddr)
return newError(int(i))
}
// Hosts wraps Unbound's ub_ctx_hosts.
func (u *Unbound) Hosts(fname string) error {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
i := C.ub_ctx_hosts(u.ctx, cfname)
return newError(int(i))
}
// Resolve wraps Unbound's ub_resolve.
func (u *Unbound) Resolve(name string, rrtype, rrclass uint16) (*Result, error) {
res := C.new_ub_result()
r := new(Result)
defer C.ub_resolve_free(res)
i := C.ub_resolve(u.ctx, C.CString(name), C.int(rrtype), C.int(rrclass), &res)
err := newError(int(i))
if err != nil {
return nil, err
}
r.Qname = C.GoString(res.qname)
r.Qtype = uint16(res.qtype)
r.Qclass = uint16(res.qclass)
r.CanonName = C.GoString(res.canonname)
r.Rcode = int(res.rcode)
r.AnswerPacket = new(dns.Msg)
r.AnswerPacket.Unpack(C.GoBytes(res.answer_packet, res.answer_len)) // Should always work
r.HaveData = res.havedata == 1
r.NxDomain = res.nxdomain == 1
r.Secure = res.secure == 1
r.Bogus = res.bogus == 1
r.WhyBogus = C.GoString(res.why_bogus)
// Re-create the RRs
var h dns.RR_Header
h.Name = r.Qname
h.Rrtype = r.Qtype
h.Class = r.Qclass
h.Ttl = 0
r.Data = make([][]byte, 0)
r.Rr = make([]dns.RR, 0)
j := 0
if r.HaveData {
b := C.GoBytes(unsafe.Pointer(C.array_elem_char(res.data, C.int(j))), C.array_elem_int(res.len, C.int(j)))
for len(b) != 0 {
// Create the RR
h.Rdlength = uint16(len(b))
msg := make([]byte, 20+len(h.Name)) // Long enough
off, _ := dns.PackStruct(&h, msg, 0)
msg = msg[:off]
rrbuf := append(msg, b...)
rr, _, err := dns.UnpackRR(rrbuf, 0)
if err == nil {
r.Rr = append(r.Rr, rr)
}
r.Data = append(r.Data, b)
j++
b = C.GoBytes(unsafe.Pointer(C.array_elem_char(res.data, C.int(j))), C.array_elem_int(res.len, C.int(j)))
}
}
return r, err
}
// ResolveAsync does *not* wrap the Unbound function, instead
// it utilizes Go's goroutines to mimic the async behavoir Unbound
// implements. As a result the function signature is different.
// The function f is called after the resolution is finished.
// Also the ub_cancel, ub_wait_, ub_fd, us_process are not implemented.
func (u *Unbound) ResolveAsync(name string, rrtype, rrclass uint16, m interface{}, f func(interface{}, error, *Result)) error {
go func() {
r, e := u.Resolve(name, rrtype, rrclass)
if f != nil {
f(m, e, r)
}
}()
return newError(0)
}
// AddTa wraps Unbound's ub_ctx_add_ta.
func (u *Unbound) AddTa(ta string) error {
cta := C.CString(ta)
i := C.ub_ctx_add_ta(u.ctx, cta)
return newError(int(i))
}
// AddTaFile wraps Unbound's ub_ctx_add_ta_file.
func (u *Unbound) AddTaFile(fname string) error {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
i := C.ub_ctx_add_ta_file(u.ctx, cfname)
return newError(int(i))
}
// TrustedKeys wraps Unbound's ub_ctx_trustedkeys.
func (u *Unbound) TrustedKeys(fname string) error {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
i := C.ub_ctx_trustedkeys(u.ctx, cfname)
return newError(int(i))
}
// ZoneAdd wraps Unbound's ub_ctx_zone_add.
func (u *Unbound) ZoneAdd(zone_name, zone_type string) error {
czone_name := C.CString(zone_name)
defer C.free(unsafe.Pointer(czone_name))
czone_type := C.CString(zone_type)
defer C.free(unsafe.Pointer(czone_type))
i := C.ub_ctx_zone_add(u.ctx, czone_name, czone_type)
return newError(int(i))
}
// ZoneRemove wraps Unbound's ub_ctx_zone_remove.
func (u *Unbound) ZoneRemove(zone_name string) error {
czone_name := C.CString(zone_name)
defer C.free(unsafe.Pointer(czone_name))
i := C.ub_ctx_zone_remove(u.ctx, czone_name)
return newError(int(i))
}
// DataAdd wraps Unbound's ub_ctx_data_add.
func (u *Unbound) DataAdd(data string) error {
cdata := C.CString(data)
defer C.free(unsafe.Pointer(cdata))
i := C.ub_ctx_data_add(u.ctx, cdata)
return newError(int(i))
}
// DataRemove wraps Unbound's ub_ctx_data_remove.
func (u *Unbound) DataRemove(data string) error {
cdata := C.CString(data)
defer C.free(unsafe.Pointer(cdata))
i := C.ub_ctx_data_remove(u.ctx, cdata)
return newError(int(i))
}
// DebugOut wraps Unbound's ub_ctx_debugout.
func (u *Unbound) DebugOut(out *os.File) error {
cmode := C.CString("a+")
defer C.free(unsafe.Pointer(cmode))
file := C.fdopen(C.int(out.Fd()), cmode)
i := C.ub_ctx_debugout(u.ctx, unsafe.Pointer(file))
return newError(int(i))
}
// DebugLevel wraps Unbound's ub_ctx_data_level.
func (u *Unbound) DebugLevel(d int) error {
i := C.ub_ctx_debuglevel(u.ctx, C.int(d))
return newError(int(i))
}