]> git.meshlink.io Git - catta/blob - examples/browse-services.c
c58d230299464c7b8f2d6d8073218f7af30c9702
[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         avahi_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, s);
74 }
75
76 int main(int argc, char*argv[]) {
77     AvahiServerConfig config;
78     AvahiServer *server = NULL;
79     AvahiServiceBrowser *sb;
80     gint error;
81     int ret = 1;
82
83     /* Do not publish any local records */
84     avahi_server_config_init(&config);
85     config.publish_hinfo = FALSE;
86     config.publish_addresses = FALSE;
87     config.publish_workstation = FALSE;
88     config.publish_domain = FALSE;
89     
90     /* Allocate a new server */
91     server = avahi_server_new(NULL, &config, NULL, NULL, &error);
92
93     /* Free the configuration data */
94     avahi_server_config_free(&config);
95
96     /* Check wether creating the server object succeeded */
97     if (!server) {
98         g_message("Failed to create server: %s", avahi_strerror(error));
99         goto fail;
100     }
101     
102     /* Create the service browser */
103     sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server);
104     
105     /* Run the main loop */
106     main_loop = g_main_loop_new(NULL, FALSE);
107     g_main_loop_run(main_loop);
108
109     ret = 0;
110     
111 fail:
112     
113     /* Cleanup things */
114     if (sb)
115         avahi_service_browser_free(sb);
116     
117     if (server)
118         avahi_server_free(server);
119
120     if (main_loop)
121         g_main_loop_unref(main_loop);
122
123     return ret;
124 }