network: Clear IPv6 autoconfigured routes when disconnected.
[profile/ivi/connman.git] / src / network.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 <string.h>
28
29 #include "connman.h"
30
31 static GSList *network_list = NULL;
32 static GSList *driver_list = NULL;
33 static unsigned int hidden_counter = 0;
34
35 struct connman_network {
36         gint refcount;
37         enum connman_network_type type;
38         connman_bool_t available;
39         connman_bool_t connected;
40         connman_bool_t roaming;
41         connman_bool_t hidden;
42         connman_uint8_t strength;
43         connman_uint16_t frequency;
44         char *identifier;
45         char *name;
46         char *node;
47         char *group;
48         char *path;
49         int index;
50
51         struct connman_network_driver *driver;
52         void *driver_data;
53
54         connman_bool_t connecting;
55         connman_bool_t associating;
56
57         struct connman_device *device;
58
59         struct {
60                 void *ssid;
61                 int ssid_len;
62                 char *mode;
63                 unsigned short channel;
64                 char *security;
65                 char *passphrase;
66                 char *eap;
67                 char *identity;
68                 char *ca_cert_path;
69                 char *client_cert_path;
70                 char *private_key_path;
71                 char *private_key_passphrase;
72                 char *phase2_auth;
73                 connman_bool_t wps;
74                 connman_bool_t use_wps;
75                 char *pin_wps;
76         } wifi;
77
78         struct {
79                 char *nsp_name;
80                 int nsp_name_len;
81         } wimax;
82 };
83
84 static const char *type2string(enum connman_network_type type)
85 {
86         switch (type) {
87         case CONNMAN_NETWORK_TYPE_UNKNOWN:
88         case CONNMAN_NETWORK_TYPE_VENDOR:
89                 break;
90         case CONNMAN_NETWORK_TYPE_ETHERNET:
91                 return "ethernet";
92         case CONNMAN_NETWORK_TYPE_WIFI:
93                 return "wifi";
94         case CONNMAN_NETWORK_TYPE_WIMAX:
95                 return "wimax";
96         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
97         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
98                 return "bluetooth";
99         case CONNMAN_NETWORK_TYPE_CELLULAR:
100                 return "cellular";
101         }
102
103         return NULL;
104 }
105
106 connman_bool_t __connman_network_has_driver(struct connman_network *network)
107 {
108         if (network == NULL || network->driver == NULL)
109                 return FALSE;
110
111         return TRUE;
112 }
113
114 static gboolean match_driver(struct connman_network *network,
115                                         struct connman_network_driver *driver)
116 {
117         if (network->type == driver->type ||
118                         driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
119                 return TRUE;
120
121         return FALSE;
122 }
123
124 static int network_probe(struct connman_network *network)
125 {
126         GSList *list;
127         struct connman_network_driver *driver = NULL;
128
129         DBG("network %p name %s", network, network->name);
130
131         if (network->driver != NULL)
132                 return -EALREADY;
133
134         for (list = driver_list; list; list = list->next) {
135                 driver = list->data;
136
137                 if (match_driver(network, driver) == FALSE)
138                         continue;
139
140                 DBG("driver %p name %s", driver, driver->name);
141
142                 if (driver->probe(network) == 0)
143                         break;
144
145                 driver = NULL;
146         }
147
148         if (driver == NULL)
149                 return -ENODEV;
150
151         if (network->group == NULL)
152                 return -EINVAL;
153
154         switch (network->type) {
155         case CONNMAN_NETWORK_TYPE_UNKNOWN:
156         case CONNMAN_NETWORK_TYPE_VENDOR:
157                 return 0;
158         case CONNMAN_NETWORK_TYPE_ETHERNET:
159         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
160         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
161         case CONNMAN_NETWORK_TYPE_CELLULAR:
162         case CONNMAN_NETWORK_TYPE_WIFI:
163         case CONNMAN_NETWORK_TYPE_WIMAX:
164                 if (__connman_service_create_from_network(network) == NULL)
165                         return -EINVAL;
166         }
167
168         network->driver = driver;
169
170         return 0;
171 }
172
173 static void network_remove(struct connman_network *network)
174 {
175         DBG("network %p name %s", network, network->name);
176
177         if (network->driver == NULL)
178                 return;
179
180         switch (network->type) {
181         case CONNMAN_NETWORK_TYPE_UNKNOWN:
182         case CONNMAN_NETWORK_TYPE_VENDOR:
183                 break;
184         case CONNMAN_NETWORK_TYPE_ETHERNET:
185         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
186         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
187         case CONNMAN_NETWORK_TYPE_CELLULAR:
188         case CONNMAN_NETWORK_TYPE_WIFI:
189         case CONNMAN_NETWORK_TYPE_WIMAX:
190                 if (network->group != NULL) {
191                         __connman_service_remove_from_network(network);
192
193                         g_free(network->group);
194                         network->group = NULL;
195                 }
196                 break;
197         }
198
199         if (network->driver->remove)
200                 network->driver->remove(network);
201
202         network->driver = NULL;
203 }
204
205 static void network_change(struct connman_network *network)
206 {
207         DBG("network %p name %s", network, network->name);
208
209         if (network->connected == FALSE)
210                 return;
211
212         connman_network_unref(network);
213
214         connman_device_set_disconnected(network->device, TRUE);
215
216         if (network->driver && network->driver->disconnect) {
217                 network->driver->disconnect(network);
218                 return;
219         }
220
221         network->connected = FALSE;
222 }
223
224 static void probe_driver(struct connman_network_driver *driver)
225 {
226         GSList *list;
227
228         DBG("driver %p name %s", driver, driver->name);
229
230         for (list = network_list; list != NULL; list = list->next) {
231                 struct connman_network *network = list->data;
232
233                 if (network->driver != NULL)
234                         continue;
235
236                 if (driver->type != network->type)
237                         continue;
238
239                 if (driver->probe(network) < 0)
240                         continue;
241
242                 network->driver = driver;
243         }
244 }
245
246 int connman_network_register(struct connman_network *network)
247 {
248         network_list = g_slist_append(network_list, network);
249
250         return network_probe(network);
251 }
252
253
254 void connman_network_unregister(struct connman_network *network)
255 {
256         network_list = g_slist_remove(network_list, network);
257
258         network_remove(network);
259 }
260
261 static void remove_driver(struct connman_network_driver *driver)
262 {
263         GSList *list;
264
265         DBG("driver %p name %s", driver, driver->name);
266
267         for (list = network_list; list != NULL; list = list->next) {
268                 struct connman_network *network = list->data;
269
270                 if (network->driver == driver)
271                         network_remove(network);
272         }
273 }
274
275 static gint compare_priority(gconstpointer a, gconstpointer b)
276 {
277         const struct connman_network_driver *driver1 = a;
278         const struct connman_network_driver *driver2 = b;
279
280         return driver2->priority - driver1->priority;
281 }
282
283 /**
284  * connman_network_driver_register:
285  * @driver: network driver definition
286  *
287  * Register a new network driver
288  *
289  * Returns: %0 on success
290  */
291 int connman_network_driver_register(struct connman_network_driver *driver)
292 {
293         GSList *list;
294
295         DBG("driver %p name %s", driver, driver->name);
296
297         for (list = driver_list; list; list = list->next) {
298                 struct connman_network_driver *tmp = list->data;
299
300                 if (tmp->type == driver->type)
301                         return -EALREADY;
302
303         }
304
305         driver_list = g_slist_insert_sorted(driver_list, driver,
306                                                         compare_priority);
307
308         probe_driver(driver);
309
310         return 0;
311 }
312
313 /**
314  * connman_network_driver_unregister:
315  * @driver: network driver definition
316  *
317  * Remove a previously registered network driver
318  */
319 void connman_network_driver_unregister(struct connman_network_driver *driver)
320 {
321         DBG("driver %p name %s", driver, driver->name);
322
323         driver_list = g_slist_remove(driver_list, driver);
324
325         remove_driver(driver);
326 }
327
328 static void network_destruct(struct connman_network *network)
329 {
330         DBG("network %p name %s", network, network->name);
331
332         g_free(network->wifi.ssid);
333         g_free(network->wifi.mode);
334         g_free(network->wifi.security);
335         g_free(network->wifi.passphrase);
336         g_free(network->wifi.eap);
337         g_free(network->wifi.identity);
338         g_free(network->wifi.ca_cert_path);
339         g_free(network->wifi.client_cert_path);
340         g_free(network->wifi.private_key_path);
341         g_free(network->wifi.private_key_passphrase);
342         g_free(network->wifi.phase2_auth);
343         g_free(network->wifi.pin_wps);
344
345         g_free(network->path);
346         g_free(network->group);
347         g_free(network->node);
348         g_free(network->name);
349         g_free(network->identifier);
350         g_free(network->path);
351
352         network->device = NULL;
353
354         g_free(network);
355 }
356
357 /**
358  * connman_network_create:
359  * @identifier: network identifier (for example an unqiue name)
360  *
361  * Allocate a new network and assign the #identifier to it.
362  *
363  * Returns: a newly-allocated #connman_network structure
364  */
365 struct connman_network *connman_network_create(const char *identifier,
366                                                 enum connman_network_type type)
367 {
368         struct connman_network *network;
369         char *temp;
370
371         DBG("identifier %s type %d", identifier, type);
372
373         network = g_try_new0(struct connman_network, 1);
374         if (network == NULL)
375                 return NULL;
376
377         DBG("network %p", network);
378
379         network->refcount = 1;
380
381         if (identifier == NULL) {
382                 temp = g_strdup_printf("hidden_%d", hidden_counter++);
383                 network->hidden = TRUE;
384         } else
385                 temp = g_strdup(identifier);
386
387         if (temp == NULL) {
388                 g_free(network);
389                 return NULL;
390         }
391
392         network->type       = type;
393         network->identifier = g_strdup(temp);
394
395         return network;
396 }
397
398 /**
399  * connman_network_ref:
400  * @network: network structure
401  *
402  * Increase reference counter of  network
403  */
404 struct connman_network *connman_network_ref(struct connman_network *network)
405 {
406         DBG("network %p name %s refcount %d", network, network->name,
407                 g_atomic_int_get(&network->refcount) + 1);
408
409         g_atomic_int_inc(&network->refcount);
410
411         return network;
412 }
413
414 /**
415  * connman_network_unref:
416  * @network: network structure
417  *
418  * Decrease reference counter of network
419  */
420 void connman_network_unref(struct connman_network *network)
421 {
422         DBG("network %p name %s refcount %d", network, network->name,
423                 g_atomic_int_get(&network->refcount) - 1);
424
425         if (g_atomic_int_dec_and_test(&network->refcount) == FALSE)
426                 return;
427
428         network_destruct(network);
429 }
430
431 const char *__connman_network_get_type(struct connman_network *network)
432 {
433         return type2string(network->type);
434 }
435
436 /**
437  * connman_network_get_type:
438  * @network: network structure
439  *
440  * Get type of network
441  */
442 enum connman_network_type connman_network_get_type(struct connman_network *network)
443 {
444         return network->type;
445 }
446
447 /**
448  * connman_network_get_identifier:
449  * @network: network structure
450  *
451  * Get identifier of network
452  */
453 const char *connman_network_get_identifier(struct connman_network *network)
454 {
455         return network->identifier;
456 }
457
458 /**
459  * connman_network_set_index:
460  * @network: network structure
461  * @index: index number
462  *
463  * Set index number of network
464  */
465 void connman_network_set_index(struct connman_network *network, int index)
466 {
467         struct connman_service *service;
468         struct connman_ipconfig *ipconfig;
469
470         service = __connman_service_lookup_from_network(network);
471         if (service == NULL)
472                 goto done;
473
474         ipconfig = __connman_service_get_ip4config(service);
475
476         DBG("index %d service %p ip4config %p", network->index,
477                 service, ipconfig);
478
479         if (network->index < 0 && ipconfig == NULL) {
480
481                 ipconfig = __connman_service_get_ip4config(service);
482                 if (ipconfig == NULL)
483                         /*
484                          * This is needed for plugins that havent set their
485                          * ipconfig layer yet, due to not being able to get
486                          * a network index prior to creating a service.
487                          */
488                         __connman_service_create_ip4config(service, index);
489                 else
490                         __connman_ipconfig_set_index(ipconfig, index);
491
492         } else {
493                 /* If index changed, the index of ipconfig must be reset. */
494                 if (ipconfig == NULL)
495                         goto done;
496
497                 __connman_ipconfig_set_index(ipconfig, index);
498         }
499
500 done:
501         network->index = index;
502 }
503
504 /**
505  * connman_network_get_index:
506  * @network: network structure
507  *
508  * Get index number of network
509  */
510 int connman_network_get_index(struct connman_network *network)
511 {
512         return network->index;
513 }
514
515 /**
516  * connman_network_set_group:
517  * @network: network structure
518  * @group: group name
519  *
520  * Set group name for automatic clustering
521  */
522 void connman_network_set_group(struct connman_network *network,
523                                                         const char *group)
524 {
525         switch (network->type) {
526         case CONNMAN_NETWORK_TYPE_UNKNOWN:
527         case CONNMAN_NETWORK_TYPE_VENDOR:
528                 return;
529         case CONNMAN_NETWORK_TYPE_ETHERNET:
530         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
531         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
532         case CONNMAN_NETWORK_TYPE_CELLULAR:
533         case CONNMAN_NETWORK_TYPE_WIFI:
534         case CONNMAN_NETWORK_TYPE_WIMAX:
535                 break;
536         }
537
538         if (g_strcmp0(network->group, group) == 0) {
539                 if (group != NULL)
540                         __connman_service_update_from_network(network);
541                 return;
542         }
543
544         if (network->group != NULL) {
545                 __connman_service_remove_from_network(network);
546
547                 g_free(network->group);
548         }
549
550         network->group = g_strdup(group);
551
552         if (network->group != NULL)
553                 network_probe(network);
554 }
555
556 /**
557  * connman_network_get_group:
558  * @network: network structure
559  *
560  * Get group name for automatic clustering
561  */
562 const char *connman_network_get_group(struct connman_network *network)
563 {
564         return network->group;
565 }
566
567 const char *__connman_network_get_ident(struct connman_network *network)
568 {
569         if (network->device == NULL)
570                 return NULL;
571
572         return connman_device_get_ident(network->device);
573 }
574
575 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
576 {
577         switch (network->type) {
578         case CONNMAN_NETWORK_TYPE_UNKNOWN:
579         case CONNMAN_NETWORK_TYPE_VENDOR:
580         case CONNMAN_NETWORK_TYPE_ETHERNET:
581         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
582         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
583         case CONNMAN_NETWORK_TYPE_CELLULAR:
584         case CONNMAN_NETWORK_TYPE_WIMAX:
585                 break;
586         case CONNMAN_NETWORK_TYPE_WIFI:
587                 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
588                         return TRUE;
589                 if (network->strength > 0 && network->strength < 20)
590                         return TRUE;
591                 break;
592         }
593
594         return FALSE;
595 }
596
597 connman_bool_t connman_network_get_connecting(struct connman_network *network)
598 {
599         return network->connecting;
600 }
601
602 /**
603  * connman_network_set_available:
604  * @network: network structure
605  * @available: availability state
606  *
607  * Change availability state of network (in range)
608  */
609 int connman_network_set_available(struct connman_network *network,
610                                                 connman_bool_t available)
611 {
612         DBG("network %p available %d", network, available);
613
614         if (network->available == available)
615                 return -EALREADY;
616
617         network->available = available;
618
619         return 0;
620 }
621
622 /**
623  * connman_network_get_available:
624  * @network: network structure
625  *
626  * Get network available setting
627  */
628 connman_bool_t connman_network_get_available(struct connman_network *network)
629 {
630         if (network->hidden == TRUE)
631                 return TRUE;
632
633         return network->available;
634 }
635
636 /**
637  * connman_network_set_associating:
638  * @network: network structure
639  * @associating: associating state
640  *
641  * Change associating state of network
642  */
643 int connman_network_set_associating(struct connman_network *network,
644                                                 connman_bool_t associating)
645 {
646         DBG("network %p associating %d", network, associating);
647
648         if (network->associating == associating)
649                 return -EALREADY;
650
651         network->associating = associating;
652
653         if (associating == TRUE) {
654                 struct connman_service *service;
655
656                 service = __connman_service_lookup_from_network(network);
657                 __connman_service_indicate_state(service,
658                                         CONNMAN_SERVICE_STATE_ASSOCIATION,
659                                         CONNMAN_IPCONFIG_TYPE_IPV4);
660                 __connman_service_indicate_state(service,
661                                         CONNMAN_SERVICE_STATE_ASSOCIATION,
662                                         CONNMAN_IPCONFIG_TYPE_IPV6);
663         }
664
665         return 0;
666 }
667
668 static void set_associate_error(struct connman_network *network)
669 {
670         struct connman_service *service;
671
672         if (network->associating == FALSE)
673                 return ;
674
675         network->associating = FALSE;
676
677         service = __connman_service_lookup_from_network(network);
678
679         __connman_service_indicate_state(service,
680                                         CONNMAN_SERVICE_STATE_FAILURE,
681                                         CONNMAN_IPCONFIG_TYPE_IPV4);
682 }
683
684 static void set_configure_error(struct connman_network *network)
685 {
686         struct connman_service *service;
687
688         network->connecting = FALSE;
689
690         service = __connman_service_lookup_from_network(network);
691
692         __connman_service_indicate_state(service,
693                                         CONNMAN_SERVICE_STATE_FAILURE,
694                                         CONNMAN_IPCONFIG_TYPE_IPV4);
695 }
696
697 static void set_invalid_key_error(struct connman_network *network)
698 {
699         struct connman_service *service;
700
701         service = __connman_service_lookup_from_network(network);
702
703         __connman_service_indicate_error(service,
704                                         CONNMAN_SERVICE_ERROR_INVALID_KEY);
705 }
706
707 static void set_connect_error(struct connman_network *network)
708 {
709         struct connman_service *service;
710
711         service = __connman_service_lookup_from_network(network);
712
713         __connman_service_indicate_error(service,
714                                         CONNMAN_SERVICE_ERROR_CONNECT_FAILED);
715 }
716
717 void connman_network_set_ipv4_method(struct connman_network *network,
718                                         enum connman_ipconfig_method method)
719 {
720         struct connman_service *service;
721         struct connman_ipconfig *ipconfig;
722
723         service = __connman_service_lookup_from_network(network);
724         if (service == NULL)
725                 return;
726
727         ipconfig = __connman_service_get_ip4config(service);
728         if (ipconfig == NULL)
729                 return;
730
731         connman_ipconfig_set_method(ipconfig, method);
732 }
733
734 void connman_network_set_ipv6_method(struct connman_network *network,
735                                         enum connman_ipconfig_method method)
736 {
737         struct connman_service *service;
738         struct connman_ipconfig *ipconfig;
739
740         service = __connman_service_lookup_from_network(network);
741         if (service == NULL)
742                 return;
743
744         ipconfig = __connman_service_get_ip6config(service);
745         if (ipconfig == NULL)
746                 return;
747
748         connman_ipconfig_set_method(ipconfig, method);
749 }
750
751 void connman_network_set_error(struct connman_network *network,
752                                         enum connman_network_error error)
753 {
754         DBG("nework %p, error %d", network, error);
755
756         network->connecting = FALSE;
757
758         switch (error) {
759         case CONNMAN_NETWORK_ERROR_UNKNOWN:
760                 return;
761         case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
762                 set_associate_error(network);
763                 break;
764         case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
765                 set_configure_error(network);
766                 break;
767         case CONNMAN_NETWORK_ERROR_INVALID_KEY:
768                 set_invalid_key_error(network);
769                 break;
770         case CONNMAN_NETWORK_ERROR_CONNECT_FAIL:
771                 set_connect_error(network);
772                 break;
773         }
774
775         network_change(network);
776 }
777
778 void connman_network_clear_error(struct connman_network *network)
779 {
780         struct connman_service *service;
781
782         DBG("network %p", network);
783
784         if (network == NULL)
785                 return;
786
787         if (network->connecting == TRUE || network->associating == TRUE)
788                 return;
789
790         service = __connman_service_lookup_from_network(network);
791         __connman_service_clear_error(service);
792 }
793
794 static void set_configuration(struct connman_network *network)
795 {
796         struct connman_service *service;
797
798         DBG("network %p", network);
799
800         __connman_device_increase_connections(network->device);
801
802         __connman_device_set_network(network->device, network);
803
804         connman_device_set_disconnected(network->device, FALSE);
805
806         service = __connman_service_lookup_from_network(network);
807         __connman_service_indicate_state(service,
808                                         CONNMAN_SERVICE_STATE_CONFIGURATION,
809                                         CONNMAN_IPCONFIG_TYPE_IPV4);
810 }
811
812 static void dhcp_success(struct connman_network *network)
813 {
814         struct connman_service *service;
815         struct connman_ipconfig *ipconfig_ipv4;
816         int err;
817
818         service = __connman_service_lookup_from_network(network);
819         if (service == NULL)
820                 goto err;
821
822         connman_network_set_associating(network, FALSE);
823
824         network->connecting = FALSE;
825
826         ipconfig_ipv4 = __connman_service_get_ip4config(service);
827         err = __connman_ipconfig_address_add(ipconfig_ipv4);
828         if (err < 0)
829                 goto err;
830
831         err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
832         if (err < 0)
833                 goto err;
834
835         __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
836
837         return;
838
839 err:
840         connman_network_set_error(network,
841                                 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
842 }
843
844 static void dhcp_failure(struct connman_network *network)
845 {
846         struct connman_service *service;
847
848         service = __connman_service_lookup_from_network(network);
849         if (service == NULL)
850                 return;
851
852         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
853                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
854 }
855
856 static void dhcp_callback(struct connman_network *network,
857                         connman_bool_t success)
858 {
859         DBG("success %d", success);
860
861         if (success == TRUE)
862                 dhcp_success(network);
863         else
864                 dhcp_failure(network);
865 }
866
867 static int set_connected_fixed(struct connman_network *network)
868 {
869         struct connman_service *service;
870         struct connman_ipconfig *ipconfig_ipv4;
871         int err;
872
873         DBG("");
874
875         service = __connman_service_lookup_from_network(network);
876
877         ipconfig_ipv4 = __connman_service_get_ip4config(service);
878
879         set_configuration(network);
880
881         network->connecting = FALSE;
882
883         connman_network_set_associating(network, FALSE);
884
885         err = __connman_ipconfig_address_add(ipconfig_ipv4);
886         if (err < 0)
887                 goto err;
888
889         err = __connman_ipconfig_gateway_add(ipconfig_ipv4);
890         if (err < 0)
891                 goto err;
892
893         __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
894
895         return 0;
896
897 err:
898         connman_network_set_error(network,
899                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
900
901         return err;
902 }
903
904 static void set_connected_manual(struct connman_network *network)
905 {
906         struct connman_service *service;
907         struct connman_ipconfig *ipconfig;
908         int err;
909
910         DBG("network %p", network);
911
912         service = __connman_service_lookup_from_network(network);
913
914         ipconfig = __connman_service_get_ip4config(service);
915
916         set_configuration(network);
917
918         err = __connman_ipconfig_address_add(ipconfig);
919         if (err < 0)
920                 goto err;
921
922         err = __connman_ipconfig_gateway_add(ipconfig);
923         if (err < 0)
924                 goto err;
925
926         network->connecting = FALSE;
927
928         connman_network_set_associating(network, FALSE);
929
930         __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
931
932         return;
933
934 err:
935         connman_network_set_error(network,
936                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
937         return;
938 }
939
940 static int set_connected_dhcp(struct connman_network *network)
941 {
942         int err;
943
944         DBG("network %p", network);
945
946         set_configuration(network);
947
948         err = __connman_dhcp_start(network, dhcp_callback);
949         if (err < 0) {
950                 connman_error("Can not request DHCP lease");
951                 return err;
952         }
953
954         return 0;
955 }
956
957 static int manual_ipv6_set(struct connman_network *network,
958                                 struct connman_ipconfig *ipconfig_ipv6)
959 {
960         struct connman_service *service;
961         int err;
962
963         DBG("network %p ipv6 %p", network, ipconfig_ipv6);
964
965         service = __connman_service_lookup_from_network(network);
966         if (service == NULL)
967                 return -EINVAL;
968
969         err = __connman_ipconfig_address_add(ipconfig_ipv6);
970         if (err < 0) {
971                 connman_network_set_error(network,
972                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
973                 return err;
974         }
975
976         err = __connman_ipconfig_gateway_add(ipconfig_ipv6);
977         if (err < 0)
978                 return err;
979
980         __connman_connection_gateway_activate(service,
981                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
982
983         __connman_device_increase_connections(network->device);
984
985         __connman_device_set_network(network->device, network);
986
987         connman_device_set_disconnected(network->device, FALSE);
988
989         network->connecting = FALSE;
990
991         __connman_service_set_ipconfig_ready(service,
992                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
993
994         return 0;
995 }
996
997 static void autoconf_ipv6_set(struct connman_network *network)
998 {
999         struct connman_service *service;
1000
1001         DBG("network %p", network);
1002
1003         service = __connman_service_lookup_from_network(network);
1004
1005         __connman_device_increase_connections(network->device);
1006
1007         __connman_device_set_network(network->device, network);
1008
1009         connman_device_set_disconnected(network->device, FALSE);
1010
1011         /* XXX: Append IPv6 nameservers here */
1012
1013         network->connecting = FALSE;
1014
1015         __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV6);
1016 }
1017
1018 static gboolean set_connected(gpointer user_data)
1019 {
1020         struct connman_network *network = user_data;
1021         struct connman_service *service;
1022         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
1023         enum connman_ipconfig_method ipv4_method, ipv6_method;
1024
1025         service = __connman_service_lookup_from_network(network);
1026
1027         ipconfig_ipv4 = __connman_service_get_ip4config(service);
1028         ipconfig_ipv6 = __connman_service_get_ip6config(service);
1029
1030         DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
1031                 ipconfig_ipv6);
1032
1033         ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
1034         ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
1035
1036         DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
1037         DBG("network connected %d", network->connected);
1038
1039         if (network->connected == TRUE) {
1040                 int ret;
1041
1042                 switch (ipv6_method) {
1043                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1044                 case CONNMAN_IPCONFIG_METHOD_OFF:
1045                         break;
1046                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1047                         autoconf_ipv6_set(network);
1048                         break;
1049                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1050                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1051                         ret = manual_ipv6_set(network, ipconfig_ipv6);
1052                         if (ret != 0) {
1053                                 connman_network_set_error(network,
1054                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1055                                 return FALSE;
1056                         }
1057                         break;
1058                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1059                         break;
1060                 }
1061
1062                 switch (ipv4_method) {
1063                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1064                 case CONNMAN_IPCONFIG_METHOD_OFF:
1065                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1066                         return FALSE;
1067                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1068                         if (set_connected_fixed(network) < 0) {
1069                                 connman_network_set_error(network,
1070                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1071                                 return FALSE;
1072                         }
1073                         return TRUE;
1074                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1075                         set_connected_manual(network);
1076                         return TRUE;
1077                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1078                         if (set_connected_dhcp(network) < 0) {
1079                                 connman_network_set_error(network,
1080                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1081                                 return FALSE;
1082                         }
1083                 }
1084
1085         } else {
1086                 struct connman_service *service;
1087
1088                 __connman_device_set_network(network->device, NULL);
1089                 network->hidden = FALSE;
1090
1091                 service = __connman_service_lookup_from_network(network);
1092
1093                 __connman_service_indicate_state(service,
1094                                         CONNMAN_SERVICE_STATE_DISCONNECT,
1095                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1096
1097                 __connman_service_indicate_state(service,
1098                                         CONNMAN_SERVICE_STATE_DISCONNECT,
1099                                         CONNMAN_IPCONFIG_TYPE_IPV6);
1100
1101                 __connman_connection_gateway_remove(service,
1102                                                 CONNMAN_IPCONFIG_TYPE_ALL);
1103
1104                 __connman_ipconfig_address_unset(ipconfig_ipv4);
1105                 __connman_ipconfig_address_unset(ipconfig_ipv6);
1106
1107                 /*
1108                  * Special handling for IPv6 autoconfigured address.
1109                  * The simplest way to remove autoconfigured routes is to
1110                  * disable IPv6 temporarily so that kernel will do the cleanup
1111                  * automagically.
1112                  */
1113                 if (ipv6_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1114                         __connman_ipconfig_disable_ipv6(ipconfig_ipv6);
1115                         __connman_ipconfig_enable_ipv6(ipconfig_ipv6);
1116                 }
1117
1118                 __connman_service_indicate_state(service,
1119                                         CONNMAN_SERVICE_STATE_IDLE,
1120                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1121
1122                 __connman_service_indicate_state(service,
1123                                         CONNMAN_SERVICE_STATE_IDLE,
1124                                         CONNMAN_IPCONFIG_TYPE_IPV6);
1125         }
1126
1127         network->connecting = FALSE;
1128
1129         connman_network_set_associating(network, FALSE);
1130
1131         return FALSE;
1132 }
1133
1134 /**
1135  * connman_network_set_connected:
1136  * @network: network structure
1137  * @connected: connected state
1138  *
1139  * Change connected state of network
1140  */
1141 int connman_network_set_connected(struct connman_network *network,
1142                                                 connman_bool_t connected)
1143 {
1144         DBG("network %p connected %d", network, connected);
1145
1146         if ((network->connecting == TRUE || network->associating == TRUE) &&
1147                                                         connected == FALSE) {
1148                 connman_network_set_error(network,
1149                                         CONNMAN_NETWORK_ERROR_CONNECT_FAIL);
1150                 __connman_network_disconnect(network);
1151         }
1152
1153         if (network->connected == connected)
1154                 return -EALREADY;
1155
1156         if (connected == FALSE)
1157                 __connman_device_decrease_connections(network->device);
1158
1159         network->connected = connected;
1160
1161         set_connected(network);
1162
1163         return 0;
1164 }
1165
1166 /**
1167  * connman_network_get_connected:
1168  * @network: network structure
1169  *
1170  * Get network connection status
1171  */
1172 connman_bool_t connman_network_get_connected(struct connman_network *network)
1173 {
1174         return network->connected;
1175 }
1176
1177 /**
1178  * connman_network_get_associating:
1179  * @network: network structure
1180  *
1181  * Get network associating status
1182  */
1183 connman_bool_t connman_network_get_associating(struct connman_network *network)
1184 {
1185         return network->associating;
1186 }
1187
1188 /**
1189  * __connman_network_connect:
1190  * @network: network structure
1191  *
1192  * Connect network
1193  */
1194 int __connman_network_connect(struct connman_network *network)
1195 {
1196         int err;
1197
1198         DBG("network %p", network);
1199
1200         if (network->connected == TRUE)
1201                 return -EISCONN;
1202
1203         if (network->connecting == TRUE || network->associating == TRUE)
1204                 return -EALREADY;
1205
1206         if (network->driver == NULL)
1207                 return -EUNATCH;
1208
1209         if (network->driver->connect == NULL)
1210                 return -ENOSYS;
1211
1212         if (network->device == NULL)
1213                 return -ENODEV;
1214
1215         network->connecting = TRUE;
1216
1217         __connman_device_disconnect(network->device);
1218
1219         err = network->driver->connect(network);
1220         if (err < 0) {
1221                 if (err == -EINPROGRESS)
1222                         connman_network_set_associating(network, TRUE);
1223                 else {
1224                         network->connecting = FALSE;
1225                         network->hidden = FALSE;
1226                 }
1227
1228                 return err;
1229         }
1230
1231         network->connected = TRUE;
1232         set_connected(network);
1233
1234         return err;
1235 }
1236
1237 /**
1238  * __connman_network_disconnect:
1239  * @network: network structure
1240  *
1241  * Disconnect network
1242  */
1243 int __connman_network_disconnect(struct connman_network *network)
1244 {
1245         int err;
1246
1247         DBG("network %p", network);
1248
1249         if (network->connected == FALSE && network->connecting == FALSE &&
1250                                                 network->associating == FALSE)
1251                 return -ENOTCONN;
1252
1253         if (network->driver == NULL)
1254                 return -EUNATCH;
1255
1256         if (network->driver->disconnect == NULL)
1257                 return -ENOSYS;
1258
1259         network->connecting = FALSE;
1260
1261         err = network->driver->disconnect(network);
1262         if (err == 0) {
1263                 connman_network_set_connected(network, FALSE);
1264                 set_connected(network);
1265         }
1266
1267         return err;
1268 }
1269
1270 static int manual_ipv4_set(struct connman_network *network,
1271                                 struct connman_ipconfig *ipconfig)
1272 {
1273         struct connman_service *service;
1274         int err;
1275
1276         service = __connman_service_lookup_from_network(network);
1277         if (service == NULL)
1278                 return -EINVAL;
1279
1280         err = __connman_ipconfig_address_add(ipconfig);
1281         if (err < 0) {
1282                 connman_network_set_error(network,
1283                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1284                 return err;
1285         }
1286
1287         __connman_ipconfig_gateway_add(ipconfig);
1288
1289         __connman_service_set_ipconfig_ready(service, CONNMAN_IPCONFIG_TYPE_IPV4);
1290
1291         return 0;
1292 }
1293
1294 int __connman_network_clear_ipconfig(struct connman_network *network,
1295                                         struct connman_ipconfig *ipconfig)
1296 {
1297         struct connman_service *service;
1298         enum connman_ipconfig_method method;
1299         enum connman_ipconfig_type type;
1300
1301         service = __connman_service_lookup_from_network(network);
1302         if (service == NULL)
1303                 return -EINVAL;
1304
1305         method = __connman_ipconfig_get_method(ipconfig);
1306         type = __connman_ipconfig_get_config_type(ipconfig);
1307
1308         switch (method) {
1309         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1310         case CONNMAN_IPCONFIG_METHOD_OFF:
1311         case CONNMAN_IPCONFIG_METHOD_FIXED:
1312         case CONNMAN_IPCONFIG_METHOD_AUTO:
1313                 return -EINVAL;
1314         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1315                 __connman_ipconfig_address_remove(ipconfig);
1316                 break;
1317         case CONNMAN_IPCONFIG_METHOD_DHCP:
1318                 __connman_dhcp_stop(network);
1319                 break;
1320         }
1321
1322         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1323                 __connman_service_indicate_state(service,
1324                                         CONNMAN_SERVICE_STATE_CONFIGURATION,
1325                                         CONNMAN_IPCONFIG_TYPE_IPV6);
1326         else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1327                 __connman_service_indicate_state(service,
1328                                         CONNMAN_SERVICE_STATE_CONFIGURATION,
1329                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1330
1331         return 0;
1332 }
1333
1334 int __connman_network_set_ipconfig(struct connman_network *network,
1335                                         struct connman_ipconfig *ipconfig_ipv4,
1336                                         struct connman_ipconfig *ipconfig_ipv6)
1337 {
1338         enum connman_ipconfig_method method;
1339         int ret;
1340
1341         if (ipconfig_ipv6) {
1342                 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1343
1344                 switch (method) {
1345                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1346                 case CONNMAN_IPCONFIG_METHOD_OFF:
1347                         break;
1348                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1349                         autoconf_ipv6_set(network);
1350                         break;
1351                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1352                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1353                         ret = manual_ipv6_set(network, ipconfig_ipv6);
1354                         if (ret != 0) {
1355                                 connman_network_set_error(network,
1356                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1357                                 return ret;
1358                         }
1359                         break;
1360                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1361                         break;
1362                 }
1363         }
1364
1365         if (ipconfig_ipv4) {
1366                 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1367
1368                 switch (method) {
1369                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1370                 case CONNMAN_IPCONFIG_METHOD_OFF:
1371                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1372                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1373                         return -EINVAL;
1374                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1375                         return manual_ipv4_set(network, ipconfig_ipv4);
1376                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1377                         return __connman_dhcp_start(network, dhcp_callback);
1378                 }
1379         }
1380
1381         return 0;
1382 }
1383
1384 int connman_network_set_ipaddress(struct connman_network *network,
1385                                         struct connman_ipaddress *ipaddress)
1386 {
1387         struct connman_service *service;
1388         struct connman_ipconfig *ipconfig = NULL;
1389
1390         DBG("network %p", network);
1391
1392         service = __connman_service_lookup_from_network(network);
1393         if (service == NULL)
1394                 return -EINVAL;
1395
1396         ipconfig = __connman_service_get_ipconfig(service, ipaddress->family);
1397         if (ipconfig == NULL)
1398                 return -EINVAL;
1399
1400         __connman_ipconfig_set_local(ipconfig, ipaddress->local);
1401         __connman_ipconfig_set_peer(ipconfig, ipaddress->peer);
1402         __connman_ipconfig_set_broadcast(ipconfig, ipaddress->broadcast);
1403         __connman_ipconfig_set_prefixlen(ipconfig, ipaddress->prefixlen);
1404         __connman_ipconfig_set_gateway(ipconfig, ipaddress->gateway);
1405
1406         return 0;
1407 }
1408
1409 int connman_network_set_pac(struct connman_network *network,
1410                                 const char *pac)
1411 {
1412         struct connman_service *service;
1413
1414         DBG("network %p pac %s", network, pac);
1415
1416         service = __connman_service_lookup_from_network(network);
1417         if (service == NULL)
1418                 return -EINVAL;
1419
1420         __connman_service_set_pac(service, pac);
1421
1422         return 0;
1423 }
1424
1425 int connman_network_set_nameservers(struct connman_network *network,
1426                                 const char *nameservers)
1427 {
1428         struct connman_service *service;
1429         char **nameservers_array = NULL;
1430         int i;
1431
1432         DBG("network %p nameservers %s", network, nameservers);
1433
1434         service = __connman_service_lookup_from_network(network);
1435         if (service == NULL)
1436                 return -EINVAL;
1437
1438         __connman_service_nameserver_clear(service);
1439
1440         if (nameservers != NULL)
1441                 nameservers_array = g_strsplit(nameservers, " ", 0);
1442
1443         for (i = 0; nameservers_array[i] != NULL; i++) {
1444                 __connman_service_nameserver_append(service,
1445                                                 nameservers_array[i]);
1446         }
1447
1448         g_strfreev(nameservers_array);
1449
1450         return 0;
1451 }
1452
1453 int connman_network_set_domain(struct connman_network *network,
1454                                 const char *domain)
1455 {
1456         struct connman_service *service;
1457
1458         DBG("network %p domain %s", network, domain);
1459
1460         service = __connman_service_lookup_from_network(network);
1461         if (service == NULL)
1462                 return -EINVAL;
1463
1464         __connman_service_set_domainname(service, domain);
1465
1466         return 0;
1467 }
1468
1469 /**
1470  * connman_network_set_name:
1471  * @network: network structure
1472  * @name: name value
1473  *
1474  * Set display name value for network
1475  */
1476 int connman_network_set_name(struct connman_network *network,
1477                                                         const char *name)
1478 {
1479         DBG("network %p name %s", network, name);
1480
1481         g_free(network->name);
1482         network->name = g_strdup(name);
1483
1484         return 0;
1485 }
1486
1487 /**
1488  * connman_network_set_strength:
1489  * @network: network structure
1490  * @strength: strength value
1491  *
1492  * Set signal strength value for network
1493  */
1494
1495 int connman_network_set_strength(struct connman_network *network,
1496                                                 connman_uint8_t strength)
1497 {
1498         DBG("network %p strengh %d", network, strength);
1499
1500         network->strength = strength;
1501
1502         return 0;
1503 }
1504
1505 connman_uint8_t connman_network_get_strength(struct connman_network *network)
1506 {
1507         return network->strength;
1508 }
1509
1510 int connman_network_set_frequency(struct connman_network *network,
1511                                                 connman_uint16_t frequency)
1512 {
1513         DBG("network %p frequency %d", network, frequency);
1514
1515         network->frequency = frequency;
1516
1517         return 0;
1518 }
1519
1520 connman_uint16_t connman_network_get_frequency(struct connman_network *network)
1521 {
1522         return network->frequency;
1523 }
1524
1525 int connman_network_set_wifi_channel(struct connman_network *network,
1526                                                 connman_uint16_t channel)
1527 {
1528         DBG("network %p wifi channel %d", network, channel);
1529
1530         network->wifi.channel = channel;
1531
1532         return 0;
1533 }
1534
1535 connman_uint16_t connman_network_get_wifi_channel(struct connman_network *network)
1536 {
1537         return network->wifi.channel;
1538 }
1539
1540 /**
1541  * connman_network_set_roaming:
1542  * @network: network structure
1543  * @roaming: roaming state
1544  *
1545  * Set roaming state for network
1546  */
1547 int connman_network_set_roaming(struct connman_network *network,
1548                                                 connman_bool_t roaming)
1549 {
1550         DBG("network %p roaming %d", network, roaming);
1551
1552         network->roaming = roaming;
1553
1554         return 0;
1555 }
1556
1557 /**
1558  * connman_network_set_string:
1559  * @network: network structure
1560  * @key: unique identifier
1561  * @value: string value
1562  *
1563  * Set string value for specific key
1564  */
1565 int connman_network_set_string(struct connman_network *network,
1566                                         const char *key, const char *value)
1567 {
1568         DBG("network %p key %s value %s", network, key, value);
1569
1570         if (g_strcmp0(key, "Name") == 0)
1571                 return connman_network_set_name(network, value);
1572
1573         if (g_str_equal(key, "Path") == TRUE) {
1574                 g_free(network->path);
1575                 network->path = g_strdup(value);
1576         } else if (g_str_equal(key, "Node") == TRUE) {
1577                 g_free(network->node);
1578                 network->node = g_strdup(value);
1579         } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1580                 g_free(network->wifi.mode);
1581                 network->wifi.mode = g_strdup(value);
1582         } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1583                 g_free(network->wifi.security);
1584                 network->wifi.security = g_strdup(value);
1585         } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1586                 g_free(network->wifi.passphrase);
1587                 network->wifi.passphrase = g_strdup(value);
1588         } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1589                 g_free(network->wifi.eap);
1590                 network->wifi.eap = g_strdup(value);
1591         } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1592                 g_free(network->wifi.identity);
1593                 network->wifi.identity = g_strdup(value);
1594         } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1595                 g_free(network->wifi.ca_cert_path);
1596                 network->wifi.ca_cert_path = g_strdup(value);
1597         } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1598                 g_free(network->wifi.client_cert_path);
1599                 network->wifi.client_cert_path = g_strdup(value);
1600         } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1601                 g_free(network->wifi.private_key_path);
1602                 network->wifi.private_key_path = g_strdup(value);
1603         } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1604                 g_free(network->wifi.private_key_passphrase);
1605                 network->wifi.private_key_passphrase = g_strdup(value);
1606         } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1607                 g_free(network->wifi.phase2_auth);
1608                 network->wifi.phase2_auth = g_strdup(value);
1609         } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1610                 g_free(network->wifi.pin_wps);
1611                 network->wifi.pin_wps = g_strdup(value);
1612         } else {
1613                 return -EINVAL;
1614         }
1615
1616         return 0;
1617 }
1618
1619 /**
1620  * connman_network_get_string:
1621  * @network: network structure
1622  * @key: unique identifier
1623  *
1624  * Get string value for specific key
1625  */
1626 const char *connman_network_get_string(struct connman_network *network,
1627                                                         const char *key)
1628 {
1629         DBG("network %p key %s", network, key);
1630
1631         if (g_str_equal(key, "Path") == TRUE)
1632                 return network->path;
1633         else if (g_str_equal(key, "Name") == TRUE)
1634                 return network->name;
1635         else if (g_str_equal(key, "Node") == TRUE)
1636                 return network->node;
1637         else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1638                 return network->wifi.mode;
1639         else if (g_str_equal(key, "WiFi.Security") == TRUE)
1640                 return network->wifi.security;
1641         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1642                 return network->wifi.passphrase;
1643         else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1644                 return network->wifi.eap;
1645         else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1646                 return network->wifi.identity;
1647         else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1648                 return network->wifi.ca_cert_path;
1649         else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1650                 return network->wifi.client_cert_path;
1651         else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1652                 return network->wifi.private_key_path;
1653         else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1654                 return network->wifi.private_key_passphrase;
1655         else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1656                 return network->wifi.phase2_auth;
1657         else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1658                 return network->wifi.pin_wps;
1659
1660         return NULL;
1661 }
1662
1663 /**
1664  * connman_network_set_bool:
1665  * @network: network structure
1666  * @key: unique identifier
1667  * @value: boolean value
1668  *
1669  * Set boolean value for specific key
1670  */
1671 int connman_network_set_bool(struct connman_network *network,
1672                                         const char *key, connman_bool_t value)
1673 {
1674         DBG("network %p key %s value %d", network, key, value);
1675
1676         if (g_strcmp0(key, "Roaming") == 0)
1677                 return connman_network_set_roaming(network, value);
1678         else if (g_strcmp0(key, "WiFi.WPS") == 0)
1679                 network->wifi.wps = value;
1680         else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1681                 network->wifi.use_wps = value;
1682
1683         return -EINVAL;
1684 }
1685
1686 /**
1687  * connman_network_get_bool:
1688  * @network: network structure
1689  * @key: unique identifier
1690  *
1691  * Get boolean value for specific key
1692  */
1693 connman_bool_t connman_network_get_bool(struct connman_network *network,
1694                                                         const char *key)
1695 {
1696         DBG("network %p key %s", network, key);
1697
1698         if (g_str_equal(key, "Roaming") == TRUE)
1699                 return network->roaming;
1700         else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1701                 return network->wifi.wps;
1702         else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1703                 return network->wifi.use_wps;
1704
1705         return FALSE;
1706 }
1707
1708 /**
1709  * connman_network_set_blob:
1710  * @network: network structure
1711  * @key: unique identifier
1712  * @data: blob data
1713  * @size: blob size
1714  *
1715  * Set binary blob value for specific key
1716  */
1717 int connman_network_set_blob(struct connman_network *network,
1718                         const char *key, const void *data, unsigned int size)
1719 {
1720         DBG("network %p key %s size %d", network, key, size);
1721
1722         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1723                 g_free(network->wifi.ssid);
1724                 network->wifi.ssid = g_try_malloc(size);
1725                 if (network->wifi.ssid != NULL) {
1726                         memcpy(network->wifi.ssid, data, size);
1727                         network->wifi.ssid_len = size;
1728                 } else
1729                         network->wifi.ssid_len = 0;
1730         } else {
1731                 return -EINVAL;
1732         }
1733
1734         return 0;
1735 }
1736
1737 /**
1738  * connman_network_get_blob:
1739  * @network: network structure
1740  * @key: unique identifier
1741  * @size: pointer to blob size
1742  *
1743  * Get binary blob value for specific key
1744  */
1745 const void *connman_network_get_blob(struct connman_network *network,
1746                                         const char *key, unsigned int *size)
1747 {
1748         DBG("network %p key %s", network, key);
1749
1750         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1751                 if (size != NULL)
1752                         *size = network->wifi.ssid_len;
1753                 return network->wifi.ssid;
1754         }
1755
1756         return NULL;
1757 }
1758
1759 void __connman_network_set_device(struct connman_network *network,
1760                                         struct connman_device *device)
1761 {
1762         if (network->device == device)
1763                 return;
1764
1765         if (network->device != NULL)
1766                 network_remove(network);
1767
1768         network->device = device;
1769
1770         if (network->device != NULL)
1771                 network_probe(network);
1772 }
1773
1774 /**
1775  * connman_network_get_device:
1776  * @network: network structure
1777  *
1778  * Get parent device of network
1779  */
1780 struct connman_device *connman_network_get_device(struct connman_network *network)
1781 {
1782         return network->device;
1783 }
1784
1785 /**
1786  * connman_network_get_data:
1787  * @network: network structure
1788  *
1789  * Get private network data pointer
1790  */
1791 void *connman_network_get_data(struct connman_network *network)
1792 {
1793         return network->driver_data;
1794 }
1795
1796 /**
1797  * connman_network_set_data:
1798  * @network: network structure
1799  * @data: data pointer
1800  *
1801  * Set private network data pointer
1802  */
1803 void connman_network_set_data(struct connman_network *network, void *data)
1804 {
1805         network->driver_data = data;
1806 }
1807
1808 void connman_network_update(struct connman_network *network)
1809 {
1810         switch (network->type) {
1811         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1812         case CONNMAN_NETWORK_TYPE_VENDOR:
1813                 return;
1814         case CONNMAN_NETWORK_TYPE_ETHERNET:
1815         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1816         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1817         case CONNMAN_NETWORK_TYPE_CELLULAR:
1818         case CONNMAN_NETWORK_TYPE_WIFI:
1819         case CONNMAN_NETWORK_TYPE_WIMAX:
1820                 break;
1821         }
1822
1823         if (network->group != NULL)
1824                 __connman_service_update_from_network(network);
1825 }
1826
1827 int __connman_network_init(void)
1828 {
1829         DBG("");
1830
1831         return 0;
1832 }
1833
1834 void __connman_network_cleanup(void)
1835 {
1836         DBG("");
1837 }