zeroconf/dnsparser/src/cdnsmsgbuf.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 "cdnsmsgbuf.h"
       
    20 	
       
    21 	
       
    22 CDnsMsgBuf::CDnsMsgBuf(const TDesC8& aBuf):iDataLength(aBuf.Size())
       
    23 	{	
       
    24 	}
       
    25 
       
    26 CDnsMsgBuf* CDnsMsgBuf::NewL(const TDesC8& aBuf)
       
    27 	{
       
    28 	CDnsMsgBuf* self =  new (ELeave) CDnsMsgBuf(aBuf);	
       
    29 	CleanupStack::PushL(self);
       
    30 	self->ConstructL(aBuf);
       
    31 	CleanupStack::Pop(self);
       
    32 	return self;
       
    33 	}	
       
    34 		
       
    35 CDnsMsgBuf::~CDnsMsgBuf()
       
    36 	{	
       
    37 	}
       
    38 		
       
    39 void CDnsMsgBuf::ConstructL(const TDesC8& aBuf)
       
    40 	{
       
    41 	iPtr = (TUint8*)aBuf.Ptr();
       
    42 	iIndex = 0;
       
    43 	}
       
    44 	
       
    45 void CDnsMsgBuf::SetChar(TUint8 aVal)
       
    46 	{
       
    47 	*(iPtr+iIndex) = aVal;
       
    48 	iIndex += sizeof(TUint8);		
       
    49 	}
       
    50 		
       
    51 void CDnsMsgBuf::SetInt16(TUint16 aVal)
       
    52 	{
       
    53     BigEndian::Put16(iPtr + iIndex, aVal);
       
    54     iIndex += sizeof(TUint16);
       
    55 	}
       
    56 		
       
    57 void CDnsMsgBuf::SetInt32(TUint32 aVal)
       
    58 	{
       
    59 	BigEndian::Put32(iPtr + iIndex, aVal);
       
    60 	iIndex += sizeof(TUint32);		
       
    61 	}
       
    62 
       
    63 void CDnsMsgBuf::SetDomainNameL(const TDesC8& aName)
       
    64 	{	
       
    65 	RBuf8 name;
       
    66     if(aName.LocateReverse('.')== aName.Length()-1)
       
    67         {
       
    68         name.Create(aName.Left(aName.Length()-1));
       
    69         }
       
    70     else 
       
    71         {
       
    72         name.Create(aName);
       
    73         }
       
    74 	CStringParser* strParser =  CStringParser::NewL (name);
       
    75 	CleanupStack::PushL ( strParser );
       
    76 	TPtrC8 token;
       
    77 	TChar ch = '.';
       
    78 	TUint8 len = 0;
       
    79 	while(1)
       
    80 		{
       
    81 		TBool result = strParser->ParseTill(token,ch);
       
    82 		len = token.Size();
       
    83 		SetChar(len);
       
    84 		for(TInt i = 0; i< len; i++)
       
    85 			{
       
    86 			SetChar(token.Ptr()[i]);
       
    87 			}
       
    88 		strParser->SkipLength(1);
       
    89 		if(!result)
       
    90 			break;
       
    91 		}
       
    92 	
       
    93 	// append a zero at the end
       
    94 	SetChar(NULL);
       
    95 	CleanupStack::PopAndDestroy ( strParser );
       
    96 	name.Close();
       
    97 	}
       
    98 
       
    99 void CDnsMsgBuf::SetText(const TDesC8& aTxt)
       
   100 	{
       
   101 	TUint8 len = 0;
       
   102     len = aTxt.Size();
       
   103 	SetChar(len);
       
   104 	for(TInt i=0; i<len; i++)
       
   105 		{
       
   106 		SetChar(aTxt.Ptr()[i]);
       
   107 		}
       
   108 	}
       
   109 	
       
   110 void CDnsMsgBuf::SetPtrRdLengthL(const TDesC8& aName)
       
   111 	{
       
   112 	TUint16 len = GetDomainLengthL(aName);
       
   113 	SetInt16(len);
       
   114 	}
       
   115     
       
   116 void CDnsMsgBuf::SetSrvRdLengthL(const TDesC8& aName)
       
   117 	{
       
   118 	TUint16 len = GetDomainLengthL(aName);
       
   119 	len += 6;  //sizeof(prio+weight+port)
       
   120 	SetInt16(len);
       
   121 	}		
       
   122 
       
   123 void CDnsMsgBuf::SetTxtRdLength(const RArray<RBuf8>& aTxtList)
       
   124 	{
       
   125 	TUint16 len = 0;	
       
   126 	TInt count = aTxtList.Count();
       
   127 	for (TInt i=0; i< count; i++)
       
   128 		{
       
   129 		len++;
       
   130 		len += aTxtList[i].Size();  
       
   131 		}
       
   132 	SetInt16(len);
       
   133 	}
       
   134  
       
   135 TUint16 CDnsMsgBuf::GetDomainLengthL(const TDesC8& aName)const
       
   136 	{
       
   137     RBuf8 name;
       
   138     if(aName.LocateReverse('.')== aName.Length()-1)
       
   139         {
       
   140         name.Create(aName.Left(aName.Length()-1));
       
   141         }
       
   142     else 
       
   143         {
       
   144         name.Create(aName);
       
   145         }
       
   146 	
       
   147 	CStringParser* strParser =  CStringParser::NewL (name);
       
   148 	CleanupStack::PushL ( strParser );
       
   149 	TPtrC8 token;
       
   150 	TChar ch = '.';
       
   151 	TUint16 len = 0;	
       
   152 	while(1)
       
   153 		{
       
   154 		TBool result = strParser->ParseTill(token,ch);
       
   155 		len++;
       
   156 		len += token.Size();  
       
   157 		strParser->SkipLength(1);
       
   158 		if(!result)
       
   159 			break;
       
   160 		}		
       
   161 	len++;   //0 length octet appended at the end
       
   162 	CleanupStack::PopAndDestroy ( strParser );
       
   163 	name.Close();
       
   164 	return len;
       
   165 	}		
       
   166