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 #define ALIGN(idx) do { \
38 if (idx % sizeof(void*)) \
39 idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \
42 static int ends_with(const char *name, const char* suffix) {
47 if ((ls = strlen(suffix)) > (ln = strlen(name)))
50 return strcasecmp(name+ln-ls, suffix) == 0;
53 static int verify_name_allowed(const char *name) {
54 return ends_with(name, ".gnunet") || ends_with(name, ".zkey");
57 enum nss_status _nss_gns_gethostbyname2_r(
60 struct hostent * result,
67 enum nss_status status = NSS_STATUS_UNAVAIL;
69 size_t address_length, l, idx, astart;
84 if (af != AF_INET && af != AF_INET6)
88 *h_errnop = NO_RECOVERY;
93 address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
95 sizeof(char*)+ /* alias names */
96 strlen(name)+1) { /* official name */
99 *h_errnop = NO_RECOVERY;
100 status = NSS_STATUS_TRYAGAIN;
108 name_allowed = verify_name_allowed(name);
112 if (!gns_resolve_name(af, name, &u) == 0)
114 status = NSS_STATUS_NOTFOUND;
120 *h_errnop = HOST_NOT_FOUND;
121 printf("not found\n");
127 *((char**) buffer) = NULL;
128 result->h_aliases = (char**) buffer;
132 strcpy(buffer+idx, name);
133 result->h_name = buffer+idx;
134 idx += strlen(name)+1;
138 result->h_addrtype = af;
139 result->h_length = address_length;
141 /* Check if there's enough space for the addresses */
142 if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1)) {
144 *h_errnop = NO_RECOVERY;
145 status = NSS_STATUS_TRYAGAIN;
151 l = u.count*address_length;
152 memcpy(buffer+astart, &u.data, l);
153 /* address_length is a multiple of 32bits, so idx is still aligned
157 /* Address array address_lenght is always a multiple of 32bits */
158 for (i = 0; i < u.count; i++)
159 ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
160 ((char**) (buffer+idx))[i] = NULL;
161 result->h_addr_list = (char**) (buffer+idx);
163 status = NSS_STATUS_SUCCESS;
169 enum nss_status _nss_gns_gethostbyname_r (
171 struct hostent *result,
177 return _nss_gns_gethostbyname2_r(
187 enum nss_status _nss_gns_gethostbyaddr_r(
191 struct hostent *result,
197 /* we dont do this */
199 enum nss_status status = NSS_STATUS_UNAVAIL;
202 *h_errnop = NO_RECOVERY;
204 /* Check for address types */
206 *h_errnop = NO_RECOVERY;
208 status = NSS_STATUS_NOTFOUND;