networkprotocols/ipeventnotifier/src/ipeventnotifier.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 //
       
    15 
       
    16 /**
       
    17  @file ipeventnotifier.cpp
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 
       
    22 #include <in_chk.h>
       
    23 #include <icmp6_hdr.h>
       
    24 #include <in_sock.h>
       
    25 #include <in_bind.h>
       
    26 #include "in6_opt.h"
       
    27 
       
    28 #include "ipeventnotifier.h"
       
    29 #include "HookLog.h"
       
    30 #include <networking/ipeventtypes.h>
       
    31 #include "ipeventnotifierinterface.h"
       
    32 #include "ipeventlistener.h"
       
    33 #include <comms-infras/idquerynetmsg.h>
       
    34 #include "DHCPUnicastTranslator.h"
       
    35 
       
    36 
       
    37 CIPEventNotifier* CIPEventNotifier::NewL()
       
    38 	{
       
    39 	CIPEventNotifier* self = new(ELeave) CIPEventNotifier();
       
    40 	CleanupStack::PushL(self);
       
    41 
       
    42 	self->ConstructL();
       
    43 
       
    44 	CleanupStack::Pop();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 
       
    49 
       
    50 void CIPEventNotifier::ConstructL()
       
    51 	{
       
    52 	/**
       
    53 	 * Create the address/interface listener.
       
    54 	 */
       
    55 	iIPEventListener = new (ELeave) CIPEventListener(*this);
       
    56 	}
       
    57 
       
    58 
       
    59 CIPEventNotifier::CIPEventNotifier()
       
    60 	{
       
    61 	}
       
    62 
       
    63 
       
    64 
       
    65 CIPEventNotifier::~CIPEventNotifier()
       
    66 	{
       
    67 	iInterfaces.ResetAndDestroy();
       
    68 
       
    69 	delete iIPEventListener;
       
    70 	
       
    71 	delete iDHCPUnicastTranslator;
       
    72 	}
       
    73 
       
    74 
       
    75 
       
    76 void CIPEventNotifier::BindL(CProtocolBase* aProtocol, TUint aId)
       
    77 	{
       
    78 	// Do sanity checks
       
    79 	//
       
    80 	if ((aId != KProtocolInet6Ip) || (aProtocol == this))
       
    81 		{
       
    82 		User::Leave(KErrArgument);
       
    83 		}
       
    84 
       
    85 	TUint ourId;
       
    86 		{
       
    87 		TServerProtocolDesc info;
       
    88 		Identify(&info);
       
    89 		ourId = info.iProtocol;
       
    90 		}
       
    91 
       
    92 	if (aId == ourId)
       
    93 		{
       
    94 		User::Leave(KErrArgument);
       
    95 		}
       
    96 
       
    97 	if ( iProtocolIPv6 != NULL )
       
    98 		{
       
    99 		if ( iProtocolIPv6 == aProtocol )
       
   100 			{
       
   101 			// We don't need to bind to the same protocol twice.
       
   102 			//
       
   103 			return;
       
   104 			}
       
   105 		else
       
   106 			{
       
   107 			// We don't want to bind to a different protocol either...
       
   108 			//
       
   109 			User::Leave(KErrAlreadyExists);
       
   110 			}
       
   111 		}
       
   112 
       
   113 	iProtocolIPv6 = (CProtocolInet6Binder*) aProtocol;
       
   114 
       
   115 	// Get a pointer to the event service.
       
   116 	//
       
   117 	ObtainEventService();
       
   118 
       
   119 	// Must always register hooks on startup, til the required
       
   120 	//  initial values have been received.
       
   121 	//
       
   122 	RegisterHooksL();
       
   123 	
       
   124 	// Create cache of interfaces known to IP stack
       
   125 	//
       
   126 	AddKnownInterfacesL();
       
   127 
       
   128 	/**
       
   129 	 * Create owned hook to allow unicast DHCP packets to pass.
       
   130 	 * Notice we are bypassing the standard mechanism for including
       
   131 	 *  another protocol hook (esk files), and are directly creating
       
   132 	 *  it here as it is needed for the same lifetime as IPEN itself.
       
   133 	 */
       
   134 	if (iDHCPUnicastTranslator == NULL)
       
   135 		{
       
   136 		iDHCPUnicastTranslator = new (ELeave) CDHCPUnicastTranslator(*iProtocolIPv6);
       
   137 		}
       
   138 	// connect the hook to the stack..
       
   139 	iProtocolIPv6->BindL(iDHCPUnicastTranslator, BindForwardHook());
       
   140 	
       
   141 	}
       
   142 
       
   143 
       
   144 
       
   145 void CIPEventNotifier::Unbind(CProtocolBase* aProtocol, TUint /* aId */)
       
   146 	{
       
   147 	if (iProtocolIPv6 != aProtocol)
       
   148 		{
       
   149 		return;
       
   150 		}
       
   151 
       
   152 	iProtocolIPv6->Unbind(iDHCPUnicastTranslator, 0);
       
   153 
       
   154 	UnregisterHooks();
       
   155 
       
   156 	iProtocolIPv6 = 0;
       
   157 	iEventService = 0;
       
   158 	}
       
   159 	
       
   160 
       
   161 
       
   162 TInt CIPEventNotifier::GetOption(TUint aLevel, TUint aName,TDes8& aOption,CProtocolBase* aSourceProtocol)
       
   163 /**
       
   164  * Performs one of the following actions, depending on the option name:
       
   165  *
       
   166  * KHandleAttach: Sets up IPEN to catch and publish events on the interface named in aOption.
       
   167  *                  Session handle TUint (the pubsub address key) returned in first 4 bytes of aOption buffer.
       
   168  *
       
   169  * KHandleRelease: Tells IPEN to stop catching and publishing events to the given session handle
       
   170  *                   Handle passed in as TUint in first 4 bytes of aOption buffer.
       
   171  *
       
   172  * @param aLevel: should be KSolInetIp for this protocol
       
   173  * @param aName: see above
       
   174  * @param aOption: in/out, dependent on option name. See above
       
   175  * @return error status
       
   176  *
       
   177  */
       
   178 	{
       
   179 	(void) aLevel;
       
   180 	(void) aSourceProtocol;
       
   181 
       
   182 	TInt retVal = KErrNotSupported;
       
   183 		
       
   184 	switch (aName)
       
   185 		{
       
   186 		case NetMessages::KHandleAttach:
       
   187 		    {
       
   188 			TName buf;
       
   189 			buf.Copy( aOption );
       
   190 			TUint *out_handlenum = (TUint *)aOption.Ptr();
       
   191 			TRAPD(retval, *out_handlenum = OpenSessionL(buf));
       
   192 			return retval;
       
   193 			}
       
   194 
       
   195 		case NetMessages::KHandleRelease:
       
   196 			{
       
   197 			TUint *in_handle = (TUint *)aOption.Ptr();
       
   198 			TInt ret = CloseSession(*in_handle);
       
   199 			*in_handle = 0;
       
   200 			return ret;
       
   201 		    }
       
   202 			
       
   203 	    default:
       
   204 	        break;
       
   205 		}
       
   206 
       
   207 	return retVal ;
       
   208 	}
       
   209 
       
   210 
       
   211 
       
   212 TUint CIPEventNotifier::OpenSessionL(const TDesC& aIfName)
       
   213 /**
       
   214   * Sets up IPEN to catch and publish events on the interface named by aIfName.
       
   215   *                  Session handle TUint (the pubsub address key) is returned.
       
   216   *
       
   217   * @internalTechnology
       
   218   */
       
   219 	{
       
   220 	// The IP stack's first interface, a dummy, has an empty name string.
       
   221 	// Therefore an empty string to open against would match this dummy
       
   222 	//  interface! So we should report failure if a naughty client is trying to
       
   223 	//  monitor events on an interface with an empty name string.
       
   224 	//
       
   225 	if(aIfName == KNullDesC)
       
   226 		{
       
   227 		LOG(
       
   228 			_LIT(KHookNotifyStr,"ERROR: client attempted to monitor interface with empty name");
       
   229 			HookLog::Printf( KHookNotifyStr );
       
   230 			)
       
   231 		User::Leave(KErrBadName);
       
   232 		}
       
   233 	
       
   234 	TInt ifPos = FindPositionOfInterfaceByName(aIfName);
       
   235 
       
   236 	CIPEventNotifierInterface* iface = 0;
       
   237 
       
   238 	if(ifPos == KErrNotFound)
       
   239 		{
       
   240 		AddKnownInterfacesL();
       
   241 		ifPos = FindPositionOfInterfaceByName(aIfName);
       
   242 		User::LeaveIfError(ifPos);
       
   243 		}
       
   244 	
       
   245 	iface = iInterfaces[ifPos];
       
   246 	
       
   247 	if( AnyClientsCurrentlyInterested() == false )
       
   248 		{
       
   249 		RegisterHooksL();
       
   250 		}
       
   251 		
       
   252 	__ASSERT_ALWAYS(iface , User::Leave(KErrCorrupt));
       
   253 	iface->IncreaseRefCount();
       
   254 	
       
   255 	PublishLastKnownStates(iface);
       
   256 
       
   257 	LOG(
       
   258 		_LIT(KHookNotifyStr2,"CIPEventNotifier::OpenSessionL - now monitoring interface %S (pos #%d)");
       
   259 		HookLog::Printf(KHookNotifyStr2,&aIfName,ifPos);
       
   260 		)
       
   261 		
       
   262 	return iface->GetInterfaceIndex(); // later we might create a session id and map it to i/f #
       
   263 	}
       
   264 
       
   265 	
       
   266 TInt CIPEventNotifier::CloseSession(TUint aHandle)
       
   267 	{
       
   268 	TInt acIfPos = FindPositionOfInterface(aHandle);
       
   269 
       
   270 	if(acIfPos == KErrNotFound)
       
   271 		{
       
   272 		return KErrNotFound;
       
   273 		}
       
   274 
       
   275 	CIPEventNotifierInterface* acIf = iInterfaces[acIfPos];
       
   276 
       
   277 	acIf->DecreaseRefCount();
       
   278 	
       
   279 	// shouldn't be more closes than news! naughty client.
       
   280 	__ASSERT_ALWAYS((acIf->GetRefCount() >= 0), User::Panic(_L("IPEN:CloseSession"),1));
       
   281 	
       
   282 	LOG(
       
   283 		_LIT(KHookNotifyStr2,"CIPEventNotifier::CloseSession handle %d (pos #%d)");
       
   284 		HookLog::Printf(KHookNotifyStr2,aHandle,acIfPos);
       
   285 		)	
       
   286 	
       
   287 	if( AnyClientsCurrentlyInterested() == false )
       
   288 		{
       
   289 		UnregisterHooks();
       
   290 		}
       
   291 
       
   292 	return KErrNone;
       
   293 	}
       
   294 
       
   295 
       
   296 
       
   297 void CIPEventNotifier::AddInterfaceL(TUint32 aIfIndex, const TDesC& aIfName)
       
   298 	{
       
   299 	CIPEventNotifierInterface* newActIf = new (ELeave)CIPEventNotifierInterface(aIfIndex, aIfName);
       
   300 	CleanupStack::PushL( newActIf );
       
   301 	iInterfaces.AppendL(newActIf);
       
   302 	CleanupStack::Pop( newActIf );
       
   303 	}
       
   304 
       
   305 
       
   306 
       
   307 void CIPEventNotifier::AddKnownInterfacesL()
       
   308     {
       
   309     TUint   bufsize = 2048;
       
   310     HBufC8 *buffer = 0;
       
   311     TInt exceed;
       
   312     buffer = GetBuffer(buffer, bufsize);
       
   313     User::LeaveIfNull(buffer);
       
   314     TPtr8 bufdes = buffer->Des();
       
   315     
       
   316 	do
       
   317 		{
       
   318 	    //-- get list of network interfaces
       
   319 	    // if exceed>0, all interface could not fit into the buffer.
       
   320 	    // In that case allocate a bigger buffer and retry.
       
   321 	    // There should be no reason for this while loop to take more than 2 rounds.
       
   322 	    bufdes.Set(buffer->Des());
       
   323 	    exceed = iProtocolIPv6->GetOption(KSolInetIfQuery, KSoInetInterfaceInfo, bufdes);
       
   324 	    if(exceed > 0)
       
   325 	        {
       
   326 	        bufsize += exceed * sizeof(TInetInterfaceInfo);
       
   327 	        buffer = GetBuffer(buffer, bufsize);
       
   328 	        User::LeaveIfNull(buffer);
       
   329 	        }
       
   330 	    } while (exceed > 0);
       
   331     
       
   332     TOverlayArray<TInetInterfaceInfo> infoIface(bufdes);
       
   333     
       
   334     TInt idx;
       
   335     for(idx=0; idx < infoIface.Length(); ++idx)
       
   336         {
       
   337         TInetInterfaceInfo& iface = infoIface[idx];
       
   338 
       
   339 		if(FindPositionOfInterface(iface.iIndex) == KErrNotFound)
       
   340 			{
       
   341 			LOG(
       
   342 		        TBuf<2048> tmpBuf;
       
   343 	        	tmpBuf.Format(_L("Detected new i/f: index:%d, name: "),iface.iIndex);
       
   344 		        tmpBuf.Append(iface.iName);
       
   345 		        tmpBuf.AppendFormat(_L(", state:%d, SMti:%d, RMtu:%d, SpeedMetric:%d, feat:%d,"),
       
   346 		        			iface.iState,iface.iSMtu,iface.iRMtu,iface.iSpeedMetric,iface.iFeatures/*,iface.iHwAddr*/);
       
   347 		        HookLog::Printf(tmpBuf);
       
   348 	        	)
       
   349 
       
   350 			TRAP_IGNORE(AddInterfaceL(iface.iIndex,iface.iName));
       
   351 			}
       
   352         }
       
   353 	LOG(LogKnownInterfaces());
       
   354 	delete buffer;
       
   355     }
       
   356 
       
   357 
       
   358 HBufC8* CIPEventNotifier::GetBuffer(HBufC8* apBuf, TInt aBufLenRequired)
       
   359     {
       
   360     HBufC8* pBufResult = NULL;
       
   361     
       
   362     if(aBufLenRequired <= 0 ) 
       
   363         return NULL; //-- invalid argument
       
   364     
       
   365     if(!apBuf)
       
   366         {//-- the buffer is not allocated at all, try allocate it
       
   367         pBufResult = HBufC8::New(aBufLenRequired);
       
   368         }
       
   369     else
       
   370         {//-- the buffer is already allocated. Check its size and try to reallocate if
       
   371         //-- required size is bigger than existing. Otherwise do nothing.   
       
   372         
       
   373         if(apBuf->Des().MaxSize() >= aBufLenRequired)
       
   374             pBufResult = apBuf; //-- do nothing, existing buffer is big enough
       
   375         else
       
   376             {  //-- delete and allocate new buffer to avoid data copying
       
   377             delete apBuf;
       
   378             pBufResult = HBufC8::New(aBufLenRequired);
       
   379             }
       
   380         }
       
   381     
       
   382     return pBufResult;
       
   383     }
       
   384 
       
   385 
       
   386 TInt CIPEventNotifier::FindPositionOfInterface(TUint32 aIfIndex) const
       
   387 	{
       
   388 	TInt i;
       
   389 	for( i=0 ; i<iInterfaces.Count() /*inline call*/ ; ++i )
       
   390 		{
       
   391 		if(iInterfaces[i]->GetInterfaceIndex() == aIfIndex)
       
   392 			{
       
   393 			return i;
       
   394 			}
       
   395 		}
       
   396 	return KErrNotFound;
       
   397 	}
       
   398 
       
   399 
       
   400 
       
   401 TInt CIPEventNotifier::FindPositionOfInterfaceByName(const TDesC& aIfName) const
       
   402 	{
       
   403 	TInt i;
       
   404 	for( i=0 ; i<iInterfaces.Count() ; ++i )
       
   405 		{
       
   406 		if(iInterfaces[i]->GetInterfaceName() == aIfName)
       
   407 			{
       
   408 			return i;
       
   409 			}
       
   410 		}
       
   411 
       
   412 	return KErrNotFound;
       
   413 	}
       
   414 
       
   415 
       
   416 CIPEventNotifierInterface* CIPEventNotifier::GetInterfaceWithIndexL(TInt aIpIfIndex)
       
   417 	{
       
   418 		TInt aIfPos;
       
   419 		aIfPos = FindPositionOfInterface(aIpIfIndex);
       
   420 		if(aIfPos < 0)
       
   421 			{
       
   422 			LOG (
       
   423 				_LIT(KHookNotifyStr,"ERROR: couldn't find interface %d. err %d");
       
   424 				HookLog::Printf( KHookNotifyStr,aIpIfIndex, aIfPos);
       
   425 				)
       
   426 			User::Leave(aIfPos);
       
   427 			}
       
   428 		
       
   429 		return iInterfaces[aIfPos];
       
   430 	}
       
   431 
       
   432 
       
   433 TBool CIPEventNotifier::AnyClientsCurrentlyInterested(void) const
       
   434 	{
       
   435 #ifdef ALWAYS_PUBLISH_IP_EVENTS
       
   436 	return true;
       
   437 #else
       
   438 	TInt i;
       
   439 	for( i=0 ; i<iInterfaces.Count() ; ++i )
       
   440 		{
       
   441 		if(iInterfaces[i]->GetRefCount() != 0       ||
       
   442 		   iInterfaces[i]->iMFlagReceived == 0         ||
       
   443 		   iInterfaces[i]->iLinklocalAddressKnown == 0   )
       
   444 		   		// (We must always listen until we've received the M flag and a LL addr.
       
   445 		   		//  This is because DHCP must have access to these events, even if it hasn't started yet.)
       
   446 			{
       
   447 			return true;
       
   448 			}
       
   449 		}
       
   450 	return false;
       
   451 #endif
       
   452 	}
       
   453 
       
   454 
       
   455 TBool CIPEventNotifier::ShouldPublishEvent(TUint aInterfaceIndex)
       
   456 	{
       
   457 	TInt aIfPos;
       
   458 	aIfPos = FindPositionOfInterface(aInterfaceIndex);
       
   459 	if(aIfPos == KErrNotFound)
       
   460 		{
       
   461 		LOG ( HookLog::Printf(_L("UNEXPECTED: Interface %d not yet known. Trying to find it"),aInterfaceIndex); )
       
   462 		TRAP_IGNORE(AddKnownInterfacesL())
       
   463 		aIfPos = FindPositionOfInterface(aInterfaceIndex);
       
   464 		if(aIfPos == KErrNotFound)
       
   465 			{
       
   466 			LOG ( HookLog::Printf(_L("VERY UNEXPECTED: Couldn't add interface %d"),aInterfaceIndex); )
       
   467 			return EFalse;
       
   468 			}
       
   469 		aIfPos = FindPositionOfInterface(aInterfaceIndex);
       
   470 		}
       
   471 	
       
   472 #ifdef ALWAYS_PUBLISH_IP_EVENTS
       
   473     return ETrue;
       
   474 #else
       
   475 	CIPEventNotifierInterface* aIf = iInterfaces[aIfPos];
       
   476 	// if the M flag / LL addr has not yet been received we should try to catch it
       
   477 	return( (aIf->iMFlagReceived == 0)  ||
       
   478 	        (aIf->iLinklocalAddressKnown == 0)  ||
       
   479 	        (aIf->GetRefCount() > 0)      );
       
   480 	    
       
   481 #endif
       
   482 	}
       
   483 
       
   484 
       
   485 void CIPEventNotifier::InterfaceDetached(const TDesC & aName, CNifIfBase *aIf)
       
   486 	{
       
   487 	(void)aIf;
       
   488 	TInt pos = FindPositionOfInterfaceByName(aName);
       
   489 	LOG(
       
   490 		_LIT(KHookNotifyStr,"CIPEventNotifier::InterfaceDetached %S ptr=%08x [pos %d of %d interfaces]");
       
   491 		HookLog::Printf(KHookNotifyStr,&aName,aIf,pos,iInterfaces.Count());
       
   492 		)	
       
   493 	if(pos != KErrNotFound)
       
   494 		{
       
   495 		CIPEventNotifierInterface* notIf = iInterfaces[pos];
       
   496 		delete notIf;
       
   497 		iInterfaces.Remove(pos);
       
   498 		}
       
   499 	LOG(LogKnownInterfaces());
       
   500 	}
       
   501 	
       
   502 #ifdef _DEBUG
       
   503 void CIPEventNotifier::LogKnownInterfaces()
       
   504 	{
       
   505 	LOG(
       
   506 		_LIT(KHookNotifyHeader,"Dumping iInterfaces:");
       
   507 		HookLog::Printf(KHookNotifyHeader);
       
   508 		)
       
   509 			
       
   510 	for(TInt i=0 ; i<iInterfaces.Count() ; ++i )
       
   511 		{
       
   512 		LOG(
       
   513 			_LIT(KHookNotifyStr,"\tPos %d: %S, stack's index %d");
       
   514 			TName name = iInterfaces[i]->GetInterfaceName();
       
   515 			TUint32 idx = iInterfaces[i]->GetInterfaceIndex();
       
   516 			HookLog::Printf(KHookNotifyStr, i, &name, idx);
       
   517 			)	
       
   518 		}
       
   519 	}
       
   520 #endif	
       
   521 
       
   522 
       
   523 void CIPEventNotifier::RegisterHooksL(void)
       
   524 /**
       
   525  * Registers the hook to catch IP stack events
       
   526  */
       
   527 	{
       
   528 	if(!(iProtocolIPv6 && iEventService && iIPEventListener))
       
   529 		{
       
   530 		User::Leave(KErrNotReady);
       
   531 		}
       
   532 
       
   533 	// bind to IP for incoming packets
       
   534 	iProtocolIPv6->BindL((CProtocolBase*)this, BindHookAll());
       
   535 
       
   536 
       
   537 	// Register our listener class with the event service. We're interested in:
       
   538 	//
       
   539 	// i.  address events
       
   540 	//     for determining when DAD has completed and when link-local addresses have been assigned
       
   541 	//     to an interface.
       
   542 	// ii. interface events to allow the notifier to associate interface names with the stack's
       
   543 	//     internal indices.
       
   544 	//
       
   545 	iEventService->RegisterListener(iIPEventListener, EClassAddress);
       
   546 	iEventService->RegisterListener(iIPEventListener, EClassInterface);
       
   547 	}
       
   548 
       
   549 
       
   550 	
       
   551 void CIPEventNotifier::UnregisterHooks(void)
       
   552 /**
       
   553  * Detaches this hook from the running of the stack.
       
   554  *  This is so IPEN doesn't slow down the stack while it isn't
       
   555  *   needed by any clients.
       
   556  *
       
   557  *  e.g. without this mechanism, every incoming packet would pass through
       
   558  *   CIPEventNotifier::ApplyL pointlessly.
       
   559  *
       
   560  */
       
   561 	{
       
   562 	if(!(iProtocolIPv6 && iEventService && iIPEventListener))
       
   563 		{
       
   564 		return;
       
   565 		}
       
   566 
       
   567 	// If IPv6 exists, unbind the hook. Covers both in/out hooks (I hope)
       
   568 	iProtocolIPv6->Unbind(this);
       
   569 
       
   570 	// Unregister the address/interface listener.
       
   571 	iEventService->RemoveListener(iIPEventListener);
       
   572 	}
       
   573 
       
   574 	
       
   575 
       
   576 void CIPEventNotifier::FillIdentification(TServerProtocolDesc& anEntry)
       
   577 /**
       
   578  * Fills in an existing protocol description structure with details of this protocol.
       
   579  *
       
   580  * @param aProtocolDesc: reference to the structure to be filled in
       
   581  */
       
   582 	{
       
   583 	anEntry.iName=_S("ipen");
       
   584 	anEntry.iAddrFamily=KAfInet; //KAfExain;
       
   585 	anEntry.iSockType=KSockDatagram;
       
   586 	anEntry.iProtocol=KProtocolId;
       
   587 	anEntry.iVersion=TVersion(1, 0, 0);
       
   588 	anEntry.iByteOrder=EBigEndian;
       
   589 	anEntry.iServiceInfo=KSIDatagram | KSIConnectionLess;
       
   590 	anEntry.iNamingServices=0;
       
   591 	anEntry.iSecurity=KSocketNoSecurity;
       
   592 	anEntry.iMessageSize=0xffff;
       
   593 	anEntry.iServiceTypeInfo=0; // 1 for ability to create sockets
       
   594 	anEntry.iNumSockets=KUnlimitedSockets;
       
   595 	}
       
   596 
       
   597 
       
   598 void CIPEventNotifier::Identify(TServerProtocolDesc* aProtocolDesc) const
       
   599 /**
       
   600  * Fills in an existing protocol description structure with details of this protocol.
       
   601  *
       
   602  * @param aProtocolDesc: pointer to the structure to be filled in
       
   603  */
       
   604 	{
       
   605 	FillIdentification(*aProtocolDesc);
       
   606 	}
       
   607 
       
   608 
       
   609 TInt CIPEventNotifier::DefineProperty(Meta::SMetaData& aEvent, TUint aHandle)
       
   610 	{
       
   611 	TSecurityPolicy readPol(TSecurityPolicy::EAlwaysPass);
       
   612 	_LIT_SECURE_ID(ipeventsid,0x102045B9);
       
   613 	_LIT_SECURITY_POLICY_S0(writePol,ipeventsid);
       
   614 
       
   615 	TInt err = iPublisher.Define(TUid::Uid(aEvent.GetTypeId().iType), aHandle,
       
   616 						RProperty::EByteArray/*, readPol, writePol*/);
       
   617 						
       
   618 	// Does not matter if property has already been defined - we ignore the error.
       
   619 	if( err == KErrAlreadyExists )
       
   620 		{
       
   621 		err = KErrNone;
       
   622 		}
       
   623 		
       
   624 	return err;
       
   625 	}
       
   626 
       
   627 
       
   628 void CIPEventNotifier::PublishToHandle(TUint aHandle, Meta::SMetaData& aData)
       
   629 	{
       
   630 	TBuf8<RProperty::KMaxPropertySize> buf;
       
   631 
       
   632 	TInt res =  aData.Store(buf);
       
   633 	if(res != KErrNone)
       
   634 		{
       
   635 		LOG(HookLog::Printf( _L("IPEN can't store: %d"), res));
       
   636 		return;
       
   637 		}
       
   638 
       
   639 	TPtrC8 toSet = buf;
       
   640 	res = iPublisher.Set(TUid::Uid(aData.GetTypeId().iType), aHandle, toSet);
       
   641 	if(res != KErrNone)
       
   642 		{
       
   643 		LOG(HookLog::Printf( _L("IPEN can't publish: %d"), res));
       
   644 		}
       
   645 	}
       
   646 
       
   647 
       
   648 
       
   649 /**
       
   650  * Incoming packet
       
   651  */
       
   652 TInt CIPEventNotifier::ApplyL(RMBufHookPacket& aPacket, RMBufRecvInfo& aInfo)
       
   653 	{
       
   654 
       
   655 	LOG	(
       
   656 		_LIT(KHookNotifyStr,"CIPEventNotifier::ApplyL hit with protocol: %d");
       
   657 		HookLog::Printf( KHookNotifyStr , aInfo.iProtocol );
       
   658 		)
       
   659 
       
   660 	if(aInfo.iProtocol == static_cast<TInt>(KProtocolInet6Icmp) &&  // quickest first
       
   661 		ShouldPublishEvent(aInfo.iInterfaceIndex) )
       
   662 		{
       
   663 
       
   664 		LOG	(
       
   665 			_LIT(KHookNotifyStr," ICMPv6 packet detected");
       
   666 			HookLog::Printf( KHookNotifyStr );
       
   667 			)
       
   668 
       
   669 		/**
       
   670 		 * Could switch on ICMP type here, in case the hook needs to catch
       
   671 		 *   other ICMP packet types..
       
   672 		 */
       
   673 		TInet6Checksum<TInet6HeaderICMP_RouterAdv> icmp(aPacket,aInfo.iOffset);
       
   674 
       
   675 		if(icmp.iHdr)
       
   676 			{
       
   677 			if (icmp.iHdr->Type() == KInet6ICMP_RouterAdv &&
       
   678 				icmp.VerifyChecksum( aPacket,
       
   679 							  aInfo.iProtocol == static_cast<TInt>(KProtocolInet6Icmp) ? &aInfo : NULL,
       
   680 							  aInfo.iOffset) )
       
   681 				{
       
   682 				TBool mflag = (icmp.iHdr->M() ? ETrue : EFalse) ;
       
   683 				TBool oflag = (icmp.iHdr->O() ? ETrue : EFalse) ;
       
   684 
       
   685 				LOG	(
       
   686 					_LIT(KHookNotifyStr," IP EVENT on if# %d: Publishing M flag: %d");
       
   687 					HookLog::Printf( KHookNotifyStr, aInfo.iInterfaceIndex, mflag );
       
   688 					_LIT(KHookOFlagNotifyStr," IP EVENT on if# %d: Publishing O flag: %d");
       
   689 					HookLog::Printf( KHookOFlagNotifyStr, aInfo.iInterfaceIndex, oflag );
       
   690 					)
       
   691 
       
   692 				TRAPD(err, PublishMFlagL(aInfo.iInterfaceIndex, mflag, oflag));
       
   693 				if(err!=KErrNone)
       
   694 					{
       
   695 					LOG	(
       
   696 					_LIT(KLogStr, "Can't publish M and O flags message! Err %d\n");
       
   697 					HookLog::Printf(KLogStr, err);
       
   698 					)
       
   699 					}
       
   700 				}
       
   701 			}
       
   702 
       
   703 		}
       
   704 			
       
   705 	return KIp6Hook_PASS;   // ensure we don't affect the control flow
       
   706 	}
       
   707 
       
   708 
       
   709 
       
   710 void CIPEventNotifier::ObtainEventService()
       
   711 	{
       
   712 	MInterfaceManager* ifacer = iProtocolIPv6->NetworkService()->Interfacer();
       
   713 
       
   714 	TRAPD(err, iEventService = IMPORT_API_L (ifacer, MEventService));
       
   715 	if (err != KErrNone)
       
   716 		{
       
   717 		LOG	(
       
   718 			_LIT(KHookNotifyStr, "MEventService not available (%d). Some features may be missing\n");
       
   719 			HookLog::Printf(KHookNotifyStr, err);
       
   720 			)
       
   721 		}
       
   722 	}
       
   723 
       
   724 
       
   725 
       
   726 
       
   727 void CIPEventNotifier::PublishMFlagL(TInt aIpIfIndex, TBool aMflag, TBool aOflag)
       
   728 	{
       
   729 	CIPEventNotifierInterface* iface = GetInterfaceWithIndexL(aIpIfIndex);
       
   730 
       
   731 	if(iface)
       
   732 		{
       
   733 		if(iface->iMFlagReceived == 0)
       
   734 			{
       
   735 			iface->iMFlagReceived = CMFlagReceived::NewL();
       
   736 			User::LeaveIfError(DefineProperty(*(iface->iMFlagReceived), aIpIfIndex));
       
   737 			}
       
   738 		
       
   739 		iface->iMFlagReceived->SetMFlag(aMflag);
       
   740 		iface->iMFlagReceived->SetOFlag(aOflag);
       
   741 		PublishToHandle(aIpIfIndex, *(iface->iMFlagReceived));
       
   742 		}
       
   743 	}
       
   744 
       
   745 		
       
   746 void CIPEventNotifier::PublishIPReadyL(TInt aIpIfIndex, const TInetAddr& tmpInetAddr, TBool addressValid)
       
   747 	{
       
   748 	CIPEventNotifierInterface* iface = GetInterfaceWithIndexL(aIpIfIndex);
       
   749 
       
   750 	if(iface)
       
   751 		{
       
   752 		if(iface->iIPReady == 0)
       
   753 			{
       
   754 			iface->iIPReady = CIPReady::NewL();			
       
   755 			User::LeaveIfError(DefineProperty(*(iface->iIPReady), aIpIfIndex));
       
   756 			}
       
   757 		
       
   758 		iface->iIPReady->SetIPAddress(tmpInetAddr);
       
   759 		iface->iIPReady->SetAddressValid(addressValid);
       
   760 		PublishToHandle(aIpIfIndex, *(iface->iIPReady));
       
   761 		}
       
   762 	}
       
   763 
       
   764 void CIPEventNotifier::PublishLinklocalAddressKnownL(TInt aIpIfIndex, const TInetAddr& tmpInetAddr)
       
   765 	{
       
   766 	CIPEventNotifierInterface* iface = GetInterfaceWithIndexL(aIpIfIndex);
       
   767 
       
   768 	if(iface)
       
   769 		{
       
   770 		if(iface->iLinklocalAddressKnown == 0)
       
   771 			{
       
   772 			iface->iLinklocalAddressKnown = CLinklocalAddressKnown::NewL();
       
   773 			User::LeaveIfError(DefineProperty(*(iface->iLinklocalAddressKnown), aIpIfIndex));
       
   774 			}			
       
   775 		iface->iLinklocalAddressKnown->SetIPAddress(tmpInetAddr);
       
   776 		PublishToHandle(aIpIfIndex, *(iface->iLinklocalAddressKnown));
       
   777 		}
       
   778 	}
       
   779 
       
   780 
       
   781 void CIPEventNotifier::PublishLastKnownStates(CIPEventNotifierInterface* aIface)
       
   782 {
       
   783 	TUint32 ifIdx = aIface->GetInterfaceIndex();
       
   784 	
       
   785 	// For each message from which we need state, not just events
       
   786 	//  i.e. everything that might happen before DHCP starts and registers
       
   787 	//
       
   788 	if(aIface->iMFlagReceived)
       
   789 		{
       
   790 		PublishToHandle(ifIdx, *(aIface->iMFlagReceived));
       
   791 		}
       
   792 	if(aIface->iLinklocalAddressKnown)
       
   793 		{
       
   794 		PublishToHandle(ifIdx, *(aIface->iLinklocalAddressKnown));
       
   795 		}
       
   796 }