tcpiputils/dhcp/src/DomainNameDecoder.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 // Decodes domain names within protocol messages as specified in:
       
    15 // "RFC 1035 - Domain names - implementation and specification"
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file DomainNameDecoder.cpp
       
    21  @internalTechnology
       
    22 */
       
    23 
       
    24 #include "DomainNameDecoder.h"
       
    25 
       
    26 void CDomainNameCodec::EncodeL(TDomainNameArray& aNames, RBuf8& aBuf8)
       
    27 	{
       
    28 	TUint requiredLength = 0;
       
    29 	TUint8 nameIdx = 0;
       
    30 	
       
    31 	for (nameIdx=0;nameIdx<aNames.Count();nameIdx++)
       
    32 		{
       
    33 		// The total length required for the labels that comprise an
       
    34 		// individual domain name needs to take into the length octet
       
    35 		// for the initial label and the null-termination character.
       
    36 		// Hence the '+ 2' below.
       
    37 		requiredLength += (aNames[nameIdx].Length() + 2);		
       
    38 		
       
    39 		// A further length check is performed on each domain name to
       
    40 		// ensure it does not exceed the maximum length permitted according
       
    41 		// to RFC 1035.
       
    42 		if(aNames[nameIdx].Length() > KMaxDomainNameLength)
       
    43 			{
       
    44 			User::Leave(KErrArgument);
       
    45 			}
       
    46 		}		
       
    47 	
       
    48 	aBuf8.Zero();
       
    49 	aBuf8.ReAllocL(requiredLength);
       
    50 	
       
    51 	TLex8 domainName;
       
    52 	TPtrC8 currentLabel;
       
    53 	
       
    54 	for (nameIdx=0;nameIdx<aNames.Count();nameIdx++)
       
    55 		{
       
    56 		domainName.Assign(aNames[nameIdx]);		
       
    57 		domainName.Mark();
       
    58 		
       
    59 		while (!domainName.Eos())
       
    60 			{
       
    61 			TChar ch;
       
    62 			do
       
    63 				{
       
    64 				ch = domainName.Get();
       
    65 				}
       
    66 			while ( ch != TChar('.') && !domainName.Eos() );
       
    67 			
       
    68 			// if not the end of the string, unget the previous char to skip the trailing
       
    69 			//  dot in our marked token
       
    70 			//
       
    71 			if( !domainName.Eos() )
       
    72 				{
       
    73 				domainName.UnGet();
       
    74 				}
       
    75 			
       
    76 			currentLabel.Set(domainName.MarkedToken());
       
    77 			
       
    78 			// move past the dot again, or do nothing in particular at EOS
       
    79 			//
       
    80 			domainName.Get();
       
    81 			
       
    82 			User::LeaveIfError(currentLabel.Length() > KMaxDnsLabelLength ? 
       
    83 				KErrArgument : KErrNone);
       
    84 			
       
    85 			aBuf8.Append(TChar(currentLabel.Length()));
       
    86 			aBuf8.Append(currentLabel);
       
    87 			
       
    88 			domainName.Mark();
       
    89 			}
       
    90 		
       
    91 		aBuf8.Append(TChar(0));
       
    92 		}
       
    93 	}
       
    94 
       
    95 void CDomainNameCodec::DecodeL(TPtr8& aInDes)
       
    96 	{
       
    97 	TDomainName domainName;
       
    98 
       
    99 	TUint8* pChar = const_cast<TUint8*>(aInDes.Ptr());
       
   100 	TUint listLength = aInDes.Length();
       
   101 	TUint8 labelLength = 0;
       
   102 	
       
   103 	// Walk the list of domain names
       
   104 	while(pChar < aInDes.Ptr() + listLength)
       
   105 		{
       
   106 		domainName.Zero();
       
   107 		
       
   108 		while(*pChar++ != NULL)
       
   109 			{
       
   110 			if(domainName.Length() > 0)
       
   111 				{
       
   112 				domainName.Append('.');
       
   113 				}
       
   114 				
       
   115 			labelLength = *(pChar - 1);
       
   116 			
       
   117 			// The two highest order bits must be clear
       
   118 			User::LeaveIfError(labelLength & 0xC0 ? KErrArgument : KErrNone);
       
   119 					
       
   120 			// Add in the label data
       
   121 			domainName.Append(pChar, labelLength);
       
   122 			
       
   123 			// Advance the pointer to the next length value
       
   124 			pChar += labelLength;
       
   125 			}
       
   126 			
       
   127 		iDomainList.Append(domainName);
       
   128 		}
       
   129 	}
       
   130 	
       
   131 CDomainNameCodec::~CDomainNameCodec()
       
   132 	{
       
   133 	iDomainList.Close();
       
   134 	}
       
   135 	
       
   136 	
       
   137 	
       
   138 	
       
   139 	
       
   140 	
       
   141