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