OpenDNSSEC-signer  2.0.2
buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #ifndef WIRE_BUFFER_H
33 #define WIRE_BUFFER_H
34 
35 #include "config.h"
36 #include "status.h"
37 #include "log.h"
38 #include "status.h"
39 
40 #include <ldns/ldns.h>
41 #include <stdint.h>
42 
43 #define BUFFER_PKT_HEADER_SIZE 12
44 #define MAXDOMAINLEN 255
45 #define MAXLABELLEN 63
46 #define MAX_RDLENGTH 65535
47 #define MAX_RR_SIZE \
48  (MAXDOMAINLEN + sizeof(uint32_t) + 4*sizeof(uint16_t) + MAX_RDLENGTH)
49 #define MAX_PACKET_SIZE 65535
50 #define PACKET_BUFFER_SIZE (MAX_PACKET_SIZE + MAX_RR_SIZE)
51 
52 #define QR_MASK 0x80U
53 #define QR_SHIFT 7
54 #define QR(packet) (*buffer_at((packet), 2) & QR_MASK)
55 #define QR_SET(packet) (*buffer_at((packet), 2) |= QR_MASK)
56 #define QR_CLR(packet) (*buffer_at((packet), 2) &= ~QR_MASK)
57 
58 #define OPCODE_MASK 0x78U
59 #define OPCODE_SHIFT 3
60 #define OPCODE(packet) ((*buffer_at((packet), 2) & OPCODE_MASK) >> OPCODE_SHIFT)
61 #define OPCODE_SET(packet, opcode) \
62  (*buffer_at((packet), 2) = (*buffer_at((packet), 2) & ~OPCODE_MASK) | ((opcode) << OPCODE_SHIFT))
63 
64 #define AA_MASK 0x04U
65 #define AA_SHIFT 2
66 #define AA(packet) (*buffer_at((packet), 2) & AA_MASK)
67 #define AA_SET(packet) (*buffer_at((packet), 2) |= AA_MASK)
68 #define AA_CLR(packet) (*buffer_at((packet), 2) &= ~AA_MASK)
69 
70 #define TC_MASK 0x02U
71 #define TC_SHIFT 1
72 #define TC(packet) (*buffer_at((packet), 2) & TC_MASK)
73 #define TC_SET(packet) (*buffer_at((packet), 2) |= TC_MASK)
74 #define TC_CLR(packet) (*buffer_at((packet), 2) &= ~TC_MASK)
75 
76 #define RD_MASK 0x01U
77 #define RD_SHIFT 0
78 #define RD(packet) (*buffer_at((packet), 2) & RD_MASK)
79 #define RD_SET(packet) (*buffer_at((packet), 2) |= RD_MASK)
80 #define RD_CLR(packet) (*buffer_at((packet), 2) &= ~RD_MASK)
81 
82 #define RA_MASK 0x80U
83 #define RA_SHIFT 7
84 #define RA(packet) (*buffer_at((packet), 3) & RA_MASK)
85 #define RA_SET(packet) (*buffer_at((packet), 3) |= RA_MASK)
86 #define RA_CLR(packet) (*buffer_at((packet), 3) &= ~RA_MASK)
87 
88 #define AD_MASK 0x20U
89 #define AD_SHIFT 5
90 #define AD(packet) (*buffer_at((packet), 3) & AD_MASK)
91 #define AD_SET(packet) (*buffer_at((packet), 3) |= AD_MASK)
92 #define AD_CLR(packet) (*buffer_at((packet), 3) &= ~AD_MASK)
93 
94 #define CD_MASK 0x10U
95 #define CD_SHIFT 4
96 #define CD(packet) (*buffer_at((packet), 3) & CD_MASK)
97 #define CD_SET(packet) (*buffer_at((packet), 3) |= CD_MASK)
98 #define CD_CLR(packet) (*buffer_at((packet), 3) &= ~CD_MASK)
99 
100 #define RCODE_MASK 0x0fU
101 #define RCODE_SHIFT 0
102 #define RCODE(packet) (*buffer_at((packet), 3) & RCODE_MASK)
103 #define RCODE_SET(packet, rcode) \
104  (*buffer_at((packet), 3) = (*buffer_at((packet), 3) & ~RCODE_MASK) | (rcode))
105 
106 extern ods_lookup_table ods_rcode_str[];
107 
111 typedef struct buffer_struct buffer_type;
113  size_t position;
114  size_t limit;
115  size_t capacity;
116  uint8_t* data;
117  unsigned fixed : 1;
118 };
119 
128 
135 void buffer_clear(buffer_type* buffer);
136 
145 void buffer_flip(buffer_type* buffer);
146 
153 size_t buffer_position(buffer_type* buffer);
154 
162 void buffer_set_position(buffer_type* buffer, size_t pos);
163 
172 void buffer_skip(buffer_type* buffer, ssize_t count);
173 
181 int buffer_skip_dname(buffer_type* buffer);
182 
191 int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
192 
199 size_t buffer_limit(buffer_type* buffer);
200 
208 void buffer_set_limit(buffer_type* buffer, size_t limit);
209 
216 size_t buffer_capacity(buffer_type* buffer);
217 
225 uint8_t* buffer_at(buffer_type* buffer, size_t at);
226 
233 uint8_t* buffer_begin(buffer_type* buffer);
234 
241 uint8_t* buffer_current(buffer_type* buffer);
242 
249 size_t buffer_remaining(buffer_type* buffer);
250 
259 int buffer_available(buffer_type* buffer, size_t count);
260 
268 void buffer_write(buffer_type* buffer, const void* data, size_t count);
269 
276 void buffer_write_u8(buffer_type* buffer, uint8_t data);
277 
284 void buffer_write_u16(buffer_type* buffer, uint16_t data);
285 
293 void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
294 
301 void buffer_write_u32(buffer_type* buffer, uint32_t data);
302 
309 void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
310 
318 int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
319 
327 void buffer_read(buffer_type* buffer, void* data, size_t count);
328 
335 uint8_t buffer_read_u8(buffer_type* buffer);
336 
343 uint16_t buffer_read_u16(buffer_type* buffer);
344 
351 uint32_t buffer_read_u32(buffer_type* buffer);
352 
361 size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
362  unsigned allow_pointers);
363 
370 uint16_t buffer_pkt_id(buffer_type* buffer);
371 
378 
385 uint16_t buffer_pkt_flags(buffer_type* buffer);
386 
393 void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
394 
402 int buffer_pkt_qr(buffer_type* buffer);
403 
409 void buffer_pkt_set_qr(buffer_type* buffer);
410 
416 void buffer_pkt_clear_qr(buffer_type* buffer);
417 
425 int buffer_pkt_aa(buffer_type* buffer);
426 
432 void buffer_pkt_set_aa(buffer_type* buffer);
433 
441 int buffer_pkt_tc(buffer_type* buffer);
442 
450 int buffer_pkt_rd(buffer_type* buffer);
451 
459 int buffer_pkt_ra(buffer_type* buffer);
460 
468 int buffer_pkt_ad(buffer_type* buffer);
469 
477 int buffer_pkt_cd(buffer_type* buffer);
478 
485 ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
486 
493 void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
494 
501 ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
502 
509 void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
510 
517 const char* buffer_rcode2str(ldns_pkt_rcode rcode);
518 
525 uint16_t buffer_pkt_qdcount(buffer_type* buffer);
526 
533 void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
534 
541 uint16_t buffer_pkt_ancount(buffer_type* buffer);
542 
549 void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
550 
557 uint16_t buffer_pkt_nscount(buffer_type* buffer);
558 
565 void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
566 
573 uint16_t buffer_pkt_arcount(buffer_type* buffer);
574 
581 void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
582 
591 void
592 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
593  ldns_rr_class qclass);
594 
602 void
603 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
604 
611 void buffer_cleanup(buffer_type* buffer);
612 
615 /*
616  * Copy data allowing for unaligned accesses in network byte order
617  * (big endian).
618  */
619 
620 static inline uint16_t
621 read_uint16(const void *src)
622 {
623 #ifdef ALLOW_UNALIGNED_ACCESSES
624  return ntohs(* (uint16_t *) src);
625 #else
626  uint8_t *p = (uint8_t *) src;
627  return (p[0] << 8) | p[1];
628 #endif
629 }
630 
631 static inline uint32_t
632 read_uint32(const void *src)
633 {
634 #ifdef ALLOW_UNALIGNED_ACCESSES
635  return ntohl(* (uint32_t *) src);
636 #else
637  uint8_t *p = (uint8_t *) src;
638  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
639 #endif
640 }
641 
642 static inline void
643 write_uint16(void *dst, uint16_t data)
644 {
645 #ifdef ALLOW_UNALIGNED_ACCESSES
646  * (uint16_t *) dst = htons(data);
647 #else
648  uint8_t *p = (uint8_t *) dst;
649  p[0] = (uint8_t) ((data >> 8) & 0xff);
650  p[1] = (uint8_t) (data & 0xff);
651 #endif
652 }
653 
654 static inline void
655 write_uint32(void *dst, uint32_t data)
656 {
657 #ifdef ALLOW_UNALIGNED_ACCESSES
658  * (uint32_t *) dst = htonl(data);
659 #else
660  uint8_t *p = (uint8_t *) dst;
661  p[0] = (uint8_t) ((data >> 24) & 0xff);
662  p[1] = (uint8_t) ((data >> 16) & 0xff);
663  p[2] = (uint8_t) ((data >> 8) & 0xff);
664  p[3] = (uint8_t) (data & 0xff);
665 #endif
666 }
667 
668 #endif /* WIRE_BUFFER_H */
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:413
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:942
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:310
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1066
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1018
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:954
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:906
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:994
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:438
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:692
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition: buffer.c:978
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:870
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1042
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:706
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:538
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:751
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:930
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:463
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1120
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:591
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1054
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:846
size_t capacity
Definition: buffer.h:115
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:798
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:125
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:137
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:552
unsigned fixed
Definition: buffer.h:117
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:385
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:565
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:894
ods_lookup_table ods_rcode_str[]
Definition: buffer.c:57
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:918
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:578
size_t position
Definition: buffer.h:113
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:401
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1133
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:342
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:112
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:966
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:246
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:882
uint8_t * data
Definition: buffer.h:116
size_t limit
Definition: buffer.h:114
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition: buffer.c:834
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:810
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1078
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:605
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:512
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:426
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1030
void buffer_cleanup(buffer_type *buffer)
Definition: buffer.c:1145
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:822
buffer_type * buffer_create(size_t capacity)
Definition: buffer.c:78
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:786
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:487
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:150
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:772
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:721
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:736
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1006
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:858
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:99
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:373