2 This file is part of nss-gns.
4 Parts taken from: nss.c in nss-mdns
6 nss-mdns is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3 of the License,
9 or (at your option) any later version.
11 nss-mdns is distributed in the hope that it will be useful, but1
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with nss-mdns; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 #include <gnunet_config.h>
28 #include <sys/socket.h>
33 #include "nss_gns_query.h"
35 #include <arpa/inet.h>
37 /** macro to align idx to 32bit boundary */
38 #define ALIGN(idx) do { \
39 if (idx % sizeof(void*)) \
40 idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \
45 * The gethostbyname hook executed by nsswitch
47 * @param name the name to resolve
48 * @param af the address family to resolve
49 * @param result the result hostent
50 * @param buffer the result buffer
51 * @param buflen length of the buffer
54 * @return a nss_status code
57 _nss_gns_gethostbyname2_r(const char *name,
59 struct hostent *result,
66 enum nss_status status = NSS_STATUS_UNAVAIL;
68 size_t address_length;
85 if ( (af != AF_INET) &&
90 *h_errnop = NO_RECOVERY;
95 address_length = (af == AF_INET) ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
97 sizeof(char*)+ /* alias names */
101 *h_errnop = NO_RECOVERY;
102 status = NSS_STATUS_TRYAGAIN;
108 i = gns_resolve_name (af,
113 status = NSS_STATUS_NOTFOUND;
118 status = NSS_STATUS_UNAVAIL;
125 *h_errnop = HOST_NOT_FOUND;
126 status = NSS_STATUS_NOTFOUND;
130 *((char**) buffer) = NULL;
131 result->h_aliases = (char**) buffer;
137 result->h_name = buffer+idx;
138 idx += strlen (name)+1;
142 result->h_addrtype = af;
143 result->h_length = address_length;
145 /* Check if there's enough space for the addresses */
146 if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1))
149 *h_errnop = NO_RECOVERY;
150 status = NSS_STATUS_TRYAGAIN;
155 l = u.count*address_length;
157 memcpy (buffer+astart,
160 /* address_length is a multiple of 32bits, so idx is still aligned
164 /* Address array address_length is always a multiple of 32bits */
165 for (i = 0; i < u.count; i++)
166 ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
167 ((char**) (buffer+idx))[i] = NULL;
168 result->h_addr_list = (char**) (buffer+idx);
170 status = NSS_STATUS_SUCCESS;
178 * The gethostbyname hook executed by nsswitch
180 * @param name the name to resolve
181 * @param result the result hostent
182 * @param buffer the result buffer
183 * @param buflen length of the buffer
184 * @param errnop[out] the low-level error code to return to the application
185 * @param h_errnop idk
186 * @return a nss_status code
189 _nss_gns_gethostbyname_r (const char *name,
190 struct hostent *result,
196 return _nss_gns_gethostbyname2_r (name,
207 * The gethostbyaddr hook executed by nsswitch
208 * We can't do this so we always return NSS_STATUS_UNAVAIL
210 * @param addr the address to resolve
211 * @param len the length of the address
212 * @param af the address family of the address
213 * @param result the result hostent
214 * @param buffer the result buffer
215 * @param buflen length of the buffer
216 * @param errnop[out] the low-level error code to return to the application
217 * @param h_errnop idk
218 * @return NSS_STATUS_UNAVAIL
221 _nss_gns_gethostbyaddr_r (const void* addr,
224 struct hostent *result,
231 *h_errnop = NO_RECOVERY;
232 //NOTE we allow to leak this into DNS so no NOTFOUND
233 return NSS_STATUS_UNAVAIL;