GNUnet 0.22.2
nss_gns.c File Reference
#include <gnunet_private_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.

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 66 of file nss_gns.c.

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

References ALIGN, gns_resolve_name(), name, result, status, and u.

Referenced by _nss_gns_gethostbyname_r().

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
[out]errnopthe low-level error code to return to the application
h_errnopidk
Returns
a nss_status code

Definition at line 215 of file nss_gns.c.

221{
223 AF_UNSPEC,
224 result,
225 buffer,
226 buflen,
227 errnop,
228 h_errnop);
229}
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:66

References _nss_gns_gethostbyname2_r(), name, and result.

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
[out]errnopthe low-level error code to return to the application
h_errnopidk
Returns
NSS_STATUS_UNAVAIL

Definition at line 255 of file nss_gns.c.

263{
264 (void) addr;
265 (void) len;
266 (void) af;
267 (void) result;
268 (void) buffer;
269 (void) buflen;
270 *errnop = EINVAL;
271 *h_errnop = NO_RECOVERY;
272 /* NOTE we allow to leak this into DNS so no NOTFOUND */
273 return NSS_STATUS_UNAVAIL;
274}

References result.