packet.c
Go to the documentation of this file.
1 /*
2  * packet.c
3  *
4  * dns packet implementation
5  *
6  * a Net::DNS like library for C
7  *
8  * (c) NLnet Labs, 2004-2006
9  *
10  * See the file LICENSE for the license
11  */
12 
13 #include <ldns/config.h>
14 
15 #include <ldns/ldns.h>
16 
17 #include <strings.h>
18 #include <limits.h>
19 
20 #ifdef HAVE_SSL
21 #include <openssl/rand.h>
22 #endif
23 
24 /* Access functions
25  * do this as functions to get type checking
26  */
27 
28 #define LDNS_EDNS_MASK_DO_BIT 0x8000
29 #define LDNS_EDNS_MASK_UNASSIGNED (0xFFFF & ~LDNS_EDNS_MASK_DO_BIT)
30 
31 /* TODO defines for 3600 */
32 /* convert to and from numerical flag values */
34  { 3600, "do"},
35  { 0, NULL}
36 };
37 
38 /* read */
39 uint16_t
40 ldns_pkt_id(const ldns_pkt *packet)
41 {
42  return packet->_header->_id;
43 }
44 
45 bool
46 ldns_pkt_qr(const ldns_pkt *packet)
47 {
48  return packet->_header->_qr;
49 }
50 
51 bool
52 ldns_pkt_aa(const ldns_pkt *packet)
53 {
54  return packet->_header->_aa;
55 }
56 
57 bool
58 ldns_pkt_tc(const ldns_pkt *packet)
59 {
60  return packet->_header->_tc;
61 }
62 
63 bool
64 ldns_pkt_rd(const ldns_pkt *packet)
65 {
66  return packet->_header->_rd;
67 }
68 
69 bool
70 ldns_pkt_cd(const ldns_pkt *packet)
71 {
72  return packet->_header->_cd;
73 }
74 
75 bool
76 ldns_pkt_ra(const ldns_pkt *packet)
77 {
78  return packet->_header->_ra;
79 }
80 
81 bool
82 ldns_pkt_ad(const ldns_pkt *packet)
83 {
84  return packet->_header->_ad;
85 }
86 
89 {
90  return packet->_header->_opcode;
91 }
92 
95 {
96  return packet->_header->_rcode;
97 }
98 
99 uint16_t
101 {
102  return packet->_header->_qdcount;
103 }
104 
105 uint16_t
107 {
108  return packet->_header->_ancount;
109 }
110 
111 uint16_t
113 {
114  return packet->_header->_nscount;
115 }
116 
117 uint16_t
119 {
120  return packet->_header->_arcount;
121 }
122 
123 ldns_rr_list *
125 {
126  return packet->_question;
127 }
128 
129 ldns_rr_list *
130 ldns_pkt_answer(const ldns_pkt *packet)
131 {
132  return packet->_answer;
133 }
134 
135 ldns_rr_list *
137 {
138  return packet->_authority;
139 }
140 
141 ldns_rr_list *
143 {
144  return packet->_additional;
145 }
146 
147 /* return ALL section concatenated */
148 ldns_rr_list *
149 ldns_pkt_all(const ldns_pkt *packet)
150 {
151  ldns_rr_list *all, *prev_all;
152 
154  ldns_pkt_question(packet),
155  ldns_pkt_answer(packet));
156  prev_all = all;
157  all = ldns_rr_list_cat_clone(all,
158  ldns_pkt_authority(packet));
159  ldns_rr_list_deep_free(prev_all);
160  prev_all = all;
161  all = ldns_rr_list_cat_clone(all,
162  ldns_pkt_additional(packet));
163  ldns_rr_list_deep_free(prev_all);
164  return all;
165 }
166 
167 ldns_rr_list *
169 {
170  ldns_rr_list *all, *all2;
171 
173  ldns_pkt_answer(packet),
174  ldns_pkt_authority(packet));
175  all2 = ldns_rr_list_cat_clone(all,
176  ldns_pkt_additional(packet));
177 
179  return all2;
180 }
181 
182 size_t
183 ldns_pkt_size(const ldns_pkt *packet)
184 {
185  return packet->_size;
186 }
187 
188 uint32_t
190 {
191  return packet->_querytime;
192 }
193 
194 ldns_rdf *
196 {
197  return packet->_answerfrom;
198 }
199 
200 struct timeval
201 ldns_pkt_timestamp(const ldns_pkt *packet)
202 {
203  return packet->timestamp;
204 }
205 
206 uint16_t
208 {
209  return packet->_edns_udp_size;
210 }
211 
212 uint8_t
214 {
215  return packet->_edns_extended_rcode;
216 }
217 
218 uint8_t
220 {
221  return packet->_edns_version;
222 }
223 
224 uint16_t
225 ldns_pkt_edns_z(const ldns_pkt *packet)
226 {
227  return packet->_edns_z;
228 }
229 
230 bool
232 {
233  return (packet->_edns_z & LDNS_EDNS_MASK_DO_BIT);
234 }
235 
236 void
237 ldns_pkt_set_edns_do(ldns_pkt *packet, bool value)
238 {
239  if (value) {
240  packet->_edns_z = packet->_edns_z | LDNS_EDNS_MASK_DO_BIT;
241  } else {
242  packet->_edns_z = packet->_edns_z & ~LDNS_EDNS_MASK_DO_BIT;
243  }
244 }
245 
246 uint16_t
248 {
249  return (packet->_edns_z & LDNS_EDNS_MASK_UNASSIGNED);
250 }
251 
252 void
253 ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value)
254 {
255  packet->_edns_z = (packet->_edns_z & ~LDNS_EDNS_MASK_UNASSIGNED)
256  | (value & LDNS_EDNS_MASK_UNASSIGNED);
257 }
258 
259 ldns_rdf *
261 {
262  return packet->_edns_data;
263 }
264 
265 /* return only those rr that share the ownername */
266 ldns_rr_list *
268  const ldns_rdf *ownername,
269  ldns_pkt_section sec)
270 {
271  ldns_rr_list *rrs;
272  ldns_rr_list *ret;
273  uint16_t i;
274 
275  if (!packet) {
276  return NULL;
277  }
278 
279  rrs = ldns_pkt_get_section_clone(packet, sec);
280  ret = NULL;
281 
282  for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
284  ldns_rr_list_rr(rrs, i)),
285  ownername) == 0) {
286  /* owner names match */
287  if (ret == NULL) {
288  ret = ldns_rr_list_new();
289  }
292  ldns_rr_list_rr(rrs, i))
293  );
294  }
295  }
296 
298 
299  return ret;
300 }
301 
302 /* return only those rr that share a type */
303 ldns_rr_list *
305  ldns_rr_type type,
306  ldns_pkt_section sec)
307 {
308  ldns_rr_list *rrs;
309  ldns_rr_list *new;
310  uint16_t i;
311 
312  if(!packet) {
313  return NULL;
314  }
315 
316  rrs = ldns_pkt_get_section_clone(packet, sec);
317  new = ldns_rr_list_new();
318 
319  for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
320  if (type == ldns_rr_get_type(ldns_rr_list_rr(rrs, i))) {
321  /* types match */
324  ldns_rr_list_rr(rrs, i))
325  );
326  }
327  }
329 
330  if (ldns_rr_list_rr_count(new) == 0) {
331  ldns_rr_list_free(new);
332  return NULL;
333  } else {
334  return new;
335  }
336 }
337 
338 /* return only those rrs that share name and type */
339 ldns_rr_list *
341  const ldns_rdf *ownername,
342  ldns_rr_type type,
343  ldns_pkt_section sec)
344 {
345  ldns_rr_list *rrs;
346  ldns_rr_list *new;
347  ldns_rr_list *ret;
348  uint16_t i;
349 
350  if(!packet) {
351  return NULL;
352  }
353 
354  rrs = ldns_pkt_get_section_clone(packet, sec);
355  new = ldns_rr_list_new();
356  ret = NULL;
357 
358  for(i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
359  if (type == ldns_rr_get_type(ldns_rr_list_rr(rrs, i)) &&
361  ownername
362  ) == 0
363  ) {
364  /* types match */
366  ret = new;
367  }
368  }
370  if (!ret) {
371  ldns_rr_list_free(new);
372  }
373  return ret;
374 }
375 
376 bool
377 ldns_pkt_rr(const ldns_pkt *pkt, ldns_pkt_section sec, const ldns_rr *rr)
378 {
379  bool result = false;
380 
381  switch (sec) {
384  case LDNS_SECTION_ANSWER:
385  return ldns_rr_list_contains_rr(ldns_pkt_answer(pkt), rr);
390  case LDNS_SECTION_ANY:
391  result = ldns_rr_list_contains_rr(ldns_pkt_question(pkt), rr);
392  /* fallthrough */
394  result = result
398  }
399 
400  return result;
401 }
402 
403 uint16_t
405 {
406  switch(s) {
408  return ldns_pkt_qdcount(packet);
409  case LDNS_SECTION_ANSWER:
410  return ldns_pkt_ancount(packet);
412  return ldns_pkt_nscount(packet);
414  return ldns_pkt_arcount(packet);
415  case LDNS_SECTION_ANY:
416  return ldns_pkt_qdcount(packet) +
417  ldns_pkt_ancount(packet) +
418  ldns_pkt_nscount(packet) +
419  ldns_pkt_arcount(packet);
421  return ldns_pkt_ancount(packet) +
422  ldns_pkt_nscount(packet) +
423  ldns_pkt_arcount(packet);
424  default:
425  return 0;
426  }
427 }
428 
429 bool
431 {
432  if (!p) {
433  return true; /* NULL is empty? */
434  }
436  return false;
437  } else {
438  return true;
439  }
440 }
441 
442 
443 ldns_rr_list *
445 {
446  switch(s) {
448  return ldns_rr_list_clone(ldns_pkt_question(packet));
449  case LDNS_SECTION_ANSWER:
450  return ldns_rr_list_clone(ldns_pkt_answer(packet));
452  return ldns_rr_list_clone(ldns_pkt_authority(packet));
454  return ldns_rr_list_clone(ldns_pkt_additional(packet));
455  case LDNS_SECTION_ANY:
456  /* these are already clones */
457  return ldns_pkt_all(packet);
459  return ldns_pkt_all_noquestion(packet);
460  default:
461  return NULL;
462  }
463 }
464 
466  return pkt->_tsig_rr;
467 }
468 
469 /* write */
470 void
471 ldns_pkt_set_id(ldns_pkt *packet, uint16_t id)
472 {
473  packet->_header->_id = id;
474 }
475 
476 void
478 {
479  uint16_t rid = ldns_get_random();
480  ldns_pkt_set_id(packet, rid);
481 }
482 
483 
484 void
485 ldns_pkt_set_qr(ldns_pkt *packet, bool qr)
486 {
487  packet->_header->_qr = qr;
488 }
489 
490 void
491 ldns_pkt_set_aa(ldns_pkt *packet, bool aa)
492 {
493  packet->_header->_aa = aa;
494 }
495 
496 void
497 ldns_pkt_set_tc(ldns_pkt *packet, bool tc)
498 {
499  packet->_header->_tc = tc;
500 }
501 
502 void
503 ldns_pkt_set_rd(ldns_pkt *packet, bool rd)
504 {
505  packet->_header->_rd = rd;
506 }
507 
508 void
510 {
511  p->_additional = rr;
512 }
513 
514 void
516 {
517  p->_question = rr;
518 }
519 
520 void
522 {
523  p->_answer = rr;
524 }
525 
526 void
528 {
529  p->_authority = rr;
530 }
531 
532 void
533 ldns_pkt_set_cd(ldns_pkt *packet, bool cd)
534 {
535  packet->_header->_cd = cd;
536 }
537 
538 void
539 ldns_pkt_set_ra(ldns_pkt *packet, bool ra)
540 {
541  packet->_header->_ra = ra;
542 }
543 
544 void
545 ldns_pkt_set_ad(ldns_pkt *packet, bool ad)
546 {
547  packet->_header->_ad = ad;
548 }
549 
550 void
552 {
553  packet->_header->_opcode = opcode;
554 }
555 
556 void
557 ldns_pkt_set_rcode(ldns_pkt *packet, uint8_t rcode)
558 {
559  packet->_header->_rcode = rcode;
560 }
561 
562 void
563 ldns_pkt_set_qdcount(ldns_pkt *packet, uint16_t qdcount)
564 {
565  packet->_header->_qdcount = qdcount;
566 }
567 
568 void
569 ldns_pkt_set_ancount(ldns_pkt *packet, uint16_t ancount)
570 {
571  packet->_header->_ancount = ancount;
572 }
573 
574 void
575 ldns_pkt_set_nscount(ldns_pkt *packet, uint16_t nscount)
576 {
577  packet->_header->_nscount = nscount;
578 }
579 
580 void
581 ldns_pkt_set_arcount(ldns_pkt *packet, uint16_t arcount)
582 {
583  packet->_header->_arcount = arcount;
584 }
585 
586 void
587 ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
588 {
589  packet->_querytime = time;
590 }
591 
592 void
594 {
595  packet->_answerfrom = answerfrom;
596 }
597 
598 void
599 ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
600 {
601  packet->timestamp.tv_sec = timeval.tv_sec;
602  packet->timestamp.tv_usec = timeval.tv_usec;
603 }
604 
605 void
606 ldns_pkt_set_size(ldns_pkt *packet, size_t s)
607 {
608  packet->_size = s;
609 }
610 
611 void
613 {
614  packet->_edns_udp_size = s;
615 }
616 
617 void
619 {
620  packet->_edns_extended_rcode = c;
621 }
622 
623 void
625 {
626  packet->_edns_version = v;
627 }
628 
629 void
630 ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z)
631 {
632  packet->_edns_z = z;
633 }
634 
635 void
637 {
638  packet->_edns_data = data;
639 }
640 
641 void
643 {
644  if (packet->_edns_list)
646  packet->_edns_list = list;
647 }
648 
649 
650 void
652 {
653  switch(s) {
655  ldns_pkt_set_qdcount(packet, count);
656  break;
657  case LDNS_SECTION_ANSWER:
658  ldns_pkt_set_ancount(packet, count);
659  break;
661  ldns_pkt_set_nscount(packet, count);
662  break;
664  ldns_pkt_set_arcount(packet, count);
665  break;
666  case LDNS_SECTION_ANY:
668  break;
669  }
670 }
671 
673 {
674  pkt->_tsig_rr = rr;
675 }
676 
677 bool
679 {
680  switch(section) {
682  if (!ldns_rr_list_push_rr(ldns_pkt_question(packet), rr)) {
683  return false;
684  }
685  ldns_pkt_set_qdcount(packet, ldns_pkt_qdcount(packet) + 1);
686  break;
687  case LDNS_SECTION_ANSWER:
688  if (!ldns_rr_list_push_rr(ldns_pkt_answer(packet), rr)) {
689  return false;
690  }
691  ldns_pkt_set_ancount(packet, ldns_pkt_ancount(packet) + 1);
692  break;
694  if (!ldns_rr_list_push_rr(ldns_pkt_authority(packet), rr)) {
695  return false;
696  }
697  ldns_pkt_set_nscount(packet, ldns_pkt_nscount(packet) + 1);
698  break;
700  if (!ldns_rr_list_push_rr(ldns_pkt_additional(packet), rr)) {
701  return false;
702  }
703  ldns_pkt_set_arcount(packet, ldns_pkt_arcount(packet) + 1);
704  break;
705  case LDNS_SECTION_ANY:
707  /* shouldn't this error? */
708  break;
709  }
710  return true;
711 }
712 
713 bool
715 {
716 
717  /* check to see if its there */
718  if (ldns_pkt_rr(pkt, sec, rr)) {
719  /* already there */
720  return false;
721  }
722  return ldns_pkt_push_rr(pkt, sec, rr);
723 }
724 
725 bool
727 {
728  size_t i;
729  for(i = 0; i < ldns_rr_list_rr_count(list); i++) {
730  if (!ldns_pkt_push_rr(p, s, ldns_rr_list_rr(list, i))) {
731  return false;
732  }
733  }
734  return true;
735 }
736 
737 bool
739 {
740  size_t i;
741  for(i = 0; i < ldns_rr_list_rr_count(list); i++) {
742  if (!ldns_pkt_safe_push_rr(p, s, ldns_rr_list_rr(list, i))) {
743  return false;
744  }
745  }
746  return true;
747 }
748 
749 bool
751 {
752  return (ldns_pkt_edns_udp_size(pkt) > 0 ||
753  ldns_pkt_edns_extended_rcode(pkt) > 0 ||
754  ldns_pkt_edns_data(pkt) ||
755  ldns_pkt_edns_do(pkt) ||
756  pkt->_edns_list ||
757  pkt->_edns_present
758  );
759 }
760 
762 pkt_edns_data2edns_option_list(const ldns_rdf *edns_data);
765 {
766  size_t pos = 0;
767  ldns_edns_option_list* edns_list;
768  size_t max;
769  const uint8_t* wire;
770 
771  if (!edns_data)
772  return NULL;
773 
774  max = ldns_rdf_size(edns_data);
775  wire = ldns_rdf_data(edns_data);
776  if (!max)
777  return NULL;
778 
779  if (!(edns_list = ldns_edns_option_list_new()))
780  return NULL;
781 
782  while (pos < max) {
783  ldns_edns_option* edns;
784  uint8_t *data;
785 
786  if (pos + 4 > max) { /* make sure the header fits */
788  return NULL;
789  }
790  ldns_edns_option_code code = ldns_read_uint16(&wire[pos]);
791  size_t size = ldns_read_uint16(&wire[pos+2]);
792  pos += 4;
793 
794  if (pos + size > max) { /* make sure the size fits the data */
796  return NULL;
797  }
798  data = LDNS_XMALLOC(uint8_t, size);
799 
800  if (!data) {
802  return NULL;
803  }
804  memcpy(data, &wire[pos], size);
805  pos += size;
806 
807  edns = ldns_edns_new(code, size, data);
808 
809  if (!edns) {
811  return NULL;
812  }
813  if (!ldns_edns_option_list_push(edns_list, edns)) {
815  return NULL;
816  }
817  }
818  return edns_list;
819 
820 }
821 
824 {
825  /* return the list if it already exists */
826  if (packet->_edns_list != NULL)
827  return packet->_edns_list;
828 
829  /* if the list doesn't exists, we create it by parsing the
830  * packet->_edns_data
831  */
832  if (!ldns_pkt_edns_data(packet))
833  return NULL;
834 
835  return ( packet->_edns_list
837 }
838 
839 
840 /* Create/destroy/convert functions
841  */
842 ldns_pkt *
844 {
845  ldns_pkt *packet;
846  packet = LDNS_MALLOC(ldns_pkt);
847  if (!packet) {
848  return NULL;
849  }
850 
851  packet->_header = LDNS_MALLOC(ldns_hdr);
852  if (!packet->_header) {
853  LDNS_FREE(packet);
854  return NULL;
855  }
856 
857  packet->_question = ldns_rr_list_new();
858  packet->_answer = ldns_rr_list_new();
859  packet->_authority = ldns_rr_list_new();
860  packet->_additional = ldns_rr_list_new();
861 
862  /* default everything to false */
863  ldns_pkt_set_qr(packet, false);
864  ldns_pkt_set_aa(packet, false);
865  ldns_pkt_set_tc(packet, false);
866  ldns_pkt_set_rd(packet, false);
867  ldns_pkt_set_ra(packet, false);
868  ldns_pkt_set_ad(packet, false);
869  ldns_pkt_set_cd(packet, false);
870 
872  ldns_pkt_set_rcode(packet, 0);
873  ldns_pkt_set_id(packet, 0);
874  ldns_pkt_set_size(packet, 0);
875  ldns_pkt_set_querytime(packet, 0);
876  memset(&packet->timestamp, 0, sizeof(packet->timestamp));
877  ldns_pkt_set_answerfrom(packet, NULL);
882 
883  ldns_pkt_set_edns_udp_size(packet, 0);
885  ldns_pkt_set_edns_version(packet, 0);
886  ldns_pkt_set_edns_z(packet, 0);
887  ldns_pkt_set_edns_data(packet, NULL);
888  packet->_edns_list = NULL;
889  packet->_edns_present = false;
890 
891  ldns_pkt_set_tsig(packet, NULL);
892 
893  return packet;
894 }
895 
896 void
898 {
899  if (packet) {
900  LDNS_FREE(packet->_header);
905  ldns_rr_free(packet->_tsig_rr);
909  LDNS_FREE(packet);
910  }
911 }
912 
913 bool
914 ldns_pkt_set_flags(ldns_pkt *packet, uint16_t flags)
915 {
916  if (!packet) {
917  return false;
918  }
919  if ((flags & LDNS_QR) == LDNS_QR) {
920  ldns_pkt_set_qr(packet, true);
921  }
922  if ((flags & LDNS_AA) == LDNS_AA) {
923  ldns_pkt_set_aa(packet, true);
924  }
925  if ((flags & LDNS_RD) == LDNS_RD) {
926  ldns_pkt_set_rd(packet, true);
927  }
928  if ((flags & LDNS_TC) == LDNS_TC) {
929  ldns_pkt_set_tc(packet, true);
930  }
931  if ((flags & LDNS_CD) == LDNS_CD) {
932  ldns_pkt_set_cd(packet, true);
933  }
934  if ((flags & LDNS_RA) == LDNS_RA) {
935  ldns_pkt_set_ra(packet, true);
936  }
937  if ((flags & LDNS_AD) == LDNS_AD) {
938  ldns_pkt_set_ad(packet, true);
939  }
940  return true;
941 }
942 
943 
944 static ldns_rr*
945 ldns_pkt_authsoa(const ldns_rdf* rr_name, ldns_rr_class rr_class)
946 {
947  ldns_rr* soa_rr = ldns_rr_new();
948  ldns_rdf *owner_rdf;
949  ldns_rdf *mname_rdf;
950  ldns_rdf *rname_rdf;
951  ldns_rdf *serial_rdf;
952  ldns_rdf *refresh_rdf;
953  ldns_rdf *retry_rdf;
954  ldns_rdf *expire_rdf;
955  ldns_rdf *minimum_rdf;
956 
957  if (!soa_rr) {
958  return NULL;
959  }
960  owner_rdf = ldns_rdf_clone(rr_name);
961  if (!owner_rdf) {
962  ldns_rr_free(soa_rr);
963  return NULL;
964  }
965 
966  ldns_rr_set_owner(soa_rr, owner_rdf);
968  ldns_rr_set_class(soa_rr, rr_class);
969  ldns_rr_set_question(soa_rr, false);
970 
971  if (ldns_str2rdf_dname(&mname_rdf, ".") != LDNS_STATUS_OK) {
972  ldns_rr_free(soa_rr);
973  return NULL;
974  } else {
975  ldns_rr_push_rdf(soa_rr, mname_rdf);
976  }
977  if (ldns_str2rdf_dname(&rname_rdf, ".") != LDNS_STATUS_OK) {
978  ldns_rr_free(soa_rr);
979  return NULL;
980  } else {
981  ldns_rr_push_rdf(soa_rr, rname_rdf);
982  }
984  if (!serial_rdf) {
985  ldns_rr_free(soa_rr);
986  return NULL;
987  } else {
988  ldns_rr_push_rdf(soa_rr, serial_rdf);
989  }
990  refresh_rdf = ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32, 0);
991  if (!refresh_rdf) {
992  ldns_rr_free(soa_rr);
993  return NULL;
994  } else {
995  ldns_rr_push_rdf(soa_rr, refresh_rdf);
996  }
998  if (!retry_rdf) {
999  ldns_rr_free(soa_rr);
1000  return NULL;
1001  } else {
1002  ldns_rr_push_rdf(soa_rr, retry_rdf);
1003  }
1004  expire_rdf = ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32, 0);
1005  if (!expire_rdf) {
1006  ldns_rr_free(soa_rr);
1007  return NULL;
1008  } else {
1009  ldns_rr_push_rdf(soa_rr, expire_rdf);
1010  }
1011  minimum_rdf = ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32, 0);
1012  if (!minimum_rdf) {
1013  ldns_rr_free(soa_rr);
1014  return NULL;
1015  } else {
1016  ldns_rr_push_rdf(soa_rr, minimum_rdf);
1017  }
1018  return soa_rr;
1019 }
1020 
1021 
1022 static ldns_status
1023 ldns_pkt_query_new_frm_str_internal(ldns_pkt **p, const char *name,
1024  ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags,
1025  ldns_rr* authsoa_rr)
1026 {
1027  ldns_pkt *packet;
1028  ldns_rr *question_rr;
1029  ldns_rdf *name_rdf;
1030 
1031  packet = ldns_pkt_new();
1032  if (!packet) {
1033  return LDNS_STATUS_MEM_ERR;
1034  }
1035 
1036  if (!ldns_pkt_set_flags(packet, flags)) {
1037  ldns_pkt_free(packet);
1038  return LDNS_STATUS_ERR;
1039  }
1040 
1041  question_rr = ldns_rr_new();
1042  if (!question_rr) {
1043  ldns_pkt_free(packet);
1044  return LDNS_STATUS_MEM_ERR;
1045  }
1046 
1047  if (rr_type == 0) {
1048  rr_type = LDNS_RR_TYPE_A;
1049  }
1050  if (rr_class == 0) {
1051  rr_class = LDNS_RR_CLASS_IN;
1052  }
1053 
1054  if (ldns_str2rdf_dname(&name_rdf, name) == LDNS_STATUS_OK) {
1055  ldns_rr_set_owner(question_rr, name_rdf);
1056  ldns_rr_set_type(question_rr, rr_type);
1057  ldns_rr_set_class(question_rr, rr_class);
1058  ldns_rr_set_question(question_rr, true);
1059 
1060  ldns_pkt_push_rr(packet, LDNS_SECTION_QUESTION, question_rr);
1061  } else {
1062  ldns_rr_free(question_rr);
1063  ldns_pkt_free(packet);
1064  return LDNS_STATUS_ERR;
1065  }
1066 
1067  if (authsoa_rr) {
1068  ldns_pkt_push_rr(packet, LDNS_SECTION_AUTHORITY, authsoa_rr);
1069  }
1070 
1071  packet->_tsig_rr = NULL;
1072  ldns_pkt_set_answerfrom(packet, NULL);
1073  if (p) {
1074  *p = packet;
1075  return LDNS_STATUS_OK;
1076  } else {
1077  ldns_pkt_free(packet);
1078  return LDNS_STATUS_NULL;
1079  }
1080 }
1081 
1083 ldns_pkt_query_new_frm_str(ldns_pkt **p, const char *name,
1084  ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
1085 {
1086  return ldns_pkt_query_new_frm_str_internal(p, name, rr_type,
1087  rr_class, flags, NULL);
1088 }
1089 
1092  ldns_rr_class rr_class, uint16_t flags, ldns_rr *soa)
1093 {
1094  ldns_rr* authsoa_rr = soa;
1095  if (!authsoa_rr) {
1096  ldns_rdf *name_rdf;
1097  if (ldns_str2rdf_dname(&name_rdf, name) == LDNS_STATUS_OK) {
1098  authsoa_rr = ldns_pkt_authsoa(name_rdf, rr_class);
1099  }
1100  ldns_rdf_free(name_rdf);
1101  }
1102  return ldns_pkt_query_new_frm_str_internal(p, name, LDNS_RR_TYPE_IXFR,
1103  rr_class, flags, authsoa_rr);
1104 }
1105 
1106 static ldns_pkt *
1107 ldns_pkt_query_new_internal(ldns_rdf *rr_name, ldns_rr_type rr_type,
1108  ldns_rr_class rr_class, uint16_t flags, ldns_rr* authsoa_rr)
1109 {
1110  ldns_pkt *packet;
1111  ldns_rr *question_rr;
1112 
1113  packet = ldns_pkt_new();
1114  if (!packet) {
1115  return NULL;
1116  }
1117 
1118  if (!ldns_pkt_set_flags(packet, flags)) {
1119  return NULL;
1120  }
1121 
1122  question_rr = ldns_rr_new();
1123  if (!question_rr) {
1124  ldns_pkt_free(packet);
1125  return NULL;
1126  }
1127 
1128  if (rr_type == 0) {
1129  rr_type = LDNS_RR_TYPE_A;
1130  }
1131  if (rr_class == 0) {
1132  rr_class = LDNS_RR_CLASS_IN;
1133  }
1134 
1135  ldns_rr_set_owner(question_rr, rr_name);
1136  ldns_rr_set_type(question_rr, rr_type);
1137  ldns_rr_set_class(question_rr, rr_class);
1138  ldns_rr_set_question(question_rr, true);
1139  ldns_pkt_push_rr(packet, LDNS_SECTION_QUESTION, question_rr);
1140 
1141  if (authsoa_rr) {
1142  ldns_pkt_push_rr(packet, LDNS_SECTION_AUTHORITY, authsoa_rr);
1143  }
1144 
1145  packet->_tsig_rr = NULL;
1146  return packet;
1147 }
1148 
1149 ldns_pkt *
1151  ldns_rr_class rr_class, uint16_t flags)
1152 {
1153  return ldns_pkt_query_new_internal(rr_name, rr_type,
1154  rr_class, flags, NULL);
1155 }
1156 
1157 ldns_pkt *
1159  uint16_t flags, ldns_rr* soa)
1160 {
1161  ldns_rr* authsoa_rr = soa;
1162  if (!authsoa_rr) {
1163  authsoa_rr = ldns_pkt_authsoa(rr_name, rr_class);
1164  }
1165  return ldns_pkt_query_new_internal(rr_name, LDNS_RR_TYPE_IXFR,
1166  rr_class, flags, authsoa_rr);
1167 }
1168 
1171 {
1172  ldns_rr_list *tmp;
1173 
1174  if (!p) {
1175  return LDNS_PACKET_UNKNOWN;
1176  }
1177 
1179  return LDNS_PACKET_NXDOMAIN;
1180  }
1181 
1182  if (ldns_pkt_ancount(p) == 0 && ldns_pkt_arcount(p) == 0
1183  && ldns_pkt_nscount(p) == 1) {
1184 
1185  /* check for SOA */
1188  if (tmp) {
1190  return LDNS_PACKET_NODATA;
1191  } else {
1192  /* I have no idea ... */
1193  }
1194  }
1195 
1196  if (ldns_pkt_ancount(p) == 0 && ldns_pkt_nscount(p) > 0) {
1199  if (tmp) {
1200  /* there are nameservers here */
1202  return LDNS_PACKET_REFERRAL;
1203  } else {
1204  /* I have no idea */
1205  }
1207  }
1208 
1209  /* if we cannot determine the packet type, we say it's an
1210  * answer...
1211  */
1212  return LDNS_PACKET_ANSWER;
1213 }
1214 
1215 ldns_pkt *
1217 {
1218  ldns_pkt *new_pkt;
1219 
1220  if (!pkt) {
1221  return NULL;
1222  }
1223  new_pkt = ldns_pkt_new();
1224 
1225  ldns_pkt_set_id(new_pkt, ldns_pkt_id(pkt));
1226  ldns_pkt_set_qr(new_pkt, ldns_pkt_qr(pkt));
1227  ldns_pkt_set_aa(new_pkt, ldns_pkt_aa(pkt));
1228  ldns_pkt_set_tc(new_pkt, ldns_pkt_tc(pkt));
1229  ldns_pkt_set_rd(new_pkt, ldns_pkt_rd(pkt));
1230  ldns_pkt_set_cd(new_pkt, ldns_pkt_cd(pkt));
1231  ldns_pkt_set_ra(new_pkt, ldns_pkt_ra(pkt));
1232  ldns_pkt_set_ad(new_pkt, ldns_pkt_ad(pkt));
1234  ldns_pkt_set_rcode(new_pkt, ldns_pkt_get_rcode(pkt));
1235  ldns_pkt_set_qdcount(new_pkt, ldns_pkt_qdcount(pkt));
1236  ldns_pkt_set_ancount(new_pkt, ldns_pkt_ancount(pkt));
1237  ldns_pkt_set_nscount(new_pkt, ldns_pkt_nscount(pkt));
1238  ldns_pkt_set_arcount(new_pkt, ldns_pkt_arcount(pkt));
1239  if (ldns_pkt_answerfrom(pkt))
1240  ldns_pkt_set_answerfrom(new_pkt,
1244  ldns_pkt_set_size(new_pkt, ldns_pkt_size(pkt));
1246 
1251  new_pkt->_edns_present = pkt->_edns_present;
1252  ldns_pkt_set_edns_z(new_pkt, ldns_pkt_edns_z(pkt));
1253  if(ldns_pkt_edns_data(pkt))
1254  ldns_pkt_set_edns_data(new_pkt,
1256  ldns_pkt_set_edns_do(new_pkt, ldns_pkt_edns_do(pkt));
1257  if (pkt->_edns_list)
1260 
1262  ldns_rr_list_deep_free(new_pkt->_answer);
1266  new_pkt->_answer = ldns_rr_list_clone(ldns_pkt_answer(pkt));
1269  return new_pkt;
1270 }
int ldns_dname_compare(const ldns_rdf *dname1, const ldns_rdf *dname2)
Compares the two dname rdf's according to the algorithm for ordering in RFC4034 Section 6.
Definition: dname.c:359
ldns_edns_option_list * ldns_edns_option_list_new(void)
allocates space for a new list of EDNS options
Definition: edns.c:209
enum ldns_enum_edns_option ldns_edns_option_code
Definition: edns.h:46
signed char ldns_edns_option_list_push(ldns_edns_option_list *options_list, ldns_edns_option *option)
adds an EDNS option at the end of the list of options.
Definition: edns.c:338
void ldns_edns_option_list_deep_free(ldns_edns_option_list *options_list)
Definition: edns.c:264
ldns_edns_option_list * ldns_edns_option_list_clone(ldns_edns_option_list *options_list)
clone the EDNS options list and it's contents
Definition: edns.c:224
ldns_edns_option * ldns_edns_new(ldns_edns_option_code code, size_t size, void *data)
allocates a new EDNS structure and fills it.
Definition: edns.c:139
@ LDNS_STATUS_NULL
Definition: error.h:51
@ LDNS_STATUS_ERR
Definition: error.h:37
@ LDNS_STATUS_MEM_ERR
Definition: error.h:34
@ LDNS_STATUS_OK
Definition: error.h:26
enum ldns_enum_status ldns_status
Definition: error.h:148
Including this file will include all ldns files, and define some lookup tables.
ldns_pkt * ldns_pkt_clone(const ldns_pkt *pkt)
clones the given packet, creating a fully allocated copy
Definition: packet.c:1216
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition: packet.c:897
uint8_t ldns_pkt_edns_version(const ldns_pkt *packet)
return the packet's edns version
Definition: packet.c:219
void ldns_pkt_set_ancount(ldns_pkt *packet, uint16_t ancount)
Set the packet's an count.
Definition: packet.c:569
ldns_rdf * ldns_pkt_answerfrom(const ldns_pkt *packet)
Return the packet's answerfrom.
Definition: packet.c:195
void ldns_pkt_set_rd(ldns_pkt *packet, signed char rd)
Set the packet's rd bit.
Definition: packet.c:503
void ldns_pkt_set_querytime(ldns_pkt *packet, uint32_t time)
Set the packet's query time.
Definition: packet.c:587
void ldns_pkt_set_tsig(ldns_pkt *pkt, ldns_rr *rr)
Set the packet's tsig rr.
Definition: packet.c:672
signed char ldns_pkt_push_rr_list(ldns_pkt *p, ldns_pkt_section s, ldns_rr_list *list)
push a rr_list on a packet
Definition: packet.c:726
void ldns_pkt_set_tc(ldns_pkt *packet, signed char tc)
Set the packet's tc bit.
Definition: packet.c:497
ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *packet)
Read the packet's code.
Definition: packet.c:88
signed char ldns_pkt_edns(const ldns_pkt *pkt)
returns true if this packet needs and EDNS rr to be sent.
Definition: packet.c:750
ldns_pkt * ldns_pkt_ixfr_request_new(ldns_rdf *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr *soa)
creates an IXFR request packet for the given name, type and class.
Definition: packet.c:1158
signed char ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr)
push an rr on a packet, provided the RR is not there.
Definition: packet.c:714
signed char ldns_pkt_rr(const ldns_pkt *pkt, ldns_pkt_section sec, const ldns_rr *rr)
check to see if an rr exist in the packet
Definition: packet.c:377
void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s)
Set the packet's edns udp size.
Definition: packet.c:612
void ldns_pkt_set_opcode(ldns_pkt *packet, ldns_pkt_opcode opcode)
Set the packet's opcode.
Definition: packet.c:551
void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr)
directly set the additional section
Definition: packet.c:509
void ldns_pkt_set_aa(ldns_pkt *packet, signed char aa)
Set the packet's aa bit.
Definition: packet.c:491
void ldns_pkt_set_random_id(ldns_pkt *packet)
Set the packet's id to a random value.
Definition: packet.c:477
void ldns_pkt_set_edns_option_list(ldns_pkt *packet, ldns_edns_option_list *list)
Set the packet's structured EDNS data.
Definition: packet.c:642
ldns_rr_list * ldns_pkt_question(const ldns_pkt *packet)
Return the packet's question section.
Definition: packet.c:124
ldns_edns_option_list * pkt_edns_data2edns_option_list(const ldns_rdf *edns_data)
Definition: packet.c:764
uint16_t ldns_pkt_id(const ldns_pkt *packet)
Read the packet id.
Definition: packet.c:40
uint16_t ldns_pkt_arcount(const ldns_pkt *packet)
Return the packet's ar count.
Definition: packet.c:118
#define LDNS_EDNS_MASK_UNASSIGNED
Definition: packet.c:29
struct timeval ldns_pkt_timestamp(const ldns_pkt *packet)
Return the packet's timestamp.
Definition: packet.c:201
void ldns_pkt_set_nscount(ldns_pkt *packet, uint16_t nscount)
Set the packet's ns count.
Definition: packet.c:575
void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr)
directly set the authority section
Definition: packet.c:527
void ldns_pkt_set_ra(ldns_pkt *packet, signed char ra)
Set the packet's ra bit.
Definition: packet.c:539
void ldns_pkt_set_timestamp(ldns_pkt *packet, struct timeval timeval)
Set the packet's timestamp.
Definition: packet.c:599
void ldns_pkt_set_cd(ldns_pkt *packet, signed char cd)
Set the packet's cd bit.
Definition: packet.c:533
ldns_rr_list * ldns_pkt_rr_list_by_name(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_pkt_section sec)
return all the rr with a specific name from a packet.
Definition: packet.c:267
void ldns_pkt_set_size(ldns_pkt *packet, size_t s)
Set the packet's size.
Definition: packet.c:606
signed char ldns_pkt_aa(const ldns_pkt *packet)
Read the packet's aa bit.
Definition: packet.c:52
ldns_pkt * ldns_pkt_new(void)
allocates and initializes a ldns_pkt structure.
Definition: packet.c:843
signed char ldns_pkt_empty(ldns_pkt *p)
check if a packet is empty
Definition: packet.c:430
#define LDNS_EDNS_MASK_DO_BIT
Definition: packet.c:28
signed char ldns_pkt_set_flags(ldns_pkt *packet, uint16_t flags)
sets the flags in a packet.
Definition: packet.c:914
ldns_rr_list * ldns_pkt_authority(const ldns_pkt *packet)
Return the packet's authority section.
Definition: packet.c:136
uint16_t ldns_pkt_edns_unassigned(const ldns_pkt *packet)
return the packet's EDNS header bits that are unassigned.
Definition: packet.c:247
uint16_t ldns_pkt_ancount(const ldns_pkt *packet)
Return the packet's an count.
Definition: packet.c:106
signed char ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr)
push an rr on a packet
Definition: packet.c:678
ldns_rr_list * ldns_pkt_rr_list_by_name_and_type(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_rr_type type, ldns_pkt_section sec)
return all the rr with a specific type and type from a packet.
Definition: packet.c:340
void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr)
directly set the answer section
Definition: packet.c:521
void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr)
directly set the question section
Definition: packet.c:515
void ldns_pkt_set_edns_do(ldns_pkt *packet, signed char value)
Set the packet's edns do bit.
Definition: packet.c:237
void ldns_pkt_set_qdcount(ldns_pkt *packet, uint16_t qdcount)
Set the packet's qd count.
Definition: packet.c:563
ldns_rr_list * ldns_pkt_all_noquestion(const ldns_pkt *packet)
Return the packet's answer, authority and additional sections concatenated, in a new rr_list clone.
Definition: packet.c:168
ldns_rr_list * ldns_pkt_additional(const ldns_pkt *packet)
Return the packet's additional section.
Definition: packet.c:142
signed char ldns_pkt_cd(const ldns_pkt *packet)
Read the packet's cd bit.
Definition: packet.c:70
size_t ldns_pkt_size(const ldns_pkt *packet)
Return the packet's size in bytes.
Definition: packet.c:183
void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z)
Set the packet's edns z value.
Definition: packet.c:630
signed char ldns_pkt_qr(const ldns_pkt *packet)
Read the packet's qr bit.
Definition: packet.c:46
uint32_t ldns_pkt_querytime(const ldns_pkt *packet)
Return the packet's querytime.
Definition: packet.c:189
void ldns_pkt_set_ad(ldns_pkt *packet, signed char ad)
Set the packet's ad bit.
Definition: packet.c:545
uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet)
return the packet's edns extended rcode
Definition: packet.c:213
uint16_t ldns_pkt_section_count(const ldns_pkt *packet, ldns_pkt_section s)
Return the number of RRs in the given section.
Definition: packet.c:404
void ldns_pkt_set_qr(ldns_pkt *packet, signed char qr)
Set the packet's qr bit.
Definition: packet.c:485
void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data)
Set the packet's EDNS data.
Definition: packet.c:636
ldns_pkt_type ldns_pkt_reply_type(const ldns_pkt *p)
looks inside the packet to determine what kind of packet it is, AUTH, NXDOMAIN, REFERRAL,...
Definition: packet.c:1170
ldns_rr_list * ldns_pkt_answer(const ldns_pkt *packet)
Return the packet's answer section.
Definition: packet.c:130
signed char ldns_pkt_tc(const ldns_pkt *packet)
Read the packet's tc bit.
Definition: packet.c:58
signed char ldns_pkt_rd(const ldns_pkt *packet)
Read the packet's rd bit.
Definition: packet.c:64
uint16_t ldns_pkt_nscount(const ldns_pkt *packet)
Return the packet's ns count.
Definition: packet.c:112
signed char ldns_pkt_edns_do(const ldns_pkt *packet)
return the packet's edns do bit
Definition: packet.c:231
signed char ldns_pkt_safe_push_rr_list(ldns_pkt *p, ldns_pkt_section s, ldns_rr_list *list)
push an rr_list to a packet, provided the RRs are not already there.
Definition: packet.c:738
ldns_rr_list * ldns_pkt_all(const ldns_pkt *packet)
Return the packet's question, answer, authority and additional sections concatenated,...
Definition: packet.c:149
signed char ldns_pkt_ra(const ldns_pkt *packet)
Read the packet's ra bit.
Definition: packet.c:76
ldns_rr_list * ldns_pkt_get_section_clone(const ldns_pkt *packet, ldns_pkt_section s)
return all the rr_list's in the packet.
Definition: packet.c:444
void ldns_pkt_set_id(ldns_pkt *packet, uint16_t id)
Set the packet's id.
Definition: packet.c:471
uint16_t ldns_pkt_qdcount(const ldns_pkt *packet)
Return the packet's qd count.
Definition: packet.c:100
signed char ldns_pkt_ad(const ldns_pkt *packet)
Read the packet's ad bit.
Definition: packet.c:82
ldns_lookup_table ldns_edns_flags[]
EDNS flags.
Definition: packet.c:33
void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c)
Set the packet's edns extended rcode.
Definition: packet.c:618
ldns_rdf * ldns_pkt_edns_data(const ldns_pkt *packet)
return the packet's EDNS data
Definition: packet.c:260
ldns_status ldns_pkt_ixfr_request_new_frm_str(ldns_pkt **p, const char *name, ldns_rr_class rr_class, uint16_t flags, ldns_rr *soa)
creates an IXFR request packet for the given name, class.
Definition: packet.c:1091
ldns_rr * ldns_pkt_tsig(const ldns_pkt *pkt)
Return the packet's tsig pseudo rr's.
Definition: packet.c:465
void ldns_pkt_set_answerfrom(ldns_pkt *packet, ldns_rdf *answerfrom)
Set the packet's answering server.
Definition: packet.c:593
void ldns_pkt_set_section_count(ldns_pkt *packet, ldns_pkt_section s, uint16_t count)
Set a packet's section count to x.
Definition: packet.c:651
ldns_status ldns_pkt_query_new_frm_str(ldns_pkt **p, const char *name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a query packet for the given name, type, class.
Definition: packet.c:1083
uint16_t ldns_pkt_edns_z(const ldns_pkt *packet)
return the packet's edns z value
Definition: packet.c:225
ldns_edns_option_list * ldns_pkt_edns_get_option_list(ldns_pkt *packet)
Returns a list of structured EDNS options.
Definition: packet.c:823
void ldns_pkt_set_arcount(ldns_pkt *packet, uint16_t arcount)
Set the packet's arcount.
Definition: packet.c:581
ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *packet)
Return the packet's response code.
Definition: packet.c:94
void ldns_pkt_set_rcode(ldns_pkt *packet, uint8_t rcode)
Set the packet's response code.
Definition: packet.c:557
ldns_pkt * ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a packet with a query in it for the given name, type and class.
Definition: packet.c:1150
uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet)
return the packet's edns udp size
Definition: packet.c:207
void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v)
Set the packet's edns version.
Definition: packet.c:624
void ldns_pkt_set_edns_unassigned(ldns_pkt *packet, uint16_t value)
Set the packet's EDNS header bits that are unassigned.
Definition: packet.c:253
ldns_rr_list * ldns_pkt_rr_list_by_type(const ldns_pkt *packet, ldns_rr_type type, ldns_pkt_section sec)
return all the rr with a specific type from a packet.
Definition: packet.c:304
enum ldns_enum_pkt_type ldns_pkt_type
Definition: packet.h:300
#define LDNS_RA
Definition: packet.h:32
#define LDNS_QR
Definition: packet.h:27
#define LDNS_CD
Definition: packet.h:31
#define LDNS_TC
Definition: packet.h:29
enum ldns_enum_pkt_rcode ldns_pkt_rcode
Definition: packet.h:69
@ LDNS_PACKET_QUERY
Definition: packet.h:47
enum ldns_enum_pkt_opcode ldns_pkt_opcode
Definition: packet.h:53
enum ldns_enum_pkt_section ldns_pkt_section
Definition: packet.h:287
#define LDNS_AA
Definition: packet.h:28
@ LDNS_SECTION_ANY
bogus section, if not interested
Definition: packet.h:283
@ LDNS_SECTION_QUESTION
Definition: packet.h:278
@ LDNS_SECTION_ANSWER
Definition: packet.h:279
@ LDNS_SECTION_ADDITIONAL
Definition: packet.h:281
@ LDNS_SECTION_AUTHORITY
Definition: packet.h:280
@ LDNS_SECTION_ANY_NOQUESTION
used to get all non-question rrs from a packet
Definition: packet.h:285
#define LDNS_RD
Definition: packet.h:30
@ LDNS_RCODE_NXDOMAIN
Definition: packet.h:60
#define LDNS_AD
Definition: packet.h:33
@ LDNS_PACKET_REFERRAL
Definition: packet.h:294
@ LDNS_PACKET_UNKNOWN
Definition: packet.h:298
@ LDNS_PACKET_NODATA
Definition: packet.h:297
@ LDNS_PACKET_NXDOMAIN
Definition: packet.h:296
@ LDNS_PACKET_ANSWER
Definition: packet.h:295
void ldns_rdf_deep_free(ldns_rdf *rd)
frees a rdf structure and frees the data.
Definition: rdata.c:230
@ LDNS_RDF_TYPE_INT32
32 bits
Definition: rdata.h:56
size_t ldns_rdf_size(const ldns_rdf *rd)
returns the size of the rdf.
Definition: rdata.c:24
uint8_t * ldns_rdf_data(const ldns_rdf *rd)
returns the data of the rdf.
Definition: rdata.c:38
ldns_rdf * ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value)
returns an rdf that contains the given int32 value.
Definition: rdata.c:147
void ldns_rdf_free(ldns_rdf *rd)
frees a rdf structure, leaving the data pointer intact.
Definition: rdata.c:241
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition: rdata.c:222
void ldns_rr_list_free(ldns_rr_list *rr_list)
frees an rr_list structure.
Definition: rr.c:1015
ldns_rr * ldns_rr_list_rr(const ldns_rr_list *rr_list, size_t nr)
returns a specific rr of an rrlist.
Definition: rr.c:994
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
Definition: rr.c:923
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition: rr.c:1024
void ldns_rr_free(ldns_rr *rr)
frees an RR structure
Definition: rr.c:81
void ldns_rr_set_owner(ldns_rr *rr, ldns_rdf *owner)
sets the owner in the rr structure.
Definition: rr.c:808
signed char ldns_rr_list_contains_rr(const ldns_rr_list *rr_list, const ldns_rr *rr)
returns true if the given rr is one of the rrs in the list, or if it is equal to one
Definition: rr.c:1244
enum ldns_enum_rr_type ldns_rr_type
Definition: rr.h:251
void ldns_rr_set_type(ldns_rr *rr, ldns_rr_type rr_type)
sets the type in the rr.
Definition: rr.c:832
@ LDNS_RR_TYPE_A
a host address
Definition: rr.h:80
@ LDNS_RR_TYPE_IXFR
Definition: rr.h:217
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition: rr.h:90
@ LDNS_RR_TYPE_NS
an authoritative name server
Definition: rr.h:82
signed char ldns_rr_list_push_rr(ldns_rr_list *rr_list, const ldns_rr *rr)
pushes an rr to an rrlist.
Definition: rr.c:1136
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
Definition: rr.c:961
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition: rr.c:947
void ldns_rr_set_question(ldns_rr *rr, signed char question)
sets the question flag in the rr structure.
Definition: rr.c:814
ldns_rr_list * ldns_rr_list_cat_clone(const ldns_rr_list *left, const ldns_rr_list *right)
concatenates two ldns_rr_lists together, but makes clones of the rr's (instead of pointer copying).
Definition: rr.c:1063
enum ldns_enum_rr_class ldns_rr_class
Definition: rr.h:61
void ldns_rr_set_class(ldns_rr *rr, ldns_rr_class rr_class)
sets the class in the rr.
Definition: rr.c:838
ldns_rr_list * ldns_rr_list_new(void)
creates a new rr_list structure.
Definition: rr.c:1004
signed char ldns_rr_push_rdf(ldns_rr *rr, const ldns_rdf *f)
sets rd_field member, it will be placed in the next available spot.
Definition: rr.c:861
@ LDNS_RR_CLASS_IN
the Internet
Definition: rr.h:47
ldns_rr * ldns_rr_clone(const ldns_rr *rr)
clones a rr and all its data
Definition: rr.c:1404
ldns_rr_list * ldns_rr_list_clone(const ldns_rr_list *rrlist)
clones an rrlist.
Definition: rr.c:1435
ldns_rr * ldns_rr_new(void)
creates a new rr structure.
Definition: rr.c:30
ldns_status ldns_str2rdf_dname(ldns_rdf **rd, const char *str)
convert a dname string into wireformat
Definition: str2host.c:311
The struct that stores an ordered EDNS option.
Definition: edns.h:97
signed char _rd
Recursion desired.
Definition: packet.h:204
uint16_t _id
Id of a packet.
Definition: packet.h:196
ldns_pkt_opcode _opcode
Query type.
Definition: packet.h:212
signed char _cd
Checking disabled.
Definition: packet.h:206
signed char _ra
Recursion available.
Definition: packet.h:208
uint16_t _ancount
answer sec
Definition: packet.h:218
signed char _qr
Query bit (0=query, 1=answer)
Definition: packet.h:198
signed char _tc
Packet truncated.
Definition: packet.h:202
signed char _ad
Authentic data.
Definition: packet.h:210
uint16_t _qdcount
question sec
Definition: packet.h:216
uint16_t _arcount
add sec
Definition: packet.h:222
uint8_t _rcode
Response code.
Definition: packet.h:214
uint16_t _nscount
auth sec
Definition: packet.h:220
signed char _aa
Authoritative answer.
Definition: packet.h:200
A general purpose lookup table.
Definition: util.h:156
DNS packet.
Definition: packet.h:235
struct timeval timestamp
Timestamp of the time the packet was sent or created.
Definition: packet.h:242
uint8_t _edns_extended_rcode
EDNS0 Extended rcode.
Definition: packet.h:252
ldns_rr_list * _authority
Authority section.
Definition: packet.h:268
ldns_hdr * _header
Header section.
Definition: packet.h:237
uint32_t _querytime
The duration of the query this packet is an answer to.
Definition: packet.h:244
uint16_t _edns_udp_size
EDNS0 available buffer size, see RFC2671.
Definition: packet.h:250
ldns_rdf * _answerfrom
an rdf (A or AAAA) with the IP address of the server it is from
Definition: packet.h:240
uint8_t _edns_present
Definition: packet.h:256
uint8_t _edns_version
EDNS Version.
Definition: packet.h:254
size_t _size
The size of the wire format of the packet in octets.
Definition: packet.h:246
ldns_rr_list * _answer
Answer section.
Definition: packet.h:266
ldns_rdf * _edns_data
Arbitrary EDNS rdata.
Definition: packet.h:260
ldns_edns_option_list * _edns_list
Structed EDNS data.
Definition: packet.h:262
uint16_t _edns_z
Reserved EDNS data bits.
Definition: packet.h:258
ldns_rr_list * _additional
Additional section.
Definition: packet.h:270
ldns_rr * _tsig_rr
Optional tsig rr.
Definition: packet.h:248
ldns_rr_list * _question
Question section.
Definition: packet.h:264
Resource record data field.
Definition: rdata.h:197
List or Set of Resource Records.
Definition: rr.h:346
Resource Record.
Definition: rr.h:318
#define LDNS_FREE(ptr)
Definition: util.h:60
#define LDNS_MALLOC(type)
Memory management macros.
Definition: util.h:49
#define LDNS_XMALLOC(type, count)
Definition: util.h:51
uint16_t ldns_get_random(void)
Get random number.
Definition: util.c:412