eapol/eapol_framework/eapol_common/type/radius/core/eap_radius_header.cpp
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 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 105 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 #include "eap_radius_header.h"
       
    29 #include "eap_radius_header_string.h"
       
    30 
       
    31 //--------------------------------------------------
       
    32 
       
    33 EAP_FUNC_EXPORT eap_radius_header_base_c::~eap_radius_header_base_c()
       
    34 {
       
    35 }
       
    36 
       
    37 //--------------------------------------------------
       
    38 
       
    39 // 
       
    40 EAP_FUNC_EXPORT eap_radius_header_base_c::eap_radius_header_base_c(
       
    41 	abs_eap_am_tools_c * const tools,
       
    42 	void * const header_begin,
       
    43 	const u32_t header_buffer_length)
       
    44 	: eap_general_header_base_c(tools, header_begin, header_buffer_length)
       
    45 	, m_am_tools(tools)
       
    46 {
       
    47 }
       
    48 
       
    49 //--------------------------------------------------
       
    50 
       
    51 EAP_FUNC_EXPORT u32_t eap_radius_header_base_c::get_header_length()
       
    52 {
       
    53 	return m_attributes_offset;
       
    54 }
       
    55 
       
    56 //--------------------------------------------------
       
    57 
       
    58 EAP_FUNC_EXPORT eap_radius_code_value_e eap_radius_header_base_c::get_code() const
       
    59 {
       
    60 	const u8_t * const code_data = get_header_offset(m_code_offset, sizeof(u8_t));
       
    61 	if (code_data != 0)
       
    62 	{
       
    63 		return static_cast<eap_radius_code_value_e>(*code_data);
       
    64 	}
       
    65 	else
       
    66 	{
       
    67 		return eap_radius_code_none;
       
    68 	}
       
    69 }
       
    70 
       
    71 /// This function returns the authenticator field of RADIUS-header.
       
    72 EAP_FUNC_EXPORT u8_t * eap_radius_header_base_c::get_authenticator() const
       
    73 {
       
    74 	u8_t * authenticator = get_header_offset(
       
    75 		m_authenticator_offset,
       
    76 		get_authenticator_length());
       
    77 	if (authenticator != 0)
       
    78 	{
       
    79 		return authenticator;
       
    80 	}
       
    81 	else
       
    82 	{
       
    83 		return 0;
       
    84 	}
       
    85 }
       
    86 
       
    87 /// This function returns the length of the authenticator field of RADIUS-header.
       
    88 EAP_FUNC_EXPORT u32_t eap_radius_header_base_c::get_authenticator_length() const
       
    89 {
       
    90 	return EAP_RADIUS_AUTHENTICATOR_LENGTH*sizeof(u8_t);
       
    91 }
       
    92 
       
    93 //--------------------------------------------------
       
    94 
       
    95 EAP_FUNC_EXPORT u8_t eap_radius_header_base_c::get_identifier() const
       
    96 {
       
    97 	const u8_t * const identifier_data = get_header_offset(m_identifier_offset, sizeof(u8_t));
       
    98 	if (identifier_data != 0)
       
    99 	{
       
   100 		return *identifier_data;
       
   101 	}
       
   102 	else
       
   103 	{
       
   104 		return 0xff;
       
   105 	}
       
   106 }
       
   107 
       
   108 //--------------------------------------------------
       
   109 
       
   110 EAP_FUNC_EXPORT u16_t eap_radius_header_base_c::get_length() const
       
   111 {
       
   112 	const u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   113 	if (length_data != 0)
       
   114 	{
       
   115 		return static_cast<u16_t>(static_cast<u16_t>(length_data[0]) << 8
       
   116 								  | static_cast<u16_t>(length_data[1]));		
       
   117 	}
       
   118 	else
       
   119 	{
       
   120 		return 0ul;
       
   121 	}
       
   122 }
       
   123 
       
   124 //--------------------------------------------------
       
   125 
       
   126 EAP_FUNC_EXPORT u16_t eap_radius_header_base_c::get_data_length() const
       
   127 {
       
   128 	u32_t length = get_length();
       
   129 	if (length >= get_header_length())
       
   130 	{
       
   131 		return static_cast<u16_t>(length-get_header_length());		
       
   132 	}
       
   133 	else
       
   134 	{
       
   135 		return 0ul;
       
   136 	}
       
   137 }
       
   138 
       
   139 //--------------------------------------------------
       
   140 
       
   141 EAP_FUNC_EXPORT u8_t * eap_radius_header_base_c::get_data_offset(
       
   142 	const u32_t p_offset, const u32_t p_continuous_bytes) const
       
   143 {
       
   144 	if (get_length() >= static_cast<u16_t>(get_header_length()+p_offset+p_continuous_bytes))
       
   145 	{
       
   146 		u8_t * const data_begin = get_header_offset(m_attributes_offset, sizeof(u8_t));
       
   147 		if (data_begin == 0)
       
   148 		{
       
   149 			return 0;
       
   150 		}
       
   151 		return &(data_begin[p_offset]);
       
   152 	}
       
   153 	else
       
   154 	{
       
   155 		return 0;
       
   156 	}
       
   157 }
       
   158 
       
   159 //--------------------------------------------------
       
   160 
       
   161 EAP_FUNC_EXPORT void eap_radius_header_base_c::set_code(const eap_radius_code_value_e p_code)
       
   162 {
       
   163 	u8_t * const code_data = get_header_offset(m_code_offset, sizeof(u8_t));
       
   164 	EAP_ASSERT(code_data != 0);
       
   165 
       
   166 	*code_data = static_cast<u8_t>(p_code);
       
   167 }
       
   168 
       
   169 //--------------------------------------------------
       
   170 
       
   171 EAP_FUNC_EXPORT void eap_radius_header_base_c::set_identifier(const u8_t p_identifier)
       
   172 {
       
   173 	u8_t * const identifier_data = get_header_offset(m_identifier_offset, sizeof(u8_t));
       
   174 	EAP_ASSERT(identifier_data != 0);
       
   175 
       
   176 	*identifier_data = p_identifier;
       
   177 }
       
   178 
       
   179 //--------------------------------------------------
       
   180 
       
   181 EAP_FUNC_EXPORT void eap_radius_header_base_c::set_length(const u16_t p_length)
       
   182 {
       
   183 	u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   184 	EAP_ASSERT(length_data != 0);
       
   185 
       
   186 	length_data[0] = static_cast<u8_t>((p_length & 0xff00) >> 8);
       
   187 	length_data[1] = static_cast<u8_t>(p_length & 0x00ff);
       
   188 }
       
   189 
       
   190 //--------------------------------------------------
       
   191 
       
   192 EAP_FUNC_EXPORT void eap_radius_header_base_c::set_data_length(const u16_t data_length)
       
   193 {
       
   194 	u16_t length = static_cast<u16_t>(data_length+get_header_length());
       
   195 
       
   196 	u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   197 	EAP_ASSERT(length_data != 0);
       
   198 
       
   199 	length_data[0] = static_cast<u8_t>((length & 0xff00) >> 8);
       
   200 	length_data[1] = static_cast<u8_t>(length & 0x00ff);
       
   201 }
       
   202 
       
   203 //--------------------------------------------------
       
   204 
       
   205 EAP_FUNC_EXPORT eap_const_string eap_radius_header_base_c::get_code_string() const
       
   206 {
       
   207 	return eap_radius_header_string_c::get_code_string(get_code());
       
   208 }
       
   209 
       
   210 //--------------------------------------------------
       
   211 
       
   212 EAP_FUNC_EXPORT eap_status_e eap_radius_header_base_c::check_header() const
       
   213 {
       
   214 	eap_status_e status = eap_status_process_general_error;
       
   215 
       
   216 	if (get_code() > eap_radius_code_maximum_supported
       
   217 		|| eap_radius_code_maximum_supported < get_code())
       
   218 	{
       
   219 		status = eap_status_illegal_eap_code;
       
   220 
       
   221 		EAP_TRACE_DEBUG(
       
   222 			get_am_tools(),
       
   223 			TRACE_FLAGS_DEFAULT,
       
   224 			(EAPL("ERROR: eap_header_c::check_header(), illegal code field 0x%02x, eap_status_e %d\n"),
       
   225 			 get_code(),
       
   226 			 status));
       
   227 	}
       
   228 	else
       
   229 	{
       
   230 		status = eap_status_ok;
       
   231 	}
       
   232 
       
   233 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   234 }
       
   235 
       
   236 /// This function resets the RADIUS-header.
       
   237 /// The buffer_length parameter is the length of the RADISU-header and the following attribute data buffer.
       
   238 EAP_FUNC_EXPORT void eap_radius_header_base_c::reset_header(u16_t buffer_length)
       
   239 {
       
   240 	set_code(eap_radius_code_none);
       
   241 	set_identifier(0u);
       
   242 	set_length(buffer_length);
       
   243 }
       
   244 
       
   245 //--------------------------------------------------
       
   246 
       
   247 
       
   248 
       
   249 // End.