]> git.meshlink.io Git - catta/blob - avahi-client/resolver.c
* Disable debug output of avahi-client
[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, n;
97             uint8_t *k;
98             
99             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
100                 break;
101             
102             assert(at == DBUS_TYPE_ARRAY);
103             
104             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
105                 fprintf(stderr, "Error parsing service resolving message");
106                 goto fail;
107             }
108             
109             dbus_message_iter_recurse(&sub, &sub2);
110             dbus_message_iter_get_fixed_array(&sub2, &k, &n);
111             strlst = avahi_string_list_add_arbitrary(strlst, k, n);
112             
113             dbus_message_iter_next(&sub);
114         }
115
116         assert(address);
117         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
118             fprintf(stderr, "Failed to parse address\n");
119             goto fail;
120         }
121     
122         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
123
124     } else {
125
126         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
127
128         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
129
130         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
131     }
132
133 fail:
134     
135     if (message)
136         dbus_message_unref(message);
137     
138     avahi_string_list_free(strlst);
139     
140     dbus_error_free (&error);
141 }
142
143 AvahiServiceResolver * avahi_service_resolver_new(
144     AvahiClient *client,
145     AvahiIfIndex interface,
146     AvahiProtocol protocol,
147     const char *name,
148     const char *type,
149     const char *domain,
150     AvahiProtocol aprotocol,
151     AvahiServiceResolverCallback callback,
152     void *userdata) {
153
154     DBusError error;
155     AvahiServiceResolver *r;
156     DBusMessage *message;
157     int32_t i_interface, i_protocol, i_aprotocol;
158     
159     assert(client);
160     assert(name);
161     assert(type);
162
163     if (!domain)
164         domain = "";
165     
166     dbus_error_init (&error);
167
168     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
169         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
170         goto fail;
171     }
172
173     if (!(r = avahi_new(AvahiServiceResolver, 1))) {
174         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
175         goto fail;
176     }
177
178     r->client = client;
179     r->callback = callback;
180     r->userdata = userdata;
181     r->call = NULL;
182     
183     AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
184
185     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) {
186         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
187         goto fail;
188     }
189
190     i_interface = interface;
191     i_protocol = protocol;
192     i_aprotocol = aprotocol;
193
194     if (!(dbus_message_append_args(
195               message,
196               DBUS_TYPE_INT32, &i_interface,
197               DBUS_TYPE_INT32, &i_protocol,
198               DBUS_TYPE_STRING, &name,
199               DBUS_TYPE_STRING, &type,
200               DBUS_TYPE_STRING, &domain,
201               DBUS_TYPE_INT32, &i_aprotocol,
202               DBUS_TYPE_INVALID))) {
203         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
204         goto fail;
205     }
206
207     if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
208         !dbus_pending_call_set_notify(r->call, pending_call_callback, r, NULL)) {
209         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
210         goto fail;
211     }
212
213     dbus_message_unref(message);
214
215     return r;
216     
217 fail:
218
219     if (dbus_error_is_set(&error)) {
220         avahi_client_set_dbus_error(client, &error);
221         dbus_error_free(&error);
222     }
223
224     if (r)
225         avahi_service_resolver_free(r);
226     
227     if (message)
228         dbus_message_unref(message);
229
230     return NULL;
231
232 }
233
234 int avahi_service_resolver_free(AvahiServiceResolver *r) {
235     AvahiClient *client;
236
237     assert(r);
238     client = r->client;
239
240     if (r->call) {
241         dbus_pending_call_cancel(r->call);
242         dbus_pending_call_unref(r->call);
243     }
244
245     AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
246
247     avahi_free(r);
248
249     return AVAHI_OK;
250 }
251
252 int avahi_service_resolver_block(AvahiServiceResolver *r) {
253     AvahiClient *client;
254
255     assert(r);
256     client = r->client;
257
258     if (r->call)
259         dbus_pending_call_block(r->call);
260
261     return AVAHI_OK;
262 }
263