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>
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 */ \
43 static void ipv4_callback(const ipv4_address_t *ipv4, void *userdata) {
44 struct userdata *u = userdata;
47 ipv4_address_t *ipv4_test;
48 struct in_addr testaddr;
49 inet_pton(AF_INET, "5.5.5.5", &testaddr);
50 ipv4_test = (ipv4_address_t *)&testaddr;
52 /*assert(ipv4 && userdata);*/
54 if (u->count >= MAX_ENTRIES)
57 u->data.ipv4[u->count++] = *ipv4_test;
58 u->data_len += sizeof(ipv4_address_t);
63 static void ipv6_callback(const ipv6_address_t *ipv6, void *userdata) {
64 struct userdata *u = userdata;
65 assert(ipv6 && userdata);
67 if (u->count >= MAX_ENTRIES)
70 u->data.ipv6[u->count++] = *ipv6;
71 u->data_len += sizeof(ipv6_address_t);
75 static void name_callback(const char*name, void *userdata) {
76 struct userdata *u = userdata;
77 assert(name && userdata);
79 if (u->count >= MAX_ENTRIES)
82 u->data.name[u->count++] = strdup(name);
83 u->data_len += strlen(name)+1;
86 static int ends_with(const char *name, const char* suffix) {
91 if ((ls = strlen(suffix)) > (ln = strlen(name)))
94 return strcasecmp(name+ln-ls, suffix) == 0;
97 static int verify_name_allowed(const char *name) {
98 return ends_with(name, ".gnunet") || ends_with(name, ".zkey");
101 enum nss_status _nss_gns_gethostbyname2_r(
104 struct hostent * result,
111 enum nss_status status = NSS_STATUS_UNAVAIL;
113 size_t address_length, l, idx, astart;
116 printf("v6: %d\n", af == AF_INET6);
130 if (af != AF_INET && af != AF_INET6)
134 *h_errnop = NO_RECOVERY;
139 address_length = af == AF_INET ? sizeof(ipv4_address_t) : sizeof(ipv6_address_t);
141 sizeof(char*)+ /* alias names */
142 strlen(name)+1) { /* official name */
145 *h_errnop = NO_RECOVERY;
146 status = NSS_STATUS_TRYAGAIN;
154 name_allowed = verify_name_allowed(name);
158 if (gns_resolve_name(af, name, &u) == 0)
160 printf("GNS success\n");
162 status = NSS_STATUS_NOTFOUND;
167 *h_errnop = HOST_NOT_FOUND;
168 printf("not found\n");
174 *((char**) buffer) = NULL;
175 result->h_aliases = (char**) buffer;
179 strcpy(buffer+idx, name);
180 result->h_name = buffer+idx;
181 idx += strlen(name)+1;
185 result->h_addrtype = af;
186 result->h_length = address_length;
188 /* Check if there's enough space for the addresses */
189 if (buflen < idx+u.data_len+sizeof(char*)*(u.count+1)) {
191 *h_errnop = NO_RECOVERY;
192 status = NSS_STATUS_TRYAGAIN;
198 l = u.count*address_length;
199 memcpy(buffer+astart, &u.data, l);
200 /* address_length is a multiple of 32bits, so idx is still aligned
204 /* Address array address_lenght is always a multiple of 32bits */
205 for (i = 0; i < u.count; i++)
206 ((char**) (buffer+idx))[i] = buffer+astart+address_length*i;
207 ((char**) (buffer+idx))[i] = NULL;
208 result->h_addr_list = (char**) (buffer+idx);
210 status = NSS_STATUS_SUCCESS;
216 enum nss_status _nss_gns_gethostbyname_r (
218 struct hostent *result,
224 return _nss_gns_gethostbyname2_r(
234 enum nss_status _nss_gns_gethostbyaddr_r(
238 struct hostent *result,
244 /* we dont do this */
246 enum nss_status status = NSS_STATUS_UNAVAIL;
249 *h_errnop = NO_RECOVERY;
251 /* Check for address types */
253 *h_errnop = NO_RECOVERY;
255 status = NSS_STATUS_NOTFOUND;