GNUnet 0.22.2
nss_gns_query.c File Reference
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "nss_gns_query.h"
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
Include dependency graph for nss_gns_query.c:

Go to the source code of this file.

Macros

#define TIMEOUT   "5s"
 

Functions

static void kwait (pid_t chld)
 
int gns_resolve_name (int af, const char *name, struct userdata *u)
 Wrapper function that uses gnunet-gns cli tool to resolve an IPv4/6 address. More...
 

Macro Definition Documentation

◆ TIMEOUT

#define TIMEOUT   "5s"

Definition at line 33 of file nss_gns_query.c.

Function Documentation

◆ kwait()

static void kwait ( pid_t  chld)
static

Definition at line 36 of file nss_gns_query.c.

37{
38 int ret;
39
40 kill (chld, SIGKILL);
41 waitpid (chld, &ret, 0);
42}
static int ret
Final status code.
Definition: gnunet-arm.c:93

References ret.

Referenced by gns_resolve_name().

Here is the caller graph for this function:

◆ gns_resolve_name()

int gns_resolve_name ( int  af,
const char *  name,
struct userdata u 
)

Wrapper function that uses gnunet-gns cli tool to resolve an IPv4/6 address.

Parameters
afaddress family
namethe name to resolve
uthe userdata (result struct)
Returns
-1 on internal error, -2 if request is not for GNS, -3 on timeout, else 0

Definition at line 58 of file nss_gns_query.c.

59{
60 FILE *p;
61 char line[128];
62 int ret;
63 int retry = 0;
64 int out[2];
65 pid_t pid;
66
67 if (0 == getuid ())
68 return -2; /* GNS via NSS is NEVER for root */
69
70query_gns:
71 if (0 != pipe (out))
72 return -1;
73 pid = fork ();
74 if (-1 == pid)
75 return -1;
76 if (0 == pid)
77 {
78 const char *argv[] = { "gnunet-gns",
79 "-r", /* Raw output for easier parsing */
80 "-d", /* DNS compatibility (allow IDNA names, no UTF-8) */
81 "-t",
82 (AF_INET6 == af) ? "AAAA" : "A",
83 "-u",
84 (char *) name,
85 "-T",
86 TIMEOUT,
87 NULL };
88
89 (void) close (STDOUT_FILENO);
90 if ((0 != close (out[0])) ||
91 (STDOUT_FILENO != dup2 (out[1], STDOUT_FILENO)))
92 _exit (1);
93 (void) execvp ("gnunet-gns", (char* const*)argv);
94 _exit (1);
95 }
96 (void) close (out[1]);
97 p = fdopen (out[0], "r");
98 if (NULL == p)
99 {
100 kwait (pid);
101 return -1;
102 }
103 while (NULL != fgets (line, sizeof(line), 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, line, &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, line, &u->data.ipv6[u->count]))
128 {
129 u->count++;
130 u->data_len += sizeof(ipv6_address_t);
131 }
132 else
133 {
134 (void) fclose (p);
135 kwait (pid);
136 errno = EINVAL;
137 return -1;
138 }
139 }
140 }
141 }
142 (void) fclose (p);
143 waitpid (pid, &ret, 0);
144 if (! WIFEXITED (ret))
145 return -1;
146 if (4 == WEXITSTATUS (ret))
147 return -2; /* not for GNS */
148 if (5 == WEXITSTATUS (ret))
149 {
150 if (1 == retry)
151 return -2; /* no go -> service unavailable */
152 retry = 1;
153 system ("gnunet-arm -s");
154 goto query_gns; /* Try again */
155 }
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 */
160 return 0;
161}
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
static char * line
Desired phone line (string to be converted to a hash).
static char * name
Name (label) of the records to list.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
#define TIMEOUT
Definition: nss_gns_query.c:33
static void kwait(pid_t chld)
Definition: nss_gns_query.c:36
#define MAX_ENTRIES
Parts taken from nss-mdns.
Definition: nss_gns_query.h:29

References kwait(), line, MAX_ENTRIES, name, p, pid, ret, TIMEOUT, and u.

Referenced by _nss_gns_gethostbyname2_r().

Here is the call graph for this function:
Here is the caller graph for this function: