1 /***
2 This file is part of avahi.
3
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include <avahi-common/domain.h>
27 #include "avahi-common/avahi-malloc.h"
28 #include <avahi-common/error.h>
29
30 #include "browse.h"
31 #include "log.h"
32
33 struct AvahiSServiceBrowser {
34 AvahiServer *server;
35 char *domain_name;
36 char *service_type;
37
38 AvahiSRecordBrowser *record_browser;
39
40 AvahiSServiceBrowserCallback callback;
41 void* userdata;
42
43 AVAHI_LLIST_FIELDS(AvahiSServiceBrowser, browser);
44 };
45
record_browser_callback(AvahiSRecordBrowser * rr,AvahiIfIndex interface,AvahiProtocol protocol,AvahiBrowserEvent event,AvahiRecord * record,AvahiLookupResultFlags flags,void * userdata)46 static void record_browser_callback(
47 AvahiSRecordBrowser*rr,
48 AvahiIfIndex interface,
49 AvahiProtocol protocol,
50 AvahiBrowserEvent event,
51 AvahiRecord *record,
52 AvahiLookupResultFlags flags,
53 void* userdata) {
54
55 AvahiSServiceBrowser *b = userdata;
56
57 assert(rr);
58 assert(b);
59
60 /* Filter flags */
61 flags &= AVAHI_LOOKUP_RESULT_CACHED | AVAHI_LOOKUP_RESULT_MULTICAST | AVAHI_LOOKUP_RESULT_WIDE_AREA;
62
63 if (record) {
64 char service[AVAHI_LABEL_MAX], type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
65
66 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
67
68 if (event == AVAHI_BROWSER_NEW && avahi_server_is_service_local(b->server, interface, protocol, record->data.ptr.name))
69 flags |= AVAHI_LOOKUP_RESULT_LOCAL;
70
71 if (avahi_service_name_split(record->data.ptr.name, service, sizeof(service), type, sizeof(type), domain, sizeof(domain)) < 0) {
72 avahi_log_warn("Failed to split '%s'", record->key->name);
73 return;
74 }
75
76 b->callback(b, interface, protocol, event, service, type, domain, flags, b->userdata);
77
78 } else
79 b->callback(b, interface, protocol, event, NULL, b->service_type, b->domain_name, flags, b->userdata);
80
81 }
82
avahi_s_service_browser_new(AvahiServer * server,AvahiIfIndex interface,AvahiProtocol protocol,const char * service_type,const char * domain,AvahiLookupFlags flags,AvahiSServiceBrowserCallback callback,void * userdata)83 AvahiSServiceBrowser *avahi_s_service_browser_new(
84 AvahiServer *server,
85 AvahiIfIndex interface,
86 AvahiProtocol protocol,
87 const char *service_type,
88 const char *domain,
89 AvahiLookupFlags flags,
90 AvahiSServiceBrowserCallback callback,
91 void* userdata) {
92
93 AvahiSServiceBrowser *b;
94 AvahiKey *k = NULL;
95 char n[AVAHI_DOMAIN_NAME_MAX];
96 int r;
97
98 assert(server);
99 assert(callback);
100 assert(service_type);
101
102 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
103 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
104 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
105 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
106 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_service_type_generic(service_type), AVAHI_ERR_INVALID_SERVICE_TYPE);
107
108 if (!domain)
109 domain = server->domain_name;
110
111 if ((r = avahi_service_name_join(n, sizeof(n), NULL, service_type, domain)) < 0) {
112 avahi_server_set_errno(server, r);
113 return NULL;
114 }
115
116 if (!(b = avahi_new(AvahiSServiceBrowser, 1))) {
117 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
118 return NULL;
119 }
120
121 b->server = server;
122 b->domain_name = b->service_type = NULL;
123 b->callback = callback;
124 b->userdata = userdata;
125 b->record_browser = NULL;
126
127 AVAHI_LLIST_PREPEND(AvahiSServiceBrowser, browser, server->service_browsers, b);
128
129 if (!(b->domain_name = avahi_normalize_name_strdup(domain)) ||
130 !(b->service_type = avahi_normalize_name_strdup(service_type))) {
131 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
132 goto fail;
133 }
134
135 if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
136 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
137 goto fail;
138 }
139
140 if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
141 goto fail;
142
143 avahi_key_unref(k);
144
145 return b;
146
147 fail:
148
149 if (k)
150 avahi_key_unref(k);
151
152 avahi_s_service_browser_free(b);
153 return NULL;
154 }
155
avahi_s_service_browser_free(AvahiSServiceBrowser * b)156 void avahi_s_service_browser_free(AvahiSServiceBrowser *b) {
157 assert(b);
158
159 AVAHI_LLIST_REMOVE(AvahiSServiceBrowser, browser, b->server->service_browsers, b);
160
161 if (b->record_browser)
162 avahi_s_record_browser_free(b->record_browser);
163
164 avahi_free(b->domain_name);
165 avahi_free(b->service_type);
166 avahi_free(b);
167 }
168