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 Lesser General Public License as published
8 by the Free Software Foundation; either version 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 * function to check if name ends with a specific suffix
47 * @param name the name to check
48 * @param suffix the suffix to check for
51 static int ends_with(const char *name, const char* suffix) {
56 if ((ls = strlen(suffix)) > (ln = strlen(name)))
59 return strcasecmp(name+ln-ls, suffix) == 0;
64 * Check if name is inside .gnunet or .zkey TLD
66 * @param name name to check
69 static int verify_name_allowed(const char *name) {
70 return ends_with(name, ".gnunet") || ends_with(name, ".zkey");
74 * The gethostbyname hook executed by nsswitch
76 * @param name the name to resolve
77 * @param af the address family to resolve
78 * @param result the result hostent
79 * @param buffer the result buffer
80 * @param buflen length of the buffer
83 * @return a nss_status code
85 enum nss_status _nss_gns_gethostbyname2_r(
88 struct hostent * result,
95 enum nss_status status = NSS_STATUS_UNAVAIL;
97 size_t address_length, l, idx, astart;
112 if (af != AF_INET && af != AF_INET6)
116 *h_errnop = NO_RECOVERY;
121 address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
123 sizeof(char*)+ /* alias names */
124 strlen(name)+1) { /* official name */
127 *h_errnop = NO_RECOVERY;
128 status = NSS_STATUS_TRYAGAIN;
136 name_allowed = verify_name_allowed(name);
140 if (!gns_resolve_name(af, name, &u) == 0)
142 status = NSS_STATUS_NOTFOUND;
148 *h_errnop = HOST_NOT_FOUND;
149 printf("not found\n");
155 *((char**) buffer) = NULL;
156 result->h_aliases = (char**) buffer;
160 strcpy(buffer+idx, name);
161 result->h_name = buffer+idx;
162 idx += strlen(name)+1;
166 result->h_addrtype = af;
167 result->h_length = address_length;
169 /* Check if there's enough space for the addresses */
170 if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1)) {
172 *h_errnop = NO_RECOVERY;
173 status = NSS_STATUS_TRYAGAIN;
179 l = u.count*address_length;
180 memcpy(buffer+astart, &u.data, l);
181 /* address_length is a multiple of 32bits, so idx is still aligned
185 /* Address array address_lenght is always a multiple of 32bits */
186 for (i = 0; i < u.count; i++)
187 ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
188 ((char**) (buffer+idx))[i] = NULL;
189 result->h_addr_list = (char**) (buffer+idx);
191 status = NSS_STATUS_SUCCESS;
198 * The gethostbyname hook executed by nsswitch
200 * @param name the name to resolve
201 * @param result the result hostent
202 * @param buffer the result buffer
203 * @param buflen length of the buffer
205 * @param h_errnop idk
206 * @return a nss_status code
208 enum nss_status _nss_gns_gethostbyname_r (
210 struct hostent *result,
216 return _nss_gns_gethostbyname2_r(
227 * The gethostbyaddr hook executed by nsswitch
228 * We can't do this so we always return NSS_STATUS_UNAVAIL
230 * @param addr the address to resolve
231 * @param len the length of the address
232 * @param af the address family of the address
233 * @param result the result hostent
234 * @param buffer the result buffer
235 * @param buflen length of the buffer
237 * @param h_errnop idk
238 * @return NSS_STATUS_UNAVAIL
240 enum nss_status _nss_gns_gethostbyaddr_r(
244 struct hostent *result,
250 /* we dont do this */
252 enum nss_status status = NSS_STATUS_UNAVAIL;
255 *h_errnop = NO_RECOVERY;
257 /* Check for address types */
259 *h_errnop = NO_RECOVERY;
261 status = NSS_STATUS_NOTFOUND;