buffer.h
Go to the documentation of this file.
/*
* Copyright (c) 2011 NLNet Labs. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WIRE_BUFFER_H
#define WIRE_BUFFER_H
#include "config.h"
#include "status.h"
#include "log.h"
#include "status.h"
#include <ldns/ldns.h>
#include <stdint.h>
#define BUFFER_PKT_HEADER_SIZE 12
#define MAXDOMAINLEN 255
#define MAXLABELLEN 63
#define MAX_RDLENGTH 65535
#define MAX_RR_SIZE \
(MAXDOMAINLEN + sizeof(uint32_t) + 4*sizeof(uint16_t) + MAX_RDLENGTH)
#define MAX_PACKET_SIZE 65535
#define PACKET_BUFFER_SIZE (MAX_PACKET_SIZE + MAX_RR_SIZE)
#define QR_MASK 0x80U
#define QR_SHIFT 7
#define QR(packet) (*buffer_at((packet), 2) & QR_MASK)
#define QR_SET(packet) (*buffer_at((packet), 2) |= QR_MASK)
#define QR_CLR(packet) (*buffer_at((packet), 2) &= ~QR_MASK)
#define OPCODE_MASK 0x78U
#define OPCODE_SHIFT 3
#define OPCODE(packet) ((*buffer_at((packet), 2) & OPCODE_MASK) >> OPCODE_SHIFT)
#define OPCODE_SET(packet, opcode) \
(*buffer_at((packet), 2) = (*buffer_at((packet), 2) & ~OPCODE_MASK) | ((opcode) << OPCODE_SHIFT))
#define AA_MASK 0x04U
#define AA_SHIFT 2
#define AA(packet) (*buffer_at((packet), 2) & AA_MASK)
#define AA_SET(packet) (*buffer_at((packet), 2) |= AA_MASK)
#define AA_CLR(packet) (*buffer_at((packet), 2) &= ~AA_MASK)
#define TC_MASK 0x02U
#define TC_SHIFT 1
#define TC(packet) (*buffer_at((packet), 2) & TC_MASK)
#define TC_SET(packet) (*buffer_at((packet), 2) |= TC_MASK)
#define TC_CLR(packet) (*buffer_at((packet), 2) &= ~TC_MASK)
#define RD_MASK 0x01U
#define RD_SHIFT 0
#define RD(packet) (*buffer_at((packet), 2) & RD_MASK)
#define RD_SET(packet) (*buffer_at((packet), 2) |= RD_MASK)
#define RD_CLR(packet) (*buffer_at((packet), 2) &= ~RD_MASK)
#define RA_MASK 0x80U
#define RA_SHIFT 7
#define RA(packet) (*buffer_at((packet), 3) & RA_MASK)
#define RA_SET(packet) (*buffer_at((packet), 3) |= RA_MASK)
#define RA_CLR(packet) (*buffer_at((packet), 3) &= ~RA_MASK)
#define AD_MASK 0x20U
#define AD_SHIFT 5
#define AD(packet) (*buffer_at((packet), 3) & AD_MASK)
#define AD_SET(packet) (*buffer_at((packet), 3) |= AD_MASK)
#define AD_CLR(packet) (*buffer_at((packet), 3) &= ~AD_MASK)
#define CD_MASK 0x10U
#define CD_SHIFT 4
#define CD(packet) (*buffer_at((packet), 3) & CD_MASK)
#define CD_SET(packet) (*buffer_at((packet), 3) |= CD_MASK)
#define CD_CLR(packet) (*buffer_at((packet), 3) &= ~CD_MASK)
#define RCODE_MASK 0x0fU
#define RCODE_SHIFT 0
#define RCODE(packet) (*buffer_at((packet), 3) & RCODE_MASK)
#define RCODE_SET(packet, rcode) \
(*buffer_at((packet), 3) = (*buffer_at((packet), 3) & ~RCODE_MASK) | (rcode))
extern ods_lookup_table ods_rcode_str[];
typedef struct buffer_struct buffer_type;
struct buffer_struct {
size_t position;
size_t limit;
size_t capacity;
uint8_t* data;
unsigned fixed : 1;
};
extern buffer_type* buffer_create(size_t capacity);
extern void buffer_clear(buffer_type* buffer);
void buffer_flip(buffer_type* buffer);
extern size_t buffer_position(buffer_type* buffer);
extern void buffer_set_position(buffer_type* buffer, size_t pos);
extern void buffer_skip(buffer_type* buffer, ssize_t count);
extern int buffer_skip_dname(buffer_type* buffer);
extern int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
extern size_t buffer_limit(buffer_type* buffer);
extern void buffer_set_limit(buffer_type* buffer, size_t limit);
extern size_t buffer_capacity(buffer_type* buffer);
extern uint8_t* buffer_at(buffer_type* buffer, size_t at);
extern uint8_t* buffer_begin(buffer_type* buffer);
extern uint8_t* buffer_current(buffer_type* buffer);
extern size_t buffer_remaining(buffer_type* buffer);
extern int buffer_available(buffer_type* buffer, size_t count);
extern void buffer_write(buffer_type* buffer, const void* data, size_t count);
extern void buffer_write_u8(buffer_type* buffer, uint8_t data);
extern void buffer_write_u16(buffer_type* buffer, uint16_t data);
extern void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
extern void buffer_write_u32(buffer_type* buffer, uint32_t data);
extern void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
extern int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
extern void buffer_read(buffer_type* buffer, void* data, size_t count);
extern uint8_t buffer_read_u8(buffer_type* buffer);
extern uint16_t buffer_read_u16(buffer_type* buffer);
extern uint32_t buffer_read_u32(buffer_type* buffer);
extern size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
unsigned allow_pointers);
extern uint16_t buffer_pkt_id(buffer_type* buffer);
extern void buffer_pkt_set_random_id(buffer_type* buffer);
extern uint16_t buffer_pkt_flags(buffer_type* buffer);
extern void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
extern int buffer_pkt_qr(buffer_type* buffer);
extern void buffer_pkt_set_qr(buffer_type* buffer);
extern void buffer_pkt_clear_qr(buffer_type* buffer);
extern int buffer_pkt_aa(buffer_type* buffer);
extern void buffer_pkt_set_aa(buffer_type* buffer);
extern int buffer_pkt_tc(buffer_type* buffer);
extern int buffer_pkt_rd(buffer_type* buffer);
extern int buffer_pkt_ra(buffer_type* buffer);
extern int buffer_pkt_ad(buffer_type* buffer);
extern int buffer_pkt_cd(buffer_type* buffer);
extern ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
extern void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
extern ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
extern void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
extern const char* buffer_rcode2str(ldns_pkt_rcode rcode);
extern uint16_t buffer_pkt_qdcount(buffer_type* buffer);
extern void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
extern uint16_t buffer_pkt_ancount(buffer_type* buffer);
extern void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
extern uint16_t buffer_pkt_nscount(buffer_type* buffer);
extern void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
extern uint16_t buffer_pkt_arcount(buffer_type* buffer);
extern void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
extern void
buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
ldns_rr_class qclass);
extern void
buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
extern void buffer_cleanup(buffer_type* buffer);
/*
* Copy data allowing for unaligned accesses in network byte order
* (big endian).
*/
static inline uint16_t
read_uint16(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
return ntohs(* (uint16_t *) src);
#else
uint8_t *p = (uint8_t *) src;
return (p[0] << 8) | p[1];
#endif
}
static inline uint32_t
read_uint32(const void *src)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
return ntohl(* (uint32_t *) src);
#else
uint8_t *p = (uint8_t *) src;
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
#endif
}
static inline void
write_uint16(void *dst, uint16_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
* (uint16_t *) dst = htons(data);
#else
uint8_t *p = (uint8_t *) dst;
p[0] = (uint8_t) ((data >> 8) & 0xff);
p[1] = (uint8_t) (data & 0xff);
#endif
}
static inline void
write_uint32(void *dst, uint32_t data)
{
#ifdef ALLOW_UNALIGNED_ACCESSES
* (uint32_t *) dst = htonl(data);
#else
uint8_t *p = (uint8_t *) dst;
p[0] = (uint8_t) ((data >> 24) & 0xff);
p[1] = (uint8_t) ((data >> 16) & 0xff);
p[2] = (uint8_t) ((data >> 8) & 0xff);
p[3] = (uint8_t) (data & 0xff);
#endif
}
#endif /* WIRE_BUFFER_H */
buffer_pkt_set_flags
void buffer_pkt_set_flags(buffer_type *buffer, uint16_t flags)
Definition buffer.c:798
buffer_available
int buffer_available(buffer_type *buffer, size_t count)
Definition buffer.c:487
ods_rcode_str
ods_lookup_table ods_rcode_str[]
Definition buffer.c:57
buffer_pkt_set_opcode
void buffer_pkt_set_opcode(buffer_type *buffer, ldns_pkt_opcode opcode)
Definition buffer.c:858
buffer_pkt_set_rcode
void buffer_pkt_set_rcode(buffer_type *buffer, ldns_pkt_rcode rcode)
Definition buffer.c:966
buffer_capacity
size_t buffer_capacity(buffer_type *buffer)
Definition buffer.c:401
buffer_pkt_qdcount
uint16_t buffer_pkt_qdcount(buffer_type *buffer)
Definition buffer.c:994
buffer_clear
void buffer_clear(buffer_type *buffer)
Definition buffer.c:99
buffer_pkt_ad
int buffer_pkt_ad(buffer_type *buffer)
Definition buffer.c:930
buffer_pkt_clear_qr
void buffer_pkt_clear_qr(buffer_type *buffer)
Definition buffer.c:834
buffer_skip_rr
int buffer_skip_rr(buffer_type *buffer, unsigned qrr)
Definition buffer.c:342
buffer_cleanup
void buffer_cleanup(buffer_type *buffer)
Definition buffer.c:1145
buffer_read_u32
uint32_t buffer_read_u32(buffer_type *buffer)
Definition buffer.c:736
buffer_read_u16
uint16_t buffer_read_u16(buffer_type *buffer)
Definition buffer.c:721
buffer_current
uint8_t * buffer_current(buffer_type *buffer)
Definition buffer.c:438
buffer_set_limit
void buffer_set_limit(buffer_type *buffer, size_t limit)
Definition buffer.c:385
buffer_write_u8
void buffer_write_u8(buffer_type *buffer, uint8_t data)
Definition buffer.c:552
buffer_set_position
void buffer_set_position(buffer_type *buffer, size_t pos)
Definition buffer.c:137
buffer_pkt_cd
int buffer_pkt_cd(buffer_type *buffer)
Definition buffer.c:942
buffer_rcode2str
const char * buffer_rcode2str(ldns_pkt_rcode rcode)
Definition buffer.c:978
buffer_pkt_qr
int buffer_pkt_qr(buffer_type *buffer)
Definition buffer.c:810
buffer_begin
uint8_t * buffer_begin(buffer_type *buffer)
Definition buffer.c:426
buffer_pkt_opcode
ldns_pkt_opcode buffer_pkt_opcode(buffer_type *buffer)
Definition buffer.c:846
buffer_pkt_rcode
ldns_pkt_rcode buffer_pkt_rcode(buffer_type *buffer)
Definition buffer.c:954
buffer_flip
void buffer_flip(buffer_type *buffer)
Definition buffer.c:112
buffer_pkt_aa
int buffer_pkt_aa(buffer_type *buffer)
Definition buffer.c:870
buffer_at
uint8_t * buffer_at(buffer_type *buffer, size_t at)
Definition buffer.c:413
buffer_position
size_t buffer_position(buffer_type *buffer)
Definition buffer.c:125
buffer_pkt_notify
void buffer_pkt_notify(buffer_type *buffer, ldns_rdf *qname, ldns_rr_class qclass)
Definition buffer.c:1133
buffer_pkt_arcount
uint16_t buffer_pkt_arcount(buffer_type *buffer)
Definition buffer.c:1066
buffer_pkt_set_nscount
void buffer_pkt_set_nscount(buffer_type *buffer, uint16_t count)
Definition buffer.c:1054
buffer_write
void buffer_write(buffer_type *buffer, const void *data, size_t count)
Definition buffer.c:538
buffer_write_rdf
void buffer_write_rdf(buffer_type *buffer, ldns_rdf *rdf)
Definition buffer.c:591
buffer_pkt_set_qr
void buffer_pkt_set_qr(buffer_type *buffer)
Definition buffer.c:822
buffer_read
void buffer_read(buffer_type *buffer, void *data, size_t count)
Definition buffer.c:692
buffer_pkt_rd
int buffer_pkt_rd(buffer_type *buffer)
Definition buffer.c:906
buffer_write_u16_at
void buffer_write_u16_at(buffer_type *buffer, size_t at, uint16_t data)
Definition buffer.c:512
buffer_skip
void buffer_skip(buffer_type *buffer, ssize_t count)
Definition buffer.c:150
buffer_pkt_flags
uint16_t buffer_pkt_flags(buffer_type *buffer)
Definition buffer.c:786
buffer_pkt_tc
int buffer_pkt_tc(buffer_type *buffer)
Definition buffer.c:894
buffer_skip_dname
int buffer_skip_dname(buffer_type *buffer)
Definition buffer.c:310
buffer_pkt_ra
int buffer_pkt_ra(buffer_type *buffer)
Definition buffer.c:918
buffer_remaining
size_t buffer_remaining(buffer_type *buffer)
Definition buffer.c:463
buffer_read_u8
uint8_t buffer_read_u8(buffer_type *buffer)
Definition buffer.c:706
buffer_write_u32
void buffer_write_u32(buffer_type *buffer, uint32_t data)
Definition buffer.c:578
buffer_write_rr
int buffer_write_rr(buffer_type *buffer, ldns_rr *rr)
Definition buffer.c:605
buffer_pkt_query
void buffer_pkt_query(buffer_type *buffer, ldns_rdf *qname, ldns_rr_type qtype, ldns_rr_class qclass)
Definition buffer.c:1120
buffer_pkt_set_random_id
void buffer_pkt_set_random_id(buffer_type *buffer)
Definition buffer.c:772
buffer_pkt_set_ancount
void buffer_pkt_set_ancount(buffer_type *buffer, uint16_t count)
Definition buffer.c:1030
buffer_write_u16
void buffer_write_u16(buffer_type *buffer, uint16_t data)
Definition buffer.c:565
buffer_create
buffer_type * buffer_create(size_t capacity)
Definition buffer.c:78
buffer_read_dname
size_t buffer_read_dname(buffer_type *buffer, uint8_t *dname, unsigned allow_pointers)
Definition buffer.c:246
buffer_limit
size_t buffer_limit(buffer_type *buffer)
Definition buffer.c:373
buffer_pkt_set_aa
void buffer_pkt_set_aa(buffer_type *buffer)
Definition buffer.c:882
buffer_pkt_ancount
uint16_t buffer_pkt_ancount(buffer_type *buffer)
Definition buffer.c:1018
buffer_pkt_nscount
uint16_t buffer_pkt_nscount(buffer_type *buffer)
Definition buffer.c:1042
buffer_pkt_set_arcount
void buffer_pkt_set_arcount(buffer_type *buffer, uint16_t count)
Definition buffer.c:1078
buffer_pkt_set_qdcount
void buffer_pkt_set_qdcount(buffer_type *buffer, uint16_t count)
Definition buffer.c:1006
buffer_pkt_id
uint16_t buffer_pkt_id(buffer_type *buffer)
Definition buffer.c:751
buffer_struct
Definition buffer.h:112
buffer_struct::capacity
size_t capacity
Definition buffer.h:115
buffer_struct::limit
size_t limit
Definition buffer.h:114
buffer_struct::position
size_t position
Definition buffer.h:113
buffer_struct::fixed
unsigned fixed
Definition buffer.h:117
buffer_struct::data
uint8_t * data
Definition buffer.h:116