032b2c93bd3752b2ce5a4e5e8b899876c21601b5
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "nss_gns_query.h"
22 #include <arpa/inet.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <errno.h>
27
28
29 /**
30  * Wrapper function that uses gnunet-gns cli tool to resolve
31  * an IPv4/6 address.
32  *
33  * @param af address family
34  * @param name the name to resolve
35  * @param u the userdata (result struct)
36  * @return -1 on internal error,
37  *         -2 if request is not for GNS,
38  *         -3 on timeout,
39  *          else 0
40  */
41 int
42 gns_resolve_name (int af,
43                   const char *name,
44                   struct userdata *u)
45 {
46   FILE *p;
47   char *cmd;
48   char line[128];
49   int ret;
50   int es;
51
52   if (AF_INET6 == af)
53   {
54     if (-1 == asprintf (&cmd,
55                         "%s -t AAAA -u %s\n",
56                         "gnunet-gns -r",
57                         name))
58       return -1;
59   }
60   else
61   {
62     if (-1 == asprintf (&cmd,
63                         "%s %s\n",
64                         "gnunet-gns -r -u",
65                         name))
66       return -1;
67   }
68   if (NULL == (p = popen (cmd, "r")))
69   {
70     es = errno;
71     free (cmd);
72     errno = es;
73     return -1;
74   }
75   while (NULL != fgets (line,
76                         sizeof(line),
77                         p))
78   {
79     if (u->count >= MAX_ENTRIES)
80       break;
81     if (line[strlen(line)-1] == '\n')
82     {
83       line[strlen(line)-1] = '\0';
84       if (AF_INET == af)
85       {
86         if (inet_pton(af,
87                       line,
88                       &u->data.ipv4[u->count]))
89         {
90           u->count++;
91           u->data_len += sizeof(ipv4_address_t);
92         }
93         else
94         {
95           (void) pclose (p);
96           free (cmd);
97           errno = EINVAL;
98           return -1;
99         }
100       }
101       else if (AF_INET6 == af)
102       {
103         if (inet_pton(af,
104                       line,
105                       &u->data.ipv6[u->count]))
106         {
107           u->count++;
108           u->data_len += sizeof(ipv6_address_t);
109         }
110         else
111         {
112           (void) pclose (p);
113           free (cmd);
114           errno = EINVAL;
115           return -1;
116         }
117       }
118     }
119   }
120   ret = pclose (p);
121   free (cmd);
122   if (! WIFEXITED (ret)) 
123     return -1;
124   if (4 == WEXITSTATUS (ret)) 
125     return -2; /* not for GNS */
126   if (3 == ret)
127     return -3; /* timeout -> not found */
128   if ( (2 == WEXITSTATUS (ret)) ||
129        (1 == WEXITSTATUS (ret)) )
130     return -2; /* launch failure -> service unavailable */
131   return 0;
132 }
133
134 /* end of nss_gns_query.c */