about summary refs log tree commit diff
path: root/src/libs6dns/s6dns_engine.c
blob: 63d8dd68638f9651447d044131c9b6ea0b5185f1 (plain) (blame)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* ISC license. */

#include <errno.h>
#include <skalibs/uint16.h>
#include <skalibs/uint32.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/bytestr.h>
#include <skalibs/error.h>
#include <skalibs/tai.h>
#include <skalibs/stralloc.h>
#include <skalibs/mininetstring.h>
#include <skalibs/socket.h>
#include <skalibs/djbunix.h>
#include <skalibs/ip46.h>
#include <skalibs/random.h>
#include <s6-dns/s6dns-constants.h>
#include <s6-dns/s6dns-message.h>
#include <s6-dns/s6dns-engine.h>
#include "s6dns-message-internal.h"


 /* Utility functions */

static inline int qdomain_diff (char const *s1, unsigned int n1, char const *s2, unsigned int n2)
{
  return (n1 < n2) ? -1 : (n1 > n2) ? 1 : case_diffb(s1, n1, s2) ;
}

static int relevant (char const *q, unsigned int qlen, char const *ans, unsigned int anslen, int strict)
{
  {
    s6dns_message_header_t h ;
    uint16 id ;
    s6dns_message_header_unpack(ans, &h) ;
    if (!h.qr || h.opcode || h.z || (h.counts.qd != 1)) return 0 ;
    if (h.rd != (q[2] & 1)) return 0 ;
    if (strict && !h.aa && !(q[2] & 1)) return 0 ;
    uint16_unpack_big(q, &id) ;
    if (id != h.id) return 0 ;
  }
  {
    char buf[255] ;
    unsigned int pos = 12 ;
    unsigned int n = s6dns_message_get_domain_internal(buf, 255, ans, anslen, &pos) ;
    if (!n) return -1 ;
    if (pos + 4 > anslen) return (errno = EPROTO, -1) ;
    if (qdomain_diff(buf, n, q + 12, qlen - 16)) return 0 ;
    if (byte_diff(q + qlen - 4, 4, ans + pos)) return 0 ;
  }
  return 1 ;
}


 /* Network core functions: transport-dependent */

#ifdef SKALIBS_IPV6_ENABLED
# define socketbind46(fd, ip, port, flag) ((flag) ? socket_bind6(fd, ip, port) : socket_bind4(fd, ip, port))
# define socketudp46(flag) ((flag) ? socket_udp6() : socket_udp4())
# define sockettcp46(flag) ((flag) ? socket_tcp6() : socket_tcp4())
# define socketconnect46(fd, ip, port, flag) ((flag) ? socket_connect6(fd, ip, port) : socket_connect4(fd, ip, port))
# define S6DNS_ENGINE_LOCAL0 IP6_ANY
#else
# define socketbind46(fd, ip, port, flag) ((void)(flag), socket_bind4(fd, ip, port))
# define socketudp46(flag) socket_udp4()
# define sockettcp46(flag) socket_tcp4()
# define socketconnect46(fd, ip, port, flag) socket_connect4(fd, ip, port)
# define S6DNS_ENGINE_LOCAL0 "\0\0\0"
#endif
 
static int randombind (int fd, int flag)
{
  register unsigned int i = 0 ;
  for (; i < 10 ; i++)
    if (socketbind46(fd, S6DNS_ENGINE_LOCAL0, 1025 + badrandom_int(64510), flag) >= 0) return 1 ;
  return (socketbind46(fd, S6DNS_ENGINE_LOCAL0, 0, flag) >= 0) ;
}

