summaryrefslogtreecommitdiffstats
path: root/tinyNET/src/dhcp/tnet_dhcp.c
blob: efd57c569706dbc40b0724f53d842b6e5fcccc7f (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (C) 2010-2015 Mamadou DIOP.
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*
*/
/**@file tnet_dhcp.c
 * @brief DHCP/BOOTP (RFC 2131 - Dynamic Host Configuration Protocol) utilities function for P-CSCF discovery(RFC 3319 and 3361).
 *		  Also implement: RFC 3315, 3118, 3319, 3825 (Geoconf), 4676 (Civic Addresses Configuration Information) ...
 */
#include "tnet_dhcp.h"

#include "../tnet_endianness.h"

#include "tsk_string.h"
#include "tsk_thread.h"
#include "tsk_memory.h"
#include "tsk_time.h"
#include "tsk_debug.h"

#include <string.h>

// Useful link: http://support.microsoft.com/?scid=kb%3Ben-us%3B169289&x=21&y=14
// Another one: http://www.iana.org/assignments/bootp-dhcp-parameters/
// Another one: http://www.slideshare.net/raini/DHCP-Presentation-v102
// RFC 3319 Dynamic Host Configuration Protocol (DHCPv6) Options for Session Initiation Protocol (SIP) Servers
// RFC 3361  Dynamic Host Configuration Protocol (DHCP-for-IPv4) Option for Session Initiation Protocol (SIP) Servers

/**@defgroup tnet_dhcp_group DHCPv4/BOOTP (RFC 2131) implementation.
*/

/**@ingroup tnet_dhcp_group
* Creates new DHCPv4 context.
*/
tnet_dhcp_ctx_t* tnet_dhcp_ctx_create()
{
	return tsk_object_new(tnet_dhcp_ctx_def_t);
}

/**@ingroup tnet_dhcp_group
* Creates new DHCPv4 parameters.
*/
tnet_dhcp_params_t* tnet_dhcp_params_create()
{
	return tsk_object_new(tnet_dhcp_params_def_t);
}

/* FIXME: USE retransmission mech (*2*2...)
*/
/**@ingroup tnet_dhcp_group
*/
tnet_dhcp_reply_t* tnet_dhcp_send_request(tnet_dhcp_ctx_t* ctx, tnet_dhcp_request_t* request)
{
	tsk_buffer_t *output;
	tnet_dhcp_reply_t* reply = tsk_null;
	int ret;
	struct timeval tv;
	fd_set set;
	uint64_t timeout = 0;
	tsk_list_item_t *item;
	const tnet_interface_t *iface;

	tnet_socket_t *localsocket4 = tsk_null;
	struct sockaddr_storage server;

	if (!ctx || !request){
		goto bail;
	}

	localsocket4 = tnet_socket_create(TNET_SOCKET_HOST_ANY, ctx->port_client, tnet_socket_type_udp_ipv4);
	if (!TNET_SOCKET_IS_VALID(localsocket4)){
		TSK_DEBUG_ERROR("Failed to create/bind DHCP client socket.");
		goto bail;
	}

	/* Always wait for 200ms before retransmission */
	tv.tv_sec = 0;
	tv.tv_usec = (200 * 1000);

	if (tnet_sockaddr_init("255.255.255.255", ctx->server_port, tnet_socket_type_udp_ipv4, &server)){
		TNET_PRINT_LAST_ERROR("Failed to initialize the DHCP server address");
		goto bail;
	}

	/* ENABLE BROADCASTING */
	{
#if defined(SOLARIS)
		char yes = '1';
#else
		int yes = 1;
#endif
		if (setsockopt(localsocket4->fd, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(int))){
			TNET_PRINT_LAST_ERROR("Failed to enable broadcast option");
			goto bail;
		}
	}

	/* Set timeout */
	timeout = tsk_time_now() + ctx->timeout;

	do
	{
		/* RFC 2131 - 3.6 Use of DHCP in clients with multiple interfaces
			A client with multiple network interfaces must use DHCP through each
			interface independently to obtain configuration information
			parameters for those separate interfaces.
			*/

		tsk_list_foreach(item, ctx->interfaces){
			iface = item->data;

			/* Set FD */
			FD_ZERO(&set);
			FD_SET(localsocket4->fd, &set);

			/* ciaddr */
			if (request->type == dhcp_type_inform){
				struct sockaddr_storage ss;
				if (!tnet_getsockname(localsocket4->fd, &ss)){
					uint32_t addr = (uint32_t)tnet_htonl_2(&((struct sockaddr_in*)&ss)->sin_addr);
					memcpy(&request->ciaddr, &addr, 4);
				}
			}

			/* chaddr */
			memset(request->chaddr, 0, sizeof(request->chaddr));
			request->hlen = (uint8_t)(iface->mac_address_length > sizeof(request->chaddr) ? sizeof(request->chaddr) : iface->mac_address_length);
			memcpy(request->chaddr, iface->mac_address, request->hlen);

			/* Serialize and send to the server. */
			if (!(output = tnet_dhcp_message_serialize(ctx, request))){
				TSK_DEBUG_ERROR("Failed to serialize the DHCP message.");
				goto next_iface;
			}
			/* Send the request to the DHCP server */
			if ((ret = tnet_sockfd_sendto(localsocket4->fd, (const struct sockaddr*)&server, output->data, output->size)) < 0){
				TNET_PRINT_LAST_ERROR("Failed to send DHCP request");

				tsk_thread_sleep(150); // wait 150ms before trying the next iface.
				goto next_iface;
			}
			/* wait for response */
			if ((ret = select(localsocket4->fd + 1, &set, NULL, NULL, &tv)) < 0){	/* Error */
				TNET_PRINT_LAST_ERROR("select have failed.");
				tsk_thread_sleep(150); // wait 150ms before trying the next iface.
				goto next_iface;
			}
			else if (ret == 0)
			{	/* timeout ==> do nothing */
			}
			else
			{	/* there is data to read */
				unsigned int len = 0;
				void* data = tsk_null;

				/* Check how how many bytes are pending */
				if ((ret = tnet_ioctlt(localsocket4->fd, FIONREAD, &len)) < 0){
					goto next_iface;
				}

				/* Receive pending data */
				data = tsk_calloc(len, sizeof(uint8_t));
				if ((ret = tnet_sockfd_recv(localsocket4->fd, data, len, 0)) < 0){
					TSK_FREE(data);

					TNET_PRINT_LAST_ERROR("Failed to receive DHCP dgrams.");
					goto next_iface;
				}

				/* Parse the incoming response. */
				reply = tnet_dhcp_message_deserialize(ctx, data, (tsk_size_t)ret);
				TSK_FREE(data);

				if (reply)
				{	/* response successfuly parsed */
					if (request->xid != reply->xid)
					{ /* Not same transaction id ==> continue*/
						TSK_OBJECT_SAFE_FREE(reply);
					}
				}
			}

		next_iface:
			TSK_OBJECT_SAFE_FREE(output);
			if (reply){
				goto bail;
			}
		}
		//break;//FIXME
	} while (timeout > tsk_time_epoch());

bail:
	TSK_OBJECT_SAFE_FREE(localsocket4);

	return reply;
}

/**@ingroup tnet_dhcp_group
*/
tnet_dhcp_reply_t* tnet_dhcp_query(tnet_dhcp_ctx_t* ctx, tnet_dhcp_message_type_t type, tnet_dhcp_params_t* params)
{
	tnet_dhcp_reply_t* reply = tsk_null;
	tnet_dhcp_request_t* request = tnet_dhcp_request_create();

	if (!ctx || !params || !request){
		goto bail;
	}

	request->type = type;
	tnet_dhcp_message_add_codes(request, params->codes, params->codes_count);

	reply = tnet_dhcp_send_request(ctx, request);

bail:
	TSK_OBJECT_SAFE_FREE(request);

	return reply;
}

/**@ingroup tnet_dhcp_group
*/
int tnet_dhcp_params_add_code(tnet_dhcp_params_t* params, tnet_dhcp_option_code_t code)
{
	if (params){
		if (params->codes_count < TNET_DHCP_MAX_CODES){
			unsigned i;
			for (i = 0; i < params->codes_count; i++){
				if (params->codes[i] == code){
					return -3;
				}
			}
			params->codes[params->codes_count++] = code;
		}
		else return -2;
	}
	return -1;
}



//=================================================================================================
//	[[DHCP CONTEXT]] object definition
//
static tsk_object_t* tnet_dhcp_ctx_ctor(tsk_object_t * self, va_list * app)
{
	tnet_dhcp_ctx_t *ctx = self;
	if (ctx){
		tnet_host_t host;

		ctx->vendor_id = tsk_strdup(TNET_DHCP_VENDOR_ID_DEFAULT);
		if (!tnet_gethostname(&host)){
			ctx->hostname = tsk_strndup(host, tsk_strlen(host));
		}
		ctx->timeout = TNET_DHCP_TIMEOUT_DEFAULT;
		ctx->max_msg_size = TNET_DHCP_MAX_MSG_SIZE;
		ctx->port_client = TNET_DHCP_CLIENT_PORT;
		ctx->server_port = TNET_DHCP_SERVER_PORT;
		ctx->interfaces = tnet_get_interfaces();

		if (!ctx->interfaces || TSK_LIST_IS_EMPTY(ctx->interfaces)){
			TSK_DEBUG_ERROR("Failed to retrieve network interfaces.");
		}

		tsk_safeobj_init(ctx);
	}
	return self;
}

static tsk_object_t* tnet_dhcp_ctx_dtor(tsk_object_t * self)
{
	tnet_dhcp_ctx_t *ctx = self;
	if (ctx){
		tsk_safeobj_deinit(ctx);

		TSK_FREE(ctx->vendor_id);
		TSK_FREE(ctx->hostname);

		TSK_OBJECT_SAFE_FREE(ctx->interfaces);
	}
	return self;
}

static const tsk_object_def_t tnet_dhcp_ctx_def_s =
{
	sizeof(tnet_dhcp_ctx_t),
	tnet_dhcp_ctx_ctor,
	tnet_dhcp_ctx_dtor,
	tsk_null,
};
const tsk_object_def_t *tnet_dhcp_ctx_def_t = &tnet_dhcp_ctx_def_s;

//=================================================================================================
//	[[DHCP PARAMS]] object definition
//
static tsk_object_t* tnet_dhcp_params_ctor(tsk_object_t * self, va_list * app)
{
	tnet_dhcp_params_t *params = self;
	if (params){
	}
	return self;
}

static tsk_object_t* tnet_dhcp_params_dtor(tsk_object_t * self)
{
	tnet_dhcp_params_t *params = self;
	if (params){
	}
	return self;
}

static const tsk_object_def_t tnet_dhcp_params_def_s =
{
	sizeof(tnet_dhcp_params_t),
	tnet_dhcp_params_ctor,
	tnet_dhcp_params_dtor,
	tsk_null,
};
const tsk_object_def_t *tnet_dhcp_params_def_t = &tnet_dhcp_params_def_s;
OpenPOWER on IntegriCloud