aboutsummaryrefslogtreecommitdiff
path: root/src/resolver/resolver_api.c
blob: 27358996aca89e41a29a1614979c8271b70bf8ca (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file resolver/resolver_api.c
 * @brief resolver for writing a tool
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_client_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_resolver_service.h"
#include "gnunet_server_lib.h"
#include "resolver.h"


struct GetAddressContext
{
  GNUNET_RESOLVER_AddressCallback callback;
  void *cls;
  struct GNUNET_RESOLVER_GetMessage *msg;
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_TIME_Absolute timeout;
};



/**
 * Convert IP address to string without DNS resolution.
 */
static char *
no_resolve (const struct sockaddr *sa, socklen_t salen)
{
  char *ret;
  char inet4[INET_ADDRSTRLEN];
  char inet6[INET6_ADDRSTRLEN];

  if (salen < sizeof (struct sockaddr))
    return NULL;
  switch (sa->sa_family)
    {
    case AF_INET:
      if (salen != sizeof (struct sockaddr_in))
        return NULL;
      inet_ntop (AF_INET,
                 &((struct sockaddr_in *) sa)->sin_addr,
                 inet4, INET_ADDRSTRLEN);
      ret = GNUNET_strdup (inet4);
      break;
    case AF_INET6:
      if (salen != sizeof (struct sockaddr_in6))
        return NULL;
      inet_ntop (AF_INET6,
                 &((struct sockaddr_in6 *) sa)->sin6_addr,
                 inet6, INET6_ADDRSTRLEN);
      ret = GNUNET_strdup (inet6);
      break;
    default:
      ret = NULL;
      break;
    }
  return ret;
}


static void
handle_address_response (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct GetAddressContext *gac = cls;
  uint16_t size;
  const struct sockaddr *sa;
  socklen_t salen;


  if (msg == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  _("Timeout trying to resolve hostname.\n"));
      gac->callback (gac->cls, NULL, 0);
      GNUNET_CLIENT_disconnect (gac->client);
      GNUNET_free (gac);
      return;
    }
  if (GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE != ntohs (msg->type))
    {
      GNUNET_break (0);
      gac->callback (gac->cls, NULL, 0);
      GNUNET_CLIENT_disconnect (gac->client);
      GNUNET_free (gac);
      return;
    }

  size = ntohs (msg->size);
  if (size == sizeof (struct GNUNET_MessageHeader))
    {
#if DEBUG_RESOLVER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  _("Received end message resolving hostname.\n"));
#endif
      gac->callback (gac->cls, NULL, 0);
      GNUNET_CLIENT_disconnect (gac->client);
      GNUNET_free (gac);
      return;
    }
  sa = (const struct sockaddr *) &msg[1];
  salen = size - sizeof (struct GNUNET_MessageHeader);
  if (salen < sizeof (struct sockaddr))
    {
      GNUNET_break (0);
      gac->callback (gac->cls, NULL, 0);
      GNUNET_CLIENT_disconnect (gac->client);
      GNUNET_free (gac);
      return;
    }
#if DEBUG_RESOLVER
  {
    char *ips = no_resolve (sa, salen);
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Resolver returns `%s'.\n"), ips);
    GNUNET_free (ips);
  }
#endif
  gac->callback (gac->cls, sa, salen);
  GNUNET_CLIENT_receive (gac->client,
                         &handle_address_response,
                         gac,
                         GNUNET_TIME_absolute_get_remaining (gac->timeout));
}


static size_t
transmit_get_ip (void *cls, size_t size, void *buf)
{
  struct GetAddressContext *actx = cls;
  uint16_t ms;

  if (buf == NULL)
    {
      /* timeout / error */
      GNUNET_free (actx->msg);
      actx->callback (actx->cls, NULL, 0);
      GNUNET_CLIENT_disconnect (actx->client);
      GNUNET_free (actx);
      return 0;
    }
  ms = ntohs (actx->msg->header.size);
  GNUNET_assert (size >= ms);
  memcpy (buf, actx->msg, ms);
  GNUNET_free (actx->msg);
  actx->msg = NULL;
  GNUNET_CLIENT_receive (actx->client,
                         &handle_address_response,
                         actx,
                         GNUNET_TIME_absolute_get_remaining (actx->timeout));
  return ms;
}



/**
 * Convert a string to one or more IP addresses.
 *
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @param hostname the hostname to resolve
 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
 * @param callback function to call with addresses
 * @param cls closure for callback
 * @param timeout how long to try resolving
 */
void
GNUNET_RESOLVER_ip_get (struct GNUNET_SCHEDULER_Handle *sched,
                        struct GNUNET_CONFIGURATION_Handle *cfg,
                        const char *hostname,
                        int domain,
                        struct GNUNET_TIME_Relative timeout,
                        GNUNET_RESOLVER_AddressCallback callback, void *cls)
{
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_RESOLVER_GetMessage *msg;
  struct GetAddressContext *actx;
  size_t slen;

  slen = strlen (hostname) + 1;
  if (slen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
      GNUNET_SERVER_MAX_MESSAGE_SIZE)
    {
      GNUNET_break (0);
      callback (cls, NULL, 0);
      return;
    }
  client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
  if (client == NULL)
    {
      callback (cls, NULL, 0);
      return;
    }
  msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
  msg->header.size =
    htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + slen);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
  msg->direction = htonl (GNUNET_NO);
  msg->domain = htonl (domain);
  memcpy (&msg[1], hostname, slen);
  actx = GNUNET_malloc (sizeof (struct GetAddressContext));
  actx->callback = callback;
  actx->cls = cls;
  actx->client = client;
  actx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  actx->msg = msg;

#if DEBUG_RESOLVER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Resolver requests DNS resolution of hostname `%s'.\n"),
              hostname);
#endif
  if (NULL ==
      GNUNET_CLIENT_notify_transmit_ready (client,
                                           slen +
                                           sizeof (struct
                                                   GNUNET_RESOLVER_GetMessage),
                                           timeout, &transmit_get_ip, actx))
    {
      GNUNET_free (msg);
      GNUNET_free (actx);
      callback (cls, NULL, 0);
      GNUNET_CLIENT_disconnect (client);
      return;
    }
}


