summaryrefslogtreecommitdiff
path: root/plugins/dhcp.c
blob: 1e6b83d72a729850453557a17810b563ba2d4027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/dhcp.h>
#include <connman/utsname.h>
#include <connman/log.h>

#include <gdhcp/gdhcp.h>

static void dhcp_debug(const char *str, void *data)
{
	connman_info("%s: %s\n", (const char *) data, str);
}

static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;

	DBG("No lease available");

	connman_dhcp_fail(dhcp);
}

static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	DBG("Lease lost");
}

static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;
	GList *list, *option = NULL;
	char *address, *nameservers;
	size_t ns_strlen = 0;

	DBG("Lease available");

	address = g_dhcp_client_get_address(dhcp_client);
	if (address != NULL)
		connman_dhcp_set_value(dhcp, "Address", address);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
	if (option != NULL)
		connman_dhcp_set_value(dhcp, "Netmask", option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
	for (list = option; list; list = list->next)
		ns_strlen += strlen((char *) list->data) + 2;
	nameservers = g_try_malloc0(ns_strlen);
	if (nameservers) {
		char *ns_index = nameservers;

		for (list = option; list; list = list->next) {
			sprintf(ns_index, "%s ", (char *) list->data);
			ns_index += strlen((char *) list->data) + 1;
		}

		connman_dhcp_set_value(dhcp, "Nameserver", nameservers);
	}
	g_free(nameservers);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
	if (option != NULL)
		connman_dhcp_set_value(dhcp, "Domainname", option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
	if (option != NULL)
		connman_dhcp_set_value(dhcp, "Gateway", option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
	if (option != NULL)
		connman_dhcp_set_value(dhcp, "Hostname", option->data);

	option = g_dhcp_client_get_option(dhcp_client, 252);
	if (option != NULL)
		connman_dhcp_set_value(dhcp, "PAC", option->data);

	connman_dhcp_bound(dhcp);
}

static int dhcp_request(struct connman_dhcp *dhcp)
{
	GDHCPClient *dhcp_client;
	GDHCPClientError error;
	const char *hostname;
	int index;

	DBG("dhcp %p", dhcp);

	index = connman_dhcp_get_index(dhcp);

	dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
	if (error != G_DHCP_CLIENT_ERROR_NONE)
		return -EINVAL;

	if (getenv("CONNMAN_DHCP_DEBUG"))
		g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");

	hostname = connman_utsname_get_hostname();
	if (hostname != NULL)
		g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);

	g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
	g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
	g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
	g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
	g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
	g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
	g_dhcp_client_set_request(dhcp_client, 252);

	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
						lease_available_cb, dhcp);

	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);

	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);

	connman_dhcp_set_data(dhcp, dhcp_client);

	g_dhcp_client_ref(dhcp_client);

	return g_dhcp_client_start(dhcp_client);
}

static int dhcp_release(struct connman_dhcp *dhcp)
{
	GDHCPClient *dhcp_client = connman_dhcp_get_data(dhcp);

	DBG("dhcp %p", dhcp);

	g_dhcp_client_stop(dhcp_client);
	g_dhcp_client_unref(dhcp_client);

	return 0;
}

static struct connman_dhcp_driver dhcp_driver = {
	.name		= "dhcp",
	.priority	= CONNMAN_DHCP_PRIORITY_DEFAULT,
	.request	= dhcp_request,
	.release	= dhcp_release,
};

static int dhcp_init(void)
{
	return connman_dhcp_driver_register(&dhcp_driver);
}

static void dhcp_exit(void)
{
	connman_dhcp_driver_unregister(&dhcp_driver);
}

CONNMAN_PLUGIN_DEFINE(dhcp, "Generic DHCP plugin", VERSION,
			CONNMAN_PLUGIN_PRIORITY_LOW, dhcp_init, dhcp_exit)