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