ba9fa149672fa948ae3ac457ecaa1f722032da2c
[oweals/gnunet.git] / src / gns / nss / nss_gns_query.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "nss_gns_query.h"
5 #include <arpa/inet.h>
6
7 int gns_resolve_name(int af, const char *name, struct userdata *u)
8 {
9   FILE *p;
10   char *cmd;
11   char line[128];
12
13   if (af == AF_INET6)
14   {
15     if (-1 == asprintf(&cmd, "%s -t AAAA -u %s\n", "gnunet-gns -r", name))
16       return -1;
17   }
18   else
19   {
20     if (-1 == asprintf(&cmd, "%s %s\n", "gnunet-gns -r -u", name))
21       return -1;
22   }
23
24   p = popen(cmd,"r");
25
26   if (p != NULL )
27   {
28     while (fgets( line, sizeof(line), p ) != NULL)
29     {
30
31       if (u->count >= MAX_ENTRIES)
32         break;
33
34       if (line[strlen(line)-1] == '\n')
35       {
36         line[strlen(line)-1] = '\0';
37         if (af == AF_INET)
38         {
39           inet_pton(af, line, &(u->data.ipv4[u->count++]));
40           u->data_len += sizeof(ipv4_address_t);
41         }
42         else if ((af == AF_INET6))
43         {
44           inet_pton(af, line, &(u->data.ipv6[u->count++]));
45           u->data_len += sizeof(ipv6_address_t);
46         }
47       }
48     }
49   }
50   fclose(p);
51   free(cmd);
52
53   return 0;
54
55 }