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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
*
* Connection Manager
*
* Copyright (C) 2014 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <gdbus.h>
#include "connman.h"
static DBusConnection *connection;
struct _peer_service {
bool registered;
const char *owner;
DBusMessage *pending;
GBytes *specification;
GBytes *query;
int version;
bool master;
};
struct _peer_service_owner {
char *owner;
guint watch;
GList *services;
};
static struct connman_peer_driver *peer_driver;
static GHashTable *owners_map;
static GHashTable *services_map;
static int peer_master;
static void reply_pending(struct _peer_service *service, int error)
{
if (!service->pending)
return;
connman_dbus_reply_pending(service->pending, error, NULL);
service->pending = NULL;
}
static struct _peer_service *find_peer_service(GBytes *specification,
GBytes *query, int version,
const char *owner, bool remove)
{
struct _peer_service *service = NULL;
struct _peer_service_owner *ps_owner;
GList *list;
ps_owner = g_hash_table_lookup(services_map, specification);
if (!ps_owner)
return NULL;
if (owner && g_strcmp0(owner, ps_owner->owner) != 0)
return NULL;
for (list = ps_owner->services; list; list = list->next) {
service = list->data;
if (service->specification == specification)
break;
if (version) {
if (!service->version)
continue;
if (version != service->version)
continue;
}
if (query) {
if (!service->query)
continue;
if (g_bytes_equal(service->query, query))
continue;
}
if (g_bytes_equal(service->specification, specification))
break;
}
if (!service)
return NULL;
if (owner && remove)
ps_owner->services = g_list_delete_link(ps_owner->services,
list);
return service;
}
static void unregister_peer_service(struct _peer_service *service)
{
gsize spec_length, query_length = 0;
const void *spec, *query = NULL;
if (!peer_driver || !service->specification)
return;
spec = g_bytes_get_data(service->specification, &spec_length);
if (service->query)
query = g_bytes_get_data(service->query, &query_length);
peer_driver->unregister_service(spec, spec_length, query,
query_length, service->version);
}
static void remove_peer_service(gpointer user_data)
{
struct _peer_service *service = user_data;
reply_pending(service, ECONNABORTED);
if (service->registered)
unregister_peer_service(service);
if (service->specification) {
if (service->owner) {
find_peer_service(service->specification,
service->query, service->version,
service->owner, true);
}
g_hash_table_remove(services_map, service->specification);
g_bytes_unref(service->specification);
}
if (service->query)
g_bytes_unref(service->query);
if (service->master)
peer_master--;
g_free(service);
}
static void apply_peer_service_removal(gpointer user_data)
{
struct _peer_service *service = user_data;
service->owner = NULL;
remove_peer_service(user_data);
}
static void remove_peer_service_owner(gpointer user_data)
{
struct _peer_service_owner *ps_owner = user_data;
DBG("owner %s", ps_owner->owner);
if (ps_owner->watch > 0)
g_dbus_remove_watch(connection, ps_owner->watch);
if (ps_owner->services) {
g_list_free_full(ps_owner->services,
apply_peer_service_removal);
}
g_free(ps_owner->owner);
g_free(ps_owner);
}
static void owner_disconnect(DBusConnection *conn, void *user_data)
{
struct _peer_service_owner *ps_owner = user_data;
ps_owner->watch = 0;
g_hash_table_remove(owners_map, ps_owner->owner);
}
static void service_registration_result(int result, void *user_data)
{
struct _peer_service *service = user_data;
reply_pending(service, -result);
if (service->registered)
return;
if (result == 0) {
service->registered = true;
if (service->master)
peer_master++;
return;
}
remove_peer_service(service);
}
static int register_peer_service(struct _peer_service *service)
{
gsize spec_length, query_length = 0;
const void *spec, *query = NULL;
if (!peer_driver)
return 0;
spec = g_bytes_get_data(service->specification, &spec_length);
if (service->query)
query = g_bytes_get_data(service->query, &query_length);
return peer_driver->register_service(spec, spec_length, query,
query_length, service->version,
service_registration_result, service);
}
static void register_all_services(gpointer key, gpointer value,
gpointer user_data)
{
struct _peer_service_owner *ps_owner = value;
GList *list;
for (list = ps_owner->services; list; list = list->next) {
struct _peer_service *service = list->data;
if (service->registered)
register_peer_service(service);
}
}
void __connman_peer_service_set_driver(struct connman_peer_driver *driver)
{
peer_driver = driver;
if (!peer_driver)
return;
g_hash_table_foreach(owners_map, register_all_services, NULL);
}
int __connman_peer_service_register(const char *owner, DBusMessage *msg,
const unsigned char *specification,
int specification_length,
const unsigned char *query,
int query_length, int version,
bool master)
{
struct _peer_service_owner *ps_owner;
GBytes *spec, *query_spec = NULL;
struct _peer_service *service;
bool new = false;
int ret = 0;
DBG("owner %s - spec %p/length %d - query %p/length %d - version %d",
owner,specification, specification_length,
query, query_length, version);
if (!specification || specification_length == 0)
return -EINVAL;
ps_owner = g_hash_table_lookup(owners_map, owner);
if (!ps_owner) {
ps_owner = g_try_new0(struct _peer_service_owner, 1);
if (!ps_owner)
return -ENOMEM;
ps_owner->owner = g_strdup(owner);
ps_owner->watch = g_dbus_add_disconnect_watch(connection,
owner, owner_disconnect,
ps_owner, NULL);
g_hash_table_insert(owners_map, ps_owner->owner, ps_owner);
new = true;
}
spec = g_bytes_new(specification, specification_length);
if (query)
query_spec = g_bytes_new(query, query_length);
service = find_peer_service(spec, query_spec, version, NULL, false);
if (service) {
DBG("Found one existing service %p", service);
if (g_strcmp0(service->owner, owner))
ret = -EBUSY;
if (service->pending)
ret = -EINPROGRESS;
else
ret = -EEXIST;
service = NULL;
goto error;
}
service = g_try_new0(struct _peer_service, 1);
if (!service) {
ret = -ENOMEM;
goto error;
}
service->owner = ps_owner->owner;
service->specification = spec;
service->query = query_spec;
service->version = version;
service->master = master;
g_hash_table_insert(services_map, spec, ps_owner);
spec = query_spec = NULL;
ret = register_peer_service(service);
if (ret != 0 && ret != -EINPROGRESS)
goto error;
else if (ret == -EINPROGRESS)
service->pending = dbus_message_ref(msg);
else {
service->registered = true;
if (master)
peer_master++;
}
ps_owner->services = g_list_prepend(ps_owner->services, service);
return ret;
error:
if (spec)
g_bytes_unref(spec);
if (query_spec)
g_bytes_unref(query_spec);
if (service)
remove_peer_service(service);
if (new)
g_hash_table_remove(owners_map, ps_owner->owner);
return ret;
}
int __connman_peer_service_unregister(const char *owner,
const unsigned char *specification,
int specification_length,
const unsigned char *query,
int query_length, int version)
{
struct _peer_service_owner *ps_owner;
GBytes *spec, *query_spec = NULL;
struct _peer_service *service;
DBG("owner %s - spec %p/length %d - query %p/length %d - version %d",
owner,specification, specification_length,
query, query_length, version);
ps_owner = g_hash_table_lookup(owners_map, owner);
if (!ps_owner)
return -ESRCH;
spec = g_bytes_new(specification, specification_length);
if (query)
query_spec = g_bytes_new(query, query_length);
service = find_peer_service(spec, query_spec, version, owner, true);
g_bytes_unref(spec);
g_bytes_unref(query_spec);
if (!service)
return -ESRCH;
remove_peer_service(service);
if (!ps_owner->services)
g_hash_table_remove(owners_map, ps_owner->owner);
return 0;
}
bool connman_peer_service_is_master(void)
{
if (!peer_master || !peer_driver)
return false;
return true;
}
int __connman_peer_service_init(void)
{
DBG("");
connection = connman_dbus_get_connection();
if (!connection)
return -1;
peer_driver = NULL;
owners_map = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
remove_peer_service_owner);
services_map = g_hash_table_new_full(g_bytes_hash, g_bytes_equal,
NULL, NULL);
peer_master = 0;
return 0;
}
void __connman_peer_service_cleanup(void)
{
DBG("");
if (!connection)
return;
g_hash_table_destroy(owners_map);
g_hash_table_destroy(services_map);
peer_master = 0;
dbus_connection_unref(connection);
connection = NULL;
}
|