GNUnet  0.10.x
Macros | Functions
nss_gns.c File Reference
#include <gnunet_config.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <netdb.h>
#include <sys/socket.h>
#include <nss.h>
#include <stdio.h>
#include <stdlib.h>
#include "nss_gns_query.h"
#include <arpa/inet.h>
Include dependency graph for nss_gns.c:

Go to the source code of this file.

Macros

#define ALIGN(idx)
 macro to align idx to 32bit boundary More...
 

Functions

enum nss_status _nss_gns_gethostbyname2_r (const char *name, int af, struct hostent *result, char *buffer, size_t buflen, int *errnop, int *h_errnop)
 The gethostbyname hook executed by nsswitch. More...
 
enum nss_status _nss_gns_gethostbyname_r (const char *name, struct hostent *result, char *buffer, size_t buflen, int *errnop, int *h_errnop)
 The gethostbyname hook executed by nsswitch. More...
 
enum nss_status _nss_gns_gethostbyaddr_r (const void *addr, int len, int af, struct hostent *result, char *buffer, size_t buflen, int *errnop, int *h_errnop)
 The gethostbyaddr hook executed by nsswitch We can't do this so we always return NSS_STATUS_UNAVAIL. More...
 

Macro Definition Documentation

◆ ALIGN

#define ALIGN (   idx)
Value:
do { \
if (idx % sizeof(void*)) \
idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \
} while(0)

macro to align idx to 32bit boundary

Definition at line 39 of file nss_gns.c.

Referenced by _nss_gns_gethostbyname2_r().

Function Documentation

◆ _nss_gns_gethostbyname2_r()

enum nss_status _nss_gns_gethostbyname2_r ( const char *  name,
int  af,
struct hostent *  result,
char *  buffer,
size_t  buflen,
int *  errnop,
int *  h_errnop 
)

The gethostbyname hook executed by nsswitch.

Parameters
namethe name to resolve
afthe address family to resolve
resultthe result hostent
bufferthe result buffer
buflenlength of the buffer
errnopidk
h_errnopidk
Returns
a nss_status code

Definition at line 58 of file nss_gns.c.

References ALIGN, userdata::count, userdata::data, userdata::data_len, gns_resolve_name(), l, and status.

Referenced by _nss_gns_gethostbyname_r().

