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