GNUnet  0.11.x
Macros | Functions
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.

Referenced by gns_resolve_name().

Function Documentation

◆ kwait()

static void kwait ( pid_t  chld)
static

Definition at line 36 of file nss_gns_query.c.

References ret.

Referenced by gns_resolve_name().

37 {
38  int ret;
39 
40  kill (chld, SIGKILL);
41  waitpid (chld, &ret, 0);
42 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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.

References userdata::count, userdata::data, userdata::data_len, userdata::ipv4, userdata::ipv6, kwait(), line, MAX_ENTRIES, name, p, pid, ret, and TIMEOUT.

Referenced by _nss_gns_gethostbyname2_r().

59 {
60  FILE *p;
61  char line[128];
62  int ret;
63  int out[2];
64  pid_t pid;
65 
66  if (0 == getuid ())
67  return -2; /* GNS via NSS is NEVER for root */
68  if (0 != pipe (out))
69  return -1;
70  pid = fork ();
71  if (-1 == pid)
72  return -1;
73  if (0 == pid)
74  {
75  char *argv[] = { "gnunet-gns",
76  "-r", /* Raw output for easier parsing */
77  "-d", /* DNS compatibility (allow IDNA names, no UTF-8) */
78  "-t",
79  (AF_INET6 == af) ? "AAAA" : "A",
80  "-u",
81  (char *) name,
82  "-T",
83  TIMEOUT,
84  NULL };
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", argv);
91  _exit (1);
92  }
93  (void) close (out[1]);
94  p = fdopen (out[0], "r");
95  if (NULL == p)
96  {
97  kwait (pid);
98  return -1;
99  }
100  while (NULL != fgets (line, sizeof(line), p))
101  {
102  if (u->count >= MAX_ENTRIES)
103  break;
104  if (line[strlen (line) - 1] == '\n')
105  {
106  line[strlen (line) - 1] = '\0';
107  if (AF_INET == af)
108  {
109  if (inet_pton (af, line, &u->data.ipv4[u->count]))
110  {
111  u->count++;
112  u->data_len += sizeof(ipv4_address_t);
113  }
114  else
115  {
116  (void) fclose (p);
117  kwait (pid);
118  errno = EINVAL;
119  return -1;
120  }
121  }
122  else if (AF_INET6 == af)
123  {
124  if (inet_pton (af, line, &u->data.ipv6[u->count]))
125  {
126  u->count++;
127  u->data_len += sizeof(ipv6_address_t);
128  }
129  else
130  {
131  (void) fclose (p);
132  kwait (pid);
133  errno = EINVAL;
134  return -1;
135  }
136  }
137  }
138  }
139  (void) fclose (p);
140  waitpid (pid, &ret, 0);
141  if (! WIFEXITED (ret))
142  return -1;
143  if (4 == WEXITSTATUS (ret))
144  return -2; /* not for GNS */
145  if (3 == ret)
146  return -3; /* timeout -> not found */
147  if ((2 == WEXITSTATUS (ret)) || (1 == WEXITSTATUS (ret)))
148  return -2; /* launch failure -> service unavailable */
149  return 0;
150 }
ipv4_address_t ipv4[16]
Definition: nss_gns_query.h:49
#define MAX_ENTRIES
Parts taken from nss-mdns.
Definition: nss_gns_query.h:29
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static char * line
Desired phone line (string to be converted to a hash).
ipv6_address_t ipv6[16]
Definition: nss_gns_query.h:50
#define TIMEOUT
Definition: nss_gns_query.c:33
static void kwait(pid_t chld)
Definition: nss_gns_query.c:36
const char * name
int data_len
Definition: nss_gns_query.h:46
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
union userdata::@21 data
Here is the call graph for this function:
Here is the caller graph for this function: