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