diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-05-08 15:11:29 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-05-08 15:11:29 +0000 |
commit | d264f516533751356e265a70b9f9e5ae23dd3bdc (patch) | |
tree | b924510399a5ba4d1ae957c697b5d3db240dc354 /src/gns | |
parent | d185c91952be196c8bcfc4f6ed2d67066da669a2 (diff) | |
download | gnunet-d264f516533751356e265a70b9f9e5ae23dd3bdc.tar.gz gnunet-d264f516533751356e265a70b9f9e5ae23dd3bdc.zip |
-preliminary nss plugin AKA works for me
Diffstat (limited to 'src/gns')
-rw-r--r-- | src/gns/nss/Makefile.am | 4 | ||||
-rw-r--r-- | src/gns/nss/nss_gns.c | 88 | ||||
-rw-r--r-- | src/gns/nss/query.c | 49 | ||||
-rw-r--r-- | src/gns/nss/query.h | 33 |
4 files changed, 86 insertions, 88 deletions
diff --git a/src/gns/nss/Makefile.am b/src/gns/nss/Makefile.am index f77743e5c..a75a85670 100644 --- a/src/gns/nss/Makefile.am +++ b/src/gns/nss/Makefile.am | |||
@@ -31,11 +31,11 @@ lib_LTLIBRARIES += \ | |||
31 | libnss_gns4.la \ | 31 | libnss_gns4.la \ |
32 | libnss_gns6.la | 32 | libnss_gns6.la |
33 | 33 | ||
34 | sources = util.c util.h query.h | 34 | sources = util.c util.h query.h query.c |
35 | 35 | ||
36 | # GNU Libc | 36 | # GNU Libc |
37 | libnss_gns_la_SOURCES= $(sources) nss_gns.c | 37 | libnss_gns_la_SOURCES= $(sources) nss_gns.c |
38 | libnss_gns_la_CFLAGS=$(AM_CFLAGS) | 38 | libnss_gns_la_CFLAGS=$(AM_CFLAGS) -D_GNU_SOURCE |
39 | libnss_gns_la_LDFLAGS=$(AM_LDFLAGS) -shrext .so.2 -Wl,-version-script=$(srcdir)/map-file | 39 | libnss_gns_la_LDFLAGS=$(AM_LDFLAGS) -shrext .so.2 -Wl,-version-script=$(srcdir)/map-file |
40 | 40 | ||
41 | libnss_gns4_la_SOURCES=$(libnss_gns_la_SOURCES) | 41 | libnss_gns4_la_SOURCES=$(libnss_gns_la_SOURCES) |
diff --git a/src/gns/nss/nss_gns.c b/src/gns/nss/nss_gns.c index 0fe8a75ba..2e55980ff 100644 --- a/src/gns/nss/nss_gns.c +++ b/src/gns/nss/nss_gns.c | |||
@@ -32,53 +32,29 @@ | |||
32 | 32 | ||
33 | #include "query.h" | 33 | #include "query.h" |
34 | 34 | ||
35 | #ifdef NSS_IPV4_ONLY | 35 | #include <arpa/inet.h> |
36 | #define _nss_mdns_gethostbyname2_r _nss_gns4_minimal_gethostbyname2_r | ||
37 | #define _nss_mdns_gethostbyname_r _nss_gns4_minimal_gethostbyname_r | ||
38 | #define _nss_mdns_gethostbyaddr_r _nss_gns4_minimal_gethostbyaddr_r | ||
39 | #endif | ||
40 | |||
41 | #ifdef NSS_IPV6_ONLY | ||
42 | #define _nss_mdns_gethostbyname2_r _nss_gns6_gethostbyname2_r | ||
43 | #define _nss_mdns_gethostbyname_r _nss_gns6_gethostbyname_r | ||
44 | #define _nss_mdns_gethostbyaddr_r _nss_gns6_gethostbyaddr_r | ||
45 | #endif | ||
46 | |||
47 | #ifndef NSS_IPV4_ONLY | ||
48 | #ifndef NSS_IPV6_ONLY | ||
49 | #define _nss_mdns_gethostbyname2_r _nss_gns_gethostbyname2_r | ||
50 | #define _nss_mdns_gethostbyname_r _nss_gns_gethostbyname_r | ||
51 | #define _nss_mdns_gethostbyaddr_r _nss_gns_gethostbyaddr_r | ||
52 | #endif | ||
53 | #endif | ||
54 | |||
55 | /* Maximum number of entries to return */ | ||
56 | #define MAX_ENTRIES 16 | ||
57 | 36 | ||
58 | #define ALIGN(idx) do { \ | 37 | #define ALIGN(idx) do { \ |
59 | if (idx % sizeof(void*)) \ | 38 | if (idx % sizeof(void*)) \ |
60 | idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \ | 39 | idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \ |
61 | } while(0) | 40 | } while(0) |
62 | 41 | ||
63 | struct userdata { | ||
64 | int count; | ||
65 | int data_len; /* only valid when doing reverse lookup */ | ||
66 | union { | ||
67 | ipv4_address_t ipv4[MAX_ENTRIES]; | ||
68 | ipv6_address_t ipv6[MAX_ENTRIES]; | ||
69 | char *name[MAX_ENTRIES]; | ||
70 | } data; | ||
71 | }; | ||
72 | |||
73 | #ifndef NSS_IPV6_ONLY | 42 | #ifndef NSS_IPV6_ONLY |
74 | static void ipv4_callback(const ipv4_address_t *ipv4, void *userdata) { | 43 | static void ipv4_callback(const ipv4_address_t *ipv4, void *userdata) { |
75 | struct userdata *u = userdata; | 44 | struct userdata *u = userdata; |
76 | assert(ipv4 && userdata); | 45 | |
46 | /*test!*/ | ||
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; | ||
51 | /*test!*/ | ||
52 | /*assert(ipv4 && userdata);*/ | ||
77 | 53 | ||
78 | if (u->count >= MAX_ENTRIES) | 54 | if (u->count >= MAX_ENTRIES) |
79 | return; | 55 | return; |
80 | 56 | ||
81 | u->data.ipv4[u->count++] = *ipv4; | 57 | u->data.ipv4[u->count++] = *ipv4_test; |
82 | u->data_len += sizeof(ipv4_address_t); | 58 | u->data_len += sizeof(ipv4_address_t); |
83 | } | 59 | } |
84 | #endif | 60 | #endif |
@@ -135,9 +111,9 @@ enum nss_status _nss_gns_gethostbyname2_r( | |||
135 | enum nss_status status = NSS_STATUS_UNAVAIL; | 111 | enum nss_status status = NSS_STATUS_UNAVAIL; |
136 | int i; | 112 | int i; |
137 | size_t address_length, l, idx, astart; | 113 | size_t address_length, l, idx, astart; |
138 | void (*ipv4_func)(const ipv4_address_t *ipv4, void *userdata); | ||
139 | void (*ipv6_func)(const ipv6_address_t *ipv6, void *userdata); | ||
140 | int name_allowed; | 114 | int name_allowed; |
115 | |||
116 | printf("v6: %d\n", af == AF_INET6); | ||
141 | 117 | ||
142 | if (af == AF_UNSPEC) | 118 | if (af == AF_UNSPEC) |
143 | #ifdef NSS_IPV6_ONLY | 119 | #ifdef NSS_IPV6_ONLY |
@@ -175,43 +151,25 @@ enum nss_status _nss_gns_gethostbyname2_r( | |||
175 | u.count = 0; | 151 | u.count = 0; |
176 | u.data_len = 0; | 152 | u.data_len = 0; |
177 | 153 | ||
178 | #ifdef NSS_IPV6_ONLY | ||
179 | ipv4_func = NULL; | ||
180 | #else | ||
181 | ipv4_func = af == AF_INET ? ipv4_callback : NULL; | ||
182 | #endif | ||
183 | |||
184 | #ifdef NSS_IPV4_ONLY | ||
185 | ipv6_func = NULL; | ||
186 | #else | ||
187 | ipv6_func = af == AF_INET6 ? ipv6_callback : NULL; | ||
188 | #endif | ||
189 | |||
190 | #ifdef ENABLE_GNS | ||
191 | name_allowed = verify_name_allowed(name); | 154 | name_allowed = verify_name_allowed(name); |
192 | 155 | ||
193 | if (gns_works && name_allowed) { | 156 | if (name_allowed) { |
194 | int r; | 157 | |
195 | 158 | if (gns_resolve_name(af, name, &u) == 0) | |
196 | if ((r = gns_resolve_name(af, name, data)) < 0) | 159 | { |
197 | gns_works = 0; | 160 | printf("GNS success\n"); |
198 | else if (r == 0) { | ||
199 | if (af == AF_INET && ipv4_func) | ||
200 | ipv4_func((ipv4_address_t*) data, &u); | ||
201 | if (af == AF_INET6 && ipv6_func) | ||
202 | ipv6_func((ipv6_address_t*)data, &u); | ||
203 | } else | 161 | } else |
204 | status = NSS_STATUS_NOTFOUND; | 162 | status = NSS_STATUS_NOTFOUND; |
205 | } | 163 | } |
206 | 164 | ||
207 | #endif /* ENABLE_GNS */ | ||
208 | |||
209 | if (u.count == 0) { | 165 | if (u.count == 0) { |
210 | *errnop = ETIMEDOUT; | 166 | *errnop = ETIMEDOUT; |
211 | *h_errnop = HOST_NOT_FOUND; | 167 | *h_errnop = HOST_NOT_FOUND; |
168 | printf("not found\n"); | ||
212 | goto finish; | 169 | goto finish; |
213 | } | 170 | } |
214 | 171 | ||
172 | |||
215 | /* Alias names */ | 173 | /* Alias names */ |
216 | *((char**) buffer) = NULL; | 174 | *((char**) buffer) = NULL; |
217 | result->h_aliases = (char**) buffer; | 175 | result->h_aliases = (char**) buffer; |
@@ -285,17 +243,11 @@ enum nss_status _nss_gns_gethostbyaddr_r( | |||
285 | 243 | ||
286 | /* we dont do this */ | 244 | /* we dont do this */ |
287 | 245 | ||
288 | struct userdata u; | ||
289 | enum nss_status status = NSS_STATUS_UNAVAIL; | 246 | enum nss_status status = NSS_STATUS_UNAVAIL; |
290 | int r; | ||
291 | size_t address_length, idx, astart; | ||
292 | 247 | ||
293 | *errnop = EINVAL; | 248 | *errnop = EINVAL; |
294 | *h_errnop = NO_RECOVERY; | 249 | *h_errnop = NO_RECOVERY; |
295 | 250 | ||
296 | u.count = 0; | ||
297 | u.data_len = 0; | ||
298 | |||
299 | /* Check for address types */ | 251 | /* Check for address types */ |
300 | 252 | ||
301 | *h_errnop = NO_RECOVERY; | 253 | *h_errnop = NO_RECOVERY; |
diff --git a/src/gns/nss/query.c b/src/gns/nss/query.c new file mode 100644 index 000000000..231e0d877 --- /dev/null +++ b/src/gns/nss/query.c | |||
@@ -0,0 +1,49 @@ | |||
1 | #include <string.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | #include "query.h" | ||
5 | #include <arpa/inet.h> | ||
6 | |||
7 | int gns_resolve_name(int af, const char *name, struct userdata *u) | ||
8 | { | ||
9 | FILE *p; | ||
10 | char *cmd; | ||
11 | char line[128]; | ||
12 | |||
13 | if (af == AF_INET6) | ||
14 | asprintf(&cmd, "%s -t AAAA -u %s\n", "gnunet-gns -r", name); | ||
15 | else | ||
16 | asprintf(&cmd, "%s %s\n", "gnunet-gns -r -u", name); | ||
17 | |||
18 | p = popen(cmd,"r"); | ||
19 | |||
20 | if (p != NULL ) | ||
21 | { | ||
22 | while (fgets( line, sizeof(line), p ) != NULL) | ||
23 | { | ||
24 | |||
25 | if (u->count >= MAX_ENTRIES) | ||
26 | break; | ||
27 | |||
28 | if (line[strlen(line)-1] == '\n') | ||
29 | { | ||
30 | line[strlen(line)-1] = '\0'; | ||
31 | if (af == AF_INET) | ||
32 | { | ||
33 | inet_pton(af, line, &(u->data.ipv4[u->count++])); | ||
34 | u->data_len += sizeof(ipv4_address_t); | ||
35 | } | ||
36 | else if ((af == AF_INET6)) | ||
37 | { | ||
38 | inet_pton(af, line, &(u->data.ipv6[u->count++])); | ||
39 | u->data_len += sizeof(ipv6_address_t); | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | fclose(p); | ||
45 | free(cmd); | ||
46 | |||
47 | return 0; | ||
48 | |||
49 | } | ||
diff --git a/src/gns/nss/query.h b/src/gns/nss/query.h index 03148f60b..ab8b6cb43 100644 --- a/src/gns/nss/query.h +++ b/src/gns/nss/query.h | |||
@@ -24,6 +24,9 @@ | |||
24 | 24 | ||
25 | #include <inttypes.h> | 25 | #include <inttypes.h> |
26 | 26 | ||
27 | /* Maximum number of entries to return */ | ||
28 | #define MAX_ENTRIES 16 | ||
29 | |||
27 | typedef struct { | 30 | typedef struct { |
28 | uint32_t address; | 31 | uint32_t address; |
29 | } ipv4_address_t; | 32 | } ipv4_address_t; |
@@ -32,25 +35,19 @@ typedef struct { | |||
32 | uint8_t address[16]; | 35 | uint8_t address[16]; |
33 | } ipv6_address_t; | 36 | } ipv6_address_t; |
34 | 37 | ||
35 | int gns_open_socket(void); | ||
36 | 38 | ||
37 | int gns_query_name(int fd, | 39 | struct userdata { |
40 | int count; | ||
41 | int data_len; /* only valid when doing reverse lookup */ | ||
42 | union { | ||
43 | ipv4_address_t ipv4[MAX_ENTRIES]; | ||
44 | ipv6_address_t ipv6[MAX_ENTRIES]; | ||
45 | char *name[MAX_ENTRIES]; | ||
46 | } data; | ||
47 | }; | ||
48 | |||
49 | int gns_resolve_name(int af, | ||
38 | const char *name, | 50 | const char *name, |
39 | void (*ipv4_func)(const ipv4_address_t *ipv4, void *userdata), | 51 | struct userdata *userdata); |
40 | void (*ipv6_func)(const ipv6_address_t *ipv6, void *userdata), | ||
41 | void *userdata); | ||
42 | |||
43 | #ifndef NSS_IPV6_ONLY | ||
44 | int gns_query_ipv4(int fd, | ||
45 | const ipv4_address_t *ipv4, | ||
46 | void (*name_func)(const char *name, void *userdata), | ||
47 | void *userdata); | ||
48 | #endif | ||
49 | #ifndef NSS_IPV4_ONLY | ||
50 | int gns_query_ipv6(int fd, | ||
51 | const ipv6_address_t *ipv6, | ||
52 | void (*name_func)(const char *name, void *userdata), | ||
53 | void *userdata); | ||
54 | #endif | ||
55 | 52 | ||
56 | #endif | 53 | #endif |