epoc32/include/ip4_hdr.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // ip4_hdr.h - IPv4 header structure
    15 // Defines the basic classes for accessing the header
    16 // structures within IPv4 packets.
    17 //
    18 
    19 
    20 
    21 /**
    22  @file ip4_hdr.h
    23  @ingroup ip_packet_formats
    24  @publishedAll
    25  @released
    26 */
    27 
    28 #ifndef __IP4_HDR_H__
    29 #define __IP4_HDR_H__
    30 
    31 #include "in_hdr.h"
    32 #include "es_sock.h"		// for ByteOrder only!!!
    33 
    34 /**
    35 * @addtogroup ip_packet_formats
    36 * @{
    37 */
    38 
    39 /**
    40 * @name IP v4 constants 
    41 * @since v7.0
    42 * @publishedAll
    43 * @released
    44 */
    45 ///@{
    46 const TUint8 KInet4IP_DF = 0x40;	///< Don't Fragment flag
    47 const TUint8 KInet4IP_MF = 0x20;	///< More Fragments flag
    48 const TInt KInetMinMtu = 68;		///< Minimum MTU as defined in RFC-791 
    49 ///@}
    50 
    51 class TInet6HeaderIP4
    52 /**
    53 *	Encapsulates an IPv4 IP header. 
    54 * 
    55 @verbatim
    56   ************************
    57   Extract from the RFC-791
    58   ************************
    59   0                   1                   2                   3
    60   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    61   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    62   |Version|  IHL  |Type of Service|          Total Length         |
    63   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    64   |         Identification        |Flags|      Fragment Offset    |
    65   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    66   |  Time to Live |    Protocol   |         Header Checksum       |
    67   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    68   |                       Source Address                          |
    69   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    70   |                    Destination Address                        |
    71   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    72   |                    Options                    |    Padding    |
    73   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    74   
    75   Version:  4 bits = 4
    76   IHL:  4 bits
    77 
    78     Internet Header Length is the length of the internet header in 32
    79     bit words, and thus points to the beginning of the data.  Note that
    80     the minimum value for a correct header is 5
    81 
    82   Type of Service:  8 bits
    83       Bits 0-2:  Precedence.
    84       Bit    3:  0 = Normal Delay,      1 = Low Delay.
    85       Bits   4:  0 = Normal Throughput, 1 = High Throughput.
    86       Bits   5:  0 = Normal Relibility, 1 = High Relibility.
    87       Bit  6-7:  Reserved for Future Use.
    88 
    89 	  Precedence
    90 
    91           111 - Network Control
    92           110 - Internetwork Control
    93           101 - CRITIC/ECP
    94           100 - Flash Override
    95           011 - Flash
    96           010 - Immediate
    97           001 - Priority
    98           000 - Routine
    99   Total Length:  16 bits
   100     Total Length is the length of the datagram, measured in octets,
   101     including internet header and data.
   102 
   103   Identification:  16 bits
   104     An identifying value assigned by the sender to aid in assembling the
   105     fragments of a datagram.
   106 
   107   Flags:  3 bits
   108     Various Control Flags.
   109 
   110       Bit 0: reserved, must be zero
   111       Bit 1: (DF) 0 = May Fragment,  1 = Don't Fragment.
   112       Bit 2: (MF) 0 = Last Fragment, 1 = More Fragments.
   113 
   114   Fragment Offset:  13 bits
   115 
   116     This field indicates where in the datagram this fragment belongs.
   117     The fragment offset is measured in units of 8 octets (64 bits).  The
   118     first fragment has offset zero.
   119 
   120   Time to Live:  8 bits
   121   Protocol:  8 bits
   122   Header Checksum:  16 bits
   123   Source Address:  32 bits
   124   Destination Address:  32 bits
   125 @endverbatim
   126 
   127 @publishedAll
   128 @released
   129 @since v7.0
   130 */
   131 	{
   132 public:
   133 	//
   134 	// Basic
   135 	//
   136 	/**
   137 	* Gets the minimum header length.	
   138 	* @return Minimum header length (= 20) 
   139 	* @since v7.0
   140 	*/
   141 	inline static TInt MinHeaderLength() {return 4*5; }
   142 	/**
   143 	* Gets the maximum header length.
   144 	* @return Maximum header length (= 60)
   145 	* @since v7.0
   146 	*/
   147 	inline static TInt MaxHeaderLength() {return 4*15; }
   148 	/**
   149 	* Gets a pointer to the byte following the header.
   150 	* @return Pointer to the byte following the header
   151 	* @since v7.0
   152 	*/
   153 	inline TUint8 *EndPtr() {return i + HeaderLength();}
   154 
   155 	enum TOffsets
   156 		{
   157 		O_TotalLength = 2,
   158 		O_FragmentOffset = 6,
   159 		O_TTL = 8,
   160 		O_Protocol = 9
   161 		};
   162 	inline TInt Version() const
   163 		/**
   164 		* Gets the IP version from the header.
   165 		* @return IP version (should be 4 for IPv4)
   166 		*/ 
   167 		{
   168 		return (i[0] >> 4) & 0xf;
   169 		}
   170 	inline TInt HeaderLength() const
   171 		/**
   172 		* Gets the header length.
   173 		* @return Header length in bytes (based on IHL field)
   174 		*/
   175 		{
   176 		return (i[0] & 0x0f) * 4;	// Note: Returns bytes length!
   177 		}
   178 	inline TInt TOS() const
   179 		/**
   180 		* Gets the TOS from the header.
   181 		* @return TOS
   182 		*/
   183 		{
   184 		return i[1];
   185 		}
   186 	inline TBool EcnIsCongestion()
   187 		/**
   188 		* Gets ECN congestion status.
   189 		*
   190 		* see RFC-3168 for details.
   191 		*
   192 		* @return	True, if CE bit is set on an ECN capable packet.
   193 		*/
   194 		{
   195 		return ((TOS() & 3) == 3);
   196 		}
   197 	inline TInt TotalLength() const
   198 		/**
   199 		* Gets the Total Length from the header.
   200 		* @return Total Length (includes IP header and payload)
   201 		*/
   202 		{
   203 		return (i[2] << 8) | i[3];
   204 		}
   205 	inline TInt Identification() const
   206 		/**
   207 		* Gets the Identification from the header.
   208 		* @return Identification [0..65535]
   209 		*/
   210 		{
   211 		return (i[4] << 8) | i[5];
   212 		}
   213 	inline TInt Flags() const
   214 		/**
   215 		* Gets the Flags from the header.
   216 		* @note returns flags byte as is, may include bits of fragment offset!
   217 		* @return Flags
   218 		*/
   219 		{
   220 		return i[6];
   221 		}
   222 	inline TInt DF() const
   223 		/**
   224 		* Gets the DF flag from the header.
   225 		* @return DF flag (= KInet4IP_DF, if set and zero otherwise)
   226 		*/
   227 		{
   228 		return i[6] & KInet4IP_DF;
   229 		}
   230 	inline TInt MF() const
   231 		/**
   232 		* Gets the MF flag from the header.
   233 		* @return MF flag (= KInet4IP_MF, if set and zero otherwise)
   234 		*/
   235 		{
   236 		return i[6] & KInet4IP_MF;
   237 		}
   238 	inline TInt FragmentOffset() const
   239 		/**
   240 		* Gets the Fragment Offset from the header.
   241 		* @return Fragment Offset (raw 8 octet units, not a bytes offset!)
   242 		*/
   243 		{
   244 		return ((i[6] & 0x1f) << 8) | i[7];
   245 		}
   246 	inline TInt Ttl() const
   247 		/**
   248 		* Gets the Time to Live from the header.
   249 		* @return Time to Live [0..255]
   250 		*/
   251 		{
   252 		return i[8];
   253 		}
   254 	inline TInt Protocol() const
   255 		/**
   256 		* Gets the Protocol from the header.
   257 		* @return Protocol [0..255]
   258 		*/
   259 		{
   260 		return i[9];
   261 		}
   262 	inline TInt Checksum() const
   263 		/**
   264 		* Gets the Header Checksum from the header.
   265 		* @return Header Checksum (in NETWORK byte order)
   266 		*/
   267 		{
   268 		// Checksum is used in network byte order
   269 		return *((TUint16 *)&i[10]);
   270 		}
   271 	inline TUint32 SrcAddr() const
   272 		/**
   273 		* Gets the source address from the header.
   274 		* @return Source address (in host byte order)
   275 		*/
   276 		{
   277 		return (i[12] << 24) | (i[13] << 16) | (i[14] << 8) | i[15];
   278 		}
   279 	inline TUint32 DstAddr() const
   280 		/**
   281 		* Gets the destination address from the header.
   282 		* @return Destination address (in host byte order)
   283 		*/
   284 		{
   285 		return (i[16] << 24) | (i[17] << 16) | (i[18] << 8) | i[19];
   286 		}
   287 	inline TUint32 &SrcAddrRef() const
   288 		/**
   289 		* Gets a raw reference to the source address in network byte order.
   290 		* @return Raw reference to the source address
   291 		*/
   292 		{
   293 		return (TUint32 &)i[12];
   294 		}
   295 	inline TUint32 &DstAddrRef() const
   296 		/**
   297 		* Gets a raw reference to the destination address in network byte order.
   298 		* @return Raw reference to the destination address
   299 		*/
   300 		{
   301 		return (TUint32 &)i[16];
   302 		}
   303 	inline TPtrC8 Options() const
   304 		/**
   305 		* Gets the Options from the header (const overload).
   306 		* @return Options
   307 		*
   308 		* @note
   309 		*	This relies on correct value of IHL! Must not be used with
   310 		*	corrupt headers (will panic if IHL < 5!).
   311 		*/
   312 		{
   313 		// *NOTE* This includes the padding bytes, or can be empty!
   314 		return TPtrC8((TUint8 *)&i[20], HeaderLength() - 20);
   315 		}
   316 	inline TPtr8 Options()
   317 		/**
   318 		* Gets the Options from the header.
   319 		* @return Options
   320 		*
   321 		* @note
   322 		*	This relies on correct value of IHL! Must not be used with
   323 		*	corrupt headers (will panic if IHL < 5!).
   324 		*/
   325 		{
   326 		// It is yet unclear what will be the best way to build
   327 		// the options into the header. For the time being this
   328 		// method returns a modifiable Ptr8 to the the available
   329 		// option space. For this to work, the application must
   330 		// have used the SetHeaderLength() to fix the current
   331 		// available length.
   332 		return TPtr8((TUint8 *)&i[20], HeaderLength() - 20);
   333 		}
   334 
   335 	inline void Init(TInt aTOS = 0)
   336 		/**
   337 		* Initialises the IPv4 header to basic initial values.
   338 		*
   339 		* @li	Version = 4
   340 		* @li	IHL = 5
   341 		* @li	Total Length = 20
   342 		* @li	Identification = 0
   343 		* @li	Fragment Offset = 0
   344 		* @li	Flags = 0
   345 		* @li	TTL = 0
   346 		* @li	Checksum = 0
   347 		* @li	TOS = aTOS (optional parameter, default = 0)
   348 		*
   349 		* However, address fields are not touched, because
   350 		* they are most often set separately in any case.
   351 		*
   352 		* @param	aTOS	initial value for TOS (= 0)
   353 		*/
   354 		{
   355 		i[0] = 0x45;			// Version=4, IHL=5 (= 20 bytes)
   356 		i[1] = (TUint8)aTOS;	// TOS
   357 		i[2] = 0;
   358 		i[3] = 20;				// Total length = 20
   359 		*((TInt32 *)&i[4]) = 0;	// Identification = 0, flags=0, Fragment offset = 0;
   360 		*((TInt32 *)&i[8]) = 0;	// TTL=0,Protocol=0,Checksum=0
   361 		}
   362 	//
   363 	// Build, set IP header field values into the packet
   364 	//
   365 
   366 	inline void SetVersion(TInt aVersion)
   367 		/**
   368 		* Sets the IP version in the header.
   369 		* @param	aVersion the value to be set [0..15]
   370 		*/
   371 		{
   372 		i[0] = (TUint8)((i[0] & 0x0f) | ((aVersion << 4) & 0xf0));
   373 		}
   374 	inline void SetHeaderLength(TInt aLength)
   375 		/**
   376 		* Sets the header length (IHL).
   377 		*
   378 		* @param aLength
   379 		*	the length of the IPv4 header in BYTES. The
   380 		*	IHL is computed from this, without any sanity
   381 		*	checks. The valid range is [20..60].
   382 		*/
   383 		{
   384 		i[0] =  (TUint8)((i[0] & 0xf0) | ((aLength >> 2) & 0x0f));
   385 		}
   386 	inline void SetTOS(TInt aTos)
   387 		/**
   388 		* Sets the TOS in the header.
   389 		* @param	aTos	The TOS value to set [0..255]
   390 		*/
   391 		{
   392 		i[1] = (TUint8)aTos;
   393 		}
   394 	inline void SetTotalLength(TInt aLength)
   395 		/**
   396 		* Sets the Total Length in the header.
   397 		*
   398 		* @param	aLength the length of combined header and
   399 		*			payload in bytes (no sanity checks, but the
   400 		*			value should be in range [20..65535]). Only
   401 		*			16 least significant bits used.
   402 		*/
   403 		{
   404 		i[3] = (TUint8)aLength;
   405 		i[2] = (TUint8)(aLength >> 8);
   406 		}
   407 	inline void SetIdentification(TInt aId)
   408 		/**
   409 		* Sets the Identification in the header.
   410 		*
   411 		* @param	aId	the value to be set (only 16 least significant
   412 		*			bits are used, rest is ignored).
   413 		*/
   414 		{
   415 		i[5] = (TUint8)aId;
   416 		i[4] = (TUint8)(aId >> 8);
   417 		}
   418 
   419 	inline void SetFlags(TUint8 aFlags)
   420 		/**
   421 		* Sets the Flags in the header.
   422 		*
   423 		* Flags are assumed to be in the three most significant bits
   424 		* of aFlags, in their proper positions.
   425 		* (No individual settings provided, if you need to set a flag
   426 		* without affecting others, use Flags() to get old values,
   427 		* update and store the result with SetFlags()).
   428 		*
   429 		* @param	aFlags	contains the new flags
   430 		*/
   431 		{
   432 		i[6] = (TUint8)((i[6] & 0x1f) | (aFlags & 0xe0));
   433 		}
   434 	inline void SetFragmentOffset(TUint16 aOffset)
   435 		/**
   436 		* Sets the Fragment Offset in the header.
   437 		* @param aOffset Fragment Offset (8 octet units, not in bytes)
   438 		*/
   439 		{
   440 		i[6] = (TUint8)((i[6] & 0xe0) | ((aOffset >> 8) & 0x1f));
   441 		i[7] = (TUint8)aOffset;
   442 		}
   443 	inline void SetTtl(TInt aTTL)
   444 		/**
   445 		* Sets the Time to Live in the header.
   446 		* @param aTTL Time to Live [0..255]
   447 		*/
   448 		{
   449 		i[8] = (TUint8)aTTL;
   450 		}
   451 	inline void SetProtocol(TInt aProtocol)
   452 		/**
   453 		* Sets the Protocol in the header.
   454 		* @param aProtocol Protocol [0..255]
   455 		*/
   456 		{
   457 		i[9] = (TUint8)aProtocol;
   458 		}
   459 	inline void SetChecksum(TInt aSum)
   460 		/**
   461 		* Sets the Header Checksum in the header.
   462 		* @param aSum Header Checksum [0..65535]
   463 		*	(16 least significant bits stored
   464 		*	as is (assumed to be in NETWORK byte order).					
   465 		*/
   466 		{
   467 		// Checksum is used in network byte order
   468 		*((TUint16 *)&i[10]) = (TUint16)aSum;
   469 		}
   470 
   471 	inline void SetSrcAddr(TUint32 aAddr)
   472 		/**
   473 		* Sets the source address in the header.
   474 		* @param aAddr Source address (IPv4, 32 bit integer in host byte order)
   475 		*/
   476 		{
   477 		i[15] = (TUint8)aAddr;
   478 		i[14] = (TUint8)(aAddr >> 8);
   479 		i[13] = (TUint8)(aAddr >> 16);
   480 		i[12] = (TUint8)(aAddr >> 24);
   481 		}
   482 
   483 	inline void SetDstAddr(TUint32 aAddr)
   484 		/**
   485 		* Sets the destination address in the header.
   486 		* @param aAddr Destination IPv4 address (32 bit integer) in host byte order
   487 		*/
   488 		{
   489 		i[19] = (TUint8)aAddr;
   490 		i[18] = (TUint8)(aAddr >> 8);
   491 		i[17] = (TUint8)(aAddr >> 16);
   492 		i[16] = (TUint8)(aAddr >> 24);
   493 		}
   494 
   495 	//
   496 	// The old IPv4 stack leaves IP header in packet when passing it upwards,
   497 	// but this header is swapped into host order. As upper layers really don't
   498 	// need this stuff much, only few "compatibility" methods is defined here
   499 	//
   500 
   501 	inline TInt HostHeaderLength() const
   502 		{
   503 		/**
   504 		* Gets the Header Length from a header that is in Host byte order.
   505 		* @return Header Length
   506 		*
   507 		* @deprecated There is no reason to use swapped headers
   508 		*/
   509 		return (i[3] & 0x0f) * 4;
   510 		}
   511 	inline TInt HostProtocol() const
   512 		/**
   513 		* Gets the Protocol from a header that is in Host byte order.
   514 		* @return Protocol
   515 		* @deprecated There is no reason to use swapped headers
   516 		*/
   517 		{
   518 		return i[10];
   519 		}
   520 
   521 	inline void Swap()
   522 		/**
   523 		* Swaps the byte order in the header.
   524 		* @deprecated There is no reason to use swapped headers
   525 		*/
   526 		{
   527 		*(TUint32 *)(&i[0]) = ByteOrder::Swap32(*(TUint32 *)(&i[0]));
   528 		*(TUint32 *)(&i[4]) = ByteOrder::Swap32(*(TUint32 *)(&i[4]));
   529 		*(TUint32 *)(&i[8]) = ByteOrder::Swap32(*(TUint32 *)(&i[8]));
   530 		*(TUint32 *)(&i[12]) = ByteOrder::Swap32(*(TUint32 *)(&i[12]));
   531 		*(TUint32 *)(&i[16]) = ByteOrder::Swap32(*(TUint32 *)(&i[16]));
   532 		}
   533 				
   534 private:
   535 	union
   536 		{
   537 		TUint8 i[4*15];	///< This allocates maximum length (60 bytes)
   538 		TUint32 iAlign;	///< A dummy member to force the 4 byte alignment
   539 		};
   540 	};
   541 
   542 /**
   543 * @name IP v4 Option constants
   544 * @since v7.0
   545 * @publishedAll
   546 * @released
   547 */
   548 ///@{
   549 const TUint8 KInet4Option_End			= 0x00;
   550 const TUint8 KInet4Option_Nop			= 0x01;
   551 
   552 const TUint8 KInet4OptionFlag_Copy		= 0x80;
   553 ///@}
   554 
   555 //
   556 //	ICMP v4 constants
   557 //	=================
   558 //
   559 /**
   560 * @name ICMP v4 constants 
   561 * @since v7.0
   562 * @publishedAll
   563 * @released
   564 */
   565 ///@{
   566 /** Echo Reply. See TInet6HeaderICMP_Echo (IPv4 and IPv6 use the same format). */
   567 const TUint8 KInet4ICMP_EchoReply		=  0;
   568 const TUint8 KInet4ICMP_Unreachable		=  3;
   569 const TUint8 KInet4ICMP_SourceQuench	=  4;
   570 const TUint8 KInet4ICMP_Redirect		=  5;
   571 /** Echo Request. See TInet6HeaderICMP_Echo (IPv4 and IPv6 use the same format). */
   572 const TUint8 KInet4ICMP_Echo			=  8;
   573 const TUint8 KInet4ICMP_TimeExceeded	= 11;
   574 const TUint8 KInet4ICMP_ParameterProblem = 12;
   575 const TUint8 KInet4ICMP_TimeStamp		= 13;
   576 const TUint8 KInet4ICMP_TimeStampReply	= 14;
   577 ///@}
   578 
   579 ///@}
   580 #endif