networkprotocols/dnsproxy/dnsproxyserver/src/dnsproxyengine.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2008-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 // This file consists of implementation of DNS Proxy engine. This is the controller
       
    15 // class which reads configuration from resolver.ini, configures DNS Proxy components
       
    16 // and starts and stops the listener.
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23  @internalTechnology
       
    24 */
       
    25 
       
    26 #include <es_sock.h>
       
    27 #include <cflog.h>
       
    28 #include "e32debug.h"
       
    29 #include "dnsproxylistener.h"
       
    30 #include "dnsproxyengine.h"
       
    31 #include "dnsproxyservconf.h"
       
    32 #include "dnsproxylog.h"
       
    33 #include "inet6log.h"
       
    34 
       
    35 //intializing the static object
       
    36 CDnsProxyEngine* CDnsProxyEngine::iDnsProxyEngine = NULL;
       
    37 
       
    38 CDnsProxyEngine* CDnsProxyEngine::NewL()
       
    39 /**
       
    40  * This is the two phase construction method and is leaving function
       
    41  * @param - None
       
    42  * @return - None
       
    43  *
       
    44  * @internalTechnology
       
    45  */
       
    46 	{
       
    47 	__LOG("\n CDnsProxyEngine::NewL Entry")
       
    48 	CDnsProxyEngine* pEngine = new(ELeave)CDnsProxyEngine();
       
    49 	CleanupStack::PushL(pEngine);
       
    50 	pEngine->ConstructL();
       
    51 	CleanupStack::Pop();
       
    52 	__LOG("\n CDnsProxyEngine::NewL Exit")
       
    53 	return pEngine;
       
    54 	}
       
    55 
       
    56 
       
    57 CDnsProxyEngine::CDnsProxyEngine()
       
    58 /**
       
    59  * This is the constructor for this class
       
    60  * @param - None
       
    61  * @return - None
       
    62  *
       
    63  * @internalTechnology
       
    64  */
       
    65 
       
    66 	{
       
    67 	__LOG("\n CDnsProxyEngine::CDnsProxyEngine Entry")
       
    68 	iConfigData = NULL;
       
    69 	iConfigDataErr = 0;
       
    70 	__LOG("\n  CDnsProxyEngine::CDnsProxyEngine Exit")
       
    71 	}
       
    72 /* This is the destructor  and deletes all the member variables and thus
       
    73  * releases all the memory assosicated with those objects
       
    74  */
       
    75 CDnsProxyEngine::~CDnsProxyEngine()
       
    76 /**
       
    77  * This is the destructor  and deletes all the member variables and thus
       
    78  * releases all the memory assosicated with those objects
       
    79  * @param - None
       
    80  * @return - None
       
    81  *
       
    82  * @internalTechnology
       
    83  **/
       
    84 
       
    85 	{
       
    86 	__LOG("\n CDnsProxyEngine::~CDnsProxyEngine Entry")
       
    87 	delete iConfigData;
       
    88 	delete iProxyServer;
       
    89 	delete iListener;
       
    90 	delete iDnsproxydb;
       
    91 	iSockServ.Close();
       
    92 	__LOG("\n CDnsProxyEngine::~CDnsProxyEngine Exit")
       
    93 	}
       
    94 /* This is second phase construction where in all objects will be created
       
    95  * which potentially may leave due to resource problem
       
    96  */
       
    97 void CDnsProxyEngine::ConstructL()
       
    98 /**
       
    99  * This is the second phase of construction.
       
   100  * Create intances of proxy database, config server and listener classes.
       
   101  * Reads configuration values from .ini file
       
   102  * @param - None
       
   103  * @return - None
       
   104  *
       
   105  * @internalTechnology
       
   106  **/
       
   107 	{
       
   108 	__LOG("\n CDnsProxyEngine::ConstructL Entry")
       
   109 	//Reads the configuration from .ini file
       
   110 	ReadConfiguration();
       
   111 	User::LeaveIfError(iSockServ.Connect());
       
   112 	__LOG("After Reading configuration")
       
   113 	iDnsproxydb = CDnsProxyDb::CreateInstanceL(*this);
       
   114 	iProxyServer = CDnsServerConfig::NewL(*this);
       
   115 	iListener    = CDnsProxyListener::NewL(*this);
       
   116 	
       
   117 	__LOG("\n CDnsProxyEngine::ConstructL Exit")
       
   118 	}
       
   119 
       
   120 /* This is function reads configuration values from configuration file
       
   121  *
       
   122  */
       
   123 void CDnsProxyEngine::ReadConfiguration()
       
   124 /**
       
   125  *
       
   126  * This method reads configuration values from .ini file
       
   127  * @param - None
       
   128  * @return - None
       
   129  *
       
   130  * @internalTechnology
       
   131  **/
       
   132 
       
   133 	{
       
   134 	__LOG("\n CDnsProxyEngine::ReadConfiguration Entry")
       
   135 	iConfigParams.iSessionCount = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_NUM_SOCK_SESSION,KSessioncount);
       
   136 	iConfigParams.iRetryCount   = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_NUM_RETRIES,KRetrycount);
       
   137 	iConfigParams.iQSize        = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_QUEUE_SIZE,KQSize);
       
   138 	iConfigParams.iTTL          = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_TTL,KTtl);
       
   139 	iConfigParams.iDbSize       = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_DB_SIZE,KDbSize);
       
   140 	iConfigParams.iTimerVal     = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_TIMER_VAL,KTimerVal);
       
   141 
       
   142 	iConfigParams.iSerialNum    = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_SERIAL_NUM,KSerialNum);
       
   143 	iConfigParams.iRefreshTime  = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_REFRESH_TIME,KRefreshTime);
       
   144 	iConfigParams.iRetryTime    = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_RETRY_TIME,KRetryTime);
       
   145 	iConfigParams.iExpiretime   = GetIniValue(DNS_PROXY_INI_SECTION,DNS_PROXY_INI_EXPIRE_TIME,KExpireTime);	
       
   146 
       
   147 	__LOG1("\n CDnsProxyEngine::ReadConfiguration iSessionCount (%d)",iConfigParams.iSessionCount)
       
   148 	__LOG1("\n CDnsProxyEngine::ReadConfiguration iRetryCount (%d)",iConfigParams.iRetryCount)
       
   149 	__LOG1("\n CDnsProxyEngine::ReadConfiguration iTimerVal (%d)",iConfigParams.iTimerVal)
       
   150 	UnLoadConfigurationFile();
       
   151 	}
       
   152 
       
   153 TInt CDnsProxyEngine::Start(const RMessage2& aMessage)
       
   154 /**
       
   155  * This is entry point of DNS Proxy functionality. The DHCP server session
       
   156  * calls this using client API to start dnsproxy.
       
   157  * @param aMessage - Message send by the client
       
   158  * @return - None
       
   159  *
       
   160  * @internalTechnology
       
   161  **/
       
   162 
       
   163 	{
       
   164     TInt err;
       
   165 	__LOG("\n CDnsProxyEngine::Start Entry")
       
   166 	err = SetDlinkConnectionInfo(aMessage);
       
   167 	if(err!=KErrNone)
       
   168 		return err;
       
   169 
       
   170 	err = iListener->Activate();
       
   171 	__LOG("\n CDnsProxyEngine::ReadConfiguration Exit")
       
   172 	return err;
       
   173 	}
       
   174 
       
   175 void CDnsProxyEngine::Stop()
       
   176 /**
       
   177  * This stops the listener and deletes all the objects.The method is called at the time
       
   178  * of shutdown.
       
   179  * @param - None
       
   180  * @return - None
       
   181  *
       
   182  * @internalTechnology
       
   183  **/
       
   184 	{
       
   185 	__LOG("\n CDnsProxyEngine::Stop Entry")
       
   186 	iListener->StopListener();
       
   187 	__LOG("\n CDnsProxyEngine::Stop Exit")
       
   188 	}
       
   189 
       
   190 TBool CDnsProxyEngine::FindVar(const TDesC &aSection, const TDesC &aVarName, TInt &aResult)
       
   191 /**
       
   192  * This will find specified value for variable in the configuation file
       
   193  * @param aSection - represents the section name in configuration file
       
   194  * @param aVarName - represents name in the specified section
       
   195  * @param aResult - sets the corresponding values for above given varname
       
   196  * @return - returns the boolean flag - if name value pair or loading configuration
       
   197  * 			- fails then returns false
       
   198  * @panic - system wide errors
       
   199  *
       
   200  * @internalTechnology
       
   201  **/
       
   202 	{
       
   203 	__LOG("\n DnsProxyEngine::FindVar")
       
   204 	if(LoadConfigurationFile())
       
   205 	  {
       
   206 	   ASSERT(iConfigData);	// <-- lint gag
       
   207 	   return iConfigData->FindVar(aSection, aVarName, aResult);
       
   208 	  }
       
   209 	__LOG("\n DnsProxyEngine::FindVar Exit")
       
   210 	return EFalse;
       
   211 	}
       
   212 
       
   213 TBool CDnsProxyEngine::LoadConfigurationFile()
       
   214 /**
       
   215  * This will load the configuaration file into memory.
       
   216  * If it is already loaded then returns true otherwise loads the file
       
   217  * @param - None
       
   218  * @return boolean - retursn boolean value either true or false
       
   219  *
       
   220  * @internalTechnology
       
   221  **/
       
   222 
       
   223 	{
       
   224 	__LOG("\n CDnsProxyEngine::LoadConfigurationFile Entry")
       
   225 	if (iConfigData)
       
   226 		return TRUE;	// Already loaded!
       
   227 	if (iConfigDataErr)
       
   228 		return FALSE;
       
   229 	TRAP(iConfigDataErr, iConfigData = CESockIniData::NewL(DNS_PROXY_INI_DATA));
       
   230 	__LOG("\n CDnsProxyEngine::LoadConfigurationFile Exit")
       
   231 	return (iConfigData != NULL);
       
   232 	}
       
   233 
       
   234 void CDnsProxyEngine::UnLoadConfigurationFile()
       
   235 /**
       
   236  * This will unload the configuaration file from memory.
       
   237  * @param - None
       
   238  * @return - None
       
   239  *
       
   240  * @internalTechnology
       
   241  **/
       
   242 
       
   243 	{
       
   244 	__LOG("\n CDnsProxyEngine::UnloadConfigurationFile Entry")
       
   245 	delete iConfigData;
       
   246 	iConfigData = NULL;
       
   247 	iConfigDataErr = 0;
       
   248 	__LOG("\n CDnsProxyEngine::UnloadConfigurationFile Exit")
       
   249 	}
       
   250 
       
   251 TInt CDnsProxyEngine::GetIniValue(const TDesC &aSection, const TDesC &aName, TInt aValue)
       
   252 /**
       
   253  * This function reads the value for given name in the specified section
       
   254  * @param aSection - represents the section name in configuration file
       
   255  * @param aName - represents name in specified section
       
   256  * @param aValue - represents default value
       
   257  * @return boolean - retursn boolean value either true or false
       
   258  *
       
   259  * @internalTechnology
       
   260  **/
       
   261 
       
   262 	{
       
   263 	__LOG("\n CDnsProxyEngine::GetIniValue Entry")
       
   264 	LOG(_LIT(KFormat, "\t[%S] %S = %d"));
       
   265 	TInt value;
       
   266 	if (!FindVar(aSection, aName, value))
       
   267 		value = aValue;
       
   268     LOG(Log::Printf(KFormat, &aSection, &aName, value));
       
   269     __LOG1("CDnsProxyEngine::GetIniValue %d",value);
       
   270     
       
   271 	return value;
       
   272 	}
       
   273 
       
   274 TInt CDnsProxyEngine::AddDbEntryL(const RMessage2& aMsg)
       
   275 /**
       
   276  * This method updates DB.
       
   277  * @param aMessage - Message send by the client
       
   278  * @return - None
       
   279  *
       
   280  * @internalTechnology
       
   281  **/
       
   282 	{
       
   283 	if(!iDnsproxydb)
       
   284 	   return KErrBadHandle;
       
   285 	
       
   286 	TBuf8<KMaxName> dbdata;
       
   287 	TBuf16<KAddrLength> ipaddress;
       
   288 
       
   289 	aMsg.Read(0,ipaddress);
       
   290 	aMsg.Read(1,dbdata);
       
   291 
       
   292 	TInetAddr address;
       
   293     address.Input(ipaddress);
       
   294     
       
   295     TInt current_size = iDnsproxydb->GetDbSize();
       
   296     if(current_size < iConfigParams.iDbSize)
       
   297     	{
       
   298   		iDnsproxydb->UpdateDbL(dbdata,address);
       
   299     	}
       
   300     else
       
   301 	    {
       
   302 	    return KErrGeneral;	
       
   303 	    }	
       
   304 	__LOG("\n CDnsProxyEngine::AddDbEntryL Exit")
       
   305 	return KErrNone;
       
   306 	}
       
   307 
       
   308 TInt CDnsProxyEngine::RemoveDbEntry(const RMessage2& aMsg)
       
   309 /**
       
   310  * This method updates DB.
       
   311  * @param aMessage - Message send by the client
       
   312  * @return - None
       
   313  *
       
   314  * @internalTechnology
       
   315  **/
       
   316 	{
       
   317 	if(!iDnsproxydb)
       
   318 		return KErrBadHandle;
       
   319 
       
   320 	__LOG("\n CDnsProxyEngine::RemoveDbEntry Entry")
       
   321 	TBuf16<KAddrLength> ipaddress;
       
   322 
       
   323 	aMsg.Read(0,ipaddress);
       
   324 
       
   325 	TInetAddr address;
       
   326     address.Input(ipaddress);
       
   327     iDnsproxydb->DeleteDbEntry(address);
       
   328     __LOG("\n CDnsProxyEngine::RemoveDbEntry Exit")
       
   329     return KErrNone;
       
   330 	}
       
   331 
       
   332 void CDnsProxyEngine::UpdateLocalAddr(TInetAddr& aAddr)
       
   333 /**
       
   334  * This method updates interface address in the engine.
       
   335  * @param addr - Listen interface address
       
   336  * @return - None
       
   337  *
       
   338  * @internalTechnology
       
   339  **/
       
   340 	{
       
   341 	__LOG("\n CDnsProxyEngine::UpdateLocalAddr Entry/Exit")
       
   342 	iProxyIfAddr = aAddr;
       
   343 	}
       
   344 
       
   345 TInt CDnsProxyEngine::UpdateDomainInfo(const RMessage2& aMsg)
       
   346 /**
       
   347  * This method updates DB.
       
   348  * @param aMessage - Message send by the client
       
   349  * @return - None
       
   350  *
       
   351  * @internalTechnology
       
   352  **/
       
   353 	{
       
   354 	TInt err = KErrNone;
       
   355 	__LOG("\n CDnsProxyEngine::UpdateDomainInfo Entry")
       
   356 	err = aMsg.Read(0,iSuffixInfo);
       
   357 	return err;
       
   358 	}
       
   359 
       
   360     
       
   361 TInt CDnsProxyEngine::IsSuffixAvailable()
       
   362 /**
       
   363  * This method checks if suffix is available. 
       
   364  * @param  None
       
   365  * @return Length- Length of suffix data
       
   366  *
       
   367  * @internalTechnology
       
   368  **/
       
   369 
       
   370 	{
       
   371 	__LOG("\n CDnsProxyEngine::IsSuffixAvailable Entry/Exit")
       
   372 	return iSuffixInfo.Length();
       
   373 	}
       
   374     
       
   375 
       
   376 
       
   377 CDnsProxyEngine* CDnsProxyEngine::GetInstanceL()
       
   378     {
       
   379 /**
       
   380  * This method returns single ton pointer to CDnsProxyEngine 
       
   381  * @param  None
       
   382  * @return iUplinkConnection
       
   383  *
       
   384  * @internalTechnology
       
   385  **/    
       
   386     __LOG("\n CDnsProxyEngine::GetInstanceL Entry")    
       
   387     if(!iDnsProxyEngine)
       
   388         {
       
   389         __LOG("\n first time creation ")            
       
   390         iDnsProxyEngine = CDnsProxyEngine::NewL();
       
   391         }
       
   392     __LOG("\n CDnsProxyEngine::GetInstanceL  Exit")        
       
   393     return iDnsProxyEngine;
       
   394     }
       
   395 
       
   396 
       
   397 TInt CDnsProxyEngine::SetUplinkConnectionInfo(const RMessage2 &aMessage)
       
   398 /**
       
   399  * This method reads uplink connection info from the message. 
       
   400  * @param aMessage - Message send by the client
       
   401  * @return - None
       
   402  *
       
   403  * @internalTechnology
       
   404  **/
       
   405 
       
   406 	{
       
   407 	
       
   408 	__LOG("\n CDnsProxyEngine::SetUplinkConnectionInfo Entry")
       
   409 	
       
   410 	TInt err = iListener->SetUplinkConnectionInfo(aMessage);
       
   411 	__LOG("\n CDnsProxyEngine::SetUplinkConnectionInfo Exit")
       
   412 	return err;
       
   413 	
       
   414     }
       
   415     
       
   416 TInt CDnsProxyEngine::SetDlinkConnectionInfo(const RMessage2 &aMessage)
       
   417 /**
       
   418  * This method reads downlink connection info from the message. 
       
   419  * @param aMessage - Message send by the client
       
   420  * @return - None
       
   421  *
       
   422  * @internalTechnology
       
   423  **/
       
   424 
       
   425 	{
       
   426 	__LOG("\n CDnsProxyEngine::SetDlinkConnectionInfo Entry")
       
   427 	TInt err = iListener->SetDlinkConnectionInfo(aMessage);
       
   428 	__LOG("\n CDnsProxyEngine::SetDlinkConnectionInfo End")
       
   429 	return err;
       
   430 	}