-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp_packet.c
335 lines (290 loc) · 9.45 KB
/
dhcp_packet.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include "dhcp_packet.h"
#include "dhcp_log.h"
char DHCP_MAGIC_COOKIE[4] = {0x63, 0x82, 0x53, 0x63};
//Caller need to free the memory used for the DHCP packet
struct dhcp_packet *marshall(char buffer[], int offset, int length)
{
INFO("==>marshall, offset=%d, length=%d", offset, length);
struct dhcp_packet *packet = NULL;
//first check if the arguments is valid
if(NULL == buffer)
{
ERROR("***BUFFER for marshall is NULL***");
goto ERROR;
}
if(length < BOOTP_ABSOLUTE_MIN_LEN)
{
ERROR("The length of dhcp packet is less than min size");
goto ERROR;
}
if(length > DHCP_MAX_MTU)
{
ERROR("The length of dhcp packet is more than max MTU");
goto ERROR;
}
packet = (struct dhcp_packet*)malloc(sizeof(struct dhcp_packet));
if(NULL == packet)
{
FATAL("***Allocate memory failed! %s(%d)***", strerror(errno), errno);
goto ERROR;
}
memset(packet, 0, sizeof(struct dhcp_packet));
void* packet_begin = buffer + offset;
//parse static part of packet
memcpy(&(packet->op), packet_begin, 1);
memcpy(&(packet->htype), packet_begin + 1, 1);
memcpy(&(packet->hlen), packet_begin + 2, 1);
memcpy(&(packet->hops), packet_begin + offset + 3, 1);
memcpy(packet->xid, packet_begin + 4, 4);
memcpy(packet->secs, packet_begin + 8, 2);
memcpy(packet->flags, packet_begin + 10, 2);
memcpy(packet->ciaddr, packet_begin + 12, 4);
memcpy(packet->yiaddr, packet_begin + 16, 4);
memcpy(packet->siaddr, packet_begin + 20, 4);
memcpy(packet->giaddr, packet_begin + 24, 4);
memcpy(packet->chaddr, packet_begin + 28, 16);
memcpy(packet->sname, packet_begin + 44, 64);
memcpy(packet->file, packet_begin + 108, 128);
DEBUG("--------------DUMP DHCP PACKET-------------");
DEBUG("packet->op=%d", packet->op);
DEBUG("packet->htype=%d", packet->htype);
DEBUG("packet->hlen=%d", packet->hlen);
DEBUG("packet->hops=%d", packet->hops);
DEBUG("packet->xid=%x,%x,%x,%x", packet->xid[0], packet->xid[1], packet->xid[2], packet->xid[3]);
DEBUG("packet->secs=%x,%x", packet->secs[0], packet->secs[1]);
DEBUG("packet->flags=%x,%x", packet->flags[0], packet->flags[1]);
DEBUG("packet->ciaddr=%x,%x,%x,%x", packet->ciaddr[0], packet->ciaddr[1], packet->ciaddr[2], packet->ciaddr[3]);
DEBUG("packet->yiaddr=%x,%x,%x,%x", packet->yiaddr[0], packet->yiaddr[1], packet->yiaddr[2], packet->yiaddr[3]);
DEBUG("packet->siaddr=%x,%x,%x,%x", packet->siaddr[0], packet->siaddr[1], packet->siaddr[2], packet->siaddr[3]);
DEBUG("packet->giaddr=%x,%x,%x,%x", packet->giaddr[0], packet->giaddr[1], packet->giaddr[2], packet->giaddr[3]);
DEBUG("packet->chaddr=%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", packet->chaddr[0], packet->chaddr[1], packet->chaddr[2],
packet->chaddr[3], packet->chaddr[4], packet->chaddr[5], packet->chaddr[6], packet->chaddr[7], packet->chaddr[8], packet->chaddr[9],
packet->chaddr[10], packet->chaddr[11], packet->chaddr[12], packet->chaddr[13], packet->chaddr[14], packet->chaddr[15]);
DEBUG("packet->sname=%s", packet->sname);
DEBUG("packet->file=%s", packet->file);
DEBUG("---------------------------------------------");
//check DHCP magic cookie
char magic[4];
memcpy(magic, packet_begin + 236, 4);
if(0 != memcmp(DHCP_MAGIC_COOKIE, magic, 4))
{
ERROR("DHCP packet magic cookie is not matched!");
goto ERROR;
}
//parse options
int options_offset = 240; //236 + 4
packet->options = NULL;
struct dhcp_option *prev = NULL;
while(1)
{
if(options_offset > length - 1)
{
break;
}
//code
char code;
memcpy(&code, packet_begin + options_offset, 1);
options_offset++;
DEBUG("dhcp option code=%d", code);
if(DHO_PAD == code)
{
continue;
}
if(DHO_END == code)
{
INFO("dhcp option end");
break;
}
//length
int len;
char len_buff;
memcpy(&len_buff, packet_begin + options_offset, 1);
len = (int)len_buff;
options_offset++;
DEBUG("dhcp option length=%d", len);
if(options_offset + len > length - 1)
{
WARN("The options length is more than packet length, but no end mark.");
break;
}
//value
struct dhcp_option * option = (struct dhcp_option*)malloc(sizeof(struct dhcp_option));
if(NULL == option)
{
FATAL("***Allocate memory failed! %s(%d)***", strerror(errno), errno);
goto ERROR;
}
memset(option, 0, sizeof(struct dhcp_option));
option->code = code;
option->length = len_buff;
option->value = (char*)malloc(len);
if(NULL == option->value)
{
FATAL("***Allocate memory failed! %s(%d)***", strerror(errno), errno);
goto ERROR;
}
memcpy(option->value, buffer + options_offset, len);
option->next = NULL;
options_offset += len;
//Add the option into the packet
if(NULL == packet->options)
{
packet->options = option;
}
if(NULL != prev)
{
prev->next = option;
}
prev = option;
}
if(options_offset < length - 1)
{
packet->padding = (char*)malloc(length - options_offset);
if(NULL == packet->padding)
{
FATAL("***Allocate memory failed! %s(%d)***", strerror(errno), errno);
}
else
{
memcpy(packet->padding, buffer + options_offset, length - options_offset - 1);
}
}
else
{
packet->padding = NULL;
}
INFO("marshall==>");
return packet;
ERROR:
if(NULL != packet)
{
free_packet(packet);
}
WARN("***error!*** marshall==>");
return NULL;
}
//Use this function to free dhcp packet
void free_packet(struct dhcp_packet *packet)
{
INFO("==>free_packet, packet=%d", packet);
if(NULL == packet)
{
INFO("packet pointer is NULL, free_packet==>");
return;
}
if(NULL != packet->padding)
{
free(packet->padding);
}
struct dhcp_option *option = packet->options;
while(NULL == option)
{
if(NULL != option->value)
{
free(option->value);
}
struct dhcp_option *current = option;
option = current->next;
free(current);
}
free(packet);
INFO("free_packet==>");
return;
}
int serialize(struct dhcp_packet *packet, char buffer[], int length)
{
INFO("serialize==>, packet=%d", packet);
if(NULL == packet)
{
INFO("packet pointer is NULL, ==>serialize");
return 0;
}
//calculate the total size of the packet
//static part
int packet_len = BOOTP_ABSOLUTE_MIN_LEN;
//magic cookie
packet_len += sizeof(DHCP_MAGIC_COOKIE);
//options
struct dhcp_option *option = packet->options;
while(NULL != option)
{
packet_len += 2;
packet_len += (int)option->length;
option = option->next;
}
//end option
packet_len++;
//calculate padding length
int padding_len = 0;
if(packet_len < BOOTP_ABSOLUTE_MIN_LEN + DHCP_VEND_SIZE)
{
padding_len = DHCP_VEND_SIZE + BOOTP_ABSOLUTE_MIN_LEN - packet_len;
packet_len = DHCP_VEND_SIZE + BOOTP_ABSOLUTE_MIN_LEN;
}
if(packet_len > length)
{
ERROR("Buffer size is less than packet length, buffer size=%d, packet length=%d", sizeof(buffer), packet_len);
INFO("==>serialize");
return 0;
}
DEBUG("--------------DUMP DHCP PACKET-------------");
DEBUG("packet->op=%d", packet->op);
DEBUG("packet->htype=%d", packet->htype);
DEBUG("packet->hlen=%d", packet->hlen);
DEBUG("packet->hops=%d", packet->hops);
DEBUG("packet->xid=%x,%x,%x,%x", packet->xid[0], packet->xid[1], packet->xid[2], packet->xid[3]);
DEBUG("packet->secs=%x,%x", packet->secs[0], packet->secs[1]);
DEBUG("packet->flags=%x,%x", packet->flags[0], packet->flags[1]);
DEBUG("packet->ciaddr=%x,%x,%x,%x", packet->ciaddr[0], packet->ciaddr[1], packet->ciaddr[2], packet->ciaddr[3]);
DEBUG("packet->yiaddr=%x,%x,%x,%x", packet->yiaddr[0], packet->yiaddr[1], packet->yiaddr[2], packet->yiaddr[3]);
DEBUG("packet->siaddr=%x,%x,%x,%x", packet->siaddr[0], packet->siaddr[1], packet->siaddr[2], packet->siaddr[3]);
DEBUG("packet->giaddr=%x,%x,%x,%x", packet->giaddr[0], packet->giaddr[1], packet->giaddr[2], packet->giaddr[3]);
DEBUG("packet->chaddr=%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x", packet->chaddr[0], packet->chaddr[1], packet->chaddr[2],
packet->chaddr[3], packet->chaddr[4], packet->chaddr[5], packet->chaddr[6], packet->chaddr[7], packet->chaddr[8], packet->chaddr[9],
packet->chaddr[10], packet->chaddr[11], packet->chaddr[12], packet->chaddr[13], packet->chaddr[14], packet->chaddr[15]);
DEBUG("packet->sname=%s", packet->sname);
DEBUG("packet->file=%s", packet->file);
DEBUG("---------------------------------------------");
memcpy(buffer, &(packet->op), 1);
memcpy(buffer + 1, &(packet->htype), 1);
memcpy(buffer + 2, &(packet->hlen), 1);
memcpy(buffer + 3, &(packet->hops), 1);
memcpy(buffer + 4, packet->xid, 4);
memcpy(buffer + 8, packet->secs, 2);
memcpy(buffer + 10, packet->flags, 2);
memcpy(buffer + 12, packet->ciaddr, 4);
memcpy(buffer + 16, packet->yiaddr, 4);
memcpy(buffer + 20, packet->siaddr, 4);
memcpy(buffer + 24, packet->giaddr, 4);
memcpy(buffer + 28, packet->chaddr, 16);
memcpy(buffer + 44, packet->sname, 64);
memcpy(buffer + 108, packet->file, 128);
memcpy(buffer + 236, DHCP_MAGIC_COOKIE, 4);
int options_offset = 240;
option = packet->options;
while(NULL != option)
{
DEBUG("dhcp option code=%d, length=%d", option->code, option->length);
memcpy(buffer + options_offset, &(option->code), 1);
options_offset++;
memcpy(buffer + options_offset, &(option->length), 1);
options_offset++;
int len = (int)option->length;
memcpy(buffer + options_offset, option->value, len);
options_offset += len;
option = option->next;
}
char dhcp_option_end = DHO_END;
memcpy(buffer + options_offset, &dhcp_option_end, 1);
options_offset++;
if(padding_len > 0)
{
memset(buffer + options_offset, 0, padding_len);
}
INFO("total %d bytes writen, ==>serialize", packet_len);
return packet_len;
}