eapol/eapol_framework/eapol_common/include/eap_configuration_field.h
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #if !defined(_EAP_CONFIGURATION_FIELD_H_)
       
    22 #define _EAP_CONFIGURATION_FIELD_H_
       
    23 
       
    24 #include "eap_am_memory.h"
       
    25 #include "eap_am_types.h"
       
    26 #include "eap_am_tools.h"
       
    27 
       
    28 
       
    29 const char EAP_FILECONFIG_SECTION_SEPARATOR[] = ":";
       
    30 const u32_t EAP_FILECONFIG_SECTION_SEPARATOR_LENGTH = (sizeof(EAP_FILECONFIG_SECTION_SEPARATOR)-1ul);
       
    31 
       
    32 
       
    33 //--------------------------------------------------------------------------------------------------
       
    34 
       
    35 enum eap_configure_type_e
       
    36 {
       
    37 	eap_configure_type_none,
       
    38 	eap_configure_type_u32_t,
       
    39 	eap_configure_type_boolean,
       
    40 	eap_configure_type_string,
       
    41 	eap_configure_type_hex_data,
       
    42 	eap_configure_type_u32array,
       
    43 	eap_configure_type_section,
       
    44 	eap_configure_type_subsection,
       
    45 };
       
    46 
       
    47 //--------------------------------------------------------------------------------------------------
       
    48 
       
    49 class eap_configuration_field_c
       
    50 {
       
    51 public:
       
    52 
       
    53 	~eap_configuration_field_c()
       
    54 	{
       
    55 		delete m_subsection_name;
       
    56 		m_subsection_name = 0;
       
    57 	}
       
    58 
       
    59 	const bool get_is_secret() const
       
    60 	{
       
    61 		return m_is_secret;
       
    62 	}
       
    63 
       
    64 	const eap_configure_type_e get_type() const
       
    65 	{
       
    66 		return m_type;
       
    67 	}
       
    68 
       
    69 	const u32_t get_field_length() const
       
    70 	{
       
    71 		return m_field_length;
       
    72 	}
       
    73 
       
    74 	eap_config_string get_field() const
       
    75 	{
       
    76 		return m_field;
       
    77 	}
       
    78 
       
    79 	eap_status_e set_subsection(
       
    80 		abs_eap_am_tools_c * const tools,
       
    81 		const eap_configuration_field_c * const p_subsection_name,
       
    82 		const eap_variable_data_c * const p_subsection_value)
       
    83 	{
       
    84 		delete m_subsection_name;
       
    85 		m_subsection_name = 0;
       
    86 
       
    87 		m_subsection_name = new eap_variable_data_c(tools);
       
    88 		if (m_subsection_name == 0)
       
    89 		{
       
    90 			return EAP_STATUS_RETURN(tools, eap_status_allocation_error);
       
    91 		}
       
    92 
       
    93 		eap_status_e status = m_subsection_name->add_data(
       
    94 			p_subsection_name->get_field(),
       
    95 			p_subsection_name->get_field_length());
       
    96 		if (status != eap_status_ok)
       
    97 		{
       
    98 			return EAP_STATUS_RETURN(tools, status);
       
    99 		}
       
   100 
       
   101 		status = m_subsection_name->add_data(
       
   102 			EAP_FILECONFIG_SECTION_SEPARATOR,
       
   103 			EAP_FILECONFIG_SECTION_SEPARATOR_LENGTH);
       
   104 		if (status != eap_status_ok)
       
   105 		{
       
   106 			return EAP_STATUS_RETURN(tools, status);
       
   107 		}
       
   108 
       
   109 		status = m_subsection_name->add_data(
       
   110 			p_subsection_value->get_data(),
       
   111 			p_subsection_value->get_data_length());
       
   112 		if (status != eap_status_ok)
       
   113 		{
       
   114 			return EAP_STATUS_RETURN(tools, status);
       
   115 		}
       
   116 
       
   117 		return EAP_STATUS_RETURN(tools, eap_status_ok);
       
   118 	}
       
   119 
       
   120 	const eap_variable_data_c * get_subsection() const
       
   121 	{
       
   122 		return m_subsection_name;
       
   123 	}
       
   124 
       
   125 	bool compare(
       
   126 		abs_eap_am_tools_c * const tools,
       
   127 		const eap_configuration_field_c * const field) const
       
   128 	{
       
   129 		return field->get_field_length() == get_field_length()
       
   130 			&& tools->memcmp(
       
   131 				field->get_field(),
       
   132 				get_field(),
       
   133 				field->get_field_length()) == 0;
       
   134 	}
       
   135 
       
   136 
       
   137 	bool compare(
       
   138 		abs_eap_am_tools_c * const tools,
       
   139 		const eap_variable_data_c * const data) const
       
   140 	{
       
   141 		return data->get_data_length() == get_field_length()
       
   142 			&& tools->memcmp(
       
   143 				data->get_data(),
       
   144 				get_field(),
       
   145 				data->get_data_length()) == 0;
       
   146 	}
       
   147 
       
   148 	eap_configuration_field_c * copy(
       
   149 		abs_eap_am_tools_c * const tools,
       
   150 		const eap_configuration_field_c * const p_subsection_name,
       
   151 		const eap_variable_data_c * const p_subsection_value) const
       
   152 	{
       
   153 		// This allocates memory for object and following field.
       
   154 		u32_t data_length = sizeof(eap_configuration_field_c) + m_field_length;
       
   155 		u8_t * const new_buffer = new u8_t[data_length];
       
   156 		if (new_buffer == 0)
       
   157 		{
       
   158 			return 0;
       
   159 		}
       
   160 
       
   161 		eap_configuration_field_c * tmp = reinterpret_cast<eap_configuration_field_c *>(new_buffer);
       
   162 
       
   163 		tools->memmove(tmp, this, data_length);
       
   164 
       
   165 		eap_status_e status = tmp->set_subsection(
       
   166 			tools,
       
   167 			p_subsection_name,
       
   168 			p_subsection_value);
       
   169 		if (status != eap_status_ok
       
   170 			|| tmp->get_subsection() == 0)
       
   171 		{
       
   172 			delete tmp;
       
   173 			tmp = 0;
       
   174 		}
       
   175 
       
   176 		return tmp;
       
   177 	}
       
   178 
       
   179 private:
       
   180 	bool                 m_is_secret;
       
   181 	eap_configure_type_e m_type;
       
   182 	eap_variable_data_c *m_subsection_name;
       
   183 	u32_t                m_field_length;
       
   184 	i8_t                 m_field[1];
       
   185 };
       
   186 
       
   187 //--------------------------------------------------------------------------------------------------
       
   188 
       
   189 template <u32_t buffer_length>
       
   190 class eap_configuration_field_template_c
       
   191 {
       
   192 public:
       
   193 	inline const eap_configuration_field_c * get_field() const;
       
   194 
       
   195 	inline eap_status_e set_fields(
       
   196 		abs_eap_am_tools_c* const am_tools,
       
   197 		const void * const field,
       
   198 		const eap_configure_type_e type,
       
   199 		const bool is_secret)
       
   200 	{
       
   201 		m_is_secret = false;
       
   202 		m_type = eap_configure_type_none;
       
   203 
       
   204 		u32_t field_length = (static_cast<eap_am_tools_c *> (am_tools))->strlen(
       
   205 			static_cast<const eap_char *>(field));
       
   206 		if (field_length+1 > buffer_length)
       
   207 		{
       
   208 			m_field_length = 0ul;
       
   209 			m_field[0] = '\0';
       
   210 			return EAP_STATUS_RETURN(am_tools, eap_status_allocation_error);
       
   211 		}
       
   212 
       
   213 		m_is_secret = is_secret;
       
   214 		m_type = type;
       
   215 		am_tools->memmove(m_field, field, field_length+1);
       
   216 		m_field_length = field_length;
       
   217 
       
   218 		return EAP_STATUS_RETURN(am_tools, eap_status_ok);
       
   219 	}
       
   220 
       
   221 	inline bool get_is_valid() const
       
   222 	{
       
   223 		EAP_STATIC_ASSERT(sizeof(eap_configuration_field_template_c<buffer_length>) >= sizeof(eap_configuration_field_c));
       
   224 		return true;
       
   225 	}
       
   226 
       
   227 
       
   228 public:
       
   229 	bool                 m_is_secret;
       
   230 	eap_configure_type_e m_type;
       
   231 	eap_variable_data_c *m_subsection_name;
       
   232 	u32_t                m_field_length;
       
   233 	i8_t                 m_field[buffer_length];
       
   234 };
       
   235 
       
   236 template <u32_t buffer_length>
       
   237 inline const eap_configuration_field_c * eap_configuration_field_template_c<buffer_length>::get_field() const
       
   238 {
       
   239 	return reinterpret_cast<const eap_configuration_field_c *>(this);
       
   240 }
       
   241 
       
   242 #define EAP_CONFIGURATION_FIELD(name, field, type, is_secret) \
       
   243 	static const eap_configuration_field_template_c<sizeof(field)> name \
       
   244 		EAP_ATTRIBUTE_UNUSED = {is_secret, type, 0, sizeof(field)-1, field}; \
       
   245 	EAP_AM_CONFIGURATION_FIELD_LITERAL(name, field)
       
   246 
       
   247 //--------------------------------------------------------------------------------------------------
       
   248 
       
   249 
       
   250 #endif //#if !defined(_EAP_CONFIGURATION_FIELD_H_)
       
   251 
       
   252 //--------------------------------------------------
       
   253 
       
   254 
       
   255 // End.