ea7d37b5c649c4fcb66bd6a8957294c7716b55c6
[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 <stdio.h>
27 #include <string.h>
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection = NULL;
33
34 static GHashTable *provider_hash = NULL;
35
36 static GSList *driver_list = NULL;
37
38 struct connman_provider {
39         struct connman_element element;
40         struct connman_service *vpn_service;
41         char *identifier;
42         char *name;
43         char *type;
44         char *host;
45         char *dns;
46         char *domain;
47         struct connman_provider_driver *driver;
48         void *driver_data;
49 };
50
51 void __connman_provider_append_properties(struct connman_provider *provider,
52                                                         DBusMessageIter *iter)
53 {
54         if (provider->host != NULL)
55                 connman_dbus_dict_append_basic(iter, "Host",
56                                         DBUS_TYPE_STRING, &provider->host);
57
58         if (provider->domain != NULL)
59                 connman_dbus_dict_append_basic(iter, "Domain",
60                                         DBUS_TYPE_STRING, &provider->domain);
61
62         if (provider->type != NULL)
63                 connman_dbus_dict_append_basic(iter, "Type", DBUS_TYPE_STRING,
64                                                  &provider->type);
65 }
66
67 static struct connman_provider *connman_provider_lookup(const char *identifier)
68 {
69         struct connman_provider *provider = NULL;
70
71         provider = g_hash_table_lookup(provider_hash, identifier);
72
73         return provider;
74 }
75
76 static int connman_provider_setup_vpn_ipv4(struct connman_provider *provider,
77                                                 struct connman_element *element)
78 {
79         if (element == NULL || provider == NULL)
80                 return -EINVAL;
81
82         DBG("set vpn type %d", element->type);
83
84         g_free(element->ipv4.address);
85         element->ipv4.address = g_strdup(provider->element.ipv4.address);
86
87         g_free(element->ipv4.peer);
88         element->ipv4.peer = g_strdup(provider->element.ipv4.peer);
89
90         g_free(element->ipv4.netmask);
91         element->ipv4.netmask = g_strdup(provider->element.ipv4.netmask);
92
93         g_free(element->ipv4.gateway);
94         element->ipv4.gateway = g_strdup(provider->element.ipv4.gateway);
95
96         g_free(element->ipv4.broadcast);
97         element->ipv4.broadcast = g_strdup(provider->element.ipv4.broadcast);
98
99         g_free(element->ipv4.pac);
100         element->ipv4.pac = g_strdup(provider->element.ipv4.pac);
101
102         return connman_element_register(element, &provider->element);
103 }
104
105 struct connman_provider *connman_provider_ref(struct connman_provider *provider)
106 {
107         DBG("provider %p", provider);
108
109         if (connman_element_ref(&provider->element) == NULL)
110                 return NULL;
111
112         return provider;
113 }
114
115 void connman_provider_unref(struct connman_provider *provider)
116 {
117         DBG("provider %p", provider);
118
119         connman_element_unref(&provider->element);
120 }
121
122 static gboolean match_driver(struct connman_provider *provider,
123                                 struct connman_provider_driver *driver)
124 {
125         if (g_strcmp0(driver->name, provider->type) == 0)
126                 return TRUE;
127
128         return FALSE;
129 }
130
131 static int provider_probe(struct connman_provider *provider)
132 {
133         GSList *list;
134
135         DBG("provider %p name %s", provider, provider->name);
136
137         if (provider->driver != NULL)
138                 return -EALREADY;
139
140         for (list = driver_list; list; list = list->next) {
141                 struct connman_provider_driver *driver = list->data;
142
143                 if (match_driver(provider, driver) == FALSE)
144                         continue;
145
146                 DBG("driver %p name %s", driver, driver->name);
147
148                 if (driver->probe != NULL && driver->probe(provider) == 0) {
149                         provider->driver = driver;
150                         break;
151                 }
152         }
153
154         if (provider->driver == NULL)
155                 return -ENODEV;
156
157         return 0;
158 }
159
160 int __connman_provider_disconnect(struct connman_provider *provider)
161 {
162         int err;
163
164         DBG("provider %p", provider);
165
166         if (provider->driver != NULL && provider->driver->disconnect != NULL)
167                 err = provider->driver->disconnect(provider);
168         else
169                 return -EOPNOTSUPP;
170
171         if (provider->vpn_service != NULL)
172                 __connman_service_indicate_state(provider->vpn_service,
173                                         CONNMAN_SERVICE_STATE_DISCONNECT);
174         if (err < 0) {
175                 if (err != -EINPROGRESS)
176                         return err;
177
178                 return -EINPROGRESS;
179         }
180
181         return 0;
182 }
183
184 int __connman_provider_connect(struct connman_provider *provider)
185 {
186         int err;
187
188         DBG("provider %p", provider);
189
190         g_free(provider->element.ipv4.address);
191         g_free(provider->element.ipv4.peer);
192         g_free(provider->element.ipv4.netmask);
193         g_free(provider->element.ipv4.gateway);
194         g_free(provider->element.ipv4.broadcast);
195         g_free(provider->element.ipv4.pac);
196
197         provider->element.ipv4.address = NULL;
198         provider->element.ipv4.peer = NULL;
199         provider->element.ipv4.netmask = NULL;
200         provider->element.ipv4.gateway = NULL;
201         provider->element.ipv4.broadcast = NULL;
202         provider->element.ipv4.pac = NULL;
203
204         if (provider->driver != NULL && provider->driver->connect != NULL)
205                 err = provider->driver->connect(provider);
206         else
207                 return -EOPNOTSUPP;
208
209         if (err < 0) {
210                 if (err != -EINPROGRESS)
211                         return err;
212
213                 __connman_service_indicate_state(provider->vpn_service,
214                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
215                 return -EINPROGRESS;
216         }
217
218         return 0;
219 }
220
221 int __connman_provider_remove(const char *path)
222 {
223         struct connman_provider *provider;
224         GHashTableIter iter;
225         gpointer value, key;
226
227         DBG("path %s", path);
228
229         g_hash_table_iter_init(&iter, provider_hash);
230         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
231                 const char *srv_path;
232                 provider = value;
233
234                 if (provider->vpn_service == NULL)
235                         continue;
236
237                 srv_path = __connman_service_get_path(provider->vpn_service);
238
239                 if (g_strcmp0(srv_path, path) == 0) {
240                         DBG("Removing VPN %s", provider->identifier);
241                         g_hash_table_remove(provider_hash,
242                                                 provider->identifier);
243                         return 0;
244                 }
245         }
246
247         return -ENXIO;
248 }
249
250 static int set_connected(struct connman_provider *provider,
251                                         connman_bool_t connected)
252 {
253         struct connman_service *service = provider->vpn_service;
254
255         if (service == NULL)
256                 return -ENODEV;
257
258         if (connected == TRUE) {
259                 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
260                 struct connman_element *element;
261                 char *nameservers = NULL, *name = NULL;
262                 const char *value;
263                 char *second_ns;
264                 int err;
265
266                 __connman_service_indicate_state(provider->vpn_service,
267                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
268
269                 type = CONNMAN_ELEMENT_TYPE_IPV4;
270
271                 element = connman_element_create(NULL);
272                 if (element == NULL)
273                         return -ENOMEM;
274
275                 element->type  = type;
276                 element->index = provider->element.index;
277
278                 err = connman_provider_setup_vpn_ipv4(provider, element);
279                 if (err < 0) {
280                         connman_element_unref(element);
281
282                         __connman_service_indicate_state(service,
283                                                 CONNMAN_SERVICE_STATE_FAILURE);
284
285                         return err;
286                 }
287
288                 __connman_service_indicate_state(service,
289                                                 CONNMAN_SERVICE_STATE_READY);
290
291                 __connman_service_set_domainname(service, provider->domain);
292
293                 name = connman_inet_ifname(provider->element.index);
294
295                 nameservers = g_strdup(provider->dns);
296                 value = nameservers;
297                 second_ns = strchr(value, ' ');
298                 if (second_ns)
299                         *(second_ns++) = 0;
300                 __connman_service_append_nameserver(service, value);
301                 value = second_ns;
302
303                 while (value) {
304                         char *next = strchr(value, ' ');
305                         if (next)
306                                 *(next++) = 0;
307
308                         connman_resolver_append(name, provider->domain, value);
309                         value = next;
310                 }
311
312                 g_free(nameservers);
313                 g_free(name);
314
315         } else {
316                 connman_element_unregister_children(&provider->element);
317                 __connman_service_indicate_state(service,
318                                                 CONNMAN_SERVICE_STATE_IDLE);
319         }
320
321         return 0;
322 }
323
324 int connman_provider_set_state(struct connman_provider *provider,
325                                         enum connman_provider_state state)
326 {
327         if (provider == NULL || provider->vpn_service == NULL)
328                 return -EINVAL;
329
330         switch (state) {
331         case CONNMAN_PROVIDER_STATE_UNKNOWN:
332                 return -EINVAL;
333         case CONNMAN_PROVIDER_STATE_IDLE:
334                 return set_connected(provider, FALSE);
335         case CONNMAN_PROVIDER_STATE_CONNECT:
336                 return __connman_service_indicate_state(provider->vpn_service,
337                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
338         case CONNMAN_PROVIDER_STATE_READY:
339                 return set_connected(provider, TRUE);
340         case CONNMAN_PROVIDER_STATE_DISCONNECT:
341                 return __connman_service_indicate_state(provider->vpn_service,
342                                         CONNMAN_SERVICE_STATE_DISCONNECT);
343         case CONNMAN_PROVIDER_STATE_FAILURE:
344                 return __connman_service_indicate_state(provider->vpn_service,
345                                         CONNMAN_SERVICE_STATE_FAILURE);
346         }
347
348         return -EINVAL;
349 }
350
351 static void unregister_provider(gpointer data)
352 {
353         struct connman_provider *provider = data;
354         struct connman_service *service = provider->vpn_service;
355
356         DBG("provider %p", provider);
357
358         provider->vpn_service = NULL;
359         __connman_service_put(service);
360
361         connman_element_unregister(&provider->element);
362         connman_provider_unref(provider);
363 }
364
365 static void provider_destruct(struct connman_element *element)
366 {
367         struct connman_provider *provider = element->private;
368
369         DBG("provider %p", provider);
370
371         g_free(provider->name);
372         g_free(provider->type);
373         g_free(provider->domain);
374         g_free(provider->identifier);
375         g_free(provider->dns);
376 }
377
378 static void provider_initialize(struct connman_provider *provider)
379 {
380         DBG("provider %p", provider);
381
382         __connman_element_initialize(&provider->element);
383
384         provider->element.private = provider;
385         provider->element.destruct = provider_destruct;
386
387         provider->element.ipv4.address = NULL;
388         provider->element.ipv4.netmask = NULL;
389         provider->element.ipv4.gateway = NULL;
390         provider->element.ipv4.broadcast = NULL;
391         provider->element.ipv4.pac = NULL;
392
393         provider->name = NULL;
394         provider->type = NULL;
395         provider->dns = NULL;
396         provider->domain = NULL;
397         provider->identifier = NULL;
398 }
399
400 static struct connman_provider *connman_provider_new(void)
401 {
402         struct connman_provider *provider;
403
404         provider = g_try_new0(struct connman_provider, 1);
405         if (provider == NULL)
406                 return NULL;
407
408         DBG("provider %p", provider);
409         provider_initialize(provider);
410
411         return provider;
412 }
413
414 static struct connman_provider *connman_provider_get(const char *identifier)
415 {
416         struct connman_provider *provider;
417
418         provider = g_hash_table_lookup(provider_hash, identifier);
419         if (provider != NULL)
420                 return provider;
421
422         provider = connman_provider_new();
423         if (provider == NULL)
424                 return NULL;
425
426         DBG("provider %p", provider);
427
428         provider->identifier = g_strdup(identifier);
429
430         g_hash_table_insert(provider_hash, provider->identifier, provider);
431
432         provider->element.name = g_strdup(identifier);
433         connman_element_register(&provider->element, NULL);
434
435         return provider;
436 }
437
438 static void provider_dbus_ident(char *ident)
439 {
440         int i, len = strlen(ident);
441
442         for (i = 0; i < len; i++) {
443                 if (ident[i] >= '0' && ident[i] <= '9')
444                         continue;
445                 if (ident[i] >= 'a' && ident[i] <= 'z')
446                         continue;
447                 if (ident[i] >= 'A' && ident[i] <= 'Z')
448                         continue;
449                 ident[i] = '_';
450         }
451 }
452
453 int __connman_provider_create_and_connect(DBusMessage *msg)
454 {
455         struct connman_provider *provider;
456         DBusMessageIter iter, array;
457         const char *type = NULL, *name = NULL, *service_path = NULL;
458         const char *host = NULL, *domain = NULL;
459         char *ident;
460         gboolean created = FALSE;
461         int err;
462
463         dbus_message_iter_init(msg, &iter);
464         dbus_message_iter_recurse(&iter, &array);
465
466         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
467                 DBusMessageIter entry, value;
468                 const char *key;
469
470                 dbus_message_iter_recurse(&array, &entry);
471                 dbus_message_iter_get_basic(&entry, &key);
472
473                 dbus_message_iter_next(&entry);
474                 dbus_message_iter_recurse(&entry, &value);
475
476                 switch (dbus_message_iter_get_arg_type(&value)) {
477                 case DBUS_TYPE_STRING:
478                         if (g_str_equal(key, "Type") == TRUE)
479                                 dbus_message_iter_get_basic(&value, &type);
480                         else if (g_str_equal(key, "Name") == TRUE)
481                                 dbus_message_iter_get_basic(&value, &name);
482                         else if (g_str_equal(key, "Host") == TRUE)
483                                 dbus_message_iter_get_basic(&value, &host);
484                         else if (g_str_equal(key, "VPN.Domain") == TRUE)
485                                 dbus_message_iter_get_basic(&value, &domain);
486                         break;
487                 }
488
489                 dbus_message_iter_next(&array);
490         }
491
492         if (host == NULL || domain == NULL) {
493                 err = -EINVAL;
494                 goto failed;
495         }
496
497         DBG("Type %s name %s", type, name);
498
499         if (type == NULL || name == NULL) {
500                 err = -EOPNOTSUPP;
501                 goto failed;
502         }
503
504         ident = g_strdup_printf("%s_%s", host, domain);
505         provider_dbus_ident(ident);
506
507         DBG("ident %s", ident);
508
509         provider = connman_provider_lookup(ident);
510
511         if (provider == NULL) {
512                 created = TRUE;
513                 provider = connman_provider_get(ident);
514                 if (provider) {
515                         provider->host = g_strdup(host);
516                         provider->domain = g_strdup(domain);
517                         provider->name = g_strdup(name);
518                         provider->type = g_strdup(type);
519                 }
520         }
521
522         if (provider == NULL) {
523                 DBG("can not create provider");
524                 err = -EOPNOTSUPP;
525                 goto failed;
526         }
527         dbus_message_iter_init(msg, &iter);
528         dbus_message_iter_recurse(&iter, &array);
529
530         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
531                 DBusMessageIter entry, value;
532                 const char *key, *str;
533
534                 dbus_message_iter_recurse(&array, &entry);
535                 dbus_message_iter_get_basic(&entry, &key);
536
537                 dbus_message_iter_next(&entry);
538                 dbus_message_iter_recurse(&entry, &value);
539
540                 switch (dbus_message_iter_get_arg_type(&value)) {
541                 case DBUS_TYPE_STRING:
542                         dbus_message_iter_get_basic(&value, &str);
543                         connman_provider_set_string(provider, key, str);
544                         break;
545                 }
546
547                 dbus_message_iter_next(&array);
548         }
549
550         g_free(ident);
551
552         if (provider == NULL) {
553                 err = -EOPNOTSUPP;
554                 goto failed;
555         }
556
557         if (created == TRUE)
558                 provider_probe(provider);
559
560         if (provider->vpn_service == NULL)
561                 provider->vpn_service =
562                         __connman_service_create_from_provider(provider);
563         if (provider->vpn_service == NULL) {
564                 err = -EOPNOTSUPP;
565                 goto failed;
566         }
567
568         err = __connman_service_connect(provider->vpn_service);
569         if (err < 0 && err != -EINPROGRESS)
570                 goto failed;
571
572         service_path = __connman_service_get_path(provider->vpn_service);
573         g_dbus_send_reply(connection, msg,
574                                 DBUS_TYPE_OBJECT_PATH, &service_path,
575                                                         DBUS_TYPE_INVALID);
576         return 0;
577
578 failed:
579         if (provider != NULL && created == TRUE) {
580                 DBG("can not connect delete provider");
581                 connman_provider_unref(provider);
582
583                 if (provider->vpn_service != NULL) {
584                         __connman_service_put(provider->vpn_service);
585                         provider->vpn_service = NULL;
586                 }
587         }
588
589         return err;
590 }
591
592 const char * __connman_provider_get_ident(struct connman_provider *provider)
593 {
594         if (provider == NULL)
595                 return NULL;
596
597         return provider->identifier;
598 }
599
600 int connman_provider_set_string(struct connman_provider *provider,
601                                         const char *key, const char *value)
602 {
603         DBG("provider %p key %s value %s", provider, key, value);
604
605         if (g_str_equal(key, "Type") == TRUE) {
606                 g_free(provider->type);
607                 provider->type = g_strdup(value);
608         } else if (g_str_equal(key, "Name") == TRUE) {
609                 g_free(provider->name);
610                 provider->name = g_strdup(value);
611         } else if (g_str_equal(key, "Gateway") == TRUE) {
612                 g_free(provider->element.ipv4.gateway);
613                 provider->element.ipv4.gateway = g_strdup(value);
614         } else if (g_str_equal(key, "Address") == TRUE) {
615                 g_free(provider->element.ipv4.address);
616                 provider->element.ipv4.address = g_strdup(value);
617         } else if (g_str_equal(key, "Peer") == TRUE) {
618                 g_free(provider->element.ipv4.peer);
619                 provider->element.ipv4.peer = g_strdup(value);
620         } else if (g_str_equal(key, "Netmask") == TRUE) {
621                 g_free(provider->element.ipv4.netmask);
622                 provider->element.ipv4.netmask = g_strdup(value);
623         } else if (g_str_equal(key, "PAC") == TRUE) {
624                 g_free(provider->element.ipv4.pac);
625                 provider->element.ipv4.pac = g_strdup(value);
626                 __connman_service_set_proxy_autoconfig(provider->vpn_service,
627                                                                         value);
628         } else if (g_str_equal(key, "DNS") == TRUE) {
629                 g_free(provider->dns);
630                 provider->dns = g_strdup(value);
631         } else if (g_str_equal(key, "Domain") == TRUE) {
632                 g_free(provider->domain);
633                 provider->domain = g_strdup(value);
634         }
635
636         return connman_element_set_string(&provider->element, key, value);
637 }
638
639 const char *connman_provider_get_string(struct connman_provider *provider,
640                                                         const char *key)
641 {
642         DBG("provider %p key %s", provider, key);
643
644         if (g_str_equal(key, "Type") == TRUE)
645                 return provider->type;
646         else if (g_str_equal(key, "Name") == TRUE)
647                 return provider->name;
648
649         return connman_element_get_string(&provider->element, key);
650 }
651
652 void *connman_provider_get_data(struct connman_provider *provider)
653 {
654         return provider->driver_data;
655 }
656
657 void connman_provider_set_data(struct connman_provider *provider, void *data)
658 {
659         provider->driver_data = data;
660 }
661
662 void connman_provider_set_index(struct connman_provider *provider, int index)
663 {
664         struct connman_service *service = provider->vpn_service;
665         struct connman_ipconfig *ipconfig;
666
667         DBG("");
668
669         if (service == NULL)
670                 return;
671
672         ipconfig = __connman_service_get_ipconfig(service);
673
674         if (ipconfig == NULL) {
675                 __connman_service_create_ipconfig(service, index);
676
677                 ipconfig = __connman_service_get_ipconfig(service);
678                 if (ipconfig == NULL) {
679                         DBG("Couldnt create ipconfig");
680                         goto done;
681                 }
682         }
683
684         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_FIXED);
685         __connman_ipconfig_set_index(ipconfig, index);
686
687
688 done:
689         provider->element.index = index;
690 }
691
692 int connman_provider_get_index(struct connman_provider *provider)
693 {
694         return provider->element.index;
695 }
696
697 const char *connman_provider_get_driver_name(struct connman_provider *provider)
698 {
699         return provider->driver->name;
700 }
701
702 static gint compare_priority(gconstpointer a, gconstpointer b)
703 {
704         return 0;
705 }
706
707 static void clean_provider(gpointer key, gpointer value, gpointer user_data)
708 {
709         struct connman_provider *provider = value;
710
711         if (provider->driver != NULL && provider->driver->remove)
712                 provider->driver->remove(provider);
713 }
714
715 int connman_provider_driver_register(struct connman_provider_driver *driver)
716 {
717         DBG("driver %p name %s", driver, driver->name);
718
719         driver_list = g_slist_insert_sorted(driver_list, driver,
720                                                         compare_priority);
721         return 0;
722 }
723
724 void connman_provider_driver_unregister(struct connman_provider_driver *driver)
725 {
726         DBG("driver %p name %s", driver, driver->name);
727
728         driver_list = g_slist_remove(driver_list, driver);
729 }
730
731 static void provider_remove(gpointer key, gpointer value,
732                                                 gpointer user_data)
733 {
734         struct connman_provider *provider = value;
735
736         g_hash_table_remove(provider_hash, provider->identifier);
737 }
738
739 static void provider_offline_mode(connman_bool_t enabled)
740 {
741         DBG("enabled %d", enabled);
742
743         if (enabled == TRUE)
744                 g_hash_table_foreach(provider_hash, provider_remove, NULL);
745
746 }
747
748 static struct connman_notifier provider_notifier = {
749         .name                   = "provider",
750         .offline_mode           = provider_offline_mode,
751 };
752
753 int __connman_provider_init(void)
754 {
755         int err;
756
757         DBG("");
758
759         connection = connman_dbus_get_connection();
760
761         provider_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
762                                                 NULL, unregister_provider);
763
764         err = connman_notifier_register(&provider_notifier);
765         if (err < 0) {
766                 g_hash_table_destroy(provider_hash);
767                 dbus_connection_unref(connection);
768         }
769
770         return err;
771 }
772
773 void __connman_provider_cleanup(void)
774 {
775         DBG("");
776
777         connman_notifier_unregister(&provider_notifier);
778
779         g_hash_table_foreach(provider_hash, clean_provider, NULL);
780
781         g_hash_table_destroy(provider_hash);
782         provider_hash = NULL;
783
784         dbus_connection_unref(connection);
785 }