tcpiputils/dhcp/src/DHCPServer.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-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 a Symbian OS server that exposes the RCDHCP API
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #include <e32svr.h>
       
    24 #include <e32base.h>
       
    25 #include <e32std.h>
       
    26 #include "DHCP_Std.h"
       
    27 #include "DHCPServer.h"
       
    28 #include "DHCPSess.h"
       
    29 #include "DHCPIP4Msg.h"
       
    30 #ifdef SYMBIAN_NETWORKING_PLATSEC
       
    31 #include <ecom/ecom.h>
       
    32 #include "DHCPPolicy.h"
       
    33 #endif
       
    34 
       
    35 #ifdef _DEBUG
       
    36 #include <e32property.h>
       
    37 #endif
       
    38 
       
    39 /**
       
    40  *  Holds current debug options of the server
       
    41  *  @internalComponent
       
    42  */
       
    43 TInt CDHCPServer::iDebugFlags(0);
       
    44 
       
    45 CDHCPServer::~CDHCPServer()
       
    46 /**
       
    47  *
       
    48  * Destructor
       
    49  *
       
    50  * @internalComponent
       
    51  */
       
    52 	{
       
    53 	__CFLOG_CLOSE;
       
    54 	__CFLOG_DELETE;
       
    55 	iEsock.Close();
       
    56 	delete iTimer;
       
    57 	}
       
    58 
       
    59 #ifdef SYMBIAN_NETWORKING_PLATSEC
       
    60 CDHCPServer::CDHCPServer(): CPolicyServer(EPriorityStandard,Policy,ESharableSessions)
       
    61 #elif defined(EKA2)
       
    62 CDHCPServer::CDHCPServer(): CServer2(EPriorityStandard)
       
    63 #else
       
    64 //On  EKA1, session is open in one thread and used in another
       
    65 CDHCPServer::CDHCPServer(): CServer2(EPriorityStandard, ESharableSessions)
       
    66 #endif
       
    67 /**
       
    68  * The CDHCPServer::CDHCPServer method
       
    69  *
       
    70  * Constructor
       
    71  *
       
    72  * @internalComponent
       
    73  */
       
    74 	{
       
    75 	}
       
    76 	
       
    77 CDHCPServer* CDHCPServer::NewL()
       
    78 /**
       
    79  * Creates and start the CServer2 derived server.
       
    80  * Called inside the MainL() function.
       
    81  *
       
    82  * @return - Instance of the server
       
    83  */
       
    84 	{
       
    85 	CDHCPServer* server = new (ELeave) CDHCPServer();
       
    86 	CleanupStack::PushL(server);
       
    87 	server->ConstructL();
       
    88 	// CServer2 base class call
       
    89 	server->StartL(KDHCPServerName);
       
    90 	CleanupStack::Pop(server);
       
    91 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPServer::NewL")));
       
    92 	return server;
       
    93 	}
       
    94 
       
    95 void CDHCPServer::ConstructL()
       
    96 /**
       
    97   * Second stage construction
       
    98   *
       
    99   * @internalTechnology
       
   100   *
       
   101   */
       
   102 	{
       
   103 	__CFLOG_CREATEL;
       
   104 	__CFLOG_OPEN;
       
   105 	User::LeaveIfError(iEsock.Connect());
       
   106 #ifdef _DEBUG
       
   107 	DebugFlags() = 0;
       
   108 #endif
       
   109 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPServer::ConstructL - connected to ESock")));
       
   110 	}
       
   111 
       
   112 
       
   113 CSession2* CDHCPServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
       
   114 /**
       
   115  *
       
   116  * Create a new If on this server
       
   117  *
       
   118  * @param	&aVersion	Vesion of server
       
   119  * @return	A new CDHCP session to be used for the connection
       
   120  *
       
   121  * @internalTechnology
       
   122  */
       
   123 	{
       
   124 //	TVersion v(KDHCPSrvMajorVersionNumber,KDHCPSrvMinorVersionNumber,KDHCPSrvBuildVersionNumber);
       
   125 
       
   126 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPServer::NewSessionL - creating new session...")));
       
   127 
       
   128 	//	if (!User::QueryVersionSupported(v, aVersion))
       
   129 //		User::Leave(KErrNotSupported);
       
   130 	CDHCPSession* ses = CDHCPSession::NewL();
       
   131 	SessionConnected();
       
   132 	return ses;
       
   133 	}
       
   134 	
       
   135 void CDHCPServer::SessionConnected() const
       
   136 /**
       
   137   * Called by session to alert server
       
   138   * that something has connected so
       
   139   * cancel the shutdown timer if there
       
   140   * is one running
       
   141   *
       
   142   */
       
   143   	{
       
   144 	if (iTimer)
       
   145 		{
       
   146 		iTimer->Cancel();
       
   147 		}  	
       
   148   	}
       
   149 	
       
   150 void CDHCPServer::Close(CDHCPSession* /*aDHCPSession*/)
       
   151 /**
       
   152   * Reference counting for the server.
       
   153   * When there are no sessions open then
       
   154   * we shutdown, unless the only one open
       
   155   * is oursleves, then we obviously still shutdown...
       
   156   *
       
   157   * @internalTechnology
       
   158   */
       
   159 	{
       
   160 	iSessionIter.SetToFirst();
       
   161 	
       
   162    	TInt count = 0;
       
   163    	while (iSessionIter++!=NULL)
       
   164    		{
       
   165    		count++;
       
   166    		}
       
   167 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPServer::Close - Current Session Count: (%d)") ,count));
       
   168 		         
       
   169 		         
       
   170 	// if we have one session then it is the current one
       
   171 	// and ok to shutdown...as this function is called
       
   172 	// before the session is destroyed...
       
   173 	if (count==1)
       
   174 		{
       
   175 		if (!iTimer)
       
   176 			{
       
   177 			TRAPD(ret, iTimer = CExpireTimer::NewL());
       
   178 			if (ret!=KErrNone)
       
   179 				{
       
   180 				// OOM so why not just shut down to free some
       
   181 				// seeing as that's the process that's been set
       
   182 				// in motion anyway...
       
   183 				TimerExpired();
       
   184 				return;
       
   185 				}
       
   186 			}			
       
   187 		// wait for 10 seconds before shutting down completely...
       
   188 		// in case anyone wants to connect			
       
   189 		iTimer->After(TTimeIntervalSeconds(KOneSecond/*10*/), *this);
       
   190 		}	
       
   191 	REComSession::FinalClose();
       
   192 	}
       
   193 	
       
   194 void CDHCPServer::TimerExpired()
       
   195 /**
       
   196   * CExpireTimer interface function to  alert server timer has popped
       
   197   *
       
   198   */
       
   199   	{
       
   200   	// just stop the server if we get here....
       
   201 	CActiveScheduler::Stop();
       
   202   	}
       
   203 
       
   204 TInt& CDHCPServer::DebugFlags()
       
   205 /**
       
   206   * Provides acess to debug flags
       
   207   */
       
   208 	{
       
   209 	return iDebugFlags;
       
   210 	}
       
   211 
       
   212 LOCAL_C void MainL()
       
   213 /**
       
   214  * Do the funky stuff, and use Rendezvous to signal completion...
       
   215  */
       
   216 	{
       
   217 	// Leave the hooks in for platform security
       
   218 #if (defined __DATA_CAGING__)
       
   219 	RProcess().DataCaging(RProcess::EDataCagingOn);
       
   220 	RProcess().SecureApi(RProcess::ESecureApiOn);
       
   221 #endif
       
   222 
       
   223 #if defined(__WINS__) && !defined(EKA2) //WINS emulator
       
   224 	User::LeaveIfError(RThread().Rename(KDHCPProcessName));
       
   225 #elif !defined(EKA2)
       
   226 	User::LeaveIfError(RProcess().Rename(KDHCPProcessName));
       
   227 #else //WINCW emulator or any target
       
   228 	User::LeaveIfError(RProcess::RenameMe(KDHCPProcessName));
       
   229 #endif
       
   230 
       
   231 #ifdef _DEBUG
       
   232 	TInt err = RProperty::Define(KMyPropertyCat, KMyPropertyDestPortv4, RProperty::EInt, TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
       
   233 				TSecurityPolicy(ECapabilityWriteDeviceData));
       
   234 	if (err == KErrNone)
       
   235 		{
       
   236 		// not defined by test code
       
   237 		RProperty::Define(KMyPropertyCat, KMyPropertyDestPortv6, RProperty::EInt, TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
       
   238 					TSecurityPolicy(ECapabilityWriteDeviceData));
       
   239 		RProperty::Set(KMyPropertyCat, KMyPropertyDestPortv4, 67);	// set to default values
       
   240 		RProperty::Set(KMyPropertyCat, KMyPropertyDestPortv6, 547);	// set to default values
       
   241 		RProperty::Define(KMyPropertyCat, KMyDefaultLeaseTime, RProperty::EInt, TSecurityPolicy(TSecurityPolicy::EAlwaysPass),
       
   242 					TSecurityPolicy(ECapabilityWriteDeviceData));
       
   243 		RProperty::Set(KMyPropertyCat, KMyDefaultLeaseTime, 21600);	// Define and set default values for server lease time
       
   244 		}
       
   245 #endif
       
   246 
       
   247 	CActiveScheduler* pScheduler = new(ELeave)CActiveScheduler;
       
   248 	CActiveScheduler::Install(pScheduler);
       
   249 	CleanupStack::PushL(pScheduler);
       
   250 	CDHCPServer* pServer = CDHCPServer::NewL();
       
   251 	CleanupStack::PushL(pServer);
       
   252 
       
   253 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("MainL - about to start activescheduler")));
       
   254 	// Sync with the starter and enter the active scheduler
       
   255 #if defined(__WINS__) && !defined(EKA2)
       
   256 	RThread::Rendezvous(KErrNone); //WINS emulator
       
   257 #else	//WINSCW emulator or any target
       
   258 	RProcess::Rendezvous(KErrNone);
       
   259 #endif
       
   260 	CActiveScheduler::Start();
       
   261 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("MainL - activescheduler has stopped...")));
       
   262 	CleanupStack::PopAndDestroy(pServer);
       
   263 	CleanupStack::PopAndDestroy(pScheduler);
       
   264 	}
       
   265 
       
   266 
       
   267 EXPORT_C TInt E32Main()
       
   268 /**
       
   269  * The main thread function, the Ordinal 1!
       
   270  * @return - Standard Epoc error code on exit
       
   271  */
       
   272 	{	
       
   273 __UHEAP_MARK;
       
   274 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   275 	if (cleanup == NULL)
       
   276 		{
       
   277 		return KErrNoMemory;
       
   278 		}
       
   279 	TInt err = KErrNone;
       
   280 	TRAP(err,MainL());
       
   281 	delete cleanup;
       
   282 __UHEAP_MARKEND;
       
   283 	return err;
       
   284     }
       
   285     
       
   286 #if !defined(EKA2)
       
   287 GLDEF_C TInt E32Dll(enum TDllReason)
       
   288 	{
       
   289 	return 0;
       
   290 	}
       
   291 #endif