testexecmgmt/ucc/Source/facontroller/CSFacontroller.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-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 * This file was autogenerated by rpcgen, but should be modified by the developer.
       
    16 * Make sure you don't use the -component_mod flag in future or this file will be overwritten.
       
    17 * Mon Oct 06 12:02:04 2003
       
    18 * System Includes
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 #include <stdio.h>
       
    27 #include <signal.h>
       
    28 #ifndef WIN32
       
    29 #include <unistd.h>
       
    30 #include <net/if.h>
       
    31 #endif
       
    32 
       
    33 
       
    34 /****************************************************************************************
       
    35  * 
       
    36  * Local Includes
       
    37  * 
       
    38  ***************************************************************************************/
       
    39 #include "CSvcFacontroller.h"
       
    40 #include "CSFacontroller.h"
       
    41 
       
    42 
       
    43 /****************************************************************************************
       
    44  * 
       
    45  * Definition
       
    46  * 
       
    47  ***************************************************************************************/
       
    48 #define TEMPLATE_CONFIG_FILE			"dynfad.template.conf"
       
    49 #define MOBILE_AGENT_COMMAND_LINE		"./dynfad --fg --debug --config "
       
    50 #define MAXCOMMANDLINELENGTH			(31 + 256 + 1)
       
    51 #define MAXTUNNELS                      32
       
    52 #define MAXINTERFACECONFIGENTRY			(IFNAMSIZ + 128)
       
    53 #define CONFIGCOUNT						16
       
    54 
       
    55 
       
    56 /*******************************************************************************
       
    57  *
       
    58  * Macro Functions
       
    59  *
       
    60  ******************************************************************************/
       
    61 
       
    62 
       
    63 /****************************************************************************************
       
    64  * 
       
    65  * Implementation
       
    66  * 
       
    67  ***************************************************************************************/
       
    68 CSFacontroller::CSFacontroller()
       
    69 {
       
    70 	iDynamicsCallTimeout = -1;
       
    71 	iAgentProcess = NULL;
       
    72 }
       
    73 
       
    74 CSFacontroller::~CSFacontroller()
       
    75 {
       
    76 	assert( iAgentProcess == NULL );
       
    77 }
       
    78 
       
    79 int CSFacontroller::GetKey()
       
    80 {
       
    81 	return iKey;
       
    82 }
       
    83 
       
    84 void CSFacontroller::SetKey( int aKey )
       
    85 {
       
    86 	iKey = aKey;
       
    87 }
       
    88 
       
    89 
       
    90 /****************************************************************************************
       
    91  * 
       
    92  * PUBLIC FUNCTION: cstr_createagent
       
    93  * 
       
    94  ***************************************************************************************/
       
    95 TResult CSFacontroller::cstr_createagent( void )
       
    96 {
       
    97 	TResult rv;
       
    98 	TDCFError terr, terr_a[CONFIGCOUNT];
       
    99 	TInterfaceAliasError ierr;
       
   100 	TStartupInfo startup_info;
       
   101 	CIntegerAllocator *ialloc;
       
   102 	char dynamics_config[MAXINTERFACECONFIGENTRY];
       
   103 	string interface_name, interface_address;
       
   104 	int errcode, i;
       
   105 
       
   106 	// initialise the result
       
   107 	rv.iServiceResult = ERR_NONE;
       
   108 	rv.iSubComponentResult = 0;
       
   109 	rv.iData0 = 0;
       
   110 	rv.iData1 = 0;
       
   111 
       
   112 	// get an address for the new interface alias
       
   113 	ialloc = CSvcFacontroller::GetIntegerAllocator();
       
   114 	assert( ialloc != NULL );
       
   115 	iInterfaceHostAddress = ialloc->AllocateInteger();
       
   116 	if( iInterfaceHostAddress == -1 ) {
       
   117 	  rv.iServiceResult = ERR_NO_MORE_INTERFACES;
       
   118 	  return rv;
       
   119 	}
       
   120 
       
   121 	// create a local copy of the configuration file
       
   122 	terr = iDynamicsConfigFile.SetReferenceFile( TEMPLATE_CONFIG_FILE );
       
   123 	if( terr != DCE_NONE ) {
       
   124 	  ialloc->FreeInteger( iInterfaceHostAddress );
       
   125 	  rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   126 	  rv.iSubComponentResult = (int)terr;
       
   127 	  return rv;
       
   128 	}
       
   129 	terr = iDynamicsConfigFile.CreateLocalCopy( &errcode );
       
   130 	if( terr != DCE_NONE ) {
       
   131 	  ialloc->FreeInteger( iInterfaceHostAddress );
       
   132 	  rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   133 	  rv.iSubComponentResult = (int)terr;
       
   134 	  rv.iData0 = errcode;
       
   135 	  return rv;
       
   136 	}	  
       
   137 
       
   138 	// create an alias interface to use
       
   139 	startup_info = CSvcFacontroller::GetStartupInfo();
       
   140 	ierr = iAgentInterface.CreateNewInterfaceAlias( startup_info.iBaseInterfaceIndex, startup_info.iNetworkMask, iInterfaceHostAddress, &iInterfaceIndex, &errcode );
       
   141 	if( ierr != IE_NONE ) {
       
   142 	  rv.iServiceResult = ERR_CREATE_INTERFACE_ERROR;
       
   143 	  rv.iSubComponentResult = ierr;
       
   144 	  rv.iData0 = errcode;
       
   145 	  ialloc->FreeInteger( iInterfaceHostAddress );
       
   146 	  iDynamicsConfigFile.RemoveLocalCopy( &errcode );
       
   147 	  return rv;
       
   148 	}
       
   149 	ierr = iAgentInterface.GetInterfaceAddress( &interface_address );
       
   150 	assert( ierr == IE_NONE );
       
   151 
       
   152 	// set the interface name in the config file
       
   153 	ierr = iAgentInterface.GetInterfaceName( &interface_name );
       
   154 	snprintf( dynamics_config, MAXINTERFACECONFIGENTRY, "%s 2 -1 10", interface_name.c_str() ); 
       
   155 	terr_a[0] = iDynamicsConfigFile.AddListOption( "INTERFACES_BEGIN", "INTERFACES_END", dynamics_config, &errcode );
       
   156 
       
   157 	// set the tunnel name prefix to a unique value for this agent
       
   158 	//	snprintf( dynamics_config, MAXINTERFACECONFIGENTRY, "TunnelDevice \"TUNL-%s-\"", interface_name.c_str() );
       
   159 	snprintf( dynamics_config, MAXINTERFACECONFIGENTRY, "TunnelDevice \"TUNL\"" );
       
   160 	terr_a[1] = iDynamicsConfigFile.SetSingleOption( "TunnelDevice", dynamics_config, &errcode );
       
   161 
       
   162 	// set the FA addresses to the alias interface address
       
   163 	snprintf( dynamics_config, MAXINTERFACECONFIGENTRY, "HighestFAIPAddress %s", interface_address.c_str() );
       
   164 	terr_a[2] = iDynamicsConfigFile.SetSingleOption( "HighestFAIPAddress", dynamics_config, &errcode );
       
   165 
       
   166 	// set the FA addresses to the alias interface address
       
   167 	snprintf( dynamics_config, MAXINTERFACECONFIGENTRY, "UpperFAIPAddress %s", interface_address.c_str() );
       
   168 	terr_a[3] = iDynamicsConfigFile.SetSingleOption( "UpperFAIPAddress", dynamics_config, &errcode );
       
   169 
       
   170 	// see if any errors occured setting the config info
       
   171 	for( i = 0; i < 3; i++ ) {
       
   172 		if( terr_a[i] != DCE_NONE ) {
       
   173 			rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   174 			rv.iSubComponentResult = (int)terr_a[i];
       
   175 			iAgentInterface.DestroyInterfaceAlias( &errcode );
       
   176 			ialloc->FreeInteger( iInterfaceHostAddress );
       
   177 			iDynamicsConfigFile.RemoveLocalCopy( &errcode );
       
   178 			return rv;
       
   179 		}
       
   180 	}
       
   181 
       
   182 	// done - success
       
   183 	return rv;
       
   184 }
       
   185 
       
   186 
       
   187 /****************************************************************************************
       
   188  * 
       
   189  * PUBLIC FUNCTION: dstr_removeagent
       
   190  * 
       
   191  ***************************************************************************************/
       
   192 int CSFacontroller::dstr_removeagent( int aArgs, int *aDeleteInstance )
       
   193 {
       
   194 	int rv = ERR_NONE, errcode;
       
   195 	TDCFError terr;
       
   196 	TInterfaceAliasError ierr;
       
   197 	CIntegerAllocator *ialloc;
       
   198 
       
   199 	// make sure that the proces isn't running
       
   200 	if( iAgentProcess != NULL ) {
       
   201 	  *aDeleteInstance = 0;
       
   202 	  rv = ERR_INVALIDSTATE;
       
   203 	  return rv;
       
   204 	}
       
   205 
       
   206 	// free the interface index
       
   207 	ialloc = CSvcFacontroller::GetIntegerAllocator();
       
   208 	assert( ialloc != NULL );
       
   209 	ialloc->FreeInteger( iInterfaceHostAddress );
       
   210 
       
   211 	// clean up the config file
       
   212 	terr = iDynamicsConfigFile.RemoveLocalCopy( &errcode );
       
   213 
       
   214 	// destroy the interface alias
       
   215 	ierr = iAgentInterface.DestroyInterfaceAlias( &errcode );
       
   216 	  
       
   217 	// check for errors from the config file - delete the instance in any case
       
   218 	if( terr != DCE_NONE ) {
       
   219 	  rv = ERR_CONFIG_FILE_ERROR;
       
   220 	  return rv;
       
   221 	}
       
   222 
       
   223 	// check for errors from the interface alias - delete the instance
       
   224 	if( ierr != IE_NONE ) {
       
   225 	  rv = ERR_DESTROY_INTERFACE_ERROR;
       
   226 	  return rv;
       
   227 	}
       
   228 
       
   229 	// done - success
       
   230 	return rv;
       
   231 }
       
   232 
       
   233 
       
   234 /****************************************************************************************
       
   235  * 
       
   236  * PUBLIC FUNCTION: startmobileagent
       
   237  * 
       
   238  ***************************************************************************************/
       
   239 TResult CSFacontroller::startmobileagent( TStartAgentRequest aArgs )
       
   240 {
       
   241 	TResult rv;
       
   242 	int errcode;
       
   243 	TCAProcessError perr;
       
   244 	char command_line_str[MAXCOMMANDLINELENGTH];
       
   245 	TDCFError terr;
       
   246 
       
   247 	// initialise the result
       
   248 	rv.iServiceResult = ERR_NONE;
       
   249 	rv.iSubComponentResult = 0;
       
   250 	rv.iData0 = 0;
       
   251 	rv.iData1 = 0;
       
   252 
       
   253 	// check that there is currently no process
       
   254 	if( iAgentProcess != NULL ) {
       
   255 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   256 		return rv;
       
   257 	}
       
   258 
       
   259 	// do some semantic checking of the params
       
   260 	if( (aArgs.iSolicitationMode < -1) || (aArgs.iSolicitationMode > 1) ) {
       
   261 		rv.iServiceResult = ERR_INVALIDARG;
       
   262 		rv.iData0 = 3;
       
   263 		return rv;
       
   264 	}
       
   265 	if( (aArgs.iSolicitationMode == 1) && (aArgs.iSolicitationInterval < 1) ) {
       
   266 		rv.iServiceResult = ERR_INVALIDARG;
       
   267 		rv.iData0 = 4;
       
   268 		return rv;
       
   269 	}
       
   270 	if( aArgs.iSolicitationMode != 1 ) {
       
   271 	  aArgs.iSolicitationInterval = 1;
       
   272 	}
       
   273 
       
   274 	// add the interface name to the config file
       
   275 	snprintf( command_line_str, MAXCOMMANDLINELENGTH, "%s 3 %d %d", aArgs.iLowerInterface, aArgs.iSolicitationMode, aArgs.iSolicitationInterval );
       
   276 	terr = iDynamicsConfigFile.AddListOption( "INTERFACES_BEGIN", "INTERFACES_END", command_line_str, &errcode );
       
   277 	if( terr != DCE_NONE ) {
       
   278 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   279 		rv.iSubComponentResult = (int)terr;
       
   280 		return rv;
       
   281 	}
       
   282 	
       
   283 	// create the new process object
       
   284 	iAgentProcess = new CAProcess();
       
   285 	assert( iAgentProcess != NULL );
       
   286 
       
   287 	// construct the command line
       
   288 	snprintf( command_line_str, MAXCOMMANDLINELENGTH, "%s%s", MOBILE_AGENT_COMMAND_LINE, iDynamicsConfigFile.GetLocalFilename() );
       
   289 
       
   290 	// start the actual process
       
   291 	perr = iAgentProcess->StartProcess( command_line_str, &errcode, false, false, false ); 
       
   292 	if( perr != CAE_NONE ) {
       
   293 		rv.iServiceResult = ERR_START_PROCESS_ERROR;
       
   294 		rv.iSubComponentResult = (int)perr;
       
   295 		delete iAgentProcess;
       
   296 		iAgentProcess = NULL;
       
   297 		return rv;
       
   298 	}
       
   299 	iLowerInterface = aArgs.iLowerInterface;
       
   300 
       
   301 	// done - success
       
   302 	rv.iServiceResult = ERR_NONE;
       
   303 	return rv;
       
   304 }
       
   305 
       
   306 
       
   307 /****************************************************************************************
       
   308  * 
       
   309  * PUBLIC FUNCTION: stopmobileagent
       
   310  * 
       
   311  ***************************************************************************************/
       
   312 TResult CSFacontroller::stopmobileagent( int aArgs )
       
   313 {
       
   314 	TResult rv;
       
   315 	TCAProcessError perr;
       
   316 	TProcessStatus pstatus;
       
   317 	TDCFError terr;
       
   318 	int errcode;
       
   319 
       
   320 	// initialise the result
       
   321 	rv.iServiceResult = ERR_NONE;
       
   322 	rv.iSubComponentResult = 0;
       
   323 	rv.iData0 = 0;
       
   324 	rv.iData1 = 0;
       
   325 
       
   326 
       
   327 	// if the process isn't started then invalid state
       
   328 	if( iAgentProcess == NULL ) {
       
   329 	  rv.iServiceResult = ERR_INVALIDSTATE;
       
   330 	  return rv;
       
   331 	}
       
   332 
       
   333 	// remove the interface line added in startup
       
   334 	terr = iDynamicsConfigFile.RemoveListOption( "INTERFACES_BEGIN", "INTERFACES_END", iLowerInterface.c_str(), &errcode );
       
   335 	if( terr != DCE_NONE ) {
       
   336 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   337 		rv.iSubComponentResult = (int)terr;
       
   338 		return rv;
       
   339 	}
       
   340 
       
   341 	// if the process isn't running then it has died outside the scope of this controller, clean up the state, return an error since
       
   342 	// this is noteworthy and should be either expected or not happen.
       
   343 	perr = iAgentProcess->GetProcessStatus( &pstatus );
       
   344 	assert( perr == CAE_NONE );
       
   345 	if( pstatus != PS_STARTED ) {
       
   346 	  assert( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) );
       
   347 	  delete iAgentProcess;
       
   348 	  iAgentProcess = NULL;
       
   349 	  rv.iServiceResult = ERR_PROCESS_TERMINATED_OUTSIDE_SCOPE;
       
   350 	  return rv;
       
   351 	}
       
   352 
       
   353 	// request the process to stop
       
   354 	perr = iAgentProcess->RequestStop( SIGTERM );
       
   355 	if( perr != CAE_NONE ) {
       
   356 	  rv.iServiceResult = ERR_STOP_PROCESS_ERROR;
       
   357 	  rv.iSubComponentResult = (int)perr;
       
   358 	  return rv;
       
   359 	}
       
   360 
       
   361 	// wait for the process to stop
       
   362 	perr = iAgentProcess->WaitForProcessToTerminate( -1 );
       
   363 	if( perr != CAE_NONE ) {
       
   364 	  rv.iServiceResult = ERR_WAIT_PROCESS_ERROR;
       
   365 	  rv.iSubComponentResult = (int)perr;
       
   366 	  return rv;
       
   367 	}
       
   368 
       
   369 	// remove the process
       
   370 	delete iAgentProcess;
       
   371 	iAgentProcess = NULL;
       
   372 
       
   373 	// done - success
       
   374 	return rv;
       
   375 }
       
   376 
       
   377 
       
   378 /****************************************************************************************
       
   379  * 
       
   380  * PUBLIC FUNCTION: getmobileagentstatus
       
   381  * 
       
   382  ***************************************************************************************/
       
   383 TResult CSFacontroller::getmobileagentstatus( int aArgs )
       
   384 {
       
   385 	TResult rv;
       
   386 	TCAProcessError perr;
       
   387 	TProcessStatus pstatus;
       
   388 
       
   389 	// initialise the result
       
   390 	rv.iServiceResult = ERR_NONE;
       
   391 	rv.iSubComponentResult = 0;
       
   392 	rv.iData0 = 0;
       
   393 	rv.iData1 = 0;
       
   394 
       
   395 
       
   396 	// if the process doesn't exist then we return PS_INVALID
       
   397 	if( iAgentProcess == NULL ) {
       
   398 		rv.iData0 = PS_INVALID;
       
   399 		return rv;
       
   400 	}
       
   401 
       
   402 	// otherwise just return whatever iAgentProcess->GetProcessStatus() returns
       
   403 	perr = iAgentProcess->GetProcessStatus( &pstatus );
       
   404 	assert( perr == CAE_NONE );
       
   405 	rv.iData0 = (int)pstatus;
       
   406 	return rv;
       
   407 }
       
   408 
       
   409 
       
   410 /****************************************************************************************
       
   411  * 
       
   412  * PUBLIC FUNCTION: setsingleoption
       
   413  * 
       
   414  ***************************************************************************************/
       
   415 TResult CSFacontroller::setsingleoption( TOptionDesc aArgs )
       
   416 {
       
   417 	TResult rv;
       
   418 	int errcode;
       
   419 	TDCFError cerr;
       
   420 
       
   421 	// initialise the result
       
   422 	rv.iServiceResult = ERR_NONE;
       
   423 	rv.iSubComponentResult = 0;
       
   424 	rv.iData0 = 0;
       
   425 	rv.iData1 = 0;
       
   426 
       
   427 	// make sure that the process isn't running
       
   428 	if( iAgentProcess != NULL ) {
       
   429 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   430 		return rv;
       
   431 	}
       
   432 
       
   433 	// modify the config file
       
   434 	cerr = iDynamicsConfigFile.SetSingleOption( aArgs.iOptionToken, aArgs.iOptionValue, &errcode );
       
   435 	if( cerr != DCE_NONE ) {
       
   436 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   437 		rv.iSubComponentResult = (int)cerr;
       
   438 		rv.iData0 = errcode;
       
   439 		return rv;
       
   440 	}
       
   441 
       
   442 	// return success
       
   443 	rv.iServiceResult = ERR_NONE;
       
   444 	return rv;
       
   445 }
       
   446 
       
   447 
       
   448 /****************************************************************************************
       
   449  * 
       
   450  * PUBLIC FUNCTION: removesingleoption
       
   451  * 
       
   452  ***************************************************************************************/
       
   453 TResult CSFacontroller::removesingleoption( TOptionDesc aArgs )
       
   454 {
       
   455 	TResult rv;
       
   456 	int errcode;
       
   457 	TDCFError cerr;
       
   458 
       
   459 	// initialise the result
       
   460 	rv.iServiceResult = ERR_NONE;
       
   461 	rv.iSubComponentResult = 0;
       
   462 	rv.iData0 = 0;
       
   463 	rv.iData1 = 0;
       
   464 
       
   465 	// make sure that the process isn't running
       
   466 	if( iAgentProcess != NULL ) {
       
   467 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   468 		return rv;
       
   469 	}
       
   470 
       
   471 	// modify the config file
       
   472 	cerr = iDynamicsConfigFile.RemoveSingleOption( aArgs.iOptionToken, &errcode );
       
   473 	if( cerr != DCE_NONE ) {
       
   474 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   475 		rv.iSubComponentResult = (int)cerr;
       
   476 		rv.iData0 = errcode;
       
   477 		return rv;
       
   478 	}
       
   479 
       
   480 	// return success
       
   481 	rv.iServiceResult = ERR_NONE;
       
   482 	return rv;
       
   483 }
       
   484 
       
   485 
       
   486 /****************************************************************************************
       
   487  * 
       
   488  * PUBLIC FUNCTION: addlistoption
       
   489  * 
       
   490  ***************************************************************************************/
       
   491 TResult CSFacontroller::addlistoption( TOptionDesc aArgs )
       
   492 {
       
   493 	TResult rv;
       
   494 	int errcode;
       
   495 	TDCFError cerr;
       
   496 
       
   497 	// initialise the result
       
   498 	rv.iServiceResult = ERR_NONE;
       
   499 	rv.iSubComponentResult = 0;
       
   500 	rv.iData0 = 0;
       
   501 	rv.iData1 = 0;
       
   502 
       
   503 	// make sure that the process isn't running
       
   504 	if( iAgentProcess != NULL ) {
       
   505 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   506 		return rv;
       
   507 	}
       
   508 
       
   509 	// modify the config file
       
   510 	cerr = iDynamicsConfigFile.AddListOption( aArgs.iOptionBlockStart, aArgs.iOptionBlockEnd, aArgs.iOptionValue, &errcode );
       
   511 	if( cerr != DCE_NONE ) {
       
   512 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   513 		rv.iSubComponentResult = (int)cerr;
       
   514 		rv.iData0 = errcode;
       
   515 		return rv;
       
   516 	}
       
   517 
       
   518 	// return success
       
   519 	rv.iServiceResult = ERR_NONE;
       
   520 	return rv;
       
   521 }
       
   522 
       
   523 
       
   524 /****************************************************************************************
       
   525  * 
       
   526  * PUBLIC FUNCTION: removelistoption
       
   527  * 
       
   528  ***************************************************************************************/
       
   529 TResult CSFacontroller::removelistoption( TOptionDesc aArgs )
       
   530 {
       
   531 	TResult rv;
       
   532 	int errcode;
       
   533 	TDCFError cerr;
       
   534 
       
   535 	// initialise the result
       
   536 	rv.iServiceResult = ERR_NONE;
       
   537 	rv.iSubComponentResult = 0;
       
   538 	rv.iData0 = 0;
       
   539 	rv.iData1 = 0;
       
   540 
       
   541 	// make sure that the process isn't running
       
   542 	if( iAgentProcess != NULL ) {
       
   543 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   544 		return rv;
       
   545 	}
       
   546 
       
   547 	// modify the config file
       
   548 	cerr = iDynamicsConfigFile.RemoveListOption( aArgs.iOptionBlockStart, aArgs.iOptionBlockEnd, aArgs.iOptionToken, &errcode );
       
   549 	if( cerr != DCE_NONE ) {
       
   550 		rv.iServiceResult = ERR_CONFIG_FILE_ERROR;
       
   551 		rv.iSubComponentResult = (int)cerr;
       
   552 		rv.iData0 = errcode;
       
   553 		return rv;
       
   554 	}
       
   555 
       
   556 	// return success
       
   557 	rv.iServiceResult = ERR_NONE;
       
   558 	return rv;
       
   559 }
       
   560 
       
   561 
       
   562 /****************************************************************************************
       
   563  * 
       
   564  * PUBLIC FUNCTION: getstatus
       
   565  * 
       
   566  ***************************************************************************************/
       
   567 TFaStatusInfo CSFacontroller::getstatus( int aArgs )
       
   568 {
       
   569 	TFaStatusInfo rv;
       
   570 	TDynamicsCallInfo cres;
       
   571 	TForeignAgentStatusInfo foreign_agent_info;
       
   572 
       
   573 	// init the return value
       
   574 	memset( &rv, 0, sizeof(rv) );
       
   575 	rv.iCallResult.iServiceResult = ERR_NONE;
       
   576 	
       
   577 	// check that the agent is running
       
   578 	rv.iCallResult = is_agent_running();
       
   579 	if( rv.iCallResult.iServiceResult != ERR_NONE ) {
       
   580 		return rv;
       
   581 	}
       
   582 
       
   583 	// make the call to dynamics
       
   584 	cres = iDynamicsCommand.ForeignAgentGetStatus( &foreign_agent_info );
       
   585 	if( cres.iResult != DC_SUCCESS ) {
       
   586 		set_dynamics_error( &(rv.iCallResult), &cres );
       
   587 		return rv;
       
   588 	}
       
   589 
       
   590 	// set the result
       
   591 	rv.iAdvertisementsSent = foreign_agent_info.iAdvertisementsSent;
       
   592 	rv.iDiscardedMalformed = foreign_agent_info.iDiscardedMalformed;
       
   593 	rv.iDiscardedUnknownExtension = foreign_agent_info.iDiscardedUnknownExtension;
       
   594 	rv.iDiscardedVendor = foreign_agent_info.iDiscardedVendor;
       
   595 	rv.iPendingRegistrationRequests = foreign_agent_info.iPendingRegistrationRequests;
       
   596 	rv.iReplysAccepted = foreign_agent_info.iReplysAccepted;
       
   597 	rv.iReplysRejected = foreign_agent_info.iReplysRejected;
       
   598 	rv.iRequestsAccepted = foreign_agent_info.iRequestsAccepted;
       
   599 	rv.iRequestsRejected = foreign_agent_info.iRequestsRejected;
       
   600 	rv.iTunnelCount = foreign_agent_info.iTunnelCount;
       
   601 	return rv;
       
   602 }
       
   603 
       
   604 
       
   605 /****************************************************************************************
       
   606  * 
       
   607  * PUBLIC FUNCTION: destroytunnelid
       
   608  * 
       
   609  ***************************************************************************************/
       
   610 TResult CSFacontroller::destroytunnelid( TFaTunnelID aArgs )
       
   611 {
       
   612 	TResult rv;
       
   613 	TDynamicsCallInfo cres;
       
   614 
       
   615 	// init the return value
       
   616 	memset( &rv, 0, sizeof(rv) );
       
   617 	rv.iServiceResult = ERR_NONE;
       
   618 	
       
   619 	// check that the agent is running
       
   620 	rv = is_agent_running();
       
   621 	if( rv.iServiceResult != ERR_NONE ) {
       
   622 		return rv;
       
   623 	}
       
   624 
       
   625 	// make the call to dynamics
       
   626 	cres = iDynamicsCommand.ForeignAgentDestroyTunnel( aArgs.iMobileNodeAddress );
       
   627 	if( cres.iResult != DC_SUCCESS ) {
       
   628 		set_dynamics_error( &rv, &cres );
       
   629 		return rv;
       
   630 	}
       
   631 
       
   632 	// done 
       
   633 	return rv;
       
   634 }
       
   635 
       
   636 
       
   637 /****************************************************************************************
       
   638  * 
       
   639  * PUBLIC FUNCTION: listtunnels
       
   640  * 
       
   641  ***************************************************************************************/
       
   642 TFaTunnelList CSFacontroller::listtunnels( int aArgs )
       
   643 {
       
   644 	TResult res;
       
   645 	TFaTunnelList rv = { 0, NULL };
       
   646 	TDynamicsCallInfo cres;
       
   647 	TTunnelID tunnel_list[MAXTUNNELS];
       
   648 	int tunnel_count = MAXTUNNELS, i;
       
   649 
       
   650 	// check that the agent is running
       
   651 	res = is_agent_running();
       
   652 	if( res.iServiceResult != ERR_NONE ) {
       
   653 		return rv;
       
   654 	}
       
   655 
       
   656 	// make the call to dynamics
       
   657 	cres = iDynamicsCommand.ForeignAgentGetTunnels( &tunnel_count, tunnel_list );
       
   658 	if( cres.iResult != DC_SUCCESS ) {
       
   659 		return rv;
       
   660 	}
       
   661 
       
   662 	// allocate memory for the list
       
   663 	rv.TFaTunnelList_len = tunnel_count;
       
   664 	rv.TFaTunnelList_val = (TFaTunnelID*)calloc( tunnel_count, sizeof(TFaTunnelID) );
       
   665 	assert( rv.TFaTunnelList_val != NULL );
       
   666 
       
   667 	// now return the information
       
   668 	for( i = 0; i < tunnel_count; i++ ) {
       
   669 		(rv.TFaTunnelList_val)[i].iAgentID = 0;
       
   670 		(rv.TFaTunnelList_val)[i].iID = (tunnel_list[i]).iTunnelID;
       
   671 		(rv.TFaTunnelList_val)[i].iHomeAgentAddress = (tunnel_list[i]).iHomeAgentAddress;
       
   672 		(rv.TFaTunnelList_val)[i].iMobileNodeAddress = (tunnel_list[i]).iMobileNodeAddress;
       
   673 	}
       
   674 
       
   675 	// done - success
       
   676 	return rv;
       
   677 }
       
   678 
       
   679 /****************************************************************************************
       
   680  * 
       
   681  * PUBLIC FUNCTION: gettunnelinfo
       
   682  * 
       
   683  ***************************************************************************************/
       
   684 TFaTunnelInfo CSFacontroller::gettunnelinfo( TGetTunnelRequest aArgs )
       
   685 {
       
   686 	TFaTunnelInfo rv;
       
   687 	TDynamicsCallInfo cres;
       
   688 	TForeignAgentTunnelInfo fati;
       
   689 
       
   690 	// init the result
       
   691 	memset( &rv, 0, sizeof(rv) );
       
   692 	rv.iCallResult.iServiceResult = ERR_NONE;
       
   693 
       
   694 	// check that the agent is running
       
   695 	rv.iCallResult = is_agent_running();
       
   696 	if( rv.iCallResult.iServiceResult != ERR_NONE ) {
       
   697 		return rv;
       
   698 	}
       
   699 
       
   700 	// make the call to dynamics
       
   701 	cres = iDynamicsCommand.ForeignAgentGetTunnelInfo( aArgs.iMobileNodeAddress, &fati );
       
   702 	if( cres.iResult != DC_SUCCESS ) {
       
   703 		set_dynamics_error( &(rv.iCallResult), &cres );
       
   704 		return rv;
       
   705 	}
       
   706 
       
   707 	// set the info
       
   708 	rv.iCareofAddress = fati.iCareofAddress;
       
   709 	rv.iCreationTime = fati.iCreationTime;
       
   710 	rv.iExpirationTime = fati.iExpirationTime;
       
   711 	rv.iHomeAgentAddress = fati.iHomeAgentAddress;
       
   712 	rv.iLastTimestamp = fati.iLastTimestamp;
       
   713 	rv.iMobileNodeAddress = fati.iMobileNodeAddress;
       
   714 	rv.iPrivateHomeAgentID = fati.iPrivateHomeAgentID;
       
   715 	rv.iRefreshTime = fati.iRefreshTime;
       
   716 	rv.iSPI = fati.iSPI;
       
   717 	rv.iTimeout = fati.iTimeout;
       
   718 	return rv;
       
   719 }
       
   720 
       
   721 
       
   722 
       
   723 /****************************************************************************************
       
   724  * 
       
   725  * PUBLIC FUNCTION: settimeout
       
   726  * 
       
   727  ***************************************************************************************/
       
   728 void CSFacontroller::settimeout( TTimeoutRequest aArgs )
       
   729 {
       
   730 	iDynamicsCallTimeout = aArgs.iTimeout;
       
   731 	iDynamicsCommand.SetTimeout( aArgs.iTimeout );
       
   732 }
       
   733 
       
   734 
       
   735 /****************************************************************************************
       
   736  * 
       
   737  * PRIVATE FUNCTION: is_agent_running
       
   738  * 
       
   739  ***************************************************************************************/
       
   740 TResult CSFacontroller::is_agent_running()
       
   741 {	
       
   742 	TResult rv;
       
   743 	TCAProcessError perr;
       
   744 	TProcessStatus pstatus;
       
   745 
       
   746 	// clear the rv
       
   747 	memset( &rv, 0, sizeof(rv) );
       
   748 
       
   749 	// check that the agent was running
       
   750 	if( iAgentProcess == NULL ) {
       
   751 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   752 		return rv;
       
   753 	}
       
   754 
       
   755 	// check that the agent is still runnning
       
   756 	perr = iAgentProcess->GetProcessStatus( &pstatus );
       
   757 	assert( perr == CAE_NONE );
       
   758 	if( pstatus != PS_STARTED ) {
       
   759 		rv.iServiceResult = ERR_INVALIDSTATE;
       
   760 		return rv;
       
   761 	}
       
   762 
       
   763 	// done - success
       
   764 	return rv;
       
   765 }
       
   766 
       
   767 
       
   768 /****************************************************************************************
       
   769  * 
       
   770  * PRIVATE FUNCTION: is_agent_running
       
   771  * 
       
   772  ***************************************************************************************/
       
   773 void CSFacontroller::set_dynamics_error( TResult *result, TDynamicsCallInfo *cres )
       
   774 {
       
   775 	result->iServiceResult = ERR_DYNAMICS_CALL_FAILED;
       
   776 	result->iSubComponentResult = cres->iResult;
       
   777 	result->iData0 = cres->iErrorCode;
       
   778 	result->iData1 = cres->iErrorDetail;
       
   779 }
       
   780