OpenDNSSEC-signer 2.1.10
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
106extern ods_lookup_table ods_rcode_str[];
107
113 size_t position;
114 size_t limit;
115 size_t capacity;
116 uint8_t* data;
117 unsigned fixed : 1;
118};
119
127extern buffer_type* buffer_create(size_t capacity);
128
135extern void buffer_clear(buffer_type* buffer);
136
145void buffer_flip(buffer_type* buffer);
146
153extern size_t buffer_position(buffer_type* buffer);
154
162extern void buffer_set_position(buffer_type* buffer, size_t pos);
163
172extern void buffer_skip(buffer_type* buffer, ssize_t count);
173
181extern int buffer_skip_dname(buffer_type* buffer);
182
191extern int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
192
199extern size_t buffer_limit(buffer_type* buffer);
200
208extern void buffer_set_limit(buffer_type* buffer, size_t limit);
209
216extern size_t buffer_capacity(buffer_type* buffer);
217
225extern uint8_t* buffer_at(buffer_type* buffer, size_t at);
226
233extern uint8_t* buffer_begin(buffer_type* buffer);
234
241extern uint8_t* buffer_current(buffer_type* buffer);
242
249extern size_t buffer_remaining(buffer_type* buffer);
250
259extern int buffer_available(buffer_type* buffer, size_t count);
260
268extern void buffer_write(buffer_type* buffer, const void* data, size_t count);
269
276extern void buffer_write_u8(buffer_type* buffer, uint8_t data);
277
284extern void buffer_write_u16(buffer_type* buffer, uint16_t data);
285
293extern void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
294
301extern void buffer_write_u32(buffer_type* buffer, uint32_t data);
302
309extern void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
310
318extern int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
319
327extern void buffer_read(buffer_type* buffer, void* data, size_t count);
328
335extern uint8_t buffer_read_u8(buffer_type* buffer);
336
343extern uint16_t buffer_read_u16(buffer_type* buffer);
344
351extern uint32_t buffer_read_u32(buffer_type* buffer);
352
361extern size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
362 unsigned allow_pointers);
363
370extern uint16_t buffer_pkt_id(buffer_type* buffer);
371
377extern void buffer_pkt_set_random_id(buffer_type* buffer);
378
385extern uint16_t buffer_pkt_flags(buffer_type* buffer);
386
393extern void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
394
402extern int buffer_pkt_qr(buffer_type* buffer);
403
409extern void buffer_pkt_set_qr(buffer_type* buffer);
410
416extern void buffer_pkt_clear_qr(buffer_type* buffer);
417
425extern int buffer_pkt_aa(buffer_type* buffer);
426
432extern void buffer_pkt_set_aa(buffer_type* buffer);
433
441extern int buffer_pkt_tc(buffer_type* buffer);
442
450extern int buffer_pkt_rd(buffer_type* buffer);
451
459extern int buffer_pkt_ra(buffer_type* buffer);
460
468extern int buffer_pkt_ad(buffer_type* buffer);
469
477extern int buffer_pkt_cd(buffer_type* buffer);
478
485extern ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
486
493extern void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
494
501extern ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
502
509extern void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
510
517extern const char* buffer_rcode2str(ldns_pkt_rcode rcode);
518
525extern uint16_t buffer_pkt_qdcount(buffer_type* buffer);
526
533extern void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
534
541extern uint16_t buffer_pkt_ancount(buffer_type* buffer);
542
549extern void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
550
557extern uint16_t buffer_pkt_nscount(buffer_type* buffer);
558
565extern void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
566
573extern uint16_t buffer_pkt_arcount(buffer_type* buffer);
574
581extern void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
582
591extern void
592buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
593 ldns_rr_class qclass);
594
602extern void
603buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
604
611extern void buffer_cleanup(buffer_type* buffer);
612
615/*
616 * Copy data allowing for unaligned accesses in network byte order
617 * (big endian).
618 */
619
620static inline uint16_t
621read_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
631static inline uint32_t
632read_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
642static inline void
643write_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
654static inline void
655write_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 */
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition: buffer.c:798
int buffer_available(buffer_type *buffer, size_t count)
Definition: buffer.c:487
ods_lookup_table ods_rcode_str[]
Definition: buffer.c:57
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition: buffer.c:858
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition: buffer.c:966
size_t buffer_capacity(buffer_type *buffer)
Definition: buffer.c:401
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition: buffer.c:994
void buffer_clear(buffer_type *buffer)
Definition: buffer.c:99
int buffer_pkt_ad(buffer_type *buffer)
Definition: buffer.c:930
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition: buffer.c:834
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition: buffer.c:342
void buffer_cleanup(buffer_type *buffer)
Definition: buffer.c:1145
uint32_t buffer_read_u32(buffer_type *buffer)
Definition: buffer.c:736
uint16_t buffer_read_u16(buffer_type *buffer)
Definition: buffer.c:721
uint8_t * buffer_current(buffer_type *buffer)
Definition: buffer.c:438
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition: buffer.c:385
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition: buffer.c:552
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition: buffer.c:137
int buffer_pkt_cd(buffer_type *buffer)
Definition: buffer.c:942
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition: buffer.c:978
int buffer_pkt_qr(buffer_type *buffer)
Definition: buffer.c:810
uint8_t * buffer_begin(buffer_type *buffer)
Definition: buffer.c:426
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition: buffer.c:846
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition: buffer.c:954
void buffer_flip(buffer_type *buffer)
Definition: buffer.c:112
int buffer_pkt_aa(buffer_type *buffer)
Definition: buffer.c:870
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition: buffer.c:413
size_t buffer_position(buffer_type *buffer)
Definition: buffer.c:125
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition: buffer.c:1133
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition: buffer.c:1066
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1054
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition: buffer.c:538
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition: buffer.c:591
void buffer_pkt_set_qr(buffer_type *buffer)
Definition: buffer.c:822
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition: buffer.c:692
int buffer_pkt_rd(buffer_type *buffer)
Definition: buffer.c:906
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition: buffer.c:512
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition: buffer.c:150
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition: buffer.c:786
int buffer_pkt_tc(buffer_type *buffer)
Definition: buffer.c:894
int buffer_skip_dname(buffer_type *buffer)
Definition: buffer.c:310
int buffer_pkt_ra(buffer_type *buffer)
Definition: buffer.c:918
size_t buffer_remaining(buffer_type *buffer)
Definition: buffer.c:463
uint8_t buffer_read_u8(buffer_type *buffer)
Definition: buffer.c:706
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition: buffer.c:578
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition: buffer.c:605
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition: buffer.c:1120
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition: buffer.c:772
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1030
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition: buffer.c:565
buffer_type * buffer_create(size_t capacity)
Definition: buffer.c:78
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition: buffer.c:246
size_t buffer_limit(buffer_type *buffer)
Definition: buffer.c:373
void buffer_pkt_set_aa(buffer_type *buffer)
Definition: buffer.c:882
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition: buffer.c:1018
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition: buffer.c:1042
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1078
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition: buffer.c:1006
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition: buffer.c:751
size_t capacity
Definition: buffer.h:115
size_t limit
Definition: buffer.h:114
size_t position
Definition: buffer.h:113
unsigned fixed
Definition: buffer.h:117
uint8_t * data
Definition: buffer.h:116