epoc32/include/ip6_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
williamr@2
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2
     2
// All rights reserved.
williamr@2
     3
// This component and the accompanying materials are made available
williamr@2
     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
williamr@2
     5
// which accompanies this distribution, and is available
williamr@2
     6
// at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
williamr@2
     7
//
williamr@2
     8
// Initial Contributors:
williamr@2
     9
// Nokia Corporation - initial contribution.
williamr@2
    10
//
williamr@2
    11
// Contributors:
williamr@2
    12
//
williamr@2
    13
// Description:
williamr@2
    14
// ip6_hdr.h - IPv6 header structure
williamr@2
    15
// This module defines the basic classes for accessing the header
williamr@2
    16
// structures within IPv6 packets.
williamr@2
    17
// Defines the IPv6 header structure.
williamr@2
    18
//
williamr@2
    19
williamr@2
    20
williamr@2
    21
williamr@2
    22
/**
williamr@2
    23
 @file ip6_hdr.h
williamr@2
    24
 @ingroup ip_packet_formats
williamr@2
    25
 @publishedAll
williamr@2
    26
 @released
williamr@2
    27
*/
williamr@2
    28
williamr@2
    29
#ifndef __IP6_HDR_H__
williamr@2
    30
#define __IP6_HDR_H__
williamr@2
    31
williamr@2
    32
#include "in_hdr.h"
williamr@2
    33
#include <in_sock.h> // Only for TIp6Addr !
williamr@2
    34
williamr@2
    35
/**
williamr@2
    36
* @defgroup  ip_packet_formats	IPv4 and IPv6 packet formats and constants
williamr@2
    37
*
williamr@2
    38
* These headers define various constants and special <em>mapping or overlay
williamr@2
    39
* classes</em> to be used in accessing and building the IP packets. The
williamr@2
    40
* mapping classes are not intended to be used in declaring
williamr@2
    41
* variables (although some of them do support that use also).
williamr@2
    42
*
williamr@2
    43
* The normal usage is to typecast a binary buffer
williamr@2
    44
* to a mapping class. For example
williamr@2
    45
* assuming a buf contains a raw IPv6 packet:
williamr@2
    46
*
williamr@2
    47
@code
williamr@2
    48
	TBuf8<2000> buf;
williamr@2
    49
williamr@2
    50
	TInet6HeaderIP &ip = *(TInet6HeaderIP *)buf.Ptr();
williamr@2
    51
	if (ip.MinHeaderLength() > buf.Length() ||
williamr@2
    52
		ip.HeaderLength() + ip.PayloadLength() > buf.Length())
williamr@2
    53
		{
williamr@2
    54
		// Oops. packet too short!
williamr@2
    55
		}
williamr@2
    56
	else if (ip.Version() == 6)
williamr@2
    57
		{
williamr@2
    58
		// Packet seems to be IPv6 packet.
williamr@2
    59
		}
williamr@2
    60
@endcode
williamr@2
    61
*
williamr@2
    62
* Within the TCP/IP stack, the packets are stored in RMBufChain objects.
williamr@2
    63
* The data is not in a contiguous memory area and accessing the headers is
williamr@2
    64
* more complicated. For that purpose there is a template class TInet6Packet,
williamr@2
    65
* which can use any <em>mapping class</em> as a template parameter and
williamr@2
    66
* provides functions to access the header within the chain. Assuming the
williamr@2
    67
* a RMBufChain object buf contains the packet, equivalent code would be:
williamr@2
    68
*
williamr@2
    69
@code
williamr@2
    70
	RMBufChain buf;
williamr@2
    71
williamr@2
    72
	TInet6Packet<TInet6HeaderIP> ip(buf);
williamr@2
    73
	if (ip.iHdr == NULL ||
williamr@2
    74
		ip.iHdr->HeaderLength() + ip.iHdr->PayloadLength() > buf.Length())
williamr@2
    75
		{
williamr@2
    76
		// Opps. packet too short!
williamr@2
    77
		}
williamr@2
    78
	else if (ip.iHdr->Version() == 6)
williamr@2
    79
		{
williamr@2
    80
		// Packet seems to be IPv6 packet.
williamr@2
    81
		}
williamr@2
    82
@endcode
williamr@2
    83
* The TInet6Packet template does automatic MinHeaderLength check. The iHdr
williamr@2
    84
* member variable is set only if the mapping succeeds at least for MinHeaderLength
williamr@2
    85
* octets.
williamr@2
    86
*
williamr@2
    87
* All mapping headers must have the following functions defined:
williamr@2
    88
*
williamr@2
    89
* - static function MinHeaderLength returns the minimum header length in octets.
williamr@2
    90
*
williamr@2
    91
* - static function MaxHeaderLength returns the maximum header length in octets (not used much).
williamr@2
    92
*
williamr@2
    93
* - HeaderLength returns the actual length of a header instance in octets.
williamr@2
    94
*	Note that this may return garbage value, if the mapped header is invalid.
williamr@2
    95
*	The return value can be larger than indicated by MinHeaderLength, and
williamr@2
    96
*	as the mapping is only guaranteed up to that size, user must separately
williamr@2
    97
*	verify the accessibility of a full header in such case. 
williamr@2
    98
* @{
williamr@2
    99
* @}
williamr@2
   100
*/
williamr@2
   101
