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