doubango/trunk/tinyNET/src/dhcp6/tnet_dhcp6_option.c

337 lines
9.1 KiB
C
Raw Normal View History

/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.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_dhcp6_option.c
* @brief DHCPv6 Options as per RFC 3315 subclause 22.
*
* @author Mamadou Diop <diopmamadou(at)doubango.org>
*
* @date Created: Sat Nov 8 16:54:58 2009 mdiop
*/
#include "tnet_dhcp6_option.h"
#include "../tnet_types.h"
#include "../tnet_endianness.h"
2010-02-11 17:50:33 +00:00
2010-03-01 18:15:37 +00:00
#include "tsk_memory.h"
2010-03-07 20:52:23 +00:00
#include <string.h>
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_t* tnet_dhcp6_option_create(tnet_dhcp6_option_code_t code, const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tsk_object_new(tnet_dhcp6_option_def_t, code, payload, payload_size);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_identifier_t* tnet_dhcp6_option_indentifer_create(tnet_dhcp6_option_code_t code, const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tsk_object_new(tnet_dhcp6_option_identifier_def_t, code, payload, payload_size);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_identifier_t* tnet_dhcp6_option_clientid_create(const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tnet_dhcp6_option_indentifer_create(dhcp6_code_clientid, payload, payload_size);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_identifier_t* tnet_dhcp6_option_serverid_create(const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tnet_dhcp6_option_indentifer_create(dhcp6_code_serverid, payload, payload_size);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_orequest_t* tnet_dhcp6_option_orequest_create(const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tsk_object_new(tnet_dhcp6_option_orequest_def_t, payload, payload_size);
}
tnet_dhcp6_option_orequest_t* tnet_dhcp6_option_orequest_create_null()
{
return tnet_dhcp6_option_orequest_create(tsk_null, 0);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_vendorclass_t* tnet_dhcp6_option_vendorclass_create(const void* payload, tsk_size_t payload_size)
2010-04-19 00:27:32 +00:00
{
return tsk_object_new(tnet_dhcp6_option_vendorclass_def_t, payload, payload_size);
}
tnet_dhcp6_option_vendorclass_t* tnet_dhcp6_option_vendorclass_create_null()
{
return tnet_dhcp6_option_vendorclass_create(tsk_null, 0);
}
2010-05-06 17:47:34 +00:00
tnet_dhcp6_option_t* tnet_dhcp6_option_deserialize(const void* data, tsk_size_t size)
{
2010-02-11 17:50:33 +00:00
tnet_dhcp6_option_t *option = 0;
uint8_t* dataPtr = ((uint8_t*)data);
2010-02-12 00:16:23 +00:00
//uint8_t* dataEnd = (dataPtr+size);
2010-02-11 17:50:33 +00:00
tnet_dhcp6_option_code_t code;
uint16_t len;
/* Check validity */
if(!dataPtr || size<4/*Code Len*/){
goto bail;
}
code = (tnet_dhcp6_option_code_t) tnet_ntohs_2(dataPtr);
2010-02-11 17:50:33 +00:00
dataPtr += 2;
len = tnet_ntohs_2(dataPtr);
2010-02-11 17:50:33 +00:00
dataPtr += 2;
2010-04-19 00:27:32 +00:00
switch(code){
2010-02-11 17:50:33 +00:00
case dhcp6_code_clientid:
case dhcp6_code_serverid:
{
break;
}
default:
{
break;
}
}
bail:
return option;
}
int tnet_dhcp6_option_serialize(const tnet_dhcp6_option_t* self, tsk_buffer_t *output)
{
2010-02-11 17:50:33 +00:00
uint16_t _2bytes;
int ret = -1;
2010-02-12 00:16:23 +00:00
if(!self || !output){
goto bail;
}
2010-03-01 18:15:37 +00:00
/*== Code */
_2bytes = tnet_htons(self->code);
2010-02-12 00:16:23 +00:00
tsk_buffer_append(output, &(_2bytes), 2);
2010-04-19 00:27:32 +00:00
switch(self->code){
2010-02-11 17:50:33 +00:00
case dhcp6_code_clientid:
case dhcp6_code_serverid:
{
break;
}
case dhcp6_code_oro:
default:
{
2010-03-01 18:15:37 +00:00
if(self->data)
2010-02-11 17:50:33 +00:00
{
2010-03-01 18:15:37 +00:00
const tnet_dhcp6_option_orequest_t* opt = (const tnet_dhcp6_option_orequest_t*)self->data;
if(opt->codes){
/* option-len */
_2bytes = tnet_htons(opt->codes->size);
2010-03-01 18:15:37 +00:00
tsk_buffer_append(output, &(_2bytes), 2);
/* option-data */
ret = tsk_buffer_append(output, opt->codes->data, opt->codes->size);
}
2010-02-11 17:50:33 +00:00
}
break;
}
}
bail:
return ret;
}
int tnet_dhcp6_option_serializeex(tnet_dhcp6_option_code_t code, uint8_t length, const void* value, tsk_buffer_t *output)
{
return -1;
}
//
2010-02-11 17:50:33 +00:00
// [[DHCPv6 OPTION]] object definition
//
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_ctor(tsk_object_t * self, va_list * app)
{
tnet_dhcp6_option_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-03-01 18:15:37 +00:00
tnet_dhcp6_option_code_t code = va_arg(*app, tnet_dhcp6_option_code_t);
const void* payload = va_arg(*app, const void*);
2010-05-06 17:47:34 +00:00
tsk_size_t payload_size = va_arg(*app, tsk_size_t);
2010-03-01 18:15:37 +00:00
option->code = code;
if(payload && payload_size){
if((option->data = (tnet_dhcp6_option_data_t*)tsk_calloc(payload_size, sizeof(uint8_t)))){
memcpy(option->data, payload, payload_size);
option->len = payload_size;
}
}
}
return self;
}
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_dtor(tsk_object_t * self)
{
tnet_dhcp6_option_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-03-01 18:15:37 +00:00
TSK_OBJECT_SAFE_FREE(option->data);
}
return self;
}
static const tsk_object_def_t tnet_dhcp6_option_def_s =
{
sizeof(tnet_dhcp6_option_t),
2010-04-19 00:27:32 +00:00
tnet_dhcp6_option_ctor,
tnet_dhcp6_option_dtor,
tsk_null,
};
2010-04-19 00:27:32 +00:00
const tsk_object_def_t *tnet_dhcp6_option_def_t = &tnet_dhcp6_option_def_s;
2010-02-11 17:50:33 +00:00
/*======================================================================================
* RFC 3315 -
22.2. Client Identifier Option
22.3. Server Identifier Option
*=======================================================================================*/
//
// [[DHCPv6 Option Request Option]] object definition
//
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_identifier_ctor(tsk_object_t * self, va_list * app)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_identifier_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-04-01 22:01:00 +00:00
//tnet_dhcp6_option_code_t code = va_arg(*app, tnet_dhcp6_option_code_t);
2010-02-11 17:50:33 +00:00
const void* payload = va_arg(*app, const void*);
2010-05-06 17:47:34 +00:00
tsk_size_t payload_size = va_arg(*app, tsk_size_t);
2010-02-11 17:50:33 +00:00
2010-04-19 00:27:32 +00:00
if(payload && payload_size){
/* DESERIALIZATION */
2010-02-11 17:50:33 +00:00
}
}
return self;
}
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_identifier_dtor(tsk_object_t * self)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_identifier_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-02-11 17:50:33 +00:00
TSK_OBJECT_SAFE_FREE(option->duid);
}
return self;
}
static const tsk_object_def_t tnet_dhcp6_option_identifier_def_s =
{
sizeof(tnet_dhcp6_option_identifier_t),
2010-04-19 00:27:32 +00:00
tnet_dhcp6_option_identifier_ctor,
tnet_dhcp6_option_identifier_dtor,
tsk_null,
2010-02-11 17:50:33 +00:00
};
2010-04-19 00:27:32 +00:00
const tsk_object_def_t *tnet_dhcp6_option_identifier_def_t = &tnet_dhcp6_option_identifier_def_s;
2010-02-11 17:50:33 +00:00
/*======================================================================================
* RFC 3315 - 22.7. Option Request Option
*=======================================================================================*/
int tnet_dhcp6_option_orequest_add_code(tnet_dhcp6_option_orequest_t* self, uint16_t code)
{
uint16_t _2bytes;
int ret = -1;
2010-04-19 00:27:32 +00:00
if(self){
2010-03-01 18:15:37 +00:00
if(!self->codes){
2010-04-19 00:27:32 +00:00
if(!(self->codes = tsk_buffer_create_null())){
2010-02-11 17:50:33 +00:00
return -3;
}
}
_2bytes = tnet_ntohs(code);
2010-03-01 18:15:37 +00:00
if(!(ret = tsk_buffer_append(self->codes, &_2bytes, 2))){
2010-02-11 17:50:33 +00:00
TNET_DHCP6_OPTION(self)->len += 2;
}
}
return ret;
}
//
// [[DHCPv6 Option Request Option]] object definition
//
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_orequest_ctor(tsk_object_t * self, va_list * app)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_orequest_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-02-11 17:50:33 +00:00
const void* payload = va_arg(*app, const void*);
2010-05-06 17:47:34 +00:00
tsk_size_t payload_size = va_arg(*app, tsk_size_t);
2010-02-11 17:50:33 +00:00
if(payload && payload_size)
{ /* DESERIALIZATION */
}
}
return self;
}
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_orequest_dtor(tsk_object_t * self)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_orequest_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-03-01 18:15:37 +00:00
TSK_OBJECT_SAFE_FREE(option->codes);
2010-02-11 17:50:33 +00:00
}
return self;
}
static const tsk_object_def_t tnet_dhcp6_option_orequest_def_s =
{
sizeof(tnet_dhcp6_option_orequest_t),
2010-04-19 00:27:32 +00:00
tnet_dhcp6_option_orequest_ctor,
tnet_dhcp6_option_orequest_dtor,
tsk_null,
2010-02-11 17:50:33 +00:00
};
2010-04-19 00:27:32 +00:00
const tsk_object_def_t *tnet_dhcp6_option_orequest_def_t = &tnet_dhcp6_option_orequest_def_s;
2010-02-11 17:50:33 +00:00
/*======================================================================================
* RFC 3315 - 22.16. Vendor Class Option
*=======================================================================================*/
//
// [[DHCPv6 Option Request Option]] object definition
//
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_vendorclass_ctor(tsk_object_t * self, va_list * app)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_vendorclass_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-02-11 17:50:33 +00:00
const void* payload = va_arg(*app, const void*);
2010-05-06 17:47:34 +00:00
tsk_size_t payload_size = va_arg(*app, tsk_size_t);
2010-02-11 17:50:33 +00:00
2010-04-19 00:27:32 +00:00
if(payload && payload_size){
/* DESERIALIZATION */
2010-02-11 17:50:33 +00:00
}
}
return self;
}
2010-04-19 00:27:32 +00:00
static tsk_object_t* tnet_dhcp6_option_vendorclass_dtor(tsk_object_t * self)
2010-02-11 17:50:33 +00:00
{
tnet_dhcp6_option_vendorclass_t *option = self;
2010-04-19 00:27:32 +00:00
if(option){
2010-02-11 17:50:33 +00:00
TSK_OBJECT_SAFE_FREE(option->vendor_class_data);
}
return self;
}
static const tsk_object_def_t tnet_dhcp6_option_vendorclass_def_s =
{
sizeof(tnet_dhcp6_option_vendorclass_t),
2010-04-19 00:27:32 +00:00
tnet_dhcp6_option_vendorclass_ctor,
tnet_dhcp6_option_vendorclass_dtor,
tsk_null,
2010-02-11 17:50:33 +00:00
};
2010-04-19 00:27:32 +00:00
const tsk_object_def_t *tnet_dhcp6_option_vendorclass_def_t = &tnet_dhcp6_option_vendorclass_def_s;