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(
60 struct hostent * result,
67 enum nss_status status = NSS_STATUS_UNAVAIL;
69 size_t address_length, l, idx, astart;
83 if (af != AF_INET && af != AF_INET6)
87 *h_errnop = NO_RECOVERY;
92 address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
94 sizeof(char*)+ /* alias names */
95 strlen(name)+1) { /* official name */
98 *h_errnop = NO_RECOVERY;
99 status = NSS_STATUS_TRYAGAIN;
107 i = gns_resolve_name(af, name, &u);
110 status = NSS_STATUS_NOTFOUND;
115 status = NSS_STATUS_UNAVAIL;
122 *h_errnop = HOST_NOT_FOUND;
123 status = NSS_STATUS_NOTFOUND;
129 *((char**) buffer) = NULL;
130 result->h_aliases = (char**) buffer;
134 strcpy(buffer+idx, name);
135 result->h_name = buffer+idx;
136 idx += strlen(name)+1;
140 result->h_addrtype = af;
141 result->h_length = address_length;
143 /* Check if there's enough space for the addresses */
144 if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1)) {
146 *h_errnop = NO_RECOVERY;
147 status = NSS_STATUS_TRYAGAIN;
153 l = u.count*address_length;
155 memcpy(buffer+astart, &u.data, l);
156 /* address_length is a multiple of 32bits, so idx is still aligned
160 /* Address array address_lenght is always a multiple of 32bits */
161 for (i = 0; i < u.count; i++)
162 ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
163 ((char**) (buffer+idx))[i] = NULL;
164 result->h_addr_list = (char**) (buffer+idx);
166 status = NSS_STATUS_SUCCESS;
173 * The gethostbyname hook executed by nsswitch
175 * @param name the name to resolve
176 * @param result the result hostent
177 * @param buffer the result buffer
178 * @param buflen length of the buffer
180 * @param h_errnop idk
181 * @return a nss_status code
184 _nss_gns_gethostbyname_r (
186 struct hostent *result,
192 return _nss_gns_gethostbyname2_r(
203 * The gethostbyaddr hook executed by nsswitch
204 * We can't do this so we always return NSS_STATUS_UNAVAIL
206 * @param addr the address to resolve
207 * @param len the length of the address
208 * @param af the address family of the address
209 * @param result the result hostent
210 * @param buffer the result buffer
211 * @param buflen length of the buffer
213 * @param h_errnop idk
214 * @return NSS_STATUS_UNAVAIL
217 _nss_gns_gethostbyaddr_r(
221 struct hostent *result,
228 *h_errnop = NO_RECOVERY;
229 //NOTE we allow to leak this into DNS so no NOTFOUND
230 return NSS_STATUS_UNAVAIL;