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