summaryrefslogtreecommitdiffstats
path: root/tinyNET/src/dhcp/tnet_dhcp_message.c
blob: 005327dd2efd559aef1df44e5ffe81fd1b954c9f (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* 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_message.c
 * @brief DHCP Message as per RFC 2131 subclause 2.
 */
#include "tnet_dhcp_message.h"
#include "tnet_dhcp.h"

#include "../tnet_utils.h"
#include "../tnet_endianness.h"

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

#include <string.h>

tnet_dhcp_message_t* tnet_dhcp_message_create(tnet_dhcp_message_op_t opcode)
{
	return tsk_object_new(tnet_dhcp_message_def_t, opcode);
}

tnet_dhcp_request_t* tnet_dhcp_request_create()
{
	return tnet_dhcp_message_create(dhcp_op_bootrequest);
}

tnet_dhcp_message_t* tnet_dhcp_reply_create()
{
	return tnet_dhcp_message_create(dhcp_op_bootreply);
}

tsk_buffer_t* tnet_dhcp_message_serialize(const tnet_dhcp_ctx_t *ctx, const tnet_dhcp_message_t *message)
{
	tsk_buffer_t* output = 0;
	uint8_t _1byte;
	uint16_t _2bytes;
	uint32_t _4bytes;

	/* Check message validity */
	if (!message){
		goto bail;
	}

	output = tsk_buffer_create_null();

	/*== OP HTYPE HLEN HOPS */
	_4bytes = (((uint32_t)(message->op)) << 24) |
		(((uint32_t)(message->htype)) << 16) |
		(((uint16_t)(message->hlen)) << 8) | message->hops;
	_4bytes = (uint32_t)tnet_ntohl(_4bytes);
	tsk_buffer_append(output, &(_4bytes), 4);

	/*== XID */
	_4bytes = (uint32_t)tnet_ntohl(message->xid);
	tsk_buffer_append(output, &(_4bytes), 4);
	/*== SECS */
	_2bytes = tnet_ntohs(message->secs);
	tsk_buffer_append(output, &(_2bytes), 2);
	/*== FLAGS */
	_2bytes = tnet_ntohs(message->flags);
	tsk_buffer_append(output, &(_2bytes), 2);
	/*== CIADDR */
	_4bytes = (uint32_t)tnet_ntohl(message->ciaddr);
	tsk_buffer_append(output, &(_4bytes), 4);
	/*== YIADDR */
	_4bytes = (uint32_t)tnet_ntohl(message->yiaddr);
	tsk_buffer_append(output, &(_4bytes), 4);
	/*== SIADDR */
	_4bytes = (uint32_t)tnet_ntohl(message->siaddr);
	tsk_buffer_append(output, &(_4bytes), 4);
	/*== GIADDR */
	_4bytes = (uint32_t)tnet_ntohl(message->giaddr);
	tsk_buffer_append(output, &(_4bytes), 4);
	/*== CHADDR */
	tsk_buffer_append(output, message->chaddr, sizeof(message->chaddr));
	/*== sname (unused) */
	tsk_buffer_append(output, message->sname, sizeof(message->sname));
	/*== file (unused) */
	tsk_buffer_append(output, message->file, sizeof(message->file));
	/*== Magic Cookie */
	_4bytes = (uint32_t)tnet_ntohl(TNET_DHCP_MAGIC_COOKIE);
	tsk_buffer_append(output, &(_4bytes), 4);

	/*== Message Type (option 53)
	*/
	tnet_dhcp_option_serializeex(dhcp_code_DHCP_Msg_Type, 1, &message->type, output);

	/*== Client Identifier (option 61) ==> RFC 2132 - 9.14. Client-identifier
		Code   Len   Type  Client-Identifier
		+-----+-----+-----+-----+-----+---
		|  61 |  n  |  t1 |  i1 |  i2 | ...
		+-----+-----+-----+-----+-----+---
		*/
	if (message->hlen){
		uint8_t client_id[17]; // 16 /*sizeof(chaddr)*/+ 1/*htype*/
		/*if(client_id)*/{
			client_id[0] = message->htype;
			memcpy(&client_id[1], message->chaddr, message->hlen);
			tnet_dhcp_option_serializeex(dhcp_code_Client_Id, (message->hlen + 1), client_id, output);
		}
	}
	/*== Host name(10) ==> RFC 2132 - 3.14. Host Name Option
		Code   Len                 Host Name
		+-----+-----+-----+-----+-----+-----+-----+-----+--
		|  12 |  n  |  h1 |  h2 |  h3 |  h4 |  h5 |  h6 |  ...
		+-----+-----+-----+-----+-----+-----+-----+-----+--
		*/
	if (TNET_DHCP_MESSAGE_IS_REQUEST(message) && ctx->hostname){
		tnet_dhcp_option_serializeex(dhcp_code_Hostname, (uint8_t)tsk_strlen(ctx->hostname), ctx->hostname, output);
	}
	/*== Vendor classId(60) ==> RFC 2132 - 9.13. Vendor class identifier
		Code   Len   Vendor class Identifier
		+-----+-----+-----+-----+---
		|  60 |  n  |  i1 |  i2 | ...
		+-----+-----+-----+-----+---
		*/
	if (TNET_DHCP_MESSAGE_IS_REQUEST(message) && ctx->vendor_id){
		tnet_dhcp_option_serializeex(dhcp_code_Class_Id, (uint8_t)tsk_strlen(ctx->vendor_id), ctx->vendor_id, output);
	}

	/*== RFC 2132 - 9.10. Maximum DHCP Message Size (57)
		Code   Len     Length
		+-----+-----+-----+-----+
		|  57 |  2  |  l1 |  l2 |
		+-----+-----+-----+-----+
		*/
	if (TNET_DHCP_MESSAGE_IS_REQUEST(message) && ctx->max_msg_size){
		_2bytes = tnet_ntohs(ctx->max_msg_size);
		tnet_dhcp_option_serializeex(dhcp_code_DHCP_Max_Msg_Size, 2, &_2bytes, output);
	}

	/*== DHCP Options
	*/
	{
		tsk_list_item_t *item;
		tnet_dhcp_option_t* option;
		tsk_list_foreach(item, message->options)
		{
			option = (tnet_dhcp_option_t*)item->data;
			if (tnet_dhcp_option_serialize(option, output)){
				TSK_DEBUG_WARN("Failed to serialize DHCP OPTION (%u)", option->code);
			}
		}
	}

	/*	RFC 2131 - 4.1 Constructing and sending DHCP messages
		The last option must always be the 'end' option.
		*/
	_1byte = dhcp_code_End;
	tsk_buffer_append(output, &(_1byte), 1);

bail:
	return output;
}

tnet_dhcp_message_t* tnet_dhcp_message_deserialize(const struct tnet_dhcp_ctx_s *ctx, const uint8_t *data, tsk_size_t size)
{
	tnet_dhcp_message_t *message = 0;
	uint8_t *dataPtr, *dataEnd, *dataStart;

	if (!data || !size)
	{
		goto bail;
	}

	if (size < TNET_DHCP_MESSAGE_MIN_SIZE){
		TSK_DEBUG_ERROR("DHCP message too short.");
		goto bail;
	}

	if (!(message = tnet_dhcp_reply_create())){ /* If REQUEST OP will be overridedden */
		TSK_DEBUG_ERROR("Failed to create new DHCP message.");
		goto bail;
	}

	dataPtr = (uint8_t*)data;
	dataStart = dataPtr;
	dataEnd = (dataStart + size);

	/*== op (1)*/
	message->op = *(dataPtr++);
	/*== htype (1) */
	message->htype = *(dataPtr++);
	/*== hlen (1) */
	message->hlen = *(dataPtr++);
	/*== htype (1) */
	message->hops = *(dataPtr++);
	/*== xid (4) */
	message->xid = (uint32_t)tnet_htonl_2(dataPtr);
	dataPtr += 4;
	/*== secs (2) */
	message->secs = tnet_ntohs_2(dataPtr);
	dataPtr += 2;
	/*== flags (2) */
	message->flags = tnet_ntohs_2(dataPtr);
	dataPtr += 2;
	/*== ciaddr  (4) */
	message->ciaddr = (uint32_t)tnet_htonl_2(dataPtr);
	dataPtr += 4;
	/*== yiaddr  (4) */
	message->yiaddr = (uint32_t)tnet_htonl_2(dataPtr);
	dataPtr += 4;
	/*== siaddr  (4) */
	message->siaddr = (uint32_t)tnet_htonl_2(dataPtr);
	dataPtr += 4;
	/*== giaddr  (4) */
	message->giaddr = (uint32_t)tnet_htonl_2(dataPtr);
	dataPtr += 4;
	/*== chaddr  (16[max]) */
	memcpy(message->chaddr, dataPtr, message->hlen > 16 ? 16 : message->hlen);
	dataPtr += 16;
	/*== sname   (64) */
	memcpy(message->sname, dataPtr, 64);
	dataPtr += 64;
	/*== file    (128) */
	memcpy(message->file, dataPtr, 128);
	dataPtr += 128;
	/*== Magic Cookie (4) */
	if (tnet_htonl_2(dataPtr) != TNET_DHCP_MAGIC_COOKIE){
		TSK_DEBUG_ERROR("Invalid DHCP magic cookie.");
		// Do not exit ==> continue parsing.
	}
	dataPtr += 4;

	/*== options (variable) */
	while (dataPtr < dataEnd && *dataPtr != dhcp_code_End)
	{
		tnet_dhcp_option_t* option = tnet_dhcp_option_deserialize(dataPtr, (tsk_size_t)(dataEnd - dataPtr));
		if (option && option->value){

			if (option->code == dhcp_code_DHCP_Msg_Type){
				message->type = (tnet_dhcp_message_type_t)*TSK_BUFFER_TO_U8(option->value);
			}

			dataPtr += option->value->size + 2/*Code Len*/;
			tsk_list_push_back_data(message->options, (void**)&option);
		}
		else break;
	}

bail:
	return message;
}


const tnet_dhcp_option_t* tnet_dhcp_message_find_option(const tnet_dhcp_message_t *message, tnet_dhcp_option_code_t code)
{
	tsk_list_item_t *item;

	if (!message){
		goto bail;
	}

	tsk_list_foreach(item, message->options)
	{
		if (((tnet_dhcp_option_t*)item->data)->code == code){
			return ((tnet_dhcp_option_t*)item->data);
		}
	}

bail:
	return 0;
}

int tnet_dhcp_message_add_codes(tnet_dhcp_message_t *self, tnet_dhcp_option_code_t codes[], unsigned codes_count)
{
	int ret = -1;

	if (!self){
		goto bail;
	}
	if (codes_count){
		unsigned i;

		tnet_dhcp_option_paramslist_t* option = (tnet_dhcp_option_paramslist_t*)tnet_dhcp_message_find_option(self, dhcp_code_Parameter_List);
		if (!option){
			tnet_dhcp_option_paramslist_t *option_paramslist = tnet_dhcp_option_paramslist_create();
			option = option_paramslist;
			tsk_list_push_back_data(self->options, (void**)&option_paramslist);
		}

		for (i = 0; i < codes_count; i++){
			if ((ret = tnet_dhcp_option_paramslist_add_code(option, codes[i]))){
				break;
			}
		}
	}

bail:
	return ret;
}


//=================================================================================================
//	[[DHCP MESSAGE]] object definition
//
static tsk_object_t* tnet_dhcp_message_ctor(tsk_object_t * self, va_list * app)
{
	tnet_dhcp_message_t *message = self;
	if (message){
		static uint32_t __dhcpmessage_unique_xid = 0;//(uint32_t)tsk_time_epoch();

		message->op = va_arg(*app, tnet_dhcp_message_op_t);
		message->htype = tnet_htype_Ethernet_10Mb;
		message->hlen = 0x06;

		message->xid = ++(__dhcpmessage_unique_xid);
		message->options = tsk_list_create();
	}
	return self;
}

static tsk_object_t* tnet_dhcp_message_dtor(tsk_object_t * self)
{
	tnet_dhcp_message_t *message = self;
	if (message){
		TSK_OBJECT_SAFE_FREE(message->options);
	}
	return self;
}

static const tsk_object_def_t tnet_dhcp_message_def_s =
{
	sizeof(tnet_dhcp_message_t),
	tnet_dhcp_message_ctor,
	tnet_dhcp_message_dtor,
	tsk_null,
};
const tsk_object_def_t *tnet_dhcp_message_def_t = &tnet_dhcp_message_def_s;

OpenPOWER on IntegriCloud