]> git.meshlink.io Git - catta/blob - examples/core-browse-services.c
update examples to use xxx_is_service_local()
[catta] / examples / core-browse-services.c
1 /* $Id$ */
2
3 /* PLEASE NOTE *
4  * This file demonstrates how to use Avahi's core API, this is
5  * the embeddable mDNS stack for embedded applications.
6  *
7  * End user applications should *not* use this API and should use
8  * the DBUS or C APIs, please see
9  * client-browse-services.c and glib-integration.c
10  * 
11  * I repeat, you probably do *not* want to use this example.
12  */
13
14 /***
15   This file is part of avahi.
16  
17   avahi is free software; you can redistribute it and/or modify it
18   under the terms of the GNU Lesser General Public License as
19   published by the Free Software Foundation; either version 2.1 of the
20   License, or (at your option) any later version.
21  
22   avahi is distributed in the hope that it will be useful, but WITHOUT
23   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
25   Public License for more details.
26  
27   You should have received a copy of the GNU Lesser General Public
28   License along with avahi; if not, write to the Free Software
29   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30   USA.
31 ***/
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdio.h>
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <time.h>
41
42 #include <avahi-core/core.h>
43 #include <avahi-common/simple-watch.h>
44 #include <avahi-common/malloc.h>
45 #include <avahi-common/error.h>
46
47 static AvahiSimplePoll *simple_poll = NULL;
48 static AvahiServer *server = NULL;
49
50 static void resolve_callback(
51     AvahiSServiceResolver *r,
52     AvahiIfIndex interface,
53     AvahiProtocol protocol,
54     AvahiResolverEvent event,
55     const char *name,
56     const char *type,
57     const char *domain,
58     const char *host_name,
59     const AvahiAddress *address,
60     uint16_t port,
61     AvahiStringList *txt,
62     void* userdata) {
63     
64     assert(r);
65
66     /* Called whenever a service has been resolved successfully or timed out */
67
68     if (event == AVAHI_RESOLVER_TIMEOUT)
69         fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain);
70     else {
71         char a[128], *t;
72
73         assert(event == AVAHI_RESOLVER_FOUND);
74         
75         fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
76
77         avahi_address_snprint(a, sizeof(a), address);
78         t = avahi_string_list_to_string(txt);
79         fprintf(stderr,
80                 "\t%s:%u (%s)\n"
81                 "\tTXT=%s\n"
82                 "\tcookie is %u\n"
83                 "\tis_local: %i\n",
84                 host_name, port, a,
85                 t,
86                 avahi_string_list_get_service_cookie(txt),
87                 avahi_server_is_service_local(server, interface, protocol, name, type, domain));
88         avahi_free(t);
89     }
90
91     avahi_s_service_resolver_free(r);
92 }
93
94 static void browse_callback(
95     AvahiSServiceBrowser *b,
96     AvahiIfIndex interface,
97     AvahiProtocol protocol,
98     AvahiBrowserEvent event,
99     const char *name,
100     const char *type,
101     const char *domain,
102     void* userdata) {
103     
104     AvahiServer *s = userdata;
105     assert(b);
106
107     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
108
109     fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n",
110             event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
111             name,
112             type,
113             domain);
114     
115     /* If it's new, let's resolve it */
116     if (event == AVAHI_BROWSER_NEW)
117         
118         /* We ignore the returned resolver object. In the callback function
119         we free it. If the server is terminated before the callback
120         function is called the server will free the resolver for us. */
121
122         if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s)))
123             fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
124 }
125
126 int main(int argc, char*argv[]) {
127     AvahiServerConfig config;
128     AvahiSServiceBrowser *sb;
129     int error;
130     int ret = 1;
131
132     /* Initialize the psuedo-RNG */
133     srand(time(NULL));
134
135     /* Allocate main loop object */
136     if (!(simple_poll = avahi_simple_poll_new())) {
137         fprintf(stderr, "Failed to create simple poll object.\n");
138         goto fail;
139     }
140
141     /* Do not publish any local records */
142     avahi_server_config_init(&config);
143     config.publish_hinfo = 0;
144     config.publish_addresses = 0;
145     config.publish_workstation = 0;
146     config.publish_domain = 0;
147     
148     /* Allocate a new server */
149     server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
150
151     /* Free the configuration data */
152     avahi_server_config_free(&config);
153
154     /* Check wether creating the server object succeeded */
155     if (!server) {
156         fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
157         goto fail;
158     }
159     
160     /* Create the service browser */
161     if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server))) {
162         fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
163         goto fail;
164     }
165     
166     /* Run the main loop */
167     for (;;)
168         if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
169             break;
170     
171     ret = 0;
172     
173 fail:
174     
175     /* Cleanup things */
176     if (sb)
177         avahi_s_service_browser_free(sb);
178     
179     if (server)
180         avahi_server_free(server);
181
182     if (simple_poll)
183         avahi_simple_poll_free(simple_poll);
184
185     return ret;
186 }