]> git.meshlink.io Git - catta/blob - examples/core-browse-services.c
rename everything avahi to catta
[catta] / examples / core-browse-services.c
1 /* PLEASE NOTE *
2  * This file demonstrates how to use Catta's core API, this is
3  * the embeddable mDNS stack for embedded applications.
4  *
5  * End user applications should *not* use this API and should use
6  * the D-Bus or C APIs, please see
7  * client-browse-services.c and glib-integration.c
8  *
9  * I repeat, you probably do *not* want to use this example.
10  */
11
12 /***
13   This file is part of catta.
14
15   catta is free software; you can redistribute it and/or modify it
16   under the terms of the GNU Lesser General Public License as
17   published by the Free Software Foundation; either version 2.1 of the
18   License, or (at your option) any later version.
19
20   catta is distributed in the hope that it will be useful, but WITHOUT
21   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
23   Public License for more details.
24
25   You should have received a copy of the GNU Lesser General Public
26   License along with catta; if not, write to the Free Software
27   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28   USA.
29 ***/
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <time.h>
39
40 #include <catta/core.h>
41 #include <catta/lookup.h>
42 #include <catta/simple-watch.h>
43 #include <catta/malloc.h>
44 #include <catta/error.h>
45
46 static CattaSimplePoll *simple_poll = NULL;
47 static CattaServer *server = NULL;
48
49 static void resolve_callback(
50     CattaSServiceResolver *r,
51     CATTA_GCC_UNUSED CattaIfIndex interface,
52     CATTA_GCC_UNUSED CattaProtocol protocol,
53     CattaResolverEvent event,
54     const char *name,
55     const char *type,
56     const char *domain,
57     const char *host_name,
58     const CattaAddress *address,
59     uint16_t port,
60     CattaStringList *txt,
61     CattaLookupResultFlags flags,
62     CATTA_GCC_UNUSED void* userdata) {
63
64     assert(r);
65
66     /* Called whenever a service has been resolved successfully or timed out */
67
68     switch (event) {
69         case CATTA_RESOLVER_FAILURE:
70             fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, catta_strerror(catta_server_errno(server)));
71             break;
72
73         case CATTA_RESOLVER_FOUND: {
74             char a[CATTA_ADDRESS_STR_MAX], *t;
75
76             fprintf(stderr, "(Resolver) Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
77
78             catta_address_snprint(a, sizeof(a), address);
79             t = catta_string_list_to_string(txt);
80             fprintf(stderr,
81                     "\t%s:%u (%s)\n"
82                     "\tTXT=%s\n"
83                     "\tcookie is %u\n"
84                     "\tis_local: %i\n"
85                     "\twide_area: %i\n"
86                     "\tmulticast: %i\n"
87                     "\tcached: %i\n",
88                     host_name, port, a,
89                     t,
90                     catta_string_list_get_service_cookie(txt),
91                     !!(flags & CATTA_LOOKUP_RESULT_LOCAL),
92                     !!(flags & CATTA_LOOKUP_RESULT_WIDE_AREA),
93                     !!(flags & CATTA_LOOKUP_RESULT_MULTICAST),
94                     !!(flags & CATTA_LOOKUP_RESULT_CACHED));
95             catta_free(t);
96         }
97     }
98
99     catta_s_service_resolver_free(r);
100 }
101
102 static void browse_callback(
103     CattaSServiceBrowser *b,
104     CattaIfIndex interface,
105     CattaProtocol protocol,
106     CattaBrowserEvent event,
107     const char *name,
108     const char *type,
109     const char *domain,
110     CATTA_GCC_UNUSED CattaLookupResultFlags flags,
111     void* userdata) {
112
113     CattaServer *s = userdata;
114     assert(b);
115
116     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
117
118     switch (event) {
119
120         case CATTA_BROWSER_FAILURE:
121
122             fprintf(stderr, "(Browser) %s\n", catta_strerror(catta_server_errno(server)));
123             catta_simple_poll_quit(simple_poll);
124             return;
125
126         case CATTA_BROWSER_NEW:
127             fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
128
129             /* We ignore the returned resolver object. In the callback
130                function we free it. If the server is terminated before
131                the callback function is called the server will free
132                the resolver for us. */
133
134             if (!(catta_s_service_resolver_new(s, interface, protocol, name, type, domain, CATTA_PROTO_UNSPEC, 0, resolve_callback, s)))
135                 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, catta_strerror(catta_server_errno(s)));
136
137             break;
138
139         case CATTA_BROWSER_REMOVE:
140             fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
141             break;
142
143         case CATTA_BROWSER_ALL_FOR_NOW:
144         case CATTA_BROWSER_CACHE_EXHAUSTED:
145             fprintf(stderr, "(Browser) %s\n", event == CATTA_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
146             break;
147     }
148 }
149
150 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char*argv[]) {
151     CattaServerConfig config;
152     CattaSServiceBrowser *sb = NULL;
153     int error;
154     int ret = 1;
155
156     /* Initialize the psuedo-RNG */
157     srand(time(NULL));
158
159     /* Allocate main loop object */
160     if (!(simple_poll = catta_simple_poll_new())) {
161         fprintf(stderr, "Failed to create simple poll object.\n");
162         goto fail;
163     }
164
165     /* Do not publish any local records */
166     catta_server_config_init(&config);
167     config.publish_hinfo = 0;
168     config.publish_addresses = 0;
169     config.publish_workstation = 0;
170     config.publish_domain = 0;
171
172     /* Set a unicast DNS server for wide area DNS-SD */
173     catta_address_parse("192.168.50.1", CATTA_PROTO_UNSPEC, &config.wide_area_servers[0]);
174     config.n_wide_area_servers = 1;
175     config.enable_wide_area = 1;
176
177     /* Allocate a new server */
178     server = catta_server_new(catta_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
179
180     /* Free the configuration data */
181     catta_server_config_free(&config);
182
183     /* Check wether creating the server object succeeded */
184     if (!server) {
185         fprintf(stderr, "Failed to create server: %s\n", catta_strerror(error));
186         goto fail;
187     }
188
189     /* Create the service browser */
190     if (!(sb = catta_s_service_browser_new(server, CATTA_IF_UNSPEC, CATTA_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, browse_callback, server))) {
191         fprintf(stderr, "Failed to create service browser: %s\n", catta_strerror(catta_server_errno(server)));
192         goto fail;
193     }
194
195     /* Run the main loop */
196     catta_simple_poll_loop(simple_poll);
197
198     ret = 0;
199
200 fail:
201
202     /* Cleanup things */
203     if (sb)
204         catta_s_service_browser_free(sb);
205
206     if (server)
207         catta_server_free(server);
208
209     if (simple_poll)
210         catta_simple_poll_free(simple_poll);
211
212     return ret;
213 }