| 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 |
#include "../config.h" |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <dbus/dbus.h> |
| 27 |
|
| 28 |
#include "dbus.h" |
| 29 |
#include "llist.h" |
| 30 |
#include "error.h" |
| 31 |
#include "dbus.h" |
| 32 |
#include "malloc.h" |
| 33 |
#include "dbus-watch-glue.h" |
| 34 |
#include "i18n.h" |
| 35 |
|
| 36 |
#include "client.h" |
| 37 |
#include "internal.h" |
| 38 |
|
| 39 |
#define AVAHI_CLIENT_DBUS_API_SUPPORTED ((uint32_t) 0x0201) |
| 40 |
|
| 41 |
static int init_server(AvahiClient *client, int *ret_error); |
| 42 |
|
| 43 |
int avahi_client_set_errno (AvahiClient *client, int error) { |
| 44 |
assert(client); |
| 45 |
|
| 46 |
return client->error = error; |
| 47 |
} |
| 48 |
|
| 49 |
int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error) { |
| 50 |
assert(client); |
| 51 |
assert(error); |
| 52 |
|
| 53 |
return avahi_client_set_errno(client, avahi_error_dbus_to_number(error->name)); |
| 54 |
} |
| 55 |
|
| 56 |
static void client_set_state(AvahiClient *client, AvahiClientState state) { |
| 57 |
assert(client); |
| 58 |
|
| 59 |
if (client->state == state) |
| 60 |
return; |
| 61 |
|
| 62 |
client->state = state; |
| 63 |
|
| 64 |
switch (client->state) { |
| 65 |
case AVAHI_CLIENT_FAILURE: |
| 66 |
if (client->bus) { |
| 67 |
#ifdef HAVE_DBUS_CONNECTION_CLOSE |
| 68 |
dbus_connection_close(client->bus); |
| 69 |
#else |
| 70 |
dbus_connection_disconnect(client->bus); |
| 71 |
#endif |
| 72 |
dbus_connection_unref(client->bus); |
| 73 |
client->bus = NULL; |
| 74 |
} |
| 75 |
|
| 76 |
/* Fall through */ |
| 77 |
|
| 78 |
case AVAHI_CLIENT_S_COLLISION: |
| 79 |
case AVAHI_CLIENT_S_REGISTERING: |
| 80 |
|
| 81 |
/* Clear cached strings */ |
| 82 |
avahi_free(client->host_name); |
| 83 |
avahi_free(client->host_name_fqdn); |
| 84 |
avahi_free(client->domain_name); |
| 85 |
|
| 86 |
client->host_name = NULL; |
| 87 |
client->host_name_fqdn = NULL; |
| 88 |
client->domain_name = NULL; |
| 89 |
break; |
| 90 |
|
| 91 |
case AVAHI_CLIENT_S_RUNNING: |
| 92 |
case AVAHI_CLIENT_CONNECTING: |
| 93 |
break; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
if (client->callback) |
| 98 |
client->callback (client, state, client->userdata); |
| 99 |
} |
| 100 |
|
| 101 |
static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, void *userdata) { |
| 102 |
AvahiClient *client = (AvahiClient *)userdata; |
| 103 |
DBusError error; |
| 104 |
|
| 105 |
assert(bus); |
| 106 |
assert(message); |
| 107 |
|
| 108 |
dbus_error_init(&error); |
| 109 |
|
| 110 |
/* fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */ |
| 111 |
/* dbus_message_get_interface (message), */ |
| 112 |
/* dbus_message_get_path (message), */ |
| 113 |
/* dbus_message_get_member (message)); */ |
| 114 |
|
| 115 |
if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { |
| 116 |
|
| 117 |
/* The DBUS server died or kicked us */ |
| 118 |
avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED); |
| 119 |
goto fail; |
| 120 |
|
| 121 |
} else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameAcquired")) { |
| 122 |
|
| 123 |
/* Ignore this message */ |
| 124 |
|
| 125 |
} else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { |
| 126 |
char *name, *old, *newname; |
| 127 |
|
| 128 |
if (!dbus_message_get_args( |
| 129 |
message, &error, |
| 130 |
DBUS_TYPE_STRING, &name, |
| 131 |
DBUS_TYPE_STRING, &old, |
| 132 |
DBUS_TYPE_STRING, &newname, |
| 133 |
DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) { |
| 134 |
|
| 135 |
fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message); |
| 136 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 137 |
goto fail; |
| 138 |
} |
| 139 |
|
| 140 |
if (strcmp(name, AVAHI_DBUS_NAME) == 0) { |
| 141 |
|
| 142 |
if (old[0] && |
| 143 |
avahi_client_is_connected(client)) { |
| 144 |
|
| 145 |
/* Regardless if the server lost its name or |
| 146 |
* if the name was transfered: our services are no longer |
| 147 |
* available, so we disconnect ourselves */ |
| 148 |
avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED); |
| 149 |
goto fail; |
| 150 |
|
| 151 |
} else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) { |
| 152 |
int ret; |
| 153 |
|
| 154 |
/* Server appeared */ |
| 155 |
|
| 156 |
if ((ret = init_server(client, NULL)) < 0) { |
| 157 |
avahi_client_set_errno(client, ret); |
| 158 |
goto fail; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
} else if (!avahi_client_is_connected(client)) { |
| 164 |
|
| 165 |
/* Ignore messages we get in unconnected state */ |
| 166 |
|
| 167 |
} else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) { |
| 168 |
int32_t state; |
| 169 |
char *e = NULL; |
| 170 |
int c; |
| 171 |
|
| 172 |
if (!dbus_message_get_args( |
| 173 |
message, &error, |
| 174 |
DBUS_TYPE_INT32, &state, |
| 175 |
DBUS_TYPE_STRING, &e, |
| 176 |
DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) { |
| 177 |
|
| 178 |
fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message); |
| 179 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 180 |
goto fail; |
| 181 |
} |
| 182 |
|
| 183 |
if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK) |
| 184 |
avahi_client_set_errno(client, c); |
| 185 |
|
| 186 |
client_set_state(client, (AvahiClientState) state); |
| 187 |
|
| 188 |
} else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) { |
| 189 |
const char *path; |
| 190 |
AvahiEntryGroup *g; |
| 191 |
path = dbus_message_get_path(message); |
| 192 |
|
| 193 |
for (g = client->groups; g; g = g->groups_next){ |
| 194 |
if (strcmp(g->path, path) == 0) |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
if (g) { |
| 199 |
int32_t state; |
| 200 |
char *e; |
| 201 |
int c; |
| 202 |
|
| 203 |
if (!dbus_message_get_args( |
| 204 |
message, &error, |
| 205 |
DBUS_TYPE_INT32, &state, |
| 206 |
DBUS_TYPE_STRING, &e, |
| 207 |
DBUS_TYPE_INVALID) || |
| 208 |
dbus_error_is_set(&error)) { |
| 209 |
|
| 210 |
fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message); |
| 211 |
avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 212 |
goto fail; |
| 213 |
} |
| 214 |
|
| 215 |
if ((c = avahi_error_dbus_to_number(e)) != AVAHI_OK) |
| 216 |
avahi_client_set_errno(client, c); |
| 217 |
|
| 218 |
avahi_entry_group_set_state(g, (AvahiEntryGroupState)state); |
| 219 |
} |
| 220 |
|
| 221 |
return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message); |
| 222 |
} |
| 223 |
else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) |
| 224 |
return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message); |
| 225 |
else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "CacheExhausted")) |
| 226 |
return avahi_domain_browser_event(client, AVAHI_BROWSER_CACHE_EXHAUSTED, message); |
| 227 |
else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "AllForNow")) |
| 228 |
return avahi_domain_browser_event(client, AVAHI_BROWSER_ALL_FOR_NOW, message); |
| 229 |
else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Failure")) |
| 230 |
return avahi_domain_browser_event(client, AVAHI_BROWSER_FAILURE, message); |
| 231 |
|
| 232 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) |
| 233 |
return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message); |
| 234 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) |
| 235 |
return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message); |
| 236 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "CacheExhausted")) |
| 237 |
return avahi_service_type_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message); |
| 238 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "AllForNow")) |
| 239 |
return avahi_service_type_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message); |
| 240 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Failure")) |
| 241 |
return avahi_service_type_browser_event (client, AVAHI_BROWSER_FAILURE, message); |
| 242 |
|
| 243 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) |
| 244 |
return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message); |
| 245 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) |
| 246 |
return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message); |
| 247 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "CacheExhausted")) |
| 248 |
return avahi_service_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message); |
| 249 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "AllForNow")) |
| 250 |
return avahi_service_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message); |
| 251 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Failure")) |
| 252 |
return avahi_service_browser_event (client, AVAHI_BROWSER_FAILURE, message); |
| 253 |
|
| 254 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Found")) |
| 255 |
return avahi_service_resolver_event (client, AVAHI_RESOLVER_FOUND, message); |
| 256 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Failure")) |
| 257 |
return avahi_service_resolver_event (client, AVAHI_RESOLVER_FAILURE, message); |
| 258 |
|
| 259 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Found")) |
| 260 |
return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FOUND, message); |
| 261 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Failure")) |
| 262 |
return avahi_host_name_resolver_event (client, AVAHI_RESOLVER_FAILURE, message); |
| 263 |
|
| 264 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found")) |
| 265 |
return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message); |
| 266 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure")) |
| 267 |
return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message); |
| 268 |
|
| 269 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemNew")) |
| 270 |
return avahi_record_browser_event (client, AVAHI_BROWSER_NEW, message); |
| 271 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "ItemRemove")) |
| 272 |
return avahi_record_browser_event (client, AVAHI_BROWSER_REMOVE, message); |
| 273 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "CacheExhausted")) |
| 274 |
return avahi_record_browser_event (client, AVAHI_BROWSER_CACHE_EXHAUSTED, message); |
| 275 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "AllForNow")) |
| 276 |
return avahi_record_browser_event (client, AVAHI_BROWSER_ALL_FOR_NOW, message); |
| 277 |
else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Failure")) |
| 278 |
return avahi_record_browser_event (client, AVAHI_BROWSER_FAILURE, message); |
| 279 |
|
| 280 |
else { |
| 281 |
|
| 282 |
fprintf(stderr, "WARNING: Unhandled message: interface=%s, path=%s, member=%s\n", |
| 283 |
dbus_message_get_interface(message), |
| 284 |
dbus_message_get_path(message), |
| 285 |
dbus_message_get_member(message)); |
| 286 |
|
| 287 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 288 |
} |
| 289 |
|
| 290 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 291 |
|
| 292 |
fail: |
| 293 |
|
| 294 |
if (dbus_error_is_set(&error)) { |
| 295 |
avahi_client_set_errno(client, avahi_error_dbus_to_number(error.name)); |
| 296 |
dbus_error_free(&error); |
| 297 |
} |
| 298 |
|
| 299 |
client_set_state(client, AVAHI_CLIENT_FAILURE); |
| 300 |
|
| 301 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 302 |
} |
| 303 |
|
| 304 |
static int get_server_state(AvahiClient *client, int *ret_error) { |
| 305 |
DBusMessage *message = NULL, *reply = NULL; |
| 306 |
DBusError error; |
| 307 |
int32_t state; |
| 308 |
int e = AVAHI_ERR_NO_MEMORY; |
| 309 |
|
| 310 |
assert(client); |
| 311 |
|
| 312 |
dbus_error_init(&error); |
| 313 |
|
| 314 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState"))) |
| 315 |
goto fail; |
| 316 |
|
| 317 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 318 |
|
| 319 |
if (!reply || dbus_error_is_set (&error)) |
| 320 |
goto fail; |
| 321 |
|
| 322 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) || |
| 323 |
dbus_error_is_set (&error)) |
| 324 |
goto fail; |
| 325 |
|
| 326 |
client_set_state(client, (AvahiClientState) state); |
| 327 |
|
| 328 |
dbus_message_unref(message); |
| 329 |
dbus_message_unref(reply); |
| 330 |
|
| 331 |
return AVAHI_OK; |
| 332 |
|
| 333 |
fail: |
| 334 |
if (dbus_error_is_set(&error)) { |
| 335 |
e = avahi_error_dbus_to_number (error.name); |
| 336 |
dbus_error_free(&error); |
| 337 |
} |
| 338 |
|
| 339 |
if (ret_error) |
| 340 |
*ret_error = e; |
| 341 |
|
| 342 |
if (message) |
| 343 |
dbus_message_unref(message); |
| 344 |
if (reply) |
| 345 |
dbus_message_unref(reply); |
| 346 |
|
| 347 |
return e; |
| 348 |
} |
| 349 |
|
| 350 |
static int check_version(AvahiClient *client, int *ret_error) { |
| 351 |
DBusMessage *message = NULL, *reply = NULL; |
| 352 |
DBusError error; |
| 353 |
uint32_t version; |
| 354 |
int e = AVAHI_ERR_NO_MEMORY; |
| 355 |
|
| 356 |
assert(client); |
| 357 |
|
| 358 |
dbus_error_init(&error); |
| 359 |
|
| 360 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetAPIVersion"))) |
| 361 |
goto fail; |
| 362 |
|
| 363 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 364 |
|
| 365 |
if (!reply || dbus_error_is_set (&error)) { |
| 366 |
char *version_str; |
| 367 |
|
| 368 |
if (!dbus_error_is_set(&error) || strcmp(error.name, DBUS_ERROR_UNKNOWN_METHOD)) |
| 369 |
goto fail; |
| 370 |
|
| 371 |
/* If the method GetAPIVersion is not known, we look if |
| 372 |
* GetVersionString matches "avahi 0.6" which is the only |
| 373 |
* version we support which doesn't have GetAPIVersion() .*/ |
| 374 |
|
| 375 |
dbus_message_unref(message); |
| 376 |
if (reply) dbus_message_unref(reply); |
| 377 |
dbus_error_free(&error); |
| 378 |
|
| 379 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString"))) |
| 380 |
goto fail; |
| 381 |
|
| 382 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 383 |
|
| 384 |
if (!reply || dbus_error_is_set (&error)) |
| 385 |
goto fail; |
| 386 |
|
| 387 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &version_str, DBUS_TYPE_INVALID) || |
| 388 |
dbus_error_is_set (&error)) |
| 389 |
goto fail; |
| 390 |
|
| 391 |
version = strcmp(version_str, "avahi 0.6") == 0 ? 0x0201 : 0x0000; |
| 392 |
|
| 393 |
} else { |
| 394 |
|
| 395 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &version, DBUS_TYPE_INVALID) || |
| 396 |
dbus_error_is_set(&error)) |
| 397 |
goto fail; |
| 398 |
} |
| 399 |
|
| 400 |
/*fprintf(stderr, "API Version 0x%04x\n", version);*/ |
| 401 |
|
| 402 |
if ((version & 0xFF00) != (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0xFF00) || |
| 403 |
(version & 0x00FF) < (AVAHI_CLIENT_DBUS_API_SUPPORTED & 0x00FF)) { |
| 404 |
e = AVAHI_ERR_VERSION_MISMATCH; |
| 405 |
goto fail; |
| 406 |
} |
| 407 |
|
| 408 |
dbus_message_unref(message); |
| 409 |
dbus_message_unref(reply); |
| 410 |
|
| 411 |
return AVAHI_OK; |
| 412 |
|
| 413 |
fail: |
| 414 |
if (dbus_error_is_set(&error)) { |
| 415 |
e = avahi_error_dbus_to_number (error.name); |
| 416 |
dbus_error_free(&error); |
| 417 |
} |
| 418 |
|
| 419 |
if (ret_error) |
| 420 |
*ret_error = e; |
| 421 |
|
| 422 |
if (message) |
| 423 |
dbus_message_unref(message); |
| 424 |
if (reply) |
| 425 |
dbus_message_unref(reply); |
| 426 |
|
| 427 |
return e; |
| 428 |
} |
| 429 |
|
| 430 |
static int init_server(AvahiClient *client, int *ret_error) { |
| 431 |
int r; |
| 432 |
|
| 433 |
if ((r = check_version(client, ret_error)) < 0) |
| 434 |
return r; |
| 435 |
|
| 436 |
if ((r = get_server_state(client, ret_error)) < 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
return AVAHI_OK; |
| 440 |
} |
| 441 |
|
| 442 |
/* This function acts like dbus_bus_get but creates a private |
| 443 |
* connection instead. */ |
| 444 |
static DBusConnection* avahi_dbus_bus_get(DBusError *error) { |
| 445 |
DBusConnection *c; |
| 446 |
|
| 447 |
#ifdef HAVE_DBUS_BUS_GET_PRIVATE |
| 448 |
if (!(c = dbus_bus_get_private(DBUS_BUS_SYSTEM, error))) |
| 449 |
return NULL; |
| 450 |
|
| 451 |
dbus_connection_set_exit_on_disconnect(c, FALSE); |
| 452 |
#else |
| 453 |
const char *a; |
| 454 |
|
| 455 |
if (!(a = getenv("DBUS_SYSTEM_BUS_ADDRESS")) || !*a) |
| 456 |
a = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS; |
| 457 |
|
| 458 |
if (!(c = dbus_connection_open_private(a, error))) |
| 459 |
return NULL; |
| 460 |
|
| 461 |
dbus_connection_set_exit_on_disconnect(c, FALSE); |
| 462 |
|
| 463 |
if (!dbus_bus_register(c, error)) { |
| 464 |
#ifdef HAVE_DBUS_CONNECTION_CLOSE |
| 465 |
dbus_connection_close(c); |
| 466 |
#else |
| 467 |
dbus_connection_disconnect(c); |
| 468 |
#endif |
| 469 |
dbus_connection_unref(c); |
| 470 |
return NULL; |
| 471 |
} |
| 472 |
#endif |
| 473 |
|
| 474 |
return c; |
| 475 |
} |
| 476 |
|
| 477 |
AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) { |
| 478 |
AvahiClient *client = NULL; |
| 479 |
DBusError error; |
| 480 |
DBusMessage *message = NULL, *reply = NULL; |
| 481 |
|
| 482 |
avahi_init_i18n(); |
| 483 |
|
| 484 |
dbus_error_init(&error); |
| 485 |
|
| 486 |
if (!(client = avahi_new(AvahiClient, 1))) { |
| 487 |
if (ret_error) |
| 488 |
*ret_error = AVAHI_ERR_NO_MEMORY; |
| 489 |
goto fail; |
| 490 |
} |
| 491 |
|
| 492 |
client->poll_api = poll_api; |
| 493 |
client->error = AVAHI_OK; |
| 494 |
client->callback = callback; |
| 495 |
client->userdata = userdata; |
| 496 |
client->state = (AvahiClientState) -1; |
| 497 |
client->flags = flags; |
| 498 |
|
| 499 |
client->host_name = NULL; |
| 500 |
client->host_name_fqdn = NULL; |
| 501 |
client->domain_name = NULL; |
| 502 |
client->version_string = NULL; |
| 503 |
client->local_service_cookie_valid = 0; |
| 504 |
|
| 505 |
AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups); |
| 506 |
AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers); |
| 507 |
AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers); |
| 508 |
AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers); |
| 509 |
AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers); |
| 510 |
AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers); |
| 511 |
AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers); |
| 512 |
AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, client->record_browsers); |
| 513 |
|
| 514 |
if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) { |
| 515 |
if (ret_error) |
| 516 |
*ret_error = AVAHI_ERR_DBUS_ERROR; |
| 517 |
goto fail; |
| 518 |
} |
| 519 |
|
| 520 |
if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) { |
| 521 |
if (ret_error) |
| 522 |
*ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */ |
| 523 |
goto fail; |
| 524 |
} |
| 525 |
|
| 526 |
if (!dbus_connection_add_filter(client->bus, filter_func, client, NULL)) { |
| 527 |
if (ret_error) |
| 528 |
*ret_error = AVAHI_ERR_NO_MEMORY; |
| 529 |
goto fail; |
| 530 |
} |
| 531 |
|
| 532 |
dbus_bus_add_match( |
| 533 |
client->bus, |
| 534 |
"type='signal', " |
| 535 |
"interface='" AVAHI_DBUS_INTERFACE_SERVER "', " |
| 536 |
"sender='" AVAHI_DBUS_NAME "', " |
| 537 |
"path='" AVAHI_DBUS_PATH_SERVER "'", |
| 538 |
&error); |
| 539 |
|
| 540 |
if (dbus_error_is_set(&error)) |
| 541 |
goto fail; |
| 542 |
|
| 543 |
dbus_bus_add_match ( |
| 544 |
client->bus, |
| 545 |
"type='signal', " |
| 546 |
"interface='" DBUS_INTERFACE_DBUS "', " |
| 547 |
"sender='" DBUS_SERVICE_DBUS "', " |
| 548 |
"path='" DBUS_PATH_DBUS "'", |
| 549 |
&error); |
| 550 |
|
| 551 |
if (dbus_error_is_set(&error)) |
| 552 |
goto fail; |
| 553 |
|
| 554 |
dbus_bus_add_match( |
| 555 |
client->bus, |
| 556 |
"type='signal', " |
| 557 |
"interface='" DBUS_INTERFACE_LOCAL "'", |
| 558 |
&error); |
| 559 |
|
| 560 |
if (dbus_error_is_set(&error)) |
| 561 |
goto fail; |
| 562 |
|
| 563 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, "org.freedesktop.DBus.Peer", "Ping"))) |
| 564 |
goto fail; |
| 565 |
|
| 566 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 567 |
|
| 568 |
if (!reply || dbus_error_is_set (&error)) { |
| 569 |
/* We free the error so its not set, that way the fail target |
| 570 |
* will return the NO_DAEMON error rather than a DBUS error */ |
| 571 |
dbus_error_free(&error); |
| 572 |
|
| 573 |
if (!(flags & AVAHI_CLIENT_NO_FAIL)) { |
| 574 |
|
| 575 |
if (ret_error) |
| 576 |
*ret_error = AVAHI_ERR_NO_DAEMON; |
| 577 |
|
| 578 |
goto fail; |
| 579 |
} |
| 580 |
|
| 581 |
/* The user doesn't want this call to fail if the daemon is not |
| 582 |
* available, so let's return succesfully */ |
| 583 |
client_set_state(client, AVAHI_CLIENT_CONNECTING); |
| 584 |
|
| 585 |
} else { |
| 586 |
|
| 587 |
if (init_server(client, ret_error) < 0) |
| 588 |
goto fail; |
| 589 |
} |
| 590 |
|
| 591 |
dbus_message_unref(message); |
| 592 |
|
| 593 |
if (reply) |
| 594 |
dbus_message_unref(reply); |
| 595 |
|
| 596 |
return client; |
| 597 |
|
| 598 |
fail: |
| 599 |
|
| 600 |
if (message) |
| 601 |
dbus_message_unref(message); |
| 602 |
if (reply) |
| 603 |
dbus_message_unref(reply); |
| 604 |
|
| 605 |
if (client) |
| 606 |
avahi_client_free(client); |
| 607 |
|
| 608 |
if (dbus_error_is_set(&error)) { |
| 609 |
|
| 610 |
if (ret_error) { |
| 611 |
if (strcmp(error.name, DBUS_ERROR_FILE_NOT_FOUND) == 0) |
| 612 |
/* DBUS returns this error when the DBUS daemon is not running */ |
| 613 |
*ret_error = AVAHI_ERR_NO_DAEMON; |
| 614 |
else |
| 615 |
*ret_error = avahi_error_dbus_to_number(error.name); |
| 616 |
} |
| 617 |
|
| 618 |
dbus_error_free(&error); |
| 619 |
} |
| 620 |
|
| 621 |
return NULL; |
| 622 |
} |
| 623 |
|
| 624 |
void avahi_client_free(AvahiClient *client) { |
| 625 |
assert(client); |
| 626 |
|
| 627 |
if (client->bus) |
| 628 |
/* Disconnect in advance, so that the free() functions won't |
| 629 |
* issue needless server calls */ |
| 630 |
#ifdef HAVE_DBUS_CONNECTION_CLOSE |
| 631 |
dbus_connection_close(client->bus); |
| 632 |
#else |
| 633 |
dbus_connection_disconnect(client->bus); |
| 634 |
#endif |
| 635 |
|
| 636 |
while (client->groups) |
| 637 |
avahi_entry_group_free(client->groups); |
| 638 |
|
| 639 |
while (client->domain_browsers) |
| 640 |
avahi_domain_browser_free(client->domain_browsers); |
| 641 |
|
| 642 |
while (client->service_browsers) |
| 643 |
avahi_service_browser_free(client->service_browsers); |
| 644 |
|
| 645 |
while (client->service_type_browsers) |
| 646 |
avahi_service_type_browser_free(client->service_type_browsers); |
| 647 |
|
| 648 |
while (client->service_resolvers) |
| 649 |
avahi_service_resolver_free(client->service_resolvers); |
| 650 |
|
| 651 |
while (client->host_name_resolvers) |
| 652 |
avahi_host_name_resolver_free(client->host_name_resolvers); |
| 653 |
|
| 654 |
while (client->address_resolvers) |
| 655 |
avahi_address_resolver_free(client->address_resolvers); |
| 656 |
|
| 657 |
while (client->record_browsers) |
| 658 |
avahi_record_browser_free(client->record_browsers); |
| 659 |
|
| 660 |
if (client->bus) |
| 661 |
dbus_connection_unref(client->bus); |
| 662 |
|
| 663 |
avahi_free(client->version_string); |
| 664 |
avahi_free(client->host_name); |
| 665 |
avahi_free(client->host_name_fqdn); |
| 666 |
avahi_free(client->domain_name); |
| 667 |
|
| 668 |
avahi_free(client); |
| 669 |
} |
| 670 |
|
| 671 |
static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) { |
| 672 |
DBusMessage *message = NULL, *reply = NULL; |
| 673 |
DBusError error; |
| 674 |
char *ret, *n; |
| 675 |
|
| 676 |
assert(client); |
| 677 |
assert(method); |
| 678 |
|
| 679 |
dbus_error_init (&error); |
| 680 |
|
| 681 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) { |
| 682 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 683 |
goto fail; |
| 684 |
} |
| 685 |
|
| 686 |
if (param) { |
| 687 |
if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) { |
| 688 |
avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY); |
| 689 |
goto fail; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 694 |
|
| 695 |
if (!reply || dbus_error_is_set (&error)) |
| 696 |
goto fail; |
| 697 |
|
| 698 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) || |
| 699 |
dbus_error_is_set (&error)) |
| 700 |
goto fail; |
| 701 |
|
| 702 |
if (!(n = avahi_strdup(ret))) { |
| 703 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 704 |
goto fail; |
| 705 |
} |
| 706 |
|
| 707 |
dbus_message_unref(message); |
| 708 |
dbus_message_unref(reply); |
| 709 |
|
| 710 |
return n; |
| 711 |
|
| 712 |
fail: |
| 713 |
|
| 714 |
if (message) |
| 715 |
dbus_message_unref(message); |
| 716 |
if (reply) |
| 717 |
dbus_message_unref(reply); |
| 718 |
|
| 719 |
if (dbus_error_is_set(&error)) { |
| 720 |
avahi_client_set_dbus_error(client, &error); |
| 721 |
dbus_error_free(&error); |
| 722 |
} |
| 723 |
|
| 724 |
return NULL; |
| 725 |
} |
| 726 |
|
| 727 |
const char* avahi_client_get_version_string(AvahiClient *client) { |
| 728 |
assert(client); |
| 729 |
|
| 730 |
if (!avahi_client_is_connected(client)) { |
| 731 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 732 |
return NULL; |
| 733 |
} |
| 734 |
|
| 735 |
if (!client->version_string) |
| 736 |
client->version_string = avahi_client_get_string_reply_and_block(client, "GetVersionString", NULL); |
| 737 |
|
| 738 |
return client->version_string; |
| 739 |
} |
| 740 |
|
| 741 |
const char* avahi_client_get_domain_name(AvahiClient *client) { |
| 742 |
assert(client); |
| 743 |
|
| 744 |
if (!avahi_client_is_connected(client)) { |
| 745 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 746 |
return NULL; |
| 747 |
} |
| 748 |
|
| 749 |
if (!client->domain_name) |
| 750 |
client->domain_name = avahi_client_get_string_reply_and_block(client, "GetDomainName", NULL); |
| 751 |
|
| 752 |
return client->domain_name; |
| 753 |
} |
| 754 |
|
| 755 |
const char* avahi_client_get_host_name(AvahiClient *client) { |
| 756 |
assert(client); |
| 757 |
|
| 758 |
if (!avahi_client_is_connected(client)) { |
| 759 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 760 |
return NULL; |
| 761 |
} |
| 762 |
|
| 763 |
if (!client->host_name) |
| 764 |
client->host_name = avahi_client_get_string_reply_and_block(client, "GetHostName", NULL); |
| 765 |
|
| 766 |
return client->host_name; |
| 767 |
} |
| 768 |
|
| 769 |
const char* avahi_client_get_host_name_fqdn (AvahiClient *client) { |
| 770 |
assert(client); |
| 771 |
|
| 772 |
if (!avahi_client_is_connected(client)) { |
| 773 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 774 |
return NULL; |
| 775 |
} |
| 776 |
|
| 777 |
if (!client->host_name_fqdn) |
| 778 |
client->host_name_fqdn = avahi_client_get_string_reply_and_block(client, "GetHostNameFqdn", NULL); |
| 779 |
|
| 780 |
return client->host_name_fqdn; |
| 781 |
} |
| 782 |
|
| 783 |
AvahiClientState avahi_client_get_state(AvahiClient *client) { |
| 784 |
assert(client); |
| 785 |
|
| 786 |
return client->state; |
| 787 |
} |
| 788 |
|
| 789 |
int avahi_client_errno(AvahiClient *client) { |
| 790 |
assert(client); |
| 791 |
|
| 792 |
return client->error; |
| 793 |
} |
| 794 |
|
| 795 |
/* Just for internal use */ |
| 796 |
int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) { |
| 797 |
DBusMessage *message = NULL, *reply = NULL; |
| 798 |
DBusError error; |
| 799 |
int r = AVAHI_OK; |
| 800 |
|
| 801 |
dbus_error_init(&error); |
| 802 |
|
| 803 |
assert(client); |
| 804 |
assert(path); |
| 805 |
assert(interface); |
| 806 |
assert(method); |
| 807 |
|
| 808 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, method))) { |
| 809 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 810 |
goto fail; |
| 811 |
} |
| 812 |
|
| 813 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 814 |
dbus_error_is_set (&error)) { |
| 815 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 816 |
goto fail; |
| 817 |
} |
| 818 |
|
| 819 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 820 |
dbus_error_is_set (&error)) { |
| 821 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 822 |
goto fail; |
| 823 |
} |
| 824 |
|
| 825 |
dbus_message_unref(message); |
| 826 |
dbus_message_unref(reply); |
| 827 |
|
| 828 |
return AVAHI_OK; |
| 829 |
|
| 830 |
fail: |
| 831 |
if (dbus_error_is_set(&error)) { |
| 832 |
r = avahi_client_set_dbus_error(client, &error); |
| 833 |
dbus_error_free(&error); |
| 834 |
} |
| 835 |
|
| 836 |
if (message) |
| 837 |
dbus_message_unref(message); |
| 838 |
|
| 839 |
if (reply) |
| 840 |
dbus_message_unref(reply); |
| 841 |
|
| 842 |
return r; |
| 843 |
} |
| 844 |
|
| 845 |
uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) { |
| 846 |
DBusMessage *message = NULL, *reply = NULL; |
| 847 |
DBusError error; |
| 848 |
assert(client); |
| 849 |
|
| 850 |
if (!avahi_client_is_connected(client)) { |
| 851 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 852 |
return AVAHI_SERVICE_COOKIE_INVALID; |
| 853 |
} |
| 854 |
|
| 855 |
if (client->local_service_cookie_valid) |
| 856 |
return client->local_service_cookie; |
| 857 |
|
| 858 |
dbus_error_init (&error); |
| 859 |
|
| 860 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetLocalServiceCookie"))) { |
| 861 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 862 |
goto fail; |
| 863 |
} |
| 864 |
|
| 865 |
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); |
| 866 |
|
| 867 |
if (!reply || dbus_error_is_set (&error)) |
| 868 |
goto fail; |
| 869 |
|
| 870 |
if (!dbus_message_get_args (reply, &error, DBUS_TYPE_UINT32, &client->local_service_cookie, DBUS_TYPE_INVALID) || |
| 871 |
dbus_error_is_set (&error)) |
| 872 |
goto fail; |
| 873 |
|
| 874 |
dbus_message_unref(message); |
| 875 |
dbus_message_unref(reply); |
| 876 |
|
| 877 |
client->local_service_cookie_valid = 1; |
| 878 |
return client->local_service_cookie; |
| 879 |
|
| 880 |
fail: |
| 881 |
|
| 882 |
if (message) |
| 883 |
dbus_message_unref(message); |
| 884 |
if (reply) |
| 885 |
dbus_message_unref(reply); |
| 886 |
|
| 887 |
if (dbus_error_is_set(&error)) { |
| 888 |
avahi_client_set_dbus_error(client, &error); |
| 889 |
dbus_error_free(&error); |
| 890 |
} |
| 891 |
|
| 892 |
return AVAHI_SERVICE_COOKIE_INVALID; |
| 893 |
} |
| 894 |
|
| 895 |
int avahi_client_is_connected(AvahiClient *client) { |
| 896 |
assert(client); |
| 897 |
|
| 898 |
return |
| 899 |
client->bus && |
| 900 |
dbus_connection_get_is_connected(client->bus) && |
| 901 |
(client->state == AVAHI_CLIENT_S_RUNNING || client->state == AVAHI_CLIENT_S_REGISTERING || client->state == AVAHI_CLIENT_S_COLLISION); |
| 902 |
} |
| 903 |
|
| 904 |
int avahi_client_set_host_name(AvahiClient* client, const char *name) { |
| 905 |
DBusMessage *message = NULL, *reply = NULL; |
| 906 |
DBusError error; |
| 907 |
|
| 908 |
assert(client); |
| 909 |
|
| 910 |
if (!avahi_client_is_connected(client)) |
| 911 |
return avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 912 |
|
| 913 |
dbus_error_init (&error); |
| 914 |
|
| 915 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "SetHostName"))) { |
| 916 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 917 |
goto fail; |
| 918 |
} |
| 919 |
|
| 920 |
if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) { |
| 921 |
avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY); |
| 922 |
goto fail; |
| 923 |
} |
| 924 |
|
| 925 |
reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error); |
| 926 |
|
| 927 |
if (!reply || dbus_error_is_set (&error)) |
| 928 |
goto fail; |
| 929 |
|
| 930 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 931 |
dbus_error_is_set (&error)) |
| 932 |
goto fail; |
| 933 |
|
| 934 |
dbus_message_unref(message); |
| 935 |
dbus_message_unref(reply); |
| 936 |
|
| 937 |
avahi_free(client->host_name); |
| 938 |
client->host_name = NULL; |
| 939 |
avahi_free(client->host_name_fqdn); |
| 940 |
client->host_name_fqdn = NULL; |
| 941 |
|
| 942 |
return 0; |
| 943 |
|
| 944 |
fail: |
| 945 |
|
| 946 |
if (message) |
| 947 |
dbus_message_unref(message); |
| 948 |
if (reply) |
| 949 |
dbus_message_unref(reply); |
| 950 |
|
| 951 |
if (dbus_error_is_set(&error)) { |
| 952 |
avahi_client_set_dbus_error(client, &error); |
| 953 |
dbus_error_free(&error); |
| 954 |
} |
| 955 |
|
| 956 |
return client->error; |
| 957 |
} |