OpenDNSSEC-signer  1.4.3
buffer.h
Go to the documentation of this file.
1 /*
2  * $Id: buffer.h 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 
34 #ifndef WIRE_BUFFER_H
35 #define WIRE_BUFFER_H
36 
37 #include "config.h"
38 #include "shared/allocator.h"
39 #include "shared/log.h"
40 #include "shared/status.h"
41 
42 #include <ldns/ldns.h>
43 #include <stdint.h>
44 
45 #define BUFFER_PKT_HEADER_SIZE 12
46 #define MAXDOMAINLEN 255
47 #define MAX_RDLENGTH 65535
48 #define MAX_RR_SIZE \
49  (MAXDOMAINLEN + sizeof(uint32_t) + 4*sizeof(uint16_t) + MAX_RDLENGTH)
50 #define MAX_PACKET_SIZE 65535
51 #define PACKET_BUFFER_SIZE (MAX_PACKET_SIZE + MAX_RR_SIZE)
52 
53 #define QR_MASK 0x80U
54 #define QR_SHIFT 7
55 #define QR(packet) (*buffer_at((packet), 2) & QR_MASK)
56 #define QR_SET(packet) (*buffer_at((packet), 2) |= QR_MASK)
57 #define QR_CLR(packet) (*buffer_at((packet), 2) &= ~QR_MASK)
58 
59 #define OPCODE_MASK 0x78U
60 #define OPCODE_SHIFT 3
61 #define OPCODE(packet) ((*buffer_at((packet), 2) & OPCODE_MASK) >> OPCODE_SHIFT)
62 #define OPCODE_SET(packet, opcode) \
63  (*buffer_at((packet), 2) = (*buffer_at((packet), 2) & ~OPCODE_MASK) | ((opcode) << OPCODE_SHIFT))
64 
65 #define AA_MASK 0x04U
66 #define AA_SHIFT 2
67 #define AA(packet) (*buffer_at((packet), 2) & AA_MASK)
68 #define AA_SET(packet) (*buffer_at((packet), 2) |= AA_MASK)
69 #define AA_CLR(packet) (*buffer_at((packet), 2) &= ~AA_MASK)
70 
71 #define TC_MASK 0x02U
72 #define TC_SHIFT 1
73 #define TC(packet) (*buffer_at((packet), 2) & TC_MASK)
74 #define TC_SET(packet) (*buffer_at((packet), 2) |= TC_MASK)
75 #define TC_CLR(packet) (*buffer_at((packet), 2) &= ~TC_MASK)
76 
77 #define RD_MASK 0x01U
78 #define RD_SHIFT 0
79 #define RD(packet) (*buffer_at((packet), 2) & RD_MASK)
80 #define RD_SET(packet) (*buffer_at((packet), 2) |= RD_MASK)
81 #define RD_CLR(packet) (*buffer_at((packet), 2) &= ~RD_MASK)
82 
83 #define RA_MASK 0x80U
84 #define RA_SHIFT 7
85 #define RA(packet) (*buffer_at((packet), 3) & RA_MASK)
86 #define RA_SET(packet) (*buffer_at((packet), 3) |= RA_MASK)
87 #define RA_CLR(packet) (*buffer_at((packet), 3) &= ~RA_MASK)
88 
89 #define AD_MASK 0x20U
90 #define AD_SHIFT 5
91 #define AD(packet) (*buffer_at((packet), 3) & AD_MASK)
92 #define AD_SET(packet) (*buffer_at((packet), 3) |= AD_MASK)
93 #define AD_CLR(packet) (*buffer_at((packet), 3) &= ~AD_MASK)
94 
95 #define CD_MASK 0x10U
96 #define CD_SHIFT 4
97 #define CD(packet) (*buffer_at((packet), 3) & CD_MASK)
98 #define CD_SET(packet) (*buffer_at((packet), 3) |= CD_MASK)
99 #define CD_CLR(packet) (*buffer_at((packet), 3) &= ~CD_MASK)
100 
101 #define RCODE_MASK 0x0fU
102 #define RCODE_SHIFT 0
103 #define RCODE(packet) (*buffer_at((packet), 3) & RCODE_MASK)
104 #define RCODE_SET(packet, rcode) \
105  (*buffer_at((packet), 3) = (*buffer_at((packet), 3) & ~RCODE_MASK) | (rcode))
106 
108 
112 typedef struct buffer_struct buffer_type;
114  size_t position;
115  size_t limit;
116  size_t capacity;
117  uint8_t* data;
118  unsigned fixed : 1;
119 };
120 
128 buffer_type* buffer_create(allocator_type* allocator, size_t capacity);
129 
139 void buffer_create_from(buffer_type* buffer, void* data, size_t size);
140 
147 void buffer_clear(buffer_type* buffer);
148 
157 void buffer_flip(buffer_type* buffer);
158 
165 void buffer_rewind(buffer_type* buffer);
166 
173 size_t buffer_position(buffer_type* buffer);
174 
182 void buffer_set_position(buffer_type* buffer, size_t pos);
183 
192 void buffer_skip(buffer_type* buffer, ssize_t count);
193 
201 int buffer_skip_dname(buffer_type* buffer);
202 
211 int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
212 
219 size_t buffer_limit(buffer_type* buffer);
220 
228 void buffer_set_limit(buffer_type* buffer, size_t limit);
229 
236 size_t buffer_capacity(buffer_type* buffer);
237 
245 uint8_t* buffer_at(buffer_type* buffer, size_t at);
246 
253 uint8_t* buffer_begin(buffer_type* buffer);
254 
261 uint8_t* buffer_end(buffer_type* buffer);
262 
269 uint8_t* buffer_current(buffer_type* buffer);
270 
277 size_t buffer_remaining(buffer_type* buffer);
278 
287 int buffer_available(buffer_type* buffer, size_t count);
288 
296 void buffer_write(buffer_type* buffer, const void* data, size_t count);
297 
304 void buffer_write_u8(buffer_type* buffer, uint8_t data);
305 
312 void buffer_write_u16(buffer_type* buffer, uint16_t data);
313 
321 void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
322 
329 void buffer_write_u32(buffer_type* buffer, uint32_t data);
330 
337 void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
338 
346 int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
347 
355 void buffer_read(buffer_type* buffer, void* data, size_t count);
356 
363 uint8_t buffer_read_u8(buffer_type* buffer);
364 
371 uint16_t buffer_read_u16(buffer_type* buffer);
372 
379 uint32_t buffer_read_u32(buffer_type* buffer);
380 
389 size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
390  unsigned allow_pointers);
391 
398 uint16_t buffer_pkt_id(buffer_type* buffer);
399 
406 
413 uint16_t buffer_pkt_flags(buffer_type* buffer);
414 
421 void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
422 
430 int buffer_pkt_qr(buffer_type* buffer);
431 
437 void buffer_pkt_set_qr(buffer_type* buffer);
438 
444 void buffer_pkt_clear_qr(buffer_type* buffer);
445 
453 int buffer_pkt_aa(buffer_type* buffer);
454 
460 void buffer_pkt_set_aa(buffer_type* buffer);
461 
469 int buffer_pkt_tc(buffer_type* buffer);
470 
478 int buffer_pkt_rd(buffer_type* buffer);
479 
487 int buffer_pkt_ra(buffer_type* buffer);
488 
496 int buffer_pkt_ad(buffer_type* buffer);
497 
505 int buffer_pkt_cd(buffer_type* buffer);
506 
513 ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
514 
521 void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
522 
529 ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
530 
537 void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
538 
545 const char* buffer_rcode2str(ldns_pkt_rcode rcode);
546 
553 uint16_t buffer_pkt_qdcount(buffer_type* buffer);
554 
561 void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
562 
569 uint16_t buffer_pkt_ancount(buffer_type* buffer);
570 
577 void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
578 
585 uint16_t buffer_pkt_nscount(buffer_type* buffer);
586 
593 void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
594 
601 uint16_t buffer_pkt_arcount(buffer_type* buffer);
602 
609 void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
610 
619 void
620 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
621  ldns_rr_class qclass);
622 
630 void
631 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
632 
640 void
641 buffer_pkt_axfr(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
642 
649 void buffer_pkt_print(FILE* fd, buffer_type* buffer);
650 
657 void buffer_cleanup(buffer_type* buffer, allocator_type* allocator);
658 
661 /*
662  * Copy data allowing for unaligned accesses in network byte order
663  * (big endian).
664  */
665 
666 static inline uint16_t
667 read_uint16(const void *src)
668 {
669 #ifdef ALLOW_UNALIGNED_ACCESSES
670  return ntohs(* (uint16_t *) src);
671 #else
672  uint8_t *p = (uint8_t *) src;
673  return (p[0] << 8) | p[1];
674 #endif
675 }
676 
677 static inline uint32_t
678 read_uint32(const void *src)
679 {
680 #ifdef ALLOW_UNALIGNED_ACCESSES
681  return ntohl(* (uint32_t *) src);
682 #else
683  uint8_t *p = (uint8_t *) src;
684  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
685 #endif
686 }
687 
688 static inline void
689 write_uint16(void *dst, uint16_t data)
690 {
691 #ifdef ALLOW_UNALIGNED_ACCESSES
692  * (uint16_t *) dst = htons(data);
693 #else
694  uint8_t *p = (uint8_t *) dst;
695  p[0] = (uint8_t) ((data >> 8) & 0xff);
696  p[1] = (uint8_t) (data & 0xff);
697 #endif
698 }
699 
700 static inline void
701 write_uint32(void *dst, uint32_t data)
702 {
703 #ifdef ALLOW_UNALIGNED_ACCESSES
704  * (uint32_t *) dst = htonl(data);
705 #else
706  uint8_t *p = (uint8_t *) dst;
707  p[0] = (uint8_t) ((data >> 24) & 0xff);
708  p[1] = (uint8_t) ((data >> 16) & 0xff);
709  p[2] = (uint8_t) ((data >> 8) & 0xff);
710  p[3] = (uint8_t) (data & 0xff);
711 #endif
712 }
713 
714 #endif /* WIRE_BUFFER_H */
uint8_t * buffer_end(buffer_type *buffer)
Definition: buffer.c:479
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:454
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:1010
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:350
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1138
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1088
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:1022
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:974
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:1063
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:491
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:753
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition: buffer.c:1047
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:937
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1113
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:768
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
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:998
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:516
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_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:651
void buffer_pkt_print(FILE *fd, buffer_type *buffer)
Definition: buffer.c:1235
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1125
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:912
size_t capacity
Definition: buffer.h:116
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:861
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:162
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:174
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:609
void buffer_pkt_axfr(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1221
unsigned fixed
Definition: buffer.h:118
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:425
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:623
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:962
ods_lookup_table ods_rcode_str[]
Definition: buffer.c:59
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:986
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:637
size_t position
Definition: buffer.h:114
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:442
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1208
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_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:1034
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:286
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:949
uint8_t * data
Definition: buffer.h:117
size_t limit
Definition: buffer.h:115
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition: buffer.c:899
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:874
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1150
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:666
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:566
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:467
void buffer_rewind(buffer_type *buffer)
Definition: buffer.c:149
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1100
void buffer_create_from(buffer_type *buffer, void *data, size_t size)
Definition: buffer.c:104
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:886
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1263
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:849
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:540
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:188
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:834
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:783
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:798
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1075
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:80
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:924
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:121
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:413