1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* $Id$ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>
#include <avahi-client/client.h>
#include <avahi-common/dbus.h>
#include <avahi-common/llist.h>
#include <avahi-common/error.h>
#include <avahi-common/malloc.h>
#include "client.h"
#include "internal.h"
void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
{
if (group == NULL || group->callback == NULL)
return;
group->callback (group, state, group->userdata);
}
AvahiEntryGroup*
avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
{
AvahiEntryGroup *tmp = NULL;
DBusMessage *message = NULL, *reply;
DBusError error;
char *path;
if (client == NULL)
return NULL;
dbus_error_init (&error);
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
if (dbus_error_is_set (&error))
{
dbus_error_free (&error);
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
if (reply == NULL)
{
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
if (dbus_error_is_set (&error))
{
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
tmp = avahi_new(AvahiEntryGroup, 1);
tmp->client = client;
tmp->path = avahi_strdup (path);
tmp->callback = callback;
tmp->userdata = user_data;
AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
dbus_message_unref (message);
avahi_client_set_errno (client, AVAHI_OK);
return tmp;
fail:
if (tmp) avahi_free (tmp);
if (message) dbus_message_unref (message);
return NULL;
}
int
avahi_entry_group_free (AvahiEntryGroup *group)
{
AvahiClient *client = group->client;
DBusMessage *message;
if (group == NULL || group->path == NULL)
return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
if (message == NULL)
return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
dbus_connection_send (client->bus, message, NULL);
AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
avahi_free (group);
return avahi_client_set_errno (client, AVAHI_OK);
}
int
avahi_entry_group_commit (AvahiEntryGroup *group)
{
DBusMessage *message;
DBusError error;
dbus_error_init (&error);
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
dbus_connection_send (group->client->bus, message, NULL);
return avahi_client_set_errno (group->client, AVAHI_OK);
}
int
avahi_entry_group_reset (AvahiEntryGroup *group)
{
DBusMessage *message;
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
dbus_connection_send (group->client->bus, message, NULL);
return avahi_client_set_errno (group->client, AVAHI_OK);
}
int
avahi_entry_group_get_state (AvahiEntryGroup *group)
{
DBusMessage *message, *reply;
DBusError error;
int state;
dbus_error_init (&error);
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
if (dbus_error_is_set (&error))
{
dbus_error_free (&error);
return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
}
dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
if (dbus_error_is_set (&error))
{
dbus_error_free (&error);
return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
}
avahi_client_set_errno (group->client, AVAHI_OK);
return state;
}
int
avahi_client_errno (AvahiClient *client)
{
return client->error;
}
AvahiClient*
avahi_entry_group_get_client (AvahiEntryGroup *group)
{
return group->client;
}
int
avahi_entry_group_is_empty (AvahiEntryGroup *group)
{
return AVAHI_OK;
}
int
avahi_entry_group_add_service (AvahiEntryGroup *group,
AvahiIfIndex interface,
AvahiProtocol protocol,
const char *name,
const char *type,
const char *domain,
const char *host,
uint16_t port,
AvahiStringList *txt)
{
DBusMessage *message;
DBusMessageIter iter, sub;
AvahiStringList *p;
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
if (!message)
{
dbus_message_unref (message);
return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
}
if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
{
dbus_message_unref (message);
return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
}
dbus_message_iter_init_append(message, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
/* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
for (p = txt; p != NULL; p = p->next) {
DBusMessageIter sub2;
const uint8_t *data = p->text;
dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
dbus_message_iter_close_container(&sub, &sub2);
}
dbus_message_iter_close_container(&iter, &sub);
dbus_connection_send (group->client->bus, message, NULL);
return avahi_client_set_errno (group->client, AVAHI_OK);
}
/* XXX: debug function */
const char* avahi_entry_group_path (AvahiEntryGroup *group)
{
if (group != NULL) return group->path;
else return NULL;
}
|