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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2013 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gdbus.h>
#include <gweb/gresolv.h>
#include "connman.h"
static DBusConnection *connection = NULL;
static GHashTable *provider_hash = NULL;
static GSList *driver_list = NULL;
struct connman_provider {
int refcount;
bool immutable;
struct connman_service *vpn_service;
int index;
char *identifier;
int family;
struct connman_provider_driver *driver;
void *driver_data;
};
void __connman_provider_append_properties(struct connman_provider *provider,
DBusMessageIter *iter)
{
const char *host, *domain, *type;
if (!provider->driver || !provider->driver->get_property)
return;
host = provider->driver->get_property(provider, "Host");
domain = provider->driver->get_property(provider, "Domain");
type = provider->driver->get_property(provider, "Type");
if (host)
connman_dbus_dict_append_basic(iter, "Host",
DBUS_TYPE_STRING, &host);
if (domain)
connman_dbus_dict_append_basic(iter, "Domain",
DBUS_TYPE_STRING, &domain);
if (type)
connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
&type);
}
struct connman_provider *
connman_provider_ref_debug(struct connman_provider *provider,
const char *file, int line, const char *caller)
{
DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount + 1,
file, line, caller);
__sync_fetch_and_add(&provider->refcount, 1);
return provider;
}
static void provider_remove(struct connman_provider *provider)
{
if (provider->driver) {
provider->driver->remove(provider);
provider->driver = NULL;
}
}
static void provider_destruct(struct connman_provider *provider)
{
DBG("provider %p", provider);
g_free(provider->identifier);
g_free(provider);
}
void connman_provider_unref_debug(struct connman_provider *provider,
const char *file, int line, const char *caller)
{
DBG("%p ref %d by %s:%d:%s()", provider, provider->refcount - 1,
file, line, caller);
if (__sync_fetch_and_sub(&provider->refcount, 1) != 1)
return;
provider_destruct(provider);
}
static int provider_indicate_state(struct connman_provider *provider,
enum connman_service_state state)
{
DBG("state %d", state);
__connman_service_ipconfig_indicate_state(provider->vpn_service, state,
CONNMAN_IPCONFIG_TYPE_IPV4);
return __connman_service_ipconfig_indicate_state(provider->vpn_service,
state, CONNMAN_IPCONFIG_TYPE_IPV6);
}
int connman_provider_disconnect(struct connman_provider *provider)
{
int err;
DBG("provider %p", provider);
if (provider->driver && provider->driver->disconnect)
err = provider->driver->disconnect(provider);
else
return -EOPNOTSUPP;
if (provider->vpn_service)
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
if (err < 0) {
if (err != -EINPROGRESS)
return err;
return -EINPROGRESS;
}
return 0;
}
int connman_provider_remove(struct connman_provider *provider)
{
DBG("Removing VPN %s", provider->identifier);
provider_remove(provider);
connman_provider_set_state(provider, CONNMAN_PROVIDER_STATE_IDLE);
g_hash_table_remove(provider_hash, provider->identifier);
return 0;
}
int __connman_provider_connect(struct connman_provider *provider)
{
int err;
DBG("provider %p", provider);
if (provider->driver && provider->driver->connect)
err = provider->driver->connect(provider);
else
return -EOPNOTSUPP;
if (err < 0) {
if (err != -EINPROGRESS)
return err;
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_ASSOCIATION);
return -EINPROGRESS;
}
return 0;
}
int __connman_provider_remove_by_path(const char *path)
{
struct connman_provider *provider;
GHashTableIter iter;
gpointer value, key;
DBG("path %s", path);
g_hash_table_iter_init(&iter, provider_hash);
while (g_hash_table_iter_next(&iter, &key, &value)) {
const char *srv_path;
provider = value;
if (!provider->vpn_service)
continue;
srv_path = __connman_service_get_path(provider->vpn_service);
if (g_strcmp0(srv_path, path) == 0) {
DBG("Removing VPN %s", provider->identifier);
provider_remove(provider);
connman_provider_set_state(provider,
CONNMAN_PROVIDER_STATE_IDLE);
g_hash_table_remove(provider_hash,
provider->identifier);
return 0;
}
}
return -ENXIO;
}
static int set_connected(struct connman_provider *provider,
bool connected)
{
struct connman_service *service = provider->vpn_service;
struct connman_ipconfig *ipconfig;
if (!service)
return -ENODEV;
ipconfig = __connman_service_get_ipconfig(service, provider->family);
if (connected) {
if (!ipconfig) {
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_FAILURE);
return -EIO;
}
__connman_ipconfig_address_add(ipconfig);
#if defined TIZEN_EXT
__connman_ipconfig_gateway_add(ipconfig, service);
#else
__connman_ipconfig_gateway_add(ipconfig);
#endif
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_READY);
if (provider->driver && provider->driver->set_routes)
provider->driver->set_routes(provider,
CONNMAN_PROVIDER_ROUTE_ALL);
} else {
if (ipconfig) {
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
__connman_ipconfig_gateway_remove(ipconfig);
}
provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_IDLE);
}
return 0;
}
int connman_provider_set_state(struct connman_provider *provider,
enum connman_provider_state state)
{
if (!provider || !provider->vpn_service)
return -EINVAL;
switch (state) {
case CONNMAN_PROVIDER_STATE_UNKNOWN:
return -EINVAL;
case CONNMAN_PROVIDER_STATE_IDLE:
return set_connected(provider, false);
case CONNMAN_PROVIDER_STATE_CONNECT:
return provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_ASSOCIATION);
case CONNMAN_PROVIDER_STATE_READY:
return set_connected(provider, true);
case CONNMAN_PROVIDER_STATE_DISCONNECT:
return provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_DISCONNECT);
case CONNMAN_PROVIDER_STATE_FAILURE:
return provider_indicate_state(provider,
CONNMAN_SERVICE_STATE_FAILURE);
}
return -EINVAL;
}
int connman_provider_indicate_error(struct connman_provider *provider,
enum connman_provider_error error)
{
enum connman_service_error service_error;
switch (error) {
case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
break;
case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
break;
case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
break;
default:
service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
break;
}
return __connman_service_indicate_error(provider->vpn_service,
service_error);
}
int connman_provider_create_service(struct connman_provider *provider)
{
if (provider->vpn_service) {
bool connected;
connected = __connman_service_is_connected_state(
provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV4);
if (connected)
return -EALREADY;
connected = __connman_service_is_connected_state(
provider->vpn_service, CONNMAN_IPCONFIG_TYPE_IPV6);
if (connected)
return -EALREADY;
return 0;
}
provider->vpn_service =
__connman_service_create_from_provider(provider);
if (!provider->vpn_service) {
connman_warn("service creation failed for provider %s",
provider->identifier);
g_hash_table_remove(provider_hash, provider->identifier);
return -EOPNOTSUPP;
}
return 0;
}
bool __connman_provider_is_immutable(struct connman_provider *provider)
{
if (provider)
return provider->immutable;
return false;
}
int connman_provider_set_immutable(struct connman_provider *provider,
bool immutable)
{
if (!provider)
return -EINVAL;
provider->immutable = immutable;
return 0;
}
static struct connman_provider *provider_lookup(const char *identifier)
{
return g_hash_table_lookup(provider_hash, identifier);
}
static void connection_ready(DBusMessage *msg, int error_code, void *user_data)
{
DBusMessage *reply;
const char *identifier = user_data;
DBG("msg %p error %d", msg, error_code);
if (error_code != 0) {
reply = __connman_error_failed(msg, -error_code);
if (!g_dbus_send_message(connection, reply))
DBG("reply %p send failed", reply);
} else {
const char *path;
struct connman_provider *provider;
provider = provider_lookup(identifier);
if (!provider) {
reply = __connman_error_failed(msg, EINVAL);
g_dbus_send_message(connection, reply);
return;
}
path = __connman_service_get_path(provider->vpn_service);
g_dbus_send_reply(connection, msg,
DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
}
}
int __connman_provider_create_and_connect(DBusMessage *msg)
{
struct connman_provider_driver *driver;
if (!driver_list)
return -EINVAL;
driver = driver_list->data;
if (!driver || !driver->create)
return -EINVAL;
DBG("msg %p", msg);
return driver->create(msg, connection_ready);
}
const char *__connman_provider_get_ident(struct connman_provider *provider)
{
if (!provider)
return NULL;
return provider->identifier;
}
int connman_provider_set_string(struct connman_provider *provider,
const char *key, const char *value)
{
if (provider->driver && provider->driver->set_property)
return provider->driver->set_property(provider, key, value);
return 0;
}
const char *connman_provider_get_string(struct connman_provider *provider,
const char *key)
{
if (provider->driver && provider->driver->get_property)
return provider->driver->get_property(provider, key);
return NULL;
}
bool
__connman_provider_check_routes(struct connman_provider *provider)
{
if (!provider)
return false;
if (provider->driver && provider->driver->check_routes)
return provider->driver->check_routes(provider);
return false;
}
void *connman_provider_get_data(struct connman_provider *provider)
{
return provider->driver_data;
}
void connman_provider_set_data(struct connman_provider *provider, void *data)
{
provider->driver_data = data;
}
void connman_provider_set_index(struct connman_provider *provider, int index)
{
struct connman_service *service = provider->vpn_service;
struct connman_ipconfig *ipconfig;
DBG("");
if (!service)
return;
ipconfig = __connman_service_get_ip4config(service);
if (!ipconfig) {
connman_service_create_ip4config(service, index);
ipconfig = __connman_service_get_ip4config(service);
if (!ipconfig) {
DBG("Couldnt create ipconfig");
goto done;
}
}
__connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
__connman_ipconfig_set_index(ipconfig, index);
ipconfig = __connman_service_get_ip6config(service);
if (!ipconfig) {
connman_service_create_ip6config(service, index);
ipconfig = __connman_service_get_ip6config(service);
if (!ipconfig) {
DBG("Couldnt create ipconfig for IPv6");
goto done;
}
}
__connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
__connman_ipconfig_set_index(ipconfig, index);
done:
provider->index = index;
}
int connman_provider_get_index(struct connman_provider *provider)
{
return provider->index;
}
int connman_provider_set_ipaddress(struct connman_provider *provider,
struct connman_ipaddress *ipaddress)
{
struct connman_ipconfig *ipconfig = NULL;
ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
ipaddress->family);
if (!ipconfig)
return -EINVAL;
provider->family = ipaddress->family;
__connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
__connman_ipconfig_set_local(ipconfig, ipaddress->local);
__connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
__connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
__connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
__connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
return 0;
}
int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
{
DBG("provider %p pac %s", provider, pac);
__connman_service_set_pac(provider->vpn_service, pac);
return 0;
}
int connman_provider_set_domain(struct connman_provider *provider,
const char *domain)
{
DBG("provider %p domain %s", provider, domain);
__connman_service_set_domainname(provider->vpn_service, domain);
return 0;
}
int connman_provider_set_nameservers(struct connman_provider *provider,
char * const *nameservers)
{
int i;
DBG("provider %p nameservers %p", provider, nameservers);
__connman_service_nameserver_clear(provider->vpn_service);
if (!nameservers)
return 0;
for (i = 0; nameservers[i]; i++)
__connman_service_nameserver_append(provider->vpn_service,
nameservers[i], false);
return 0;
}
static void unregister_provider(gpointer data)
{
struct connman_provider *provider = data;
DBG("provider %p service %p", provider, provider->vpn_service);
if (provider->vpn_service) {
connman_service_unref(provider->vpn_service);
provider->vpn_service = NULL;
}
connman_provider_unref(provider);
}
static gint compare_priority(gconstpointer a, gconstpointer b)
{
return 0;
}
int connman_provider_driver_register(struct connman_provider_driver *driver)
{
DBG("driver %p name %s", driver, driver->name);
driver_list = g_slist_insert_sorted(driver_list, driver,
compare_priority);
return 0;
}
void connman_provider_driver_unregister(struct connman_provider_driver *driver)
{
DBG("driver %p name %s", driver, driver->name);
driver_list = g_slist_remove(driver_list, driver);
}
void connman_provider_set_driver(struct connman_provider *provider,
struct connman_provider_driver *driver)
{
provider->driver = driver;
}
static void provider_disconnect_all(gpointer key, gpointer value,
gpointer user_data)
{
struct connman_provider *provider = value;
connman_provider_disconnect(provider);
}
static void provider_offline_mode(bool enabled)
{
DBG("enabled %d", enabled);
if (enabled)
g_hash_table_foreach(provider_hash, provider_disconnect_all,
NULL);
}
static void provider_initialize(struct connman_provider *provider)
{
DBG("provider %p", provider);
provider->index = 0;
provider->identifier = NULL;
}
static struct connman_provider *provider_new(void)
{
struct connman_provider *provider;
provider = g_try_new0(struct connman_provider, 1);
if (!provider)
return NULL;
provider->refcount = 1;
DBG("provider %p", provider);
provider_initialize(provider);
return provider;
}
struct connman_provider *connman_provider_get(const char *identifier)
{
struct connman_provider *provider;
provider = g_hash_table_lookup(provider_hash, identifier);
if (provider)
return provider;
provider = provider_new();
if (!provider)
return NULL;
DBG("provider %p", provider);
provider->identifier = g_strdup(identifier);
g_hash_table_insert(provider_hash, provider->identifier, provider);
return provider;
}
void connman_provider_put(struct connman_provider *provider)
{
g_hash_table_remove(provider_hash, provider->identifier);
}
static struct connman_provider *provider_get(int index)
{
GHashTableIter iter;
gpointer value, key;
g_hash_table_iter_init(&iter, provider_hash);
while (g_hash_table_iter_next(&iter, &key, &value)) {
struct connman_provider *provider = value;
if (provider->index == index)
return provider;
}
return NULL;
}
static void provider_service_changed(struct connman_service *service,
enum connman_service_state state)
{
struct connman_provider *provider;
int vpn_index, service_index;
if (!service)
return;
switch (state) {
case CONNMAN_SERVICE_STATE_UNKNOWN:
case CONNMAN_SERVICE_STATE_IDLE:
case CONNMAN_SERVICE_STATE_ASSOCIATION:
case CONNMAN_SERVICE_STATE_CONFIGURATION:
case CONNMAN_SERVICE_STATE_READY:
case CONNMAN_SERVICE_STATE_ONLINE:
return;
case CONNMAN_SERVICE_STATE_DISCONNECT:
case CONNMAN_SERVICE_STATE_FAILURE:
break;
}
service_index = __connman_service_get_index(service);
vpn_index = __connman_connection_get_vpn_index(service_index);
DBG("service %p %s state %d index %d/%d", service,
__connman_service_get_ident(service),
state, service_index, vpn_index);
if (vpn_index < 0)
return;
provider = provider_get(vpn_index);
if (!provider)
return;
DBG("disconnect %p index %d", provider, vpn_index);
connman_provider_disconnect(provider);
return;
}
static struct connman_notifier provider_notifier = {
.name = "provider",
.offline_mode = provider_offline_mode,
.service_state_changed = provider_service_changed,
};
int __connman_provider_init(void)
{
int err;
DBG("");
connection = connman_dbus_get_connection();
provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, unregister_provider);
err = connman_notifier_register(&provider_notifier);
if (err < 0) {
g_hash_table_destroy(provider_hash);
dbus_connection_unref(connection);
}
return err;
}
void __connman_provider_cleanup(void)
{
DBG("");
connman_notifier_unregister(&provider_notifier);
g_hash_table_destroy(provider_hash);
provider_hash = NULL;
dbus_connection_unref(connection);
}
|