0a2f6fc663f5402dc66459bdf6ebc4659137a5c4
[platform/upstream/connman.git] / src / dhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <connman/ipconfig.h>
31 #include <include/setting.h>
32
33 #include <gdhcp/gdhcp.h>
34
35 #include <glib.h>
36
37 #include "connman.h"
38
39 #define RATE_LIMIT_INTERVAL     60      /* delay between successive attempts */
40
41 struct connman_dhcp {
42         struct connman_ipconfig *ipconfig;
43         struct connman_network *network;
44         dhcp_cb callback;
45         gpointer user_data;
46
47         char **nameservers;
48         char **timeservers;
49         char *pac;
50
51         unsigned int timeout;
52
53         GDHCPClient *ipv4ll_client;
54         GDHCPClient *dhcp_client;
55         char *ipv4ll_debug_prefix;
56         char *dhcp_debug_prefix;
57 };
58
59 static GHashTable *ipconfig_table;
60 static bool ipv4ll_running;
61
62 static void dhcp_free(struct connman_dhcp *dhcp)
63 {
64         g_strfreev(dhcp->nameservers);
65         g_strfreev(dhcp->timeservers);
66         g_free(dhcp->pac);
67
68         dhcp->nameservers = NULL;
69         dhcp->timeservers = NULL;
70         dhcp->pac = NULL;
71
72         g_free(dhcp);
73 }
74
75 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
76 {
77         if (!dhcp->ipv4ll_client)
78                 return;
79
80         g_dhcp_client_stop(dhcp->ipv4ll_client);
81         g_dhcp_client_unref(dhcp->ipv4ll_client);
82         dhcp->ipv4ll_client = NULL;
83         ipv4ll_running = false;
84
85         g_free(dhcp->ipv4ll_debug_prefix);
86         dhcp->ipv4ll_debug_prefix = NULL;
87 }
88
89 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
90 {
91         struct connman_service *service;
92         int i;
93
94         if (!dhcp->network)
95                 return true;
96
97         service = connman_service_lookup_from_network(dhcp->network);
98         if (!service) {
99                 connman_error("Can not lookup service");
100                 return false;
101         }
102
103         __connman_service_set_domainname(service, NULL);
104         __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
105
106         if (dhcp->timeservers) {
107                 for (i = 0; dhcp->timeservers[i]; i++) {
108                         __connman_service_timeserver_remove(service,
109                                                         dhcp->timeservers[i]);
110                 }
111         }
112         if (dhcp->nameservers) {
113                 for (i = 0; dhcp->nameservers[i]; i++) {
114 #if defined TIZEN_EXT
115                         __connman_service_nameserver_remove(service,
116                                                 dhcp->nameservers[i], false,
117                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
118 #else
119                         __connman_service_nameserver_remove(service,
120                                                 dhcp->nameservers[i], false);
121 #endif
122                 }
123         }
124
125         return true;
126 }
127
128 /**
129  * dhcp_invalidate: Invalidate an existing DHCP lease
130  * @dhcp: pointer to the DHCP lease to invalidate.
131  * @callback: flag indicating whether or not to invoke the client callback
132  *            if present.
133  *
134  * Invalidates an existing DHCP lease, optionally invoking the client
135  * callback. The caller may wish to avoid the client callback invocation
136  * when the invocation of that callback might otherwise unnecessarily upset
137  * service state due to the IP configuration change implied by this
138  * invalidation.
139  */
140 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
141 {
142         DBG("dhcp %p callback %u", dhcp, callback);
143
144         if (!dhcp)
145                 return;
146
147         __connman_6to4_remove(dhcp->ipconfig);
148
149         if (!apply_dhcp_invalidate_on_network(dhcp))
150                 return;
151
152         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
153                                 __connman_ipconfig_get_local(dhcp->ipconfig));
154         DBG("last address %s",
155                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
156
157         __connman_ipconfig_address_remove(dhcp->ipconfig);
158
159         __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
160         __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
161         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
162         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
163
164         if (dhcp->callback && callback)
165                 dhcp->callback(dhcp->ipconfig, dhcp->network,
166                                                 false, dhcp->user_data);
167 }
168
169 static void dhcp_valid(struct connman_dhcp *dhcp)
170 {
171         if (dhcp->callback)
172                 dhcp->callback(dhcp->ipconfig, dhcp->network,
173                                                 true, dhcp->user_data);
174 }
175
176 static void dhcp_debug(const char *str, void *data)
177 {
178         connman_info("%s: %s", (const char *) data, str);
179 }
180
181 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
182 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
183
184 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
185 {
186         GDHCPClient *ipv4ll_client;
187         GDHCPClientError error;
188         const char *hostname;
189         int index;
190         int err;
191
192         if (dhcp->ipv4ll_client)
193                 return -EALREADY;
194
195         index = __connman_ipconfig_get_index(dhcp->ipconfig);
196
197         ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
198         if (error != G_DHCP_CLIENT_ERROR_NONE)
199                 return -EINVAL;
200
201 #if !defined TIZEN_EXT
202         if (getenv("CONNMAN_DHCP_DEBUG")) {
203 #endif
204                 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
205                                                         index);
206                 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
207                                         dhcp->ipv4ll_debug_prefix);
208 #if !defined TIZEN_EXT
209         }
210 #endif
211
212         g_dhcp_client_set_id(ipv4ll_client);
213
214         if (dhcp->network) {
215                 hostname = connman_utsname_get_hostname();
216                 if (hostname)
217                         g_dhcp_client_set_send(ipv4ll_client,
218                                                 G_DHCP_HOST_NAME, hostname);
219         }
220
221         g_dhcp_client_register_event(ipv4ll_client,
222                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
223
224         g_dhcp_client_register_event(ipv4ll_client,
225                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
226                                                 ipv4ll_available_cb, dhcp);
227
228         dhcp->ipv4ll_client = ipv4ll_client;
229
230         err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
231         if (err < 0) {
232                 ipv4ll_stop_client(dhcp);
233                 return err;
234         }
235
236         ipv4ll_running = true;
237         return 0;
238 }
239
240 static gboolean dhcp_retry_cb(gpointer user_data)
241 {
242         struct connman_dhcp *dhcp = user_data;
243
244         dhcp->timeout = 0;
245
246         g_dhcp_client_start(dhcp->dhcp_client,
247                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
248
249         return FALSE;
250 }
251
252 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
253 {
254         struct connman_dhcp *dhcp = user_data;
255         int err;
256
257         DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
258                 dhcp->ipv4ll_client);
259
260         if (dhcp->timeout > 0)
261                 g_source_remove(dhcp->timeout);
262
263         dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
264                         dhcp_retry_cb,
265                         dhcp);
266         if (ipv4ll_running)
267                 return;
268
269         err = ipv4ll_start_client(dhcp);
270         if (err < 0)
271                 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
272
273         /* Only notify upper layer if we have a problem */
274         dhcp_invalidate(dhcp, !ipv4ll_running);
275 }
276
277 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
278 {
279         struct connman_dhcp *dhcp = user_data;
280
281         DBG("Lease lost");
282
283         /* Upper layer will decide what to do, e.g. nothing or retry. */
284         dhcp_invalidate(dhcp, true);
285 }
286
287 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
288 {
289         struct connman_dhcp *dhcp = user_data;
290
291         DBG("Lease lost");
292
293         ipv4ll_stop_client(dhcp);
294
295         /*
296          * Since we lost our IPv4LL configuration we might as notify
297          * the upper layers.
298          */
299         dhcp_invalidate(dhcp, true);
300 }
301
302 static bool compare_string_arrays(char **array_a, char **array_b)
303 {
304         int i;
305
306         if (!array_a || !array_b)
307                 return false;
308
309         if (g_strv_length(array_a) != g_strv_length(array_b))
310                 return false;
311
312         for (i = 0; array_a[i] &&
313                              array_b[i]; i++) {
314                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
315                         return false;
316         }
317
318         return true;
319 }
320
321 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
322                                                 struct connman_dhcp *dhcp)
323 {
324         char **nameservers, **timeservers, *pac = NULL;
325         struct connman_service *service;
326         GList *list, *option = NULL;
327         int ns_entries;
328         int i;
329
330         if (!dhcp->network)
331                 return true;
332
333         service = connman_service_lookup_from_network(dhcp->network);
334         if (!service) {
335                 connman_error("Can not lookup service");
336                 return false;
337         }
338
339         option = g_dhcp_client_get_option(dhcp_client, 252);
340         if (option)
341                 pac = g_strdup(option->data);
342
343         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
344         ns_entries = g_list_length(option);
345         nameservers = g_try_new0(char *, ns_entries + 1);
346         if (nameservers) {
347                 for (i = 0, list = option;list; list = list->next, i++)
348                         nameservers[i] = g_strdup(list->data);
349                 nameservers[ns_entries] = NULL;
350         }
351
352         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
353         if (option)
354                 __connman_service_set_domainname(service, option->data);
355
356         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
357         if (option)
358                 __connman_service_set_hostname(service, option->data);
359
360         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
361         ns_entries = g_list_length(option);
362         timeservers = g_try_new0(char *, ns_entries + 1);
363         if (timeservers) {
364                 for (i = 0, list = option; list; list = list->next, i++)
365                         timeservers[i] = g_strdup(list->data);
366                 timeservers[ns_entries] = NULL;
367         }
368
369         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
370                 if (dhcp->nameservers) {
371 #if defined TIZEN_EXT
372                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
373                                 __connman_service_nameserver_remove(service,
374                                                 dhcp->nameservers[i], false,
375                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
376                         }
377 #else
378                         for (i = 0; dhcp->nameservers[i]; i++) {
379                                 __connman_service_nameserver_remove(service,
380                                                 dhcp->nameservers[i], false);
381                         }
382 #endif
383                         g_strfreev(dhcp->nameservers);
384                 }
385
386                 dhcp->nameservers = nameservers;
387
388                 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
389 #if defined TIZEN_EXT
390                         __connman_service_nameserver_append(service,
391                                                 dhcp->nameservers[i], false,
392                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
393 #else
394                         __connman_service_nameserver_append(service,
395                                                 dhcp->nameservers[i], false);
396 #endif
397                 }
398         } else {
399                 g_strfreev(nameservers);
400         }
401
402         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
403                 if (dhcp->timeservers) {
404                         for (i = 0; dhcp->timeservers[i]; i++) {
405                                 __connman_service_timeserver_remove(service,
406                                                         dhcp->timeservers[i]);
407                         }
408                         g_strfreev(dhcp->timeservers);
409                 }
410
411                 dhcp->timeservers = timeservers;
412
413                 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
414                         __connman_service_timeserver_append(service,
415                                                         dhcp->timeservers[i]);
416                 }
417         } else {
418                 g_strfreev(timeservers);
419         }
420
421         if (g_strcmp0(pac, dhcp->pac) != 0) {
422                 g_free(dhcp->pac);
423                 dhcp->pac = pac;
424
425                 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
426                                                                 dhcp->pac);
427         }
428
429         if (connman_setting_get_bool("Enable6to4"))
430                 __connman_6to4_probe(service);
431
432         return true;
433 }
434
435 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
436 {
437         struct connman_dhcp *dhcp = user_data;
438         GList *option = NULL;
439         char *address, *netmask = NULL, *gateway = NULL;
440         const char *c_address, *c_gateway;
441         unsigned char prefixlen, c_prefixlen;
442         bool ip_change = false;
443
444         DBG("Lease available");
445
446         if (dhcp->ipv4ll_client) {
447                 ipv4ll_stop_client(dhcp);
448                 dhcp_invalidate(dhcp, false);
449         }
450
451         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
452         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
453         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
454
455         address = g_dhcp_client_get_address(dhcp_client);
456
457         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
458         DBG("last address %s", address);
459
460         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
461         if (option)
462                 netmask = g_strdup(option->data);
463
464         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
465         if (option)
466                 gateway = g_strdup(option->data);
467
468         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
469         if (prefixlen == 255)
470                 connman_warn("netmask: %s is invalid", netmask);
471
472         DBG("c_address %s", c_address);
473
474         if (g_strcmp0(address, c_address)) {
475                 ip_change = true;
476                 if (c_address) {
477                         /* Remove old ip address */
478                         __connman_ipconfig_address_remove(dhcp->ipconfig);
479                 }
480         }
481         if (g_strcmp0(gateway, c_gateway)) {
482                 ip_change = true;
483                 if (c_gateway) {
484                         /* Remove gateway ip address */
485                         __connman_ipconfig_gateway_remove(dhcp->ipconfig);
486                 }
487         } else if (prefixlen != c_prefixlen)
488                 ip_change = true;
489
490         __connman_ipconfig_set_method(dhcp->ipconfig,
491                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
492         if (ip_change) {
493                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
494                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
495                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
496         }
497
498         if (!apply_lease_available_on_network(dhcp_client, dhcp))
499                 goto done;
500
501         if (ip_change)
502                 dhcp_valid(dhcp);
503
504 done:
505         g_free(address);
506         g_free(netmask);
507         g_free(gateway);
508 }
509
510 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
511 {
512         struct connman_dhcp *dhcp = user_data;
513                 char *address, *netmask;
514                 unsigned char prefixlen;
515
516         DBG("IPV4LL available");
517
518         address = g_dhcp_client_get_address(ipv4ll_client);
519         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
520
521         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
522
523         __connman_ipconfig_set_method(dhcp->ipconfig,
524                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
525         __connman_ipconfig_set_local(dhcp->ipconfig, address);
526         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
527         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
528
529         dhcp_valid(dhcp);
530
531         g_free(address);
532         g_free(netmask);
533 }
534
535 static int dhcp_initialize(struct connman_dhcp *dhcp)
536 {
537         GDHCPClient *dhcp_client;
538         GDHCPClientError error;
539         int index;
540
541         DBG("dhcp %p", dhcp);
542
543         index = __connman_ipconfig_get_index(dhcp->ipconfig);
544
545         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
546         if (error != G_DHCP_CLIENT_ERROR_NONE)
547 #if defined TIZEN_EXT
548         {
549                 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
550 #endif
551                 return -EINVAL;
552 #if defined TIZEN_EXT
553         }
554 #endif
555
556 #if !defined TIZEN_EXT
557         if (getenv("CONNMAN_DHCP_DEBUG")) {
558 #endif
559                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
560                                                         index);
561                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
562                                         dhcp->dhcp_debug_prefix);
563 #if !defined TIZEN_EXT
564         }
565 #endif
566
567         g_dhcp_client_set_id(dhcp_client);
568
569         if (dhcp->network) {
570                 struct connman_service *service;
571                 const char *hostname;
572
573                 service = connman_service_lookup_from_network(dhcp->network);
574
575                 hostname = __connman_service_get_hostname(service);
576                 if (!hostname)
577                         hostname = connman_utsname_get_hostname();
578
579                 if (hostname)
580                         g_dhcp_client_set_send(dhcp_client,
581                                                 G_DHCP_HOST_NAME, hostname);
582
583                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
584                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
585                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
586                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
587                 g_dhcp_client_set_request(dhcp_client, 252);
588         }
589
590         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
591         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
592
593         g_dhcp_client_register_event(dhcp_client,
594                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
595                                                 lease_available_cb, dhcp);
596
597         g_dhcp_client_register_event(dhcp_client,
598                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
599
600         g_dhcp_client_register_event(dhcp_client,
601                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
602
603         dhcp->dhcp_client = dhcp_client;
604
605         return 0;
606 }
607
608 static int dhcp_release(struct connman_dhcp *dhcp)
609 {
610         DBG("dhcp %p", dhcp);
611
612         if (dhcp->timeout > 0) {
613                 g_source_remove(dhcp->timeout);
614                 dhcp->timeout = 0;
615         }
616
617         if (dhcp->dhcp_client) {
618                 g_dhcp_client_stop(dhcp->dhcp_client);
619                 g_dhcp_client_unref(dhcp->dhcp_client);
620         }
621
622         dhcp->dhcp_client = NULL;
623
624         g_free(dhcp->dhcp_debug_prefix);
625         dhcp->dhcp_debug_prefix = NULL;
626
627         ipv4ll_stop_client(dhcp);
628
629         return 0;
630 }
631
632 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
633 {
634         struct connman_dhcp *dhcp;
635
636         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
637         if (!dhcp)
638                 return NULL;
639
640         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
641 }
642
643 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
644                         struct connman_network *network, dhcp_cb callback,
645                         gpointer user_data)
646 {
647 #if !defined TIZEN_EXT
648         const char *last_addr = NULL;
649 #endif
650         struct connman_dhcp *dhcp;
651         int err;
652
653         DBG("");
654
655         if (network) {
656                 struct connman_service *service;
657
658                 service = connman_service_lookup_from_network(network);
659                 if (!service)
660                         return -EINVAL;
661         }
662
663 #if !defined TIZEN_EXT
664         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
665 #endif
666
667         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
668         if (!dhcp) {
669
670                 dhcp = g_try_new0(struct connman_dhcp, 1);
671                 if (!dhcp)
672                         return -ENOMEM;
673
674                 dhcp->ipconfig = ipconfig;
675                 __connman_ipconfig_ref(ipconfig);
676
677                 if (network) {
678                         dhcp->network = network;
679                         connman_network_ref(network);
680                 }
681
682                 err = dhcp_initialize(dhcp);
683
684                 if (err < 0) {
685                         if (network)
686                                 connman_network_unref(network);
687                         g_free(dhcp);
688                         return err;
689                 }
690
691                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
692         }
693
694         dhcp->callback = callback;
695         dhcp->user_data = user_data;
696
697 #if defined TIZEN_EXT
698         DBG("Start DHCP with DHCPDISCOVER request");
699
700         return g_dhcp_client_start(dhcp->dhcp_client, NULL);
701 #else
702         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
703 #endif
704
705 }
706
707 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
708 {
709         struct connman_dhcp *dhcp;
710
711         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
712
713         if (!ipconfig_table)
714                 return;
715
716         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
717         if (dhcp) {
718                 g_hash_table_remove(ipconfig_table, ipconfig);
719                 __connman_ipconfig_unref(ipconfig);
720                 if (dhcp->network)
721                         connman_network_unref(dhcp->network);
722                 dhcp_release(dhcp);
723                 dhcp_invalidate(dhcp, false);
724                 dhcp_free(dhcp);
725         }
726 }
727
728 int __connman_dhcp_init(void)
729 {
730         DBG("");
731
732         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
733                                                                 NULL, NULL);
734
735         return 0;
736 }
737
738 void __connman_dhcp_cleanup(void)
739 {
740         DBG("");
741
742         g_hash_table_destroy(ipconfig_table);
743         ipconfig_table = NULL;
744
745         dhcp_cleanup_random();
746 }