2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
23 #include "nss_gns_query.h"
24 #include <arpa/inet.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
41 waitpid (chld, &ret, 0);
46 * Wrapper function that uses gnunet-gns cli tool to resolve
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,
58 gns_resolve_name (int af, const char *name, struct userdata *u)
68 return -2; /* GNS via NSS is NEVER for root */
78 char *argv[] = { "gnunet-gns",
79 "-r", /* Raw output for easier parsing */
80 "-d", /* DNS compatibility (allow IDNA names, no UTF-8) */
82 (AF_INET6 == af) ? "AAAA" : "A",
89 (void) close (STDOUT_FILENO);
90 if ((0 != close (out[0])) ||
91 (STDOUT_FILENO != dup2 (out[1], STDOUT_FILENO)))
93 (void) execvp ("gnunet-gns", argv);
96 (void) close (out[1]);
97 p = fdopen (out[0], "r");
103 while (NULL != fgets (line, sizeof(line), p))
105 if (u->count >= MAX_ENTRIES)
107 if (line[strlen (line) - 1] == '\n')
109 line[strlen (line) - 1] = '\0';
112 if (inet_pton (af, line, &u->data.ipv4[u->count]))
115 u->data_len += sizeof(ipv4_address_t);
125 else if (AF_INET6 == af)
127 if (inet_pton (af, line, &u->data.ipv6[u->count]))
130 u->data_len += sizeof(ipv6_address_t);
143 waitpid (pid, &ret, 0);
144 if (! WIFEXITED (ret))
146 if (4 == WEXITSTATUS (ret))
147 return -2; /* not for GNS */
148 if (5 == WEXITSTATUS (ret))
151 return -2; /* no go -> service unavailable */
153 system("gnunet-arm -s");
154 goto query_gns; /* Try again */
156 if (3 == WEXITSTATUS (ret))
157 return -2; /* timeout -> service unavailable */
158 if ((2 == WEXITSTATUS (ret)) || (1 == WEXITSTATUS (ret)))
159 return -2; /* launch failure -> service unavailable */
164 /* end of nss_gns_query.c */