65 {
66  struct userdata u;
67  enum nss_status status = NSS_STATUS_UNAVAIL;
68  int i;
69  size_t address_length;
70  size_t l;
71  size_t idx;
72  size_t astart;
73 
74  if (af == AF_UNSPEC)
75 #ifdef NSS_IPV6_ONLY
76  af = AF_INET6;
77 #else
78  af = AF_INET;
79 #endif
80 
81 #ifdef NSS_IPV4_ONLY
82  if (af != AF_INET)
83 #elif NSS_IPV6_ONLY
84  if (af != AF_INET6)
85 #else
86  if ( (af != AF_INET) &&
87  (af != AF_INET6) )
88 #endif
89  {
90  *errnop = EINVAL;
91  *h_errnop = NO_RECOVERY;
92 
93  goto finish;
94  }
95 
96  address_length = (af == AF_INET) ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
97  if (buflen <
98  sizeof(char*) + /* alias names */
99  strlen (name) + 1)
100  { /* official name */
101  *errnop = ERANGE;
102  *h_errnop = NO_RECOVERY;
103  status = NSS_STATUS_TRYAGAIN;
104 
105  goto finish;
106  }
107  u.count = 0;
108  u.data_len = 0;
109  i = gns_resolve_name (af,
110  name,
111  &u);
112  if (-1 == i)
113  {
114  *errnop = errno;
115  status = NSS_STATUS_UNAVAIL;
116  *h_errnop = NO_RECOVERY;
117  goto finish;
118  }
119  if (-2 == i)
120  {
121  *errnop = ENOENT;
122  *h_errnop = NO_RECOVERY;
123  status = NSS_STATUS_UNAVAIL;
124  goto finish;
125  }
126  if (-3 == i)
127  {
128  *errnop = ETIMEDOUT;
129  *h_errnop = HOST_NOT_FOUND;
130  status = NSS_STATUS_NOTFOUND;
131  goto finish;
132  }
133  if (0 == u.count)
134  {
135  *errnop = 0; /* success */
136  *h_errnop = NO_DATA; /* success */
137  status = NSS_STATUS_NOTFOUND;
138  goto finish;
139  }
140  /* Alias names */
141  *((char**) buffer) = NULL;
142  result->h_aliases = (char**) buffer;
143  idx = sizeof(char*);
144 
145  /* Official name */
146  strcpy (buffer+idx,
147  name);
148  result->h_name = buffer+idx;
149  idx += strlen (name)+1;
150 
151  ALIGN(idx);
152 
153  result->h_addrtype = af;
154  result->h_length = address_length;
155 
156  /* Check if there's enough space for the addresses */
157  if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1))
158  {
159  *errnop = ERANGE;
160  *h_errnop = NO_RECOVERY;
161  status = NSS_STATUS_TRYAGAIN;
162  goto finish;
163  }
164  /* Addresses */
165  astart = idx;
166  l = u.count*address_length;
167  if (0 != l)
168  memcpy (buffer+astart,
169  &u.data,
170  l);
171  /* address_length is a multiple of 32bits, so idx is still aligned
172  * correctly */
173  idx += l;
174 
175  /* Address array address_length is always a multiple of 32bits */
176  for (i = 0; i < u.count; i++)
177  ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
178  ((char**) (buffer+idx))[i] = NULL;
179  result->h_addr_list = (char**) (buffer+idx);
180 
181  status = NSS_STATUS_SUCCESS;
182 
183 finish:
184  return status;
185 }
#define ALIGN(idx)
macro to align idx to 32bit boundary
Definition: nss_gns.c:39
static struct LoggingHandle * l
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.
Definition: nss_gns_query.c:58
uint16_t status
See PRISM_STATUS_*-constants.
static int result
Global testing status.
const char * name
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _nss_gns_gethostbyname_r()

enum nss_status _nss_gns_gethostbyname_r ( const char *  name,
struct hostent *  result,
char *  buffer,
size_t  buflen,
int *  errnop,
int *  h_errnop 
)

The gethostbyname hook executed by nsswitch.

Parameters
namethe name to resolve
resultthe result hostent
bufferthe result buffer
buflenlength of the buffer
errnop[out]the low-level error code to return to the application
h_errnopidk
Returns
a nss_status code

Definition at line 200 of file nss_gns.c.

References _nss_gns_gethostbyname2_r().

206 {
208  AF_UNSPEC,
209  result,
210  buffer,
211  buflen,
212  errnop,
213  h_errnop);
214 }
static int result
Global testing status.
enum nss_status _nss_gns_gethostbyname2_r(const char *name, int af, struct hostent *result, char *buffer, size_t buflen, int *errnop, int *h_errnop)
The gethostbyname hook executed by nsswitch.
Definition: nss_gns.c:58
const char * name
Here is the call graph for this function:

◆ _nss_gns_gethostbyaddr_r()

enum nss_status _nss_gns_gethostbyaddr_r ( const void *  addr,
int  len,
int  af,
struct hostent *  result,
char *  buffer,
size_t  buflen,
int *  errnop,
int *  h_errnop 
)

The gethostbyaddr hook executed by nsswitch We can't do this so we always return NSS_STATUS_UNAVAIL.

Parameters
addrthe address to resolve
lenthe length of the address
afthe address family of the address
resultthe result hostent
bufferthe result buffer
buflenlength of the buffer
errnop[out]the low-level error code to return to the application
h_errnopidk
Returns
NSS_STATUS_UNAVAIL

Definition at line 232 of file nss_gns.c.

240 {
241  (void) addr;
242  (void) len;
243  (void) af;
244  (void) result;
245  (void) buffer;
246  (void) buflen;
247  *errnop = EINVAL;
248  *h_errnop = NO_RECOVERY;
249  /* NOTE we allow to leak this into DNS so no NOTFOUND */
250  return NSS_STATUS_UNAVAIL;
251 }
static int result
Global testing status.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...