4700100b59ada7d14a6e58f843c8f9ce7c8a3cf1
[oweals/gnunet.git] / src / gns / nss / nss_gns_query.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "nss_gns_query.h"
24 #include <arpa/inet.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28
29
30 /**
31  * Wrapper function that uses gnunet-gns cli tool to resolve
32  * an IPv4/6 address.
33  *
34  * @param af address family
35  * @param name the name to resolve
36  * @param u the userdata (result struct)
37  * @return -1 on error else 0
38  */
39 int
40 gns_resolve_name (int af,
41                   const char *name,
42                   struct userdata *u)
43 {
44   FILE *p;
45   char *cmd;
46   char line[128];
47   int ret;
48
49   if (AF_INET6 == af)
50   {
51     if (-1 == asprintf (&cmd,
52                         "%s -t AAAA -u %s\n",
53                         "gnunet-gns -r", name))
54       return -1;
55   }
56   else
57   {
58     if (-1 == asprintf (&cmd,
59                         "%s %s\n",
60                         "gnunet-gns -r -u", name))
61       return -1;
62   }
63   if (NULL == (p = popen (cmd, "r")))
64   {
65     free (cmd);
66     return -1;
67   }
68   while (NULL != fgets (line, sizeof(line), p))
69   {
70     if (u->count >= MAX_ENTRIES)
71       break;
72     if (line[strlen(line)-1] == '\n')
73     {
74       line[strlen(line)-1] = '\0';
75       if (AF_INET == af)
76       {
77         if (inet_pton(af, line, &(u->data.ipv4[u->count])))
78         {
79           u->count++;
80           u->data_len += sizeof(ipv4_address_t);
81         }
82         else
83         {
84           pclose (p);
85           free (cmd);
86           return -1;
87         }
88       }
89       else if (AF_INET6 == af)
90       {
91         if (inet_pton(af, line, &(u->data.ipv6[u->count])))
92         {
93           u->count++;
94           u->data_len += sizeof(ipv6_address_t);
95         }
96         else
97         {
98           pclose (p);
99           free (cmd);
100           return -1;
101         }
102       }
103     }
104   }
105   ret = pclose (p);
106   free (cmd);
107   if (4 == ret)
108     return -2; /* not for GNS */
109   if (3 == ret)
110     return -3; /* timeout */
111   return 0;
112 }
113 /* end of nss_gns_query.c */