]> git.meshlink.io Git - catta/blob - examples/client-browse-services.c
* Add custom user-specific configure flags to bootstrap.sh
[catta] / examples / client-browse-services.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <time.h>
30
31 #include <avahi-client/client.h>
32 #include <avahi-client/lookup.h>
33
34 #include <avahi-common/simple-watch.h>
35 #include <avahi-common/malloc.h>
36 #include <avahi-common/error.h>
37
38 static AvahiSimplePoll *simple_poll = NULL;
39
40 static void resolve_callback(
41     AvahiServiceResolver *r,
42     AvahiIfIndex interface,
43     AvahiProtocol protocol,
44     AvahiResolverEvent event,
45     const char *name,
46     const char *type,
47     const char *domain,
48     const char *host_name,
49     const AvahiAddress *address,
50     uint16_t port,
51     AvahiStringList *txt,
52     AvahiLookupResultFlags flags,
53     void* userdata) {
54
55     assert(r);
56
57     /* Called whenever a service has been resolved successfully or timed out */
58
59     switch (event) {
60         case AVAHI_RESOLVER_FAILURE:
61             fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
62             break;
63
64         case AVAHI_RESOLVER_FOUND: {
65             char a[AVAHI_ADDRESS_STR_MAX], *t;
66             
67             fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
68             
69             avahi_address_snprint(a, sizeof(a), address);
70             t = avahi_string_list_to_string(txt);
71             fprintf(stderr,
72                     "\t%s:%u (%s)\n"
73                     "\tTXT=%s\n"
74                     "\tcookie is %u\n"
75                     "\tis_local: %i\n"
76                     "\twide_area: %i\n"
77                     "\tmulticast: %i\n"
78                     "\tcached: %i\n",
79                     host_name, port, a,
80                     t,
81                     avahi_string_list_get_service_cookie(txt),
82                     avahi_client_is_service_local(avahi_service_resolver_get_client(r), interface, protocol, name, type, domain),
83                     !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
84                     !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
85                     !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
86                 
87             avahi_free(t);
88         }
89     }
90
91     avahi_service_resolver_free(r);
92 }
93
94 static void browse_callback(
95     AvahiServiceBrowser *b,
96     AvahiIfIndex interface,
97     AvahiProtocol protocol,
98     AvahiBrowserEvent event,
99     const char *name,
100     const char *type,
101     const char *domain,
102     AvahiLookupResultFlags flags,
103     void* userdata) {
104     
105     AvahiClient *c = userdata;
106     assert(b);
107
108     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
109
110     switch (event) {
111         case AVAHI_BROWSER_FAILURE:
112             
113             fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
114             avahi_simple_poll_quit(simple_poll);
115             return;
116
117         case AVAHI_BROWSER_NEW:
118             fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
119
120             /* We ignore the returned resolver object. In the callback
121                function we free it. If the server is terminated before
122                the callback function is called the server will free
123                the resolver for us. */
124
125             if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
126                 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
127             
128             break;
129
130         case AVAHI_BROWSER_REMOVE:
131             fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
132             break;
133
134         case AVAHI_BROWSER_ALL_FOR_NOW:
135         case AVAHI_BROWSER_CACHE_EXHAUSTED:
136             fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
137             break;
138
139     }
140 }
141
142 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
143     assert(c);
144
145     /* Called whenever the client or server state changes */
146
147     if (state == AVAHI_CLIENT_DISCONNECTED) {
148         fprintf(stderr, "Server connection terminated.\n");
149         avahi_simple_poll_quit(simple_poll);
150     }
151 }
152
153 int main(int argc, char*argv[]) {
154     AvahiClient *client = NULL;
155     AvahiServiceBrowser *sb = NULL;
156     int error;
157     int ret = 1;
158
159     /* Allocate main loop object */
160     if (!(simple_poll = avahi_simple_poll_new())) {
161         fprintf(stderr, "Failed to create simple poll object.\n");
162         goto fail;
163     }
164
165     /* Allocate a new client */
166     client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error);
167
168     /* Check wether creating the client object succeeded */
169     if (!client) {
170         fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
171         goto fail;
172     }
173     
174     /* Create the service browser */
175     if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, browse_callback, client))) {
176         fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
177         goto fail;
178     }
179     
180     /* Run the main loop */
181     for (;;)
182         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
183             break;
184     
185     ret = 0;
186     
187 fail:
188     
189     /* Cleanup things */
190     if (sb)
191         avahi_service_browser_free(sb);
192     
193     if (client)
194         avahi_client_free(client);
195
196     if (simple_poll)
197         avahi_simple_poll_free(simple_poll);
198
199     return ret;
200 }