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