]> git.meshlink.io Git - catta/blob - examples/browse-services.c
f2707fdd1e798c100750dec03aabf8dd739575ed
[catta] / examples / 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 <avahi-core/core.h>
27
28 static GMainLoop *main_loop = NULL;
29
30 static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *name,const gchar *type,const gchar *domain, const gchar *host_name, const AvahiAddress *address, guint16 port, AvahiStringList *txt, gpointer userdata) {
31     g_assert(r);
32
33     /* Called whenever a service has been resolved successfully or timed out */
34
35     if (event == AVAHI_RESOLVER_TIMEOUT)
36         g_message("Failed to resolve service '%s' of type '%s' in domain '%s'.", name, type, domain);
37     else {
38         gchar a[128], *t;
39
40         g_assert(event == AVAHI_RESOLVER_FOUND);
41         
42         g_message("Service '%s' of type '%s' in domain '%s':", name, type, domain);
43
44         avahi_address_snprint(a, sizeof(a), address);
45         t = avahi_string_list_to_string(txt);
46         g_message("\t%s:%u (%s) TXT=%s", host_name, port, a, t);
47         g_free(t);
48     }
49
50     avahi_service_resolver_free(r);
51 }
52
53 static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
54     g_assert(b);
55     
56     AvahiServer *s = userdata;
57
58     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
59
60     g_message("%s: service '%s' of type '%s' in domain '%s'",
61               event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED",
62               name,
63               type,
64               domain);
65
66     /* If it's new, let's resolve it */
67     if (event == AVAHI_BROWSER_NEW)
68         
69         /* We ignore the returned resolver object. In the callback function
70         we free it. If the server is terminated before the callback
71         function is called the server will free the resolver for us. */
72
73         if (!(avahi_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s)))
74             g_message("Failed to resolve service '%s': %s", name, avahi_strerror(avahi_server_errno(s)));
75 }
76
77 int main(int argc, char*argv[]) {
78     AvahiServerConfig config;
79     AvahiServer *server = NULL;
80     AvahiServiceBrowser *sb;
81     gint error;
82     int ret = 1;
83
84     /* Do not publish any local records */
85     avahi_server_config_init(&config);
86     config.publish_hinfo = FALSE;
87     config.publish_addresses = FALSE;
88     config.publish_workstation = FALSE;
89     config.publish_domain = FALSE;
90     
91     /* Allocate a new server */
92     server = avahi_server_new(NULL, &config, NULL, NULL, &error);
93
94     /* Free the configuration data */
95     avahi_server_config_free(&config);
96
97     /* Check wether creating the server object succeeded */
98     if (!server) {
99         g_message("Failed to create server: %s", avahi_strerror(error));
100         goto fail;
101     }
102     
103     /* Create the service browser */
104     if (!(sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server))) {
105         g_message("Failed to create service browser: %s", avahi_strerror(avahi_server_errno(server)));
106         goto fail;
107     }
108     
109     /* Run the main loop */
110     main_loop = g_main_loop_new(NULL, FALSE);
111     g_main_loop_run(main_loop);
112
113     ret = 0;
114     
115 fail:
116     
117     /* Cleanup things */
118     if (sb)
119         avahi_service_browser_free(sb);
120     
121     if (server)
122         avahi_server_free(server);
123
124     if (main_loop)
125         g_main_loop_unref(main_loop);
126
127     return ret;
128 }