| 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 <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <dbus/dbus.h> |
| 29 |
|
| 30 |
#include "client.h" |
| 31 |
#include "dbus.h" |
| 32 |
#include "llist.h" |
| 33 |
#include "error.h" |
| 34 |
#include "malloc.h" |
| 35 |
|
| 36 |
#include "internal.h" |
| 37 |
|
| 38 |
/* AvahiServiceResolver implementation */ |
| 39 |
|
| 40 |
DBusHandlerResult avahi_service_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) { |
| 41 |
AvahiServiceResolver *r = NULL; |
| 42 |
DBusError error; |
| 43 |
const char *path; |
| 44 |
AvahiStringList *strlst = NULL; |
| 45 |
|
| 46 |
assert(client); |
| 47 |
assert(message); |
| 48 |
|
| 49 |
dbus_error_init (&error); |
| 50 |
|
| 51 |
if (!(path = dbus_message_get_path(message))) |
| 52 |
goto fail; |
| 53 |
|
| 54 |
for (r = client->service_resolvers; r; r = r->service_resolvers_next) |
| 55 |
if (strcmp (r->path, path) == 0) |
| 56 |
break; |
| 57 |
|
| 58 |
if (!r) |
| 59 |
goto fail; |
| 60 |
|
| 61 |
switch (event) { |
| 62 |
case AVAHI_RESOLVER_FOUND: { |
| 63 |
int j; |
| 64 |
int32_t interface, protocol, aprotocol; |
| 65 |
uint32_t flags; |
| 66 |
char *name, *type, *domain, *host, *address; |
| 67 |
uint16_t port; |
| 68 |
DBusMessageIter iter, sub; |
| 69 |
AvahiAddress a; |
| 70 |
|
| 71 |
if (!dbus_message_get_args( |
| 72 |
message, &error, |
| 73 |
DBUS_TYPE_INT32, &interface, |
| 74 |
DBUS_TYPE_INT32, &protocol, |
| 75 |
DBUS_TYPE_STRING, &name, |
| 76 |
DBUS_TYPE_STRING, &type, |
| 77 |
DBUS_TYPE_STRING, &domain, |
| 78 |
DBUS_TYPE_STRING, &host, |
| 79 |
DBUS_TYPE_INT32, &aprotocol, |
| 80 |
DBUS_TYPE_STRING, &address, |
| 81 |
DBUS_TYPE_UINT16, &port, |
| 82 |
DBUS_TYPE_INVALID) || |
| 83 |
dbus_error_is_set (&error)) { |
| 84 |
|
| 85 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 86 |
goto fail; |
| 87 |
} |
| 88 |
|
| 89 |
dbus_message_iter_init(message, &iter); |
| 90 |
|
| 91 |
for (j = 0; j < 9; j++) |
| 92 |
dbus_message_iter_next(&iter); |
| 93 |
|
| 94 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || |
| 95 |
dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) { |
| 96 |
fprintf(stderr, "Error parsing service resolving message\n"); |
| 97 |
goto fail; |
| 98 |
} |
| 99 |
|
| 100 |
strlst = NULL; |
| 101 |
dbus_message_iter_recurse(&iter, &sub); |
| 102 |
|
| 103 |
for (;;) { |
| 104 |
DBusMessageIter sub2; |
| 105 |
int at; |
| 106 |
const uint8_t *k; |
| 107 |
int n; |
| 108 |
|
| 109 |
if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID) |
| 110 |
break; |
| 111 |
|
| 112 |
assert(at == DBUS_TYPE_ARRAY); |
| 113 |
|
| 114 |
if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) { |
| 115 |
fprintf(stderr, "Error parsing service resolving message\n"); |
| 116 |
goto fail; |
| 117 |
} |
| 118 |
|
| 119 |
dbus_message_iter_recurse(&sub, &sub2); |
| 120 |
|
| 121 |
k = NULL; n = 0; |
| 122 |
dbus_message_iter_get_fixed_array(&sub2, &k, &n); |
| 123 |
if (k && n > 0) |
| 124 |
strlst = avahi_string_list_add_arbitrary(strlst, k, n); |
| 125 |
|
| 126 |
dbus_message_iter_next(&sub); |
| 127 |
} |
| 128 |
|
| 129 |
dbus_message_iter_next(&iter); |
| 130 |
|
| 131 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) { |
| 132 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 133 |
goto fail; |
| 134 |
} |
| 135 |
|
| 136 |
dbus_message_iter_get_basic(&iter, &flags); |
| 137 |
|
| 138 |
assert(address); |
| 139 |
|
| 140 |
if (address[0] == 0) |
| 141 |
address = NULL; |
| 142 |
else |
| 143 |
avahi_address_parse(address, (AvahiProtocol) aprotocol, &a); |
| 144 |
|
| 145 |
r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, address ? &a : NULL, port, strlst, (AvahiLookupResultFlags) flags, r->userdata); |
| 146 |
|
| 147 |
avahi_string_list_free(strlst); |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
case AVAHI_RESOLVER_FAILURE: { |
| 152 |
char *etxt; |
| 153 |
|
| 154 |
if (!dbus_message_get_args( |
| 155 |
message, &error, |
| 156 |
DBUS_TYPE_STRING, &etxt, |
| 157 |
DBUS_TYPE_INVALID) || |
| 158 |
dbus_error_is_set (&error)) { |
| 159 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 160 |
goto fail; |
| 161 |
} |
| 162 |
|
| 163 |
avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt)); |
| 164 |
r->callback(r, r->interface, r->protocol, event, r->name, r->type, r->domain, NULL, NULL, 0, NULL, 0, r->userdata); |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 170 |
|
| 171 |
|
| 172 |
fail: |
| 173 |
dbus_error_free (&error); |
| 174 |
avahi_string_list_free(strlst); |
| 175 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 176 |
} |
| 177 |
|
| 178 |
AvahiServiceResolver * avahi_service_resolver_new( |
| 179 |
AvahiClient *client, |
| 180 |
AvahiIfIndex interface, |
| 181 |
AvahiProtocol protocol, |
| 182 |
const char *name, |
| 183 |
const char *type, |
| 184 |
const char *domain, |
| 185 |
AvahiProtocol aprotocol, |
| 186 |
AvahiLookupFlags flags, |
| 187 |
AvahiServiceResolverCallback callback, |
| 188 |
void *userdata) { |
| 189 |
|
| 190 |
DBusError error; |
| 191 |
AvahiServiceResolver *r = NULL; |
| 192 |
DBusMessage *message = NULL, *reply = NULL; |
| 193 |
int32_t i_interface, i_protocol, i_aprotocol; |
| 194 |
uint32_t u_flags; |
| 195 |
char *path; |
| 196 |
|
| 197 |
assert(client); |
| 198 |
assert(type); |
| 199 |
|
| 200 |
if (!domain) |
| 201 |
domain = ""; |
| 202 |
|
| 203 |
if (!name) |
| 204 |
name = ""; |
| 205 |
|
| 206 |
dbus_error_init (&error); |
| 207 |
|
| 208 |
if (!avahi_client_is_connected(client)) { |
| 209 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 210 |
goto fail; |
| 211 |
} |
| 212 |
|
| 213 |
if (!(r = avahi_new(AvahiServiceResolver, 1))) { |
| 214 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 215 |
goto fail; |
| 216 |
} |
| 217 |
|
| 218 |
r->client = client; |
| 219 |
r->callback = callback; |
| 220 |
r->userdata = userdata; |
| 221 |
r->path = NULL; |
| 222 |
r->name = r->type = r->domain = NULL; |
| 223 |
r->interface = interface; |
| 224 |
r->protocol = protocol; |
| 225 |
|
| 226 |
AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r); |
| 227 |
|
| 228 |
if (name && name[0]) |
| 229 |
if (!(r->name = avahi_strdup(name))) { |
| 230 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 231 |
goto fail; |
| 232 |
} |
| 233 |
|
| 234 |
if (!(r->type = avahi_strdup(type))) { |
| 235 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 236 |
goto fail; |
| 237 |
} |
| 238 |
|
| 239 |
if (domain && domain[0]) |
| 240 |
if (!(r->domain = avahi_strdup(domain))) { |
| 241 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 242 |
goto fail; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) { |
| 247 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 248 |
goto fail; |
| 249 |
} |
| 250 |
|
| 251 |
i_interface = (int32_t) interface; |
| 252 |
i_protocol = (int32_t) protocol; |
| 253 |
i_aprotocol = (int32_t) aprotocol; |
| 254 |
u_flags = (uint32_t) flags; |
| 255 |
|
| 256 |
if (!(dbus_message_append_args( |
| 257 |
message, |
| 258 |
DBUS_TYPE_INT32, &i_interface, |
| 259 |
DBUS_TYPE_INT32, &i_protocol, |
| 260 |
DBUS_TYPE_STRING, &name, |
| 261 |
DBUS_TYPE_STRING, &type, |
| 262 |
DBUS_TYPE_STRING, &domain, |
| 263 |
DBUS_TYPE_INT32, &i_aprotocol, |
| 264 |
DBUS_TYPE_UINT32, &u_flags, |
| 265 |
DBUS_TYPE_INVALID))) { |
| 266 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 267 |
goto fail; |
| 268 |
} |
| 269 |
|
| 270 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 271 |
dbus_error_is_set(&error)) { |
| 272 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 273 |
goto fail; |
| 274 |
} |
| 275 |
|
| 276 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || |
| 277 |
dbus_error_is_set(&error) || |
| 278 |
!path) { |
| 279 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 280 |
goto fail; |
| 281 |
} |
| 282 |
|
| 283 |
if (!(r->path = avahi_strdup(path))) { |
| 284 |
|
| 285 |
/* FIXME: We don't remove the object on the server side */ |
| 286 |
|
| 287 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 288 |
goto fail; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
dbus_message_unref(message); |
| 293 |
dbus_message_unref(reply); |
| 294 |
|
| 295 |
return r; |
| 296 |
|
| 297 |
fail: |
| 298 |
|
| 299 |
if (dbus_error_is_set(&error)) { |
| 300 |
avahi_client_set_dbus_error(client, &error); |
| 301 |
dbus_error_free(&error); |
| 302 |
} |
| 303 |
|
| 304 |
if (r) |
| 305 |
avahi_service_resolver_free(r); |
| 306 |
|
| 307 |
if (message) |
| 308 |
dbus_message_unref(message); |
| 309 |
|
| 310 |
if (reply) |
| 311 |
dbus_message_unref(reply); |
| 312 |
|
| 313 |
return NULL; |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) { |
| 318 |
assert (r); |
| 319 |
|
| 320 |
return r->client; |
| 321 |
} |
| 322 |
|
| 323 |
int avahi_service_resolver_free(AvahiServiceResolver *r) { |
| 324 |
AvahiClient *client; |
| 325 |
int ret = AVAHI_OK; |
| 326 |
|
| 327 |
assert(r); |
| 328 |
client = r->client; |
| 329 |
|
| 330 |
if (r->path && avahi_client_is_connected(client)) |
| 331 |
ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free"); |
| 332 |
|
| 333 |
AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r); |
| 334 |
|
| 335 |
avahi_free(r->path); |
| 336 |
avahi_free(r->name); |
| 337 |
avahi_free(r->type); |
| 338 |
avahi_free(r->domain); |
| 339 |
avahi_free(r); |
| 340 |
|
| 341 |
return ret; |
| 342 |
} |
| 343 |
|
| 344 |
/* AvahiHostNameResolver implementation */ |
| 345 |
|
| 346 |
DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) { |
| 347 |
AvahiHostNameResolver *r = NULL; |
| 348 |
DBusError error; |
| 349 |
const char *path; |
| 350 |
|
| 351 |
assert(client); |
| 352 |
assert(message); |
| 353 |
|
| 354 |
dbus_error_init (&error); |
| 355 |
|
| 356 |
if (!(path = dbus_message_get_path(message))) |
| 357 |
goto fail; |
| 358 |
|
| 359 |
for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next) |
| 360 |
if (strcmp (r->path, path) == 0) |
| 361 |
break; |
| 362 |
|
| 363 |
if (!r) |
| 364 |
goto fail; |
| 365 |
|
| 366 |
switch (event) { |
| 367 |
case AVAHI_RESOLVER_FOUND: { |
| 368 |
int32_t interface, protocol, aprotocol; |
| 369 |
uint32_t flags; |
| 370 |
char *name, *address; |
| 371 |
AvahiAddress a; |
| 372 |
|
| 373 |
if (!dbus_message_get_args( |
| 374 |
message, &error, |
| 375 |
DBUS_TYPE_INT32, &interface, |
| 376 |
DBUS_TYPE_INT32, &protocol, |
| 377 |
DBUS_TYPE_STRING, &name, |
| 378 |
DBUS_TYPE_INT32, &aprotocol, |
| 379 |
DBUS_TYPE_STRING, &address, |
| 380 |
DBUS_TYPE_UINT32, &flags, |
| 381 |
DBUS_TYPE_INVALID) || |
| 382 |
dbus_error_is_set (&error)) { |
| 383 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 384 |
goto fail; |
| 385 |
} |
| 386 |
|
| 387 |
assert(address); |
| 388 |
if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) { |
| 389 |
fprintf(stderr, "Failed to parse address\n"); |
| 390 |
goto fail; |
| 391 |
} |
| 392 |
|
| 393 |
r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata); |
| 394 |
break; |
| 395 |
} |
| 396 |
|
| 397 |
case AVAHI_RESOLVER_FAILURE: { |
| 398 |
char *etxt; |
| 399 |
|
| 400 |
if (!dbus_message_get_args( |
| 401 |
message, &error, |
| 402 |
DBUS_TYPE_STRING, &etxt, |
| 403 |
DBUS_TYPE_INVALID) || |
| 404 |
dbus_error_is_set (&error)) { |
| 405 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 406 |
goto fail; |
| 407 |
} |
| 408 |
|
| 409 |
avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt)); |
| 410 |
r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, 0, r->userdata); |
| 411 |
break; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 416 |
|
| 417 |
fail: |
| 418 |
dbus_error_free (&error); |
| 419 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
AvahiHostNameResolver * avahi_host_name_resolver_new( |
| 424 |
AvahiClient *client, |
| 425 |
AvahiIfIndex interface, |
| 426 |
AvahiProtocol protocol, |
| 427 |
const char *name, |
| 428 |
AvahiProtocol aprotocol, |
| 429 |
AvahiLookupFlags flags, |
| 430 |
AvahiHostNameResolverCallback callback, |
| 431 |
void *userdata) { |
| 432 |
|
| 433 |
DBusError error; |
| 434 |
AvahiHostNameResolver *r = NULL; |
| 435 |
DBusMessage *message = NULL, *reply = NULL; |
| 436 |
int32_t i_interface, i_protocol, i_aprotocol; |
| 437 |
uint32_t u_flags; |
| 438 |
char *path; |
| 439 |
|
| 440 |
assert(client); |
| 441 |
assert(name); |
| 442 |
|
| 443 |
dbus_error_init (&error); |
| 444 |
|
| 445 |
if (!avahi_client_is_connected(client)) { |
| 446 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 447 |
goto fail; |
| 448 |
} |
| 449 |
|
| 450 |
if (!(r = avahi_new(AvahiHostNameResolver, 1))) { |
| 451 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 452 |
goto fail; |
| 453 |
} |
| 454 |
|
| 455 |
r->client = client; |
| 456 |
r->callback = callback; |
| 457 |
r->userdata = userdata; |
| 458 |
r->path = NULL; |
| 459 |
r->interface = interface; |
| 460 |
r->protocol = protocol; |
| 461 |
r->host_name = NULL; |
| 462 |
|
| 463 |
AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r); |
| 464 |
|
| 465 |
if (!(r->host_name = avahi_strdup(name))) { |
| 466 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 467 |
goto fail; |
| 468 |
} |
| 469 |
|
| 470 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) { |
| 471 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 472 |
goto fail; |
| 473 |
} |
| 474 |
|
| 475 |
i_interface = (int32_t) interface; |
| 476 |
i_protocol = (int32_t) protocol; |
| 477 |
i_aprotocol = (int32_t) aprotocol; |
| 478 |
u_flags = (uint32_t) flags; |
| 479 |
|
| 480 |
if (!(dbus_message_append_args( |
| 481 |
message, |
| 482 |
DBUS_TYPE_INT32, &i_interface, |
| 483 |
DBUS_TYPE_INT32, &i_protocol, |
| 484 |
DBUS_TYPE_STRING, &name, |
| 485 |
DBUS_TYPE_INT32, &i_aprotocol, |
| 486 |
DBUS_TYPE_UINT32, &u_flags, |
| 487 |
DBUS_TYPE_INVALID))) { |
| 488 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 489 |
goto fail; |
| 490 |
} |
| 491 |
|
| 492 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 493 |
dbus_error_is_set(&error)) { |
| 494 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 495 |
goto fail; |
| 496 |
} |
| 497 |
|
| 498 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || |
| 499 |
dbus_error_is_set(&error) || |
| 500 |
!path) { |
| 501 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 502 |
goto fail; |
| 503 |
} |
| 504 |
|
| 505 |
if (!(r->path = avahi_strdup(path))) { |
| 506 |
|
| 507 |
/* FIXME: We don't remove the object on the server side */ |
| 508 |
|
| 509 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 510 |
goto fail; |
| 511 |
} |
| 512 |
|
| 513 |
dbus_message_unref(message); |
| 514 |
dbus_message_unref(reply); |
| 515 |
|
| 516 |
return r; |
| 517 |
|
| 518 |
fail: |
| 519 |
|
| 520 |
if (dbus_error_is_set(&error)) { |
| 521 |
avahi_client_set_dbus_error(client, &error); |
| 522 |
dbus_error_free(&error); |
| 523 |
} |
| 524 |
|
| 525 |
if (r) |
| 526 |
avahi_host_name_resolver_free(r); |
| 527 |
|
| 528 |
if (message) |
| 529 |
dbus_message_unref(message); |
| 530 |
|
| 531 |
if (reply) |
| 532 |
dbus_message_unref(reply); |
| 533 |
|
| 534 |
return NULL; |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
int avahi_host_name_resolver_free(AvahiHostNameResolver *r) { |
| 539 |
int ret = AVAHI_OK; |
| 540 |
AvahiClient *client; |
| 541 |
|
| 542 |
assert(r); |
| 543 |
client = r->client; |
| 544 |
|
| 545 |
if (r->path && avahi_client_is_connected(client)) |
| 546 |
ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free"); |
| 547 |
|
| 548 |
AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r); |
| 549 |
|
| 550 |
avahi_free(r->path); |
| 551 |
avahi_free(r->host_name); |
| 552 |
avahi_free(r); |
| 553 |
|
| 554 |
return ret; |
| 555 |
} |
| 556 |
|
| 557 |
AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) { |
| 558 |
assert (r); |
| 559 |
|
| 560 |
return r->client; |
| 561 |
} |
| 562 |
|
| 563 |
/* AvahiAddressResolver implementation */ |
| 564 |
|
| 565 |
DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) { |
| 566 |
AvahiAddressResolver *r = NULL; |
| 567 |
DBusError error; |
| 568 |
const char *path; |
| 569 |
|
| 570 |
assert(client); |
| 571 |
assert(message); |
| 572 |
|
| 573 |
dbus_error_init (&error); |
| 574 |
|
| 575 |
if (!(path = dbus_message_get_path(message))) |
| 576 |
goto fail; |
| 577 |
|
| 578 |
for (r = client->address_resolvers; r; r = r->address_resolvers_next) |
| 579 |
if (strcmp (r->path, path) == 0) |
| 580 |
break; |
| 581 |
|
| 582 |
if (!r) |
| 583 |
goto fail; |
| 584 |
|
| 585 |
switch (event) { |
| 586 |
case AVAHI_RESOLVER_FOUND: { |
| 587 |
int32_t interface, protocol, aprotocol; |
| 588 |
uint32_t flags; |
| 589 |
char *name, *address; |
| 590 |
AvahiAddress a; |
| 591 |
|
| 592 |
if (!dbus_message_get_args( |
| 593 |
message, &error, |
| 594 |
DBUS_TYPE_INT32, &interface, |
| 595 |
DBUS_TYPE_INT32, &protocol, |
| 596 |
DBUS_TYPE_INT32, &aprotocol, |
| 597 |
DBUS_TYPE_STRING, &address, |
| 598 |
DBUS_TYPE_STRING, &name, |
| 599 |
DBUS_TYPE_UINT32, &flags, |
| 600 |
DBUS_TYPE_INVALID) || |
| 601 |
dbus_error_is_set (&error)) { |
| 602 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 603 |
goto fail; |
| 604 |
} |
| 605 |
|
| 606 |
assert(address); |
| 607 |
if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) { |
| 608 |
fprintf(stderr, "Failed to parse address\n"); |
| 609 |
goto fail; |
| 610 |
} |
| 611 |
|
| 612 |
r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, &a, name, (AvahiLookupResultFlags) flags, r->userdata); |
| 613 |
break; |
| 614 |
} |
| 615 |
|
| 616 |
case AVAHI_RESOLVER_FAILURE: { |
| 617 |
char *etxt; |
| 618 |
|
| 619 |
if (!dbus_message_get_args( |
| 620 |
message, &error, |
| 621 |
DBUS_TYPE_STRING, &etxt, |
| 622 |
DBUS_TYPE_INVALID) || |
| 623 |
dbus_error_is_set (&error)) { |
| 624 |
fprintf(stderr, "Failed to parse resolver event.\n"); |
| 625 |
goto fail; |
| 626 |
} |
| 627 |
|
| 628 |
avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt)); |
| 629 |
r->callback(r, r->interface, r->protocol, event, &r->address, NULL, 0, r->userdata); |
| 630 |
break; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 635 |
|
| 636 |
fail: |
| 637 |
dbus_error_free (&error); |
| 638 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 639 |
} |
| 640 |
|
| 641 |
AvahiAddressResolver * avahi_address_resolver_new( |
| 642 |
AvahiClient *client, |
| 643 |
AvahiIfIndex interface, |
| 644 |
AvahiProtocol protocol, |
| 645 |
const AvahiAddress *a, |
| 646 |
AvahiLookupFlags flags, |
| 647 |
AvahiAddressResolverCallback callback, |
| 648 |
void *userdata) { |
| 649 |
|
| 650 |
DBusError error; |
| 651 |
AvahiAddressResolver *r = NULL; |
| 652 |
DBusMessage *message = NULL, *reply = NULL; |
| 653 |
int32_t i_interface, i_protocol; |
| 654 |
uint32_t u_flags; |
| 655 |
char *path; |
| 656 |
char addr[AVAHI_ADDRESS_STR_MAX], *address = addr; |
| 657 |
|
| 658 |
assert(client); |
| 659 |
assert(a); |
| 660 |
|
| 661 |
dbus_error_init (&error); |
| 662 |
|
| 663 |
if (!avahi_address_snprint (addr, sizeof(addr), a)) { |
| 664 |
avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS); |
| 665 |
return NULL; |
| 666 |
} |
| 667 |
|
| 668 |
if (!avahi_client_is_connected(client)) { |
| 669 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 670 |
goto fail; |
| 671 |
} |
| 672 |
|
| 673 |
if (!(r = avahi_new(AvahiAddressResolver, 1))) { |
| 674 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 675 |
goto fail; |
| 676 |
} |
| 677 |
|
| 678 |
r->client = client; |
| 679 |
r->callback = callback; |
| 680 |
r->userdata = userdata; |
| 681 |
r->path = NULL; |
| 682 |
r->interface = interface; |
| 683 |
r->protocol = protocol; |
| 684 |
r->address = *a; |
| 685 |
|
| 686 |
AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r); |
| 687 |
|
| 688 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) { |
| 689 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 690 |
goto fail; |
| 691 |
} |
| 692 |
|
| 693 |
i_interface = (int32_t) interface; |
| 694 |
i_protocol = (int32_t) protocol; |
| 695 |
u_flags = (uint32_t) flags; |
| 696 |
|
| 697 |
if (!(dbus_message_append_args( |
| 698 |
message, |
| 699 |
DBUS_TYPE_INT32, &i_interface, |
| 700 |
DBUS_TYPE_INT32, &i_protocol, |
| 701 |
DBUS_TYPE_STRING, &address, |
| 702 |
DBUS_TYPE_UINT32, &u_flags, |
| 703 |
DBUS_TYPE_INVALID))) { |
| 704 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 705 |
goto fail; |
| 706 |
} |
| 707 |
|
| 708 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 709 |
dbus_error_is_set(&error)) { |
| 710 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 711 |
goto fail; |
| 712 |
} |
| 713 |
|
| 714 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || |
| 715 |
dbus_error_is_set(&error) || |
| 716 |
!path) { |
| 717 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 718 |
goto fail; |
| 719 |
} |
| 720 |
|
| 721 |
if (!(r->path = avahi_strdup(path))) { |
| 722 |
|
| 723 |
/* FIXME: We don't remove the object on the server side */ |
| 724 |
|
| 725 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 726 |
goto fail; |
| 727 |
} |
| 728 |
|
| 729 |
dbus_message_unref(message); |
| 730 |
dbus_message_unref(reply); |
| 731 |
|
| 732 |
return r; |
| 733 |
|
| 734 |
fail: |
| 735 |
|
| 736 |
if (dbus_error_is_set(&error)) { |
| 737 |
avahi_client_set_dbus_error(client, &error); |
| 738 |
dbus_error_free(&error); |
| 739 |
} |
| 740 |
|
| 741 |
if (r) |
| 742 |
avahi_address_resolver_free(r); |
| 743 |
|
| 744 |
if (message) |
| 745 |
dbus_message_unref(message); |
| 746 |
|
| 747 |
if (reply) |
| 748 |
dbus_message_unref(reply); |
| 749 |
|
| 750 |
return NULL; |
| 751 |
|
| 752 |
} |
| 753 |
|
| 754 |
AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) { |
| 755 |
assert (r); |
| 756 |
|
| 757 |
return r->client; |
| 758 |
} |
| 759 |
|
| 760 |
int avahi_address_resolver_free(AvahiAddressResolver *r) { |
| 761 |
AvahiClient *client; |
| 762 |
int ret = AVAHI_OK; |
| 763 |
|
| 764 |
assert(r); |
| 765 |
client = r->client; |
| 766 |
|
| 767 |
if (r->path && avahi_client_is_connected(client)) |
| 768 |
ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free"); |
| 769 |
|
| 770 |
AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r); |
| 771 |
|
| 772 |
avahi_free(r->path); |
| 773 |
avahi_free(r); |
| 774 |
|
| 775 |
return ret; |
| 776 |
} |
| 777 |
|