static int thisudp (s6dns_engine_t *dt, tain_t const *stamp)
{
  for (;; dt->curserver++)
  {
    if (dt->curserver >= S6DNS_MAX_SERVERS)
    {
      dt->curserver = 0 ;
      if (++dt->protostate >= 4) return -2 ;
    }
    if (byte_diff(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE, S6DNS_ENGINE_LOCAL0)) break ;
  }
  if (badrandom_string(dt->sa.s + 2, 2) < 2) return -1 ; /* random query id */
  dt->fd = socketudp46(s6dns_ip46list_is6(&dt->servers, dt->curserver)) ;
  if (dt->fd < 0) return -1 ;
  if (!randombind(dt->fd, s6dns_ip46list_is6(&dt->servers, dt->curserver)))
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
    return -1 ;
  }
  if ((socketconnect46(dt->fd, s6dns_ip46list_ip(&dt->servers, dt->curserver), 53, s6dns_ip46list_is6(&dt->servers, dt->curserver)) < 0)
   && (errno != EINPROGRESS))
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
    return 0 ;
  }
  tain_add(&dt->localdeadline, stamp, &tain_infinite_relative) ;
  dt->flagreading = 0 ;
  dt->flagwriting = 1 ;
  if (dt->debughook && dt->debughook->pre_send) (*dt->debughook->pre_send)(dt, dt->debughook->external) ;
  return 1 ;
}

static int thistcp (s6dns_engine_t *dt, tain_t const *stamp)
{
  for (; dt->curserver < S6DNS_MAX_SERVERS ; dt->curserver++)
    if (byte_diff(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE, S6DNS_ENGINE_LOCAL0)) break ;
  if (dt->curserver >= S6DNS_MAX_SERVERS) return -2 ;
  if (badrandom_string(dt->sa.s + 2, 2) < 2) return -1 ;
  dt->fd = sockettcp46(s6dns_ip46list_is6(&dt->servers, dt->curserver)) ;
  if (dt->fd < 0) return -1 ;
  if (!randombind(dt->fd, s6dns_ip46list_is6(&dt->servers, dt->curserver)))
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
    return -1 ;
  }
  if ((socketconnect46(dt->fd, s6dns_ip46list_ip(&dt->servers, dt->curserver), 53, s6dns_ip46list_is6(&dt->servers, dt->curserver)) < 0)
   && (errno != EINPROGRESS))
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
    return 0 ;
  }
  tain_addsec(&dt->localdeadline, stamp, 10) ;
  dt->protostate = 0 ;
  dt->flagtcp = dt->flagconnecting = dt->flagwriting = 1 ;
  dt->flagreading = 0 ;
  if (dt->debughook && dt->debughook->pre_send) (*dt->debughook->pre_send)(dt, dt->debughook->external) ;
  return 1 ;
}


 /* all the rest is transport-agnostic */

static int s6dns_engine_prepare (s6dns_engine_t *dt, tain_t const *stamp, int istcp)
{
  for (;; dt->curserver++)
    switch (istcp ? thistcp(dt, stamp) : thisudp(dt, stamp))
    {
      case -2 : return (errno = ENETUNREACH, 0) ;
      case -1 : return 0 ;
      case 0 : break ;
      case 1 : return 1 ;
      default : return (errno = EDOM, 0) ; /* can't happen */
    }
}

static void prepare_next (s6dns_engine_t *dt, tain_t const *stamp, int istcp)
{
  if (!error_isagain(errno))
  {
    fd_close(dt->fd) ; dt->fd = -1 ;
    dt->curserver++ ;
    if (s6dns_engine_prepare(dt, stamp, istcp)) errno = EAGAIN ;
  }
}

static int s6dns_engine_write_udp (s6dns_engine_t *dt, tain_t const *stamp)
{
  static unsigned int const s6dns_engine_udp_timeouts[4] = { 1, 3, 11, 45 } ;
  if (fd_send(dt->fd, dt->sa.s + 2, dt->querylen - 2, 0) < (int)(dt->querylen - 2))
    return (prepare_next(dt, stamp, 0), 0) ;
  tain_addsec(&dt->localdeadline, stamp, s6dns_engine_udp_timeouts[dt->protostate]) ;
  dt->flagwriting = 0 ;
  dt->flagreading = 1 ;
  if (dt->debughook && dt->debughook->post_send) (*dt->debughook->post_send)(dt, dt->debughook->external) ;
  return (errno = EAGAIN, 1) ;
}

