tcpiputils/dhcp/src/DHCPIP6Msg.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     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 "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Implements the DHCP Message format
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file DHCPIP6Msg.cpp
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #include "DHCPIP6IA.h"
       
    24 #include "DHCPAuthentication.h"
       
    25 #include "DHCP_Std.h"
       
    26 
       
    27 using namespace DHCPv6;
       
    28 
       
    29 COptionNode::~COptionNode()
       
    30 	{
       
    31 	delete iNext;
       
    32 	}
       
    33 
       
    34 COptionNode* COptionNode::NewL()
       
    35 	{
       
    36 	return new(ELeave)COptionNode(NULL);
       
    37 	}
       
    38 
       
    39 COptionNode* CDHCPOptionAny::NewL()
       
    40 	{
       
    41 	return new(ELeave)CDHCPOptionAny(NULL);
       
    42 	}
       
    43 
       
    44 COptionNode* CDHCPOptionStatusCode::NewL()
       
    45 	{
       
    46 	return new(ELeave)CDHCPOptionStatusCode();
       
    47 	}
       
    48 
       
    49 COptionNode* CDHCPOptionRequestOption::NewL()
       
    50 	{
       
    51 	return new(ELeave)CDHCPOptionRequestOption();
       
    52 	}
       
    53 
       
    54 struct SOptionHeader
       
    55 	{
       
    56 	TInt iCode;
       
    57 #ifdef __FLOG_ACTIVE
       
    58 	const TText8* iName;
       
    59 #endif
       
    60 	};
       
    61 
       
    62 #ifdef __FLOG_ACTIVE
       
    63 const TInt KNumberOfSupportedOptions = 23;
       
    64 const SOptionHeader KOptions[KNumberOfSupportedOptions + 1]= 
       
    65 	{   //iCode,                     iName
       
    66 		{EClientId,          _S8( "EClientId" )},
       
    67 		{EServerId,          _S8( "EServerId" )},
       
    68 		{EIaNa,              _S8( "EIaNa" )},
       
    69 		{EIaTa,              _S8( "EIaTa" )},
       
    70 		{EIaAddr,            _S8( "EIaAddr" )},
       
    71 		{EOro,               _S8( "EOro" )},
       
    72 		{EPreference,        _S8( "EPreference" )},
       
    73 		{EElapsedTime,       _S8( "EElapsedTime" )},
       
    74 		{ERelayMsg,          _S8( "ERelayMsg" )},
       
    75 		{EAuthentication,    _S8( "EAuthentication" )},
       
    76 		{EUnicast,           _S8( "EUnicast" )},
       
    77 		{EStatusCode,        _S8( "EStatusCode" )},
       
    78 		{ERapidCommit,       _S8( "ERapidCommit" )},
       
    79 		{EUserClass,         _S8( "EUserClass" )},
       
    80 		{EVendorClass,       _S8( "EVendorClass" )},
       
    81 		{EVendorOpts,        _S8( "EVendorOpts" )},
       
    82 		{EInterfaceId,       _S8( "EInterfaceId" )},
       
    83 		{EReconfMsg,         _S8( "EReconfMsg" )},
       
    84 		{EReconfAccept,      _S8( "EReconfAccept" )},
       
    85 		{ESipServerD,        _S8( "ESipServerD" )},
       
    86 		{ESipServerA,        _S8( "ESipServerA" )},
       
    87 		{EDNSServers,        _S8( "EDNSServers" )},
       
    88 		{EDomainList,        _S8( "EDomainList" )},
       
    89 		{0,                          _S8( "EUnknown" )}
       
    90 	};
       
    91 
       
    92 void COptionNode::Dump(const TDesC& aTag, const TDesC& aFile)
       
    93 	{
       
    94 	RFileLogger f;
       
    95 	if (f.Connect() == KErrNone)
       
    96 		{
       
    97 		f.CreateLog(aTag, aFile, EFileLoggingModeAppend);
       
    98 		TInt i = 0;
       
    99       TInt nCode = OpCode();
       
   100 		while (KOptions[i].iCode != nCode && ++i < KNumberOfSupportedOptions){/*do nothing*/}
       
   101 		
       
   102 		f.WriteFormat(_L8("Option Code: %s, length: %d"), KOptions[i].iName, nCode);
       
   103 		if (GetItemLength() > KOptionHeaderLength)
       
   104 			{
       
   105 			f.HexDump(NULL, NULL, GetBodyPtr(), GetLength());
       
   106 			}
       
   107 		f.CloseLog();
       
   108 		f.Close();
       
   109 		}
       
   110 	}
       
   111 #endif
       
   112 
       
   113 //simple static const mapping option id to the handling class constructor
       
   114 //to simplify parsing
       
   115 namespace DHCPv6
       
   116 {
       
   117 typedef COptionNode* (*TOptionCostructor)();
       
   118 
       
   119 
       
   120 struct SMapOptionNumberToOption
       
   121 {
       
   122    TUint iOption;
       
   123    TOptionCostructor iNewL;
       
   124 };
       
   125 
       
   126 static const SMapOptionNumberToOption mapOptionNumberToOption[] = {
       
   127     {EIaNa                  , CDHCPOptionIA_NA::NewL},       //Identity Association for Non-temporary Addresses 
       
   128     {EIaTa                  , CDHCPOptionIA_TA::NewL},          //Identity Association for Temporary Addresses
       
   129     {EOro                   , CDHCPOptionRequestOption::NewL},//Option Request - identify a list of options in a message
       
   130     {EAuthentication        , CDHCPOptionAuthentication::NewL},
       
   131     {EDNSServers		    , CDHCPOptionDNSServers::NewL},
       
   132     {ESipServerA			, CDHCPOptionSipServerAddrs::NewL},    	
       
   133     {EOptionAny             , COptionNode::NewL}
       
   134 	};
       
   135 //the rest of the options uses CDHCPOptionAny::NewL
       
   136 
       
   137 }//namespace DHCPv6
       
   138 
       
   139 void COptionList::ParseL(TPtr8& aDes8)
       
   140 /**
       
   141   * Parse message to set pointers into descriptor buffer
       
   142   * at locations of each option supplied in a response msg
       
   143   *
       
   144   * @internalTechnology
       
   145   */
       
   146 	{
       
   147 	ASSERT(!iRecord.iFirst);
       
   148 	
       
   149     COptionNode** ppNode = reinterpret_cast<COptionNode**>(&iRecord.iFirst);
       
   150 	
       
   151 	while(aDes8.Length())
       
   152         {
       
   153         User::LeaveIfError(aDes8.Length() < KOptionHeaderLength ? KErrBadDescriptor : KErrNone);
       
   154 
       
   155         TUint nOptCode = TBigEndian::GetValue(aDes8.Ptr(), KOptionLengthOffset);
       
   156 
       
   157 		*ppNode = CreateNodeL(nOptCode);
       
   158 		(*ppNode)->ParseL(aDes8);
       
   159 		
       
   160 		ppNode = reinterpret_cast<COptionNode**>(&(*ppNode)->iNext);
       
   161 		}
       
   162 	}
       
   163 
       
   164 TUint32 COptionList::GetL( TInt aIndex, TUint aOpCode ) const
       
   165 /**
       
   166   * Return a value from the message in big endian format
       
   167   *
       
   168   * @internalTechnology
       
   169   */
       
   170 	{
       
   171 	COptionNode* pNode = FindOption(aOpCode);
       
   172    if ( !pNode || pNode->GetLength() < aIndex * K32bitNumberOctets + K32bitNumberOctets )
       
   173       {
       
   174       User::Leave( KErrNotFound );
       
   175       }
       
   176    return TBigEndian::GetValue( pNode->Ptr() + aIndex * K32bitNumberOctets, K32bitNumberOctets );
       
   177 	}
       
   178 
       
   179 COptionNode* COptionList::CreateNodeL( TUint aOpCode )
       
   180    {
       
   181    TInt n = 0;
       
   182    while ( mapOptionNumberToOption[n].iOption != aOpCode && 
       
   183       mapOptionNumberToOption[n].iOption != EOptionAny )
       
   184       {
       
   185       n++;
       
   186       };
       
   187 	return (*mapOptionNumberToOption[n].iNewL)();
       
   188    }
       
   189 
       
   190 COptionNode* COptionList::AddNodeL(TOptionCodes aOpCode, TInt aInitialLength)
       
   191 /**
       
   192   * Create a new node for an option in the message
       
   193   *
       
   194   * @internalTechnology
       
   195   */
       
   196    {
       
   197    COptionNode* pNode = CreateNodeL(aOpCode);
       
   198    AddNode(pNode);
       
   199    pNode->Header().SetInitialValue(aInitialLength);
       
   200    return pNode;
       
   201    }
       
   202 
       
   203 COptionNode* COptionList::FindOption(TUint aOpCode, TInt& aPos) const
       
   204 	{
       
   205 	COptionNode* nodeCursor = static_cast<COptionNode*>(iRecord.iFirst);
       
   206 	TInt counter = 0;
       
   207 	while (nodeCursor)
       
   208 		{
       
   209 		if ( nodeCursor->OpCode() == aOpCode && ++counter > aPos )
       
   210 			{
       
   211 			aPos = counter;
       
   212 			return nodeCursor;
       
   213 			}
       
   214 		nodeCursor = static_cast<COptionNode*>(nodeCursor->iNext);
       
   215 		}
       
   216 	return NULL;
       
   217 	}
       
   218 
       
   219 TUint32 CDHCPOptionStatusCode::GetStatusCode() const
       
   220     {
       
   221     __ASSERT_DEBUG( GetLength() >= KDHCPOptionStatusCodeLength, User::Panic( KDhcpv6, KErrBadDescriptor ) );
       
   222     return TBigEndian::GetValue(iPtr8 + KOptionHeaderLength, KDHCPOptionStatusCodeLength);
       
   223     }
       
   224 
       
   225 void CDHCPOptionStatusCode::SetStatusCode( TUint32 aStatusCode )
       
   226     {
       
   227    __ASSERT_DEBUG( GetLength() >= KDHCPOptionStatusCodeLength, User::Panic( KDhcpv6, KErrBadDescriptor ) );
       
   228 	TBigEndian::SetValue(iPtr8 + KOptionHeaderLength, KDHCPOptionStatusCodeLength, aStatusCode);
       
   229     }
       
   230 
       
   231 void CDHCPOptionDNSServers::ParseL(TPtr8& aDes8)
       
   232 	{
       
   233 	COptionNode::ParseL(aDes8);
       
   234 	
       
   235 	TPtr8 ptr(GetBodyDes());
       
   236 	
       
   237 	User::LeaveIfError(ptr.Length() % KIp6AddressLength ? KErrBadDescriptor : KErrNone);
       
   238 	}
       
   239 
       
   240 TBool CDHCPOptionDNSServers::GetDomainNameServer( TInt aIndex, TInetAddr& addr )
       
   241 	{
       
   242 	TInt pos = aIndex * KIp6AddressLength;
       
   243 	TBool ret = GetLength() >= pos + KIp6AddressLength;
       
   244 	if ( ret )
       
   245 		{
       
   246 		// Must ensure IP6 address is word aligned! So declare it locally
       
   247 		TIp6Addr ip6addr;
       
   248 		Mem::Copy(&ip6addr,GetBodyPtr() + pos,KIp6AddressLength);
       
   249 		
       
   250 		addr.SetAddress(ip6addr);
       
   251 		}
       
   252 	return ret;
       
   253 	}
       
   254 	
       
   255 void CDHCPOptionSipServerAddrs::ParseL(TPtr8& aDes8)
       
   256 	{
       
   257 	COptionNode::ParseL(aDes8);
       
   258 	
       
   259 	TPtr8 ptr(GetBodyDes());
       
   260 	
       
   261 	User::LeaveIfError(ptr.Length() % KIp6AddressLength ? KErrBadDescriptor : KErrNone);
       
   262 	}
       
   263 
       
   264 TBool CDHCPOptionSipServerAddrs::GetSipServerAddr(TInt aIndex, TInetAddr& addr)
       
   265 	{
       
   266 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPOptionSipServerAddrs::GetSipServerAddr index = %d"),aIndex));
       
   267 	TInt pos = aIndex * KIp6AddressLength;
       
   268 	TInt len = GetLength();
       
   269 	TBool ret = len >= pos + KIp6AddressLength;
       
   270 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("pos = %d, len = %d, ret = %d"), pos, len, ret));
       
   271 
       
   272 	if (ret)
       
   273 		{
       
   274 		// Must ensure IP6 address is word aligned! So declare it locally
       
   275 		TIp6Addr ip6addr;
       
   276 		Mem::Copy(&ip6addr,GetBodyPtr() + pos,KIp6AddressLength);  
       
   277 
       
   278 		addr.SetAddress(ip6addr);
       
   279 		}
       
   280 	return ret;
       
   281 	}
       
   282 
       
   283 
       
   284 void CDHCPOptionSipServerDomains::ParseL(TPtr8& aDes8)
       
   285 	{
       
   286 	COptionNode::ParseL(aDes8);
       
   287 	}
       
   288 
       
   289 TBool CDHCPOptionSipServerDomains::GetSipServerDomains(TInt aIndex, THostName& aName)
       
   290 	{
       
   291 	TInt domainIdx = 0;
       
   292 	TUint8* pChar = const_cast<TUint8*>(GetBodyDes().Ptr());
       
   293 	TUint labelLength = 0;
       
   294 	
       
   295 	// Walk the list of domain names
       
   296 	while(*pChar++ != NULL && domainIdx != aIndex)
       
   297 		{
       
   298 		labelLength = *(pChar - 1);
       
   299 		*(pChar - 1) = '.';
       
   300 		
       
   301 		TBuf8<0x100> tmp;
       
   302 		tmp.Copy(pChar, labelLength);
       
   303 		aName.Copy(tmp);
       
   304 
       
   305 		pChar += labelLength;
       
   306 		}
       
   307 	
       
   308 	return EFalse;
       
   309 	}
       
   310 
       
   311 //---------------------------------
       
   312 void CDHCPOptionRequestOption::AppendRequestedOptions()
       
   313    {
       
   314 	// the array is in network byte order(big-endian) already => could be used as a descriptor
       
   315 	// authentication option and IA for temporary addresses not involved so far
       
   316 	TUint8 requestedOptions[KDHCPOptionRequestLen] = {0,EServerId, 0,EIaNa, 0,ESipServerD, 0,ESipServerA,
       
   317 														0,EDNSServers, 0,EDomainList};
       
   318 
       
   319 
       
   320 	TPtr8 ptr(requestedOptions, KDHCPOptionRequestLen, KDHCPOptionRequestLen );
       
   321 	GetBodyDes().Copy(ptr);
       
   322    }
       
   323 
       
   324 CDHCPMessageHeaderIP6::~CDHCPMessageHeaderIP6()
       
   325 	{
       
   326 	Close();
       
   327 	}
       
   328 
       
   329 COptionNode* CDHCPMessageHeaderIP6::AddOptionL(TOptionCodes aOpCode, TInt aLength)
       
   330 /**
       
   331   * First stage of adding an option to the message. Calls AddNode to create space
       
   332   * for message.
       
   333   *
       
   334   * @param aOpCode The opcode of the options to be added
       
   335   * @param aLength The length of the option data
       
   336   * @return COptionNode* The pointer to the option in the message
       
   337   *
       
   338   * @internalTechnology
       
   339   */
       
   340 	{
       
   341 	COptionNode* pNode = iOptions.AddNodeL(aOpCode, aLength);
       
   342 	TPtr8 ptr = iMsg->Des();
       
   343 	pNode->iRecord.InitialiseL(ptr);
       
   344 	pNode->SetOpCode(static_cast<TUint>(aOpCode));
       
   345 	return pNode;
       
   346 	}
       
   347 	
       
   348 TInt CDHCPMessageHeaderIP6::Parse(const TDhcpRnd& aXid, const TDesC8& aClientId, RBuf8& aServerId)
       
   349 /**
       
   350   * When a response is received to a message
       
   351   * this function will unpack the response by creating
       
   352   * option objects which can be easily used to retrieve 
       
   353   * the values returned in these options.  Providing 
       
   354   * this functionality hides the nasty descriptor operations. 
       
   355   *
       
   356   * Each option class has a pointer set to the start point
       
   357   * of their data in the response message.
       
   358   *
       
   359   * There is no need to explicitly unpack the mandatory fields
       
   360   * of the DHCP message that has been returned.  Accessor functions
       
   361   * simply extract the data sraight out of the decsriptor.
       
   362   *
       
   363   * @internalTechnology
       
   364   */
       
   365 	{
       
   366 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse")));
       
   367 	
       
   368 	iOptions.RemoveAllNodes();
       
   369 	TPtr8 ptr = iMsg->Des();
       
   370 	TRAPD(ret,iRecord.ParseL(ptr););
       
   371 	
       
   372    if ( ret == KErrNone )
       
   373 		{
       
   374 		/* Reject the message if:
       
   375 		 * 
       
   376 		 * 1) Not an Advertise, Reply, or potentially a Reconfigure message. 
       
   377 		 * 2) The server or client ID values are nonexistent
       
   378 		 * 3) The client ID being advertised does not match ours
       
   379 		 * 4) Bad status code
       
   380 		 */
       
   381 	   if (GetXid()!=aXid.Xid())
       
   382 		   {
       
   383 		   __CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - non-matching xid")));
       
   384 		   return KErrBadDescriptor;	// does not match so we are not interested in this message
       
   385 		   }
       
   386       CDHCPOptionStatusCode* pStatus = static_cast<CDHCPOptionStatusCode*>(iOptions.FindOption( EStatusCode ));
       
   387       TInt code = pStatus ? pStatus->GetStatusCode() : ESuccess;
       
   388 		if (code != ESuccess )
       
   389 		   {
       
   390 		   __CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - status code %d"), code));
       
   391 		   return KErrBadDescriptor;	// bad status code so we are not interested in this message
       
   392 		   }
       
   393       TUint8 nType = GetMessageType();
       
   394       COptionNode* pServerId = iOptions.FindOption( EServerId );
       
   395       COptionNode* pClientId = iOptions.FindOption( EClientId );
       
   396 		switch(nType)
       
   397 			{
       
   398 			case EAdvertise:
       
   399 				if(pClientId == NULL || pServerId == NULL)
       
   400 					{
       
   401 					__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - invalid message type %d or options"), nType));
       
   402 					return KErrBadDescriptor;
       
   403 					}
       
   404 					break;			
       
   405 			case EReply:
       
   406 #if defined(DHCP_RECONFIGURE_NO_AUTHENTICATION)
       
   407 			case EReconfigure:
       
   408 #endif				
       
   409 				if (pClientId == NULL || pServerId == NULL || 
       
   410 				    (aServerId.Length() && pServerId->GetBodyDes() != aServerId))
       
   411 					{
       
   412 					__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - invalid message type %d or options"), nType));
       
   413 					return KErrBadDescriptor;
       
   414 					}
       
   415 				if ( nType == EReconfigure && !iOptions.FindOption( EReconfMsg ) )
       
   416 				    {
       
   417 					__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - Reconfigure msg with no reconfigure option")));
       
   418 					return KErrBadDescriptor;
       
   419 				    }
       
   420 				break;
       
   421 			 default:
       
   422 				__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - invalid message type %d or options"), nType));
       
   423 				return KErrBadDescriptor;
       
   424 			}
       
   425 
       
   426 		if(pClientId->GetBodyDes() != aClientId)
       
   427 			{
       
   428 			__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPMessageHeaderIP6::Parse - ClientIds don't match")));
       
   429 			return KErrBadDescriptor;
       
   430 			}
       
   431 		if ( !aServerId.Length() )
       
   432 		    {
       
   433             aServerId.Close();
       
   434             TRAP(ret, aServerId.CreateL( pServerId->GetBodyDes() ));
       
   435 		    }
       
   436       }
       
   437    return ret;
       
   438 	}
       
   439 
       
   440 #ifdef SYMBIAN_TCPIPDHCP_UPDATE
       
   441 void CDHCPOptionDomainSearchList::ParseL(TPtr8& aDes8)
       
   442 	{
       
   443 	COptionNode::ParseL(aDes8);
       
   444 	}
       
   445 
       
   446 TBool CDHCPOptionDomainSearchList::GetDomainSearchList(TInt aIndex, THostName& aName)
       
   447 	{
       
   448 	TInt domainIdx = 0;
       
   449 	TUint8* pChar = const_cast<TUint8*>(GetBodyDes().Ptr());
       
   450 	TUint labelLength = 0;
       
   451 	TBuf8<0x100> tmp;
       
   452 	
       
   453 	if(*pChar)
       
   454 		{
       
   455 		// Walk the list of domain names
       
   456 		while(*pChar++ != NULL && domainIdx != aIndex)
       
   457 			{
       
   458 			labelLength = *(pChar - 1);
       
   459 			*(pChar - 1) = '.';
       
   460 				
       
   461 			tmp.Copy(pChar, labelLength);
       
   462 			aName.Copy(tmp);
       
   463 
       
   464 			pChar += labelLength;
       
   465 			}
       
   466 		return EFalse;
       
   467 		}
       
   468 	else
       
   469 		{
       
   470 		return ETrue;
       
   471 		}
       
   472 	}
       
   473 #endif //SYMBIAN_TCPIPDHCP_UPDATE