OpenDNSSEC-signer  1.4.3
buffer.c
Go to the documentation of this file.
1 /*
2  * $Id: buffer.c 4958 2011-04-18 07:11:09Z matthijs $
3  *
4  * Copyright (c) 2011 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
51 #include "config.h"
52 #include "shared/log.h"
53 #include "wire/buffer.h"
54 
55 #include <string.h>
56 
57 static const char* buffer_str = "buffer";
58 
60  { LDNS_RCODE_NOERROR, "NOERROR" },
61  { LDNS_RCODE_FORMERR, "FORMERR" },
62  { LDNS_RCODE_SERVFAIL, "SERVFAIL" },
63  { LDNS_RCODE_NXDOMAIN, "NXDOMAIN" },
64  { LDNS_RCODE_NOTIMPL, "NOTIMPL" },
65  { LDNS_RCODE_REFUSED, "REFUSED" },
66  { LDNS_RCODE_YXDOMAIN, "YXDOMAIN" },
67  { LDNS_RCODE_YXRRSET, "YXRRSET" },
68  { LDNS_RCODE_NXRRSET, "NXRRSET" },
69  { LDNS_RCODE_NOTAUTH, "NOTAUTH" },
70  { LDNS_RCODE_NOTZONE, "NOTZONE" },
71  { 0, NULL }
72 };
73 
74 
80 buffer_create(allocator_type* allocator, size_t capacity)
81 {
82  buffer_type* buffer = NULL;
83  if (!allocator || !capacity) {
84  return NULL;
85  }
86  buffer = (buffer_type *) allocator_alloc(allocator, sizeof(buffer_type));
87  if (!buffer) {
88  return NULL;
89  }
90  buffer->data = (uint8_t*) calloc(capacity, sizeof(uint8_t));
91  buffer->position = 0;
92  buffer->limit = capacity;
93  buffer->capacity = capacity;
94  buffer->fixed = 0;
95  return buffer;
96 }
97 
98 
103 void
104 buffer_create_from(buffer_type* buffer, void* data, size_t size)
105 {
106  ods_log_assert(buffer);
107  buffer->data = (uint8_t*) data;
108  buffer->position = 0;
109  buffer->limit = size;
110  buffer->capacity = size;
111  buffer->fixed = 1;
112  return;
113 }
114 
115 
120 void
122 {
123  ods_log_assert(buffer);
124  buffer->position = 0;
125  buffer->limit = buffer->capacity;
126  return;
127 }
128 
129 
134 void
136 {
137  ods_log_assert(buffer);
138  buffer->limit = buffer->position;
139  buffer->position = 0;
140  return;
141 }
142 
143 
148 void
150 {
151  ods_log_assert(buffer);
152  buffer->position = 0;
153  return;
154 }
155 
156 
161 size_t
163 {
164  ods_log_assert(buffer);
165  return buffer->position;
166 }
167 
168 
173 void
174 buffer_set_position(buffer_type* buffer, size_t pos)
175 {
176  ods_log_assert(buffer);
177  ods_log_assert(pos <= buffer->limit);
178  buffer->position = pos;
179  return;
180 }
181 
182 
187 void
188 buffer_skip(buffer_type* buffer, ssize_t count)
189 {
190  ods_log_assert(buffer);
191  ods_log_assert(buffer->position + count <= buffer->limit);
192  buffer->position += count;
193  return;
194 }
195 
196 
201 static int
202 get_bit(uint8_t bits[], size_t index)
203 {
204  return bits[index / 8] & (1 << (7 - index % 8));
205 }
206 
207 
212 static void
213 set_bit(uint8_t bits[], size_t index)
214 {
215  bits[index / 8] |= (1 << (7 - index % 8));
216  return;
217 }
218 
219 
224 static int
225 label_is_pointer(const uint8_t* label)
226 {
227  ods_log_assert(label);
228  return (label[0] & 0xc0) == 0xc0;
229 }
230 
231 
236 static uint16_t
237 label_pointer_location(const uint8_t* label)
238 {
239  ods_log_assert(label);
240  ods_log_assert(label_is_pointer(label));
241  return ((uint16_t) (label[0] & ~0xc0) << 8) | (uint16_t) label[1];
242 }
243 
244 
249 static int
250 label_is_normal(const uint8_t* label)
251 {
252  ods_log_assert(label);
253  return (label[0] & 0xc0) == 0;
254 }
255 
256 /*
257  * Is root label?
258  *
259  */
260 static inline int
261 label_is_root(const uint8_t* label)
262 {
263  ods_log_assert(label);
264  return label[0] == 0;
265 }
266 
267 
268 /*
269  * Label length.
270  *
271  */
272 static uint8_t
273 label_length(const uint8_t* label)
274 {
275  ods_log_assert(label);
276  ods_log_assert(label_is_normal(label));
277  return label[0];
278 }
279 
280 
285 size_t
286 buffer_read_dname(buffer_type* buffer, uint8_t* dname, unsigned allow_pointers)
287 {
288  int done = 0;
289  uint8_t visited[(MAX_PACKET_SIZE+7)/8];
290  size_t dname_length = 0;
291  const uint8_t *label = NULL;
292  ssize_t mark = -1;
293  ods_log_assert(buffer);
294  memset(visited, 0, (buffer_limit(buffer)+7)/8);
295 
296  while (!done) {
297  if (!buffer_available(buffer, 1)) {
298  return 0;
299  }
300  if (get_bit(visited, buffer_position(buffer))) {
301  ods_log_error("[%s] dname loop!", buffer_str);
302  return 0;
303  }
304  set_bit(visited, buffer_position(buffer));
305  label = buffer_current(buffer);
306  if (label_is_pointer(label)) {
307  size_t pointer = 0;
308  if (!allow_pointers) {
309  return 0;
310  }
311  if (!buffer_available(buffer, 2)) {
312  return 0;
313  }
314  pointer = label_pointer_location(label);
315  if (pointer >= buffer_limit(buffer)) {
316  return 0;
317  }
318  buffer_skip(buffer, 2);
319  if (mark == -1) {
320  mark = buffer_position(buffer);
321  }
322  buffer_set_position(buffer, pointer);
323  } else if (label_is_normal(label)) {
324  size_t length = label_length(label) + 1;
325  done = label_is_root(label);
326  if (!buffer_available(buffer, length)) {
327  return 0;
328  }
329  if (dname_length + length >= MAXDOMAINLEN+1) {
330  return 0;
331  }
332  buffer_read(buffer, dname + dname_length, length);
333  dname_length += length;
334  } else {
335  return 0;
336  }
337  }
338  if (mark != -1) {
339  buffer_set_position(buffer, mark);
340  }
341  return dname_length;
342 }
343 
344 
349 int
351 {
352  ods_log_assert(buffer);
353  while (1) {
354  uint8_t label_size = 0;
355  if (!buffer_available(buffer, 1)) {
356  return 0;
357  }
358  label_size = buffer_read_u8(buffer);
359  if (label_size == 0) {
360  break;
361  } else if ((label_size & 0xc0) != 0) {
362  if (!buffer_available(buffer, 1)) {
363  return 0;
364  }
365  buffer_skip(buffer, 1);
366  break;
367  } else if (!buffer_available(buffer, label_size)) {
368  return 0;
369  } else {
370  buffer_skip(buffer, label_size);
371  }
372  }
373  return 1;
374 }
375 
376 
381 int
382 buffer_skip_rr(buffer_type* buffer, unsigned qrr)
383 {
384  if (!buffer_skip_dname(buffer)) {
385  return 0;
386  }
387  if (qrr) {
388  if (!buffer_available(buffer, 4)) {
389  return 0;
390  }
391  buffer_skip(buffer, 4);
392  } else {
393  uint16_t rdata_size;
394  if (!buffer_available(buffer, 10)) {
395  return 0;
396  }
397  buffer_skip(buffer, 8);
398  rdata_size = buffer_read_u16(buffer);
399  if (!buffer_available(buffer, rdata_size)) {
400  return 0;
401  }
402  buffer_skip(buffer, rdata_size);
403  }
404  return 1;
405 }
406 
407 
412 size_t
414 {
415  ods_log_assert(buffer);
416  return buffer->limit;
417 }
418 
419 
424 void
425 buffer_set_limit(buffer_type* buffer, size_t limit)
426 {
427  ods_log_assert(buffer);
428  ods_log_assert(limit <= buffer->capacity);
429  buffer->limit = limit;
430  if (buffer->position > buffer->limit) {
431  buffer->position = buffer->limit;
432  }
433  return;
434 }
435 
436 
441 size_t
443 {
444  ods_log_assert(buffer);
445  return buffer->capacity;
446 }
447 
448 
453 uint8_t*
454 buffer_at(buffer_type* buffer, size_t at)
455 {
456  ods_log_assert(buffer);
457  ods_log_assert(at <= buffer->limit);
458  return buffer->data + at;
459 }
460 
461 
466 uint8_t*
468 {
469  ods_log_assert(buffer);
470  return buffer_at(buffer, 0);
471 }
472 
473 
478 uint8_t*
480 {
481  ods_log_assert(buffer);
482  return buffer_at(buffer, buffer->limit);
483 }
484 
485 
490 uint8_t*
492 {
493  ods_log_assert(buffer);
494  return buffer_at(buffer, buffer->position);
495 }
496 
497 
502 static size_t
503 buffer_remaining_at(buffer_type* buffer, size_t at)
504 {
505  ods_log_assert(buffer);
506  ods_log_assert(at <= buffer->limit);
507  return buffer->limit - at;
508 }
509 
510 
515 size_t
517 {
518  ods_log_assert(buffer);
519  return buffer_remaining_at(buffer, buffer->position);
520 }
521 
522 
527 static int
528 buffer_available_at(buffer_type *buffer, size_t at, size_t count)
529 {
530  ods_log_assert(buffer);
531  return count <= buffer_remaining_at(buffer, at);
532 }
533 
534 
539 int
540 buffer_available(buffer_type *buffer, size_t count)
541 {
542  ods_log_assert(buffer);
543  return buffer_available_at(buffer, buffer->position, count);
544 }
545 
546 
551 static void
552 buffer_write_u8_at(buffer_type* buffer, size_t at, uint8_t data)
553 {
554  ods_log_assert(buffer);
555  ods_log_assert(buffer_available_at(buffer, at, sizeof(data)));
556  buffer->data[at] = data;
557  return;
558 }
559 
560 
565 void
566 buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data)
567 {
568  ods_log_assert(buffer);
569  ods_log_assert(buffer_available_at(buffer, at, sizeof(data)));
570  write_uint16(buffer->data + at, data);
571  return;
572 }
573 
574 
579 static void
580 buffer_write_u32_at(buffer_type* buffer, size_t at, uint32_t data)
581 {
582  ods_log_assert(buffer);
583  ods_log_assert(buffer_available_at(buffer, at, sizeof(data)));
584  write_uint32(buffer->data + at, data);
585  return;
586 }
587 
588 
593 void
594 buffer_write(buffer_type* buffer, const void* data, size_t count)
595 {
596  ods_log_assert(buffer);
597  ods_log_assert(buffer_available(buffer, count));
598  memcpy(buffer->data + buffer->position, data, count);
599  buffer->position += count;
600  return;
601 }
602 
603 
608 void
609 buffer_write_u8(buffer_type* buffer, uint8_t data)
610 {
611  ods_log_assert(buffer);
612  buffer_write_u8_at(buffer, buffer->position, data);
613  buffer->position += sizeof(data);
614  return;
615 }
616 
617 
622 void
623 buffer_write_u16(buffer_type* buffer, uint16_t data)
624 {
625  ods_log_assert(buffer);
626  buffer_write_u16_at(buffer, buffer->position, data);
627  buffer->position += sizeof(data);
628  return;
629 }
630 
631 
636 void
637 buffer_write_u32(buffer_type* buffer, uint32_t data)
638 {
639  ods_log_assert(buffer);
640  buffer_write_u32_at(buffer, buffer->position, data);
641  buffer->position += sizeof(data);
642  return;
643 }
644 
645 
650 void
651 buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf)
652 {
653  ods_log_assert(buffer);
654  ods_log_assert(rdf);
655  buffer_write(buffer, ldns_rdf_data(rdf), ldns_rdf_size(rdf));
656  /* position updated by buffer_write() */
657  return;
658 }
659 
660 
665 int
666 buffer_write_rr(buffer_type* buffer, ldns_rr* rr)
667 {
668  size_t i = 0;
669  size_t tc_mark = 0;
670  size_t rdlength_pos = 0;
671  uint16_t rdlength = 0;
672  ods_log_assert(buffer);
673  ods_log_assert(rr);
674  /* set truncation mark, in case rr does not fit */
675  tc_mark = buffer_position(buffer);
676  /* owner type class ttl */
677  if (!buffer_available(buffer, ldns_rdf_size(ldns_rr_owner(rr)))) {
678  goto buffer_tc;
679  }
680  buffer_write_rdf(buffer, ldns_rr_owner(rr));
681  if (!buffer_available(buffer, sizeof(uint16_t) + sizeof(uint16_t) +
682  sizeof(uint32_t) + sizeof(rdlength))) {
683  goto buffer_tc;
684  }
685  buffer_write_u16(buffer, (uint16_t) ldns_rr_get_type(rr));
686  buffer_write_u16(buffer, (uint16_t) ldns_rr_get_class(rr));
687  buffer_write_u32(buffer, (uint32_t) ldns_rr_ttl(rr));
688  /* skip rdlength */
689  rdlength_pos = buffer_position(buffer);
690  buffer_skip(buffer, sizeof(rdlength));
691  /* write rdata */
692  for (i=0; i < ldns_rr_rd_count(rr); i++) {
693  if (!buffer_available(buffer, ldns_rdf_size(ldns_rr_rdf(rr, i)))) {
694  goto buffer_tc;
695  }
696  buffer_write_rdf(buffer, ldns_rr_rdf(rr, i));
697  }
698  /* write rdlength */
699  rdlength = buffer_position(buffer) - rdlength_pos - sizeof(rdlength);
700  buffer_write_u16_at(buffer, rdlength_pos, rdlength);
701  /* position updated by buffer_write() */
702  return 1;
703 
704 buffer_tc:
705  buffer_set_position(buffer, tc_mark);
706  return 0;
707 }
708 
709 
714 static uint8_t
715 buffer_read_u8_at(buffer_type* buffer, size_t at)
716 {
717  ods_log_assert(buffer);
718  ods_log_assert(at < buffer->capacity);
719  return buffer->data[at];
720 
721 }
722 
723 
728 static uint16_t
729 buffer_read_u16_at(buffer_type* buffer, size_t at)
730 {
731  ods_log_assert(buffer);
732  return read_uint16(buffer->data + at);
733 }
734 
735 
740 static uint32_t
741 buffer_read_u32_at(buffer_type* buffer, size_t at)
742 {
743  ods_log_assert(buffer);
744  return read_uint32(buffer->data + at);
745 }
746 
747 
752 void
753 buffer_read(buffer_type* buffer, void* data, size_t count)
754 {
755  ods_log_assert(buffer);
756  ods_log_assert(buffer_available(buffer, count));
757  memcpy(data, buffer->data + buffer->position, count);
758  buffer->position += count;
759  return;
760 }
761 
762 
767 uint8_t
769 {
770  uint16_t result = 0;
771  ods_log_assert(buffer);
772  result = buffer_read_u8_at(buffer, buffer->position);
773  buffer->position += sizeof(uint8_t);
774  return result;
775 }
776 
777 
782 uint16_t
784 {
785  uint16_t result = 0;
786  ods_log_assert(buffer);
787  result = buffer_read_u16_at(buffer, buffer->position);
788  buffer->position += sizeof(uint16_t);
789  return result;
790 }
791 
792 
797 uint32_t
799 {
800  uint32_t result = 0;
801  ods_log_assert(buffer);
802  result = buffer_read_u32_at(buffer, buffer->position);
803  buffer->position += sizeof(uint32_t);
804  return result;
805 }
806 
807 
812 uint16_t
814 {
815  ods_log_assert(buffer);
816  return buffer_read_u16_at(buffer, 0);
817 }
818 
823 static uint16_t
824 random_id(void)
825 {
826  return ldns_get_random();
827 }
828 
833 void
835 {
836  uint16_t qid = 0;
837  ods_log_assert(buffer);
838  qid = random_id();
839  buffer_write_u16_at(buffer, 0, qid);
840  return;
841 }
842 
843 
848 uint16_t
850 {
851  ods_log_assert(buffer);
852  return (uint16_t) buffer_read_u16_at(buffer, 2);
853 }
854 
855 
860 void
861 buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags)
862 {
863  ods_log_assert(buffer);
864  buffer_write_u16_at(buffer, 2, flags);
865  return;
866 }
867 
868 
873 int
875 {
876  ods_log_assert(buffer);
877  return (int) QR(buffer);
878 }
879 
880 
885 void
887 {
888  ods_log_assert(buffer);
889  QR_SET(buffer);
890  return;
891 }
892 
893 
898 void
900 {
901  ods_log_assert(buffer);
902  QR_CLR(buffer);
903  return;
904 }
905 
906 
911 ldns_pkt_opcode
913 {
914  ods_log_assert(buffer);
915  return (ldns_pkt_opcode) OPCODE(buffer);
916 }
917 
918 
923 void
924 buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode)
925 {
926  ods_log_assert(buffer);
927  OPCODE_SET(buffer, opcode);
928  return;
929 }
930 
931 
936 int
938 {
939  ods_log_assert(buffer);
940  return (int) AA(buffer);
941 }
942 
943 
948 void
950 {
951  ods_log_assert(buffer);
952  AA_SET(buffer);
953  return;
954 }
955 
956 
961 int
963 {
964  ods_log_assert(buffer);
965  return (int) TC(buffer);
966 }
967 
968 
973 int
975 {
976  ods_log_assert(buffer);
977  return (int) RD(buffer);
978 }
979 
980 
985 int
987 {
988  ods_log_assert(buffer);
989  return (int) RA(buffer);
990 }
991 
992 
997 int
999 {
1000  ods_log_assert(buffer);
1001  return (int) AD(buffer);
1002 }
1003 
1004 
1009 int
1011 {
1012  ods_log_assert(buffer);
1013  return (int) CD(buffer);
1014 }
1015 
1016 
1021 ldns_pkt_rcode
1023 {
1024  ods_log_assert(buffer);
1025  return (ldns_pkt_rcode) RCODE(buffer);
1026 }
1027 
1028 
1033 void
1034 buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode)
1035 {
1036  ods_log_assert(buffer);
1037  RCODE_SET(buffer, rcode);
1038  return;
1039 }
1040 
1041 
1046 const char*
1047 buffer_rcode2str(ldns_pkt_rcode rcode)
1048 {
1049  ods_lookup_table *lt;
1050  lt = ods_lookup_by_id(ods_rcode_str, rcode);
1051  if (lt) {
1052  return lt->name;
1053  }
1054  return NULL;
1055 }
1056 
1057 
1062 uint16_t
1064 {
1065  ods_log_assert(buffer);
1066  return buffer_read_u16_at(buffer, 4);
1067 }
1068 
1069 
1074 void
1075 buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count)
1076 {
1077  ods_log_assert(buffer);
1078  buffer_write_u16_at(buffer, 4, count);
1079  return;
1080 }
1081 
1082 
1087 uint16_t
1089 {
1090  ods_log_assert(buffer);
1091  return buffer_read_u16_at(buffer, 6);
1092 }
1093 
1094 
1099 void
1100 buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count)
1101 {
1102  ods_log_assert(buffer);
1103  buffer_write_u16_at(buffer, 6, count);
1104  return;
1105 }
1106 
1107 
1112 uint16_t
1114 {
1115  ods_log_assert(buffer);
1116  return buffer_read_u16_at(buffer, 8);
1117 }
1118 
1119 
1124 void
1125 buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count)
1126 {
1127  ods_log_assert(buffer);
1128  buffer_write_u16_at(buffer, 8, count);
1129  return;
1130 }
1131 
1132 
1137 uint16_t
1139 {
1140  ods_log_assert(buffer);
1141  return buffer_read_u16_at(buffer, 10);
1142 }
1143 
1144 
1149 void
1150 buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count)
1151 {
1152  ods_log_assert(buffer);
1153  buffer_write_u16_at(buffer, 10, count);
1154  return;
1155 }
1156 
1157 
1162 static void
1163 buffer_pkt_new(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
1164  ldns_rr_class qclass, ldns_pkt_opcode opcode)
1165 {
1166  ods_log_assert(buffer);
1167  ods_log_assert(qname);
1168  ods_log_assert(qtype);
1169  ods_log_assert(qclass);
1170  /* The header */
1171  buffer_clear(buffer);
1172  buffer_pkt_set_random_id(buffer);
1173  buffer_pkt_set_opcode(buffer, opcode);
1174  buffer_pkt_clear_qr(buffer);
1175  buffer_pkt_set_rcode(buffer, LDNS_RCODE_NOERROR);
1176  buffer_pkt_set_qdcount(buffer, 1);
1177  buffer_pkt_set_ancount(buffer, 0);
1178  buffer_pkt_set_nscount(buffer, 0);
1179  buffer_pkt_set_arcount(buffer, 0);
1181  /* The question record */
1182  buffer_write_rdf(buffer, qname);
1183  buffer_write_u16(buffer, qtype);
1184  buffer_write_u16(buffer, qclass);
1185  return;
1186 }
1187 
1188 
1193 void
1194 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
1195  ldns_rr_class qclass)
1196 {
1197  buffer_pkt_new(buffer, qname, qtype, qclass, LDNS_PACKET_QUERY);
1198  buffer_pkt_set_flags(buffer, 0);
1199  return;
1200 }
1201 
1202 
1207 void
1208 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass)
1209 {
1210  buffer_pkt_new(buffer, qname, LDNS_RR_TYPE_SOA, qclass,
1211  LDNS_PACKET_NOTIFY);
1212  return;
1213 }
1214 
1215 
1220 void
1221 buffer_pkt_axfr(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass)
1222 {
1223  buffer_pkt_new(buffer, qname, LDNS_RR_TYPE_AXFR, qclass,
1224  LDNS_PACKET_QUERY);
1225  buffer_pkt_set_qr(buffer);
1226  return;
1227 }
1228 
1229 
1234 void
1235 buffer_pkt_print(FILE* fd, buffer_type* buffer)
1236 {
1237  ldns_status status = LDNS_STATUS_OK;
1238  ldns_pkt* pkt = NULL;
1239  ods_log_assert(fd);
1240  ods_log_assert(buffer);
1241  status = ldns_wire2pkt(&pkt, buffer_begin(buffer),
1242  buffer_remaining(buffer));
1243  if (status == LDNS_STATUS_OK) {
1244  ods_log_assert(pkt);
1245  ldns_pkt_print(fd, pkt);
1246  ldns_pkt_free(pkt);
1247  } else {
1248  fprintf(fd, ";;\n");
1249  fprintf(fd, ";; Bogus packet: %s\n", ldns_get_errorstr_by_id(status));
1250  fprintf(fd, ";;\n");
1251  fprintf(fd, ";;\n");
1252  fprintf(fd, "\n");
1253  }
1254  return;
1255 }
1256 
1257 
1262 void
1264 {
1265  if (!buffer || !allocator) {
1266  return;
1267  }
1268  free((void*)buffer->data);
1269  allocator_deallocate(allocator, (void*) buffer);
1270  return;
1271 }
1272 
1273 
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:912
#define OPCODE(packet)
Definition: buffer.h:61
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:974
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:834
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:454
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1138
#define BUFFER_PKT_HEADER_SIZE
Definition: buffer.h:45
void * allocator_alloc(allocator_type *allocator, size_t size)
Definition: allocator.c:68
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:1063
#define QR(packet)
Definition: buffer.h:55
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:188
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:861
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:783
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:382
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:135
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1075
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:121
#define OPCODE_SET(packet, opcode)
Definition: buffer.h:62
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:768
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:609
void ods_log_error(const char *format,...)
Definition: log.c:336
#define AA(packet)
Definition: buffer.h:67
ods_lookup_table * ods_lookup_by_id(ods_lookup_table *table, int id)
Definition: status.c:95
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:874
void buffer_pkt_print(FILE *fd, buffer_type *buffer)
Definition: buffer.c:1235
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:594
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:813
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:491
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:80
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1088
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:413
#define CD(packet)
Definition: buffer.h:97
void buffer_rewind(buffer_type *buffer)
Definition: buffer.c:149
size_t capacity
Definition: buffer.h:116
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1100
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1125
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:425
#define QR_SET(packet)
Definition: buffer.h:56
unsigned fixed
Definition: buffer.h:118
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:849
#define RA(packet)
Definition: buffer.h:85
void buffer_pkt_axfr(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1221
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:442
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:350
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:798
size_t position
Definition: buffer.h:114
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:924
#define MAX_PACKET_SIZE
Definition: buffer.h:50
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1194
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:623
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:286
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:637
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:949
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:753
#define TC(packet)
Definition: buffer.h:73
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition: buffer.c:1047
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:937
uint8_t * data
Definition: buffer.h:117
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1113
const char * name
Definition: status.h:96
ods_lookup_table ods_rcode_str[]
Definition: buffer.c:59
#define RD(packet)
Definition: buffer.h:79
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:566
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:174
size_t limit
Definition: buffer.h:115
#define AA_SET(packet)
Definition: buffer.h:68
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1208
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:540
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:998
#define MAXDOMAINLEN
Definition: buffer.h:46
#define RCODE_SET(packet, rcode)
Definition: buffer.h:104
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition: buffer.c:899
#define AD(packet)
Definition: buffer.h:91
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:516
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:1034
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:651
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:666
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:986
void allocator_deallocate(allocator_type *allocator, void *data)
Definition: allocator.c:137
#define RCODE(packet)
Definition: buffer.h:103
void buffer_create_from(buffer_type *buffer, void *data, size_t size)
Definition: buffer.c:104
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:162
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1263
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1150
#define ods_log_assert(x)
Definition: log.h:156
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:1010
#define QR_CLR(packet)
Definition: buffer.h:57
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:886
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:467
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:1022
uint8_t * buffer_end(buffer_type *buffer)
Definition: buffer.c:479
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:962