struct GetHostnameContext
{
  GNUNET_RESOLVER_HostnameCallback callback;
  void *cls;
  struct GNUNET_RESOLVER_GetMessage *msg;
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_TIME_Absolute timeout;
};


static void
handle_hostname_response (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct GetHostnameContext *ghc = cls;
  uint16_t size;
  const char *hostname;

  if (msg == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  _("Timeout trying to resolve IP address.\n"));
      ghc->callback (ghc->cls, NULL);
      GNUNET_CLIENT_disconnect (ghc->client);
      GNUNET_free (ghc);
      return;
    }
  size = ntohs (msg->size);
  if (size == sizeof (struct GNUNET_MessageHeader))
    {
#if DEBUG_RESOLVER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  _("Received end message resolving IP address.\n"));
#endif
      ghc->callback (ghc->cls, NULL);
      GNUNET_CLIENT_disconnect (ghc->client);
      GNUNET_free (ghc);
      return;
    }
  hostname = (const char *) &msg[1];
  if (hostname[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
    {
      GNUNET_break (0);
      ghc->callback (ghc->cls, NULL);
      GNUNET_CLIENT_disconnect (ghc->client);
      GNUNET_free (ghc);
      return;
    }
#if DEBUG_RESOLVER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Resolver returns `%s'.\n"), hostname);
#endif
  ghc->callback (ghc->cls, hostname);
  GNUNET_CLIENT_receive (ghc->client,
                         &handle_hostname_response,
                         ghc,
                         GNUNET_TIME_absolute_get_remaining (ghc->timeout));
}


static size_t
transmit_get_hostname (void *cls, size_t size, void *buf)
{
  struct GetHostnameContext *hctx = cls;
  uint16_t msize;

  if (buf == NULL)
    {
      GNUNET_free (hctx->msg);
      hctx->callback (hctx->cls, NULL);
      GNUNET_CLIENT_disconnect (hctx->client);
      GNUNET_free (hctx);
      return 0;
    }
  msize = ntohs (hctx->msg->header.size);
  GNUNET_assert (size >= msize);
  memcpy (buf, hctx->msg, msize);
  GNUNET_free (hctx->msg);
  hctx->msg = NULL;
  GNUNET_CLIENT_receive (hctx->client,
                         &handle_hostname_response,
                         hctx,
                         GNUNET_TIME_absolute_get_remaining (hctx->timeout));
  return msize;
}




/**
 * Get an IP address as a string.
 *
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @param sa host address
 * @param salen length of host address
 * @param do_resolve use GNUNET_NO to return numeric hostname
 * @param timeout how long to try resolving
 * @param callback function to call with hostnames
 * @param cls closure for callback
 */
void
GNUNET_RESOLVER_hostname_get (struct GNUNET_SCHEDULER_Handle *sched,
                              struct GNUNET_CONFIGURATION_Handle *cfg,
                              const struct sockaddr *sa,
                              socklen_t salen,
                              int do_resolve,
                              struct GNUNET_TIME_Relative timeout,
                              GNUNET_RESOLVER_HostnameCallback callback,
                              void *cls)
{
  char *result;
  struct GNUNET_CLIENT_Connection *client;
  struct GNUNET_RESOLVER_GetMessage *msg;
  struct GetHostnameContext *hctx;

  if (GNUNET_NO == do_resolve)
    {
      result = no_resolve (sa, salen);
#if DEBUG_RESOLVER
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  _("Resolver returns `%s'.\n"), result);
#endif
      callback (cls, result);
      if (result != NULL)
        {
          GNUNET_free (result);
          callback (cls, NULL);
        }
      return;
    }
  if (salen + sizeof (struct GNUNET_RESOLVER_GetMessage) >
      GNUNET_SERVER_MAX_MESSAGE_SIZE)
    {
      GNUNET_break (0);
      callback (cls, NULL);
      return;
    }
  client = GNUNET_CLIENT_connect (sched, "resolver", cfg);
  if (client == NULL)
    {
      callback (cls, NULL);
      return;
    }
  msg = GNUNET_malloc (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
  msg->header.size =
    htons (sizeof (struct GNUNET_RESOLVER_GetMessage) + salen);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_RESOLVER_REQUEST);
  msg->direction = htonl (GNUNET_YES);
  msg->domain = htonl (sa->sa_family);
  memcpy (&msg[1], sa, salen);
#if DEBUG_RESOLVER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Resolver requests DNS resolution of IP address.\n"));
#endif
  hctx = GNUNET_malloc (sizeof (struct GetHostnameContext));
  hctx->callback = callback;
  hctx->cls = cls;
  hctx->client = client;
  hctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  hctx->msg = msg;
  if (NULL ==
      GNUNET_CLIENT_notify_transmit_ready (client,
                                           sizeof (struct
                                                   GNUNET_RESOLVER_GetMessage)
                                           + salen, timeout,
                                           &transmit_get_hostname, hctx))
    {
      GNUNET_free (msg);
      callback (cls, NULL);
      GNUNET_CLIENT_disconnect (client);
      GNUNET_free (hctx);
    }
}

/**
 * Maximum supported length of hostname
 */
#define MAX_HOSTNAME 1024


/**
 * Resolve our hostname to an IP address.
 *
 * @param sched scheduler to use
 * @param cfg configuration to use
 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any"
 * @param callback function to call with addresses
 * @param cls closure for callback
 * @param timeout how long to try resolving
 */
void
GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
                                  struct GNUNET_CONFIGURATION_Handle *cfg,
                                  int domain,
                                  struct GNUNET_TIME_Relative timeout,
                                  GNUNET_RESOLVER_AddressCallback callback,
                                  void *cls)
{
  char hostname[MAX_HOSTNAME];

  if (0 != gethostname (hostname, sizeof (hostname) - 1))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
                           GNUNET_ERROR_TYPE_BULK, "gethostname");
      callback (cls, NULL, 0);
      return;
    }
#if DEBUG_RESOLVER
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              _("Resolving our hostname `%s'\n"), hostname);
#endif
  GNUNET_RESOLVER_ip_get (sched,
                          cfg, hostname, domain, timeout, callback, cls);
}




/* end of resolver_api.c */