forked from NLnetLabs/ldns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr.c
2760 lines (2461 loc) · 85.2 KB
/
rr.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
/* rr.c
*
* access functions for ldns_rr -
* a Net::DNS like library for C
* LibDNS Team @ NLnet Labs
*
* (c) NLnet Labs, 2004-2006
* See the file LICENSE for the license
*/
#include <ldns/config.h>
#include <ldns/ldns.h>
#include <strings.h>
#include <limits.h>
#include <errno.h>
#define LDNS_SYNTAX_DATALEN 16
#define LDNS_TTL_DATALEN 21
#define LDNS_RRLIST_INIT 8
ldns_rr *
ldns_rr_new(void)
{
ldns_rr *rr;
rr = LDNS_MALLOC(ldns_rr);
if (!rr) {
return NULL;
}
ldns_rr_set_owner(rr, NULL);
ldns_rr_set_question(rr, false);
ldns_rr_set_rd_count(rr, 0);
rr->_rdata_fields = NULL;
ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
ldns_rr_set_ttl(rr, LDNS_DEFAULT_TTL);
return rr;
}
ldns_rr *
ldns_rr_new_frm_type(ldns_rr_type t)
{
ldns_rr *rr;
const ldns_rr_descriptor *desc;
size_t i;
rr = LDNS_MALLOC(ldns_rr);
if (!rr) {
return NULL;
}
desc = ldns_rr_descript(t);
rr->_rdata_fields = LDNS_XMALLOC(ldns_rdf *, ldns_rr_descriptor_minimum(desc));
if(!rr->_rdata_fields) {
LDNS_FREE(rr);
return NULL;
}
for (i = 0; i < ldns_rr_descriptor_minimum(desc); i++) {
rr->_rdata_fields[i] = NULL;
}
ldns_rr_set_owner(rr, NULL);
ldns_rr_set_question(rr, false);
/* set the count to minimum */
ldns_rr_set_rd_count(rr, ldns_rr_descriptor_minimum(desc));
ldns_rr_set_class(rr, LDNS_RR_CLASS_IN);
ldns_rr_set_ttl(rr, LDNS_DEFAULT_TTL);
ldns_rr_set_type(rr, t);
return rr;
}
void
ldns_rr_free(ldns_rr *rr)
{
size_t i;
if (rr) {
if (ldns_rr_owner(rr)) {
ldns_rdf_deep_free(ldns_rr_owner(rr));
}
for (i = 0; i < ldns_rr_rd_count(rr); i++) {
ldns_rdf_deep_free(ldns_rr_rdf(rr, i));
}
LDNS_FREE(rr->_rdata_fields);
LDNS_FREE(rr);
}
}
/* Syntactic sugar for ldns_rr_new_frm_str_internal */
INLINE bool
ldns_rdf_type_maybe_quoted(ldns_rdf_type rdf_type)
{
return rdf_type == LDNS_RDF_TYPE_STR ||
rdf_type == LDNS_RDF_TYPE_LONG_STR;
}
/*
* 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 ldns_status
ldns_rr_new_frm_str_internal(ldns_rr **newrr, const char *str,
uint32_t default_ttl, const ldns_rdf *origin,
ldns_rdf **prev, bool question)
{
ldns_rr *new;
const ldns_rr_descriptor *desc;
ldns_rr_type rr_type;
ldns_buffer *rr_buf = NULL;
ldns_buffer *rd_buf = NULL;
uint32_t ttl_val;
char *owner = NULL;
char *ttl = NULL;
ldns_rr_class clas_val;
char *clas = NULL;
char *type = NULL;
size_t type_sz;
char *rdata = NULL;
char *rd = NULL;
char *xtok = NULL; /* For RDF types with spaces (i.e. extra tokens) */
size_t rd_strlen;
const char *delimiters;
ssize_t c;
ldns_rdf *owner_dname;
const char* endptr;
int was_unknown_rr_format = 0;
ldns_status status = LDNS_STATUS_OK;
/* used for types with unknown number of rdatas */
bool done;
bool quoted;
ldns_rdf *r = NULL;
uint16_t r_cnt;
uint16_t r_min;
uint16_t r_max;
size_t pre_data_pos;
uint16_t hex_data_size;
char *hex_data_str = NULL;
uint16_t cur_hex_data_size;
size_t hex_pos = 0;
uint8_t *hex_data = NULL;
new = ldns_rr_new();
owner = LDNS_XMALLOC(char, LDNS_MAX_DOMAINLEN + 1);
ttl = LDNS_XMALLOC(char, LDNS_TTL_DATALEN);
clas = LDNS_XMALLOC(char, LDNS_SYNTAX_DATALEN);
rdata = LDNS_XMALLOC(char, LDNS_MAX_PACKETLEN + 1);
rr_buf = LDNS_MALLOC(ldns_buffer);
rd_buf = LDNS_MALLOC(ldns_buffer);
rd = LDNS_XMALLOC(char, LDNS_MAX_RDFLEN);
xtok = LDNS_XMALLOC(char, LDNS_MAX_RDFLEN);
if (rr_buf) {
rr_buf->_data = NULL;
}
if (rd_buf) {
rd_buf->_data = NULL;
}
if (!new || !owner || !ttl || !clas || !rdata ||
!rr_buf || !rd_buf || !rd || !xtok) {
goto memerror;
}
ldns_buffer_new_frm_data(rr_buf, (char*)str, strlen(str));
/* split the rr in its parts -1 signals trouble */
if (ldns_bget_token(rr_buf, owner, "\t\n ", LDNS_MAX_DOMAINLEN) == -1){
status = LDNS_STATUS_SYNTAX_ERR;
goto error;
}
if (ldns_bget_token(rr_buf, ttl, "\t\n ", LDNS_TTL_DATALEN) == -1) {
status = LDNS_STATUS_SYNTAX_TTL_ERR;
goto error;
}
ttl_val = (uint32_t) ldns_str2period(ttl, &endptr);
if (strlen(ttl) > 0 && !isdigit((int) ttl[0])) {
/* ah, it's not there or something */
if (default_ttl == 0) {
ttl_val = LDNS_DEFAULT_TTL;
} else {
ttl_val = default_ttl;
}
/* we not ASSUMING the TTL is missing and that
* the rest of the RR is still there. That is
* CLASS TYPE RDATA
* so ttl value we read is actually the class
*/
clas_val = ldns_get_rr_class_by_name(ttl);
/* class can be left out too, assume IN, current
* token must be type
*/
if (clas_val == 0) {
clas_val = LDNS_RR_CLASS_IN;
type_sz = strlen(ttl) + 1;
type = LDNS_XMALLOC(char, type_sz);
if (!type) {
goto memerror;
}
strlcpy(type, ttl, type_sz);
}
} else {
if (-1 == ldns_bget_token(
rr_buf, clas, "\t\n ", LDNS_SYNTAX_DATALEN)) {
status = LDNS_STATUS_SYNTAX_CLASS_ERR;
goto error;
}
clas_val = ldns_get_rr_class_by_name(clas);
/* class can be left out too, assume IN, current
* token must be type
*/
if (clas_val == 0) {
clas_val = LDNS_RR_CLASS_IN;
type_sz = strlen(clas) + 1;
type = LDNS_XMALLOC(char, type_sz);
if (!type) {
goto memerror;
}
strlcpy(type, clas, type_sz);
}
}
/* the rest should still be waiting for us */
if (!type) {
type = LDNS_XMALLOC(char, LDNS_SYNTAX_DATALEN);
if (!type) {
goto memerror;
}
if (-1 == ldns_bget_token(
rr_buf, type, "\t\n ", LDNS_SYNTAX_DATALEN)) {
status = LDNS_STATUS_SYNTAX_TYPE_ERR;
goto error;
}
}
if (ldns_bget_token(rr_buf, rdata, "\0", LDNS_MAX_PACKETLEN) == -1) {
/* apparently we are done, and it's only a question RR
* so do not set status and go to ldnserror here
*/
}
ldns_buffer_new_frm_data(rd_buf, rdata, strlen(rdata));
if (strlen(owner) <= 1 && strncmp(owner, "@", 1) == 0) {
if (origin) {
ldns_rr_set_owner(new, ldns_rdf_clone(origin));
} else if (prev && *prev) {
ldns_rr_set_owner(new, ldns_rdf_clone(*prev));
} else {
/* default to root */
ldns_rr_set_owner(new, ldns_dname_new_frm_str("."));
}
/* @ also overrides prev */
if (prev) {
ldns_rdf_deep_free(*prev);
*prev = ldns_rdf_clone(ldns_rr_owner(new));
if (!*prev) {
goto memerror;
}
}
} else {
if (strlen(owner) == 0) {
/* no ownername was given, try prev, if that fails
* origin, else default to root */
if (prev && *prev) {
ldns_rr_set_owner(new, ldns_rdf_clone(*prev));
} else if (origin) {
ldns_rr_set_owner(new, ldns_rdf_clone(origin));
} else {
ldns_rr_set_owner(new,
ldns_dname_new_frm_str("."));
}
if(!ldns_rr_owner(new)) {
goto memerror;
}
} else {
owner_dname = ldns_dname_new_frm_str(owner);
if (!owner_dname) {
status = LDNS_STATUS_SYNTAX_ERR;
goto error;
}
ldns_rr_set_owner(new, owner_dname);
if (!ldns_dname_str_absolute(owner) && origin) {
if(ldns_dname_cat(ldns_rr_owner(new), origin)
!= LDNS_STATUS_OK) {
status = LDNS_STATUS_SYNTAX_ERR;
goto error;
}
}
if (prev) {
ldns_rdf_deep_free(*prev);
*prev = ldns_rdf_clone(ldns_rr_owner(new));
if (!*prev) {
goto error;
}
}
}
}
LDNS_FREE(owner);
ldns_rr_set_question(new, question);
ldns_rr_set_ttl(new, ttl_val);
LDNS_FREE(ttl);
ldns_rr_set_class(new, clas_val);
LDNS_FREE(clas);
rr_type = ldns_get_rr_type_by_name(type);
LDNS_FREE(type);
desc = ldns_rr_descript((uint16_t)rr_type);
ldns_rr_set_type(new, rr_type);
if (desc) {
/* only the rdata remains */
r_max = ldns_rr_descriptor_maximum(desc);
r_min = ldns_rr_descriptor_minimum(desc);
} else {
r_min = 0;
r_max = 1;
}
for (done = false, r_cnt = 0; !done && r_cnt < r_max; r_cnt++) {
quoted = false;
switch (ldns_rr_descriptor_field_type(desc, r_cnt)) {
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_AMTRELAY :
case LDNS_RDF_TYPE_NSEC : if (r_cnt == r_max - 1) {
delimiters = "\n";
break;
}
/* fallthrough */
default : delimiters = "\n\t ";
}
if (ldns_rdf_type_maybe_quoted(
ldns_rr_descriptor_field_type(
desc, r_cnt)) &&
ldns_buffer_remaining(rd_buf) > 0){
/* skip spaces */
while (*(ldns_buffer_current(rd_buf)) == ' ') {
ldns_buffer_skip(rd_buf, 1);
}
if (*(ldns_buffer_current(rd_buf)) == '\"') {
delimiters = "\"\0";
ldns_buffer_skip(rd_buf, 1);
quoted = true;
} else if (ldns_rr_descriptor_field_type(desc, r_cnt)
== LDNS_RDF_TYPE_LONG_STR) {
status = LDNS_STATUS_SYNTAX_RDATA_ERR;
goto error;
}
}
/* because number of fields can be variable, we can't rely on
* _maximum() only
*/
/* skip spaces */
while (ldns_buffer_position(rd_buf) < ldns_buffer_limit(rd_buf)
&& *(ldns_buffer_current(rd_buf)) == ' '
&& !quoted) {
ldns_buffer_skip(rd_buf, 1);
}
pre_data_pos = ldns_buffer_position(rd_buf);
if (-1 == (c = ldns_bget_token(
rd_buf, rd, delimiters, LDNS_MAX_RDFLEN))) {
done = true;
(void)done; /* we're breaking, so done not read anymore */
break;
}
/* hmmz, rfc3597 specifies that any type can be represented
* with \# method, which can contain spaces...
* it does specify size though...
*/
rd_strlen = strlen(rd);
/* unknown RR data */
if (strncmp(rd, "\\#", 2) == 0 && !quoted &&
(rd_strlen == 2 || rd[2]==' ')) {
was_unknown_rr_format = 1;
/* go back to before \#
* and skip it while setting delimiters better
*/
ldns_buffer_set_position(rd_buf, pre_data_pos);
delimiters = "\n\t ";
(void)ldns_bget_token(rd_buf, rd,
delimiters, LDNS_MAX_RDFLEN);
/* read rdata octet length */
c = ldns_bget_token(rd_buf, rd,
delimiters, LDNS_MAX_RDFLEN);
if (c == -1) {
/* something goes very wrong here */
status = LDNS_STATUS_SYNTAX_RDATA_ERR;
goto error;
}
hex_data_size = (uint16_t) atoi(rd);
/* copy hex chars into hex str (2 chars per byte) */
hex_data_str = LDNS_XMALLOC(char, 2*hex_data_size + 1);
if (!hex_data_str) {
/* malloc error */
goto memerror;
}
cur_hex_data_size = 0;
while(cur_hex_data_size < 2 * hex_data_size) {
c = ldns_bget_token(rd_buf, rd,
delimiters, LDNS_MAX_RDFLEN);
if (c == -1) {
status = LDNS_STATUS_SYNTAX_RDATA_ERR;
goto error;
}
rd_strlen = strlen(rd);
if ((size_t)cur_hex_data_size + rd_strlen >
2 * (size_t)hex_data_size) {
status = LDNS_STATUS_SYNTAX_RDATA_ERR;
goto error;
}
strlcpy(hex_data_str + cur_hex_data_size, rd,
rd_strlen + 1);
cur_hex_data_size += rd_strlen;
}
hex_data_str[cur_hex_data_size] = '\0';
/* correct the rdf type */
/* if *we* know the type, interpret it as wireformat */
if (desc) {
hex_pos = 0;
hex_data =
LDNS_XMALLOC(uint8_t, hex_data_size+2);
if (!hex_data) {
goto memerror;
}
ldns_write_uint16(hex_data, hex_data_size);
ldns_hexstring_to_data(
hex_data + 2, hex_data_str);
status = ldns_wire2rdf(new, hex_data,
hex_data_size + 2, &hex_pos);
if (status != LDNS_STATUS_OK) {
goto error;
}
LDNS_FREE(hex_data);
} else {
r = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_HEX,
hex_data_str);
if (!r) {
goto memerror;
}
ldns_rdf_set_type(r, LDNS_RDF_TYPE_UNKNOWN);
if (!ldns_rr_push_rdf(new, r)) {
goto memerror;
}
}
LDNS_FREE(hex_data_str);
} else if(rd_strlen > 0 || quoted) {
/* Normal RR */
switch(ldns_rr_descriptor_field_type(desc, r_cnt)) {
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) {
c = ldns_bget_token(rd_buf, xtok,
"\n", LDNS_MAX_RDFLEN);
if (c != -1) {
(void) strncat(rd, xtok,
LDNS_MAX_RDFLEN -
strlen(rd) - 1);
}
}
r = ldns_rdf_new_frm_str(
ldns_rr_descriptor_field_type(
desc, r_cnt), rd);
break;
case LDNS_RDF_TYPE_HIP:
/*
* In presentation format this RDATA type has
* three tokens: An algorithm byte, then a
* variable length HIT (in hexbytes) and then
* a variable length Public Key (in base64).
*
* We have just read the algorithm, so we need
* two more tokens: HIT and Public Key.
*/
do {
/* Read and append HIT */
if (ldns_bget_token(rd_buf,
xtok, delimiters,
LDNS_MAX_RDFLEN) == -1)
break;
(void) strncat(rd, " ",
LDNS_MAX_RDFLEN -
strlen(rd) - 1);
(void) strncat(rd, xtok,
LDNS_MAX_RDFLEN -
strlen(rd) - 1);
/* Read and append Public Key*/
if (ldns_bget_token(rd_buf,
xtok, delimiters,
LDNS_MAX_RDFLEN) == -1)
break;
(void) strncat(rd, " ",
LDNS_MAX_RDFLEN -
strlen(rd) - 1);
(void) strncat(rd, xtok,
LDNS_MAX_RDFLEN -
strlen(rd) - 1);
} while (false);
r = ldns_rdf_new_frm_str(
ldns_rr_descriptor_field_type(
desc, r_cnt), rd);
break;
case LDNS_RDF_TYPE_DNAME:
r = ldns_rdf_new_frm_str(
ldns_rr_descriptor_field_type(
desc, r_cnt), rd);
/* check if the origin should be used
* or concatenated
*/
if (r && ldns_rdf_size(r) > 1 &&
ldns_rdf_data(r)[0] == 1 &&
ldns_rdf_data(r)[1] == '@') {
ldns_rdf_deep_free(r);
r = origin ? ldns_rdf_clone(origin)
: ( rr_type == LDNS_RR_TYPE_SOA ?
ldns_rdf_clone(
ldns_rr_owner(new))
: ldns_rdf_new_frm_str(
LDNS_RDF_TYPE_DNAME, ".")
);
} else if (r && rd_strlen >= 1 && origin &&
!ldns_dname_str_absolute(rd)) {
status = ldns_dname_cat(r, origin);
if (status != LDNS_STATUS_OK) {
goto error;
}
}
break;
default:
r = ldns_rdf_new_frm_str(
ldns_rr_descriptor_field_type(
desc, r_cnt), rd);
break;
}
if (!r) {
status = LDNS_STATUS_SYNTAX_RDATA_ERR;
goto error;
}
ldns_rr_push_rdf(new, r);
}
if (quoted) {
if (ldns_buffer_available(rd_buf, 1)) {
ldns_buffer_skip(rd_buf, 1);
} else {
done = true;
}
}
} /* for (done = false, r_cnt = 0; !done && r_cnt < r_max; r_cnt++) */
LDNS_FREE(rd);
LDNS_FREE(xtok);
ldns_buffer_free(rr_buf);
LDNS_FREE(rdata);
if (ldns_buffer_remaining(rd_buf) > 0) {
ldns_buffer_free(rd_buf);
ldns_rr_free(new);
return LDNS_STATUS_SYNTAX_SUPERFLUOUS_TEXT_ERR;
}
ldns_buffer_free(rd_buf);
if (!question && desc && !was_unknown_rr_format &&
ldns_rr_rd_count(new) < r_min) {
ldns_rr_free(new);
return LDNS_STATUS_SYNTAX_MISSING_VALUE_ERR;
}
if (newrr) {
*newrr = new;
} else {
/* Maybe the caller just wanted to see if it would parse? */
ldns_rr_free(new);
}
return LDNS_STATUS_OK;
memerror:
status = LDNS_STATUS_MEM_ERR;
error:
if (rd_buf && rd_buf->_data) {
ldns_buffer_free(rd_buf);
} else {
LDNS_FREE(rd_buf);
}
if (rr_buf && rr_buf->_data) {
ldns_buffer_free(rr_buf);
} else {
LDNS_FREE(rr_buf);
}
LDNS_FREE(type);
LDNS_FREE(owner);
LDNS_FREE(ttl);
LDNS_FREE(clas);
LDNS_FREE(hex_data);
LDNS_FREE(hex_data_str);
LDNS_FREE(xtok);
LDNS_FREE(rd);
LDNS_FREE(rdata);
ldns_rr_free(new);
return status;
}
ldns_status
ldns_rr_new_frm_str(ldns_rr **newrr, const char *str,
uint32_t default_ttl, const ldns_rdf *origin,
ldns_rdf **prev)
{
return ldns_rr_new_frm_str_internal(newrr,
str,
default_ttl,
origin,
prev,
false);
}
ldns_status
ldns_rr_new_question_frm_str(ldns_rr **newrr, const char *str,
const ldns_rdf *origin, ldns_rdf **prev)
{
return ldns_rr_new_frm_str_internal(newrr,
str,
0,
origin,
prev,
true);
}
/* Strip whitespace from the start and the end of <line>. */
static char *
ldns_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;
}
ldns_status
ldns_rr_new_frm_fp(ldns_rr **newrr, FILE *fp, uint32_t *ttl, ldns_rdf **origin, ldns_rdf **prev)
{
return ldns_rr_new_frm_fp_l(newrr, fp, ttl, origin, prev, NULL);
}
ldns_status
ldns_rr_new_frm_fp_l(ldns_rr **newrr, FILE *fp, uint32_t *default_ttl, ldns_rdf **origin, ldns_rdf **prev, int *line_nr)
{
char *line;
const char *endptr; /* unused */
ldns_rr *rr;
uint32_t ttl;
ldns_rdf *tmp;
ldns_status s;
ssize_t size;
if (default_ttl) {
ttl = *default_ttl;
} else {
ttl = 0;
}
line = LDNS_XMALLOC(char, LDNS_MAX_LINELEN + 1);
if (!line) {
return LDNS_STATUS_MEM_ERR;
}
/* read an entire line in from the file */
if ((size = ldns_fget_token_l(fp, line, LDNS_PARSE_SKIP_SPACE, LDNS_MAX_LINELEN, line_nr)) == -1) {
LDNS_FREE(line);
/* 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_STATUS_SYNTAX_ERR;
}
/* 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) {
LDNS_FREE(line);
return LDNS_STATUS_SYNTAX_EMPTY;
}
if (strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
if (*origin) {
ldns_rdf_deep_free(*origin);
*origin = NULL;
}
tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
ldns_strip_ws(line + 8));
if (!tmp) {
/* could not parse what next to $ORIGIN */
LDNS_FREE(line);
return LDNS_STATUS_SYNTAX_DNAME_ERR;
}
*origin = tmp;
s = LDNS_STATUS_SYNTAX_ORIGIN;
} else if (strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
if (default_ttl) {
*default_ttl = ldns_str2period(
ldns_strip_ws(line + 5), &endptr);
}
s = LDNS_STATUS_SYNTAX_TTL;
} else if (strncmp(line, "$INCLUDE", 8) == 0) {
s = LDNS_STATUS_SYNTAX_INCLUDE;
} else if (!*ldns_strip_ws(line)) {
LDNS_FREE(line);
return LDNS_STATUS_SYNTAX_EMPTY;
} else {
if (origin && *origin) {
s = ldns_rr_new_frm_str(&rr, (const char*) line, ttl, *origin, prev);
} else {
s = ldns_rr_new_frm_str(&rr, (const char*) line, ttl, NULL, prev);
}
}
LDNS_FREE(line);
if (s == LDNS_STATUS_OK) {
if (newrr) {
*newrr = rr;
} else {
/* Just testing if it would parse? */
ldns_rr_free(rr);
}
}
return s;
}
void
ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner)
{
rr->_owner = owner;
}
void
ldns_rr_set_question(ldns_rr *rr, bool question)
{
rr->_rr_question = question;
}
void
ldns_rr_set_ttl(ldns_rr *rr, uint32_t ttl)
{
rr->_ttl = ttl;
}
void
ldns_rr_set_rd_count(ldns_rr *rr, size_t count)
{
rr->_rd_count = count;
}
void
ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type)
{
rr->_rr_type = rr_type;
}
void
ldns_rr_set_class(ldns_rr *rr, ldns_rr_class rr_class)
{
rr->_rr_class = rr_class;
}
ldns_rdf *
ldns_rr_set_rdf(ldns_rr *rr, const ldns_rdf *f, size_t position)
{
size_t rd_count;
ldns_rdf *pop;
rd_count = ldns_rr_rd_count(rr);
if (position < rd_count) {
/* dicard the old one */
pop = rr->_rdata_fields[position];
rr->_rdata_fields[position] = (ldns_rdf*)f;
return pop;
} else {
return NULL;
}
}
bool
ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f)
{
size_t rd_count;
ldns_rdf **rdata_fields;
rd_count = ldns_rr_rd_count(rr);
/* grow the array */
rdata_fields = LDNS_XREALLOC(
rr->_rdata_fields, ldns_rdf *, rd_count + 1);
if (!rdata_fields) {
return false;
}
/* add the new member */
rr->_rdata_fields = rdata_fields;
rr->_rdata_fields[rd_count] = (ldns_rdf*)f;
ldns_rr_set_rd_count(rr, rd_count + 1);
return true;
}
ldns_rdf *
ldns_rr_pop_rdf(ldns_rr *rr)
{
size_t rd_count;
ldns_rdf *pop;
ldns_rdf** newrd;
rd_count = ldns_rr_rd_count(rr);
if (rd_count == 0) {
return NULL;
}
pop = rr->_rdata_fields[rd_count - 1];
/* try to shrink the array */
if(rd_count > 1) {
newrd = LDNS_XREALLOC(
rr->_rdata_fields, ldns_rdf *, rd_count - 1);
if(newrd)
rr->_rdata_fields = newrd;
} else {
LDNS_FREE(rr->_rdata_fields);
}
ldns_rr_set_rd_count(rr, rd_count - 1);
return pop;
}
ldns_rdf *
ldns_rr_rdf(const ldns_rr *rr, size_t nr)
{
if (rr && nr < ldns_rr_rd_count(rr)) {
return rr->_rdata_fields[nr];
} else {
return NULL;
}
}
ldns_rdf *
ldns_rr_owner(const ldns_rr *rr)
{
return rr->_owner;
}
bool
ldns_rr_is_question(const ldns_rr *rr)
{
return rr->_rr_question;
}
uint32_t
ldns_rr_ttl(const ldns_rr *rr)
{
return rr->_ttl;
}
size_t
ldns_rr_rd_count(const ldns_rr *rr)
{
return rr->_rd_count;
}
ldns_rr_type
ldns_rr_get_type(const ldns_rr *rr)
{
return rr->_rr_type;
}
ldns_rr_class
ldns_rr_get_class(const ldns_rr *rr)
{
return rr->_rr_class;
}
/* rr_lists */
size_t
ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
{
if (rr_list) {
return rr_list->_rr_count;
} else {
return 0;
}
}
ldns_rr *
ldns_rr_list_set_rr(ldns_rr_list *rr_list, const ldns_rr *r, size_t count)
{
ldns_rr *old;
if (count > ldns_rr_list_rr_count(rr_list)) {
return NULL;
}
old = ldns_rr_list_rr(rr_list, count);
/* overwrite old's pointer */
rr_list->_rrs[count] = (ldns_rr*)r;
return old;
}
void
ldns_rr_list_set_rr_count(ldns_rr_list *rr_list, size_t count)
{
assert(count <= rr_list->_rr_capacity);
rr_list->_rr_count = count;
}
ldns_rr *
ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
{
if (nr < ldns_rr_list_rr_count(rr_list)) {
return rr_list->_rrs[nr];
} else {
return NULL;
}
}
ldns_rr_list *
ldns_rr_list_new(void)
{
ldns_rr_list *rr_list = LDNS_MALLOC(ldns_rr_list);
if(!rr_list) return NULL;
rr_list->_rr_count = 0;
rr_list->_rr_capacity = 0;
rr_list->_rrs = NULL;
return rr_list;
}