eapol/eapol_framework/wapi_common/src/wai_protocol_packet_header.cpp
changeset 18 7aac0b9e8906
parent 17 8840d3e38314
child 20 8b3129ac4c0f
equal deleted inserted replaced
17:8840d3e38314 18:7aac0b9e8906
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : ./accesssec/eapol/eapol_framework/wapi_common/src/wai_protocol_packet_header.cpp
       
     4 *  Part of     : WAPI / WAPI       *** Info from the SWAD
       
     5 *  Description : WAPI authentication
       
     6 *  Version     : %version: 18 % << Don't touch! Updated by Synergy at check-out.
       
     7 *
       
     8 *  Copyright © 2001-2009 Nokia.  All rights reserved.
       
     9 *  This material, including documentation and any related computer
       
    10 *  programs, is protected by copyright controlled by Nokia.  All
       
    11 *  rights are reserved.  Copying, including reproducing, storing,
       
    12 *  adapting or translating, any or all of this material requires the
       
    13 *  prior written consent of Nokia.  This material also contains
       
    14 *  confidential information which may not be disclosed to others
       
    15 *  without the prior written consent of Nokia.
       
    16 * ============================================================================
       
    17 * Template version: 4.1.1
       
    18 */
       
    19 
       
    20 // This is enumeration of WAPI source code.
       
    21 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    22 	#undef EAP_FILE_NUMBER_ENUM
       
    23 	#define EAP_FILE_NUMBER_ENUM 702 
       
    24 	#undef EAP_FILE_NUMBER_DATE 
       
    25 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    26 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    27 
       
    28 #if defined(USE_WAPI_CORE)
       
    29 
       
    30 #include "eap_am_memory.h"
       
    31 #include "wai_protocol_packet_header.h"
       
    32 
       
    33 /** @file */
       
    34 
       
    35 
       
    36 /**
       
    37  * The destructor of the wai_protocol_packet_header_c class does nothing.
       
    38  */
       
    39 wai_protocol_packet_header_c::~wai_protocol_packet_header_c()
       
    40 {
       
    41 }
       
    42 
       
    43 /**
       
    44  * The constructor of the wai_protocol_packet_header_c class.
       
    45  */
       
    46 wai_protocol_packet_header_c::wai_protocol_packet_header_c(
       
    47 	abs_eap_am_tools_c * const tools)
       
    48 	: eap_general_header_base_c(tools, 0, 0)
       
    49 	, m_am_tools(tools)
       
    50 {
       
    51 }
       
    52 
       
    53 /**
       
    54  * The constructor of the wai_protocol_packet_header_c class simply initializes the attributes.
       
    55  */
       
    56 wai_protocol_packet_header_c::wai_protocol_packet_header_c(
       
    57 	abs_eap_am_tools_c * const tools,
       
    58 	void * const header_begin,
       
    59 	const u32_t header_buffer_length)
       
    60 	: eap_general_header_base_c(tools, header_begin, header_buffer_length)
       
    61 	, m_am_tools(tools)
       
    62 {
       
    63 }
       
    64 
       
    65 /**
       
    66  * This function sets the header buffer.
       
    67  */
       
    68 eap_status_e wai_protocol_packet_header_c::set_header_buffer(
       
    69 	void * const header_begin,
       
    70 	const u32_t header_buffer_length)
       
    71 {
       
    72 	eap_general_header_base_c::set_header_buffer(reinterpret_cast<u8_t *>(header_begin), header_buffer_length);
       
    73 
       
    74 	if (get_is_valid() == false)
       
    75 	{
       
    76 		EAP_TRACE_ERROR(
       
    77 			m_am_tools, 
       
    78 			TRACE_FLAGS_DEFAULT, 
       
    79 			(EAPL("wai_protocol_packet_header_c::set_header_buffer(): packet buffer corrupted.\n")));
       
    80 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
    81 	}
       
    82 
       
    83 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
    84 }
       
    85 
       
    86 /**
       
    87  * This function returns the Version value.
       
    88  */
       
    89 wai_protocol_version_e wai_protocol_packet_header_c::get_version() const
       
    90 {
       
    91 	const u8_t * const data = get_header_offset(m_version_offset, m_version_size);
       
    92 	if (data != 0)
       
    93 	{
       
    94 		u16_t value = eap_read_u16_t_network_order(
       
    95 			data, m_version_size);
       
    96 
       
    97 		EAP_STATIC_ASSERT(m_version_size == sizeof(value));
       
    98 
       
    99 		return static_cast<wai_protocol_version_e>(value);
       
   100 	}
       
   101 
       
   102 	return wai_protocol_version_none;
       
   103 }
       
   104 
       
   105 /**
       
   106  * This function returns the Type value.
       
   107  */
       
   108 wai_protocol_type_e wai_protocol_packet_header_c::get_type() const
       
   109 {
       
   110 	const u8_t * const data = get_header_offset(m_type_offset, m_type_size);
       
   111 	if (data != 0)
       
   112 	{
       
   113 		EAP_STATIC_ASSERT(m_type_size == sizeof(*data));
       
   114 
       
   115 		return static_cast<wai_protocol_type_e>(*data);
       
   116 	}
       
   117 
       
   118 	return wai_protocol_type_none;
       
   119 }
       
   120 
       
   121 /**
       
   122  * This function returns the Subtype value.
       
   123  */
       
   124 wai_protocol_subtype_e wai_protocol_packet_header_c::get_subtype() const
       
   125 {
       
   126 	const u8_t * const data = get_header_offset(m_subtype_offset, m_subtype_size);
       
   127 	if (data != 0)
       
   128 	{
       
   129 		EAP_STATIC_ASSERT(m_subtype_size == sizeof(*data));
       
   130 
       
   131 		return static_cast<wai_protocol_subtype_e>(*data);
       
   132 	}
       
   133 
       
   134 	return wai_protocol_subtype_none;
       
   135 }
       
   136 
       
   137 /**
       
   138  * This function returns the Reserved value.
       
   139  */
       
   140 u16_t wai_protocol_packet_header_c::get_reserved() const
       
   141 {
       
   142 	const u8_t * const data = get_header_offset(m_reserved_offset, m_reserved_size);
       
   143 	if (data != 0)
       
   144 	{
       
   145 		u16_t value = eap_read_u16_t_network_order(
       
   146 			data, m_reserved_size);
       
   147 
       
   148 		EAP_STATIC_ASSERT(m_reserved_size == sizeof(value));
       
   149 
       
   150 		return value;
       
   151 	}
       
   152 
       
   153 	return 0xffff;
       
   154 }
       
   155 
       
   156 /**
       
   157  * This function returns the Length value.
       
   158  */
       
   159 u32_t wai_protocol_packet_header_c::get_length() const
       
   160 {
       
   161 	const u8_t * const data = get_header_offset(m_length_offset, m_length_size);
       
   162 	if (data != 0)
       
   163 	{
       
   164 		u16_t value = eap_read_u16_t_network_order(
       
   165 			data, m_length_size);
       
   166 
       
   167 		EAP_STATIC_ASSERT(m_length_size == sizeof(value));
       
   168 
       
   169 		return value;
       
   170 	}
       
   171 
       
   172 	return 0u;
       
   173 }
       
   174 
       
   175 /**
       
   176  * This function returns the Packet sequence number value.
       
   177  */
       
   178 u16_t wai_protocol_packet_header_c::get_packet_sequence_number() const
       
   179 {
       
   180 	const u8_t * const data = get_header_offset(m_packet_sequence_number_offset, m_packet_sequence_number_size);
       
   181 	if (data != 0)
       
   182 	{
       
   183 		u16_t value = eap_read_u16_t_network_order(
       
   184 			data, m_packet_sequence_number_size);
       
   185 
       
   186 		EAP_STATIC_ASSERT(m_packet_sequence_number_size == sizeof(value));
       
   187 
       
   188 		return value;
       
   189 	}
       
   190 
       
   191 	return 0u;
       
   192 }
       
   193 
       
   194 /**
       
   195  * This function returns the Fragment sequence number value.
       
   196  */
       
   197 u8_t wai_protocol_packet_header_c::get_fragment_sequence_number() const
       
   198 {
       
   199 	const u8_t * const data = get_header_offset(m_fragment_sequence_number_offset, m_fragment_sequence_number_size);
       
   200 	if (data != 0)
       
   201 	{
       
   202 		EAP_STATIC_ASSERT(m_fragment_sequence_number_size == sizeof(*data));
       
   203 
       
   204 		return (*data);
       
   205 	}
       
   206 
       
   207 	return 0xff;
       
   208 }
       
   209 
       
   210 /**
       
   211  * This function returns the Flag value.
       
   212  */
       
   213 u8_t wai_protocol_packet_header_c::get_flag() const
       
   214 {
       
   215 	const u8_t * const data = get_header_offset(m_flag_offset, m_flag_size);
       
   216 	if (data != 0)
       
   217 	{
       
   218 		EAP_STATIC_ASSERT(m_flag_size == sizeof(*data));
       
   219 
       
   220 		return (*data);
       
   221 	}
       
   222 
       
   223 	return 0xff;
       
   224 }
       
   225 
       
   226 /**
       
   227  * This function returns the header length of WAI protocol packet.
       
   228  */
       
   229 u32_t wai_protocol_packet_header_c::get_header_length()
       
   230 {
       
   231 	return m_data_offset;
       
   232 }
       
   233 
       
   234 /**
       
   235  * This function returns the data length of WAI protocol packet.
       
   236  */
       
   237 u32_t wai_protocol_packet_header_c::get_data_length() const
       
   238 {
       
   239 	u32_t length = get_length();
       
   240 
       
   241 	if (length >= get_header_length())
       
   242 	{
       
   243 		return (length - get_header_length());
       
   244 	}
       
   245 	else
       
   246 	{
       
   247 		return 0ul;
       
   248 	}
       
   249 }
       
   250 
       
   251 /**
       
   252  * This function returns pointer to the offset of data of WAI protocol packet.
       
   253  * @param offset is the offset of queried data in bytes.
       
   254  * @param contignuous_bytes is the length of queried data in bytes.
       
   255  */
       
   256 u8_t * wai_protocol_packet_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   257 {
       
   258 	u32_t data_length = get_data_length();
       
   259 
       
   260 	if (data_length >= offset+contignuous_bytes)
       
   261 	{
       
   262 		u8_t * const data = get_header_offset(m_data_offset, offset+contignuous_bytes);
       
   263 		if (data != 0)
       
   264 		{
       
   265 			return &data[offset];
       
   266 		}
       
   267 		else
       
   268 		{
       
   269 			return 0;
       
   270 		}
       
   271 	}
       
   272 	else
       
   273 	{
       
   274 		EAP_ASSERT_ALWAYS(data_length >= offset+contignuous_bytes);
       
   275 	}
       
   276 	return 0;
       
   277 }
       
   278 
       
   279 /**
       
   280  * This function returns pointer to the begin of data of WAI protocol packet.
       
   281  * @param contignuous_bytes is the length of queried data in bytes.
       
   282  */
       
   283 u8_t * wai_protocol_packet_header_c::get_data(const u32_t contignuous_bytes) const
       
   284 {
       
   285 	return get_data_offset(0u, contignuous_bytes);
       
   286 }
       
   287 
       
   288 /**
       
   289  * This function checks the header is valid.
       
   290  */
       
   291 eap_status_e wai_protocol_packet_header_c::check_header() const
       
   292 {
       
   293 	if (get_version() != wai_protocol_version_1)
       
   294 	{
       
   295 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   296 	}
       
   297 	else if (get_reserved() != 0)
       
   298 	{
       
   299 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   300 	}
       
   301 	else if (get_length() < get_header_length())
       
   302 	{
       
   303 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   304 	}
       
   305 	else if (get_length() > get_header_buffer_length())
       
   306 	{
       
   307 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   308 	}
       
   309 
       
   310 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   311 }
       
   312 
       
   313 /**
       
   314  * This function sets the Version value.
       
   315  */
       
   316 eap_status_e wai_protocol_packet_header_c::set_version(const wai_protocol_version_e version)
       
   317 {
       
   318 	const u16_t value = static_cast<u16_t>(version);
       
   319 
       
   320 	u8_t * const data = get_header_offset(m_version_offset, m_version_size);
       
   321 	if (data != 0)
       
   322 	{
       
   323 		EAP_STATIC_ASSERT(m_version_size == sizeof(value));
       
   324 
       
   325 		return EAP_STATUS_RETURN(m_am_tools, eap_write_u16_t_network_order(
       
   326 			data,
       
   327 			sizeof(value),
       
   328 			value));
       
   329 	}
       
   330 	else
       
   331 	{
       
   332 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   333 	}
       
   334 }
       
   335 
       
   336 /**
       
   337  * This function sets the Type value.
       
   338  */
       
   339 eap_status_e wai_protocol_packet_header_c::set_type(const wai_protocol_type_e type)
       
   340 {
       
   341 	const u8_t value = static_cast<u8_t>(type);
       
   342 
       
   343 	u8_t * const data = get_header_offset(m_type_offset, m_type_size);
       
   344 	if (data != 0)
       
   345 	{
       
   346 		EAP_STATIC_ASSERT(m_type_size == sizeof(*data));
       
   347 
       
   348 		*data = value;
       
   349 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   350 	}
       
   351 	else
       
   352 	{
       
   353 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   354 	}
       
   355 }
       
   356 
       
   357 /**
       
   358  * This function sets the Subype value.
       
   359  */
       
   360 eap_status_e wai_protocol_packet_header_c::set_subtype(const wai_protocol_subtype_e subtype)
       
   361 {
       
   362 	const u8_t value = static_cast<u8_t>(subtype);
       
   363 
       
   364 	u8_t * const data = get_header_offset(m_subtype_offset, m_subtype_size);
       
   365 	if (data != 0)
       
   366 	{
       
   367 		EAP_STATIC_ASSERT(m_subtype_size == sizeof(*data));
       
   368 
       
   369 		*data = value;
       
   370 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   371 	}
       
   372 	else
       
   373 	{
       
   374 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   375 	}
       
   376 }
       
   377 
       
   378 /**
       
   379  * This function sets the Reserved value.
       
   380  */
       
   381 eap_status_e wai_protocol_packet_header_c::set_reserved(const u16_t reserved)
       
   382 {
       
   383 	u8_t * const data = get_header_offset(m_reserved_offset, m_reserved_size);
       
   384 	if (data != 0)
       
   385 	{
       
   386 		EAP_STATIC_ASSERT(m_version_size == sizeof(reserved));
       
   387 
       
   388 		return EAP_STATUS_RETURN(m_am_tools, eap_write_u16_t_network_order(
       
   389 			data,
       
   390 			sizeof(reserved),
       
   391 			reserved));
       
   392 	}
       
   393 	else
       
   394 	{
       
   395 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   396 	}
       
   397 }
       
   398 
       
   399 /**
       
   400  * This function sets the Length value.
       
   401  */
       
   402 eap_status_e wai_protocol_packet_header_c::set_length(const u32_t length)
       
   403 {
       
   404 	u8_t * const data = get_header_offset(m_length_offset, m_length_size);
       
   405 	if (data != 0
       
   406 		&& length <= 0xffff)
       
   407 	{
       
   408 		const u16_t value = static_cast<u16_t>(length);
       
   409 
       
   410 		EAP_STATIC_ASSERT(m_length_size == sizeof(value));
       
   411 
       
   412 		return EAP_STATUS_RETURN(m_am_tools, eap_write_u16_t_network_order(
       
   413 			data,
       
   414 			sizeof(value),
       
   415 			value));
       
   416 	}
       
   417 	else
       
   418 	{
       
   419 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   420 	}
       
   421 }
       
   422 
       
   423 /**
       
   424  * This function sets the Packet sequence number value.
       
   425  */
       
   426 eap_status_e wai_protocol_packet_header_c::set_packet_sequence_number(const u16_t packet_sequence_number)
       
   427 {
       
   428 	u8_t * const data = get_header_offset(m_packet_sequence_number_offset, m_packet_sequence_number_size);
       
   429 	if (data != 0)
       
   430 	{
       
   431 		EAP_STATIC_ASSERT(m_packet_sequence_number_size == sizeof(packet_sequence_number));
       
   432 
       
   433 		return EAP_STATUS_RETURN(m_am_tools, eap_write_u16_t_network_order(
       
   434 			data,
       
   435 			sizeof(packet_sequence_number),
       
   436 			packet_sequence_number));
       
   437 	}
       
   438 	else
       
   439 	{
       
   440 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   441 	}
       
   442 }
       
   443 
       
   444 /**
       
   445  * This function sets the Fragment sequence number value.
       
   446  */
       
   447 eap_status_e wai_protocol_packet_header_c::set_fragment_sequence_number(const u8_t fragment_sequence_number)
       
   448 {
       
   449 	u8_t * const data = get_header_offset(m_fragment_sequence_number_offset, m_fragment_sequence_number_size);
       
   450 	if (data != 0)
       
   451 	{
       
   452 		EAP_STATIC_ASSERT(m_fragment_sequence_number_size == sizeof(fragment_sequence_number));
       
   453 
       
   454 		*data = fragment_sequence_number;
       
   455 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   456 	}
       
   457 	else
       
   458 	{
       
   459 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   460 	}
       
   461 }
       
   462 
       
   463 /**
       
   464  * This function sets the Subype value.
       
   465  */
       
   466 eap_status_e wai_protocol_packet_header_c::set_flag(const u8_t flag)
       
   467 {
       
   468 	u8_t * const data = get_header_offset(m_flag_offset, m_flag_size);
       
   469 	if (data != 0)
       
   470 	{
       
   471 		EAP_STATIC_ASSERT(m_flag_size == sizeof(flag));
       
   472 
       
   473 		*data = flag;
       
   474 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   475 	}
       
   476 	else
       
   477 	{
       
   478 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   479 	}
       
   480 }
       
   481 
       
   482 /**
       
   483  * This function resets the WAI protocol packet header.
       
   484  */
       
   485 eap_status_e wai_protocol_packet_header_c::reset_header()
       
   486 {
       
   487 	eap_status_e status = set_version(wai_protocol_version_1);
       
   488 	if (status != eap_status_ok)
       
   489 	{
       
   490 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   491 	}
       
   492 
       
   493 	status = set_type(wai_protocol_type_wai);
       
   494 	if (status != eap_status_ok)
       
   495 	{
       
   496 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   497 	}
       
   498 	
       
   499 	status = set_subtype(wai_protocol_subtype_none);
       
   500 	if (status != eap_status_ok)
       
   501 	{
       
   502 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   503 	}
       
   504 	
       
   505 	status = set_reserved(0u);
       
   506 	if (status != eap_status_ok)
       
   507 	{
       
   508 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   509 	}
       
   510 
       
   511 	status = set_length(get_header_length());
       
   512 	if (status != eap_status_ok)
       
   513 	{
       
   514 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   515 	}
       
   516 
       
   517 	status = set_packet_sequence_number(WAI_FIRST_SEQUENCE_NUMBER);
       
   518 	if (status != eap_status_ok)
       
   519 	{
       
   520 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   521 	}
       
   522 
       
   523 	status = set_fragment_sequence_number(WAI_FIRST_FRAGMENT_NUMBER);
       
   524 	if (status != eap_status_ok)
       
   525 	{
       
   526 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   527 	}
       
   528 
       
   529 	status = set_flag(0u);
       
   530 	if (status != eap_status_ok)
       
   531 	{
       
   532 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   533 	}
       
   534 
       
   535 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   536 }
       
   537 
       
   538 //------------------------------------------------------------------------------
       
   539 
       
   540 #endif //#if defined(USE_WAPI_CORE)
       
   541 
       
   542 // End.