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