58c38323a3d0b5863fcff260611622cd6fe27f2e
[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 <sys/wait.h>
26 #include <netinet/in.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <signal.h>
30
31
32 static void
33 kwait (pid_t chld)
34 {
35   int ret;
36
37   kill (chld, SIGKILL);
38   waitpid (chld,
39            &ret,
40            0);
41 }
42
43
44 /**
45  * Wrapper function that uses gnunet-gns cli tool to resolve
46  * an IPv4/6 address.
47  *
48  * @param af address family
49  * @param name the name to resolve
50  * @param u the userdata (result struct)
51  * @return -1 on internal error,
52  *         -2 if request is not for GNS,
53  *         -3 on timeout,
54  *          else 0
55  */
56 int
57 gns_resolve_name (int af,
58                   const char *name,
59                   struct userdata *u)
60 {
61   FILE *p;
62   char line[128];
63   int ret;
64   int out[2];
65   pid_t pid;
66
67   if (0 != pipe (out))
68     return -1;
69   pid = fork ();
70   if (-1 == pid)
71     return -1;
72   if (0 == pid)
73   {
74     char *argv[] = {
75       "gnunet-gns",
76       "-r",
77       "-t",
78       (AF_INET6 == af) ? "AAAA" : "A",
79       "-u",
80       (char *) name,
81       NULL
82     };
83
84     (void) close (STDOUT_FILENO);
85     if ( (0 != close (out[0])) ||
86          (STDOUT_FILENO != dup2 (out[1], STDOUT_FILENO)) )
87       _exit (1);
88     (void) execvp ("gnunet-gns",
89                    argv);
90     _exit (1);
91   }
92   (void) close (out[1]);
93   p = fdopen (out[0], "r");
94   if (NULL == p)
95     {
96       kwait (pid);
97       return -1;
98     }
99   while (NULL != fgets (line,
100                         sizeof (line),
101                         p))
102   {
103     if (u->count >= MAX_ENTRIES)
104       break;
105     if (line[strlen(line)-1] == '\n')
106     {
107       line[strlen(line)-1] = '\0';
108       if (AF_INET == af)
109       {
110         if (inet_pton(af,
111                       line,
112                       &u->data.ipv4[u->count]))
113         {
114           u->count++;
115           u->data_len += sizeof(ipv4_address_t);
116         }
117         else
118         {
119           (void) fclose (p);
120           kwait (pid);
121           errno = EINVAL;
122           return -1;
123         }
124       }
125       else if (AF_INET6 == af)
126       {
127         if (inet_pton(af,
128                       line,
129                       &u->data.ipv6[u->count]))
130         {
131           u->count++;
132           u->data_len += sizeof(ipv6_address_t);
133         }
134         else
135         {
136           (void) fclose (p);
137           kwait (pid);
138           errno = EINVAL;
139           return -1;
140         }
141       }
142     }
143   }
144   (void) fclose (p);
145   waitpid (pid,
146            &ret,
147            0);
148   if (! WIFEXITED (ret))
149     return -1;
150   if (4 == WEXITSTATUS (ret))
151     return -2; /* not for GNS */
152   if (3 == ret)
153     return -3; /* timeout -> not found */
154   if ( (2 == WEXITSTATUS (ret)) ||
155        (1 == WEXITSTATUS (ret)) )
156     return -2; /* launch failure -> service unavailable */
157   return 0;
158 }
159
160 /* end of nss_gns_query.c */