static int s6dns_engine_write_tcp (s6dns_engine_t *dt, tain_t const *stamp)
{
  unsigned int r ;
  r = allwrite(dt->fd, dt->sa.s + dt->protostate, dt->querylen - dt->protostate) ;
  dt->protostate += r ;
  if (r) dt->flagconnecting = 0 ;
  if (dt->protostate < dt->sa.len)
  {
    if ((errno == ECONNRESET) && dt->flagconnecting) errno = EAGAIN ;
    prepare_next(dt, stamp, 1) ;
    return 0 ;
  }
  dt->protostate = 0 ;
  tain_addsec(&dt->localdeadline, stamp, 10) ;
  dt->flagwriting = 0 ;
  dt->flagreading = 1 ;
  if (dt->debughook && dt->debughook->post_send) (*dt->debughook->post_send)(dt, dt->debughook->external) ;
  return (errno = EAGAIN, 1) ;
}

static int s6dns_engine_read_udp (s6dns_engine_t *dt, tain_t const *stamp)
{
  s6dns_message_header_t h ;
  char buf[513] ;
  register int r = fd_recv(dt->fd, buf, 513, 0) ;
  if (r < 0) return (prepare_next(dt, stamp, 0), 0) ;
  if ((r > 512) || (r < 12)) return (errno = EAGAIN, 0) ;
  switch (relevant(dt->sa.s + 2, dt->querylen - 2, buf, r, dt->flagstrict))
  {
    case -1 : if (!dt->flagstrict) prepare_next(dt, stamp, 0) ; return 0 ;
    case 0 : return (errno = EAGAIN, 0) ;
    case 1 : break ;
    default : return (errno = EDOM, 0) ; /* can't happen */
  }
  if (dt->debughook && dt->debughook->post_recv)
  {
    if (!stralloc_catb(&dt->sa, buf, r)) return 0 ;
    (*dt->debughook->post_recv)(dt, dt->debughook->external) ;
    dt->sa.len = dt->querylen ;
  }
  s6dns_message_header_unpack(buf, &h) ;
  if (h.tc)
  {
    fd_close(dt->fd) ; dt->fd = -1 ;
    dt->curserver = 0 ;
    dt->protostate = 0 ;
    if (s6dns_engine_prepare(dt, stamp, 1)) errno = EAGAIN ;
    return 0 ;
  }
  switch (h.rcode)
  {
    case 0 : case 3 : break ; /* normal operation */
    case 1 : case 4 : case 5 :
      byte_zero(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE) ; /* do not query it again */
    default : prepare_next(dt, stamp, 0) ; return 0 ;
  }
  if (!stralloc_copyb(&dt->sa, buf, r))
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
    return 0 ;
  }
  dt->querylen = 0 ;
  fd_close(dt->fd) ; dt->fd = -1 ;
  dt->flagreading = 0 ;
  return 1 ;
}

static int s6dns_engine_read_tcp (s6dns_engine_t *dt, tain_t const *stamp)
{
  register int r = sanitize_read(mininetstring_read(dt->fd, &dt->sa, &dt->protostate)) ;
  if (r < 0) return (prepare_next(dt, stamp, 1), 0) ;
  else if (!r) return (errno = EAGAIN, 0) ;
  else if ((dt->sa.len - dt->querylen) < 12)
  {
    errno = EPROTO ;
    goto badanswer ;
  }
  else
  {
    s6dns_message_header_t h ;
    switch (relevant(dt->sa.s + 2, dt->querylen - 2, dt->sa.s + dt->querylen, dt->sa.len - dt->querylen, dt->flagstrict))
    {
      case -1 : if (dt->flagstrict) { dt->sa.len = dt->querylen ; return 0 ; }
      case 0 : goto badanswer ;
      case 1 : break ;
      default : dt->sa.len = dt->querylen ; return (errno = EDOM, 0) ; /* can't happen */
    }
    if (dt->debughook && dt->debughook->post_recv) (*dt->debughook->post_recv)(dt, dt->debughook->external) ;
    s6dns_message_header_unpack(dt->sa.s + dt->querylen, &h) ;
    if (h.tc) goto badanswer ;
    switch (h.rcode)
    {
      case 0 : case 3 : break ; /* normal operation */
      case 1 : case 4 : case 5 :
        byte_zero(s6dns_ip46list_ip(&dt->servers, dt->curserver), SKALIBS_IP_SIZE) ; /* do not query it again */
      default : goto badanswer ;
    }
    fd_close(dt->fd) ; dt->fd = -1 ;
    dt->flagreading = 0 ;
    return 1 ;
  }
 badanswer:
  dt->sa.len = dt->querylen ;
  prepare_next(dt, stamp, 1) ;
  return 0 ;
}


