testexecmgmt/ucc/Source/pppcontroller/CSPppcontroller.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 * Fri Oct 17 09:37:12 2003
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 /****************************************************************************************
       
    25  * 
       
    26  * System Includes
       
    27  * 
       
    28  ***************************************************************************************/
       
    29 #include <stdio.h>
       
    30 #include <signal.h>
       
    31 #include <sys/types.h>
       
    32 #include <sys/stat.h>
       
    33 #ifndef WIN32
       
    34 #include <sys/socket.h>
       
    35 #include <netinet/in.h>
       
    36 #include <arpa/inet.h>
       
    37 #endif
       
    38 
       
    39 
       
    40 /****************************************************************************************
       
    41  * 
       
    42  * Local Includes
       
    43  * 
       
    44  ***************************************************************************************/
       
    45 #include "CSvcPppcontroller.h"
       
    46 #include "CSPppcontroller.h"
       
    47 #include "../include/standard_unix.h"
       
    48 #include "../include/strncpynt.h"
       
    49 #include "../DynamicsConfigurationLibrary/file_utilities.h"
       
    50 
       
    51 
       
    52 /****************************************************************************************
       
    53  * 
       
    54  * Definitions
       
    55  * 
       
    56  ***************************************************************************************/
       
    57 #define MAXLOGNAME				64
       
    58 #define MAXCOMMANDLINE			2048
       
    59 #define MAXLOGLINELENGTH		1024
       
    60 #define PPPD_BINARY				"./pppd"
       
    61 #define GATEWAY_BINARY			"./pppdgateway"
       
    62 #define MAXINTERFACENAMESIZE	64
       
    63 #define MAXLISTLINES			32
       
    64 #define CONNECTED_STRING		"Script /etc/ppp/ip-up finished"
       
    65 #define DISCONNECTED_STRING		"Connection terminated"
       
    66 
       
    67 #define IS_INLINE_WHITESPACE(c)	(((c) == ' ')||((c) == '\t'))
       
    68 
       
    69 /****************************************************************************************
       
    70  * 
       
    71  * Implementation
       
    72  * 
       
    73  ***************************************************************************************/
       
    74 CSPppcontroller::CSPppcontroller()
       
    75 {
       
    76 	iProcess = NULL;
       
    77 	iSessionStatus = SS_NOT_STARTED;
       
    78 }
       
    79 
       
    80 CSPppcontroller::~CSPppcontroller()
       
    81 {
       
    82 	assert( iProcess == NULL );
       
    83 }
       
    84 
       
    85 int CSPppcontroller::GetKey()
       
    86 {
       
    87 	return iKey;
       
    88 }
       
    89 
       
    90 void CSPppcontroller::SetKey( int aKey )
       
    91 {
       
    92 	iKey = aKey;
       
    93 }
       
    94 
       
    95 
       
    96 /****************************************************************************************
       
    97  * 
       
    98  * PUBLIC FUNCTION: cstr_startpppsession
       
    99  * 
       
   100  ***************************************************************************************/
       
   101 TResult CSPppcontroller::cstr_startpppsession( TPppSessionConfig aArgs )
       
   102 {
       
   103 	int err, errcode;
       
   104 	TResult rv = { ERR_NONE, 0, 0 };
       
   105 	char logfilename[MAXLOGNAME];
       
   106 	char commandline[MAXCOMMANDLINE];
       
   107 	TCAProcessError perr;
       
   108 
       
   109 	// check that we are not already started
       
   110 	assert( iProcess == NULL );
       
   111 
       
   112 	// save the config
       
   113 	iSessionConfig = aArgs;
       
   114 
       
   115 	// construct the name of the logfile
       
   116 	snprintf( logfilename, MAXLOGNAME, "pppd%04d.log", iKey );
       
   117 
       
   118 	// remove the file if it exists -- if an error other than file not exists happens then exit
       
   119 	err = unlink( logfilename );
       
   120 	if( (err != 0) && (errno != ENOENT) ) {
       
   121 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   122 		rv.iExtendedCode = 0;
       
   123 		rv.iSystemError = errno;
       
   124 		return rv;
       
   125 	}
       
   126 
       
   127 	// construct the command line for the PPP session
       
   128 #ifndef __UNIT_TEST_13
       
   129 	snprintf( commandline, MAXCOMMANDLINE, "%s pty \'%s %s %s %d\' logfile %s %s", PPPD_BINARY, GATEWAY_BINARY, 
       
   130 		  iSessionConfig.iMobsterAddress, iSessionConfig.iMobsterPort, iSessionConfig.iMTID, logfilename, iSessionConfig.iSessionConfiguration );
       
   131 #else
       
   132 	snprintf( commandline, MAXCOMMANDLINE, "%s notty logfile %s %s", PPPD_BINARY, logfilename, iSessionConfig.iSessionConfiguration );
       
   133 #endif
       
   134 	
       
   135 	// now start the process
       
   136 	iProcess = new CAProcess();
       
   137 	assert( iProcess != NULL );
       
   138 	perr = iProcess->StartProcess( commandline, &errcode, false, false, true );
       
   139 	if( perr != CAE_NONE ) {
       
   140 		rv.iStandardResult = ERR_START_PROCESS_ERROR;
       
   141 		rv.iExtendedCode = (int)perr;
       
   142 		rv.iSystemError = errcode;
       
   143 		delete iProcess;
       
   144 		iProcess = NULL;
       
   145 		return rv;
       
   146 	}
       
   147 
       
   148 	// done - success
       
   149 	return rv;
       
   150 }
       
   151 
       
   152 
       
   153 /****************************************************************************************
       
   154  * 
       
   155  * PUBLIC FUNCTION: dstr_removepppsession
       
   156  * 
       
   157  ***************************************************************************************/
       
   158 TResult CSPppcontroller::dstr_removepppsession( int aArgs, int *aDeleteInstance )
       
   159 {
       
   160 	TResult rv = { ERR_NONE, 0, 0 };
       
   161 	TCAProcessError perr;
       
   162 	TProcessStatus pstatus;
       
   163 	char logfilename[MAXLOGNAME];
       
   164 
       
   165 	// make sure that we still have a process
       
   166 	assert( iProcess != NULL );
       
   167 
       
   168 	// get the status of the process to make sure we are ok
       
   169 	perr = iProcess->GetProcessStatus( &pstatus );
       
   170 	assert( perr == CAE_NONE );
       
   171 	if( pstatus == PS_STARTED ) {
       
   172 	  *aDeleteInstance = 0;
       
   173 	  rv.iStandardResult = ERR_INVALIDSTATE;
       
   174 	  return rv;
       
   175 	}
       
   176 
       
   177 	// remove the log file
       
   178 	snprintf( logfilename, MAXLOGNAME, "pppd%04d.log", iKey );
       
   179 	unlink( logfilename );
       
   180 
       
   181 	// ok - remove the process
       
   182 	delete iProcess;
       
   183 	iProcess = NULL;
       
   184 
       
   185 	// done - success
       
   186 	*aDeleteInstance = 1; 
       
   187 	return rv;
       
   188 }
       
   189 
       
   190 
       
   191 /****************************************************************************************
       
   192  * 
       
   193  * PUBLIC FUNCTION: killsession
       
   194  * 
       
   195  ***************************************************************************************/
       
   196 TResult CSPppcontroller::killsession( int aArgs )
       
   197 {
       
   198 	return StopProcessWithSignal( SIGKILL );
       
   199 }
       
   200 
       
   201 
       
   202 /****************************************************************************************
       
   203  * 
       
   204  * PUBLIC FUNCTION: stopsession
       
   205  * 
       
   206  ***************************************************************************************/
       
   207 TResult CSPppcontroller::stopsession( int aArgs )
       
   208 {
       
   209 	return StopProcessWithSignal( SIGTERM );
       
   210 }
       
   211 
       
   212 
       
   213 /****************************************************************************************
       
   214  * 
       
   215  * PUBLIC FUNCTION: getsessioninfo
       
   216  * 
       
   217  ***************************************************************************************/
       
   218 TPppSessionDesc CSPppcontroller::getsessioninfo( int aArgs )
       
   219 {
       
   220 	TPppSessionDesc rv;
       
   221 	TResult res;
       
   222 	int exitcode = 0;
       
   223 	TCAProcessError perr;
       
   224 	TProcessStatus pstatus = PS_INVALID;
       
   225 	TProcessExitReason preason = ER_INVALID;
       
   226 
       
   227 	// check state
       
   228 	assert( iProcess != NULL );
       
   229 	memset( &rv, 0, sizeof(rv) );
       
   230 
       
   231 	// interrogate the process	
       
   232 	perr = iProcess->GetProcessStatus( &pstatus );
       
   233 	assert( perr == CAE_NONE );
       
   234 	if( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) ) {
       
   235 		perr = iProcess->GetExitReason( &preason );
       
   236 		assert( perr == CAE_NONE );
       
   237 	}
       
   238 	if( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) ) {
       
   239 		perr = iProcess->GetExitCode( &exitcode );
       
   240 		assert( perr == CAE_NONE );
       
   241 	}
       
   242 
       
   243 	// update the dynamic config
       
   244 	res = GetInterfaceName();
       
   245 	if( res.iStandardResult != ERR_NONE ) {
       
   246 		rv.iErrorCode = res.iStandardResult;
       
   247 		rv.iErrorDetail = res.iExtendedCode;
       
   248 		return rv;
       
   249 	}
       
   250 	res = UpdateSessionStatus();
       
   251 	if( res.iStandardResult != ERR_NONE ) {
       
   252 		rv.iErrorCode = res.iStandardResult;
       
   253 		rv.iErrorDetail = res.iExtendedCode;
       
   254 		return rv;
       
   255 	}
       
   256 	res = GetIPAddresses();
       
   257 	if( res.iStandardResult != ERR_NONE ) {
       
   258 		rv.iErrorCode = res.iStandardResult;
       
   259 		rv.iErrorDetail = res.iExtendedCode;
       
   260 		return rv;
       
   261 	}
       
   262 
       
   263 	// now return the info
       
   264 	rv.iErrorCode = ERR_NONE;
       
   265 	rv.iErrorDetail = 0;
       
   266 	rv.iConfig = iSessionConfig;
       
   267 	rv.iProcessStatus = (int)pstatus;
       
   268 	rv.iProcessExitReason = (int)preason;
       
   269 	rv.iProcessExitCode = exitcode;
       
   270 	rv.iSessionStatus = iSessionStatus;
       
   271 	STRNCPY_NULL_TERMINATE( rv.iInterfaceName, iInterfaceName.c_str(), MAXSESSIONNAMELEN );
       
   272 	rv.iLocalIPAddress = iLocalIPAddress;
       
   273 	rv.iRemoteIPAddress = iRemoteIPAddress;
       
   274 	return rv;
       
   275 }
       
   276 
       
   277 
       
   278 /****************************************************************************************
       
   279  * 
       
   280  * PUBLIC FUNCTION: getppplog
       
   281  * 
       
   282  ***************************************************************************************/
       
   283 TVarData CSPppcontroller::getppplog( int aArgs )
       
   284 {
       
   285 	TVarData rv;
       
   286 	TResult res;
       
   287 	char logname[MAXLOGNAME];
       
   288 
       
   289 	// get the logfilename 
       
   290 	snprintf( logname, MAXLOGNAME, "pppd%04d.log", iKey );
       
   291 
       
   292 	// now copy the file into the vardata buffer
       
   293 	res = CopyFileIntoBuffer( logname, &rv );
       
   294 	if( res.iStandardResult != ERR_NONE ) {
       
   295 		assert( rv.TVarData_len == 0 );
       
   296 		assert( rv.TVarData_val == NULL );
       
   297 	}
       
   298 
       
   299 	// done
       
   300 	return rv;
       
   301 }
       
   302 
       
   303 
       
   304 /****************************************************************************************
       
   305  * 
       
   306  * PRIVATE FUNCTION: StopProcessWithSignal
       
   307  * 
       
   308  ***************************************************************************************/
       
   309 TResult CSPppcontroller::StopProcessWithSignal( int aSignal )
       
   310 {
       
   311 	TResult rv = { ERR_NONE, 0, 0 };
       
   312 	TCAProcessError perr;
       
   313 	TProcessStatus pstatus;
       
   314 
       
   315 	// since we create in the constructor and stop in the destructor there must be a process
       
   316 	assert( iProcess != NULL );
       
   317 
       
   318 	// if the process isn't running then it has died outside the scope of this controller, 
       
   319 	// clean up the state, return an error since this is noteworthy and should be either 
       
   320 	// expected or not happen.
       
   321 	perr = iProcess->GetProcessStatus( &pstatus );
       
   322 	assert( perr == CAE_NONE );
       
   323 	if( pstatus != PS_STARTED ) {
       
   324 	  assert( (pstatus == PS_STOPPED) || (pstatus == PS_ABANDONNED) );
       
   325 	  rv.iStandardResult = ERR_PROCESS_TERMINATED_OUTSIDE_SCOPE;
       
   326 	  return rv;
       
   327 	}
       
   328 
       
   329 	// request the process to stop
       
   330 	perr = iProcess->RequestStop( aSignal );
       
   331 	if( perr != CAE_NONE ) {
       
   332 	  rv.iStandardResult = ERR_STOP_PROCESS_ERROR;
       
   333 	  rv.iExtendedCode = (int)perr;
       
   334 	  return rv;
       
   335 	}
       
   336 
       
   337 	// wait for the process to stop
       
   338 	perr = iProcess->WaitForProcessToTerminate( -1 );
       
   339 	if( perr != CAE_NONE ) {
       
   340 	  rv.iStandardResult = ERR_WAIT_PROCESS_ERROR;
       
   341 	  rv.iExtendedCode = (int)perr;
       
   342 	  return rv;
       
   343 	}
       
   344 
       
   345 	// done - success
       
   346 	return rv;
       
   347 }
       
   348 
       
   349 
       
   350 /****************************************************************************************
       
   351  * 
       
   352  * PRIVATE FUNCTION: GetInterfaceName
       
   353  * 
       
   354  ***************************************************************************************/
       
   355 TResult CSPppcontroller::GetInterfaceName()
       
   356 {
       
   357 	TResult rv;
       
   358 	string line;
       
   359 	char interface_name[MAXINTERFACENAMESIZE];
       
   360 
       
   361 	// clear the interface (so if not found we don't have an old value)
       
   362 	iInterfaceName = "";
       
   363 
       
   364 	// we want to know the interface name, on line "Using interface pppX";
       
   365 	rv = GetTokenFromFile( "Using", &line, MM_FIRST );
       
   366 	if( rv.iStandardResult != ERR_NONE ) {
       
   367 		if( rv.iStandardResult == ERR_INVALIDARG ) {
       
   368 			rv.iStandardResult = ERR_NONE;
       
   369 			rv.iExtendedCode = 0;
       
   370 		}
       
   371 		return rv;
       
   372 	}
       
   373 
       
   374 	// now grab the interface name
       
   375 	sscanf( line.c_str(), "Using interface %s", interface_name );
       
   376 	iInterfaceName = interface_name;	
       
   377 	
       
   378 	// done - success;	
       
   379 	return rv;
       
   380 }
       
   381 
       
   382 
       
   383 /****************************************************************************************
       
   384  * 
       
   385  * PRIVATE FUNCTION: GetIPAddresses
       
   386  * 
       
   387  ***************************************************************************************/
       
   388 TResult CSPppcontroller::GetIPAddresses()
       
   389 {
       
   390 	TResult rv;
       
   391 	string line;
       
   392 	char address[MAXINTERFACENAMESIZE];
       
   393 
       
   394 	// clear old values
       
   395 	iLocalIPAddress = iRemoteIPAddress = 0;
       
   396 
       
   397 	// we want to know the local IP address
       
   398 	rv = GetTokenFromFile( "local  IP address", &line, MM_LAST );
       
   399 	if( rv.iStandardResult != ERR_NONE ) {
       
   400 	  if( rv.iStandardResult == ERR_INVALIDARG ) {
       
   401 	    rv.iStandardResult = ERR_NONE;
       
   402 	    rv.iExtendedCode = 0;
       
   403 	  }
       
   404 	  return rv;
       
   405 	}
       
   406 
       
   407 	// grab address
       
   408 	sscanf( line.c_str(), "local  IP address %s", address );
       
   409 	iLocalIPAddress = inet_addr( address );
       
   410 
       
   411 	// we want to know the remote IP address
       
   412 	rv = GetTokenFromFile( "remote IP address", &line, MM_LAST );
       
   413 	if( rv.iStandardResult != ERR_NONE ) {
       
   414 	  if( rv.iStandardResult == ERR_INVALIDARG ) {
       
   415 	    rv.iStandardResult = ERR_NONE;
       
   416 	    rv.iExtendedCode = 0;
       
   417 	  }
       
   418 	  return rv;
       
   419 	}
       
   420 
       
   421 	// grab address
       
   422 	sscanf( line.c_str(), "remote IP address %s", address );
       
   423 	iRemoteIPAddress = inet_addr( address );
       
   424 
       
   425 	// done - success;	
       
   426 	return rv;
       
   427 }
       
   428 
       
   429 
       
   430 /****************************************************************************************
       
   431  * 
       
   432  * PRIVATE FUNCTION: UpdateSessionStatus
       
   433  * 
       
   434  ***************************************************************************************/
       
   435 TResult CSPppcontroller::UpdateSessionStatus()
       
   436 {
       
   437 	TResult rv = { ERR_NONE, 0, 0 };
       
   438 	TFUError ferr;
       
   439 	int mline[MAXLISTLINES], match_count, errcode, once_connected, disconnected;
       
   440 	char logfilename[MAXLOGNAME];
       
   441 	struct stat fstat;
       
   442 
       
   443 	// get the logfilename 
       
   444 	snprintf( logfilename, MAXLOGNAME, "pppd%04d.log", iKey );
       
   445 
       
   446 	// see if the file exists - if not then the status is SS_NOT_STARTED
       
   447 	errcode = stat( logfilename, &fstat );
       
   448 	if( (errcode == -1) && (errno == ENOENT) ) {
       
   449 		iSessionStatus = SS_NOT_STARTED;
       
   450 		return rv;
       
   451 	}
       
   452 
       
   453 	// look for the line with the CONNECTED_STRING prefix
       
   454 	match_count = MAXLISTLINES;
       
   455 	ferr = FindMatchingLinesByRawPrefix( logfilename, CONNECTED_STRING, mline, &match_count, &errcode );
       
   456 	if( ferr != FUE_NONE ) {
       
   457 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   458 		rv.iExtendedCode = (int)ferr;
       
   459 		rv.iSystemError = errcode;
       
   460 		return rv;
       
   461 	}
       
   462 	once_connected = ((match_count > 0) ? 1 : 0);
       
   463 
       
   464 	// look for the line with the DISCONNECTED_STRING prefix
       
   465 	match_count = MAXLISTLINES;
       
   466 	ferr = FindMatchingLinesByRawPrefix( logfilename, DISCONNECTED_STRING, mline, &match_count, &errcode );
       
   467 	if( ferr != FUE_NONE ) {
       
   468 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   469 		rv.iExtendedCode = (int)ferr;
       
   470 		rv.iSystemError = errcode;
       
   471 		return rv;
       
   472 	}
       
   473 	disconnected  = ((match_count > 0) ? 1 : 0);
       
   474 
       
   475 	// set the state based on the log
       
   476 	if( (once_connected == 0) && (disconnected == 0) ) {
       
   477 		iSessionStatus = SS_CONNECTING;
       
   478 	} else if( (once_connected == 1) && (disconnected == 0) ) {
       
   479 		iSessionStatus = SS_CONNECTED;
       
   480 	} else if( (once_connected == 1) && (disconnected == 1) ) {
       
   481 		iSessionStatus = SS_DISCONNECTED;
       
   482 	} else if( (once_connected == 0) && (disconnected == 1) ) {
       
   483 		iSessionStatus = SS_DISCONNECTED;
       
   484 	}
       
   485 			
       
   486 	// done - success
       
   487 	return rv;
       
   488 }
       
   489 
       
   490 
       
   491 /*******************************************************************************
       
   492  *
       
   493  * PRIVATE FUNCTION: CopyFileIntoBuffer
       
   494  *
       
   495  ******************************************************************************/
       
   496 TResult CSPppcontroller::CopyFileIntoBuffer( char *aFilename, TVarData *aData )
       
   497 {
       
   498 	TResult rv = { ERR_NONE, 0, 0 };
       
   499 	int err;
       
   500 	int filesize;
       
   501 	int i;
       
   502 	char c;
       
   503 	FILE *fp;
       
   504 
       
   505 	// check params
       
   506 	assert( aFilename != NULL );
       
   507 	assert( aData != NULL );
       
   508 
       
   509 	// initialise the data to empty
       
   510 	aData->TVarData_val = NULL;
       
   511 	aData->TVarData_len = 0;
       
   512 
       
   513 	// open the logfile
       
   514 	fp = fopen( aFilename, "rb" );
       
   515 	if( fp == NULL ) {
       
   516 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   517 		rv.iExtendedCode = 0;
       
   518 		rv.iSystemError = errno;
       
   519 		return rv;
       
   520 	}
       
   521 
       
   522 	// get the file size
       
   523 	fseek( fp, 0, SEEK_END );
       
   524 	filesize = ftell( fp );
       
   525 	fseek( fp, 0, SEEK_SET );
       
   526 
       
   527 	// if the filesize is zero then just return
       
   528 	if( filesize == 0 ) {
       
   529 	  return rv;
       
   530 	}
       
   531 
       
   532 	// allocate space for the file data
       
   533 	aData->TVarData_val = (char*)calloc( (filesize + 1), 1 );
       
   534 	assert( aData->TVarData_val != NULL );
       
   535 	aData->TVarData_len = (filesize + 1);
       
   536 
       
   537 	// now copy the data
       
   538 	for( i = 0; i < filesize; i++ ) {
       
   539 
       
   540 		// read one byte
       
   541 		err = fread( &c, 1, 1, fp );
       
   542 		if( err != 1 ) {
       
   543 			free( aData->TVarData_val );
       
   544 			aData->TVarData_val = NULL;
       
   545 			aData->TVarData_len = 0;
       
   546 			rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   547 			rv.iExtendedCode = 0;
       
   548 			rv.iSystemError = errno;
       
   549 			fclose( fp );
       
   550 			return rv;
       
   551 		}
       
   552 
       
   553 		// save the byte in the buffer
       
   554 		(aData->TVarData_val)[i] = c;
       
   555 	}
       
   556 
       
   557 	// cleanup and return 
       
   558 	fclose( fp );
       
   559 	return rv;
       
   560 }
       
   561 
       
   562 
       
   563 /****************************************************************************************
       
   564  * 
       
   565  * PRIVATE FUNCTION: GetTokenFromFile
       
   566  * 
       
   567  ***************************************************************************************/
       
   568 TResult CSPppcontroller::GetTokenFromFile( string aPrefix, string *aLine, TMultiMatchSemantics aMultiMatchSemantics )
       
   569 {
       
   570 	TResult rv = { ERR_NONE, 0, 0 };
       
   571 	TFUError ferr;
       
   572 	char logfilename[MAXLOGNAME];
       
   573 	char cline[MAXLOGLINELENGTH];
       
   574 	int mline[MAXLISTLINES], match_count, errcode, entry;
       
   575 	struct stat fstat;
       
   576 
       
   577 	// check params
       
   578 	assert( aLine != NULL );
       
   579 
       
   580 	// get the logfilename 
       
   581 	snprintf( logfilename, MAXLOGNAME, "pppd%04d.log", iKey );
       
   582 
       
   583 	//	fprintf( stderr, "DEBUG: request for lines starting with '%s'\n", aPrefix.c_str() );
       
   584 
       
   585 	// see if the file exists - if not then just return - this is NOT an error
       
   586 	errcode = stat( logfilename, &fstat );
       
   587 	if( (errcode == -1) && (errno == ENOENT) ) {
       
   588 		rv.iStandardResult = ERR_INVALIDARG;
       
   589 		rv.iExtendedCode = 1;
       
   590 		//		fprintf( stderr, "DEBUG: file doesn't exist.\n" );
       
   591 		return rv;
       
   592 	}
       
   593 
       
   594 	// look for the lines that match the prefix
       
   595 	match_count = MAXLISTLINES;
       
   596 	ferr = FindMatchingLinesByRawPrefix( logfilename, (char*)aPrefix.c_str(), mline, &match_count, &errcode );
       
   597 	if( ferr != FUE_NONE ) {
       
   598 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   599 		rv.iExtendedCode = (int)ferr;
       
   600 		rv.iSystemError = errcode;
       
   601 		//		fprintf( stderr, "DEBUG: error parsing logfile.\n" );
       
   602 		return rv;
       
   603 	}
       
   604 
       
   605 	// if there is no match then nothing to do
       
   606 	if( match_count == 0 ) {
       
   607 		rv.iStandardResult = ERR_INVALIDARG;
       
   608 		rv.iExtendedCode = 2;
       
   609 		//		fprintf( stderr, "DEBUG: no matches.\n" );
       
   610 		return rv;
       
   611 	}
       
   612 
       
   613 	// if there are multiple matches then print warning (just for the admin)
       
   614 	if( match_count > 1 ) {
       
   615 	  //	  fprintf( stderr, "WARNING: multiple matches for token \"%s\" in '%s'.\n", aPrefix.c_str(), logfilename );  
       
   616 	}
       
   617 
       
   618 	// handle multiple matches
       
   619 	if( (aMultiMatchSemantics == MM_ERROR) && (match_count > 1) ) {
       
   620 		rv.iStandardResult = ERR_MULTIPLE_MATCHES_IN_LOG_FILE;
       
   621 		return rv;
       
   622 	}
       
   623 	entry = ((aMultiMatchSemantics == MM_FIRST) ? 0 : (match_count-1));
       
   624 
       
   625 	// now get the matching line 
       
   626 	ferr = GetLine( logfilename, mline[entry], cline, MAXLOGLINELENGTH, &errcode );
       
   627 	if( ferr != FUE_NONE ) {
       
   628 		rv.iStandardResult = ERR_LOG_FILE_ERROR;
       
   629 		rv.iExtendedCode = (int)ferr;
       
   630 		rv.iSystemError = errcode;
       
   631 		//		fprintf( stderr, "DEBUG: error getting the matching line.\n" );
       
   632 		return rv;
       
   633 	}
       
   634 
       
   635 	// copy the line into the passed buffer
       
   636 	(*aLine) = cline;
       
   637 	//	fprintf( stderr, "DEBUG: successfully retrieved line '%s'\n", aLine->c_str() );
       
   638 
       
   639 	// done - success;	
       
   640 	return rv;
       
   641 }
       
   642