williamr@2
   102
/**
williamr@2
   103
* @addtogroup  ip_packet_formats
williamr@2
   104
* @{
williamr@2
   105
*/
williamr@2
   106
williamr@2
   107
/**
williamr@2
   108
* The IPv6 minimum value for Maximum Transmission Unit (MTU) as defined in RFC 2460. 
williamr@2
   109
* 
williamr@2
   110
* @since v7.0
williamr@2
   111
*/
williamr@2
   112
const TInt KInet6MinMtu = 1280;
williamr@2
   113
williamr@2
   114
//
williamr@2
   115
// TInet6HeaderIP
williamr@2
   116
// **************
williamr@2
   117
//	Methods of manipulating IPv6 IP header.
williamr@2
   118
//
williamr@2
   119
//	This implementation assumes TUint8 is exactly 8 bits (and not
williamr@2
   120
//	9 or more)
williamr@2
   121
class TInet6HeaderIP
williamr@2
   122
/**
williamr@2
   123
* Encapsulates an IPv6 IP header. 
williamr@2
   124
* @verbatim
williamr@2
   125
 *************************
williamr@2
   126
 Extract from the RFC-2460
williamr@2
   127
 *************************
williamr@2
   128
williamr@2
   129
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
williamr@2
   130
   |Version| Traffic Class |           Flow Label                  |
williamr@2
   131
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
williamr@2
   132
   |         Payload Length        |  Next Header  |   Hop Limit   |
williamr@2
   133
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
williamr@2
   134
   |                                                               |
williamr@2
   135
   +                                                               +
williamr@2
   136
   |                                                               |
williamr@2
   137
   +                         Source Address                        +
williamr@2
   138
   |                                                               |
williamr@2
   139
   +                                                               +
williamr@2
   140
   |                                                               |
williamr@2
   141
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
williamr@2
   142
   |                                                               |
williamr@2
   143
   +                                                               +
williamr@2
   144
   |                                                               |
williamr@2
   145
   +                      Destination Address                      +
williamr@2
   146
   |                                                               |
williamr@2
   147
   +                                                               +
williamr@2
   148
   |                                                               |
williamr@2
   149
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
williamr@2
   150
williamr@2
   151
   Version              4-bit Internet Protocol version number = 6.
williamr@2
   152
williamr@2
   153
   Traffic Class        8-bit traffic class field.  See section 7.
williamr@2
   154
williamr@2
   155
   Flow Label           20-bit flow label.  See section 6.
williamr@2
   156
williamr@2
   157
   Payload Length       16-bit unsigned integer.  Length of the IPv6
williamr@2
   158
                        payload, i.e., the rest of the packet following
williamr@2
   159
                        this IPv6 header, in octets.  (Note that any
williamr@2
   160
                        extension headers [section 4] present are
williamr@2
   161
                        considered part of the payload, i.e., included
williamr@2
   162
                        in the length count.)
williamr@2
   163
williamr@2
   164
   Next Header          8-bit selector.  Identifies the type of header
williamr@2
   165
                        immediately following the IPv6 header.  Uses the
williamr@2
   166
                        same values as the IPv4 Protocol field [RFC-1700
williamr@2
   167
                        et seq.].
williamr@2
   168
williamr@2
   169
   Hop Limit            8-bit unsigned integer.  Decremented by 1 by
williamr@2
   170
                        each node that forwards the packet. The packet
williamr@2
   171
                        is discarded if Hop Limit is decremented to
williamr@2
   172
                        zero.
williamr@2
   173
williamr@2
   174
   Source Address       128-bit address of the originator of the packet.
williamr@2
   175
williamr@2
   176
   Destination Address  128-bit address of the intended recipient of the
williamr@2
   177
                        packet (possibly not the ultimate recipient, if
williamr@2
   178
                        a Routing header is present)
williamr@2
   179
@endverbatim
williamr@2
   180
* @publishedAll
williamr@2
   181
* @released
williamr@2
   182
* @since v7.0
williamr@2
   183
*/
williamr@2
   184
	{
williamr@2
   185
public:
williamr@2
   186
	enum TOffsets
williamr@2
   187
		{
williamr@2
   188
		O_PayloadLength = 4,
williamr@2
   189
		O_NextHeader = 6,
williamr@2
   190
		O_HopLimit = 7,
williamr@2
   191
		O_SrcAddr = 8,
williamr@2
   192
		O_DstAddr = 24
williamr@2
   193
		};
williamr@2
   194
williamr@2
   195
	// Basic functions, common to all header classes
williamr@2
   196
	// *********************************************
williamr@2
   197
williamr@2
   198
	/**
williamr@2
   199
	* Gets the header length.	
williamr@2
   200
	* 
williamr@2
   201
	* Note that the header length is fixed.
williamr@2
   202
	* 
williamr@2
   203
	* @return	Header length.
williamr@2
   204
	*/
williamr@2
   205
	inline TInt HeaderLength() const {return 40;}
williamr@2
   206
	/**
williamr@2
   207
	* Gets the minimum header length.
williamr@2
   208
	* 
williamr@2
   209
	* Note that the header length is fixed.
williamr@2
   210
	* 
williamr@2
   211
	* @return	Minimum header length
williamr@2
   212
	*/
williamr@2
   213
	inline static TInt MinHeaderLength() {return 40; }
williamr@2
   214
	/**
williamr@2
   215
	* Gets the maximum header length.
williamr@2
   216
	* 
williamr@2
   217
	* Note that the header length is fixed.
williamr@2
   218
	* 
williamr@2
   219
	* @return	Maximum header length
williamr@2
   220
	*/
williamr@2
   221
	inline static TInt MaxHeaderLength() {return 40; }
williamr@2
   222
	/**
williamr@2
   223
	* Gets a pointer to the byte following the header.
williamr@2
   224
	* 
williamr@2
   225
	* @return	Pointer to the byte following the header
williamr@2
   226
	*/
williamr@2
   227
	inline TUint8 *EndPtr() {return i + HeaderLength();}
williamr@2
   228
williamr@2
   229
	// IPv6 specific methods, get IP header field values from the packet
williamr@2
   230
	// *****************************************************************
williamr@2
   231
williamr@2
   232
	inline TInt Version() const
williamr@2
   233
		/**
williamr@2
   234
		* Gets the IP version from the header.
williamr@2
   235
		*
williamr@2
   236
		* @return	IP version
williamr@2
   237
		*/
williamr@2
   238
		{
williamr@2
   239
		return (i[0] >> 4) & 0xf;
williamr@2
   240
		}
williamr@2
   241
	inline TInt TrafficClass() const
williamr@2
   242
		/**
williamr@2
   243
		* Gets the traffic class from the header.
williamr@2
   244
		*
williamr@2
   245
		* @return	Traffic class
williamr@2
   246
		*/
williamr@2
   247
		{
williamr@2
   248
		return ((i[0] << 4) & 0xf0) | ((i[1] >> 4) & 0x0f);
williamr@2
   249
		}
williamr@2
   250
	inline TInt FlowLabel() const
williamr@2
   251
		/**
williamr@2
   252
		* Gets the flow label from the header.
williamr@2
   253
		* 
williamr@2
   254
		* @return	Flow label (host byte order)
williamr@2
   255
		*/
williamr@2
   256
		{
williamr@2
   257
		return ((i[1] << 16) | (i[2] << 8) | i[3]) & 0xfffff;
williamr@2
   258
		// Could also use any deterministic permutation of the 20 bits,
williamr@2
   259
		// if Flow label can be treated as opaque 20 bits, and not as
williamr@2
   260
		// integer where host/net byte order comes into play --msa
williamr@2
   261
		}
williamr@2
   262
	inline TInt PayloadLength() const
williamr@2
   263
		/**
williamr@2
   264
		* Gets the payload length from the header.
williamr@2
   265
		*
williamr@2
   266
		* @return	Payload length
williamr@2
   267
		*/
williamr@2
   268
		{
williamr@2
   269
		return (i[4] << 8) | i[5];
williamr@2
   270
		}
williamr@2
   271
	inline TInt NextHeader() const
williamr@2
   272
		/**
williamr@2
   273
		* Gets the next header selector from the header.
williamr@2
   274
		*
williamr@2
   275
		* @return	Next header selector (0..255)
williamr@2
   276
		*/
williamr@2
   277
		{
williamr@2
   278
		return i[6];
williamr@2
   279
		}
williamr@2
   280
	inline TInt HopLimit() const
williamr@2
   281
		{
williamr@2
   282
		/**
williamr@2
   283
		* Gets the hop limit from the header.
williamr@2
   284
		*
williamr@2
   285
		* @return	Hop limit (0..255)
williamr@2
   286
		*/
williamr@2
   287
		return i[7];
williamr@2
   288
		}
williamr@2
   289
	//
williamr@2
   290
	// The following return a modifiable reference, so
williamr@2
   291
	// they can be used both for access and build.
williamr@2
   292
	//
williamr@2
   293
	inline TIp6Addr &SrcAddr() const
williamr@2
   294
		/**
williamr@2
   295
		* Gets the source address from the header.
williamr@2
   296
		*
williamr@2
   297
		* @return	Source address
williamr@2
   298
		*/
williamr@2
   299
		{
williamr@2
   300
		return (TIp6Addr &)i[8];
williamr@2
   301
		}
williamr@2
   302
	inline TIp6Addr &DstAddr() const
williamr@2
   303
		/**
williamr@2
   304
		* Gets the destination address from the header.
williamr@2
   305
		*
williamr@2
   306
		* @return	Destination address
williamr@2
   307
		*/
williamr@2
   308
		{
williamr@2
   309
		return (TIp6Addr &)i[24];
williamr@2
   310
		}
williamr@2
   311
williamr@2
   312
	inline TBool EcnIsCongestion()
williamr@2
   313
		/**
williamr@2
   314
		* Gets ECN congestion status.
williamr@2
   315
		*
williamr@2
   316
		* see RFC-3168 for details.
williamr@2
   317
		*
williamr@2
   318
		* @return	True, if CE bit is set on an ECN capable packet.
williamr@2
   319
		*/
williamr@2
   320
		{
williamr@2
   321
		return ((TrafficClass() & 3) == 3);
williamr@2
   322
		}
williamr@2
   323
williamr@2
   324
williamr@2
   325
	// IPv6 specific methods, set IP header field values into the packet
williamr@2
   326
	// *****************************************************************
williamr@2
   327
williamr@2
   328
	inline void Init()
williamr@2
   329
		/**
williamr@2
   330
		* Initialises the header to basic initial values.
williamr@2
   331
		*
williamr@2
   332
		* @li Version = 6
williamr@2
   333
		* @li Traffic Class = 0
williamr@2
   334
		* @li Flow Label = 0
williamr@2
   335
		* @li Payload Length = 0
williamr@2
   336
		* @li Next Header = 0
williamr@2
   337
		* @li Hop Limit = 0
williamr@2
   338
		*
williamr@2
   339
		* The Source and Destination address fields are not touched.
williamr@2
   340
		*/
williamr@2
   341
		{
williamr@2
   342
		static const union { TUint8 a[4]; TUint32 b;} x = { {6 << 4, 0, 0, 0} };
williamr@2
   343
		(TUint32 &)i[0] = x.b;
williamr@2
   344
		(TUint32 &)i[4] = 0;
williamr@2
   345
		}
williamr@2
   346
williamr@2
   347
williamr@2
   348
	inline void SetVersion(TInt aVersion)
williamr@2
   349
		/**
williamr@2
   350
		* Sets the IP version in the header.
williamr@2
   351
		*
williamr@2
   352
		* @param aVersion	IP version (0..15, = 6 for IPv6)
williamr@2
   353
		*/
williamr@2
   354
		{
williamr@2
   355
		i[0] = (TUint8)((i[0] & 0x0f) | ((aVersion << 4) & 0xf0));
williamr@2
   356
		}
williamr@2
   357
	inline void SetTrafficClass(TInt aClass)
williamr@2
   358
		/**
williamr@2
   359
		* Sets the traffic class in the header.
williamr@2
   360
		* 
williamr@2
   361
		* @param aClass	Traffic class (0..255)
williamr@2
   362
		*/
williamr@2
   363
		{
williamr@2
   364
		i[0] = (TUint8)((i[0] & 0xf0) | (aClass >> 4) & 0x0f);
williamr@2
   365
		i[1] = (TUint8)((i[1] & 0x0f) | (aClass << 4) & 0xf0);
williamr@2
   366
		}
williamr@2
   367
	inline void SetFlowLabel(TInt aFlow)
williamr@2
   368
		/**
williamr@2
   369
		* Sets the flow label in the header.
williamr@2
   370
		* 
williamr@2
   371
		* @param aFlow	Flow label (20 bit integer in host order)
williamr@2
   372
		*/
williamr@2
   373
		{
williamr@2
   374
		i[1] = (TUint8)((i[1] & 0xf0) | ((aFlow >> 16) & 0x0f));
williamr@2
   375
		i[2] = (TUint8)(aFlow >> 8);
williamr@2
   376
		i[3] = (TUint8)aFlow;
williamr@2
   377
		}
williamr@2
   378
williamr@2
   379
	inline void SetPayloadLength(TInt aLength)
williamr@2
   380
		/**
williamr@2
   381
		* Sets the payload length in the header.
williamr@2
   382
		* 
williamr@2
   383
		* @param aLength	Payload length
williamr@2
   384
		*/
williamr@2
   385
		{
williamr@2
   386
		i[4] = (TUint8)(aLength >> 8);
williamr@2
   387
		i[5] = (TUint8)aLength;
williamr@2
   388
		}
williamr@2
   389
williamr@2
   390
	inline void SetNextHeader(TInt aNextHeader)
williamr@2
   391
		/**
williamr@2
   392
		* Sets the next header selector from the header.
williamr@2
   393
		*
williamr@2
   394
		* @param aNextHeader	Next header selector (0..255)
williamr@2
   395
		*/
williamr@2
   396
		{
williamr@2
   397
		i[6] = (TUint8)aNextHeader;
williamr@2
   398
		}
williamr@2
   399
	inline void SetHopLimit(TInt aLimit)
williamr@2
   400
		/**
williamr@2
   401
		* Sets the hop limit in the header.
williamr@2
   402
		*
williamr@2
   403
		* @param aLimit	Hop limit (0..255)
williamr@2
   404
		*/
williamr@2
   405
		{
williamr@2
   406
		i[7] = (TUint8)aLimit;
williamr@2
   407
		}
williamr@2
   408
	inline void SetSrcAddr(const TIp6Addr &anAddr)
williamr@2
   409
		/**
williamr@2
   410
		* Sets the source address in the header.
williamr@2
   411
		* 
williamr@2
   412
		* @param anAddr	Source address
williamr@2
   413
		*/
williamr@2
   414
		{
williamr@2
   415
		(TIp6Addr &)i[8] = anAddr;
williamr@2
   416
		}
williamr@2
   417
	inline void SetDstAddr(const TIp6Addr &anAddr)
williamr@2
   418
		/**
williamr@2
   419
		* Sets the destination address in the header.
williamr@2
   420
		* 
williamr@2
   421
		* @param anAddr	Destination address
williamr@2
   422
		*/
williamr@2
   423
		{
williamr@2
   424
		(TIp6Addr &)i[24] = anAddr;
williamr@2
   425
		}
williamr@2
   426
							
williamr@2
   427
private:
williamr@2
   428
	union
williamr@2
   429
		{
williamr@2
   430
		TUint8 i[40];
williamr@2
   431
		TUint32 iAlign;	// A dummy member to force the 4 byte alignment
williamr@2
   432
		};
williamr@2
   433
	};
williamr@2
   434
williamr@2
   435
/** @} */
williamr@2
   436
#endif