zeroconf/dnsparser/src/cdnspacket.cpp
changeset 14 da856f45b798
equal deleted inserted replaced
12:78fbd574edf4 14:da856f45b798
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cdnspacket.h"
       
    20 	
       
    21 	
       
    22 CDnsPacket::CDnsPacket(const TDes8& aPacketPtr): iPtr(aPacketPtr.Ptr()),iDataLength(aPacketPtr.Size()),
       
    23            iMaxLength(aPacketPtr.MaxSize())
       
    24 	{	
       
    25 	}
       
    26 
       
    27 CDnsPacket* CDnsPacket::NewL(const TDes8& aPacketPtr)
       
    28 	{
       
    29 	CDnsPacket* self =  new (ELeave) CDnsPacket( aPacketPtr );	
       
    30 	CleanupStack::PushL(self);
       
    31 	self->ConstructL();
       
    32 	CleanupStack::Pop(self);
       
    33 	return self;
       
    34 	}	
       
    35 		
       
    36 CDnsPacket::~CDnsPacket()
       
    37 	{	
       
    38 	}
       
    39 		
       
    40 void CDnsPacket::ConstructL()
       
    41 	{
       
    42 	iIndex = 0;
       
    43 	}
       
    44 	
       
    45 TUint8 CDnsPacket::GetCharL()
       
    46 	{
       
    47 	__OOB(iIndex,iDataLength)
       
    48 	TUint8 val = *(iPtr+iIndex);
       
    49 	iIndex += sizeof(TUint8);		
       
    50 	return val;
       
    51 	}
       
    52 		
       
    53 TUint16 CDnsPacket::GetInt16L()
       
    54 	{
       
    55 	__OOB(iIndex,iDataLength)
       
    56 	TUint16 val = BigEndian::Get16(iPtr+iIndex);
       
    57 	iIndex += sizeof(TUint16);		
       
    58 	return val;
       
    59 	}
       
    60 		
       
    61 TUint32 CDnsPacket::GetInt32L()
       
    62 	{
       
    63 	__OOB(iIndex,iDataLength)
       
    64 	TUint32 val = BigEndian::Get32(iPtr+iIndex);
       
    65 	iIndex += sizeof(TUint32);		
       
    66 	return val;
       
    67 	}
       
    68 
       
    69 const TDnsHeader& CDnsPacket::GetHeaderL()
       
    70 	{
       
    71 	iHeader.SetId(GetInt16L());
       
    72 	iHeader.SetFlags(GetInt16L());
       
    73 	iHeader.SetQueryCount(GetInt16L());
       
    74 	iHeader.SetAnswerCount(GetInt16L());
       
    75 	iHeader.SetAuthorityNSCount(GetInt16L());
       
    76 	iHeader.SetAdditionalRCount(GetInt16L());
       
    77 	
       
    78 	return iHeader;
       
    79 	}
       
    80 
       
    81 void CDnsPacket::GetDomainNameL(TDes8& aName)
       
    82 	{	
       
    83 	TInt labelLen = GetCharL();
       
    84 	RBuf8 label;
       
    85 	TInt isStartingWithPointer = labelLen & KPointerBit;
       
    86 	
       
    87 	if(isStartingWithPointer)
       
    88 	    {
       
    89 		TUint16 pointerOffset = labelLen & KPointerOffsetBit;
       
    90 		__OOB(pointerOffset,iDataLength)
       
    91 		pointerOffset = (pointerOffset << 8) + GetCharL();
       
    92 		GetDomainNameAtIndexL(aName, pointerOffset);			
       
    93 	    }
       
    94 	else
       
    95 		{
       
    96 		while(labelLen)
       
    97 			{			
       
    98 			TInt isPointer = labelLen & KPointerBit;			
       
    99 			if(isPointer)
       
   100 				{
       
   101 				TUint16 pointerOffset = labelLen & KPointerOffsetBit;
       
   102 				__OOB(pointerOffset,iDataLength)
       
   103 				pointerOffset = (pointerOffset << 8) + GetCharL();
       
   104 				label.CreateL(KMaxDNSNameLength);
       
   105 				GetStringAtIndexL(label,pointerOffset);		
       
   106 				}
       
   107 			else
       
   108 				{
       
   109 				label.CreateL(labelLen);
       
   110 				GetStringL(label,labelLen);
       
   111 				}			
       
   112 			aName.Append(label);
       
   113             if(aName[aName.Size()-1]!='.')
       
   114                 aName.Append(KDot);
       
   115 			label.Close();
       
   116 			if(isPointer )
       
   117 				{
       
   118 				break;
       
   119 				}
       
   120 			else
       
   121 				{
       
   122 				labelLen = GetCharL();		
       
   123 				}			
       
   124 			}
       
   125 		}		
       
   126 	}
       
   127 	
       
   128 void CDnsPacket::GetRdDomainNameL(TDes8& aDomainName,TUint aLength)
       
   129 	{	
       
   130 	TInt labelLen = GetCharL();
       
   131 	aLength--;
       
   132 	RBuf8 label;
       
   133 	TInt isStartingWithPointer = labelLen & KPointerBit;
       
   134 	
       
   135 	while(aLength)
       
   136 		{		
       
   137 		TInt isPointer = labelLen & KPointerBit;	
       
   138 		if(isPointer)
       
   139 			{
       
   140 			TUint16 pointerOffset = labelLen & KPointerOffsetBit;
       
   141 			__OOB(pointerOffset,iDataLength)
       
   142 			pointerOffset = (pointerOffset << 8) + GetCharL();
       
   143 			aLength--;
       
   144 			label.CreateL(KMaxLabelLength);
       
   145 			if(isStartingWithPointer )  
       
   146 				{
       
   147 				GetDomainNameAtIndexL(aDomainName, pointerOffset);
       
   148 				}
       
   149 			else
       
   150 				{
       
   151 				GetDomainNameAtIndexL(label, pointerOffset);
       
   152 				//GetStringAtIndexL(label, pointerOffset);				
       
   153 				}
       
   154 			}
       
   155 		else
       
   156 			{
       
   157 			label.CreateL(labelLen);
       
   158 			GetStringL(label,labelLen);
       
   159 			aLength-=labelLen;
       
   160 			}
       
   161 			
       
   162 		aDomainName.Append(label);
       
   163 		label.Close();
       
   164 		if(isStartingWithPointer)
       
   165 			{
       
   166 			break;
       
   167 			}
       
   168 		else
       
   169 			{
       
   170 			if(aLength )
       
   171 				{
       
   172 				labelLen = GetCharL();
       
   173 				aLength--;
       
   174                 if(aDomainName[aDomainName.Size()-1]!='.')
       
   175                     aDomainName.Append(KDot);
       
   176 				}
       
   177 			}
       
   178 		}
       
   179 	}
       
   180 
       
   181 void CDnsPacket::GetStringL(TDes8& aString, TUint16 aLength)	
       
   182 	{
       
   183 	__OOB(iIndex,iDataLength)
       
   184 	aString.Copy(iPtr+iIndex,aLength);
       
   185 	iIndex+= aLength;
       
   186 	}
       
   187 
       
   188 void CDnsPacket::GetDomainNameAtIndexL(TDes8& aDomainName, TUint aOffset)
       
   189 	{
       
   190 	__OOB(aOffset,iDataLength)
       
   191 	TUint8 labelLen = *(iPtr + aOffset);
       
   192 	RBuf8 label;
       
   193 	TInt isPointer;
       
   194 	TUint16 pointerOffset;
       
   195 	TInt isStartingWithPointer = labelLen & KPointerBit;
       
   196 	TUint8 ptrPos;
       
   197 	if(isStartingWithPointer)
       
   198 		{
       
   199 		pointerOffset = labelLen & KPointerOffsetBit;
       
   200 		__OOB(aOffset,iDataLength)
       
   201 		aOffset++;
       
   202 		ptrPos = *(iPtr + aOffset);
       
   203 		pointerOffset = (pointerOffset << 8) + ptrPos;		
       
   204 		GetDomainNameAtIndexL(aDomainName, pointerOffset);
       
   205 		}
       
   206 	else
       
   207 		{
       
   208 		while(labelLen)		
       
   209 			{
       
   210 			label.CreateL(KMaxLabelLength);
       
   211 			GetStringAtIndexL(label, aOffset);
       
   212 			aDomainName.Append(label);
       
   213             if(aDomainName[aDomainName.Size()-1]!='.')
       
   214                 aDomainName.Append(KDot);
       
   215 			__OOB(aOffset,iDataLength)
       
   216 			aOffset++; // increment by one byte for length field
       
   217 			aOffset += labelLen;
       
   218 			label.Close();	
       
   219 			labelLen = 	*(iPtr + aOffset);
       
   220 			isPointer = labelLen & KPointerBit;
       
   221 			if(isPointer)
       
   222 				{
       
   223 				pointerOffset = labelLen & KPointerOffsetBit;
       
   224 				__OOB(aOffset,iDataLength)
       
   225 				aOffset++;
       
   226 				ptrPos = *(iPtr + aOffset);
       
   227 				pointerOffset = (pointerOffset << 8) + ptrPos;			
       
   228 				label.CreateL(KMaxLabelLength);
       
   229 				GetStringAtIndexL(label, pointerOffset);				
       
   230 				aDomainName.Append(label);
       
   231 				if(aDomainName[aDomainName.Size()-1]!='.')
       
   232 				    aDomainName.Append(KDot);
       
   233 				label.Close();
       
   234 				break;
       
   235 				}
       
   236 			}
       
   237 		}	
       
   238 	}
       
   239 		
       
   240 void CDnsPacket::GetStringAtIndexL(TDes8& aLabel, TUint aOffset)
       
   241 	{
       
   242 	__OOB(aOffset,iDataLength)
       
   243 	TUint8 labelLen = *(iPtr + aOffset);
       
   244 	if(labelLen & KPointerBit)		// recursive pointers
       
   245 		{
       
   246 		TUint16 pointerOffset = labelLen & KPointerOffsetBit;
       
   247 		pointerOffset = (pointerOffset << 8) + GetCharL();
       
   248 		GetStringAtIndexL(aLabel, pointerOffset);
       
   249 		}
       
   250 	else
       
   251 		{
       
   252 		aLabel.Copy(iPtr+aOffset+1, labelLen);			
       
   253 		}
       
   254 	}