eapol/eapol_framework/eapol_common/common/eap_buffer.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 21 
       
    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 
       
    29 #include "eap_am_memory.h"
       
    30 #include "eap_buffer.h"
       
    31 #include "eap_am_assert.h"
       
    32 #include "eap_am_tools.h"
       
    33 #include "eapol_ethernet_header.h"
       
    34 
       
    35 static const bool EAP_BUFFER_DO_PACKET_RETRANSMISSION_INIT_VALUE = true;
       
    36 
       
    37 //-----------------------------------------------------------------------------
       
    38 
       
    39 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::check_guard_bytes(const u8_t * const guard) const
       
    40 {
       
    41 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
    42 
       
    43 	if (m_data == 0)
       
    44 	{
       
    45 		return false;
       
    46 	}
       
    47 
       
    48 	u32_t ind = 0;
       
    49 	for (ind = 0; ind < m_data->m_mem_guard_length; ind++)
       
    50 	{
       
    51 		if (guard[ind] != EAP_MEM_GUARD_BYTE)
       
    52 		{
       
    53 			EAP_TRACE_ERROR(
       
    54 				m_am_tools,
       
    55 				TRACE_FLAGS_DEFAULT,
       
    56 				(EAPL("ERROR: check_guard_bytes(): buffer overflow in byte %d."),
       
    57 				ind));
       
    58 			EAP_ASSERT_ANYWAY_TOOLS(m_am_tools);
       
    59 			return false;
       
    60 		}
       
    61 	}
       
    62 	return true;
       
    63 }
       
    64 
       
    65 //-----------------------------------------------------------------------------
       
    66 
       
    67 EAP_FUNC_EXPORT abs_eap_am_tools_c * eap_buf_chain_base_c::get_am_tools()
       
    68 {
       
    69 	return m_am_tools;
       
    70 }
       
    71 
       
    72 //-----------------------------------------------------------------------------
       
    73 
       
    74 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::check_guards() const
       
    75 {
       
    76 
       
    77 #if defined(_DEBUG)
       
    78 	if (m_data == 0
       
    79 		|| m_data->m_buffer == 0)
       
    80 	{
       
    81 		return false;
       
    82 	}
       
    83 
       
    84 	if (m_data->m_buffer->get_is_writable() == true)
       
    85 	{
       
    86 		const u8_t * const buffer = m_data->m_buffer->get_data(
       
    87 			m_data->m_buffer->get_data_length());
       
    88 
       
    89 		if (buffer == 0)
       
    90 		{
       
    91 			return true;
       
    92 		}
       
    93 
       
    94 		if (check_guard_bytes(buffer) == false
       
    95 			|| check_guard_bytes(
       
    96 				buffer+(m_data->m_buffer->get_buffer_length()
       
    97 						- m_data->m_mem_guard_length)) == false
       
    98 			/* || check_guard_bytes(buffer+(m_data->m_real_data_length
       
    99 			   - m_data->m_mem_guard_length)) == false */)
       
   100 		{
       
   101 			EAP_TRACE_ERROR(
       
   102 				m_am_tools,
       
   103 				TRACE_FLAGS_DEFAULT,
       
   104 				(EAPL("ERROR: check_guard_bytes(): buffer overflow, buffer 0x%08x."),
       
   105 				buffer - m_data->m_mem_guard_length));
       
   106 			EAP_ASSERT_ANYWAY_TOOLS(m_am_tools);
       
   107 			return false;
       
   108 		}
       
   109 	}
       
   110 #endif //#if defined(_DEBUG)
       
   111 
       
   112 	return true;
       
   113 }
       
   114 
       
   115 //-----------------------------------------------------------------------------
       
   116 
       
   117 EAP_FUNC_EXPORT eap_buf_chain_base_c::~eap_buf_chain_base_c()
       
   118 {
       
   119 	if (m_data != 0)
       
   120 	{
       
   121 		check_guards();
       
   122 
       
   123 		delete m_data->m_buffer;
       
   124 		m_data->m_buffer = 0;
       
   125 	}
       
   126 
       
   127 	delete m_data;
       
   128 	m_data = 0;
       
   129 }
       
   130 
       
   131 //-----------------------------------------------------------------------------
       
   132 
       
   133 EAP_FUNC_EXPORT eap_buf_chain_base_c::eap_buf_chain_base_c(
       
   134 	const eap_write_buffer_e, 
       
   135 	abs_eap_am_tools_c * const tools,
       
   136 	u8_t * const data, 
       
   137 	const u32_t data_length, 
       
   138 	const bool reset_data,
       
   139 	const bool free_buffer, 
       
   140 	const u32_t mem_guard_length)
       
   141 		: m_am_tools(tools)
       
   142 		, m_data(0)
       
   143 {
       
   144 	eap_status_e status = initialize(mem_guard_length);
       
   145 	if (status != eap_status_ok)
       
   146 	{
       
   147 		if (free_buffer == true)
       
   148 		{
       
   149 			delete [] data;
       
   150 		}
       
   151 		return;
       
   152 	}
       
   153 
       
   154 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   155 
       
   156 	if (m_data == 0
       
   157 		|| m_data->m_buffer == 0)
       
   158 	{
       
   159 		if (free_buffer == true)
       
   160 		{
       
   161 			delete [] data;
       
   162 		}
       
   163 		return;
       
   164 	}
       
   165 
       
   166 	status = m_data->m_buffer->set_buffer(
       
   167 		data,
       
   168 		data_length,
       
   169 		free_buffer,
       
   170 		true);
       
   171 	if (status != eap_status_ok)
       
   172 	{
       
   173 		if (free_buffer == true)
       
   174 		{
       
   175 			delete [] data;
       
   176 		}
       
   177 		m_data->m_is_valid = false;
       
   178 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   179 		return;
       
   180 	}
       
   181 
       
   182 	EAP_ASSERT_TOOLS(m_am_tools, data_length >= m_data->m_mem_guard_length);
       
   183 	m_data->m_buffer->set_data_length(data_length - m_data->m_mem_guard_length);
       
   184 
       
   185 	set_mem_guard_bytes();
       
   186 
       
   187 	if (reset_data == true)
       
   188 	{
       
   189 		reset_data_buffer();
       
   190 	}
       
   191 }
       
   192 
       
   193 //-----------------------------------------------------------------------------
       
   194 
       
   195 EAP_FUNC_EXPORT eap_buf_chain_base_c::eap_buf_chain_base_c(
       
   196 	const eap_read_buffer_e,
       
   197 	abs_eap_am_tools_c * const tools,
       
   198 	const u8_t * const data, 
       
   199 	const u32_t data_length,
       
   200 	const bool free_buffer)
       
   201 		: m_am_tools(tools)
       
   202 		, m_data(0)
       
   203 {
       
   204 	eap_status_e status = initialize(0ul);
       
   205 	if (status != eap_status_ok)
       
   206 	{
       
   207 		return;
       
   208 	}
       
   209 
       
   210 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   211 
       
   212 	if (m_data == 0
       
   213 		|| m_data->m_buffer == 0)
       
   214 	{
       
   215 		return;
       
   216 	}
       
   217 
       
   218 	status = m_data->m_buffer->set_buffer(
       
   219 		data,
       
   220 		data_length,
       
   221 		free_buffer,
       
   222 		false);
       
   223 	if (status != eap_status_ok)
       
   224 	{
       
   225 		m_data->m_is_valid = false;
       
   226 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   227 		return;
       
   228 	}
       
   229 }
       
   230 
       
   231 //-----------------------------------------------------------------------------
       
   232 
       
   233 EAP_FUNC_EXPORT eap_buf_chain_base_c::eap_buf_chain_base_c(
       
   234 	const eap_write_buffer_e,
       
   235 	abs_eap_am_tools_c * const tools,
       
   236 	const u32_t data_length)
       
   237 		: m_am_tools(tools)
       
   238 		, m_data(0)
       
   239 {
       
   240 	eap_status_e status = initialize(EAP_MEM_GUARD_LENGTH);
       
   241 	if (status != eap_status_ok)
       
   242 	{
       
   243 		return;
       
   244 	}
       
   245 
       
   246 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   247 
       
   248 	if (m_data == 0
       
   249 		|| m_data->m_buffer == 0)
       
   250 	{
       
   251 		return;
       
   252 	}
       
   253 
       
   254 	status = m_data->m_buffer->init(data_length+2*m_data->m_mem_guard_length);
       
   255 	if (status == eap_status_ok)
       
   256 	{
       
   257 		m_data->m_buffer->set_is_valid();
       
   258 		m_data->m_buffer->set_data_length(m_data->m_mem_guard_length);
       
   259 		set_mem_guard_bytes();
       
   260 	}
       
   261 	else
       
   262 	{
       
   263 		m_data->m_is_valid = false;
       
   264 	}
       
   265 }
       
   266 
       
   267 //-----------------------------------------------------------------------------
       
   268 
       
   269 EAP_FUNC_EXPORT eap_buf_chain_base_c::eap_buf_chain_base_c(
       
   270 	const eap_read_buffer_e,
       
   271 	abs_eap_am_tools_c * const tools,
       
   272 	const u32_t data_length)
       
   273 		: m_am_tools(tools)
       
   274 		, m_data(0)
       
   275 {
       
   276 	eap_status_e status = initialize(EAP_MEM_GUARD_LENGTH);
       
   277 	if (status != eap_status_ok)
       
   278 	{
       
   279 		return;
       
   280 	}
       
   281 
       
   282 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   283 
       
   284 	if (m_data == 0
       
   285 		|| m_data->m_buffer == 0)
       
   286 	{
       
   287 		return;
       
   288 	}
       
   289 
       
   290 	if (data_length > 0ul)
       
   291 	{
       
   292 		status = m_data->m_buffer->init(data_length+2*m_data->m_mem_guard_length);
       
   293 		if (status == eap_status_ok)
       
   294 		{
       
   295 			m_data->m_buffer->set_is_valid();
       
   296 			m_data->m_buffer->set_data_length(m_data->m_mem_guard_length);
       
   297 			set_mem_guard_bytes();
       
   298 		}
       
   299 		else
       
   300 		{
       
   301 			m_data->m_is_valid = false;
       
   302 		}
       
   303 	}
       
   304 }
       
   305 
       
   306 //-----------------------------------------------------------------------------
       
   307 
       
   308 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::initialize(
       
   309 	const u32_t mem_guard_length)
       
   310 {
       
   311 	m_data = new eap_buf_chain_base_impl_str;
       
   312 	if (m_data == 0)
       
   313 	{
       
   314 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   315 	}
       
   316 
       
   317 	m_data->m_mem_guard_length = mem_guard_length;
       
   318 	m_data->m_buffer = 0;
       
   319 	m_data->m_next_buffer = 0;
       
   320 	m_data->m_random_error_type = eap_random_error_type_none_keep_this_last_case;
       
   321 	m_data->m_send_packet_index = 0ul;
       
   322 	m_data->m_stack_address = 0;
       
   323 	m_data->m_is_valid = false;
       
   324 	m_data->m_is_manipulated = false;
       
   325 	m_data->m_do_packet_retransmission = EAP_BUFFER_DO_PACKET_RETRANSMISSION_INIT_VALUE;
       
   326 	m_data->m_is_client = true;
       
   327 	m_data->m_do_length_checks = false;
       
   328 	m_data->m_encrypt = false;
       
   329 
       
   330 	m_data->m_buffer = new eap_variable_data_c(m_am_tools);
       
   331 	if (m_data->m_buffer == 0
       
   332 		|| m_data->m_buffer->get_is_valid() == false)
       
   333 	{
       
   334 		delete m_data;
       
   335 		m_data = 0;
       
   336 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   337 	}
       
   338 
       
   339 	m_data->m_is_valid = true;
       
   340 
       
   341 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   342 }
       
   343 
       
   344 //-----------------------------------------------------------------------------
       
   345 
       
   346 EAP_FUNC_EXPORT void eap_buf_chain_base_c::reset_data_buffer()
       
   347 {
       
   348 	if (m_data == 0
       
   349 		|| m_data->m_buffer == 0)
       
   350 	{
       
   351 		return;
       
   352 	}
       
   353 
       
   354 	m_am_tools->memset(
       
   355 		get_data(get_data_length()),
       
   356 		0,
       
   357 		get_data_length());
       
   358 }
       
   359 
       
   360 //-----------------------------------------------------------------------------
       
   361 
       
   362 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_mem_guard_bytes()
       
   363 {
       
   364 	EAP_ASSERT_TOOLS(
       
   365 		m_am_tools,
       
   366 		m_data != 0);
       
   367 	EAP_ASSERT_TOOLS(
       
   368 		m_am_tools,
       
   369 		m_data->m_buffer->get_buffer_length()
       
   370 		>= m_data->m_mem_guard_length);
       
   371 	EAP_ASSERT_TOOLS(
       
   372 		m_am_tools,
       
   373 		m_data->m_buffer->get_data_length()
       
   374 		>= m_data->m_mem_guard_length);
       
   375 
       
   376 	if (m_data == 0
       
   377 		|| m_data->m_buffer == 0)
       
   378 	{
       
   379 		return;
       
   380 	}
       
   381 
       
   382 	u8_t * const buffer = m_data->m_buffer->get_data(m_data->m_buffer->get_data_length());
       
   383 
       
   384 	m_am_tools->memset(
       
   385 		buffer,
       
   386 		EAP_MEM_GUARD_BYTE,
       
   387 		m_data->m_mem_guard_length);
       
   388 
       
   389 	m_am_tools->memset(
       
   390 		buffer+(m_data->m_buffer->get_buffer_length() - m_data->m_mem_guard_length),
       
   391 		EAP_MEM_GUARD_BYTE,
       
   392 		m_data->m_mem_guard_length);
       
   393 
       
   394 	m_am_tools->memset(
       
   395 		buffer+(m_data->m_buffer->get_data_length()),
       
   396 		EAP_MEM_GUARD_BYTE,
       
   397 		m_data->m_mem_guard_length);
       
   398 }
       
   399 
       
   400 //-----------------------------------------------------------------------------
       
   401 
       
   402 EAP_FUNC_EXPORT u32_t eap_buf_chain_base_c::get_mem_guard_length()
       
   403 {
       
   404 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   405 
       
   406 	if (m_data == 0)
       
   407 	{
       
   408 		return 0;
       
   409 	}
       
   410 
       
   411 	return m_data->m_mem_guard_length;
       
   412 }
       
   413 
       
   414 //-----------------------------------------------------------------------------
       
   415 
       
   416 EAP_FUNC_EXPORT u32_t eap_buf_chain_base_c::get_buffer_length() const
       
   417 {
       
   418 	EAP_ASSERT_TOOLS(
       
   419 		m_am_tools,
       
   420 		m_data != 0);
       
   421 	EAP_ASSERT_TOOLS(
       
   422 		m_am_tools,
       
   423 		m_data->m_buffer->get_buffer_length()
       
   424 		>= 2ul*m_data->m_mem_guard_length);
       
   425 
       
   426 	if (m_data == 0
       
   427 		|| m_data->m_buffer == 0)
       
   428 	{
       
   429 		return 0;
       
   430 	}
       
   431 
       
   432 	return m_data->m_buffer->get_buffer_length() - 2ul*m_data->m_mem_guard_length;
       
   433 }
       
   434 
       
   435 //-----------------------------------------------------------------------------
       
   436 
       
   437 EAP_FUNC_EXPORT u32_t eap_buf_chain_base_c::get_data_length() const
       
   438 {
       
   439 	EAP_ASSERT_TOOLS(
       
   440 		m_am_tools,
       
   441 		m_data != 0);
       
   442 	EAP_ASSERT_TOOLS(
       
   443 		m_am_tools,
       
   444 		m_data->m_buffer->get_data_length()
       
   445 		>= m_data->m_mem_guard_length);
       
   446 
       
   447 	if (m_data == 0
       
   448 		|| m_data->m_buffer == 0)
       
   449 	{
       
   450 		return 0;
       
   451 	}
       
   452 
       
   453 	return m_data->m_buffer->get_data_length() - m_data->m_mem_guard_length;
       
   454 }
       
   455 
       
   456 //-----------------------------------------------------------------------------
       
   457 
       
   458 EAP_FUNC_EXPORT u8_t * eap_buf_chain_base_c::get_data_offset(
       
   459 	const u32_t p_offset,
       
   460 	const u32_t p_continuous_bytes) const
       
   461 {
       
   462 	EAP_ASSERT_TOOLS(
       
   463 		m_am_tools,
       
   464 		m_data != 0);
       
   465 	EAP_ASSERT_TOOLS(
       
   466 		m_am_tools,
       
   467 		m_data->m_buffer->get_data_length() >= m_data->m_mem_guard_length);
       
   468 
       
   469 	if (m_data == 0
       
   470 		|| m_data->m_buffer == 0)
       
   471 	{
       
   472 		return 0;
       
   473 	}
       
   474 
       
   475 	if (p_offset+p_continuous_bytes <= get_buffer_length())
       
   476 	{
       
   477 		return m_data->m_buffer->get_buffer_offset(
       
   478 			p_offset + m_data->m_mem_guard_length,
       
   479 			p_continuous_bytes);
       
   480 	}
       
   481 	else
       
   482 	{
       
   483 		EAP_TRACE_ERROR(
       
   484 			m_am_tools,
       
   485 			TRACE_FLAGS_DEFAULT,
       
   486 			(EAPL("ERROR: Fragments are not supported yet.\n")));
       
   487 		EAP_ASSERT_ALWAYS_TOOLS(
       
   488 			m_am_tools,
       
   489 			p_offset+p_continuous_bytes <= get_data_length());
       
   490 		return 0;
       
   491 	}
       
   492 }
       
   493 
       
   494 //-----------------------------------------------------------------------------
       
   495 
       
   496 EAP_FUNC_EXPORT u8_t * eap_buf_chain_base_c::get_data(
       
   497 	const u32_t p_continuous_bytes) const
       
   498 {
       
   499 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   500 
       
   501 	if (m_data == 0
       
   502 		|| m_data->m_buffer == 0)
       
   503 	{
       
   504 		return 0;
       
   505 	}
       
   506 
       
   507 	return get_data_offset(0u, p_continuous_bytes);
       
   508 }
       
   509 
       
   510 //-----------------------------------------------------------------------------
       
   511 
       
   512 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::set_buffer_length(
       
   513 	const u32_t buffer_length)
       
   514 {
       
   515 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   516 	EAP_ASSERT_TOOLS(m_am_tools, m_data->m_buffer != 0);
       
   517 
       
   518 	if (m_data == 0
       
   519 		|| m_am_tools == 0
       
   520 		|| m_am_tools->get_is_valid() == false)
       
   521 	{
       
   522 		return eap_status_allocation_error;
       
   523 	}
       
   524 
       
   525 	if (m_data->m_buffer == 0)
       
   526 	{
       
   527 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   528 	}
       
   529 
       
   530 	m_data->m_buffer->reset();
       
   531 
       
   532 	eap_status_e status = m_data->m_buffer->init(buffer_length+2*m_data->m_mem_guard_length);
       
   533 	if (status == eap_status_ok)
       
   534 	{
       
   535 		m_data->m_buffer->set_is_valid();
       
   536 		m_data->m_buffer->set_data_length(m_data->m_mem_guard_length);
       
   537 		set_mem_guard_bytes();
       
   538 	}
       
   539 
       
   540 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   541 }
       
   542 
       
   543 //-----------------------------------------------------------------------------
       
   544 
       
   545 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::set_data_length(
       
   546 	const u32_t length)
       
   547 {
       
   548 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   549 	EAP_ASSERT_TOOLS(m_am_tools, m_data->m_buffer != 0);
       
   550 	EAP_ASSERT_TOOLS(m_am_tools, m_data->m_buffer->get_is_writable() == true);
       
   551 
       
   552 	if (m_data == 0
       
   553 		|| m_am_tools == 0
       
   554 		|| m_am_tools->get_is_valid() == false)
       
   555 	{
       
   556 		return eap_status_allocation_error;
       
   557 	}
       
   558 
       
   559 	if (m_data->m_buffer == 0)
       
   560 	{
       
   561 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   562 	}
       
   563 
       
   564 	check_guards();
       
   565 
       
   566 	if (get_buffer_length() > length)
       
   567 	{
       
   568 		// Here we must reset the memory guard bytes because not all of the buffer is used.
       
   569 		EAP_ASSERT_TOOLS(
       
   570 			m_am_tools,
       
   571 			m_data->m_buffer->get_data_length()
       
   572 			>= m_data->m_mem_guard_length);
       
   573 
       
   574 		u8_t * const buffer = m_data->m_buffer->get_data_offset(
       
   575 			m_data->m_mem_guard_length,
       
   576 			m_data->m_buffer->get_data_length() - m_data->m_mem_guard_length);
       
   577 		if (buffer == 0)
       
   578 		{
       
   579 			return EAP_STATUS_RETURN(m_am_tools, eap_status_buffer_too_short);
       
   580 		}
       
   581 
       
   582 		m_am_tools->memset(
       
   583 			buffer+length,
       
   584 			EAP_MEM_GUARD_BYTE,
       
   585 			m_data->m_mem_guard_length);
       
   586 	}
       
   587 
       
   588 	m_data->m_buffer->set_data_length(length + m_data->m_mem_guard_length);
       
   589 
       
   590 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   591 }
       
   592 
       
   593 //-----------------------------------------------------------------------------
       
   594 
       
   595 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_is_valid() const
       
   596 {
       
   597 	if (m_data == 0)
       
   598 	{
       
   599 		return false;
       
   600 	}
       
   601 
       
   602 	return m_data->m_is_valid;
       
   603 }
       
   604 
       
   605 //--------------------------------------------------
       
   606 
       
   607 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_is_valid_data() const
       
   608 {
       
   609 	if (m_data == 0)
       
   610 	{
       
   611 		return false;
       
   612 	}
       
   613 
       
   614 	if (m_data->m_is_valid == false)
       
   615 	{
       
   616 		return false;
       
   617 	}
       
   618 
       
   619 	// Note the buffer could be empty.
       
   620 	if (m_data->m_buffer == 0)
       
   621 	{
       
   622 		return false;
       
   623 	}
       
   624 
       
   625 	return true;
       
   626 }
       
   627 
       
   628 //-----------------------------------------------------------------------------
       
   629 
       
   630 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_is_manipulated()
       
   631 {
       
   632 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   633 
       
   634 	if (m_data == 0)
       
   635 	{
       
   636 		return;
       
   637 	}
       
   638 
       
   639 	m_data->m_is_manipulated = true;
       
   640 }
       
   641 
       
   642 //-----------------------------------------------------------------------------
       
   643 
       
   644 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_is_manipulated()
       
   645 {
       
   646 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   647 
       
   648 	if (m_data == 0)
       
   649 	{
       
   650 		return false;
       
   651 	}
       
   652 
       
   653 	return m_data->m_is_manipulated;
       
   654 }
       
   655 
       
   656 //-----------------------------------------------------------------------------
       
   657 
       
   658 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_send_packet_index(const u32_t send_packet_index)
       
   659 {
       
   660 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   661 
       
   662 	if (m_data == 0)
       
   663 	{
       
   664 		return;
       
   665 	}
       
   666 
       
   667 	m_data->m_send_packet_index = send_packet_index;
       
   668 }
       
   669 
       
   670 //-----------------------------------------------------------------------------
       
   671 
       
   672 EAP_FUNC_EXPORT u32_t eap_buf_chain_base_c::get_send_packet_index()
       
   673 {
       
   674 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   675 
       
   676 	if (m_data == 0)
       
   677 	{
       
   678 		return 0;
       
   679 	}
       
   680 
       
   681 	return m_data->m_send_packet_index;
       
   682 }
       
   683 
       
   684 //-----------------------------------------------------------------------------
       
   685 
       
   686 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_random_error_type(
       
   687 	eap_random_error_type error_type)
       
   688 {
       
   689 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   690 
       
   691 	if (m_data == 0)
       
   692 	{
       
   693 		return;
       
   694 	}
       
   695 
       
   696 	m_data->m_random_error_type = error_type;
       
   697 }
       
   698 
       
   699 //-----------------------------------------------------------------------------
       
   700 
       
   701 EAP_FUNC_EXPORT eap_random_error_type eap_buf_chain_base_c::get_random_error_type()
       
   702 {
       
   703 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   704 
       
   705 	if (m_data == 0)
       
   706 	{
       
   707 		return eap_random_error_type_manipulate_byte;
       
   708 	}
       
   709 
       
   710 	return m_data->m_random_error_type;
       
   711 }
       
   712 
       
   713 //-----------------------------------------------------------------------------
       
   714 
       
   715 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_do_packet_retransmission(
       
   716 	const bool do_packet_retransmission_when_true)
       
   717 {
       
   718 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   719 
       
   720 	if (m_data == 0)
       
   721 	{
       
   722 		return;
       
   723 	}
       
   724 
       
   725 	m_data->m_do_packet_retransmission = do_packet_retransmission_when_true;
       
   726 }
       
   727 
       
   728 //-----------------------------------------------------------------------------
       
   729 
       
   730 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_do_packet_retransmission()
       
   731 {
       
   732 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   733 
       
   734 	if (m_data == 0)
       
   735 	{
       
   736 		return false;
       
   737 	}
       
   738 
       
   739 	return m_data->m_do_packet_retransmission;
       
   740 }
       
   741 
       
   742 //-----------------------------------------------------------------------------
       
   743 
       
   744 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_is_client(const bool is_client_when_true)
       
   745 {
       
   746 	m_data->m_is_client = is_client_when_true;
       
   747 }
       
   748 
       
   749 //-----------------------------------------------------------------------------
       
   750 
       
   751 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_is_client() const
       
   752 {
       
   753 	return m_data->m_is_client;
       
   754 }
       
   755 
       
   756 //-----------------------------------------------------------------------------
       
   757 
       
   758 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_do_length_checks(const bool do_length_checks)
       
   759 {
       
   760 	m_data->m_do_length_checks = do_length_checks;
       
   761 }
       
   762 
       
   763 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_do_length_checks() const
       
   764 {
       
   765 	return m_data->m_do_length_checks;
       
   766 }
       
   767 
       
   768 //-----------------------------------------------------------------------------
       
   769 
       
   770 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_encrypt(const bool encrypt_when_true)
       
   771 {
       
   772 	m_data->m_encrypt = encrypt_when_true;
       
   773 }
       
   774 
       
   775 EAP_FUNC_EXPORT bool eap_buf_chain_base_c::get_encrypt() const
       
   776 {
       
   777 	return m_data->m_encrypt;
       
   778 }
       
   779 
       
   780 //-----------------------------------------------------------------------------
       
   781 
       
   782 EAP_FUNC_EXPORT void eap_buf_chain_base_c::set_stack_address(const void * const stack_address)
       
   783 {
       
   784 	m_data->m_stack_address = stack_address;
       
   785 }
       
   786 
       
   787 EAP_FUNC_EXPORT const void * eap_buf_chain_base_c::get_stack_address() const
       
   788 {
       
   789 	return m_data->m_stack_address; 
       
   790 }
       
   791 
       
   792 //-----------------------------------------------------------------------------
       
   793 
       
   794 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::add_data(
       
   795 	const void * const buffer,
       
   796 	const u32_t buffer_length)
       
   797 {
       
   798 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   799 
       
   800 	if (m_data == 0
       
   801 		|| m_am_tools == 0
       
   802 		|| m_am_tools->get_is_valid() == false)
       
   803 	{
       
   804 		return eap_status_allocation_error;
       
   805 	}
       
   806 
       
   807 	eap_status_e status = add_data_to_offset(
       
   808 		get_data_length(),
       
   809 		buffer,
       
   810 		buffer_length);
       
   811 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   812 }
       
   813 
       
   814 //-----------------------------------------------------------------------------
       
   815 
       
   816 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::add_data(
       
   817 	const eap_variable_data_c * const buffer)
       
   818 {
       
   819 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   820 
       
   821 	if (m_data == 0
       
   822 		|| m_am_tools == 0
       
   823 		|| m_am_tools->get_is_valid() == false)
       
   824 	{
       
   825 		return eap_status_allocation_error;
       
   826 	}
       
   827 
       
   828 	return EAP_STATUS_RETURN(m_am_tools, add_data(
       
   829 		buffer->get_data(buffer->get_data_length()),
       
   830 		buffer->get_data_length()));
       
   831 }
       
   832 
       
   833 //-----------------------------------------------------------------------------
       
   834 
       
   835 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::add_data_to_offset(
       
   836 	const u32_t offset,
       
   837 	const void * const buffer,
       
   838 	const u32_t buffer_length)
       
   839 {
       
   840 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   841 
       
   842 	if (m_data == 0
       
   843 		|| m_am_tools == 0
       
   844 		|| m_am_tools->get_is_valid() == false)
       
   845 	{
       
   846 		return eap_status_allocation_error;
       
   847 	}
       
   848 
       
   849 	eap_status_e status = m_data->m_buffer->add_data_to_offset(
       
   850 		m_data->m_mem_guard_length+offset,
       
   851 		buffer,
       
   852 		buffer_length);
       
   853 
       
   854 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   855 }
       
   856 
       
   857 //-----------------------------------------------------------------------------
       
   858 
       
   859 EAP_FUNC_EXPORT eap_status_e eap_buf_chain_base_c::add_data_to_offset(
       
   860 	const u32_t offset,
       
   861 	const eap_variable_data_c * const buffer)
       
   862 {
       
   863 	EAP_ASSERT_TOOLS(m_am_tools, m_data != 0);
       
   864 
       
   865 	if (m_data == 0
       
   866 		|| m_am_tools == 0
       
   867 		|| m_am_tools->get_is_valid() == false)
       
   868 	{
       
   869 		return eap_status_allocation_error;
       
   870 	}
       
   871 
       
   872 	eap_status_e status = m_data->m_buffer->add_data_to_offset(
       
   873 		m_data->m_mem_guard_length+offset,
       
   874 		buffer->get_data(buffer->get_data_length()),
       
   875 		buffer->get_data_length());
       
   876 
       
   877 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   878 }
       
   879 
       
   880 
       
   881 //-----------------------------------------------------------------------------
       
   882 //-----------------------------------------------------------------------------
       
   883 //-----------------------------------------------------------------------------
       
   884 
       
   885 EAP_FUNC_EXPORT eap_buf_chain_wr_c::~eap_buf_chain_wr_c()
       
   886 {
       
   887 }
       
   888 
       
   889 //-----------------------------------------------------------------------------
       
   890 
       
   891 EAP_FUNC_EXPORT eap_buf_chain_wr_c::eap_buf_chain_wr_c(
       
   892 	const eap_write_buffer_e,
       
   893 	abs_eap_am_tools_c * const tools,
       
   894 	u8_t * const data,
       
   895 	const u32_t data_length,
       
   896 	const bool reset_data,
       
   897 	const bool free_buffer,
       
   898 	const u32_t mem_guard_length)
       
   899 	: eap_buf_chain_base_c(
       
   900 		eap_write_buffer,
       
   901 		tools,
       
   902 		data,
       
   903 		data_length,
       
   904 		reset_data,
       
   905 		free_buffer,
       
   906 		mem_guard_length)
       
   907 {
       
   908 }
       
   909 
       
   910 //-----------------------------------------------------------------------------
       
   911 
       
   912 EAP_FUNC_EXPORT eap_buf_chain_wr_c::eap_buf_chain_wr_c(
       
   913 	const eap_write_buffer_e,
       
   914 	abs_eap_am_tools_c * const tools,
       
   915 	const u32_t data_length)
       
   916 	: eap_buf_chain_base_c(eap_write_buffer, tools, data_length)
       
   917 {
       
   918 }
       
   919 
       
   920 //-----------------------------------------------------------------------------
       
   921 
       
   922 EAP_FUNC_EXPORT eap_buf_chain_wr_c::eap_buf_chain_wr_c(
       
   923 	const eap_write_buffer_e,
       
   924 	abs_eap_am_tools_c * const tools)
       
   925 	: eap_buf_chain_base_c(eap_write_buffer, tools, 0ul)
       
   926 {
       
   927 }
       
   928 
       
   929 //-----------------------------------------------------------------------------
       
   930 
       
   931 EAP_FUNC_EXPORT void eap_buf_chain_wr_c::force_inheritance()
       
   932 {
       
   933 }
       
   934 
       
   935 //-----------------------------------------------------------------------------
       
   936 
       
   937 EAP_FUNC_EXPORT u8_t * eap_buf_chain_wr_c::get_ethernet_header()
       
   938 {
       
   939 	return static_cast<u8_t *>(
       
   940 		get_data(
       
   941 			eapol_ethernet_header_wr_c::get_header_length()));
       
   942 }
       
   943 
       
   944 //-----------------------------------------------------------------------------
       
   945 
       
   946 EAP_FUNC_EXPORT eap_buf_chain_wr_c * eap_buf_chain_wr_c::copy()
       
   947 {
       
   948 	if (get_is_valid() == false)
       
   949 	{
       
   950 		return 0;
       
   951 	}
       
   952 
       
   953 	u8_t *new_data = new u8_t[get_buffer_length()+(2u*get_mem_guard_length())];
       
   954 	
       
   955 	if (new_data == 0)
       
   956 	{
       
   957 		return 0;
       
   958 	}
       
   959 
       
   960 	get_am_tools()->memmove(
       
   961 		new_data+get_mem_guard_length(),
       
   962 		get_data(get_buffer_length()),
       
   963 		get_buffer_length());
       
   964 
       
   965 	eap_buf_chain_wr_c * const new_buffer
       
   966 		= new eap_buf_chain_wr_c(
       
   967 			eap_write_buffer,
       
   968 			get_am_tools(),
       
   969 			new_data,
       
   970 			get_buffer_length()+(2u*get_mem_guard_length()),
       
   971 			false, // Do not init data
       
   972 			true, // Free buffer on destructor
       
   973 			get_mem_guard_length());
       
   974 
       
   975 	if (new_buffer == 0
       
   976 		|| new_buffer->get_is_valid_data() == false)
       
   977 	{
       
   978 		delete new_buffer;
       
   979 		return 0;
       
   980 	}
       
   981 
       
   982 	new_buffer->set_data_length(get_data_length());
       
   983 
       
   984 	new_buffer->set_send_packet_index(get_send_packet_index());
       
   985 	new_buffer->set_random_error_type(get_random_error_type());
       
   986 	new_buffer->set_stack_address(get_stack_address());
       
   987 	new_buffer->set_is_client(get_is_client());
       
   988 
       
   989 	return new_buffer;
       
   990 }
       
   991 
       
   992 //-----------------------------------------------------------------------------
       
   993 //-----------------------------------------------------------------------------
       
   994 //-----------------------------------------------------------------------------
       
   995 
       
   996 EAP_FUNC_EXPORT eap_buf_chain_rd_c::~eap_buf_chain_rd_c()
       
   997 {
       
   998 	check_guards();
       
   999 }
       
  1000 
       
  1001 //-----------------------------------------------------------------------------
       
  1002 
       
  1003 EAP_FUNC_EXPORT eap_buf_chain_rd_c::eap_buf_chain_rd_c(
       
  1004 	const eap_read_buffer_e, 
       
  1005 	abs_eap_am_tools_c * const tools,
       
  1006 	const u8_t * const data, 
       
  1007 	const u32_t data_length,
       
  1008 	const bool free_buffer)
       
  1009 		: eap_buf_chain_base_c(
       
  1010 			eap_read_buffer,
       
  1011 			tools,
       
  1012 			data,
       
  1013 			data_length,
       
  1014 			free_buffer)
       
  1015 {
       
  1016 }
       
  1017 
       
  1018 //-----------------------------------------------------------------------------
       
  1019 
       
  1020 EAP_FUNC_EXPORT eap_buf_chain_rd_c::eap_buf_chain_rd_c(
       
  1021 	const eap_read_buffer_e,
       
  1022 	abs_eap_am_tools_c * const tools,
       
  1023 	const u32_t data_length)
       
  1024 	: eap_buf_chain_base_c(eap_read_buffer, tools, data_length)
       
  1025 {
       
  1026 }
       
  1027 
       
  1028 //-----------------------------------------------------------------------------
       
  1029 
       
  1030 EAP_FUNC_EXPORT void eap_buf_chain_rd_c::force_inheritance()
       
  1031 {
       
  1032 }
       
  1033 
       
  1034 //-----------------------------------------------------------------------------
       
  1035 
       
  1036 EAP_FUNC_EXPORT const u8_t * eap_buf_chain_rd_c::get_data(
       
  1037 	const u32_t p_continuous_bytes) const
       
  1038 {
       
  1039 	return static_cast<const u8_t *>(
       
  1040 		eap_buf_chain_base_c::get_data(p_continuous_bytes));
       
  1041 }
       
  1042 
       
  1043 //-----------------------------------------------------------------------------
       
  1044 
       
  1045 EAP_FUNC_EXPORT const u8_t * eap_buf_chain_rd_c::get_data_offset(
       
  1046 	const u32_t p_offset,
       
  1047 	const u32_t p_continuous_bytes) const
       
  1048 {
       
  1049 	return static_cast<const u8_t *>(
       
  1050 		eap_buf_chain_base_c::get_data_offset(
       
  1051 			p_offset,
       
  1052 			p_continuous_bytes));
       
  1053 }
       
  1054 
       
  1055 //-----------------------------------------------------------------------------
       
  1056 
       
  1057 EAP_FUNC_EXPORT const u8_t * eap_buf_chain_rd_c::get_ethernet_header() const
       
  1058 {
       
  1059 	return static_cast<const u8_t *>(
       
  1060 		get_data(
       
  1061 			eapol_ethernet_header_rd_c::get_header_length()));
       
  1062 }
       
  1063 
       
  1064 //-----------------------------------------------------------------------------
       
  1065 
       
  1066 
       
  1067 
       
  1068 
       
  1069 // End.