]> git.meshlink.io Git - catta/blob - avahi-client/resolver.c
* fix "sebest's bug" (TM) -- deal with dbus brokeness when passing zero length arrays
[catta] / avahi-client / resolver.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 <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 #include <avahi-client/client.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/llist.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
37
38 #include "client.h"
39 #include "internal.h"
40
41 static void pending_call_callback(DBusPendingCall *pending, void *userdata) {
42     AvahiServiceResolver *r =  userdata;
43     DBusMessage *message = NULL;
44     AvahiStringList *strlst = NULL;
45     DBusError error;
46     
47     assert(pending);
48     assert(r);
49
50     dbus_error_init(&error);
51
52     if (!(message = dbus_pending_call_steal_reply(pending)))
53         goto fail;
54
55     if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
56         int j;
57         int32_t interface, protocol, aprotocol;
58         char *name, *type, *domain, *host, *address;
59         uint16_t port;
60         DBusMessageIter iter, sub;
61         AvahiAddress a;
62         
63         if (!dbus_message_get_args(
64                 message, &error,
65                 DBUS_TYPE_INT32, &interface,
66                 DBUS_TYPE_INT32, &protocol,
67                 DBUS_TYPE_STRING, &name,
68                 DBUS_TYPE_STRING, &type,
69                 DBUS_TYPE_STRING, &domain,
70                 DBUS_TYPE_STRING, &host,
71                 DBUS_TYPE_INT32, &aprotocol,
72                 DBUS_TYPE_STRING, &address,
73                 DBUS_TYPE_UINT16, &port,
74                 DBUS_TYPE_INVALID) ||
75             dbus_error_is_set (&error)) {
76             fprintf(stderr, "Failed to parse resolver event.\n");
77             goto fail;
78         }
79         
80         dbus_message_iter_init(message, &iter);
81         
82         for (j = 0; j < 9; j++)
83             dbus_message_iter_next(&iter);
84         
85         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
86             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
87             fprintf(stderr, "Error parsing service resolving message");
88             goto fail;
89         }
90         
91         strlst = NULL;
92         dbus_message_iter_recurse(&iter, &sub);
93         
94         for (;;) {
95             DBusMessageIter sub2;
96             int at;
97             
98             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
99                 break;
100             
101             assert(at == DBUS_TYPE_ARRAY);
102             
103             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
104                 fprintf(stderr, "Error parsing service resolving message");
105                 goto fail;
106             }
107             
108             dbus_message_iter_recurse(&sub, &sub2);
109
110             if (dbus_message_iter_get_array_len(&sub2) > 0) {
111                 uint8_t *k;
112                 int n;
113                 
114                 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
115                 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
116             }
117             
118             dbus_message_iter_next(&sub);
119         }
120
121         assert(address);
122         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
123             fprintf(stderr, "Failed to parse address\n");
124             goto fail;
125         }
126     
127         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
128
129     } else {
130
131         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
132
133         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
134
135         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
136     }
137
138 fail:
139     
140     if (message)
141         dbus_message_unref(message);
142     
143     avahi_string_list_free(strlst);
144     
145     dbus_error_free (&error);
146 }
147
148 AvahiServiceResolver * avahi_service_resolver_new(
149     AvahiClient *client,
150     AvahiIfIndex interface,
151     AvahiProtocol protocol,
152     const char *name,
153     const char *type,
154     const char *domain,
155     AvahiProtocol aprotocol,
156     AvahiServiceResolverCallback callback,
157     void *userdata) {
158
159     DBusError error;
160     AvahiServiceResolver *r;
161     DBusMessage *message;
162     int32_t i_interface, i_protocol, i_aprotocol;
163     
164     assert(client);
165     assert(name);
166     assert(type);
167
168     if (!domain)
169         domain = "";
170     
171     dbus_error_init (&error);
172
173     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
174         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
175         goto fail;
176     }
177
178     if (!(r = avahi_new(AvahiServiceResolver, 1))) {
179         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
180         goto fail;
181     }
182
183     r->client = client;
184     r->callback = callback;
185     r->userdata = userdata;
186     r->call = NULL;
187     
188     AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
189
190     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) {
191         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
192         goto fail;
193     }
194
195     i_interface = interface;
196     i_protocol = protocol;
197     i_aprotocol = aprotocol;
198
199     if (!(dbus_message_append_args(
200               message,
201               DBUS_TYPE_INT32, &i_interface,
202               DBUS_TYPE_INT32, &i_protocol,
203               DBUS_TYPE_STRING, &name,
204               DBUS_TYPE_STRING, &type,
205               DBUS_TYPE_STRING, &domain,
206               DBUS_TYPE_INT32, &i_aprotocol,
207               DBUS_TYPE_INVALID))) {
208         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
209         goto fail;
210     }
211
212     if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
213         !dbus_pending_call_set_notify(r->call, pending_call_callback, r, NULL)) {
214         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
215         goto fail;
216     }
217
218     dbus_message_unref(message);
219
220     return r;
221     
222 fail:
223
224     if (dbus_error_is_set(&error)) {
225         avahi_client_set_dbus_error(client, &error);
226         dbus_error_free(&error);
227     }
228
229     if (r)
230         avahi_service_resolver_free(r);
231     
232     if (message)
233         dbus_message_unref(message);
234
235     return NULL;
236
237 }
238
239 int avahi_service_resolver_free(AvahiServiceResolver *r) {
240     AvahiClient *client;
241
242     assert(r);
243     client = r->client;
244
245     if (r->call) {
246         dbus_pending_call_cancel(r->call);
247         dbus_pending_call_unref(r->call);
248     }
249
250     AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
251
252     avahi_free(r);
253
254     return AVAHI_OK;
255 }
256
257 int avahi_service_resolver_block(AvahiServiceResolver *r) {
258     AvahiClient *client;
259
260     assert(r);
261     client = r->client;
262
263     if (r->call)
264         dbus_pending_call_block(r->call);
265
266     return AVAHI_OK;
267 }
268