OpenDNSSEC-signer  1.4.1
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 
41 #include <ldns/ldns.h>
42 #include <stdint.h>
43 
44 #define BUFFER_PKT_HEADER_SIZE 12
45 #define MAXDOMAINLEN 255
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 
109 typedef struct buffer_struct buffer_type;
111  size_t position;
112  size_t limit;
113  size_t capacity;
114  uint8_t* data;
115  unsigned fixed : 1;
116 };
117 
125 buffer_type* buffer_create(allocator_type* allocator, size_t capacity);
126 
136 void buffer_create_from(buffer_type* buffer, void* data, size_t size);
137 
144 void buffer_clear(buffer_type* buffer);
145 
154 void buffer_flip(buffer_type* buffer);
155 
162 void buffer_rewind(buffer_type* buffer);
163 
170 size_t buffer_position(buffer_type* buffer);
171 
179 void buffer_set_position(buffer_type* buffer, size_t pos);
180 
189 void buffer_skip(buffer_type* buffer, ssize_t count);
190 
198 int buffer_skip_dname(buffer_type* buffer);
199 
208 int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
209 
216 size_t buffer_limit(buffer_type* buffer);
217 
225 void buffer_set_limit(buffer_type* buffer, size_t limit);
226 
233 size_t buffer_capacity(buffer_type* buffer);
234 
242 uint8_t* buffer_at(buffer_type* buffer, size_t at);
243 
250 uint8_t* buffer_begin(buffer_type* buffer);
251 
258 uint8_t* buffer_end(buffer_type* buffer);
259 
266 uint8_t* buffer_current(buffer_type* buffer);
267 
274 size_t buffer_remaining(buffer_type* buffer);
275 
284 int buffer_available(buffer_type* buffer, size_t count);
285 
293 void buffer_write(buffer_type* buffer, const void* data, size_t count);
294 
301 void buffer_write_u8(buffer_type* buffer, uint8_t data);
302 
309 void buffer_write_u16(buffer_type* buffer, uint16_t data);
310 
318 void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
319 
326 void buffer_write_u32(buffer_type* buffer, uint32_t data);
327 
334 void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
335 
343 int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
344 
352 void buffer_read(buffer_type* buffer, void* data, size_t count);
353 
360 uint8_t buffer_read_u8(buffer_type* buffer);
361 
368 uint16_t buffer_read_u16(buffer_type* buffer);
369 
376 uint32_t buffer_read_u32(buffer_type* buffer);
377 
386 size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
387  unsigned allow_pointers);
388 
395 uint16_t buffer_pkt_id(buffer_type* buffer);
396 
403 
410 uint16_t buffer_pkt_flags(buffer_type* buffer);
411 
418 void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
419 
427 int buffer_pkt_qr(buffer_type* buffer);
428 
434 void buffer_pkt_set_qr(buffer_type* buffer);
435 
441 void buffer_pkt_clear_qr(buffer_type* buffer);
442 
450 int buffer_pkt_aa(buffer_type* buffer);
451 
457 void buffer_pkt_set_aa(buffer_type* buffer);
458 
466 int buffer_pkt_tc(buffer_type* buffer);
467 
475 int buffer_pkt_rd(buffer_type* buffer);
476 
484 int buffer_pkt_ra(buffer_type* buffer);
485 
493 int buffer_pkt_ad(buffer_type* buffer);
494 
502 int buffer_pkt_cd(buffer_type* buffer);
503 
510 ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
511 
518 void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
519 
526 ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
527 
534 void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
535 
542 uint16_t buffer_pkt_qdcount(buffer_type* buffer);
543 
550 void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
551 
558 uint16_t buffer_pkt_ancount(buffer_type* buffer);
559 
566 void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
567 
574 uint16_t buffer_pkt_nscount(buffer_type* buffer);
575 
582 void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
583 
590 uint16_t buffer_pkt_arcount(buffer_type* buffer);
591 
598 void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
599 
608 void
609 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
610  ldns_rr_class qclass);
611 
619 void
620 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
621 
629 void
630 buffer_pkt_axfr(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
631 
638 void buffer_pkt_print(FILE* fd, buffer_type* buffer);
639 
646 void buffer_cleanup(buffer_type* buffer, allocator_type* allocator);
647 
650 /*
651  * Copy data allowing for unaligned accesses in network byte order
652  * (big endian).
653  */
654 
655 static inline uint16_t
656 read_uint16(const void *src)
657 {
658 #ifdef ALLOW_UNALIGNED_ACCESSES
659  return ntohs(* (uint16_t *) src);
660 #else
661  uint8_t *p = (uint8_t *) src;
662  return (p[0] << 8) | p[1];
663 #endif
664 }
665 
666 static inline uint32_t
667 read_uint32(const void *src)
668 {
669 #ifdef ALLOW_UNALIGNED_ACCESSES
670  return ntohl(* (uint32_t *) src);
671 #else
672  uint8_t *p = (uint8_t *) src;
673  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
674 #endif
675 }
676 
677 static inline void
678 write_uint16(void *dst, uint16_t data)
679 {
680 #ifdef ALLOW_UNALIGNED_ACCESSES
681  * (uint16_t *) dst = htons(data);
682 #else
683  uint8_t *p = (uint8_t *) dst;
684  p[0] = (uint8_t) ((data >> 8) & 0xff);
685  p[1] = (uint8_t) (data & 0xff);
686 #endif
687 }
688 
689 static inline void
690 write_uint32(void *dst, uint32_t data)
691 {
692 #ifdef ALLOW_UNALIGNED_ACCESSES
693  * (uint32_t *) dst = htonl(data);
694 #else
695  uint8_t *p = (uint8_t *) dst;
696  p[0] = (uint8_t) ((data >> 24) & 0xff);
697  p[1] = (uint8_t) ((data >> 16) & 0xff);
698  p[2] = (uint8_t) ((data >> 8) & 0xff);
699  p[3] = (uint8_t) (data & 0xff);
700 #endif
701 }
702 
703 #endif /* WIRE_BUFFER_H */
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:896
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:958
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:818
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:438
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1106
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:1031
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:172
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:845
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:767
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:366
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:119
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1043
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:105
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:752
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:593
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:858
void buffer_pkt_print(FILE *fd, buffer_type *buffer)
Definition: buffer.c:1203
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:578
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:797
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:475
buffer_type * buffer_create(allocator_type *allocator, size_t capacity)
Definition: buffer.c:64
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1056
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:397
void buffer_rewind(buffer_type *buffer)
Definition: buffer.c:133
size_t capacity
Definition: buffer.h:113
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1068
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1093
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:409
unsigned fixed
Definition: buffer.h:115
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:833
void buffer_pkt_axfr(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1189
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:426
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:334
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:782
size_t position
Definition: buffer.h:111
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:908
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1162
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:607
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:270
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:621
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:933
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:737
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:921
uint8_t * data
Definition: buffer.h:114
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1081
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:550
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:158
size_t limit
Definition: buffer.h:112
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1176
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:524
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:982
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition: buffer.c:883
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:500
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:1018
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:635
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:650
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:970
void buffer_create_from(buffer_type *buffer, void *data, size_t size)
Definition: buffer.c:88
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:146
void buffer_cleanup(buffer_type *buffer, allocator_type *allocator)
Definition: buffer.c:1231
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1118
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:994
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:870
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:451
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:1006
uint8_t * buffer_end(buffer_type *buffer)
Definition: buffer.c:463
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:946