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 
27 /**
28  * Packet buffer.
29  *
30  */
31 
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 
108 /**
109  * Buffer.
110  */
111 typedef struct buffer_struct buffer_type;
112 struct buffer_struct {
113     size_t position;
114     size_t limit;
115     size_t capacity;
116     uint8_t* data;
117     unsigned fixed : 1;
118 };
119 
120 /**
121  * Create a new buffer with the specified capacity.
122  * \param[in] allocator memory allocator
123  * \param[in] capacity specified capacity
124  * \return buffer_type* buffer
125  *
126  */
127 extern buffer_type* buffer_create(size_t capacity);
128 
129 /**
130  * Clear the buffer and make it ready for writing.
131  * The buffer's limit is set to the capacity and the position is set to 0.
132  * \param[in] buffer buffer
133  *
134  */
135 extern void buffer_clear(buffer_type* buffer);
136 
137 /**
138  * Flip the buffer and make it ready for reading.
139  * The data that has been written to the buffer.
140  * The buffer's limit is set to the current position and the position is set
141  * to 0.
142  * \param[in] buffer buffer
143  *
144  */
145 void buffer_flip(buffer_type* buffer);
146 
147 /**
148  * Get the buffer's position.
149  * \param[in] buffer buffer
150  * \return size_t position
151  *
152  */
153 extern size_t buffer_position(buffer_type* buffer);
154 
155 /**
156  * Set the buffer's position.
157  * The position must be less than or equal to the buffer's limit.
158  * \param[in] buffer buffer
159  * \param[in] pos position
160  *
161  */
162 extern void buffer_set_position(buffer_type* buffer, size_t pos);
163 
164 /**
165  * Change the buffer's position.
166  * The position must not be moved behind the buffer's limit or before the
167  * beginning of the buffer.
168  * \param[in] buffer buffer
169  * \param[in] count number of bytes to skip
170  *
171  */
172 extern void buffer_skip(buffer_type* buffer, ssize_t count);
173 
174 /**
175  * Change the buffer's position so that one dname is skipped.
176  * \param[in] buffer buffer
177  * \return int 0 if dname skipping failed
178  *             1 otherwise
179  *
180  */
181 extern int buffer_skip_dname(buffer_type* buffer);
182 
183 /**
184  * Change the buffer's position so that one RR is skipped.
185  * \param[in] buffer buffer
186  * \param[in] qrr 1 if we skip RRs in the question section.
187  * \return int 0 if RR skipping failed
188  *             1 otherwise
189  *
190  */
191 extern int buffer_skip_rr(buffer_type* buffer, unsigned qrr);
192 
193 /**
194  * Get the buffer's limit.
195  * \param[in] buffer buffer
196  * \return size_t limit
197  *
198  */
199 extern size_t buffer_limit(buffer_type* buffer);
200 
201 /**
202  * Set the buffer's limit. If the buffer's position is greater
203  * than the new limit, the position is set to the limit.
204  * \param[in] buffer buffer
205  * \param[in] limit limit
206  *
207  */
208 extern void buffer_set_limit(buffer_type* buffer, size_t limit);
209 
210 /**
211  * Get the buffer's capacity.
212  * \param[in] buffer buffer
213  * \return size_t capacity
214  *
215  */
216 extern size_t buffer_capacity(buffer_type* buffer);
217 
218 /**
219  * Return a pointer to the data at the indicated position.
220  * \param[in] buffer buffer
221  * \param[in] at indicated position
222  * \return uint8_t* pointer to the data at the indicated position
223  *
224  */
225 extern uint8_t* buffer_at(buffer_type* buffer, size_t at);
226 
227 /**
228  * Return a pointer to the data at the beginning of the buffer.
229  * \param[in] buffer buffer
230  * \return uint8_t* pointer to the data at the begin of the buffer
231  *
232  */
233 extern uint8_t* buffer_begin(buffer_type* buffer);
234 
235 /**
236  * Return a pointer to the data at the buffer's current position.
237  * \param[in] buffer buffer
238  * \return uint8_t* pointer to the data at the buffer's current position
239  *
240  */
241 extern uint8_t* buffer_current(buffer_type* buffer);
242 
243 /**
244  * The number of bytes remaining between the buffer's position and limit.
245  * \param[in] buffer buffer
246  * \return size_t remaining number of bytes
247  *
248  */
249 extern size_t buffer_remaining(buffer_type* buffer);
250 
251 /**
252  * Check if the buffer has enough bytes available.
253  * \param[in] buffer buffer
254  * \param[in] count number of bytes that needs to be available
255  * \return int 0 if not enough bytes are available
256  *             1 otherwise
257  *
258  */
259 extern int buffer_available(buffer_type* buffer, size_t count);
260 
261 /**
262  * Write to buffer.
263  * \param[in] buffer buffer
264  * \param[in] data data to write
265  * \param[in] count number of bytes to write
266  *
267  */
268 extern void buffer_write(buffer_type* buffer, const void* data, size_t count);
269 
270 /**
271  * Write uint8_t to buffer.
272  * \param[in] buffer buffer
273  * \param[in] data data to write
274  *
275  */
276 extern void buffer_write_u8(buffer_type* buffer, uint8_t data);
277 
278 /**
279  * Write uint16_t to buffer.
280  * \param[in] buffer buffer
281  * \param[in] data data to write
282  *
283  */
284 extern void buffer_write_u16(buffer_type* buffer, uint16_t data);
285 
286 /**
287  * Write uint16_t to buffer at indicated position.
288  * \param[in] buffer buffer
289  * \param[in] at indicated position
290  * \param[in] data data to write
291  *
292  */
293 extern void buffer_write_u16_at(buffer_type* buffer, size_t at, uint16_t data);
294 
295 /**
296  * Write uint32_t to buffer.
297  * \param[in] buffer buffer
298  * \param[in] data data to write
299  *
300  */
301 extern void buffer_write_u32(buffer_type* buffer, uint32_t data);
302 
303 /**
304  * Write rdf to buffer.
305  * \param[in] buffer buffer
306  * \param[in] rdf data to write
307  *
308  */
309 extern void buffer_write_rdf(buffer_type* buffer, ldns_rdf* rdf);
310 
311 /**
312  * Write rr to buffer.
313  * \param[in] buffer buffer
314  * \param[in] rr data to write
315  * \return int 1 if rr fits, 0 otherwise
316  *
317  */
318 extern int buffer_write_rr(buffer_type* buffer, ldns_rr* rr);
319 
320 /**
321  * Read from buffer.
322  * \param[in] buffer buffer
323  * \param[in] data read data
324  * \param[in] count number of bytes to read
325  *
326  */
327 extern void buffer_read(buffer_type* buffer, void* data, size_t count);
328 
329 /**
330  * Read uint8_t from buffer.
331  * \param[in] buffer buffer
332  * \return uint8_t read data
333  *
334  */
335 extern uint8_t buffer_read_u8(buffer_type* buffer);
336 
337 /**
338  * Read uint16_t from buffer.
339  * \param[in] buffer buffer
340  * \return uint16_t read data
341  *
342  */
343 extern uint16_t buffer_read_u16(buffer_type* buffer);
344 
345 /**
346  * Read uint32_t from buffer.
347  * \param[in] buffer buffer
348  * \return uint32_t read data
349  *
350  */
351 extern uint32_t buffer_read_u32(buffer_type* buffer);
352 
353 /**
354  * Read dname from buffer.
355  * \param[in] buffer buffer
356  * \param[out] dname dname
357  * \param[in] allow_pointers allow pointer labels
358  * \return int dname length
359  *
360  */
361 extern size_t buffer_read_dname(buffer_type* buffer, uint8_t* dname,
362     unsigned allow_pointers);
363 
364 /**
365  * Get query id from buffer.
366  * \param[in] buffer buffer
367  * \return uint16_t query id
368  *
369  */
370 extern uint16_t buffer_pkt_id(buffer_type* buffer);
371 
372 /**
373  * Set random query id in buffer.
374  * \param[in] buffer buffer
375  *
376  */
377 extern void buffer_pkt_set_random_id(buffer_type* buffer);
378 
379 /**
380  * Get flags from buffer.
381  * \param[in] buffer buffer
382  * \return uint16_t flags
383  *
384  */
385 extern uint16_t buffer_pkt_flags(buffer_type* buffer);
386 
387 /**
388  * Set flags in buffer.
389  * \param[in] buffer buffer
390  * \param[in] flags flags
391  *
392  */
393 extern void buffer_pkt_set_flags(buffer_type* buffer, uint16_t flags);
394 
395 /**
396  * Get QR bit from buffer.
397  * \param[in] buffer buffer
398  * \return int 0 if QR bit is clear
399  *             1 if QR bit is set
400  *
401  */
402 extern int buffer_pkt_qr(buffer_type* buffer);
403 
404 /**
405  * Set QR bit in buffer.
406  * \param[in] buffer buffer
407  *
408  */
409 extern void buffer_pkt_set_qr(buffer_type* buffer);
410 
411 /**
412  * Clear QR bit in buffer.
413  * \param[in] buffer buffer
414  *
415  */
416 extern void buffer_pkt_clear_qr(buffer_type* buffer);
417 
418 /**
419  * Get AA bit from buffer.
420  * \param[in] buffer buffer
421  * \return int 0 if AA bit is clear
422  *             1 if AA bit is set
423  *
424  */
425 extern int buffer_pkt_aa(buffer_type* buffer);
426 
427 /**
428  * Set AA bit in buffer.
429  * \param[in] buffer buffer
430  *
431  */
432 extern void buffer_pkt_set_aa(buffer_type* buffer);
433 
434 /**
435  * Get TC bit from buffer.
436  * \param[in] buffer buffer
437  * \return int 0 if TC bit is clear
438  *             1 if TC bit is set
439  *
440  */
441 extern int buffer_pkt_tc(buffer_type* buffer);
442 
443 /**
444  * Get RD bit from buffer.
445  * \param[in] buffer buffer
446  * \return int 0 if RD bit is clear
447  *             1 if RD bit is set
448  *
449  */
450 extern int buffer_pkt_rd(buffer_type* buffer);
451 
452 /**
453  * Get RA bit from buffer.
454  * \param[in] buffer buffer
455  * \return int 0 if RA bit is clear
456  *             1 if RA bit is set
457  *
458  */
459 extern int buffer_pkt_ra(buffer_type* buffer);
460 
461 /**
462  * Get AD bit from buffer.
463  * \param[in] buffer buffer
464  * \return int 0 if AD bit is clear
465  *             1 if AD bit is set
466  *
467  */
468 extern int buffer_pkt_ad(buffer_type* buffer);
469 
470 /**
471  * Get CD bit from buffer.
472  * \param[in] buffer buffer
473  * \return int 0 if CD bit is clear
474  *             1 if CD bit is set
475  *
476  */
477 extern int buffer_pkt_cd(buffer_type* buffer);
478 
479 /**
480  * Get OPCODE from buffer.
481  * \param[in] buffer buffer
482  * \return ldns_pkt_opcode OPCODE
483  *
484  */
485 extern ldns_pkt_opcode buffer_pkt_opcode(buffer_type* buffer);
486 
487 /**
488  * Set OPCODE in buffer.
489  * \param[in] buffer buffer
490  * \param[in] opcode OPCODE
491  *
492  */
493 extern void buffer_pkt_set_opcode(buffer_type* buffer, ldns_pkt_opcode opcode);
494 
495 /**
496  * Get RCODE from buffer.
497  * \param[in] buffer buffer
498  * \return ldns_pkt_rcode RCODE
499  *
500  */
501 extern ldns_pkt_rcode buffer_pkt_rcode(buffer_type* buffer);
502 
503 /**
504  * Set RCODE in buffer.
505  * \param[in] buffer buffer
506  * \param[in] rcode RCODE
507  *
508  */
509 extern void buffer_pkt_set_rcode(buffer_type* buffer, ldns_pkt_rcode rcode);
510 
511 /**
512  * Look up a descriptive text by each rcode.
513  * \param[in] rcode rcode
514  * \return const char* descriptive text
515  *
516  */
517 extern const char* buffer_rcode2str(ldns_pkt_rcode rcode);
518 
519 /**
520  * Get QDCOUNT from buffer.
521  * \param[in] buffer buffer
522  * \return uint16_t QDCOUNT
523  *
524  */
525 extern uint16_t buffer_pkt_qdcount(buffer_type* buffer);
526 
527 /**
528  * Set QDCOUNT in buffer.
529  * \param[in] buffer buffer
530  * \param[in] count QDCOUNT
531  *
532  */
533 extern void buffer_pkt_set_qdcount(buffer_type* buffer, uint16_t count);
534 
535 /**
536  * Get ANCOUNT from buffer.
537  * \param[in] buffer buffer
538  * \return uint16_t ANCOUNT
539  *
540  */
541 extern uint16_t buffer_pkt_ancount(buffer_type* buffer);
542 
543 /**
544  * Set ANCOUNT in buffer.
545  * \param[in] buffer buffer
546  * \param[in] count ANCOUNT
547  *
548  */
549 extern void buffer_pkt_set_ancount(buffer_type* buffer, uint16_t count);
550 
551 /**
552  * Get NSCOUNT from buffer.
553  * \param[in] buffer buffer
554  * \return uint16_t NSCOUNT
555  *
556  */
557 extern uint16_t buffer_pkt_nscount(buffer_type* buffer);
558 
559 /**
560  * Set NSCOUNT in buffer.
561  * \param[in] buffer buffer
562  * \param[in] count NSCOUNT
563  *
564  */
565 extern void buffer_pkt_set_nscount(buffer_type* buffer, uint16_t count);
566 
567 /**
568  * Get ARCOUNT from buffer.
569  * \param[in] buffer buffer
570  * \return uint16_t ARCOUNT
571  *
572  */
573 extern uint16_t buffer_pkt_arcount(buffer_type* buffer);
574 
575 /**
576  * Set ARCOUNT in buffer.
577  * \param[in] buffer buffer
578  * \param[in] count ARCOUNT
579  *
580  */
581 extern void buffer_pkt_set_arcount(buffer_type* buffer, uint16_t count);
582 
583 /**
584  * Make a new query.
585  * \param[in] buffer buffer
586  * \param[in] qname qname
587  * \param[in] qtype qtype
588  * \param[in] qclass qclass
589  *
590  */
591 extern void
592 buffer_pkt_query(buffer_type* buffer, ldns_rdf* qname, ldns_rr_type qtype,
593    ldns_rr_class qclass);
594 
595 /**
596  * Make a new notify.
597  * \param[in] buffer buffer
598  * \param[in] qname qname
599  * \param[in] qclass qclass
600  *
601  */
602 extern void
603 buffer_pkt_notify(buffer_type* buffer, ldns_rdf* qname, ldns_rr_class qclass);
604 
605 /**
606  * Clean up buffer.
607  * \param[in] buffer buffer
608  * \param[in] allocator memory allocator
609  *
610  */
611 extern void buffer_cleanup(buffer_type* buffer);
612 
613 /** UTIL **/
614 
615 /*
616  * Copy data allowing for unaligned accesses in network byte order
617  * (big endian).
618  */
619 
620 static inline uint16_t
read_uint16(const void * src)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
read_uint32(const void * src)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
write_uint16(void * dst,uint16_t data)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
write_uint32(void * dst,uint32_t data)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 */
669