forked from wpalmer/gozone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gozone.go
1029 lines (933 loc) · 22.8 KB
/
gozone.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gozone
// https://www.ietf.org/rfc/rfc1035.txt
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
"unicode"
)
type RecordClass int
const (
RecordClass_UNKNOWN = 0 // unset
RecordClass_IN = 1 // the Internet
RecordClass_CS = 2 // the CSNET class (Obsolete - used only for examples in some obsolete RFCs)
RecordClass_CH = 3 // the CHAOS class
RecordClass_HS = 4 // Hesiod [Dyer 87]
RecordClass_any = 255 // any class (spelled: *; appears only in the question section of a query; included for completeness)
)
func (rc RecordClass) String() string {
switch rc {
case RecordClass_IN:
return "IN"
case RecordClass_CS:
return "CS"
case RecordClass_CH:
return "CH"
case RecordClass_HS:
return "HS"
case RecordClass_any:
return "*"
}
return "[UNKNOWN]"
}
type RecordType int
const (
RecordType_UNKNOWN = 0 // unset
RecordType_A = 1 // a host address
RecordType_NS = 2 // an authoritative name server
RecordType_MD = 3 // a mail destination (OBSOLETE - use MX)
RecordType_MF = 4 // a mail forwarder (OBSOLETE - use MX)
RecordType_CNAME = 5 // the canonical name for an alias
RecordType_SOA = 6 // marks the start of a zone of authority
RecordType_MB = 7 // a mailbox domain name (EXPERIMENTAL)
RecordType_MG = 8 // a mail group member (EXPERIMENTAL)
RecordType_MR = 9 // a mail rename domain name (EXPERIMENTAL)
RecordType_NULL = 10 // a null RR (EXPERIMENTAL)
RecordType_WKS = 11 // a well known service description
RecordType_PTR = 12 // a domain name pointer
RecordType_HINFO = 13 // host information
RecordType_MINFO = 14 // mailbox or mail list information
RecordType_MX = 15 // mail exchange
RecordType_TXT = 16 // text strings
RecordType_RP = 17 // for Responsible Person
RecordType_AFSDB = 18 // for AFS Data Base location
RecordType_X25 = 19 // for X.25 PSDN address
RecordType_ISDN = 20 // for ISDN address
RecordType_RT = 21 // for Route Through
RecordType_NSAP = 22 // for NSAP address, NSAP style A record
RecordType_NSAP_PTR = 23 // spelled "NSAP-PTR", for domain name pointer, NSAP style
RecordType_SIG = 24 // for security signature
RecordType_KEY = 25 // for security key
RecordType_PX = 26 // X.400 mail mapping information
RecordType_GPOS = 27 // Geographical Position
RecordType_AAAA = 28 // IP6 Address
RecordType_LOC = 29 // Location Information
RecordType_NXT = 30 // Next Domain (OBSOLETE)
RecordType_EID = 31 // Endpoint Identifier
RecordType_NIMLOC = 32 // Nimrod Locator
RecordType_SRV = 33 // Server Selection
RecordType_ATMA = 34 // ATM Address
RecordType_NAPTR = 35 // Naming Authority Pointer
RecordType_KX = 36 // Key Exchanger
RecordType_CERT = 37 // CERT
RecordType_A6 = 38 // A6 (OBSOLETE - use AAAA)
RecordType_DNAME = 39 // DNAME
RecordType_SINK = 40 // SINK
RecordType_OPT = 41 // OPT
RecordType_APL = 42 // APL
RecordType_DS = 43 // Delegation Signer
RecordType_SSHFP = 44 // SSH Key Fingerprint
RecordType_IPSECKEY = 45 // IPSECKEY
RecordType_RRSIG = 46 // RRSIG
RecordType_NSEC = 47 // NSEC
RecordType_DNSKEY = 48 // DNSKEY
RecordType_DHCID = 49 // DHCID
RecordType_NSEC3 = 50 // NSEC3
RecordType_NSEC3PARAM = 51 // NSEC3PARAM
RecordType_TLSA = 52 // TLSA
RecordType_SMIMEA = 53 // S/MIME cert association
// Unassigned 54
RecordType_HIP = 55 // Host Identity Protocol
RecordType_NINFO = 56 // NINFO
RecordType_RKEY = 57 // RKEY
RecordType_TALINK = 58 // Trust Anchor LINK
RecordType_CDS = 59 // Child DS
RecordType_CDNSKEY = 60 // DNSKEY(s) the Child wants reflected in DS
RecordType_OPENPGPKEY = 61 // OpenPGP Key
RecordType_CSYNC = 62 // Child-To-Parent Synchronization
RecordType_ZONEMD = 63 // message digest for DNS zone
// Unassigned 64-98
RecordType_SPF = 99 // declares which hosts are, and are not, authorized to use a domain name for the "HELO" and "MAIL FROM" identities (OBSOLETE - use TXT)
RecordType_UINFO = 100 // [IANA-Reserved]
RecordType_UID = 101 // [IANA-Reserved]
RecordType_GID = 102 // [IANA-Reserved]
RecordType_UNSPEC = 103 // [IANA-Reserved]
RecordType_NID = 104 // values for Node Identifiers that will be used for ILNP-capable nodes
RecordType_L32 = 105 // 32-bit Locator values for ILNPv4-capable nodes
RecordType_L64 = 106 // unsigned 64-bit Locator values for ILNPv6-capable nodes
RecordType_LP = 107 // the name of a subnetwork for ILNP
RecordType_EUI48 = 108 // an EUI-48 address
RecordType_EUI64 = 109 // an EUI-64 address
// Unassigned 110-248
RecordType_TKEY = 249 // Transaction Key
RecordType_TSIG = 250 // Transaction Signature
RecordType_IXFR = 251 // incremental transfer
RecordType_AXFR = 252 // transfer of an entire zone
RecordType_MAILB = 253 // mailbox-related RRs (MB, MG or MR)
RecordType_MAILA = 254 // mail agent RRs (OBSOLETE - see MX)
RecordType_all = 255 // Spelled "*", A request for some or all records the server has available
RecordType_URI = 256 // URI
RecordType_CAA = 257 // Certification Authority Restriction
RecordType_AVC = 258 // Application Visibility and Control
RecordType_DOA = 259 // Digital Object Architecture
RecordType_AMTRELAY = 260 // Automatic Multicast Tunneling Relay
// Unassigned 261-32767
RecordType_TA = 32768 // DNSSEC Trust Authorities
RecordType_DLV = 32769 // DNSSEC Lookaside Validation
// Unassigned 32770-65279
// Private use 65280-65534
// Reserved 65535
)
func (rt RecordType) String() string {
switch rt {
case RecordType_A:
return "A"
case RecordType_NS:
return "NS"
case RecordType_MD:
return "MD"
case RecordType_MF:
return "MF"
case RecordType_CNAME:
return "CNAME"
case RecordType_SOA:
return "SOA"
case RecordType_MB:
return "MB"
case RecordType_MG:
return "MG"
case RecordType_MR:
return "MR"
case RecordType_NULL:
return "NULL"
case RecordType_WKS:
return "WKS"
case RecordType_PTR:
return "PTR"
case RecordType_HINFO:
return "HINFO"
case RecordType_MINFO:
return "MINFO"
case RecordType_MX:
return "MX"
case RecordType_TXT:
return "TXT"
case RecordType_RP:
return "RP"
case RecordType_AFSDB:
return "AFSDB"
case RecordType_X25:
return "X25"
case RecordType_ISDN:
return "ISDN"
case RecordType_RT:
return "RT"
case RecordType_NSAP:
return "NSAP"
case RecordType_NSAP_PTR:
return "NSAP-PTR"
case RecordType_SIG:
return "SIG"
case RecordType_KEY:
return "KEY"
case RecordType_PX:
return "PX"
case RecordType_GPOS:
return "GPOS"
case RecordType_AAAA:
return "AAAA"
case RecordType_LOC:
return "LOC"
case RecordType_NXT:
return "NXT"
case RecordType_EID:
return "EID"
case RecordType_NIMLOC:
return "NIMLOC"
case RecordType_SRV:
return "SRV"
case RecordType_ATMA:
return "ATMA"
case RecordType_NAPTR:
return "NAPTR"
case RecordType_KX:
return "KX"
case RecordType_CERT:
return "CERT"
case RecordType_A6:
return "A6"
case RecordType_DNAME:
return "DNAME"
case RecordType_SINK:
return "SINK"
case RecordType_OPT:
return "OPT"
case RecordType_APL:
return "APL"
case RecordType_DS:
return "DS"
case RecordType_SSHFP:
return "SSHFP"
case RecordType_IPSECKEY:
return "IPSECKEY"
case RecordType_RRSIG:
return "RRSIG"
case RecordType_NSEC:
return "NSEC"
case RecordType_DNSKEY:
return "DNSKEY"
case RecordType_DHCID:
return "DHCID"
case RecordType_NSEC3:
return "NSEC3"
case RecordType_NSEC3PARAM:
return "NSEC3PARAM"
case RecordType_TLSA:
return "TLSA"
case RecordType_SMIMEA:
return "SMIMEA"
case RecordType_HIP:
return "HIP"
case RecordType_NINFO:
return "NINFO"
case RecordType_RKEY:
return "RKEY"
case RecordType_TALINK:
return "TALINK"
case RecordType_CDS:
return "CDS"
case RecordType_CDNSKEY:
return "CDNSKEY"
case RecordType_OPENPGPKEY:
return "OPENPGPKEY"
case RecordType_CSYNC:
return "CSYNC"
case RecordType_ZONEMD:
return "ZONEMD"
case RecordType_SPF:
return "SPF"
case RecordType_UINFO:
return "UINFO"
case RecordType_UID:
return "UID"
case RecordType_GID:
return "GID"
case RecordType_UNSPEC:
return "UNSPEC"
case RecordType_NID:
return "NID"
case RecordType_L32:
return "L32"
case RecordType_L64:
return "L64"
case RecordType_LP:
return "LP"
case RecordType_EUI48:
return "EUI48"
case RecordType_EUI64:
return "EUI64"
case RecordType_TKEY:
return "TKEY"
case RecordType_TSIG:
return "TSIG"
case RecordType_IXFR:
return "IXFR"
case RecordType_AXFR:
return "AXFR"
case RecordType_MAILB:
return "MAILB"
case RecordType_MAILA:
return "MAILA"
case RecordType_all:
return "*"
case RecordType_URI:
return "URI"
case RecordType_CAA:
return "CAA"
case RecordType_AVC:
return "AVC"
case RecordType_DOA:
return "DOA"
case RecordType_AMTRELAY:
return "AMTRELAY"
case RecordType_TA:
return "TA"
case RecordType_DLV:
return "DLV"
}
return "[UNKNOWN]"
}
type Record struct {
DomainName string
TimeToLive int64 // uint32, expanded and signed to allow for "unset" indicator
Class RecordClass
Type RecordType
Data []string
Comment string
}
func (r Record) String() string {
spec := []string{r.DomainName}
if r.TimeToLive != -1 {
spec = append(spec, fmt.Sprintf("%d", r.TimeToLive))
}
if r.Class != RecordClass_UNKNOWN {
spec = append(spec, r.Class.String())
}
if r.Type != RecordType_UNKNOWN {
spec = append(spec, r.Type.String())
}
if len(r.Data) != 0 {
spec = append(spec, strings.Join(r.Data, " "))
}
if len(r.Comment) != 0 {
spec = append(spec, r.Comment)
}
return strings.Join(spec, " ")
}
type scannerState int
const (
scannerState_Default = iota
scannerState_DefaultEscape
scannerState_String
scannerState_StringEscape
scannerState_Paren
scannerState_ParenEscape
scannerState_Comment
scannerState_Space
scannerState_ParenComment
scannerState_ParenString
scannerState_ParenStringEscape
)
type Scanner struct {
src *bufio.Reader
state scannerState
origin string
timeToLive int64
nextRune rune
nextSize int
}
func NewScanner(src io.Reader) *Scanner {
return &Scanner{
src: bufio.NewReader(src),
timeToLive: -1,
nextRune: 0,
nextSize: 0,
}
}
func (s *Scanner) SetOrigin(domain string) error {
if domain[len(domain)-1] != '.' {
return fmt.Errorf("Tried to set $ORIGIN to relative domain")
}
s.origin = domain
return nil
}
func (s *Scanner) SetTimeToLive(timeToLive int64) error {
if timeToLive < 0 {
timeToLive = -1
}
if timeToLive > math.MaxUint32 {
return fmt.Errorf("Tried to set $TTL to number greater than MaxUint32")
}
s.timeToLive = timeToLive
return nil
}
func (s *Scanner) nextToken() (string, error) {
var token bytes.Buffer
var r rune
var size int
var err error
for {
if s.nextSize != 0 {
r = s.nextRune
size = s.nextSize
s.nextSize = 0
} else {
r, size, err = s.src.ReadRune()
if err != nil {
if err == io.EOF {
if s.state != scannerState_Default &&
s.state != scannerState_Space &&
s.state != scannerState_Comment {
return "", errors.New("Unexpected end of input")
}
if token.Len() != 0 {
return token.String(), nil
}
}
return "", err
}
}
s.nextRune = r
s.nextSize = size
switch s.state {
case scannerState_Default, scannerState_Paren:
if unicode.IsSpace(r) {
if token.Len() > 0 {
return token.String(), nil
}
if s.state == scannerState_Default {
if r == '\n' {
s.nextSize = 0
s.state = scannerState_Space
return "\n", nil
}
}
// ignore whitespace between tokens
s.nextSize = 0
continue
}
if s.state == scannerState_Default {
if r == '(' {
if token.Len() > 0 {
return token.String(), nil
}
s.nextSize = 0
s.state = scannerState_Paren
return "(", nil
}
if r == '\\' {
s.nextSize = 0
s.state = scannerState_DefaultEscape
_, _ = token.WriteRune(r)
continue
}
} else if s.state == scannerState_Paren {
if r == ')' {
if token.Len() > 0 {
return token.String(), nil
}
s.nextSize = 0
s.state = scannerState_Default
return ")", nil
}
if r == '\\' {
s.nextSize = 0
s.state = scannerState_ParenEscape
_, _ = token.WriteRune(r)
continue
}
}
if r == '"' {
if token.Len() > 0 {
return token.String(), nil
}
s.nextSize = 0
if s.state == scannerState_Default {
s.state = scannerState_String
} else {
s.state = scannerState_ParenString
}
_, _ = token.WriteRune(r)
continue
}
if r == ';' {
if token.Len() > 0 {
return token.String(), nil
}
s.nextSize = 0
if s.state == scannerState_Default {
s.state = scannerState_Comment
} else {
s.state = scannerState_ParenComment
}
_, _ = token.WriteRune(r)
continue
}
s.nextSize = 0
_, _ = token.WriteRune(r)
case scannerState_String, scannerState_ParenString:
if r == '"' {
s.nextSize = 0
if s.state == scannerState_String {
s.state = scannerState_Default
} else {
s.state = scannerState_Paren
}
_, _ = token.WriteRune(r)
return token.String(), nil
}
if r == '\\' {
s.nextSize = 0
if s.state == scannerState_String {
s.state = scannerState_StringEscape
} else {
s.state = scannerState_ParenStringEscape
}
_, _ = token.WriteRune(r)
continue
}
s.nextSize = 0
_, _ = token.WriteRune(r)
case scannerState_DefaultEscape, scannerState_StringEscape, scannerState_ParenEscape, scannerState_ParenStringEscape:
s.nextSize = 0
switch s.state {
case scannerState_DefaultEscape:
s.state = scannerState_Default
case scannerState_StringEscape:
s.state = scannerState_String
case scannerState_ParenEscape:
s.state = scannerState_Paren
case scannerState_ParenStringEscape:
s.state = scannerState_String
}
_, _ = token.WriteRune(r)
case scannerState_Comment, scannerState_ParenComment:
if r == '\n' {
if s.state == scannerState_Comment {
s.state = scannerState_Default
} else {
s.state = scannerState_Paren
}
continue
}
s.nextSize = 0
_, _ = token.WriteRune(r)
case scannerState_Space:
if unicode.IsSpace(r) {
s.nextSize = 0
continue
}
s.state = scannerState_Default
continue
}
}
}
func parseClass(token string) (RecordClass, error) {
switch token {
case "IN":
return RecordClass_IN, nil
case "CS":
return RecordClass_CS, nil
case "CH":
return RecordClass_CH, nil
case "HS":
return RecordClass_HS, nil
case "*":
return RecordClass_any, nil
default:
return RecordClass_UNKNOWN, fmt.Errorf("Unknown Record Class '%s'", token)
}
}
func parseType(token string) (RecordType, error) {
switch token {
case "A":
return RecordType_A, nil
case "NS":
return RecordType_NS, nil
case "MD":
return RecordType_MD, nil
case "MF":
return RecordType_MF, nil
case "CNAME":
return RecordType_CNAME, nil
case "SOA":
return RecordType_SOA, nil
case "MB":
return RecordType_MB, nil
case "MG":
return RecordType_MG, nil
case "MR":
return RecordType_MR, nil
case "NULL":
return RecordType_NULL, nil
case "WKS":
return RecordType_WKS, nil
case "PTR":
return RecordType_PTR, nil
case "HINFO":
return RecordType_HINFO, nil
case "MINFO":
return RecordType_MINFO, nil
case "MX":
return RecordType_MX, nil
case "TXT":
return RecordType_TXT, nil
case "RP":
return RecordType_RP, nil
case "AFSDB":
return RecordType_AFSDB, nil
case "X25":
return RecordType_X25, nil
case "ISDN":
return RecordType_ISDN, nil
case "RT":
return RecordType_RT, nil
case "NSAP":
return RecordType_NSAP, nil
case "NSAP-PTR":
return RecordType_NSAP_PTR, nil
case "SIG":
return RecordType_SIG, nil
case "KEY":
return RecordType_KEY, nil
case "PX":
return RecordType_PX, nil
case "GPOS":
return RecordType_GPOS, nil
case "AAAA":
return RecordType_AAAA, nil
case "LOC":
return RecordType_LOC, nil
case "NXT":
return RecordType_NXT, nil
case "EID":
return RecordType_EID, nil
case "NIMLOC":
return RecordType_NIMLOC, nil
case "SRV":
return RecordType_SRV, nil
case "ATMA":
return RecordType_ATMA, nil
case "NAPTR":
return RecordType_NAPTR, nil
case "KX":
return RecordType_KX, nil
case "CERT":
return RecordType_CERT, nil
case "A6":
return RecordType_A6, nil
case "DNAME":
return RecordType_DNAME, nil
case "SINK":
return RecordType_SINK, nil
case "OPT":
return RecordType_OPT, nil
case "APL":
return RecordType_APL, nil
case "DS":
return RecordType_DS, nil
case "SSHFP":
return RecordType_SSHFP, nil
case "IPSECKEY":
return RecordType_IPSECKEY, nil
case "RRSIG":
return RecordType_RRSIG, nil
case "NSEC":
return RecordType_NSEC, nil
case "DNSKEY":
return RecordType_DNSKEY, nil
case "DHCID":
return RecordType_DHCID, nil
case "NSEC3":
return RecordType_NSEC3, nil
case "NSEC3PARAM":
return RecordType_NSEC3PARAM, nil
case "TLSA":
return RecordType_TLSA, nil
case "SMIMEA":
return RecordType_SMIMEA, nil
case "HIP":
return RecordType_HIP, nil
case "NINFO":
return RecordType_NINFO, nil
case "RKEY":
return RecordType_RKEY, nil
case "TALINK":
return RecordType_TALINK, nil
case "CDS":
return RecordType_CDS, nil
case "CDNSKEY":
return RecordType_CDNSKEY, nil
case "OPENPGPKEY":
return RecordType_OPENPGPKEY, nil
case "CSYNC":
return RecordType_CSYNC, nil
case "ZONEMD":
return RecordType_ZONEMD, nil
case "SPF":
return RecordType_SPF, nil
case "UINFO":
return RecordType_UINFO, nil
case "UID":
return RecordType_UID, nil
case "GID":
return RecordType_GID, nil
case "UNSPEC":
return RecordType_UNSPEC, nil
case "NID":
return RecordType_NID, nil
case "L32":
return RecordType_L32, nil
case "L64":
return RecordType_L64, nil
case "LP":
return RecordType_LP, nil
case "EUI48":
return RecordType_EUI48, nil
case "EUI64":
return RecordType_EUI64, nil
case "TKEY":
return RecordType_TKEY, nil
case "TSIG":
return RecordType_TSIG, nil
case "IXFR":
return RecordType_IXFR, nil
case "AXFR":
return RecordType_AXFR, nil
case "MAILB":
return RecordType_MAILB, nil
case "MAILA":
return RecordType_MAILA, nil
case "*":
return RecordType_all, nil
case "URI":
return RecordType_URI, nil
case "CAA":
return RecordType_CAA, nil
case "AVC":
return RecordType_AVC, nil
case "DOA":
return RecordType_DOA, nil
case "AMTRELAY":
return RecordType_AMTRELAY, nil
case "TA":
return RecordType_TA, nil
case "DLV":
return RecordType_DLV, nil
default:
return 0, fmt.Errorf("Unknown Record Type '%s'", token)
}
}
func (s *Scanner) scanControlEntry(initial string) error {
switch initial {
case "$ORIGIN":
return s.scanControlEntryOrigin()
case "$TTL":
return s.scanControlEntryTTL()
default:
return fmt.Errorf("Unknown Control Entry '%s'", initial)
}
}
func (s *Scanner) scanControlEntryOrigin() error {
var hasDomain bool
var token string
var err error
for {
if token, err = s.nextToken(); err != nil {
if err == io.EOF {
if hasDomain {
break
}
return fmt.Errorf("Incomplete $ORIGIN control entry at end of file")
}
return err
}
if token[0] == ';' {
if hasDomain {
return nil
}
return fmt.Errorf("Incomplete $ORIGIN control entry ends in comment")
}
if token == "\n" {
if !hasDomain {
return fmt.Errorf("missing DomainName in $ORIGIN control entry")
}
break
}
if hasDomain {
return fmt.Errorf("Multiple domains found in $ORIGIN control entry")
}
if err = s.SetOrigin(token); err != nil {
return err
}
hasDomain = true
}
return nil
}
func (s *Scanner) scanControlEntryTTL() error {
var hasTTL bool
var token string
var err error
for {
if token, err = s.nextToken(); err != nil {
if err == io.EOF {
if hasTTL {
break
}
return fmt.Errorf("Incomplete $TTL control entry at end of file")
}
return err
}
if token[0] == ';' {
if hasTTL {
return nil
}
return fmt.Errorf("Incomplete $TTL control entry ends in comment")
}
if token == "\n" {
if !hasTTL {
return fmt.Errorf("missing TimeToLive in $TTL control entry")
}
break
}
if hasTTL {
return fmt.Errorf("Multiple TimeToLive found in $TTL control entry")
}
var i64 uint64
i64, err = strconv.ParseUint(token, 10, 32)
if err != nil {
return fmt.Errorf("Failed to parse TimeToLive in $TTL control entry: %s", err)
}
if err = s.SetTimeToLive(int64(i64)); err != nil {
return err
}
hasTTL = true
}
return nil
}
func (s *Scanner) Next(outrecord *Record) error {
var record Record
var token string
var err error
var hasClass bool
var hasTTL bool
var hasType bool
var hasData bool
record.TimeToLive = -1
for { // ignore leading spaces / comments / process control entries
if token, err = s.nextToken(); err != nil {
return err
}
if token[0] == '$' {
// control entry
if err = s.scanControlEntry(token); err != nil {
return err
}
}
if token != "\n" && token[0] != ';' && token[0] != '$' {
break
}
}
domain := token
if domain == "@" {
if s.origin == "" {
return fmt.Errorf("Record for current domain specified when no $ORIGIN defined")
}
domain = s.origin
} else if domain[len(token)-1] != '.' {
if s.origin == "" {
return fmt.Errorf("Record relative-to-current domain specified when no $ORIGIN defined")
}
domain = fmt.Sprintf("%s.%s", token, s.origin)
}
record.DomainName = domain
for {
if token, err = s.nextToken(); err != nil {
if err == io.EOF {
if hasData {
*outrecord = record
break
}
if hasClass || hasTTL || hasType {
return fmt.Errorf("Incomplete record at end of file")
}
}
return err
}
if !hasType {
if !hasTTL {
var i64 uint64
i64, err = strconv.ParseUint(token, 10, 32)
if err != nil {
record.TimeToLive = s.timeToLive
} else {
record.TimeToLive = int64(i64)
hasTTL = true
continue
}
}
if !hasClass {
record.Class, err = parseClass(token)
if err != nil {
record.Class = RecordClass_UNKNOWN
} else {
hasClass = true
continue
}
}
record.Type, err = parseType(token)
if err != nil {
return err
} else {
hasType = true
continue
}