GNUnet  0.10.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
Final status code.
Definition: gnunet-arm.c:89
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 != 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",
75  "-t",
76  (AF_INET6 == af) ? "AAAA" : "A",
77  "-u",
78  (char *)name,
79  "-T",
80  TIMEOUT,
81  NULL };
82 
83  (void)close(STDOUT_FILENO);
84  if ((0 != close(out[0])) ||
85  (STDOUT_FILENO != dup2(out[1], STDOUT_FILENO)))
86  _exit(1);
87  (void)execvp("gnunet-gns", argv);
88  _exit(1);
89  }
90  (void)close(out[1]);
91  p = fdopen(out[0], "r");
92  if (NULL == p)
93  {
94  kwait(pid);
95  return -1;
96  }
97  while (NULL != fgets(line, sizeof(line), p))
98  {
99  if (u->count >= MAX_ENTRIES)
100  break;
101  if (line[strlen(line) - 1] == '\n')
102  {
103  line[strlen(line) - 1] = '\0';
104  if (AF_INET == af)
105  {
106  if (inet_pton(af, line, &u->data.ipv4[u->count]))
107  {
108  u->count++;
109  u->data_len += sizeof(ipv4_address_t);
110  }
111  else
112  {
113  (void)fclose(p);
114  kwait(pid);
115  errno = EINVAL;
116  return -1;
117  }
118  }
119  else if (AF_INET6 == af)
120  {
121  if (inet_pton(af, line, &u->data.ipv6[u->count]))
122  {
123  u->count++;
124  u->data_len += sizeof(ipv6_address_t);
125  }
126  else
127  {
128  (void)fclose(p);
129  kwait(pid);
130  errno = EINVAL;
131  return -1;
132  }
133  }
134  }
135  }
136  (void)fclose(p);
137  waitpid(pid, &ret, 0);
138  if (!WIFEXITED(ret))
139  return -1;
140  if (4 == WEXITSTATUS(ret))
141  return -2; /* not for GNS */
142  if (3 == ret)
143  return -3; /* timeout -> not found */
144  if ((2 == WEXITSTATUS(ret)) || (1 == WEXITSTATUS(ret)))
145  return -2; /* launch failure -> service unavailable */
146  return 0;
147 }
ipv4_address_t ipv4[16]
Definition: nss_gns_query.h:45
#define MAX_ENTRIES
Parts taken from nss-mdns.
Definition: nss_gns_query.h:29
static int ret
Final status code.
Definition: gnunet-arm.c:89
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).
union userdata::@24 data
ipv6_address_t ipv6[16]
Definition: nss_gns_query.h:46
#define TIMEOUT
Definition: nss_gns_query.c:33
const char * name
static void kwait(pid_t chld)
Definition: nss_gns_query.c:36
int data_len
Definition: nss_gns_query.h:43
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
Here is the call graph for this function:
Here is the caller graph for this function: