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