| 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 |
void avahi_entry_group_set_state(AvahiEntryGroup *group, AvahiEntryGroupState state) { |
| 39 |
assert(group); |
| 40 |
|
| 41 |
if (group->state_valid && group->state == state) |
| 42 |
return; |
| 43 |
|
| 44 |
group->state = state; |
| 45 |
group->state_valid = 1; |
| 46 |
|
| 47 |
if (group->callback) |
| 48 |
group->callback(group, state, group->userdata); |
| 49 |
} |
| 50 |
|
| 51 |
static int retrieve_state(AvahiEntryGroup *group) { |
| 52 |
DBusMessage *message = NULL, *reply = NULL; |
| 53 |
DBusError error; |
| 54 |
int r = AVAHI_OK; |
| 55 |
int32_t state; |
| 56 |
AvahiClient *client; |
| 57 |
|
| 58 |
dbus_error_init(&error); |
| 59 |
|
| 60 |
assert(group); |
| 61 |
client = group->client; |
| 62 |
|
| 63 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) { |
| 64 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 65 |
goto fail; |
| 66 |
} |
| 67 |
|
| 68 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 69 |
dbus_error_is_set (&error)) { |
| 70 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 71 |
goto fail; |
| 72 |
} |
| 73 |
|
| 74 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) || |
| 75 |
dbus_error_is_set (&error)) { |
| 76 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 77 |
goto fail; |
| 78 |
} |
| 79 |
|
| 80 |
dbus_message_unref(message); |
| 81 |
dbus_message_unref(reply); |
| 82 |
|
| 83 |
return state; |
| 84 |
|
| 85 |
fail: |
| 86 |
if (dbus_error_is_set(&error)) { |
| 87 |
r = avahi_client_set_dbus_error(client, &error); |
| 88 |
dbus_error_free(&error); |
| 89 |
} |
| 90 |
|
| 91 |
if (message) |
| 92 |
dbus_message_unref(message); |
| 93 |
|
| 94 |
if (reply) |
| 95 |
dbus_message_unref(reply); |
| 96 |
|
| 97 |
return r; |
| 98 |
} |
| 99 |
|
| 100 |
AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *userdata) { |
| 101 |
AvahiEntryGroup *group = NULL; |
| 102 |
DBusMessage *message = NULL, *reply = NULL; |
| 103 |
DBusError error; |
| 104 |
char *path; |
| 105 |
int state; |
| 106 |
|
| 107 |
assert(client); |
| 108 |
|
| 109 |
dbus_error_init (&error); |
| 110 |
|
| 111 |
if (!avahi_client_is_connected(client)) { |
| 112 |
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); |
| 113 |
goto fail; |
| 114 |
} |
| 115 |
|
| 116 |
if (!(group = avahi_new(AvahiEntryGroup, 1))) { |
| 117 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 118 |
goto fail; |
| 119 |
} |
| 120 |
|
| 121 |
group->client = client; |
| 122 |
group->callback = callback; |
| 123 |
group->userdata = userdata; |
| 124 |
group->state_valid = 0; |
| 125 |
group->path = NULL; |
| 126 |
AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group); |
| 127 |
|
| 128 |
if (!(message = dbus_message_new_method_call( |
| 129 |
AVAHI_DBUS_NAME, |
| 130 |
AVAHI_DBUS_PATH_SERVER, |
| 131 |
AVAHI_DBUS_INTERFACE_SERVER, |
| 132 |
"EntryGroupNew"))) { |
| 133 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 134 |
goto fail; |
| 135 |
} |
| 136 |
|
| 137 |
if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) || |
| 138 |
dbus_error_is_set (&error)) { |
| 139 |
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); |
| 140 |
goto fail; |
| 141 |
} |
| 142 |
|
| 143 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || |
| 144 |
dbus_error_is_set (&error)) { |
| 145 |
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); |
| 146 |
goto fail; |
| 147 |
} |
| 148 |
|
| 149 |
if (!(group->path = avahi_strdup (path))) { |
| 150 |
|
| 151 |
/* FIXME: We don't remove the object on the server side */ |
| 152 |
|
| 153 |
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 154 |
goto fail; |
| 155 |
} |
| 156 |
|
| 157 |
if ((state = retrieve_state(group)) < 0) { |
| 158 |
avahi_client_set_errno(client, state); |
| 159 |
goto fail; |
| 160 |
} |
| 161 |
|
| 162 |
avahi_entry_group_set_state(group, (AvahiEntryGroupState) state); |
| 163 |
|
| 164 |
dbus_message_unref(message); |
| 165 |
dbus_message_unref(reply); |
| 166 |
|
| 167 |
return group; |
| 168 |
|
| 169 |
fail: |
| 170 |
if (dbus_error_is_set(&error)) { |
| 171 |
avahi_client_set_dbus_error(client, &error); |
| 172 |
dbus_error_free(&error); |
| 173 |
} |
| 174 |
|
| 175 |
if (group) |
| 176 |
avahi_entry_group_free(group); |
| 177 |
|
| 178 |
if (message) |
| 179 |
dbus_message_unref(message); |
| 180 |
|
| 181 |
if (reply) |
| 182 |
dbus_message_unref(reply); |
| 183 |
|
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) { |
| 188 |
DBusMessage *message = NULL, *reply = NULL; |
| 189 |
DBusError error; |
| 190 |
int r = AVAHI_OK; |
| 191 |
AvahiClient *client; |
| 192 |
|
| 193 |
dbus_error_init(&error); |
| 194 |
|
| 195 |
assert(group); |
| 196 |
client = group->client; |
| 197 |
|
| 198 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) { |
| 199 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 200 |
goto fail; |
| 201 |
} |
| 202 |
|
| 203 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 204 |
dbus_error_is_set (&error)) { |
| 205 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 206 |
goto fail; |
| 207 |
} |
| 208 |
|
| 209 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 210 |
dbus_error_is_set (&error)) { |
| 211 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 212 |
goto fail; |
| 213 |
} |
| 214 |
|
| 215 |
dbus_message_unref(message); |
| 216 |
dbus_message_unref(reply); |
| 217 |
|
| 218 |
return AVAHI_OK; |
| 219 |
|
| 220 |
fail: |
| 221 |
if (dbus_error_is_set(&error)) { |
| 222 |
r = avahi_client_set_dbus_error(client, &error); |
| 223 |
dbus_error_free(&error); |
| 224 |
} |
| 225 |
|
| 226 |
if (message) |
| 227 |
dbus_message_unref(message); |
| 228 |
|
| 229 |
if (reply) |
| 230 |
dbus_message_unref(reply); |
| 231 |
|
| 232 |
return r; |
| 233 |
} |
| 234 |
|
| 235 |
int avahi_entry_group_free(AvahiEntryGroup *group) { |
| 236 |
AvahiClient *client = group->client; |
| 237 |
int r = AVAHI_OK; |
| 238 |
|
| 239 |
assert(group); |
| 240 |
|
| 241 |
if (group->path && avahi_client_is_connected(client)) |
| 242 |
r = entry_group_simple_method_call(group, "Free"); |
| 243 |
|
| 244 |
AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group); |
| 245 |
|
| 246 |
avahi_free(group->path); |
| 247 |
avahi_free(group); |
| 248 |
|
| 249 |
return r; |
| 250 |
} |
| 251 |
|
| 252 |
int avahi_entry_group_commit(AvahiEntryGroup *group) { |
| 253 |
int ret; |
| 254 |
assert(group); |
| 255 |
|
| 256 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 257 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 258 |
|
| 259 |
if ((ret = entry_group_simple_method_call(group, "Commit")) < 0) |
| 260 |
return ret; |
| 261 |
|
| 262 |
group->state_valid = 0; |
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
int avahi_entry_group_reset(AvahiEntryGroup *group) { |
| 267 |
int ret; |
| 268 |
assert(group); |
| 269 |
|
| 270 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 271 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 272 |
|
| 273 |
if ((ret = entry_group_simple_method_call(group, "Reset")) < 0) |
| 274 |
return ret; |
| 275 |
|
| 276 |
group->state_valid = 0; |
| 277 |
return ret; |
| 278 |
} |
| 279 |
|
| 280 |
int avahi_entry_group_get_state (AvahiEntryGroup *group) { |
| 281 |
assert (group); |
| 282 |
|
| 283 |
if (group->state_valid) |
| 284 |
return group->state; |
| 285 |
|
| 286 |
return retrieve_state(group); |
| 287 |
} |
| 288 |
|
| 289 |
AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) { |
| 290 |
assert(group); |
| 291 |
|
| 292 |
return group->client; |
| 293 |
} |
| 294 |
|
| 295 |
int avahi_entry_group_is_empty (AvahiEntryGroup *group) { |
| 296 |
DBusMessage *message = NULL, *reply = NULL; |
| 297 |
DBusError error; |
| 298 |
int r = AVAHI_OK; |
| 299 |
int b; |
| 300 |
AvahiClient *client; |
| 301 |
|
| 302 |
assert(group); |
| 303 |
client = group->client; |
| 304 |
|
| 305 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 306 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 307 |
|
| 308 |
dbus_error_init(&error); |
| 309 |
|
| 310 |
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) { |
| 311 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 312 |
goto fail; |
| 313 |
} |
| 314 |
|
| 315 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 316 |
dbus_error_is_set (&error)) { |
| 317 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 318 |
goto fail; |
| 319 |
} |
| 320 |
|
| 321 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) || |
| 322 |
dbus_error_is_set (&error)) { |
| 323 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 324 |
goto fail; |
| 325 |
} |
| 326 |
|
| 327 |
dbus_message_unref(message); |
| 328 |
dbus_message_unref(reply); |
| 329 |
|
| 330 |
return !!b; |
| 331 |
|
| 332 |
fail: |
| 333 |
if (dbus_error_is_set(&error)) { |
| 334 |
r = avahi_client_set_dbus_error(client, &error); |
| 335 |
dbus_error_free(&error); |
| 336 |
} |
| 337 |
|
| 338 |
if (message) |
| 339 |
dbus_message_unref(message); |
| 340 |
|
| 341 |
if (reply) |
| 342 |
dbus_message_unref(reply); |
| 343 |
|
| 344 |
return r; |
| 345 |
} |
| 346 |
|
| 347 |
static int append_rdata(DBusMessage *message, const void *rdata, size_t size) { |
| 348 |
DBusMessageIter iter, sub; |
| 349 |
|
| 350 |
assert(message); |
| 351 |
|
| 352 |
dbus_message_iter_init_append(message, &iter); |
| 353 |
|
| 354 |
if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) || |
| 355 |
!(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) || |
| 356 |
!(dbus_message_iter_close_container(&iter, &sub))) |
| 357 |
return -1; |
| 358 |
|
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
static int append_string_list(DBusMessage *message, AvahiStringList *txt) { |
| 363 |
DBusMessageIter iter, sub; |
| 364 |
int r = -1; |
| 365 |
AvahiStringList *p; |
| 366 |
|
| 367 |
assert(message); |
| 368 |
|
| 369 |
dbus_message_iter_init_append(message, &iter); |
| 370 |
|
| 371 |
/* Reverse the string list, so that we can pass it in-order to the server */ |
| 372 |
txt = avahi_string_list_reverse(txt); |
| 373 |
|
| 374 |
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub)) |
| 375 |
goto fail; |
| 376 |
|
| 377 |
/* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */ |
| 378 |
for (p = txt; p != NULL; p = p->next) { |
| 379 |
DBusMessageIter sub2; |
| 380 |
const uint8_t *data = p->text; |
| 381 |
|
| 382 |
if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) || |
| 383 |
!(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) || |
| 384 |
!(dbus_message_iter_close_container(&sub, &sub2))) |
| 385 |
goto fail; |
| 386 |
} |
| 387 |
|
| 388 |
if (!dbus_message_iter_close_container(&iter, &sub)) |
| 389 |
goto fail; |
| 390 |
|
| 391 |
r = 0; |
| 392 |
|
| 393 |
fail: |
| 394 |
|
| 395 |
/* Reverse the string list to the original state */ |
| 396 |
txt = avahi_string_list_reverse(txt); |
| 397 |
|
| 398 |
return r; |
| 399 |
} |
| 400 |
|
| 401 |
int avahi_entry_group_add_service_strlst( |
| 402 |
AvahiEntryGroup *group, |
| 403 |
AvahiIfIndex interface, |
| 404 |
AvahiProtocol protocol, |
| 405 |
AvahiPublishFlags flags, |
| 406 |
const char *name, |
| 407 |
const char *type, |
| 408 |
const char *domain, |
| 409 |
const char *host, |
| 410 |
uint16_t port, |
| 411 |
AvahiStringList *txt) { |
| 412 |
|
| 413 |
DBusMessage *message = NULL, *reply = NULL; |
| 414 |
int r = AVAHI_OK; |
| 415 |
DBusError error; |
| 416 |
AvahiClient *client; |
| 417 |
int32_t i_interface, i_protocol; |
| 418 |
uint32_t u_flags; |
| 419 |
|
| 420 |
assert(group); |
| 421 |
assert(name); |
| 422 |
assert(type); |
| 423 |
|
| 424 |
client = group->client; |
| 425 |
|
| 426 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 427 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 428 |
|
| 429 |
if (!domain) |
| 430 |
domain = ""; |
| 431 |
|
| 432 |
if (!host) |
| 433 |
host = ""; |
| 434 |
|
| 435 |
dbus_error_init(&error); |
| 436 |
|
| 437 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) { |
| 438 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 439 |
goto fail; |
| 440 |
} |
| 441 |
|
| 442 |
i_interface = (int32_t) interface; |
| 443 |
i_protocol = (int32_t) protocol; |
| 444 |
u_flags = (uint32_t) flags; |
| 445 |
|
| 446 |
if (!dbus_message_append_args( |
| 447 |
message, |
| 448 |
DBUS_TYPE_INT32, &i_interface, |
| 449 |
DBUS_TYPE_INT32, &i_protocol, |
| 450 |
DBUS_TYPE_UINT32, &u_flags, |
| 451 |
DBUS_TYPE_STRING, &name, |
| 452 |
DBUS_TYPE_STRING, &type, |
| 453 |
DBUS_TYPE_STRING, &domain, |
| 454 |
DBUS_TYPE_STRING, &host, |
| 455 |
DBUS_TYPE_UINT16, &port, |
| 456 |
DBUS_TYPE_INVALID) || |
| 457 |
append_string_list(message, txt) < 0) { |
| 458 |
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY); |
| 459 |
goto fail; |
| 460 |
} |
| 461 |
|
| 462 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 463 |
dbus_error_is_set (&error)) { |
| 464 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 465 |
goto fail; |
| 466 |
} |
| 467 |
|
| 468 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 469 |
dbus_error_is_set (&error)) { |
| 470 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 471 |
goto fail; |
| 472 |
} |
| 473 |
|
| 474 |
dbus_message_unref(message); |
| 475 |
dbus_message_unref(reply); |
| 476 |
|
| 477 |
return AVAHI_OK; |
| 478 |
|
| 479 |
fail: |
| 480 |
|
| 481 |
if (dbus_error_is_set(&error)) { |
| 482 |
r = avahi_client_set_dbus_error(client, &error); |
| 483 |
dbus_error_free(&error); |
| 484 |
} |
| 485 |
|
| 486 |
if (message) |
| 487 |
dbus_message_unref(message); |
| 488 |
|
| 489 |
if (reply) |
| 490 |
dbus_message_unref(reply); |
| 491 |
|
| 492 |
return r; |
| 493 |
} |
| 494 |
|
| 495 |
int avahi_entry_group_add_service( |
| 496 |
AvahiEntryGroup *group, |
| 497 |
AvahiIfIndex interface, |
| 498 |
AvahiProtocol protocol, |
| 499 |
AvahiPublishFlags flags, |
| 500 |
const char *name, |
| 501 |
const char *type, |
| 502 |
const char *domain, |
| 503 |
const char *host, |
| 504 |
uint16_t port, |
| 505 |
...) { |
| 506 |
|
| 507 |
va_list va; |
| 508 |
int r; |
| 509 |
AvahiStringList *txt; |
| 510 |
|
| 511 |
assert(group); |
| 512 |
|
| 513 |
va_start(va, port); |
| 514 |
txt = avahi_string_list_new_va(va); |
| 515 |
r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt); |
| 516 |
avahi_string_list_free(txt); |
| 517 |
va_end(va); |
| 518 |
return r; |
| 519 |
} |
| 520 |
|
| 521 |
int avahi_entry_group_add_service_subtype( |
| 522 |
AvahiEntryGroup *group, |
| 523 |
AvahiIfIndex interface, |
| 524 |
AvahiProtocol protocol, |
| 525 |
AvahiPublishFlags flags, |
| 526 |
const char *name, |
| 527 |
const char *type, |
| 528 |
const char *domain, |
| 529 |
const char *subtype) { |
| 530 |
|
| 531 |
DBusMessage *message = NULL, *reply = NULL; |
| 532 |
int r = AVAHI_OK; |
| 533 |
DBusError error; |
| 534 |
AvahiClient *client; |
| 535 |
int32_t i_interface, i_protocol; |
| 536 |
uint32_t u_flags; |
| 537 |
|
| 538 |
assert(group); |
| 539 |
assert(name); |
| 540 |
assert(type); |
| 541 |
assert(subtype); |
| 542 |
|
| 543 |
client = group->client; |
| 544 |
|
| 545 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 546 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 547 |
|
| 548 |
if (!domain) |
| 549 |
domain = ""; |
| 550 |
|
| 551 |
dbus_error_init(&error); |
| 552 |
|
| 553 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) { |
| 554 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 555 |
goto fail; |
| 556 |
} |
| 557 |
|
| 558 |
i_interface = (int32_t) interface; |
| 559 |
i_protocol = (int32_t) protocol; |
| 560 |
u_flags = (uint32_t) flags; |
| 561 |
|
| 562 |
if (!dbus_message_append_args( |
| 563 |
message, |
| 564 |
DBUS_TYPE_INT32, &i_interface, |
| 565 |
DBUS_TYPE_INT32, &i_protocol, |
| 566 |
DBUS_TYPE_UINT32, &u_flags, |
| 567 |
DBUS_TYPE_STRING, &name, |
| 568 |
DBUS_TYPE_STRING, &type, |
| 569 |
DBUS_TYPE_STRING, &domain, |
| 570 |
DBUS_TYPE_STRING, &subtype, |
| 571 |
DBUS_TYPE_INVALID)) { |
| 572 |
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY); |
| 573 |
goto fail; |
| 574 |
} |
| 575 |
|
| 576 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 577 |
dbus_error_is_set (&error)) { |
| 578 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 579 |
goto fail; |
| 580 |
} |
| 581 |
|
| 582 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 583 |
dbus_error_is_set (&error)) { |
| 584 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 585 |
goto fail; |
| 586 |
} |
| 587 |
|
| 588 |
dbus_message_unref(message); |
| 589 |
dbus_message_unref(reply); |
| 590 |
|
| 591 |
return AVAHI_OK; |
| 592 |
|
| 593 |
fail: |
| 594 |
|
| 595 |
if (dbus_error_is_set(&error)) { |
| 596 |
r = avahi_client_set_dbus_error(client, &error); |
| 597 |
dbus_error_free(&error); |
| 598 |
} |
| 599 |
|
| 600 |
if (message) |
| 601 |
dbus_message_unref(message); |
| 602 |
|
| 603 |
if (reply) |
| 604 |
dbus_message_unref(reply); |
| 605 |
|
| 606 |
return r; |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
int avahi_entry_group_update_service_txt( |
| 611 |
AvahiEntryGroup *group, |
| 612 |
AvahiIfIndex interface, |
| 613 |
AvahiProtocol protocol, |
| 614 |
AvahiPublishFlags flags, |
| 615 |
const char *name, |
| 616 |
const char *type, |
| 617 |
const char *domain, |
| 618 |
...) { |
| 619 |
|
| 620 |
va_list va; |
| 621 |
int r; |
| 622 |
AvahiStringList *txt; |
| 623 |
|
| 624 |
va_start(va, domain); |
| 625 |
txt = avahi_string_list_new_va(va); |
| 626 |
r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt); |
| 627 |
avahi_string_list_free(txt); |
| 628 |
va_end(va); |
| 629 |
return r; |
| 630 |
} |
| 631 |
|
| 632 |
int avahi_entry_group_update_service_txt_strlst( |
| 633 |
AvahiEntryGroup *group, |
| 634 |
AvahiIfIndex interface, |
| 635 |
AvahiProtocol protocol, |
| 636 |
AvahiPublishFlags flags, |
| 637 |
const char *name, |
| 638 |
const char *type, |
| 639 |
const char *domain, |
| 640 |
AvahiStringList *txt) { |
| 641 |
|
| 642 |
DBusMessage *message = NULL, *reply = NULL; |
| 643 |
int r = AVAHI_OK; |
| 644 |
DBusError error; |
| 645 |
AvahiClient *client; |
| 646 |
int32_t i_interface, i_protocol; |
| 647 |
uint32_t u_flags; |
| 648 |
|
| 649 |
assert(group); |
| 650 |
assert(name); |
| 651 |
assert(type); |
| 652 |
|
| 653 |
client = group->client; |
| 654 |
|
| 655 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 656 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 657 |
|
| 658 |
if (!domain) |
| 659 |
domain = ""; |
| 660 |
|
| 661 |
dbus_error_init(&error); |
| 662 |
|
| 663 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) { |
| 664 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 665 |
goto fail; |
| 666 |
} |
| 667 |
|
| 668 |
i_interface = (int32_t) interface; |
| 669 |
i_protocol = (int32_t) protocol; |
| 670 |
u_flags = (uint32_t) flags; |
| 671 |
|
| 672 |
if (!dbus_message_append_args( |
| 673 |
message, |
| 674 |
DBUS_TYPE_INT32, &i_interface, |
| 675 |
DBUS_TYPE_INT32, &i_protocol, |
| 676 |
DBUS_TYPE_UINT32, &u_flags, |
| 677 |
DBUS_TYPE_STRING, &name, |
| 678 |
DBUS_TYPE_STRING, &type, |
| 679 |
DBUS_TYPE_STRING, &domain, |
| 680 |
DBUS_TYPE_INVALID) || |
| 681 |
append_string_list(message, txt) < 0) { |
| 682 |
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY); |
| 683 |
goto fail; |
| 684 |
} |
| 685 |
|
| 686 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 687 |
dbus_error_is_set (&error)) { |
| 688 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 689 |
goto fail; |
| 690 |
} |
| 691 |
|
| 692 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 693 |
dbus_error_is_set (&error)) { |
| 694 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 695 |
goto fail; |
| 696 |
} |
| 697 |
|
| 698 |
dbus_message_unref(message); |
| 699 |
dbus_message_unref(reply); |
| 700 |
|
| 701 |
return AVAHI_OK; |
| 702 |
|
| 703 |
fail: |
| 704 |
|
| 705 |
if (dbus_error_is_set(&error)) { |
| 706 |
r = avahi_client_set_dbus_error(client, &error); |
| 707 |
dbus_error_free(&error); |
| 708 |
} |
| 709 |
|
| 710 |
if (message) |
| 711 |
dbus_message_unref(message); |
| 712 |
|
| 713 |
if (reply) |
| 714 |
dbus_message_unref(reply); |
| 715 |
|
| 716 |
return r; |
| 717 |
} |
| 718 |
|
| 719 |
/** Add a host/address pair */ |
| 720 |
int avahi_entry_group_add_address( |
| 721 |
AvahiEntryGroup *group, |
| 722 |
AvahiIfIndex interface, |
| 723 |
AvahiProtocol protocol, |
| 724 |
AvahiPublishFlags flags, |
| 725 |
const char *name, |
| 726 |
const AvahiAddress *a) { |
| 727 |
|
| 728 |
DBusMessage *message = NULL, *reply = NULL; |
| 729 |
int r = AVAHI_OK; |
| 730 |
DBusError error; |
| 731 |
AvahiClient *client; |
| 732 |
int32_t i_interface, i_protocol; |
| 733 |
uint32_t u_flags; |
| 734 |
char s_address[AVAHI_ADDRESS_STR_MAX]; |
| 735 |
char *p_address = s_address; |
| 736 |
|
| 737 |
assert(name); |
| 738 |
|
| 739 |
client = group->client; |
| 740 |
|
| 741 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 742 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 743 |
|
| 744 |
dbus_error_init(&error); |
| 745 |
|
| 746 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) { |
| 747 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 748 |
goto fail; |
| 749 |
} |
| 750 |
|
| 751 |
i_interface = (int32_t) interface; |
| 752 |
i_protocol = (int32_t) protocol; |
| 753 |
u_flags = (uint32_t) flags; |
| 754 |
|
| 755 |
if (!avahi_address_snprint (s_address, sizeof (s_address), a)) |
| 756 |
{ |
| 757 |
r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS); |
| 758 |
goto fail; |
| 759 |
} |
| 760 |
|
| 761 |
if (!dbus_message_append_args( |
| 762 |
message, |
| 763 |
DBUS_TYPE_INT32, &i_interface, |
| 764 |
DBUS_TYPE_INT32, &i_protocol, |
| 765 |
DBUS_TYPE_UINT32, &u_flags, |
| 766 |
DBUS_TYPE_STRING, &name, |
| 767 |
DBUS_TYPE_STRING, &p_address, |
| 768 |
DBUS_TYPE_INVALID)) { |
| 769 |
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY); |
| 770 |
goto fail; |
| 771 |
} |
| 772 |
|
| 773 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 774 |
dbus_error_is_set (&error)) { |
| 775 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 776 |
goto fail; |
| 777 |
} |
| 778 |
|
| 779 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 780 |
dbus_error_is_set (&error)) { |
| 781 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 782 |
goto fail; |
| 783 |
} |
| 784 |
|
| 785 |
dbus_message_unref(message); |
| 786 |
dbus_message_unref(reply); |
| 787 |
|
| 788 |
return AVAHI_OK; |
| 789 |
|
| 790 |
fail: |
| 791 |
|
| 792 |
if (dbus_error_is_set(&error)) { |
| 793 |
r = avahi_client_set_dbus_error(client, &error); |
| 794 |
dbus_error_free(&error); |
| 795 |
} |
| 796 |
|
| 797 |
if (message) |
| 798 |
dbus_message_unref(message); |
| 799 |
|
| 800 |
if (reply) |
| 801 |
dbus_message_unref(reply); |
| 802 |
|
| 803 |
return r; |
| 804 |
} |
| 805 |
|
| 806 |
/** Add an arbitrary record */ |
| 807 |
int avahi_entry_group_add_record( |
| 808 |
AvahiEntryGroup *group, |
| 809 |
AvahiIfIndex interface, |
| 810 |
AvahiProtocol protocol, |
| 811 |
AvahiPublishFlags flags, |
| 812 |
const char *name, |
| 813 |
uint16_t clazz, |
| 814 |
uint16_t type, |
| 815 |
uint32_t ttl, |
| 816 |
const void *rdata, |
| 817 |
size_t size) { |
| 818 |
|
| 819 |
DBusMessage *message = NULL, *reply = NULL; |
| 820 |
int r = AVAHI_OK; |
| 821 |
DBusError error; |
| 822 |
AvahiClient *client; |
| 823 |
int32_t i_interface, i_protocol; |
| 824 |
uint32_t u_flags; |
| 825 |
|
| 826 |
assert(name); |
| 827 |
|
| 828 |
client = group->client; |
| 829 |
|
| 830 |
if (!group->path || !avahi_client_is_connected(group->client)) |
| 831 |
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); |
| 832 |
|
| 833 |
dbus_error_init(&error); |
| 834 |
|
| 835 |
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) { |
| 836 |
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); |
| 837 |
goto fail; |
| 838 |
} |
| 839 |
|
| 840 |
i_interface = (int32_t) interface; |
| 841 |
i_protocol = (int32_t) protocol; |
| 842 |
u_flags = (uint32_t) flags; |
| 843 |
|
| 844 |
if (!dbus_message_append_args( |
| 845 |
message, |
| 846 |
DBUS_TYPE_INT32, &i_interface, |
| 847 |
DBUS_TYPE_INT32, &i_protocol, |
| 848 |
DBUS_TYPE_UINT32, &u_flags, |
| 849 |
DBUS_TYPE_STRING, &name, |
| 850 |
DBUS_TYPE_UINT16, &clazz, |
| 851 |
DBUS_TYPE_UINT16, &type, |
| 852 |
DBUS_TYPE_UINT32, &ttl, |
| 853 |
DBUS_TYPE_INVALID) || append_rdata(message, rdata, size) < 0) { |
| 854 |
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY); |
| 855 |
goto fail; |
| 856 |
} |
| 857 |
|
| 858 |
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) || |
| 859 |
dbus_error_is_set (&error)) { |
| 860 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 861 |
goto fail; |
| 862 |
} |
| 863 |
|
| 864 |
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) || |
| 865 |
dbus_error_is_set (&error)) { |
| 866 |
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); |
| 867 |
goto fail; |
| 868 |
} |
| 869 |
|
| 870 |
dbus_message_unref(message); |
| 871 |
dbus_message_unref(reply); |
| 872 |
|
| 873 |
return AVAHI_OK; |
| 874 |
|
| 875 |
fail: |
| 876 |
|
| 877 |
if (dbus_error_is_set(&error)) { |
| 878 |
r = avahi_client_set_dbus_error(client, &error); |
| 879 |
dbus_error_free(&error); |
| 880 |
} |
| 881 |
|
| 882 |
if (message) |
| 883 |
dbus_message_unref(message); |
| 884 |
|
| 885 |
if (reply) |
| 886 |
dbus_message_unref(reply); |
| 887 |
|
| 888 |
return r; |
| 889 |
} |