network: Remove unused parameter from autoconf_ipv6_set()
[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         const char *gateway;
690         int err;
691
692         DBG("network %p", network);
693
694         service = __connman_service_lookup_from_network(network);
695
696         ipconfig = __connman_service_get_ip4config(service);
697
698         set_configuration(network);
699
700         err = __connman_ipconfig_set_address(ipconfig);
701         if (err < 0) {
702                 connman_network_set_error(network,
703                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
704                 return;
705         }
706
707         connman_element_get_value(&network->element,
708                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
709         if (nameserver != NULL)
710                 __connman_service_nameserver_append(service, nameserver);
711
712         connman_element_get_value(&network->element,
713                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
714         if (gateway != NULL)
715                 __connman_ipconfig_set_gateway(ipconfig, gateway);
716
717         network->connecting = FALSE;
718
719         connman_network_set_associating(network, FALSE);
720
721         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
722                                         CONNMAN_IPCONFIG_TYPE_IPV4);
723 }
724
725 static int set_connected_dhcp(struct connman_network *network)
726 {
727         struct connman_element *element;
728         int error;
729
730         DBG("network %p", network);
731
732         element = connman_element_create(NULL);
733         if (element == NULL)
734                 return -ENOMEM;
735
736         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
737         element->index = network->element.index;
738
739         error = connman_element_register(element, &network->element);
740         if (error < 0) {
741                 connman_element_unref(element);
742                 return error;
743         }
744
745         set_configuration(network);
746
747         return 0;
748 }
749
750 static int manual_ipv6_set(struct connman_network *network,
751                                 struct connman_ipconfig *ipconfig_ipv6)
752 {
753         struct connman_service *service;
754         int err;
755
756         service = __connman_service_lookup_from_network(network);
757         if (service == NULL)
758                 return -EINVAL;
759
760         err = __connman_ipconfig_set_address(ipconfig_ipv6);
761         if (err < 0) {
762                 connman_network_set_error(network,
763                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
764                 return err;
765         }
766
767         /*
768          * READY state will be indicated by IPV4 setting
769          * gateway will be set by IPV4 setting
770          */
771
772         return 0;
773 }
774
775 static void autoconf_ipv6_set(struct connman_network *network)
776 {
777         struct connman_service *service;
778         struct connman_ipconfig *ipconfig;
779         const char *nameserver = NULL;
780
781         DBG("network %p", network);
782
783         service = __connman_service_lookup_from_network(network);
784
785         ipconfig = __connman_service_get_ip6config(service);
786
787         __connman_device_increase_connections(network->device);
788
789         __connman_device_set_network(network->device, network);
790
791         connman_device_set_disconnected(network->device, FALSE);
792
793         connman_element_get_value(&network->element,
794                         CONNMAN_PROPERTY_ID_IPV6_NAMESERVER, &nameserver);
795         if (nameserver != NULL)
796                 __connman_service_nameserver_append(service, nameserver);
797
798         network->connecting = FALSE;
799
800         __connman_service_indicate_state(service,
801                                         CONNMAN_SERVICE_STATE_READY,
802                                         CONNMAN_IPCONFIG_TYPE_IPV6);
803 }
804
805 static gboolean set_connected(gpointer user_data)
806 {
807         struct connman_network *network = user_data;
808         struct connman_service *service;
809         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
810         enum connman_ipconfig_method ipv4_method, ipv6_method;
811
812         service = __connman_service_lookup_from_network(network);
813
814         ipconfig_ipv4 = __connman_service_get_ip4config(service);
815         ipconfig_ipv6 = __connman_service_get_ip6config(service);
816
817         DBG("service %p ipv4 %p ipv6 %p", service, ipconfig_ipv4,
818                 ipconfig_ipv6);
819
820         ipv4_method = __connman_ipconfig_get_method(ipconfig_ipv4);
821         ipv6_method = __connman_ipconfig_get_method(ipconfig_ipv6);
822
823         DBG("method ipv4 %d ipv6 %d", ipv4_method, ipv6_method);
824         DBG("network connected %d", network->connected);
825
826         if (network->connected == TRUE) {
827                 int ret;
828
829                 switch (ipv6_method) {
830                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
831                 case CONNMAN_IPCONFIG_METHOD_OFF:
832                         break;
833                 case CONNMAN_IPCONFIG_METHOD_AUTO:
834                         autoconf_ipv6_set(network);
835                         break;
836                 case CONNMAN_IPCONFIG_METHOD_FIXED:
837                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
838                         ret = manual_ipv6_set(network, ipconfig_ipv6);
839                         if (ret != 0) {
840                                 connman_network_set_error(network,
841                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
842                                 return FALSE;
843                         }
844                         break;
845                 case CONNMAN_IPCONFIG_METHOD_DHCP:
846                         break;
847                 }
848
849                 switch (ipv4_method) {
850                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
851                 case CONNMAN_IPCONFIG_METHOD_OFF:
852                 case CONNMAN_IPCONFIG_METHOD_AUTO:
853                         return FALSE;
854                 case CONNMAN_IPCONFIG_METHOD_FIXED:
855                         if (set_connected_fixed(network) < 0) {
856                                 connman_network_set_error(network,
857                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
858                                 return FALSE;
859                         }
860                         return TRUE;
861                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
862                         set_connected_manual(network);
863                         return TRUE;
864                 case CONNMAN_IPCONFIG_METHOD_DHCP:
865                         if (set_connected_dhcp(network) < 0) {
866                                 connman_network_set_error(network,
867                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
868                                 return FALSE;
869                         }
870                 }
871
872         } else {
873                 struct connman_service *service;
874
875                 connman_element_unregister_children(&network->element);
876
877                 __connman_device_set_network(network->device, NULL);
878                 network->hidden = FALSE;
879
880                 service = __connman_service_lookup_from_network(network);
881
882                 __connman_service_indicate_state(service,
883                                         CONNMAN_SERVICE_STATE_IDLE,
884                                         CONNMAN_IPCONFIG_TYPE_IPV4);
885
886                 /* TODO: eventually the IPv6 disconnect state should be handled
887                  * in connection.c
888                  */
889                 __connman_service_indicate_state(service,
890                                         CONNMAN_SERVICE_STATE_DISCONNECT,
891                                         CONNMAN_IPCONFIG_TYPE_IPV6);
892
893                 __connman_service_indicate_state(service,
894                                         CONNMAN_SERVICE_STATE_IDLE,
895                                         CONNMAN_IPCONFIG_TYPE_IPV6);
896         }
897
898         network->connecting = FALSE;
899
900         connman_network_set_associating(network, FALSE);
901
902         return FALSE;
903 }
904
905 /**
906  * connman_network_set_connected:
907  * @network: network structure
908  * @connected: connected state
909  *
910  * Change connected state of network
911  */
912 int connman_network_set_connected(struct connman_network *network,
913                                                 connman_bool_t connected)
914 {
915         DBG("network %p connected %d", network, connected);
916
917         if ((network->connecting == TRUE || network->associating == TRUE) &&
918                                                         connected == FALSE) {
919                 connman_element_set_error(&network->element,
920                                         CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
921                 __connman_network_disconnect(network);
922         }
923
924         if (network->connected == connected)
925                 return -EALREADY;
926
927         if (connected == FALSE)
928                 __connman_device_decrease_connections(network->device);
929
930         network->connected = connected;
931
932         set_connected(network);
933
934         return 0;
935 }
936
937 /**
938  * connman_network_get_connected:
939  * @network: network structure
940  *
941  * Get network connection status
942  */
943 connman_bool_t connman_network_get_connected(struct connman_network *network)
944 {
945         return network->connected;
946 }
947
948 /**
949  * connman_network_get_associating:
950  * @network: network structure
951  *
952  * Get network associating status
953  */
954 connman_bool_t connman_network_get_associating(struct connman_network *network)
955 {
956         return network->associating;
957 }
958
959 /**
960  * __connman_network_connect:
961  * @network: network structure
962  *
963  * Connect network
964  */
965 int __connman_network_connect(struct connman_network *network)
966 {
967         struct connman_service *service;
968         int err;
969
970         DBG("network %p", network);
971
972         if (network->connected == TRUE)
973                 return -EISCONN;
974
975         if (network->connecting == TRUE || network->associating == TRUE)
976                 return -EALREADY;
977
978         if (network->driver == NULL)
979                 return -EUNATCH;
980
981         if (network->driver->connect == NULL)
982                 return -ENOSYS;
983
984         if (network->device == NULL)
985                 return -ENODEV;
986
987         network->connecting = TRUE;
988
989         __connman_device_disconnect(network->device);
990
991         service = __connman_service_lookup_from_network(network);
992
993         err = network->driver->connect(network);
994         if (err < 0) {
995                 if (err == -EINPROGRESS)
996                         connman_network_set_associating(network, TRUE);
997                 else {
998                         network->connecting = FALSE;
999                         network->hidden = FALSE;
1000                 }
1001
1002                 return err;
1003         }
1004
1005         network->connected = TRUE;
1006         set_connected(network);
1007
1008         return err;
1009 }
1010
1011 /**
1012  * __connman_network_disconnect:
1013  * @network: network structure
1014  *
1015  * Disconnect network
1016  */
1017 int __connman_network_disconnect(struct connman_network *network)
1018 {
1019         int err;
1020
1021         DBG("network %p", network);
1022
1023         if (network->connected == FALSE && network->connecting == FALSE &&
1024                                                 network->associating == FALSE)
1025                 return -ENOTCONN;
1026
1027         if (network->driver == NULL)
1028                 return -EUNATCH;
1029
1030         if (network->driver->disconnect == NULL)
1031                 return -ENOSYS;
1032
1033         network->connecting = FALSE;
1034
1035         err = network->driver->disconnect(network);
1036         if (err == 0) {
1037                 connman_network_set_connected(network, FALSE);
1038                 set_connected(network);
1039         }
1040
1041         return err;
1042 }
1043
1044 static int dhcp_start(struct connman_network *network)
1045 {
1046         struct connman_element *element;
1047         int error;
1048
1049         element = connman_element_create(NULL);
1050         if (element == NULL)
1051                 return -ENOMEM;
1052
1053         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
1054         element->index = network->element.index;
1055
1056         error = connman_element_register(element, &network->element);
1057         if (error < 0) {
1058                 connman_element_unref(element);
1059                 return error;
1060         }
1061
1062         return 0;
1063 }
1064
1065 static int dhcp_stop(struct connman_network *network)
1066 {
1067         connman_element_unregister_children_type(&network->element,
1068                                                 CONNMAN_ELEMENT_TYPE_IPV4);
1069         connman_element_unregister_children_type(&network->element,
1070                                                 CONNMAN_ELEMENT_TYPE_DHCP);
1071
1072         return 0;
1073 }
1074
1075 static int manual_ipv4_set(struct connman_network *network,
1076                                 struct connman_ipconfig *ipconfig)
1077 {
1078         struct connman_service *service;
1079         const char *gateway;
1080         int err;
1081
1082         service = __connman_service_lookup_from_network(network);
1083         if (service == NULL)
1084                 return -EINVAL;
1085
1086         err = __connman_ipconfig_set_address(ipconfig);
1087         if (err < 0) {
1088                 connman_network_set_error(network,
1089                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1090                 return err;
1091         }
1092
1093         connman_element_get_value(&network->element,
1094                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
1095         if (gateway != NULL)
1096                 __connman_ipconfig_set_gateway(ipconfig, gateway);
1097
1098         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY,
1099                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1100
1101         return 0;
1102 }
1103
1104 int __connman_network_clear_ipconfig(struct connman_network *network,
1105                                         struct connman_ipconfig *ipconfig)
1106 {
1107         struct connman_service *service;
1108         enum connman_ipconfig_method method;
1109         enum connman_ipconfig_type type;
1110
1111         service = __connman_service_lookup_from_network(network);
1112         if (service == NULL)
1113                 return -EINVAL;
1114
1115         method = __connman_ipconfig_get_method(ipconfig);
1116         type = __connman_ipconfig_get_config_type(ipconfig);
1117
1118         switch (method) {
1119         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1120         case CONNMAN_IPCONFIG_METHOD_OFF:
1121         case CONNMAN_IPCONFIG_METHOD_FIXED:
1122         case CONNMAN_IPCONFIG_METHOD_AUTO:
1123                 return -EINVAL;
1124         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1125                 __connman_ipconfig_clear_address(ipconfig);
1126                 break;
1127         case CONNMAN_IPCONFIG_METHOD_DHCP:
1128                 dhcp_stop(network);
1129                 break;
1130         }
1131
1132         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1133                 __connman_service_indicate_state(service,
1134                                         CONNMAN_SERVICE_STATE_CONFIGURATION,
1135                                         CONNMAN_IPCONFIG_TYPE_IPV6);
1136         else if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1137                 __connman_service_indicate_state(service,
1138                                         CONNMAN_SERVICE_STATE_CONFIGURATION,
1139                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1140
1141         return 0;
1142 }
1143
1144 int __connman_network_set_ipconfig(struct connman_network *network,
1145                                         struct connman_ipconfig *ipconfig_ipv4,
1146                                         struct connman_ipconfig *ipconfig_ipv6)
1147 {
1148         enum connman_ipconfig_method method;
1149         int ret;
1150
1151         if (ipconfig_ipv6) {
1152                 method = __connman_ipconfig_get_method(ipconfig_ipv6);
1153
1154                 switch (method) {
1155                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1156                 case CONNMAN_IPCONFIG_METHOD_OFF:
1157                         break;
1158                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1159                         autoconf_ipv6_set(network);
1160                         break;
1161                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1162                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1163                         ret = manual_ipv6_set(network, ipconfig_ipv6);
1164                         if (ret != 0) {
1165                                 connman_network_set_error(network,
1166                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1167                                 return FALSE;
1168                         }
1169                         break;
1170                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1171                         break;
1172                 }
1173         }
1174
1175         if (ipconfig_ipv4) {
1176                 method = __connman_ipconfig_get_method(ipconfig_ipv4);
1177
1178                 switch (method) {
1179                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1180                 case CONNMAN_IPCONFIG_METHOD_OFF:
1181                 case CONNMAN_IPCONFIG_METHOD_FIXED:
1182                 case CONNMAN_IPCONFIG_METHOD_AUTO:
1183                         return -EINVAL;
1184                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
1185                         return manual_ipv4_set(network, ipconfig_ipv4);
1186                 case CONNMAN_IPCONFIG_METHOD_DHCP:
1187                         return dhcp_start(network);
1188                 }
1189         }
1190
1191         return 0;
1192 }
1193
1194 /**
1195  * connman_network_set_address:
1196  * @network: network structure
1197  * @address: binary address value
1198  * @size: binary address length
1199  *
1200  * Set unique address value for network
1201  */
1202 int connman_network_set_address(struct connman_network *network,
1203                                 const void *address, unsigned int size)
1204 {
1205         const unsigned char *addr_octet = address;
1206         char *str;
1207
1208         DBG("network %p size %d", network, size);
1209
1210         if (size != 6)
1211                 return -EINVAL;
1212
1213         str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1214                                 addr_octet[0], addr_octet[1], addr_octet[2],
1215                                 addr_octet[3], addr_octet[4], addr_octet[5]);
1216         if (str == NULL)
1217                 return -ENOMEM;
1218
1219         g_free(network->address);
1220         network->address = str;
1221
1222         return connman_element_set_string(&network->element,
1223                                                 "Address", network->address);
1224 }
1225
1226 /**
1227  * connman_network_set_name:
1228  * @network: network structure
1229  * @name: name value
1230  *
1231  * Set display name value for network
1232  */
1233 int connman_network_set_name(struct connman_network *network,
1234                                                         const char *name)
1235 {
1236         DBG("network %p name %s", network, name);
1237
1238         g_free(network->name);
1239         network->name = g_strdup(name);
1240
1241         return connman_element_set_string(&network->element, "Name", name);
1242 }
1243
1244 /**
1245  * connman_network_set_strength:
1246  * @network: network structure
1247  * @strength: strength value
1248  *
1249  * Set signal strength value for network
1250  */
1251 int connman_network_set_strength(struct connman_network *network,
1252                                                 connman_uint8_t strength)
1253 {
1254         DBG("network %p strengh %d", network, strength);
1255
1256         network->strength = strength;
1257
1258         return connman_element_set_uint8(&network->element,
1259                                                 "Strength", strength);
1260 }
1261
1262 /**
1263  * connman_network_set_roaming:
1264  * @network: network structure
1265  * @roaming: roaming state
1266  *
1267  * Set roaming state for network
1268  */
1269 int connman_network_set_roaming(struct connman_network *network,
1270                                                 connman_bool_t roaming)
1271 {
1272         DBG("network %p roaming %d", network, roaming);
1273
1274         network->roaming = roaming;
1275
1276         return connman_element_set_bool(&network->element,
1277                                                 "Roaming", roaming);
1278 }
1279
1280 /**
1281  * connman_network_set_string:
1282  * @network: network structure
1283  * @key: unique identifier
1284  * @value: string value
1285  *
1286  * Set string value for specific key
1287  */
1288 int connman_network_set_string(struct connman_network *network,
1289                                         const char *key, const char *value)
1290 {
1291         int err;
1292
1293         DBG("network %p key %s value %s", network, key, value);
1294
1295         if (g_strcmp0(key, "Name") == 0)
1296                 return connman_network_set_name(network, value);
1297
1298         if (g_str_equal(key, "Address") == TRUE) {
1299                 g_free(network->address);
1300                 network->address = g_strdup(value);
1301         } else if (g_str_equal(key, "Node") == TRUE) {
1302                 g_free(network->node);
1303                 network->node = g_strdup(value);
1304         } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1305                 g_free(network->wifi.mode);
1306                 network->wifi.mode = g_strdup(value);
1307         } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1308                 g_free(network->wifi.security);
1309                 network->wifi.security = g_strdup(value);
1310         } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1311                 g_free(network->wifi.passphrase);
1312                 network->wifi.passphrase = g_strdup(value);
1313         } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1314                 g_free(network->wifi.eap);
1315                 network->wifi.eap = g_strdup(value);
1316         } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1317                 g_free(network->wifi.identity);
1318                 network->wifi.identity = g_strdup(value);
1319         } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1320                 g_free(network->wifi.ca_cert_path);
1321                 network->wifi.ca_cert_path = g_strdup(value);
1322         } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1323                 g_free(network->wifi.client_cert_path);
1324                 network->wifi.client_cert_path = g_strdup(value);
1325         } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1326                 g_free(network->wifi.private_key_path);
1327                 network->wifi.private_key_path = g_strdup(value);
1328         } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1329                 g_free(network->wifi.private_key_passphrase);
1330                 network->wifi.private_key_passphrase = g_strdup(value);
1331         } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1332                 g_free(network->wifi.phase2_auth);
1333                 network->wifi.phase2_auth = g_strdup(value);
1334         } else if (g_str_equal(key, "WiFi.PinWPS") == TRUE) {
1335                 g_free(network->wifi.pin_wps);
1336                 network->wifi.pin_wps = g_strdup(value);
1337         }
1338
1339         err = connman_element_set_string(&network->element, key, value);
1340         if (err < 0)
1341                 return err;
1342
1343         if (network->driver == NULL)
1344                 return 0;
1345
1346         if (network->driver->setup)
1347                 return network->driver->setup(network, key);
1348
1349         return 0;
1350 }
1351
1352 /**
1353  * connman_network_get_string:
1354  * @network: network structure
1355  * @key: unique identifier
1356  *
1357  * Get string value for specific key
1358  */
1359 const char *connman_network_get_string(struct connman_network *network,
1360                                                         const char *key)
1361 {
1362         DBG("network %p key %s", network, key);
1363
1364         if (g_str_equal(key, "Address") == TRUE)
1365                 return network->address;
1366         else if (g_str_equal(key, "Name") == TRUE)
1367                 return network->name;
1368         else if (g_str_equal(key, "Node") == TRUE)
1369                 return network->node;
1370         else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1371                 return network->wifi.mode;
1372         else if (g_str_equal(key, "WiFi.Security") == TRUE)
1373                 return network->wifi.security;
1374         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1375                 return network->wifi.passphrase;
1376         else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1377                 return network->wifi.eap;
1378         else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1379                 return network->wifi.identity;
1380         else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1381                 return network->wifi.ca_cert_path;
1382         else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1383                 return network->wifi.client_cert_path;
1384         else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1385                 return network->wifi.private_key_path;
1386         else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1387                 return network->wifi.private_key_passphrase;
1388         else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1389                 return network->wifi.phase2_auth;
1390         else if (g_str_equal(key, "WiFi.PinWPS") == TRUE)
1391                 return network->wifi.pin_wps;
1392
1393         return connman_element_get_string(&network->element, key);
1394 }
1395
1396 /**
1397  * connman_network_set_bool:
1398  * @network: network structure
1399  * @key: unique identifier
1400  * @value: boolean value
1401  *
1402  * Set boolean value for specific key
1403  */
1404 int connman_network_set_bool(struct connman_network *network,
1405                                         const char *key, connman_bool_t value)
1406 {
1407         DBG("network %p key %s value %d", network, key, value);
1408
1409         if (g_strcmp0(key, "Roaming") == 0)
1410                 return connman_network_set_roaming(network, value);
1411         else if (g_strcmp0(key, "WiFi.WPS") == 0)
1412                 network->wifi.wps = value;
1413         else if (g_strcmp0(key, "WiFi.UseWPS") == 0)
1414                 network->wifi.use_wps = value;
1415
1416         return connman_element_set_bool(&network->element, key, value);
1417 }
1418
1419 /**
1420  * connman_network_get_bool:
1421  * @network: network structure
1422  * @key: unique identifier
1423  *
1424  * Get boolean value for specific key
1425  */
1426 connman_bool_t connman_network_get_bool(struct connman_network *network,
1427                                                         const char *key)
1428 {
1429         DBG("network %p key %s", network, key);
1430
1431         if (g_str_equal(key, "Roaming") == TRUE)
1432                 return network->roaming;
1433         else if (g_str_equal(key, "WiFi.WPS") == TRUE)
1434                 return network->wifi.wps;
1435         else if (g_str_equal(key, "WiFi.UseWPS") == TRUE)
1436                 return network->wifi.use_wps;
1437
1438         return connman_element_get_bool(&network->element, key);
1439 }
1440
1441 /**
1442  * connman_network_set_uint8:
1443  * @network: network structure
1444  * @key: unique identifier
1445  * @value: integer value
1446  *
1447  * Set integer value for specific key
1448  */
1449 int connman_network_set_uint8(struct connman_network *network,
1450                                         const char *key, connman_uint8_t value)
1451 {
1452         DBG("network %p key %s value %d", network, key, value);
1453
1454         if (g_strcmp0(key, "Strength") == 0)
1455                 return connman_network_set_strength(network, value);
1456
1457         return connman_element_set_uint8(&network->element, key, value);
1458 }
1459
1460 /**
1461  * connman_network_get_uint8:
1462  * @network: network structure
1463  * @key: unique identifier
1464  *
1465  * Get integer value for specific key
1466  */
1467 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1468                                                         const char *key)
1469 {
1470         DBG("network %p key %s", network, key);
1471
1472         if (g_str_equal(key, "Strength") == TRUE)
1473                 return network->strength;
1474
1475         return connman_element_get_uint8(&network->element, key);
1476 }
1477
1478 /**
1479  * connman_network_set_uint16:
1480  * @network: network structure
1481  * @key: unique identifier
1482  * @value: integer value
1483  *
1484  * Set integer value for specific key
1485  */
1486 int connman_network_set_uint16(struct connman_network *network,
1487                                 const char *key, connman_uint16_t value)
1488 {
1489         DBG("network %p key %s value %d", network, key, value);
1490
1491         if (g_str_equal(key, "Frequency") == TRUE)
1492                 network->frequency = value;
1493         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1494                 network->wifi.channel = value;
1495
1496         return -EINVAL;
1497 }
1498
1499 /**
1500  * connman_network_get_uint16:
1501  * @network: network structure
1502  * @key: unique identifier
1503  *
1504  * Get integer value for specific key
1505  */
1506 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1507                                                         const char *key)
1508 {
1509         DBG("network %p key %s", network, key);
1510
1511         if (g_str_equal(key, "Frequency") == TRUE)
1512                 return network->frequency;
1513         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1514                 return network->wifi.channel;
1515
1516         return 0;
1517 }
1518
1519 /**
1520  * connman_network_set_blob:
1521  * @network: network structure
1522  * @key: unique identifier
1523  * @data: blob data
1524  * @size: blob size
1525  *
1526  * Set binary blob value for specific key
1527  */
1528 int connman_network_set_blob(struct connman_network *network,
1529                         const char *key, const void *data, unsigned int size)
1530 {
1531         DBG("network %p key %s size %d", network, key, size);
1532
1533         if (g_strcmp0(key, "Address") == 0)
1534                 return connman_network_set_address(network, data, size);
1535
1536         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1537                 g_free(network->wifi.ssid);
1538                 network->wifi.ssid = g_try_malloc(size);
1539                 if (network->wifi.ssid != NULL) {
1540                         memcpy(network->wifi.ssid, data, size);
1541                         network->wifi.ssid_len = size;
1542                 } else
1543                         network->wifi.ssid_len = 0;
1544         }
1545
1546         return connman_element_set_blob(&network->element, key, data, size);
1547 }
1548
1549 /**
1550  * connman_network_get_blob:
1551  * @network: network structure
1552  * @key: unique identifier
1553  * @size: pointer to blob size
1554  *
1555  * Get binary blob value for specific key
1556  */
1557 const void *connman_network_get_blob(struct connman_network *network,
1558                                         const char *key, unsigned int *size)
1559 {
1560         DBG("network %p key %s", network, key);
1561
1562         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1563                 if (size != NULL)
1564                         *size = network->wifi.ssid_len;
1565                 return network->wifi.ssid;
1566         }
1567
1568         return connman_element_get_blob(&network->element, key, size);
1569 }
1570
1571 void __connman_network_set_device(struct connman_network *network,
1572                                         struct connman_device *device)
1573 {
1574         network->device = device;
1575 }
1576
1577 /**
1578  * connman_network_get_device:
1579  * @network: network structure
1580  *
1581  * Get parent device of network
1582  */
1583 struct connman_device *connman_network_get_device(struct connman_network *network)
1584 {
1585         return network->device;
1586 }
1587
1588 /**
1589  * connman_network_get_data:
1590  * @network: network structure
1591  *
1592  * Get private network data pointer
1593  */
1594 void *connman_network_get_data(struct connman_network *network)
1595 {
1596         return network->driver_data;
1597 }
1598
1599 /**
1600  * connman_network_set_data:
1601  * @network: network structure
1602  * @data: data pointer
1603  *
1604  * Set private network data pointer
1605  */
1606 void connman_network_set_data(struct connman_network *network, void *data)
1607 {
1608         network->driver_data = data;
1609 }
1610
1611 void connman_network_update(struct connman_network *network)
1612 {
1613         switch (network->type) {
1614         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1615         case CONNMAN_NETWORK_TYPE_VENDOR:
1616                 return;
1617         case CONNMAN_NETWORK_TYPE_ETHERNET:
1618         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1619         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1620         case CONNMAN_NETWORK_TYPE_CELLULAR:
1621         case CONNMAN_NETWORK_TYPE_WIFI:
1622         case CONNMAN_NETWORK_TYPE_WIMAX:
1623                 break;
1624         }
1625
1626         if (network->group != NULL)
1627                 __connman_service_update_from_network(network);
1628
1629         return;
1630 }
1631
1632 static gboolean match_driver(struct connman_network *network,
1633                                         struct connman_network_driver *driver)
1634 {
1635         if (network->type == driver->type ||
1636                         driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1637                 return TRUE;
1638
1639         return FALSE;
1640 }
1641
1642 static int network_probe(struct connman_element *element)
1643 {
1644         struct connman_network *network = element->network;
1645         GSList *list;
1646
1647         DBG("element %p name %s", element, element->name);
1648
1649         if (network == NULL)
1650                 return -ENODEV;
1651
1652         for (list = driver_list; list; list = list->next) {
1653                 struct connman_network_driver *driver = list->data;
1654
1655                 if (match_driver(network, driver) == FALSE)
1656                         continue;
1657
1658                 DBG("driver %p name %s", driver, driver->name);
1659
1660                 if (driver->probe(network) == 0) {
1661                         network->driver = driver;
1662                         break;
1663                 }
1664         }
1665
1666         if (network->driver == NULL)
1667                 return -ENODEV;
1668
1669         switch (network->type) {
1670         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1671         case CONNMAN_NETWORK_TYPE_VENDOR:
1672                 break;
1673         case CONNMAN_NETWORK_TYPE_ETHERNET:
1674         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1675         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1676         case CONNMAN_NETWORK_TYPE_CELLULAR:
1677         case CONNMAN_NETWORK_TYPE_WIFI:
1678         case CONNMAN_NETWORK_TYPE_WIMAX:
1679                 if (network->group != NULL)
1680                         __connman_profile_add_network(network);
1681                 break;
1682         }
1683
1684         return 0;
1685 }
1686
1687 static void network_remove(struct connman_element *element)
1688 {
1689         struct connman_network *network = element->network;
1690
1691         DBG("element %p name %s", element, element->name);
1692
1693         if (network == NULL)
1694                 return;
1695
1696         if (network->driver == NULL)
1697                 return;
1698
1699         switch (network->type) {
1700         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1701         case CONNMAN_NETWORK_TYPE_VENDOR:
1702                 break;
1703         case CONNMAN_NETWORK_TYPE_ETHERNET:
1704         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1705         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1706         case CONNMAN_NETWORK_TYPE_CELLULAR:
1707         case CONNMAN_NETWORK_TYPE_WIFI:
1708         case CONNMAN_NETWORK_TYPE_WIMAX:
1709                 if (network->group != NULL) {
1710                         __connman_profile_remove_network(network);
1711
1712                         g_free(network->group);
1713                         network->group = NULL;
1714                 }
1715                 break;
1716         }
1717
1718         if (network->driver->remove)
1719                 network->driver->remove(network);
1720 }
1721
1722 static void network_change(struct connman_element *element)
1723 {
1724         struct connman_network *network = element->network;
1725
1726         DBG("element %p name %s", element, element->name);
1727
1728         if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1729                 return;
1730
1731         if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1732                 return;
1733
1734         if (network->connected == FALSE)
1735                 return;
1736
1737         connman_element_unregister_children(element);
1738
1739         connman_device_set_disconnected(network->device, TRUE);
1740
1741         if (network->driver && network->driver->disconnect) {
1742                 network->driver->disconnect(network);
1743                 return;
1744         }
1745
1746         network->connected = FALSE;
1747 }
1748
1749 static struct connman_driver network_driver = {
1750         .name           = "network",
1751         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
1752         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1753         .probe          = network_probe,
1754         .remove         = network_remove,
1755         .change         = network_change,
1756 };
1757
1758 int __connman_network_init(void)
1759 {
1760         DBG("");
1761
1762         return connman_driver_register(&network_driver);
1763 }
1764
1765 void __connman_network_cleanup(void)
1766 {
1767         DBG("");
1768
1769         connman_driver_unregister(&network_driver);
1770 }