summaryrefslogtreecommitdiffstats
path: root/tinyNET/src/dhcp/tnet_dhcp_option.c
blob: 037d65a7903881a0120cd11d2e7dcc9d9942aec5 (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
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* 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_option.c
 * @brief DHCP Options and BOOTP Vendor Extensions as per RFC 2132.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tnet_dhcp_option.h"

#include "tnet_dhcp_option_sip.h"

#include "../tnet_types.h"
#include "../tnet_endianness.h"

#include "tsk_memory.h"
#include "tsk_debug.h"

tnet_dhcp_option_t* tnet_dhcp_option_create(tnet_dhcp_option_code_t code)
{
    return tsk_object_new(tnet_dhcp_option_def_t, code);
}

tnet_dhcp_option_paramslist_t* tnet_dhcp_option_paramslist_create()
{
    return tsk_object_new(tnet_dhcp_option_paramslist_def_t);
}

tnet_dhcp_option_dns_t* tnet_dhcp_option_dns_create(const void* payload, tsk_size_t payload_size)
{
    return tsk_object_new(tnet_dhcp_option_dns_def_t, payload, payload_size);
}

/** Initializes DHCPv4 option.
 *
 * @param [in,out]	self	The option to initialize.
 * @param	code			The code of the option to initialize.
 *
 * @return	Zero if succeed and non-zero error code otherwise.
**/
int tnet_dhcp_option_init(tnet_dhcp_option_t *self, tnet_dhcp_option_code_t code)
{
    if(self) {
        if(!self->initialized) {
            self->code = code;
            //option->value = tsk_buffer_create_null();

            self->initialized = tsk_true;
            return 0;
        }
        return -2;
    }
    return -1;
}

int tnet_dhcp_option_deinit(tnet_dhcp_option_t *self)
{
    if(self) {
        if(self->initialized) {
            TSK_OBJECT_SAFE_FREE(self->value);

            self->initialized = tsk_false;
            return 0;
        }
        return -2;
    }
    return -1;
}

tnet_dhcp_option_t* tnet_dhcp_option_deserialize(const void* data, tsk_size_t size)
{
    tnet_dhcp_option_t *option = 0;
    uint8_t* dataPtr = ((uint8_t*)data);
    //uint8_t* dataEnd = (dataPtr+size);

    tnet_dhcp_option_code_t code;
    uint8_t len;

    /* Check validity */
    if(!dataPtr || size<2/*Code Len*/) {
        goto bail;
    }

    code = (tnet_dhcp_option_code_t)*dataPtr++;
    len = *dataPtr++;

    switch(code) {
    case dhcp_code_SIP_Servers_DHCP_Option: {
        option = (tnet_dhcp_option_t *)tnet_dhcp_option_sip_create(dataPtr, len);
        break;
    }

    case dhcp_code_Domain_Server: {
        option = (tnet_dhcp_option_t *)tnet_dhcp_option_dns_create(dataPtr, len);
        break;
    }

    default: {
        option = tnet_dhcp_option_create(code);
    }
    }

    /* In all case */
    if(option && !option->value && len) {
        option->value = tsk_buffer_create((((uint8_t*)data) + 2/*Code Len*/), len);
    }

bail:
    return option;
}

int tnet_dhcp_option_serialize(const tnet_dhcp_option_t* self, tsk_buffer_t *output)
{
    if(!self || !output) {
        return -1;
    }

    /* Code */
    tsk_buffer_append(output, &(self->code), 1);

    if(self->value) {
        /* Length */
        tsk_buffer_append(output, &(self->value->size), 1);

        /* Value */
        tsk_buffer_append(output, self->value->data, self->value->size);
    }
    else {
        /* Length */
        static uint8_t zero = 0x00;
        tsk_buffer_append(output, &zero, 1);
    }

    return 0;
}

int tnet_dhcp_option_serializeex(tnet_dhcp_option_code_t code, uint8_t length, const void* value, tsk_buffer_t *output)
{
    if(value && length && output) {
        tsk_buffer_append(output, &(code), 1);
        tsk_buffer_append(output, &(length), 1);
        tsk_buffer_append(output, value, length);

        return 0;
    }
    return -1;
}

//
//	[[DHCP OPTION]] object definition
//
static tsk_object_t* tnet_dhcp_option_ctor(tsk_object_t * self, va_list * app)
{
    tnet_dhcp_option_t *option = self;
    if(option) {
        tnet_dhcp_option_init(option, va_arg(*app, tnet_dhcp_option_code_t));
    }
    return self;
}

static tsk_object_t* tnet_dhcp_option_dtor(tsk_object_t * self)
{
    tnet_dhcp_option_t *option = self;
    if(option) {
        tnet_dhcp_option_deinit(option);
    }
    return self;
}

static const tsk_object_def_t tnet_dhcp_option_def_s = {
    sizeof(tnet_dhcp_option_t),
    tnet_dhcp_option_ctor,
    tnet_dhcp_option_dtor,
    tsk_null,
};
const tsk_object_def_t *tnet_dhcp_option_def_t = &tnet_dhcp_option_def_s;



/*=======================================================================================
*	RFC 2132 - 9.8. Parameter Request List
*=======================================================================================*/
int tnet_dhcp_option_paramslist_add_code(tnet_dhcp_option_paramslist_t* self, tnet_dhcp_option_code_t code)
{
    if(self) {
        if(!TNET_DHCP_OPTION(self)->value) {
            TNET_DHCP_OPTION(self)->value = tsk_buffer_create_null();
        }
        return tsk_buffer_append(TNET_DHCP_OPTION(self)->value, &code, 1);
    }
    return -1;
}

//
//	[[DHCP OPTION - RFC 2132 9.8. Parameter Request List]] object definition
//
static tsk_object_t* tnet_dhcp_option_paramslist_ctor(tsk_object_t * self, va_list * app)
{
    tnet_dhcp_option_paramslist_t *option = self;
    if(option) {
        /* init base */
        tnet_dhcp_option_init(TNET_DHCP_OPTION(option), dhcp_code_Parameter_List);
    }
    return self;
}

static tsk_object_t* tnet_dhcp_option_paramslist_dtor(tsk_object_t * self)
{
    tnet_dhcp_option_paramslist_t *option = self;
    if(option) {
        /* deinit base */
        tnet_dhcp_option_deinit(TNET_DHCP_OPTION(option));
    }
    return self;
}

static const tsk_object_def_t tnet_dhcp_option_paramslist_def_s = {
    sizeof(tnet_dhcp_option_paramslist_t),
    tnet_dhcp_option_paramslist_ctor,
    tnet_dhcp_option_paramslist_dtor,
    tsk_null,
};
const tsk_object_def_t *tnet_dhcp_option_paramslist_def_t = &tnet_dhcp_option_paramslist_def_s;


/*=======================================================================================
*	RFC 2132 - 3.8. Domain Name Server Option
*=======================================================================================*/
//
//	[[DHCP OPTION - RFC 2132 3.8. Domain Name Server Option]] object definition
//
static tsk_object_t* tnet_dhcp_option_dns_ctor(tsk_object_t * self, va_list * app)
{
    tnet_dhcp_option_dns_t *option = self;
    if(option) {
        const void* payload = va_arg(*app, const void*);
        tsk_size_t payload_size = va_arg(*app, tsk_size_t);

        const uint8_t* payloadPtr = (const uint8_t*)payload;
        const uint8_t* payloadEnd = (payloadPtr + payload_size);

        /* init base */
        tnet_dhcp_option_init(TNET_DHCP_OPTION(option), dhcp_code_Domain_Server);

        option->servers = tsk_list_create();

        if(payload_size<4 || payload_size%4) {
            TSK_DEBUG_ERROR("DHCP - The minimum length for this option is 4 octets, and the length MUST always be a multiple of 4.");
        }
        else {
            tsk_size_t i;
            char* ip4 = 0;
            uint32_t address;
            tsk_string_t* addrstring;

            for(i=0; i<payload_size && (payloadPtr< payloadEnd); i+=4) {
                /*
                Code   Len         Address 1               Address 2
                +-----+-----+-----+-----+-----+-----+-----+-----+--
                |  6  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
                +-----+-----+-----+-----+-----+-----+-----+-----+--
                */
                address = (uint32_t)tnet_htonl_2(payloadPtr);
                tsk_sprintf(&ip4, "%u.%u.%u.%u", (address>>24)&0xFF, (address>>16)&0xFF, (address>>8)&0xFF, (address>>0)&0xFF);

                addrstring = tsk_string_create(ip4);
                tsk_list_push_back_data(option->servers, (void*)&addrstring);

                TSK_FREE(ip4);
                payloadPtr+= 4;
            }
        }
    }
    return self;
}

static tsk_object_t* tnet_dhcp_option_dns_dtor(tsk_object_t * self)
{
    tnet_dhcp_option_dns_t *option = self;
    if(option) {
        /* deinit base */
        tnet_dhcp_option_deinit(TNET_DHCP_OPTION(option));

        TSK_OBJECT_SAFE_FREE(option->servers);
    }
    return self;
}

static const tsk_object_def_t tnet_dhcp_option_dns_def_s = {
    sizeof(tnet_dhcp_option_dns_t),
    tnet_dhcp_option_dns_ctor,
    tnet_dhcp_option_dns_dtor,
    tsk_null,
};
const tsk_object_def_t *tnet_dhcp_option_dns_def_t = &tnet_dhcp_option_dns_def_s;
OpenPOWER on IntegriCloud