void s6dns_engine_recycle (s6dns_engine_t *dt)
{
  dt->sa.len = 0 ;
  dt->querylen = 0 ;
  byte_zero(&dt->servers, sizeof(s6dns_ip46list_t)) ;
  if (dt->fd >= 0)
  {
    register int e = errno ;
    fd_close(dt->fd) ; dt->fd = -1 ;
    errno = e ;
  }
  dt->status = ECONNABORTED ;
  dt->flagstrict = dt->flagtcp = dt->flagconnecting = dt->flagreading = dt->flagwriting = 0 ;
}

int s6dns_engine_timeout (s6dns_engine_t *dt, tain_t const *stamp)
{
  if (!error_isagain(dt->status)) return (errno = EINVAL, -1) ;
  else if (tain_less(&dt->deadline, stamp)) goto yes ;
  else if (!tain_less(&dt->localdeadline, stamp)) return 0 ;
  else if (dt->flagwriting) goto yes ;
  else if (!dt->flagreading) return 0 ;
  fd_close(dt->fd) ; dt->fd = -1 ;
  dt->curserver++ ;
  if (!s6dns_engine_prepare(dt, stamp, dt->flagtcp))
  {
    s6dns_engine_recycle(dt) ;
    dt->status = errno ;
    return -1 ;
  }
  return 0 ;
 yes:
  s6dns_engine_recycle(dt) ;
  dt->status = ETIMEDOUT ;
  return 1 ;
}

int s6dns_engine_event (s6dns_engine_t *dt, tain_t const *stamp)
{
  if (!error_isagain(dt->status)) return (errno = EINVAL, -1) ;
  if (dt->flagwriting)
    dt->flagtcp ? s6dns_engine_write_tcp(dt, stamp) : s6dns_engine_write_udp(dt, stamp) ;
  else if (dt->flagreading)
  {
    if ((dt->flagtcp) ? s6dns_engine_read_tcp(dt, stamp) : s6dns_engine_read_udp(dt, stamp))
    {
      dt->status = 0 ;
      return 1 ;
    }
  }
  else return (errno = EINVAL, -1) ;
  if (error_isagain(errno)) return 0 ;
  s6dns_engine_recycle(dt) ;
  dt->status = errno ;
  return -1 ;
}

int s6dns_engine_init_r (s6dns_engine_t *dt, s6dns_ip46list_t const *servers, uint32 options, char const *q, unsigned int qlen, uint16 qtype, s6dns_debughook_t const *dbh, tain_t const *deadline, tain_t const *stamp)
{
  s6dns_message_header_t h = S6DNS_MESSAGE_HEADER_ZERO ;
  if (!stralloc_ready(&dt->sa, qlen + 18)) return 0 ;
  dt->deadline = *deadline ;
  dt->localdeadline = *stamp ;
  dt->querylen = qlen + 18 ;
  dt->sa.len = dt->querylen ;
  dt->servers = *servers ;
  dt->debughook = dbh ;
  dt->status = EAGAIN ;
  dt->flagconnecting = dt->flagreading = dt->flagwriting = 0 ;
  dt->flagstrict = !!(options & S6DNS_O_STRICT) ;
  h.rd = !!(options & S6DNS_O_RECURSIVE) ;
  h.counts.qd = 1 ;
  uint16_pack_big(dt->sa.s, qlen + 16) ;
  s6dns_message_header_pack(dt->sa.s + 2, &h) ;
  byte_copy(dt->sa.s + 14, qlen, q) ;
  uint16_pack_big(dt->sa.s + 14 + qlen, qtype) ;
  uint16_pack_big(dt->sa.s + 16 + qlen, S6DNS_C_IN) ;
  if (qlen > 496) dt->flagtcp = 1 ;
  else
  {
    dt->flagtcp = 0 ;
    dt->protostate = h.rd ;
  }
  if (!s6dns_engine_prepare(dt, stamp, dt->flagtcp))
  {
    s6dns_engine_recycle(dt) ;
    return 0 ;
  }
  return 1 ;
}