summaryrefslogtreecommitdiff
path: root/src/dns.cc
blob: 7c4ccef3fae553b863a1acb80f74ee8de07cbc6d (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#include <dns.h>

#include <stdlib.h> /* exit() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>

#include <events.h>

#include <v8.h>
#include <ev.h>
#include <udns.h>

namespace node {

using namespace v8;

static ev_io io_watcher;
static ev_timer timer_watcher;

static inline void set_timeout() {
  int maxwait = 20;
  int wait = dns_timeouts(NULL, maxwait, ev_now(EV_DEFAULT_UC));

  ev_timer_stop(EV_DEFAULT_UC_ &timer_watcher);

  if (!dns_active(NULL)) return;

  if (wait >= 0) {
    ev_timer_set(&timer_watcher, static_cast<double>(wait), 0.0);
    ev_timer_start(EV_DEFAULT_UC_ &timer_watcher);
  }
}

static inline void maybe_start() {
  ev_io_start(EV_DEFAULT_UC_ &io_watcher);
  set_timeout();
}

static void ioevent(EV_P_ ev_io *_watcher, int revents) {
  assert(revents == EV_READ);
  assert(_watcher == &io_watcher);
  dns_ioevent(NULL, ev_now(EV_DEFAULT_UC));
  if (!dns_active(NULL)) ev_io_stop(EV_DEFAULT_UC_ &io_watcher);
  set_timeout();
}

static void timeout(EV_P_ ev_timer *_watcher, int revents) {
  assert(revents == EV_TIMEOUT);
  assert(_watcher == &timer_watcher);
  set_timeout();
}

static void ResolveError(Promise *promise) {
  HandleScope scope;
  int status = dns_status(NULL);
  assert(status < 0);
  Local<String> msg = String::New(dns_strerror(status));

  Local<Value> argv[2] = { Integer::New(status), msg };

  promise->EmitError(2, argv);
}

static void AfterResolveA4(struct dns_ctx *ctx,
                           struct dns_rr_a4 *result,
                           void *data) {
  assert(ctx == &dns_defctx);

  HandleScope scope;

  Promise *promise = reinterpret_cast<Promise*>(data);
  assert(promise);

  if (result == NULL) {
    ResolveError(promise);
    return;
  }

  /* canonical name */
  Local<String> cname = String::New(result->dnsa4_cname);

  /* Time-To-Live (TTL) value */
  Local<Integer> ttl = Integer::New(result->dnsa4_ttl);

  Local<Array> addresses = Array::New(result->dnsa4_nrr);
  for (int i = 0; i < result->dnsa4_nrr; i++) {
    HandleScope loop_scope;

    char ip[INET_ADDRSTRLEN];
    dns_ntop(AF_INET, &(result->dnsa4_addr[i]), ip, INET_ADDRSTRLEN);
    Local<String> address = String::New(ip);

    addresses->Set(Integer::New(i), address);
  }

  Local<Value> argv[3] = { addresses, ttl, cname };
  promise->EmitSuccess(3, argv);
}

static void AfterResolveA6(struct dns_ctx *ctx,
                           struct dns_rr_a6 *result,
                           void *data) {
  assert(ctx == &dns_defctx);

  HandleScope scope;

  Promise *promise = reinterpret_cast<Promise*>(data);
  assert(promise);

  if (result == NULL) {
    ResolveError(promise);
    return;
  }

  /* canonical name */
  Local<String> cname = String::New(result->dnsa6_cname);

  /* Time-To-Live (TTL) value */
  Local<Integer> ttl = Integer::New(result->dnsa6_ttl);

  Local<Array> addresses = Array::New(result->dnsa6_nrr);
  for (int i = 0; i < result->dnsa6_nrr; i++) {
    HandleScope loop_scope;

    char ip[INET6_ADDRSTRLEN];
    dns_ntop(AF_INET6, &(result->dnsa6_addr[i]), ip, INET6_ADDRSTRLEN);
    Local<String> address = String::New(ip);

    addresses->Set(Integer::New(i), address);
  }

  Local<Value> argv[3] = { addresses, ttl, cname };
  promise->EmitSuccess(3, argv);
}

static Handle<Value> ResolveA(int type, const Arguments& args) {
  HandleScope scope;

  if (args.Length() == 0 || !args[0]->IsString()) {
    return ThrowException(Exception::Error(
          String::New("Argument must be a string.")));
  }

  String::Utf8Value name(args[0]->ToString());

  Promise *promise = Promise::Create();
  struct dns_query *query;
  switch (type) {
    case DNS_T_A:
      query = dns_submit_a4(NULL, *name, 0, AfterResolveA4, promise);
      break;

    case DNS_T_AAAA:
      query = dns_submit_a6(NULL, *name, 0, AfterResolveA6, promise);
      break;

    default:
      return ThrowException(Exception::Error(String::New("Unsupported type")));
  }

  assert(query);  // TODO(ry) better error handling.

  maybe_start();

  return scope.Close(promise->Handle());
}

static Handle<Value> ResolveA4(const Arguments& args) {
  return ResolveA(DNS_T_A, args);
}

static Handle<Value> ResolveA6(const Arguments& args) {
  return ResolveA(DNS_T_AAAA, args);
}

static void AfterReverse(struct dns_ctx *ctx,
                         struct dns_rr_ptr *result,
                         void *data) {
  assert(ctx == &dns_defctx);

  HandleScope scope;

  Promise *promise = reinterpret_cast<Promise*>(data);
  assert(promise);

  if (result == NULL) {
    ResolveError(promise);
    return;
  }

  /* canonical name */
  Local<String> cname = String::New(result->dnsptr_cname);

  /* Time-To-Live (TTL) value */
  Local<Integer> ttl = Integer::New(result->dnsptr_ttl);

  Local<Array> domains = Array::New();

  for (int i = 0; i < result->dnsptr_nrr; i++) {
    HandleScope loop_scope;

    Local<String> domain = String::New(result->dnsptr_ptr[i]);
    domains->Set(Integer::New(i), domain);
  }

  Local<Value> argv[3] = { domains, ttl, cname };
  promise->EmitSuccess(3, argv);
}

static Handle<Value> Reverse(const Arguments& args) {
  HandleScope scope;

  if (args.Length() == 0 || !args[0]->IsString()) {
    return ThrowException(Exception::Error(
          String::New("Argument must be a string.")));
  }

  String::Utf8Value ip_address(args[0]->ToString());

  union {
    struct in_addr addr;
    struct in6_addr addr6;
  } a;

  bool v4;

  if (dns_pton(AF_INET, *ip_address, &a.addr) > 0) {
    v4 = true;
  } else if (dns_pton(AF_INET6, *ip_address, &a.addr6) > 0) {
    v4 = false;
  } else {
    return ThrowException(Exception::Error(String::New("Invalid IP address")));
  }


  Promise *promise = Promise::Create();
  struct dns_query *query;

  if (v4) {
    query = dns_submit_a4ptr(NULL, &a.addr, AfterReverse, promise);
  } else {
    query = dns_submit_a6ptr(NULL, &a.addr6, AfterReverse, promise);
  }

  assert(query);  // TODO(ry) better error handling.

  maybe_start();

  return scope.Close(promise->Handle());
}

void DNS::Initialize(Handle<Object> target) {
  if (dns_init(NULL, 0) < 0) {
    fprintf(stderr, "Error initializing UDNS context\n");
    exit(-2);
  }

  int fd = dns_open(NULL);

  ev_io_init(&io_watcher, ioevent, fd, EV_READ);
  ev_init(&timer_watcher, timeout);

  HandleScope scope;

  target->Set(String::NewSymbol("TEMPFAIL"), Integer::New(DNS_E_TEMPFAIL));
  target->Set(String::NewSymbol("PROTOCOL"), Integer::New(DNS_E_PROTOCOL));
  target->Set(String::NewSymbol("NXDOMAIN"), Integer::New(DNS_E_NXDOMAIN));
  target->Set(String::NewSymbol("NODATA"), Integer::New(DNS_E_NODATA));
  target->Set(String::NewSymbol("NOMEM"), Integer::New(DNS_E_NOMEM));
  target->Set(String::NewSymbol("BADQUERY"), Integer::New(DNS_E_BADQUERY));

  Local<FunctionTemplate> resolve4 = FunctionTemplate::New(ResolveA4);
  target->Set(String::NewSymbol("resolve4"), resolve4->GetFunction());

  Local<FunctionTemplate> resolve6 = FunctionTemplate::New(ResolveA6);
  target->Set(String::NewSymbol("resolve6"), resolve6->GetFunction());

  Local<FunctionTemplate> reverse = FunctionTemplate::New(Reverse);
  target->Set(String::NewSymbol("reverse"), reverse->GetFunction());
}

}  // namespace node