chiark / gitweb /
WIP dns transport packets etc.
[secnet.git] / dns-transp-common.h
1
2 #ifndef dns_transp_common_h
3 #define dns_transp_common_h
4
5 #define MAX_DOMAIN_BYTES 255
6
7
8 #define RCODE_NOERROR  0
9 #define RCODE_FORMERR  1
10 #define RCODE_SERVFAIL 2
11 #define RCODE_NXDOMAIN 3
12 #define RCODE_REFUSED  5
13
14 #define FAKERCODE_NOTAUTH -1
15 #define FAKERCODE_MYDOMAINTOOLONG -2
16 #define FAKERCODE_MYDOMAINLABELTOOLONG -3
17
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <inttypes.h>
26
27
28 void dnsdomaindec_globalinit(void);
29
30
31 struct dnsdomainenc {
32     /* private for dnsdomainenc_... functions; do not access direcctly */
33     uint32_t pending;
34     int npending; /* never more than 4 unless we're in the middle */
35     uint8_t *out, *bufstop; /* counts backwards */
36     int labremain;
37 };
38
39 struct dnspacketenc {
40     uint8_t buf[MAX_DNSPACKET_BYTES];
41     uint8_t *ptr;
42     struct dnsdomainenc dom;
43 };
44
45 /*
46  * The only legal calling sequence is this:
47  *   start
48  *   (addbits | addu32)*
49  *   restbytes
50  *   getresult
51  */
52
53 int dnsdomainenc_start(struct dnsdomainenc *be, uint8_t *buf, int buflen,
54                        int mydompathlen, const char *const mydompath[]);
55     /* returns -1 if mydompath is too long somehow */
56
57 void dnsdomainenc_addbits(struct dnsdomainenc *be, uint32_t val, int nbits);
58     /* adds the bottom nbits bits of val; must be enough space; nbits<=28 */
59 void dnsdomainenc_addu32(struct dnsdomainenc *be, uint32_t val);
60     /* must be enough space */
61
62 int dnsdomainenc_restbytes(struct dnsdomainenc *be,
63                            const uint8_t *bytes, int avail);
64     /* returns number of bytes which did not fit */
65
66 uint8_t *dnsdomainenc_getresult(struct dnsdomainenc *be);
67     /* returns pointer into caller-supplied buffer; we have used
68      * bytes from the result to the end (so the caller needs to remember
69      * the buffer len to know what the encoded length is */
70
71
72 struct dnsdomaindec {
73     /* private for dnsbitdec_... functions; do not access direcctly */
74     uint8_t databuf[MAX_DOMAIN_BYTES];
75     const uint8_t *in;
76     uint32_t pending;
77     int npending;
78 };
79
80 int dnsdomaindec_start(struct dnsdomaindec *bd, const uint8_t *packet,
81                        const uint8_t *endpacket, const uint8_t *domain,
82                        int mydompathlen, const char *const mydompath[],
83                        const uint8_t **domain_end_r);
84
85 uint32_t dnsdomaindec_getbits(struct dnsdomaindec *bd, int nbits);
86
87 uint32_t dnsdomaindec_getu32(struct dnsdomaindec *bd);
88
89 int dnsdomaindec_restbytes(struct dnsdomaindec *bd,
90                            uint8_t outbuf[MAX_DOMAIN_BYTES]);
91
92
93 #endif /* dns_transp_common_h */