aboutsummaryrefslogtreecommitdiff
path: root/src/gns/nss/nss_gns_query.c
blob: ab88d22cb2634b4962f0621b2a360a290def1956 (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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "nss_gns_query.h"
#include <arpa/inet.h>


/**
 * Wrapper function that uses gnunet-gns cli tool to resolve
 * an IPv4/6 address.
 *
 * @param af address family
 * @param name the name to resolve
 * @param u the userdata (result struct)
 * @return -1 on error else 0
 */
int gns_resolve_name(int af, const char *name, struct userdata *u)
{
  FILE *p;
  char *cmd;
  char line[128];

  if (af == AF_INET6)
  {
    if (-1 == asprintf(&cmd, "%s -t AAAA -u %s\n", "gnunet-gns -r", name))
      return -1;
  }
  else
  {
    if (-1 == asprintf(&cmd, "%s %s\n", "gnunet-gns -r -u", name))
      return -1;
  }

  p = popen(cmd,"r");

  if (p != NULL )
  {
    while (fgets( line, sizeof(line), p ) != NULL)
    {

      if (u->count >= MAX_ENTRIES)
        break;

      if (line[strlen(line)-1] == '\n')
      {
        line[strlen(line)-1] = '\0';
        if (af == AF_INET)
        {
          inet_pton(af, line, &(u->data.ipv4[u->count++]));
          u->data_len += sizeof(ipv4_address_t);
        }
        else if ((af == AF_INET6))
        {
          inet_pton(af, line, &(u->data.ipv6[u->count++]));
          u->data_len += sizeof(ipv6_address_t);
        }
      }
    }
  }
  fclose(p);
  free(cmd);

  return 0;

}