]> git.meshlink.io Git - catta/blob - examples/browse-services.c
* add C examples
[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
81     /* Do not publish any local records */
82     avahi_server_config_init(&config);
83     config.publish_hinfo = FALSE;
84     config.publish_addresses = FALSE;
85     config.publish_workstation = FALSE;
86     config.publish_domain = FALSE;
87     
88     /* Allocate a new server */
89     server = avahi_server_new(NULL, &config, NULL, NULL);
90
91     /* Free the configuration data */
92     avahi_server_config_free(&config);
93
94     /* Create the service browser */
95     sb = avahi_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, server);
96     
97     /* Run the main loop */
98     main_loop = g_main_loop_new(NULL, FALSE);
99     g_main_loop_run(main_loop);
100
101     /* Cleanup things */
102     if (sb)
103         avahi_service_browser_free(sb);
104     
105     if (server)
106         avahi_server_free(server);
107
108     if (main_loop)
109         g_main_loop_unref(main_loop);
110
111     return 0;
112 }