OpenDNSSEC-signer  1.4.5
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 "shared/allocator.h"
37 #include "shared/log.h"
38 #include "shared/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 
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 
127 buffer_type* buffer_create(allocator_type* allocator, size_t capacity);
128 
138 void buffer_create_from(buffer_type* buffer, void* data, size_t size);
139 
146 void buffer_clear(buffer_type* buffer);
147 
156 void buffer_flip(buffer_type* buffer);
157 
164 void buffer_rewind(buffer_type* buffer);
165 
172 size_t buffer_position(buffer_type* buffer);
173 
181 void buffer_set_position(buffer_type* buffer, size_t pos);
182 
191 void buffer_skip(buffer_type* buffer, ssize_t count);
192 
200 int buffer_skip_dname(buffer_type* buffer);
201 
210 int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
211 
218 size_t buffer_limit(buffer_type* buffer);
219 
227 void buffer_set_limit(buffer_type* buffer, size_t limit);
228 
235 size_t buffer_capacity(buffer_type* buffer);
236 
244 uint8_t* buffer_at(buffer_type* buffer, size_t at);
245 
252 uint8_t* buffer_begin(buffer_type* buffer);
253 
260 uint8_t* buffer_end(buffer_type* buffer);
261 
268 uint8_t* buffer_current(buffer_type* buffer);
269 
276 size_t buffer_remaining(buffer_type* buffer);
277 
286 int buffer_available(buffer_type* buffer, size_t count);
287 
295 void buffer_write(buffer_type* buffer, const void* data, size_t count);
296 
303 void buffer_write_u8(buffer_type* buffer, uint8_t data);
304 
311 void buffer_write_u16(buffer_type* buffer, uint16_t data);
312 
320 void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
321 
328 void buffer_write_u32(buffer_type* buffer, uint32_t data);
329 
336 void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
337 
345 int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
346 
354 void buffer_read(buffer_type* buffer, void* data, size_t count);
355 
362 uint8_t buffer_read_u8(buffer_type* buffer);
363 
370 uint16_t buffer_read_u16(buffer_type* buffer);
371 
378 uint32_t buffer_read_u32(buffer_type* buffer);
379 
388 size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
389  unsigned allow_pointers);
390 
397 uint16_t buffer_pkt_id(buffer_type* buffer);
398 
405 
412 uint16_t buffer_pkt_flags(buffer_type* buffer);
413 
420 void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
421 
429 int buffer_pkt_qr(buffer_type* buffer);
430 
436 void buffer_pkt_set_qr(buffer_type* buffer);
437 
443 void buffer_pkt_clear_qr(buffer_type* buffer);
444 
452 int buffer_pkt_aa(buffer_type* buffer);
453 
459 void buffer_pkt_set_aa(buffer_type* buffer);
460 
468 int buffer_pkt_tc(buffer_type* buffer);
469 
477 int buffer_pkt_rd(buffer_type* buffer);
478 
486 int buffer_pkt_ra(buffer_type* buffer);
487 
495 int buffer_pkt_ad(buffer_type* buffer);
496 
504 int buffer_pkt_cd(buffer_type* buffer);
505 
512 ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
513 
520 void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
521 
528 ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
529 
536 void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
537 
544 const char* buffer_rcode2str(ldns_pkt_rcode rcode);
545 
552 uint16_t buffer_pkt_qdcount(buffer_type* buffer);
553 
560 void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
561 
568 uint16_t buffer_pkt_ancount(buffer_type* buffer);
569 
576 void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
577 
584 uint16_t buffer_pkt_nscount(buffer_type* buffer);
585 
592 void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
593 
600 uint16_t buffer_pkt_arcount(buffer_type* buffer);
601 
608 void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
609 
618 void
619 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
620  ldns_rr_class qclass);
621 
629 void
630 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
631 
639 void
640 buffer_pkt_axfr(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
641 
648 void buffer_pkt_print(FILE* fd, buffer_type* buffer);
649 
656 void buffer_cleanup(buffer_type* buffer, allocator_type* allocator);
657 
660 /*
661  * Copy data allowing for unaligned accesses in network byte order
662  * (big endian).
663  */
664 
665 static inline uint16_t
666 read_uint16(const void *src)
667 {
668 #ifdef ALLOW_UNALIGNED_ACCESSES
669  return ntohs(* (uint16_t *) src);
670 #else
671  uint8_t *p = (uint8_t *) src;
672  return (p[0] << 8) | p[1];
673 #endif
674 }
675 
676 static inline uint32_t
677 read_uint32(const void *src)
678 {
679 #ifdef ALLOW_UNALIGNED_ACCESSES
680  return ntohl(* (uint32_t *) src);
681 #else
682  uint8_t *p = (uint8_t *) src;
683  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
684 #endif
685 }
686 
687 static inline void
688 write_uint16(void *dst, uint16_t data)
689 {
690 #ifdef ALLOW_UNALIGNED_ACCESSES
691  * (uint16_t *) dst = htons(data);
692 #else
693  uint8_t *p = (uint8_t *) dst;
694  p[0] = (uint8_t) ((data >> 8) & 0xff);
695  p[1] = (uint8_t) (data & 0xff);
696 #endif
697 }
698 
699 static inline void
700 write_uint32(void *dst, uint32_t data)
701 {
702 #ifdef ALLOW_UNALIGNED_ACCESSES
703  * (uint32_t *) dst = htonl(data);
704 #else
705  uint8_t *p = (uint8_t *) dst;
706  p[0] = (uint8_t) ((data >> 24) & 0xff);
707  p[1] = (uint8_t) ((data >> 16) & 0xff);
708  p[2] = (uint8_t) ((data >> 8) & 0xff);
709  p[3] = (uint8_t) (data & 0xff);
710 #endif
711 }
712 
713 #endif /* WIRE_BUFFER_H */
uint8_t * buffer_end(buffer_type *buffer)
Definition: buffer.c:477
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:452
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:1008
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:348
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1136
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1086
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:1020
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:972
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:1061
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:489
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:751
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition: buffer.c:1045
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:935
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1111
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:766
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:592
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:811
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:996
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:514
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1192
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:649
void buffer_pkt_print(FILE *fd, buffer_type *buffer)
Definition: buffer.c:1233
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1123
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:910
size_t capacity
Definition: buffer.h:115
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:859
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:160
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:172
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:607
void buffer_pkt_axfr(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1219
unsigned fixed
Definition: buffer.h:117
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:423
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:621
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:960
ods_lookup_table ods_rcode_str[]
Definition: buffer.c:57
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:984
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:635
size_t position
Definition: buffer.h:113
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:440
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1206
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:380
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:133
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:1032
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:284
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:947
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:897
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:872
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1148
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:664
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:564
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:465
void buffer_rewind(buffer_type *buffer)
Definition: buffer.c:147
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1098
void buffer_create_from(buffer_type *buffer, void *data, size_t size)
Definition: buffer.c:102
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:884
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1261
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:847
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:538
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:186
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:832
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:781
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:796
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1073
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:78
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:922
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:119
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:411