forked from monero-project/unbound
-
Notifications
You must be signed in to change notification settings - Fork 1
/
str2wire.c
2120 lines (1950 loc) · 56.6 KB
/
str2wire.c
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
/**
* str2wire.c - read txt presentation of RRs
*
* (c) NLnet Labs, 2005-2006
*
* See the file LICENSE for the license
*/
/**
* \file
*
* Parses text to wireformat.
*/
#include "config.h"
#include "sldns/str2wire.h"
#include "sldns/wire2str.h"
#include "sldns/sbuffer.h"
#include "sldns/parse.h"
#include "sldns/parseutil.h"
#include <ctype.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
/** return an error */
#define RET_ERR(e, off) ((int)((e)|((off)<<LDNS_WIREPARSE_SHIFT)))
/** Move parse error but keep its ID */
#define RET_ERR_SHIFT(e, move) RET_ERR(LDNS_WIREPARSE_ERROR(e), LDNS_WIREPARSE_OFFSET(e)+(move));
#define LDNS_IP6ADDRLEN (128/8)
/*
* No special care is taken, all dots are translated into
* label separators.
* @param rel: true if the domain is not absolute (not terminated in .).
* The output is then still terminated with a '0' rootlabel.
*/
static int sldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf,
size_t* olen, int* rel)
{
size_t len;
const char *s;
uint8_t *q, *pq, label_len;
if(rel) *rel = 0;
len = strlen((char*)str);
/* octet representation can make strings a lot longer than actual length */
if (len > LDNS_MAX_DOMAINLEN * 4) {
return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0);
}
if (0 == len) {
return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0);
}
/* root label */
if (1 == len && *str == '.') {
if(*olen < 1)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
buf[0] = 0;
*olen = 1;
return LDNS_WIREPARSE_ERR_OK;
}
/* get on with the rest */
/* s is on the current character in the string
* pq points to where the labellength is going to go
* label_len keeps track of the current label's length
* q builds the dname inside the buf array
*/
len = 0;
if(*olen < 1)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
q = buf+1;
pq = buf;
label_len = 0;
for (s = str; *s; s++, q++) {
if (q >= buf + *olen)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
if (q > buf + LDNS_MAX_DOMAINLEN)
return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
switch (*s) {
case '.':
if (label_len > LDNS_MAX_LABELLEN) {
return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
}
if (label_len == 0) {
return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
}
len += label_len + 1;
*q = 0;
*pq = label_len;
label_len = 0;
pq = q;
break;
case '\\':
/* octet value or literal char */
s += 1;
if (!sldns_parse_escape(q, &s)) {
*q = 0;
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf);
}
s -= 1;
label_len++;
break;
default:
*q = (uint8_t)*s;
label_len++;
}
}
/* add root label if last char was not '.' */
if(label_len != 0) {
if(rel) *rel = 1;
if (q >= buf + *olen)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
if (q > buf + LDNS_MAX_DOMAINLEN) {
return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
}
if (label_len > LDNS_MAX_LABELLEN) {
return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
}
if (label_len == 0) { /* label_len 0 but not . at end? */
return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
}
len += label_len + 1;
*pq = label_len;
*q = 0;
}
len++;
*olen = len;
return LDNS_WIREPARSE_ERR_OK;
}
int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len)
{
return sldns_str2wire_dname_buf_rel(str, buf, len, NULL);
}
int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
uint8_t* origin, size_t origin_len)
{
size_t dlen = *len;
int rel = 0;
int s = sldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel);
if(s) return s;
if(rel && origin && dlen > 0) {
if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN)
return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
LDNS_MAX_DOMAINLEN);
if(dlen + origin_len - 1 > *len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
*len);
memmove(buf+dlen-1, origin, origin_len);
*len = dlen + origin_len - 1;
} else
*len = dlen;
return LDNS_WIREPARSE_ERR_OK;
}
uint8_t* sldns_str2wire_dname(const char* str, size_t* len)
{
uint8_t dname[LDNS_MAX_DOMAINLEN+1];
*len = sizeof(dname);
if(sldns_str2wire_dname_buf(str, dname, len) == 0) {
uint8_t* r = (uint8_t*)malloc(*len);
if(r) return memcpy(r, dname, *len);
}
*len = 0;
return NULL;
}
/** read owner name */
static int
rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len,
size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
size_t prev_len, char* token, size_t token_len)
{
/* split the rr in its parts -1 signals trouble */
if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
sldns_buffer_position(strbuf));
}
if(strcmp(token, "@") == 0) {
uint8_t* tocopy;
if (origin) {
*dname_len = origin_len;
tocopy = origin;
} else if (prev) {
*dname_len = prev_len;
tocopy = prev;
} else {
/* default to root */
*dname_len = 1;
tocopy = (uint8_t*)"\0";
}
if(*len < *dname_len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
memmove(rr, tocopy, *dname_len);
} else if(*token == '\0') {
/* no ownername was given, try prev, if that fails
* origin, else default to root */
uint8_t* tocopy;
if(prev) {
*dname_len = prev_len;
tocopy = prev;
} else if(origin) {
*dname_len = origin_len;
tocopy = origin;
} else {
*dname_len = 1;
tocopy = (uint8_t*)"\0";
}
if(*len < *dname_len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
memmove(rr, tocopy, *dname_len);
} else {
size_t dlen = *len;
int s = sldns_str2wire_dname_buf_origin(token, rr, &dlen,
origin, origin_len);
if(s) return RET_ERR_SHIFT(s,
sldns_buffer_position(strbuf)-strlen(token));
*dname_len = dlen;
}
return LDNS_WIREPARSE_ERR_OK;
}
/** read ttl */
static int
rrinternal_get_ttl(sldns_buffer* strbuf, char* token, size_t token_len,
int* not_there, uint32_t* ttl, uint32_t default_ttl)
{
const char* endptr;
if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TTL,
sldns_buffer_position(strbuf));
}
*ttl = (uint32_t) sldns_str2period(token, &endptr);
if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) {
*not_there = 1;
/* ah, it's not there or something */
if (default_ttl == 0) {
*ttl = LDNS_DEFAULT_TTL;
} else {
*ttl = default_ttl;
}
}
return LDNS_WIREPARSE_ERR_OK;
}
/** read class */
static int
rrinternal_get_class(sldns_buffer* strbuf, char* token, size_t token_len,
int* not_there, uint16_t* cl)
{
/* if 'not_there' then we got token from previous parse routine */
if(!*not_there) {
/* parse new token for class */
if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_CLASS,
sldns_buffer_position(strbuf));
}
} else *not_there = 0;
*cl = sldns_get_rr_class_by_name(token);
/* class can be left out too, assume IN, current token must be type */
if(*cl == 0 && strcmp(token, "CLASS0") != 0) {
*not_there = 1;
*cl = LDNS_RR_CLASS_IN;
}
return LDNS_WIREPARSE_ERR_OK;
}
/** read type */
static int
rrinternal_get_type(sldns_buffer* strbuf, char* token, size_t token_len,
int* not_there, uint16_t* tp)
{
/* if 'not_there' then we got token from previous parse routine */
if(!*not_there) {
/* parse new token for type */
if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
sldns_buffer_position(strbuf));
}
}
*tp = sldns_get_rr_type_by_name(token);
if(*tp == 0 && strcmp(token, "TYPE0") != 0) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
sldns_buffer_position(strbuf));
}
return LDNS_WIREPARSE_ERR_OK;
}
/** put type, class, ttl into rr buffer */
static int
rrinternal_write_typeclassttl(sldns_buffer* strbuf, uint8_t* rr, size_t len,
size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question)
{
if(question) {
/* question is : name, type, class */
if(dname_len + 4 > len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
sldns_write_uint16(rr+dname_len, tp);
sldns_write_uint16(rr+dname_len+2, cl);
return LDNS_WIREPARSE_ERR_OK;
}
/* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */
if(dname_len + 10 > len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
sldns_write_uint16(rr+dname_len, tp);
sldns_write_uint16(rr+dname_len+2, cl);
sldns_write_uint32(rr+dname_len+4, ttl);
sldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */
return LDNS_WIREPARSE_ERR_OK;
}
/** find delimiters for type */
static const char*
rrinternal_get_delims(sldns_rdf_type rdftype, size_t r_cnt, size_t r_max)
{
switch(rdftype) {
case LDNS_RDF_TYPE_B64 :
case LDNS_RDF_TYPE_HEX : /* These rdf types may con- */
case LDNS_RDF_TYPE_LOC : /* tain whitespace, only if */
case LDNS_RDF_TYPE_WKS : /* it is the last rd field. */
case LDNS_RDF_TYPE_IPSECKEY :
case LDNS_RDF_TYPE_NSEC : if (r_cnt == r_max - 1) {
return "\n";
}
break;
default : break;
}
return "\n\t ";
}
/* Syntactic sugar for sldns_rr_new_frm_str_internal */
static int
sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type)
{
return rdf_type == LDNS_RDF_TYPE_STR ||
rdf_type == LDNS_RDF_TYPE_LONG_STR;
}
/** see if rdata is quoted */
static int
rrinternal_get_quoted(sldns_buffer* strbuf, const char** delimiters,
sldns_rdf_type rdftype)
{
if(sldns_rdf_type_maybe_quoted(rdftype) &&
sldns_buffer_remaining(strbuf) > 0) {
/* skip spaces */
while(sldns_buffer_remaining(strbuf) > 0 &&
*(sldns_buffer_current(strbuf)) == ' ') {
sldns_buffer_skip(strbuf, 1);
}
if(sldns_buffer_remaining(strbuf) > 0 &&
*(sldns_buffer_current(strbuf)) == '\"') {
*delimiters = "\"\0";
sldns_buffer_skip(strbuf, 1);
return 1;
}
}
return 0;
}
/** spool hex data into rdata */
static int
rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len,
size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size)
{
char* p = token;
while(*p) {
if(isspace((unsigned char)*p)) {
p++;
continue;
}
if(!isxdigit((unsigned char)*p))
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
p-token);
if(*cur_hex_data_size >= hex_data_size)
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
p-token);
/* extra robust check */
if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
p-token);
/* see if 16s or 1s */
if( ((*cur_hex_data_size)&1) == 0) {
rr[rr_cur_len+(*cur_hex_data_size)/2] =
(uint8_t)sldns_hexdigit_to_int(*p)*16;
} else {
rr[rr_cur_len+(*cur_hex_data_size)/2] +=
(uint8_t)sldns_hexdigit_to_int(*p);
}
p++;
(*cur_hex_data_size)++;
}
return LDNS_WIREPARSE_ERR_OK;
}
/** read unknown rr type format */
static int
rrinternal_parse_unknown(sldns_buffer* strbuf, char* token, size_t token_len,
uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos)
{
const char* delim = "\n\t ";
size_t hex_data_size, cur_hex_data_size;
/* go back to before \#
* and skip it while setting delimiters better
*/
sldns_buffer_set_position(strbuf, pre_data_pos);
if(sldns_bget_token(strbuf, token, delim, token_len) == -1)
return LDNS_WIREPARSE_ERR_GENERAL; /* should not fail */
/* read rdata octet length */
if(sldns_bget_token(strbuf, token, delim, token_len) == -1) {
/* something goes very wrong here */
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
sldns_buffer_position(strbuf));
}
hex_data_size = (size_t)atoi(token);
if(hex_data_size > LDNS_MAX_RDFLEN ||
*rr_cur_len + hex_data_size > *rr_len) {
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
}
/* copy hex chars into hex str (2 chars per byte) */
hex_data_size *= 2;
cur_hex_data_size = 0;
while(cur_hex_data_size < hex_data_size) {
int status;
ssize_t c = sldns_bget_token(strbuf, token, delim, token_len);
if((status = rrinternal_spool_hex(token, rr, *rr_len,
*rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0)
return RET_ERR_SHIFT(status,
sldns_buffer_position(strbuf)-strlen(token));
if(c == -1) {
if(cur_hex_data_size != hex_data_size)
return RET_ERR(
LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
sldns_buffer_position(strbuf));
break;
}
}
*rr_cur_len += hex_data_size/2;
return LDNS_WIREPARSE_ERR_OK;
}
/** parse normal RR rdata element */
static int
rrinternal_parse_rdf(sldns_buffer* strbuf, char* token, size_t token_len,
uint8_t* rr, size_t rr_len, size_t* rr_cur_len, sldns_rdf_type rdftype,
uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len,
uint8_t* origin, size_t origin_len)
{
size_t len;
int status;
switch(rdftype) {
case LDNS_RDF_TYPE_DNAME:
/* check if the origin should be used or concatenated */
if(strcmp(token, "@") == 0) {
uint8_t* tocopy;
size_t copylen;
if(origin) {
copylen = origin_len;
tocopy = origin;
} else if(rr_type == LDNS_RR_TYPE_SOA) {
copylen = dname_len;
tocopy = rr; /* copy rr owner name */
} else {
copylen = 1;
tocopy = (uint8_t*)"\0";
}
if((*rr_cur_len) + copylen > rr_len)
return RET_ERR(
LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
memmove(rr+*rr_cur_len, tocopy, copylen);
(*rr_cur_len) += copylen;
} else {
size_t dlen = rr_len - (*rr_cur_len);
int s = sldns_str2wire_dname_buf_origin(token,
rr+*rr_cur_len, &dlen, origin, origin_len);
if(s) return RET_ERR_SHIFT(s,
sldns_buffer_position(strbuf)-strlen(token));
(*rr_cur_len) += dlen;
}
return LDNS_WIREPARSE_ERR_OK;
case LDNS_RDF_TYPE_HEX:
case LDNS_RDF_TYPE_B64:
/* When this is the last rdata field, then the
* rest should be read in (cause then these
* rdf types may contain spaces). */
if(r_cnt == r_max - 1) {
size_t tlen = strlen(token);
(void)sldns_bget_token(strbuf, token+tlen, "\n",
token_len - tlen);
}
break;
default:
break;
}
len = rr_len - (*rr_cur_len);
if((status=sldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len,
rdftype)) != 0)
return RET_ERR_SHIFT(status,
sldns_buffer_position(strbuf)-strlen(token));
*rr_cur_len += len;
return LDNS_WIREPARSE_ERR_OK;
}
/**
* Parse one rdf token. Takes care of quotes and parenthesis.
*/
static int
sldns_parse_rdf_token(sldns_buffer* strbuf, char* token, size_t token_len,
int* quoted, int* parens, size_t* pre_data_pos,
const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
{
size_t slen;
/* skip spaces */
while(sldns_buffer_remaining(strbuf) > 0 && !*quoted &&
*(sldns_buffer_current(strbuf)) == ' ') {
sldns_buffer_skip(strbuf, 1);
}
*pre_data_pos = sldns_buffer_position(strbuf);
if(sldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters,
token_len, parens, (*quoted)?NULL:" \t") == -1) {
return 0;
}
slen = strlen(token);
/* check if not quoted yet, and we have encountered quotes */
if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
slen >= 2 &&
(token[0] == '"' || token[0] == '\'') &&
(token[slen-1] == '"' || token[slen-1] == '\'')) {
/* move token two smaller (quotes) with endnull */
memmove(token, token+1, slen-2);
token[slen-2] = 0;
slen -= 2;
*quoted = 1;
} else if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
slen >= 2 &&
(token[0] == '"' || token[0] == '\'')) {
/* got the start quote (remove it) but read remainder
* of quoted string as well into remainder of token */
memmove(token, token+1, slen-1);
token[slen-1] = 0;
slen -= 1;
*quoted = 1;
/* rewind buffer over skipped whitespace */
while(sldns_buffer_position(strbuf) > 0 &&
(sldns_buffer_current(strbuf)[-1] == ' ' ||
sldns_buffer_current(strbuf)[-1] == '\t')) {
sldns_buffer_skip(strbuf, -1);
}
if(sldns_bget_token_par(strbuf, token+slen,
"\"", token_len-slen,
parens, NULL) == -1) {
return 0;
}
slen = strlen(token);
}
*token_strlen = slen;
return 1;
}
/** Add space and one more rdf token onto the existing token string. */
static int
sldns_affix_token(sldns_buffer* strbuf, char* token, size_t* token_len,
int* quoted, int* parens, size_t* pre_data_pos,
const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
{
size_t addlen = *token_len - *token_strlen;
size_t addstrlen = 0;
/* add space */
if(addlen < 1) return 0;
token[*token_strlen] = ' ';
token[++(*token_strlen)] = 0;
/* read another token */
addlen = *token_len - *token_strlen;
if(!sldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted,
parens, pre_data_pos, delimiters, rdftype, &addstrlen))
return 0;
(*token_strlen) += addstrlen;
return 1;
}
/** parse rdata from string into rr buffer(-remainder after dname). */
static int
rrinternal_parse_rdata(sldns_buffer* strbuf, char* token, size_t token_len,
uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type,
uint8_t* origin, size_t origin_len)
{
const sldns_rr_descriptor *desc = sldns_rr_descript((uint16_t)rr_type);
size_t r_cnt, r_min, r_max;
size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen;
int was_unknown_rr_format = 0, parens = 0, status, quoted;
const char* delimiters;
sldns_rdf_type rdftype;
/* a desc is always returned */
if(!desc) return LDNS_WIREPARSE_ERR_GENERAL;
r_max = sldns_rr_descriptor_maximum(desc);
r_min = sldns_rr_descriptor_minimum(desc);
/* robust check */
if(rr_cur_len > *rr_len)
return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
sldns_buffer_position(strbuf));
/* because number of fields can be variable, we can't rely on
* _maximum() only */
for(r_cnt=0; r_cnt < r_max; r_cnt++) {
rdftype = sldns_rr_descriptor_field_type(desc, r_cnt);
delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max);
quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype);
if(!sldns_parse_rdf_token(strbuf, token, token_len, "ed,
&parens, &pre_data_pos, delimiters, rdftype,
&token_strlen))
break;
/* rfc3597 specifies that any type can be represented
* with \# method, which can contain spaces...
* it does specify size though... */
/* unknown RR data */
if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 &&
!quoted && (token_strlen == 2 || token[2]==' ')) {
was_unknown_rr_format = 1;
if((status=rrinternal_parse_unknown(strbuf, token,
token_len, rr, rr_len, &rr_cur_len,
pre_data_pos)) != 0)
return status;
} else if(token_strlen > 0 || quoted) {
if(rdftype == LDNS_RDF_TYPE_HIP) {
/* affix the HIT and PK fields, with a space */
if(!sldns_affix_token(strbuf, token,
&token_len, "ed, &parens,
&pre_data_pos, delimiters,
rdftype, &token_strlen))
break;
if(!sldns_affix_token(strbuf, token,
&token_len, "ed, &parens,
&pre_data_pos, delimiters,
rdftype, &token_strlen))
break;
} else if(rdftype == LDNS_RDF_TYPE_INT16_DATA &&
strcmp(token, "0")!=0) {
/* affix len and b64 fields */
if(!sldns_affix_token(strbuf, token,
&token_len, "ed, &parens,
&pre_data_pos, delimiters,
rdftype, &token_strlen))
break;
}
/* normal RR */
if((status=rrinternal_parse_rdf(strbuf, token,
token_len, rr, *rr_len, &rr_cur_len, rdftype,
rr_type, r_cnt, r_max, dname_len, origin,
origin_len)) != 0) {
return status;
}
}
}
if(!was_unknown_rr_format && r_cnt+1 < r_min) {
return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE,
sldns_buffer_position(strbuf));
}
while(parens != 0) {
/* read remainder, must be "" */
if(sldns_bget_token_par(strbuf, token, "\n", token_len,
&parens, " \t") == -1) {
if(parens != 0)
return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
sldns_buffer_position(strbuf));
break;
}
if(strcmp(token, "") != 0)
return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
sldns_buffer_position(strbuf));
}
/* write rdata length */
sldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10));
*rr_len = rr_cur_len;
return LDNS_WIREPARSE_ERR_OK;
}
/*
* trailing spaces are allowed
* leading spaces are not allowed
* allow ttl to be optional
* class is optional too
* if ttl is missing, and default_ttl is 0, use DEF_TTL
* allow ttl to be written as 1d3h
* So the RR should look like. e.g.
* miek.nl. 3600 IN MX 10 elektron.atoom.net
* or
* miek.nl. 1h IN MX 10 elektron.atoom.net
* or
* miek.nl. IN MX 10 elektron.atoom.net
*/
static int
sldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len,
size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
size_t origin_len, uint8_t* prev, size_t prev_len, int question)
{
int status;
int not_there = 0;
char token[LDNS_MAX_RDFLEN+1];
uint32_t ttl = 0;
uint16_t tp = 0, cl = 0;
size_t ddlen = 0;
/* string in buffer */
sldns_buffer strbuf;
sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
if(!dname_len) dname_len = &ddlen;
/* parse the owner */
if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin,
origin_len, prev, prev_len, token, sizeof(token))) != 0)
return status;
/* parse the [ttl] [class] <type> */
if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token),
¬_there, &ttl, default_ttl)) != 0)
return status;
if((status=rrinternal_get_class(&strbuf, token, sizeof(token),
¬_there, &cl)) != 0)
return status;
if((status=rrinternal_get_type(&strbuf, token, sizeof(token),
¬_there, &tp)) != 0)
return status;
/* put ttl, class, type into the rr result */
if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl,
ttl, question)) != 0)
return status;
/* for a question-RR we are done, no rdata */
if(question) {
*len = *dname_len + 4;
return LDNS_WIREPARSE_ERR_OK;
}
/* rdata */
if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token),
rr, len, *dname_len, tp, origin, origin_len)) != 0)
return status;
return LDNS_WIREPARSE_ERR_OK;
}
int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
size_t origin_len, uint8_t* prev, size_t prev_len)
{
return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
default_ttl, origin, origin_len, prev, prev_len, 0);
}
int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
size_t prev_len)
{
return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
0, origin, origin_len, prev, prev_len, 1);
}
uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+2)
return 0;
return sldns_read_uint16(rr+dname_len);
}
uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+4)
return 0;
return sldns_read_uint16(rr+dname_len+2);
}
uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+8)
return 0;
return sldns_read_uint32(rr+dname_len+4);
}
uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+10)
return 0;
return sldns_read_uint16(rr+dname_len+8);
}
uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+10)
return NULL;
return rr+dname_len+10;
}
uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len)
{
if(len < dname_len+10)
return NULL;
return rr+dname_len+8;
}
const char* sldns_get_errorstr_parse(int e)
{
sldns_lookup_table *lt;
lt = sldns_lookup_by_id(sldns_wireparse_errors, LDNS_WIREPARSE_ERROR(e));
return lt?lt->name:"unknown error";
}
/* Strip whitespace from the start and the end of <line>. */
char *
sldns_strip_ws(char *line)
{
char *s = line, *e;
for (s = line; *s && isspace((unsigned char)*s); s++)
;
for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
;
*e = 0;
return s;
}
int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
struct sldns_file_parse_state* parse_state)
{
char line[LDNS_RR_BUF_SIZE+1];
ssize_t size;
/* read an entire line in from the file */
if((size = sldns_fget_token_l(in, line, LDNS_PARSE_SKIP_SPACE,
LDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL))
== -1) {
/* if last line was empty, we are now at feof, which is not
* always a parse error (happens when for instance last line
* was a comment)
*/
return LDNS_WIREPARSE_ERR_SYNTAX;
}
/* we can have the situation, where we've read ok, but still got
* no bytes to play with, in this case size is 0 */
if(size == 0) {
if(*len > 0)
rr[0] = 0;
*len = 0;
*dname_len = 0;
return LDNS_WIREPARSE_ERR_OK;
}
if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
int s;
strlcpy((char*)rr, line, *len);
*len = 0;
*dname_len = 0;
if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
parse_state->origin_len = sizeof(parse_state->origin);
s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8),
parse_state->origin, &parse_state->origin_len);
if(s) parse_state->origin_len = 0;
return s;
} else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
const char* end = NULL;
strlcpy((char*)rr, line, *len);
*len = 0;
*dname_len = 0;
if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
parse_state->default_ttl = sldns_str2period(
sldns_strip_ws(line+5), &end);
} else if (strncmp(line, "$INCLUDE", 8) == 0) {
strlcpy((char*)rr, line, *len);
*len = 0;
*dname_len = 0;
return LDNS_WIREPARSE_ERR_INCLUDE;
} else if (strncmp(line, "$", 1) == 0) {
strlcpy((char*)rr, line, *len);
*len = 0;
*dname_len = 0;
return LDNS_WIREPARSE_ERR_INCLUDE;
} else {
int r = sldns_str2wire_rr_buf(line, rr, len, dname_len,
parse_state?parse_state->default_ttl:0,
(parse_state&&parse_state->origin_len)?
parse_state->origin:NULL,
parse_state?parse_state->origin_len:0,
(parse_state&&parse_state->prev_rr_len)?
parse_state->prev_rr:NULL,
parse_state?parse_state->prev_rr_len:0);
if(r == LDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 &&
parse_state &&
(*dname_len) <= sizeof(parse_state->prev_rr)) {
memmove(parse_state->prev_rr, rr, *dname_len);
parse_state->prev_rr_len = (*dname_len);
}
return r;
}
return LDNS_WIREPARSE_ERR_OK;
}
int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
sldns_rdf_type rdftype)
{
switch (rdftype) {
case LDNS_RDF_TYPE_DNAME:
return sldns_str2wire_dname_buf(str, rd, len);
case LDNS_RDF_TYPE_INT8:
return sldns_str2wire_int8_buf(str, rd, len);
case LDNS_RDF_TYPE_INT16:
return sldns_str2wire_int16_buf(str, rd, len);
case LDNS_RDF_TYPE_INT32:
return sldns_str2wire_int32_buf(str, rd, len);
case LDNS_RDF_TYPE_A:
return sldns_str2wire_a_buf(str, rd, len);
case LDNS_RDF_TYPE_AAAA:
return sldns_str2wire_aaaa_buf(str, rd, len);
case LDNS_RDF_TYPE_STR:
return sldns_str2wire_str_buf(str, rd, len);
case LDNS_RDF_TYPE_APL:
return sldns_str2wire_apl_buf(str, rd, len);
case LDNS_RDF_TYPE_B64:
return sldns_str2wire_b64_buf(str, rd, len);
case LDNS_RDF_TYPE_B32_EXT:
return sldns_str2wire_b32_ext_buf(str, rd, len);
case LDNS_RDF_TYPE_HEX:
return sldns_str2wire_hex_buf(str, rd, len);
case LDNS_RDF_TYPE_NSEC:
return sldns_str2wire_nsec_buf(str, rd, len);
case LDNS_RDF_TYPE_TYPE:
return sldns_str2wire_type_buf(str, rd, len);
case LDNS_RDF_TYPE_CLASS:
return sldns_str2wire_class_buf(str, rd, len);
case LDNS_RDF_TYPE_CERT_ALG:
return sldns_str2wire_cert_alg_buf(str, rd, len);
case LDNS_RDF_TYPE_ALG:
return sldns_str2wire_alg_buf(str, rd, len);
case LDNS_RDF_TYPE_TIME:
return sldns_str2wire_time_buf(str, rd, len);
case LDNS_RDF_TYPE_PERIOD:
return sldns_str2wire_period_buf(str, rd, len);
case LDNS_RDF_TYPE_TSIGTIME:
return sldns_str2wire_tsigtime_buf(str, rd, len);
case LDNS_RDF_TYPE_LOC:
return sldns_str2wire_loc_buf(str, rd, len);
case LDNS_RDF_TYPE_WKS:
return sldns_str2wire_wks_buf(str, rd, len);
case LDNS_RDF_TYPE_NSAP:
return sldns_str2wire_nsap_buf(str, rd, len);
case LDNS_RDF_TYPE_ATMA:
return sldns_str2wire_atma_buf(str, rd, len);
case LDNS_RDF_TYPE_IPSECKEY:
return sldns_str2wire_ipseckey_buf(str, rd, len);
case LDNS_RDF_TYPE_NSEC3_SALT:
return sldns_str2wire_nsec3_salt_buf(str, rd, len);
case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER:
return sldns_str2wire_b32_ext_buf(str, rd, len);
case LDNS_RDF_TYPE_ILNP64:
return sldns_str2wire_ilnp64_buf(str, rd, len);
case LDNS_RDF_TYPE_EUI48:
return sldns_str2wire_eui48_buf(str, rd, len);
case LDNS_RDF_TYPE_EUI64:
return sldns_str2wire_eui64_buf(str, rd, len);
case LDNS_RDF_TYPE_TAG:
return sldns_str2wire_tag_buf(str, rd, len);
case LDNS_RDF_TYPE_LONG_STR:
return sldns_str2wire_long_str_buf(str, rd, len);
case LDNS_RDF_TYPE_TSIGERROR:
return sldns_str2wire_tsigerror_buf(str, rd, len);
case LDNS_RDF_TYPE_HIP:
return sldns_str2wire_hip_buf(str, rd, len);
case LDNS_RDF_TYPE_INT16_DATA:
return sldns_str2wire_int16_data_buf(str, rd, len);
case LDNS_RDF_TYPE_UNKNOWN: