]> git.meshlink.io Git - catta/blob - examples/client-browse-services.c
75379405021c174eea40161e331e4e73d342a514
[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-common/simple-watch.h>
33 #include <avahi-common/malloc.h>
34 #include <avahi-common/error.h>
35
36 static AvahiSimplePoll *simple_poll = NULL;
37
38 static void resolve_callback(
39     AvahiServiceResolver *r,
40     AvahiIfIndex interface,
41     AvahiProtocol protocol,
42     AvahiResolverEvent event,
43     const char *name,
44     const char *type,
45     const char *domain,
46     const char *host_name,
47     const AvahiAddress *address,
48     uint16_t port,
49     AvahiStringList *txt,
50     void* userdata) {
51
52     assert(r);
53
54     /* Called whenever a service has been resolved successfully or timed out */
55
56     if (event == AVAHI_RESOLVER_TIMEOUT)
57         fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain);
58     else {
59         char a[128], *t;
60
61         assert(event == AVAHI_RESOLVER_FOUND);
62         
63         fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
64
65         avahi_address_snprint(a, sizeof(a), address);
66         t = avahi_string_list_to_string(txt);
67         fprintf(stderr,
68                 "\t%s:%u (%s)\n"
69                 "\tTXT=%s\n"
70                 "\tcookie is %u\n"
71                 "\tis_local: %i\n",
72                 host_name, port, a,
73                 t,
74                 avahi_string_list_get_service_cookie(txt),
75                 avahi_client_is_service_local(avahi_service_resolver_get_client(r), interface, protocol, name, type, domain));
76         avahi_free(t);
77     }
78
79     avahi_service_resolver_free(r);
80 }
81
82 static void browse_callback(
83     AvahiServiceBrowser *b,
84     AvahiIfIndex interface,
85     AvahiProtocol protocol,
86     AvahiBrowserEvent event,
87     const char *name,
88     const char *type,
89     const char *domain,
90     void* userdata) {
91     
92     AvahiClient *c = userdata;
93     assert(b);
94
95     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
96
97     fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n",
98             event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
99             name,
100             type,
101             domain);
102     
103     /* If it's new, let's resolve it */
104     if (event == AVAHI_BROWSER_NEW)
105         
106         /* We ignore the returned resolver object. In the callback function
107         we free it. If the server is terminated before the callback
108         function is called the server will free the resolver for us. */
109
110         if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, c)))
111             fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
112 }
113
114 static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
115     assert(c);
116
117     /* Called whenever the client or server state changes */
118
119     if (state == AVAHI_CLIENT_DISCONNECTED) {
120         fprintf(stderr, "Server connection terminated.\n");
121         avahi_simple_poll_quit(simple_poll);
122     }
123 }
124
125 int main(int argc, char*argv[]) {
126     AvahiClient *client = NULL;
127     AvahiServiceBrowser *sb;
128     int error;
129     int ret = 1;
130
131     /* Allocate main loop object */
132     if (!(simple_poll = avahi_simple_poll_new())) {
133         fprintf(stderr, "Failed to create simple poll object.\n");
134         goto fail;
135     }
136
137     /* Allocate a new client */
138     client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error);
139
140     /* Check wether creating the client object succeeded */
141     if (!client) {
142         fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
143         goto fail;
144     }
145     
146     /* Create the service browser */
147     if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, client))) {
148         fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
149         goto fail;
150     }
151     
152     /* Run the main loop */
153     for (;;)
154         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
155             break;
156     
157     ret = 0;
158     
159 fail:
160     
161     /* Cleanup things */
162     if (sb)
163         avahi_service_browser_free(sb);
164     
165     if (client)
166         avahi_client_free(client);
167
168     if (simple_poll)
169         avahi_simple_poll_free(simple_poll);
170
171     return ret;
172 }