]> git.meshlink.io Git - catta/blob - tests/catta-test.c
Fix compilation error caused by ACX_THREAD
[catta] / tests / catta-test.c
1 /***
2   This file is part of catta.
3
4   catta is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   catta is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with catta; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32
33 #include <catta/malloc.h>
34 #include <catta/simple-watch.h>
35 #include <catta/alternative.h>
36 #include <catta/timeval.h>
37
38 #include <catta/core.h>
39 #include <catta/log.h>
40 #include <catta/publish.h>
41 #include <catta/lookup.h>
42 #include "../src/dns-srv-rr.h"
43
44 static CattaSEntryGroup *group = NULL;
45 static CattaServer *server = NULL;
46 static char *service_name = NULL;
47
48 static const CattaPoll *poll_api;
49
50 static void quit_timeout_callback(CATTA_GCC_UNUSED CattaTimeout *timeout, void* userdata) {
51     CattaSimplePoll *simple_poll = userdata;
52
53     catta_simple_poll_quit(simple_poll);
54 }
55
56 static void dump_line(const char *text, CATTA_GCC_UNUSED void* userdata) {
57     printf("%s\n", text);
58 }
59
60 static void dump_timeout_callback(CattaTimeout *timeout, void* userdata) {
61     struct timeval tv;
62
63     CattaServer *catta = userdata;
64     catta_server_dump(catta, dump_line, NULL);
65
66     catta_elapse_time(&tv, 5000, 0);
67     poll_api->timeout_update(timeout, &tv);
68 }
69
70 static const char *browser_event_to_string(CattaBrowserEvent event) {
71     switch (event) {
72         case CATTA_BROWSER_NEW : return "NEW";
73         case CATTA_BROWSER_REMOVE : return "REMOVE";
74         case CATTA_BROWSER_CACHE_EXHAUSTED : return "CACHE_EXHAUSTED";
75         case CATTA_BROWSER_ALL_FOR_NOW : return "ALL_FOR_NOW";
76         case CATTA_BROWSER_FAILURE : return "FAILURE";
77     }
78
79     abort();
80 }
81
82 static const char *resolver_event_to_string(CattaResolverEvent event) {
83     switch (event) {
84         case CATTA_RESOLVER_FOUND: return "FOUND";
85         case CATTA_RESOLVER_FAILURE: return "FAILURE";
86     }
87     abort();
88 }
89
90 static void record_browser_callback(
91     CattaSRecordBrowser *r,
92     CattaIfIndex iface,
93     CattaProtocol protocol,
94     CattaBrowserEvent event,
95     CattaRecord *record,
96     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
97     CATTA_GCC_UNUSED void* userdata) {
98     char *t;
99
100     assert(r);
101
102     if (record) {
103         catta_log_debug("RB: record [%s] on %i.%i is %s", t = catta_record_to_string(record), iface, protocol, browser_event_to_string(event));
104         catta_free(t);
105     } else
106         catta_log_debug("RB: [%s]", browser_event_to_string(event));
107
108 }
109
110 static void remove_entries(void);
111 static void create_entries(int new_name);
112
113 static void entry_group_callback(CATTA_GCC_UNUSED CattaServer *s, CATTA_GCC_UNUSED CattaSEntryGroup *g, CattaEntryGroupState state, CATTA_GCC_UNUSED void* userdata) {
114     catta_log_debug("entry group state: %i", state);
115
116     if (state == CATTA_ENTRY_GROUP_COLLISION) {
117         remove_entries();
118         create_entries(1);
119         catta_log_debug("Service name conflict, retrying with <%s>", service_name);
120     } else if (state == CATTA_ENTRY_GROUP_ESTABLISHED) {
121         catta_log_debug("Service established under name <%s>", service_name);
122     }
123 }
124
125 static void server_callback(CattaServer *s, CattaServerState state, CATTA_GCC_UNUSED void* userdata) {
126
127     server = s;
128     catta_log_debug("server state: %i", state);
129
130     if (state == CATTA_SERVER_RUNNING) {
131         catta_log_debug("Server startup complete. Host name is <%s>. Service cookie is %u", catta_server_get_host_name_fqdn(s), catta_server_get_local_service_cookie(s));
132         create_entries(0);
133     } else if (state == CATTA_SERVER_COLLISION) {
134         char *n;
135         remove_entries();
136
137         n = catta_alternative_host_name(catta_server_get_host_name(s));
138
139         catta_log_debug("Host name conflict, retrying with <%s>", n);
140         catta_server_set_host_name(s, n);
141         catta_free(n);
142     }
143 }
144
145 static void remove_entries(void) {
146     if (group)
147         catta_s_entry_group_reset(group);
148 }
149
150 static void create_entries(int new_name) {
151     CattaAddress a;
152     CattaRecord *r;
153
154     remove_entries();
155
156     if (!group)
157         group = catta_s_entry_group_new(server, entry_group_callback, NULL);
158
159     assert(catta_s_entry_group_is_empty(group));
160
161     if (!service_name)
162         service_name = catta_strdup("Test Service");
163     else if (new_name) {
164         char *n = catta_alternative_service_name(service_name);
165         catta_free(service_name);
166         service_name = n;
167     }
168
169     if (catta_server_add_service(server, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, service_name, "_http._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
170         catta_log_error("Failed to add HTTP service");
171         goto fail;
172     }
173
174     if (catta_server_add_service(server, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, service_name, "_ftp._tcp", NULL, NULL, 21, "foo", NULL) < 0) {
175         catta_log_error("Failed to add FTP service");
176         goto fail;
177     }
178
179     if (catta_server_add_service(server, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0,service_name, "_webdav._tcp", NULL, NULL, 80, "foo", NULL) < 0) {
180         catta_log_error("Failed to add WEBDAV service");
181         goto fail;
182     }
183
184     if (catta_server_add_dns_server_address(server, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, NULL, CATTA_DNS_SERVER_RESOLVE, catta_address_parse("192.168.50.1", CATTA_PROTO_UNSPEC, &a), 53) < 0) {
185         catta_log_error("Failed to add new DNS Server address");
186         goto fail;
187     }
188
189     r = catta_record_new_full("cname.local", CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_CNAME, CATTA_DEFAULT_TTL);
190     r->data.cname.name = catta_strdup("cocaine.local");
191
192     if (catta_server_add(server, group, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, 0, r) < 0) {
193         catta_record_unref(r);
194         catta_log_error("Failed to add CNAME record");
195         goto fail;
196     }
197     catta_record_unref(r);
198
199     catta_s_entry_group_commit(group);
200     return;
201
202 fail:
203     if (group)
204         catta_s_entry_group_free(group);
205
206     group = NULL;
207 }
208
209 static void hnr_callback(
210     CATTA_GCC_UNUSED CattaSHostNameResolver *r,
211     CattaIfIndex iface,
212     CattaProtocol protocol,
213     CattaResolverEvent event,
214     const char *hostname,
215     const CattaAddress *a,
216     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
217     CATTA_GCC_UNUSED void* userdata) {
218     char t[CATTA_ADDRESS_STR_MAX];
219
220     if (a)
221         catta_address_snprint(t, sizeof(t), a);
222
223     catta_log_debug("HNR: (%i.%i) <%s> -> %s [%s]", iface, protocol, hostname, a ? t : "n/a", resolver_event_to_string(event));
224 }
225
226 static void ar_callback(
227     CATTA_GCC_UNUSED CattaSAddressResolver *r,
228     CattaIfIndex iface,
229     CattaProtocol protocol,
230     CattaResolverEvent event,
231     const CattaAddress *a,
232     const char *hostname,
233     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
234     CATTA_GCC_UNUSED void* userdata) {
235     char t[CATTA_ADDRESS_STR_MAX];
236
237     catta_address_snprint(t, sizeof(t), a);
238
239     catta_log_debug("AR: (%i.%i) %s -> <%s> [%s]", iface, protocol, t, hostname ? hostname : "n/a", resolver_event_to_string(event));
240 }
241
242 static void db_callback(
243     CATTA_GCC_UNUSED CattaSDomainBrowser *b,
244     CattaIfIndex iface,
245     CattaProtocol protocol,
246     CattaBrowserEvent event,
247     const char *domain,
248     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
249     CATTA_GCC_UNUSED void* userdata) {
250
251     catta_log_debug("DB: (%i.%i) <%s> [%s]", iface, protocol, domain ? domain : "NULL", browser_event_to_string(event));
252 }
253
254 static void stb_callback(
255     CATTA_GCC_UNUSED CattaSServiceTypeBrowser *b,
256     CattaIfIndex iface,
257     CattaProtocol protocol,
258     CattaBrowserEvent event,
259     const char *service_type,
260     const char *domain,
261     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
262     CATTA_GCC_UNUSED void* userdata) {
263
264     catta_log_debug("STB: (%i.%i) %s in <%s> [%s]", iface, protocol, service_type ? service_type : "NULL", domain ? domain : "NULL", browser_event_to_string(event));
265 }
266
267 static void sb_callback(
268     CATTA_GCC_UNUSED CattaSServiceBrowser *b,
269     CattaIfIndex iface,
270     CattaProtocol protocol,
271     CattaBrowserEvent event,
272     const char *name,
273     const char *service_type,
274     const char *domain,
275     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
276     CATTA_GCC_UNUSED void* userdata) {
277     catta_log_debug("SB: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name ? name : "NULL", service_type ? service_type : "NULL", domain ? domain : "NULL", browser_event_to_string(event));
278 }
279
280 static void sr_callback(
281     CATTA_GCC_UNUSED CattaSServiceResolver *r,
282     CattaIfIndex iface,
283     CattaProtocol protocol,
284     CattaResolverEvent event,
285     const char *name,
286     const char*service_type,
287     const char*domain_name,
288     const char*hostname,
289     const CattaAddress *a,
290     uint16_t port,
291     CattaStringList *txt,
292     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
293     CATTA_GCC_UNUSED void* userdata) {
294
295     if (event != CATTA_RESOLVER_FOUND)
296         catta_log_debug("SR: (%i.%i) <%s> as %s in <%s> [%s]", iface, protocol, name, service_type, domain_name, resolver_event_to_string(event));
297     else {
298         char t[CATTA_ADDRESS_STR_MAX], *s;
299
300         catta_address_snprint(t, sizeof(t), a);
301
302         s = catta_string_list_to_string(txt);
303         catta_log_debug("SR: (%i.%i) <%s> as %s in <%s>: %s/%s:%i (%s) [%s]", iface, protocol, name, service_type, domain_name, hostname, t, port, s, resolver_event_to_string(event));
304         catta_free(s);
305     }
306 }
307
308 static void dsb_callback(
309     CATTA_GCC_UNUSED CattaSDNSServerBrowser *b,
310     CattaIfIndex iface,
311     CattaProtocol protocol,
312     CattaBrowserEvent event,
313     const char*hostname,
314     const CattaAddress *a,
315     uint16_t port,
316     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
317     CATTA_GCC_UNUSED void* userdata) {
318
319     char t[CATTA_ADDRESS_STR_MAX] = "n/a";
320
321     if (a)
322         catta_address_snprint(t, sizeof(t), a);
323
324     catta_log_debug("DSB: (%i.%i): %s/%s:%i [%s]", iface, protocol, hostname ? hostname : "NULL", t, port, browser_event_to_string(event));
325 }
326
327 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char *argv[]) {
328     CattaSRecordBrowser *r;
329     CattaSHostNameResolver *hnr;
330     CattaSAddressResolver *ar;
331     CattaKey *k;
332     CattaServerConfig config;
333     CattaAddress a;
334     CattaSDomainBrowser *db;
335     CattaSServiceTypeBrowser *stb;
336     CattaSServiceBrowser *sb;
337     CattaSServiceResolver *sr;
338     CattaSDNSServerBrowser *dsb;
339     CattaSimplePoll *simple_poll;
340     int error;
341     struct timeval tv;
342
343     simple_poll = catta_simple_poll_new();
344     poll_api = catta_simple_poll_get(simple_poll);
345
346     catta_server_config_init(&config);
347
348     catta_address_parse("192.168.50.1", CATTA_PROTO_UNSPEC, &config.wide_area_servers[0]);
349     config.n_wide_area_servers = 1;
350     config.enable_wide_area = 1;
351
352     server = catta_server_new(poll_api, &config, server_callback, NULL, &error);
353     catta_server_config_free(&config);
354
355     k = catta_key_new("_http._tcp.0pointer.de", CATTA_DNS_CLASS_IN, CATTA_DNS_TYPE_PTR);
356     r = catta_s_record_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, k, 0, record_browser_callback, NULL);
357     catta_key_unref(k);
358
359     hnr = catta_s_host_name_resolver_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, "cname.local", CATTA_PROTO_UNSPEC, 0, hnr_callback, NULL);
360
361     ar = catta_s_address_resolver_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, catta_address_parse("192.168.50.1", CATTA_PROTO_INET, &a), 0, ar_callback, NULL);
362
363     db = catta_s_domain_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, NULL, CATTA_DOMAIN_BROWSER_BROWSE, 0, db_callback, NULL);
364
365     stb = catta_s_service_type_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, NULL, 0, stb_callback, NULL);
366
367     sb = catta_s_service_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, "_http._tcp", NULL, 0, sb_callback, NULL);
368
369     sr = catta_s_service_resolver_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, "Ecstasy HTTP", "_http._tcp", "local", CATTA_PROTO_UNSPEC, 0, sr_callback, NULL);
370
371     dsb = catta_s_dns_server_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, "local", CATTA_DNS_SERVER_RESOLVE, CATTA_PROTO_UNSPEC, 0, dsb_callback, NULL);
372
373     catta_elapse_time(&tv, 1000*5, 0);
374     poll_api->timeout_new(poll_api, &tv, dump_timeout_callback, server);
375
376     catta_elapse_time(&tv, 1000*60, 0);
377     poll_api->timeout_new(poll_api, &tv, quit_timeout_callback, simple_poll);
378
379     catta_simple_poll_loop(simple_poll);
380
381     catta_s_record_browser_free(r);
382     catta_s_host_name_resolver_free(hnr);
383     catta_s_address_resolver_free(ar);
384     catta_s_domain_browser_free(db);
385     catta_s_service_type_browser_free(stb);
386     catta_s_service_browser_free(sb);
387     catta_s_service_resolver_free(sr);
388     catta_s_dns_server_browser_free(dsb);
389
390     if (group)
391         catta_s_entry_group_free(group);
392
393     if (server)
394         catta_server_free(server);
395
396     if (simple_poll)
397         catta_simple_poll_free(simple_poll);
398
399     catta_free(service_name);
400
401     return 0;
402 }