chiark / gitweb /
WIP DNS bitenc test compiles
[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
25
26 void dnsdomaindec_globalinit(void);
27
28
29 struct dnsdomainenc {
30     /* private for dnsdomainenc_... functions; do not access direcctly */
31     uint32_t pending;
32     int npending; /* never more than 4 unless we're in the middle */
33     uint8_t *out, *bufstop; /* counts backwards */
34     int labremain;
35 };
36
37 /*
38  * The only legal calling sequence is this:
39  *   start
40  *   (addbits | addu32)*
41  *   restbytes
42  *   getresult
43  */
44
45 int dnsdomainenc_start(struct dnsdomainenc *be, uint8_t *buf, int buflen,
46                        int mydompathlen, const char *const mydompath[]);
47     /* returns -1 if mydompath is too long somehow */
48
49 void dnsdomainenc_addbits(struct dnsdomainenc *be, uint32_t val, int nbits);
50     /* adds the bottom nbits bits of val; must be enough space; nbits<=28 */
51 void dnsdomainenc_addu32(struct dnsdomainenc *be, uint32_t val);
52     /* must be enough space */
53
54 int dnsdomainenc_restbytes(struct dnsdomainenc *be,
55                            const uint8_t *bytes, int avail);
56     /* returns number of bytes which did not fit */
57
58 uint8_t *dnsdomainenc_getresult(struct dnsdomainenc *be);
59     /* returns pointer into caller-supplied buffer; we have used
60      * bytes from the result to the end (so the caller needs to remember
61      * the buffer len to know what the encoded length is */
62
63
64 struct dnsdomaindec {
65     /* private for dnsbitdec_... functions; do not access direcctly */
66     uint8_t databuf[MAX_DOMAIN_BYTES];
67     const uint8_t *in;
68     uint32_t pending;
69     int npending;
70 };
71
72 int dnsdomaindec_start(struct dnsdomaindec *bd, const uint8_t *packet,
73                        const uint8_t *endpacket, const uint8_t *domain,
74                        int mydompathlen, const char *const mydompath[],
75                        const uint8_t **domain_end_r);
76
77 uint32_t dnsdomaindec_getbits(struct dnsdomaindec *bd, int nbits);
78
79 uint32_t dnsdomaindec_getu32(struct dnsdomaindec *bd);
80
81 int dnsdomaindec_restbytes(struct dnsdomaindec *bd,
82                            uint8_t outbuf[MAX_DOMAIN_BYTES]);
83
84
85 #endif /* dns_transp_common_h */