-
Notifications
You must be signed in to change notification settings - Fork 13
/
dns.c
197 lines (162 loc) · 4.75 KB
/
dns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "dns.h"
#include "ops.h"
#include "op_get.h"
#include "crypto.h"
#include <string.h>
#include <assert.h>
#include <gcrypt.h>
#include <event2/dns.h>
#include <event2/dns_struct.h>
#include <ctype.h>
/**
* Count instances of a given character in a string.
*
* @param str the string to look in. This must be null terminated!
* @param c the character to count instances of.
*/
static size_t
count_char( const char *str, const char c ) {
size_t ret = 0;
while( *str ) {
if( *str++ == c ) ret++;
}
return ret;
}
bool
f4dns_is_valid(const char *fqdn) {
char *tmp;
const char *b = fqdn;
const char *a = NULL;
int dashes = 0;
assert( fqdn != NULL );
assert( strlen(fqdn) > 3 ); // XXX: should be more like 36 chars minimum?
if( fqdn[0] == '.' ) return false;
while( (tmp = strchr(b,'.')) ) {
a = b;
b = ++tmp;
}
if( a == NULL ) return false;
if( strcmp(b, "key") != 0 ) return false;
tmp = strchr(a, '.');
assert( tmp != NULL );
dashes = count_char(a, '-');
if( (tmp - a) - dashes != 32 ) return false;
return true;
}
char *
f4dns_hash( const char *fqn, char *hash_return ) {
gcry_error_t err;
gcry_md_hd_t mh;
char *md;
const char prefix[] = "F4DNS::";
err = gcry_md_open(&mh, GCRY_MD_SHA1, GCRY_MD_FLAG_SECURE);
assert( ! gcry_err_code(err) );
gcry_md_write(mh, prefix, sizeof(prefix));
//gcry_md_write(mh, request_type, strlen(request_type));
gcry_md_write(mh, fqn, strlen(fqn));
gcry_md_final(mh);
md = (char*)gcry_md_read(mh, 0);
memcpy(hash_return, md, 20);
gcry_md_close(mh);
return hash_return;
}
f4dns_ctx_t *
f4dns_new(f4_ctx_t *f4_ctx) {
f4dns_ctx_t *ctx = (f4dns_ctx_t *)calloc(sizeof(f4dns_ctx_t), 1);
ctx->f4 = f4_ctx;
ctx->sock = -1;
return ctx;
}
/**
* Handle an incomming DNS request.
* This is a callback which is passed to libevent and libevent calls it
* when a DNS request comes in.
*
* @param req the libevent dns request which initiated the call.
* @param _ctx the ffff DNS context. This is passed as a void type because
* libevent only knows it as "user data".
*/
static void
_f4dns_cb_dnsserver(struct evdns_server_request *req, void *_ctx) {
assert( _ctx != NULL );
f4dns_ctx_t *ctx = (f4dns_ctx_t *)_ctx;
int i;
for( i = 0; i < req->nquestions; i++ ) {
char op_id[20];
char *r_type;
char *r_fqdn = req->questions[i]->name;
if( ! f4dns_is_valid(r_fqdn) ) {
fprintf(stderr,"Invalid fqdn:'%s'\n", r_fqdn);
evdns_server_request_respond(req, DNS_ERR_NOTEXIST);
break;
}
switch( req->questions[i]->type ) {
case EVDNS_TYPE_A:
r_type = "A";
break;
case EVDNS_TYPE_AAAA:
r_type = "AAAA";
break;
case EVDNS_TYPE_MX:
r_type = "MX";
break;
case EVDNS_TYPE_NS:
r_type = "NS";
break;
case EVDNS_TYPE_PTR:
r_type = "PTR";
break;
case EVDNS_TYPE_SOA:
r_type = "SOA";
break;
case EVDNS_TYPE_TXT:
r_type = "TXT";
break;
default:
assert(false);
// XXX: No dude, we shouldn't do this!
r_type = "ANY";
break;
}
f4crypto_hash_fqdn(r_fqdn, 0, op_id);
f4op_t *op = f4op_new(ctx->f4->op_ctx, F4OP_MODE_GET, op_id);
if( ! op ) {
f4_log(ctx->f4, "XXX: Couldn't initialize op!");
assert( false );
f4op_free(ctx->f4->op_ctx, op);
evdns_server_request_respond(req, 0);
}
else {
assert( op->dns_callback != NULL );
op->type = strdup(r_type);
op->fqn = strdup(r_fqdn);
op->dns_callback(ctx->f4->op_ctx, op, req);
f4op_add(ctx->f4->op_ctx, op);
}
}
}
bool
f4dns_init(f4dns_ctx_t *ctx) {
ctx->dns_base = evdns_base_new( ctx->f4->base, 1 );
assert( ctx->dns_base != NULL );
ctx->sock = socket(ctx->f4->listen_dns.ss_family, SOCK_DGRAM, 0);
evutil_make_socket_nonblocking(ctx->sock);
if( bind(ctx->sock, (struct sockaddr*)&ctx->f4->listen_dns, ctx->f4->listen_dns_sz) != 0 ) {
perror("Cannot bind() DNS socket");
return false;
}
ctx->server = evdns_add_server_port_with_base(ctx->f4->base, ctx->sock, 0, _f4dns_cb_dnsserver, ctx);
return true;
}
void
f4dns_free(f4dns_ctx_t *ctx) {
assert( ctx != NULL );
if( ctx->server != NULL ) {
evdns_close_server_port(ctx->server);
}
if( ctx->dns_base != NULL ) {
evdns_base_free(ctx->dns_base, 1);
}
memset(ctx, 0, sizeof(f4dns_ctx_t));
free(ctx);
}