provider: Untangle create and connect code block
[framework/connectivity/connman.git] / src / provider.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gdbus.h>
31
32 #include "connman.h"
33
34 static DBusConnection *connection = NULL;
35
36 static GHashTable *provider_hash = NULL;
37
38 static GSList *driver_list = NULL;
39
40 struct connman_route {
41         int family;
42         char *host;
43         char *netmask;
44         char *gateway;
45 };
46
47 struct connman_provider {
48         gint refcount;
49         struct connman_service *vpn_service;
50         int index;
51         char *identifier;
52         char *name;
53         char *type;
54         char *host;
55         char *domain;
56         int family;
57         GHashTable *routes;
58         struct connman_provider_driver *driver;
59         void *driver_data;
60         GHashTable *setting_strings;
61 };
62
63 void __connman_provider_append_properties(struct connman_provider *provider,
64                                                         DBusMessageIter *iter)
65 {
66         if (provider->host != NULL)
67                 connman_dbus_dict_append_basic(iter, "Host",
68                                         DBUS_TYPE_STRING, &provider->host);
69
70         if (provider->domain != NULL)
71                 connman_dbus_dict_append_basic(iter, "Domain",
72                                         DBUS_TYPE_STRING, &provider->domain);
73
74         if (provider->type != NULL)
75                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
76                                                  &provider->type);
77 }
78
79 static struct connman_provider *connman_provider_lookup(const char *identifier)
80 {
81         struct connman_provider *provider = NULL;
82
83         provider = g_hash_table_lookup(provider_hash, identifier);
84
85         return provider;
86 }
87
88 static gboolean match_driver(struct connman_provider *provider,
89                                 struct connman_provider_driver *driver)
90 {
91         if (g_strcmp0(driver->name, provider->type) == 0)
92                 return TRUE;
93
94         return FALSE;
95 }
96
97 static int provider_probe(struct connman_provider *provider)
98 {
99         GSList *list;
100
101         DBG("provider %p name %s", provider, provider->name);
102
103         if (provider->driver != NULL)
104                 return -EALREADY;
105
106         for (list = driver_list; list; list = list->next) {
107                 struct connman_provider_driver *driver = list->data;
108
109                 if (match_driver(provider, driver) == FALSE)
110                         continue;
111
112                 DBG("driver %p name %s", driver, driver->name);
113
114                 if (driver->probe != NULL && driver->probe(provider) == 0) {
115                         provider->driver = driver;
116                         break;
117                 }
118         }
119
120         if (provider->driver == NULL)
121                 return -ENODEV;
122
123         return 0;
124 }
125
126 static void provider_remove(struct connman_provider *provider)
127 {
128         if (provider->driver != NULL) {
129                 provider->driver->remove(provider);
130                 provider->driver = NULL;
131         }
132 }
133
134 static int provider_register(struct connman_provider *provider)
135 {
136         return provider_probe(provider);
137 }
138
139 static void provider_unregister(struct connman_provider *provider)
140 {
141         provider_remove(provider);
142 }
143
144 struct connman_provider *connman_provider_ref(struct connman_provider *provider)
145 {
146         DBG("provider %p", provider);
147
148         g_atomic_int_inc(&provider->refcount);
149
150         return provider;
151 }
152
153 static void provider_destruct(struct connman_provider *provider)
154 {
155         DBG("provider %p", provider);
156
157         g_free(provider->name);
158         g_free(provider->type);
159         g_free(provider->host);
160         g_free(provider->domain);
161         g_free(provider->identifier);
162         g_hash_table_destroy(provider->routes);
163         g_hash_table_destroy(provider->setting_strings);
164 }
165
166 void connman_provider_unref(struct connman_provider *provider)
167 {
168         DBG("provider %p", provider);
169
170         if (g_atomic_int_dec_and_test(&provider->refcount) == FALSE)
171                 return;
172
173         provider_remove(provider);
174
175         provider_destruct(provider);
176 }
177
178 static int provider_indicate_state(struct connman_provider *provider,
179                                         enum connman_service_state state)
180 {
181         DBG("state %d", state);
182
183         __connman_service_ipconfig_indicate_state(provider->vpn_service, state,
184                                         CONNMAN_IPCONFIG_TYPE_IPV4);
185
186         return __connman_service_ipconfig_indicate_state(provider->vpn_service,
187                                         state, CONNMAN_IPCONFIG_TYPE_IPV6);
188 }
189
190 int __connman_provider_disconnect(struct connman_provider *provider)
191 {
192         int err;
193
194         DBG("provider %p", provider);
195
196         if (provider->driver != NULL && provider->driver->disconnect != NULL)
197                 err = provider->driver->disconnect(provider);
198         else
199                 return -EOPNOTSUPP;
200
201         if (provider->vpn_service != NULL)
202                 provider_indicate_state(provider,
203                                         CONNMAN_SERVICE_STATE_DISCONNECT);
204
205         if (err < 0) {
206                 if (err != -EINPROGRESS)
207                         return err;
208
209                 return -EINPROGRESS;
210         }
211
212         return 0;
213 }
214
215 int __connman_provider_connect(struct connman_provider *provider)
216 {
217         int err;
218
219         DBG("provider %p", provider);
220
221         if (provider->driver != NULL && provider->driver->connect != NULL)
222                 err = provider->driver->connect(provider);
223         else
224                 return -EOPNOTSUPP;
225
226         if (err < 0) {
227                 if (err != -EINPROGRESS)
228                         return err;
229
230                 provider_indicate_state(provider,
231                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
232
233                 return -EINPROGRESS;
234         }
235
236         return 0;
237 }
238
239 int __connman_provider_remove(const char *path)
240 {
241         struct connman_provider *provider;
242         GHashTableIter iter;
243         gpointer value, key;
244
245         DBG("path %s", path);
246
247         g_hash_table_iter_init(&iter, provider_hash);
248         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
249                 const char *srv_path;
250                 provider = value;
251
252                 if (provider->vpn_service == NULL)
253                         continue;
254
255                 srv_path = __connman_service_get_path(provider->vpn_service);
256
257                 if (g_strcmp0(srv_path, path) == 0) {
258                         DBG("Removing VPN %s", provider->identifier);
259
260                         provider_unregister(provider);
261                         g_hash_table_remove(provider_hash,
262                                                 provider->identifier);
263                         return 0;
264                 }
265         }
266
267         return -ENXIO;
268 }
269
270 static void provider_append_routes(gpointer key, gpointer value,
271                                         gpointer user_data)
272 {
273         struct connman_route *route = value;
274         struct connman_provider *provider = user_data;
275         int index = provider->index;
276
277         if (route->family == AF_INET6) {
278                 unsigned char prefix_len = atoi(route->netmask);
279
280                 connman_inet_add_ipv6_network_route(index, route->host,
281                                                         route->gateway,
282                                                         prefix_len);
283         } else {
284                 connman_inet_add_network_route(index, route->host,
285                                                 route->gateway,
286                                                 route->netmask);
287         }
288 }
289
290 static int set_connected(struct connman_provider *provider,
291                                         connman_bool_t connected)
292 {
293         struct connman_service *service = provider->vpn_service;
294         struct connman_ipconfig *ipconfig;
295
296         if (service == NULL)
297                 return -ENODEV;
298
299         ipconfig = __connman_service_get_ipconfig(service, provider->family);
300
301         if (connected == TRUE) {
302                 if (ipconfig == NULL) {
303                         provider_indicate_state(provider,
304                                                 CONNMAN_SERVICE_STATE_FAILURE);
305                         return -EIO;
306                 }
307
308                 __connman_ipconfig_address_add(ipconfig);
309                 __connman_ipconfig_gateway_add(ipconfig);
310
311                 provider_indicate_state(provider,
312                                         CONNMAN_SERVICE_STATE_READY);
313
314                 g_hash_table_foreach(provider->routes, provider_append_routes,
315                                         provider);
316
317         } else {
318                 if (ipconfig != NULL) {
319                         provider_indicate_state(provider,
320                                         CONNMAN_SERVICE_STATE_DISCONNECT);
321                         __connman_ipconfig_gateway_remove(ipconfig);
322                 }
323
324                 provider_indicate_state(provider,
325                                         CONNMAN_SERVICE_STATE_IDLE);
326         }
327
328         return 0;
329 }
330
331 int connman_provider_set_state(struct connman_provider *provider,
332                                         enum connman_provider_state state)
333 {
334         if (provider == NULL || provider->vpn_service == NULL)
335                 return -EINVAL;
336
337         switch (state) {
338         case CONNMAN_PROVIDER_STATE_UNKNOWN:
339                 return -EINVAL;
340         case CONNMAN_PROVIDER_STATE_IDLE:
341                 return set_connected(provider, FALSE);
342         case CONNMAN_PROVIDER_STATE_CONNECT:
343                 return provider_indicate_state(provider,
344                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
345         case CONNMAN_PROVIDER_STATE_READY:
346                 return set_connected(provider, TRUE);
347         case CONNMAN_PROVIDER_STATE_DISCONNECT:
348                 return provider_indicate_state(provider,
349                                         CONNMAN_SERVICE_STATE_DISCONNECT);
350         case CONNMAN_PROVIDER_STATE_FAILURE:
351                 return provider_indicate_state(provider,
352                                         CONNMAN_SERVICE_STATE_FAILURE);
353         }
354
355         return -EINVAL;
356 }
357
358 int connman_provider_indicate_error(struct connman_provider *provider,
359                                         enum connman_provider_error error)
360 {
361         enum connman_service_error service_error;
362
363         switch (error) {
364         case CONNMAN_PROVIDER_ERROR_LOGIN_FAILED:
365                 service_error = CONNMAN_SERVICE_ERROR_LOGIN_FAILED;
366                 break;
367         case CONNMAN_PROVIDER_ERROR_AUTH_FAILED:
368                 service_error = CONNMAN_SERVICE_ERROR_AUTH_FAILED;
369                 break;
370         case CONNMAN_PROVIDER_ERROR_CONNECT_FAILED:
371                 service_error = CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
372                 break;
373         default:
374                 service_error = CONNMAN_SERVICE_ERROR_UNKNOWN;
375                 break;
376         }
377
378         return __connman_service_indicate_error(provider->vpn_service,
379                                                         service_error);
380 }
381
382 static void unregister_provider(gpointer data)
383 {
384         struct connman_provider *provider = data;
385         struct connman_service *service = provider->vpn_service;
386
387         DBG("provider %p", provider);
388
389         provider->vpn_service = NULL;
390         __connman_service_put(service);
391
392         connman_provider_unref(provider);
393 }
394
395 static void destroy_route(gpointer user_data)
396 {
397         struct connman_route *route = user_data;
398
399         g_free(route->host);
400         g_free(route->netmask);
401         g_free(route->gateway);
402         g_free(route);
403 }
404
405 static void provider_initialize(struct connman_provider *provider)
406 {
407         DBG("provider %p", provider);
408
409         provider->index = 0;
410         provider->name = NULL;
411         provider->type = NULL;
412         provider->domain = NULL;
413         provider->identifier = NULL;
414         provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
415                                         NULL, destroy_route);
416         provider->setting_strings = g_hash_table_new_full(g_str_hash, g_str_equal,
417                                                         g_free, g_free);
418 }
419
420 static struct connman_provider *connman_provider_new(void)
421 {
422         struct connman_provider *provider;
423
424         provider = g_try_new0(struct connman_provider, 1);
425         if (provider == NULL)
426                 return NULL;
427
428         provider->refcount = 1;
429
430         DBG("provider %p", provider);
431         provider_initialize(provider);
432
433         return provider;
434 }
435
436 static struct connman_provider *connman_provider_get(const char *identifier)
437 {
438         struct connman_provider *provider;
439
440         provider = g_hash_table_lookup(provider_hash, identifier);
441         if (provider != NULL)
442                 return provider;
443
444         provider = connman_provider_new();
445         if (provider == NULL)
446                 return NULL;
447
448         DBG("provider %p", provider);
449
450         provider->identifier = g_strdup(identifier);
451
452         g_hash_table_insert(provider_hash, provider->identifier, provider);
453
454         provider->name = g_strdup(identifier);
455
456         return provider;
457 }
458
459 static void provider_dbus_ident(char *ident)
460 {
461         int i, len = strlen(ident);
462
463         for (i = 0; i < len; i++) {
464                 if (ident[i] >= '0' && ident[i] <= '9')
465                         continue;
466                 if (ident[i] >= 'a' && ident[i] <= 'z')
467                         continue;
468                 if (ident[i] >= 'A' && ident[i] <= 'Z')
469                         continue;
470                 ident[i] = '_';
471         }
472 }
473
474 int __connman_provider_create_and_connect(DBusMessage *msg)
475 {
476         struct connman_provider *provider;
477         DBusMessageIter iter, array;
478         const char *type = NULL, *name = NULL, *service_path;
479         const char *host = NULL, *domain = NULL;
480         char *ident;
481         int err;
482
483         dbus_message_iter_init(msg, &iter);
484         dbus_message_iter_recurse(&iter, &array);
485
486         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
487                 DBusMessageIter entry, value;
488                 const char *key;
489
490                 dbus_message_iter_recurse(&array, &entry);
491                 dbus_message_iter_get_basic(&entry, &key);
492
493                 dbus_message_iter_next(&entry);
494                 dbus_message_iter_recurse(&entry, &value);
495
496                 switch (dbus_message_iter_get_arg_type(&value)) {
497                 case DBUS_TYPE_STRING:
498                         if (g_str_equal(key, "Type") == TRUE)
499                                 dbus_message_iter_get_basic(&value, &type);
500                         else if (g_str_equal(key, "Name") == TRUE)
501                                 dbus_message_iter_get_basic(&value, &name);
502                         else if (g_str_equal(key, "Host") == TRUE)
503                                 dbus_message_iter_get_basic(&value, &host);
504                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
505                                 dbus_message_iter_get_basic(&value, &domain);
506                         break;
507                 }
508
509                 dbus_message_iter_next(&array);
510         }
511
512         if (host == NULL || domain == NULL)
513                 return -EINVAL;
514
515         DBG("Type %s name %s", type, name);
516
517         if (type == NULL || name == NULL)
518                 return -EOPNOTSUPP;
519
520         ident = g_strdup_printf("%s_%s", host, domain);
521         provider_dbus_ident(ident);
522
523         DBG("ident %s", ident);
524
525         provider = connman_provider_lookup(ident);
526         if (provider == NULL) {
527                 provider = connman_provider_get(ident);
528                 if (provider == NULL) {
529                         DBG("can not create provider");
530                         return -EOPNOTSUPP;
531                 }
532
533                 provider->host = g_strdup(host);
534                 provider->domain = g_strdup(domain);
535                 provider->name = g_strdup(name);
536                 provider->type = g_strdup(type);
537
538                 provider_register(provider);
539         }
540
541         dbus_message_iter_init(msg, &iter);
542         dbus_message_iter_recurse(&iter, &array);
543
544         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
545                 DBusMessageIter entry, value;
546                 const char *key, *str;
547
548                 dbus_message_iter_recurse(&array, &entry);
549                 dbus_message_iter_get_basic(&entry, &key);
550
551                 dbus_message_iter_next(&entry);
552                 dbus_message_iter_recurse(&entry, &value);
553
554                 switch (dbus_message_iter_get_arg_type(&value)) {
555                 case DBUS_TYPE_STRING:
556                         dbus_message_iter_get_basic(&value, &str);
557                         connman_provider_set_string(provider, key, str);
558                         break;
559                 }
560
561                 dbus_message_iter_next(&array);
562         }
563
564         g_free(ident);
565
566         if (provider->vpn_service == NULL) {
567                 provider->vpn_service =
568                         __connman_service_create_from_provider(provider);
569                 if (provider->vpn_service == NULL) {
570                         err = -EOPNOTSUPP;
571                         goto unref;
572                 }
573         }
574
575         err = __connman_service_connect(provider->vpn_service);
576         if (err < 0 && err != -EINPROGRESS)
577                 goto failed;
578
579         service_path = __connman_service_get_path(provider->vpn_service);
580         g_dbus_send_reply(connection, msg,
581                                 DBUS_TYPE_OBJECT_PATH, &service_path,
582                                                         DBUS_TYPE_INVALID);
583         return 0;
584
585 failed:
586         __connman_service_put(provider->vpn_service);
587         provider->vpn_service = NULL;
588
589 unref:
590         DBG("can not connect, delete provider");
591
592         connman_provider_unref(provider);
593
594         return err;
595 }
596
597 const char * __connman_provider_get_ident(struct connman_provider *provider)
598 {
599         if (provider == NULL)
600                 return NULL;
601
602         return provider->identifier;
603 }
604
605 int connman_provider_set_string(struct connman_provider *provider,
606                                         const char *key, const char *value)
607 {
608         DBG("provider %p key %s value %s", provider, key, value);
609
610         if (g_str_equal(key, "Type") == TRUE) {
611                 g_free(provider->type);
612                 provider->type = g_strdup(value);
613         } else if (g_str_equal(key, "Name") == TRUE) {
614                 g_free(provider->name);
615                 provider->name = g_strdup(value);
616         } else if (g_str_equal(key, "Host") == TRUE) {
617                 g_free(provider->host);
618                 provider->host = g_strdup(value);
619         } else if (g_str_equal(key, "VPN.Domain") == TRUE) {
620                 g_free(provider->domain);
621                 provider->domain = g_strdup(value);
622         } else
623                 g_hash_table_replace(provider->setting_strings,
624                                 g_strdup(key), g_strdup(value));
625         return 0;
626 }
627
628 const char *connman_provider_get_string(struct connman_provider *provider,
629                                                         const char *key)
630 {
631         DBG("provider %p key %s", provider, key);
632
633         if (g_str_equal(key, "Type") == TRUE)
634                 return provider->type;
635         else if (g_str_equal(key, "Name") == TRUE)
636                 return provider->name;
637         else if (g_str_equal(key, "Host") == TRUE)
638                 return provider->host;
639         else if (g_str_equal(key, "VPN.Domain") == TRUE)
640                 return provider->domain;
641
642         return g_hash_table_lookup(provider->setting_strings, key);
643 }
644
645 void *connman_provider_get_data(struct connman_provider *provider)
646 {
647         return provider->driver_data;
648 }
649
650 void connman_provider_set_data(struct connman_provider *provider, void *data)
651 {
652         provider->driver_data = data;
653 }
654
655 void connman_provider_set_index(struct connman_provider *provider, int index)
656 {
657         struct connman_service *service = provider->vpn_service;
658         struct connman_ipconfig *ipconfig;
659
660         DBG("");
661
662         if (service == NULL)
663                 return;
664
665         ipconfig = __connman_service_get_ip4config(service);
666
667         if (ipconfig == NULL) {
668                 __connman_service_create_ip4config(service, index);
669
670                 ipconfig = __connman_service_get_ip4config(service);
671                 if (ipconfig == NULL) {
672                         DBG("Couldnt create ipconfig");
673                         goto done;
674                 }
675         }
676
677         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
678         __connman_ipconfig_set_index(ipconfig, index);
679
680
681         ipconfig = __connman_service_get_ip6config(service);
682
683         if (ipconfig == NULL) {
684                 __connman_service_create_ip6config(service, index);
685
686                 ipconfig = __connman_service_get_ip6config(service);
687                 if (ipconfig == NULL) {
688                         DBG("Couldnt create ipconfig for IPv6");
689                         goto done;
690                 }
691         }
692
693         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_OFF);
694         __connman_ipconfig_set_index(ipconfig, index);
695
696 done:
697         provider->index = index;
698 }
699
700 int connman_provider_get_index(struct connman_provider *provider)
701 {
702         return provider->index;
703 }
704
705 int connman_provider_set_ipaddress(struct connman_provider *provider,
706                                         struct connman_ipaddress *ipaddress)
707 {
708         struct connman_ipconfig *ipconfig = NULL;
709
710         ipconfig = __connman_service_get_ipconfig(provider->vpn_service,
711                                                         ipaddress->family);
712         if (ipconfig == NULL)
713                 return -EINVAL;
714
715         provider->family = ipaddress->family;
716
717         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
718         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
719         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
720         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
721         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
722
723         return 0;
724 }
725
726 int connman_provider_set_pac(struct connman_provider *provider, const char *pac)
727 {
728         DBG("provider %p pac %s", provider, pac);
729
730         __connman_service_set_pac(provider->vpn_service, pac);
731
732         return 0;
733 }
734
735
736 int connman_provider_set_domain(struct connman_provider *provider,
737                                         const char *domain)
738 {
739         DBG("provider %p domain %s", provider, domain);
740
741         g_free(provider->domain);
742         provider->domain = g_strdup(domain);
743
744         __connman_service_set_domainname(provider->vpn_service, domain);
745
746         return 0;
747 }
748
749 int connman_provider_set_nameservers(struct connman_provider *provider,
750                                         const char *nameservers)
751 {
752         int i;
753         char **nameservers_array = NULL;
754
755         DBG("provider %p nameservers %s", provider, nameservers);
756
757         __connman_service_nameserver_clear(provider->vpn_service);
758
759         if (nameservers == NULL)
760                 return 0;
761
762         nameservers_array = g_strsplit(nameservers, " ", 0);
763
764         for (i = 0; nameservers_array[i] != NULL; i++) {
765                 __connman_service_nameserver_append(provider->vpn_service,
766                                                         nameservers_array[i]);
767         }
768
769         g_strfreev(nameservers_array);
770
771         return 0;
772 }
773
774 enum provider_route_type {
775         PROVIDER_ROUTE_TYPE_NONE = 0,
776         PROVIDER_ROUTE_TYPE_MASK = 1,
777         PROVIDER_ROUTE_TYPE_ADDR = 2,
778         PROVIDER_ROUTE_TYPE_GW   = 3,
779 };
780
781 static int route_env_parse(struct connman_provider *provider, const char *key,
782                                 int *family, unsigned long *idx,
783                                 enum provider_route_type *type)
784 {
785         char *end;
786         const char *start;
787
788         DBG("name %s", provider->name);
789
790         if (!strcmp(provider->type, "openvpn")) {
791                 if (g_str_has_prefix(key, "route_network_") == TRUE) {
792                         start = key + strlen("route_network_");
793                         *type = PROVIDER_ROUTE_TYPE_ADDR;
794                 } else if (g_str_has_prefix(key, "route_netmask_") == TRUE) {
795                         start = key + strlen("route_netmask_");
796                         *type = PROVIDER_ROUTE_TYPE_MASK;
797                 } else if (g_str_has_prefix(key, "route_gateway_") == TRUE) {
798                         start = key + strlen("route_gateway_");
799                         *type = PROVIDER_ROUTE_TYPE_GW;
800                 } else
801                         return -EINVAL;
802
803                 *family = AF_INET;
804                 *idx = g_ascii_strtoull(start, &end, 10);
805
806         } else if (!strcmp(provider->type, "openconnect")) {
807                 if (g_str_has_prefix(key, "CISCO_SPLIT_INC_") == TRUE) {
808                         *family = AF_INET;
809                         start = key + strlen("CISCO_SPLIT_INC_");
810                 } else if (g_str_has_prefix(key, "CISCO_IPV6_SPLIT_INC_") == TRUE) {
811                         *family = AF_INET6;
812                         start = key + strlen("CISCO_IPV6_SPLIT_INC_");
813                 } else
814                         return -EINVAL;
815
816                 *idx = g_ascii_strtoull(start, &end, 10);
817
818                 if (strncmp(end, "_ADDR", 5) == 0)
819                         *type = PROVIDER_ROUTE_TYPE_ADDR;
820                 else if (strncmp(end, "_MASK", 5) == 0)
821                         *type = PROVIDER_ROUTE_TYPE_MASK;
822                 else if (strncmp(end, "_MASKLEN", 8) == 0 &&
823                                 *family == AF_INET6) {
824                         *type = PROVIDER_ROUTE_TYPE_MASK;
825                 } else
826                         return -EINVAL;
827         }
828
829         return 0;
830 }
831
832 int connman_provider_append_route(struct connman_provider *provider,
833                                         const char *key, const char *value)
834 {
835         struct connman_route *route;
836         int ret, family = 0;
837         unsigned long idx = 0;
838         enum provider_route_type type = PROVIDER_ROUTE_TYPE_NONE;
839
840         DBG("key %s value %s", key, value);
841
842         ret = route_env_parse(provider, key, &family, &idx, &type);
843         if (ret < 0)
844                 return ret;
845
846         DBG("idx %lu family %d type %d", idx, family, type);
847
848         route = g_hash_table_lookup(provider->routes, GINT_TO_POINTER(idx));
849         if (route == NULL) {
850                 route = g_try_new0(struct connman_route, 1);
851                 if (route == NULL) {
852                         connman_error("out of memory");
853                         return -ENOMEM;
854                 }
855
856                 route->family = family;
857
858                 g_hash_table_replace(provider->routes, GINT_TO_POINTER(idx),
859                                                 route);
860         }
861
862         switch (type) {
863         case PROVIDER_ROUTE_TYPE_NONE:
864                 break;
865         case PROVIDER_ROUTE_TYPE_MASK:
866                 route->netmask = g_strdup(value);
867                 break;
868         case PROVIDER_ROUTE_TYPE_ADDR:
869                 route->host = g_strdup(value);
870                 break;
871         case PROVIDER_ROUTE_TYPE_GW:
872                 route->gateway = g_strdup(value);
873                 break;
874         }
875
876         return 0;
877 }
878
879 const char *connman_provider_get_driver_name(struct connman_provider *provider)
880 {
881         return provider->driver->name;
882 }
883
884 static gint compare_priority(gconstpointer a, gconstpointer b)
885 {
886         return 0;
887 }
888
889 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
890 {
891         struct connman_provider *provider = value;
892
893         if (provider->driver != NULL && provider->driver->remove)
894                 provider->driver->remove(provider);
895 }
896
897 int connman_provider_driver_register(struct connman_provider_driver *driver)
898 {
899         DBG("driver %p name %s", driver, driver->name);
900
901         driver_list = g_slist_insert_sorted(driver_list, driver,
902                                                         compare_priority);
903         return 0;
904 }
905
906 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
907 {
908         DBG("driver %p name %s", driver, driver->name);
909
910         driver_list = g_slist_remove(driver_list, driver);
911 }
912
913 static void provider_remove_all(gpointer key, gpointer value,
914                                                 gpointer user_data)
915 {
916         struct connman_provider *provider = value;
917
918         __connman_provider_remove(provider->identifier);
919 }
920
921 static void provider_offline_mode(connman_bool_t enabled)
922 {
923         DBG("enabled %d", enabled);
924
925         if (enabled == TRUE)
926                 g_hash_table_foreach(provider_hash, provider_remove_all, NULL);
927
928 }
929
930 static struct connman_notifier provider_notifier = {
931         .name                   = "provider",
932         .offline_mode           = provider_offline_mode,
933 };
934
935 int __connman_provider_init(void)
936 {
937         int err;
938
939         DBG("");
940
941         connection = connman_dbus_get_connection();
942
943         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
944                                                 NULL, unregister_provider);
945
946         err = connman_notifier_register(&provider_notifier);
947         if (err < 0) {
948                 g_hash_table_destroy(provider_hash);
949                 dbus_connection_unref(connection);
950         }
951
952         return err;
953 }
954
955 void __connman_provider_cleanup(void)
956 {
957         DBG("");
958
959         connman_notifier_unregister(&provider_notifier);
960
961         g_hash_table_foreach(provider_hash, clean_provider, NULL);
962
963         g_hash_table_destroy(provider_hash);
964         provider_hash = NULL;
965
966         dbus_connection_unref(connection);
967 }