ofono: do not strdup in get_ident()
[platform/upstream/connman.git] / plugins / dhcp.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 <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/dhcp.h>
34 #include <connman/utsname.h>
35 #include <connman/log.h>
36
37 #include <gdhcp/gdhcp.h>
38
39 static void dhcp_debug(const char *str, void *data)
40 {
41         connman_info("%s: %s\n", (const char *) data, str);
42 }
43
44 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
45 {
46         struct connman_dhcp *dhcp = user_data;
47
48         DBG("No lease available");
49
50         connman_dhcp_fail(dhcp);
51 }
52
53 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
54 {
55         DBG("Lease lost");
56 }
57
58 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
59 {
60         struct connman_dhcp *dhcp = user_data;
61         GList *list, *option = NULL;
62         char *address, *nameservers;
63         size_t ns_strlen = 0;
64
65         DBG("Lease available");
66
67         address = g_dhcp_client_get_address(dhcp_client);
68         if (address != NULL)
69                 connman_dhcp_set_value(dhcp, "Address", address);
70
71         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
72         if (option != NULL)
73                 connman_dhcp_set_value(dhcp, "Netmask", option->data);
74
75         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
76         for (list = option; list; list = list->next)
77                 ns_strlen += strlen((char *) list->data) + 2;
78         nameservers = g_try_malloc0(ns_strlen);
79         if (nameservers) {
80                 char *ns_index = nameservers;
81
82                 for (list = option; list; list = list->next) {
83                         sprintf(ns_index, "%s ", (char *) list->data);
84                         ns_index += strlen((char *) list->data) + 1;
85                 }
86
87                 connman_dhcp_set_value(dhcp, "Nameserver", nameservers);
88         }
89         g_free(nameservers);
90
91         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
92         if (option != NULL)
93                 connman_dhcp_set_value(dhcp, "Domainname", option->data);
94
95         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
96         if (option != NULL)
97                 connman_dhcp_set_value(dhcp, "Gateway", option->data);
98
99         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
100         if (option != NULL)
101                 connman_dhcp_set_value(dhcp, "Hostname", option->data);
102
103         option = g_dhcp_client_get_option(dhcp_client, 252);
104         if (option != NULL)
105                 connman_dhcp_set_value(dhcp, "PAC", option->data);
106
107         connman_dhcp_bound(dhcp);
108 }
109
110 static int dhcp_request(struct connman_dhcp *dhcp)
111 {
112         GDHCPClient *dhcp_client;
113         GDHCPClientError error;
114         const char *hostname;
115         int index;
116
117         DBG("dhcp %p", dhcp);
118
119         index = connman_dhcp_get_index(dhcp);
120
121         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
122         if (error != G_DHCP_CLIENT_ERROR_NONE)
123                 return -EINVAL;
124
125         if (getenv("CONNMAN_DHCP_DEBUG"))
126                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
127
128         hostname = connman_utsname_get_hostname();
129         if (hostname != NULL)
130                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
131
132         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
133         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
134         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
135         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
136         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
137         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
138         g_dhcp_client_set_request(dhcp_client, 252);
139
140         g_dhcp_client_register_event(dhcp_client,
141                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
142                                                 lease_available_cb, dhcp);
143
144         g_dhcp_client_register_event(dhcp_client,
145                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
146
147         g_dhcp_client_register_event(dhcp_client,
148                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
149
150         connman_dhcp_set_data(dhcp, dhcp_client);
151
152         g_dhcp_client_ref(dhcp_client);
153
154         return g_dhcp_client_start(dhcp_client);
155 }
156
157 static int dhcp_release(struct connman_dhcp *dhcp)
158 {
159         GDHCPClient *dhcp_client = connman_dhcp_get_data(dhcp);
160
161         DBG("dhcp %p", dhcp);
162
163         g_dhcp_client_stop(dhcp_client);
164         g_dhcp_client_unref(dhcp_client);
165
166         return 0;
167 }
168
169 static struct connman_dhcp_driver dhcp_driver = {
170         .name           = "dhcp",
171         .priority       = CONNMAN_DHCP_PRIORITY_DEFAULT,
172         .request        = dhcp_request,
173         .release        = dhcp_release,
174 };
175
176 static int dhcp_init(void)
177 {
178         return connman_dhcp_driver_register(&dhcp_driver);
179 }
180
181 static void dhcp_exit(void)
182 {
183         connman_dhcp_driver_unregister(&dhcp_driver);
184 }
185
186 CONNMAN_PLUGIN_DEFINE(dhcp, "Generic DHCP plugin", VERSION,
187                         CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhcp_init, dhcp_exit)