networkingtestandutils/exampleinternetutilities/PING/PING.CPP
changeset 0 af10295192d8
child 37 052078dda061
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2001-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 // Started by MWT, June 1997
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <pingeng.h>
       
    19 #include <e32cons.h>
       
    20 #include <c32comm.h>
       
    21 #include <nifman.h>
       
    22 
       
    23 #ifdef __WINS__
       
    24 #define CDRV1_PATH _L("ECDRV")
       
    25 #define COMM_PATH _L("ECOMM")
       
    26 #endif
       
    27 
       
    28 LOCAL_C TInt ProgramL();
       
    29 
       
    30 const TInt  KHistoryBufferSize = 20;
       
    31 
       
    32 _LIT(KPrompt, "Command>");
       
    33 
       
    34 const TInt KPromptLength = 8;
       
    35 const TInt KQuitReturn = -100;
       
    36 const TInt KHelpReturn = -101;
       
    37 
       
    38 class CCircList : public CBase
       
    39 	{
       
    40 public:
       
    41 	CCircList(TInt aLength);
       
    42 	~CCircList();
       
    43 	TInt Add(const TDesC& aLine);
       
    44 	const HBufC& operator[](TInt anIndex) const;
       
    45 	TInt Count() const;
       
    46 
       
    47 private:
       
    48 	RArray<HBufC*> iBufPtrArray;
       
    49 	TInt iMaxLength;
       
    50 	};
       
    51 
       
    52 class CPingTestKeyStroke;
       
    53 class CPingTestUi : public CBase, public MPingNotificationHandler
       
    54     {
       
    55 public:
       
    56 	static CPingTestUi* NewL();
       
    57 	void StartL(const TPingOptions& aOptions);
       
    58 	~CPingTestUi();
       
    59 	
       
    60 	virtual void Pinging(const TNameRecord& aRecord, TInt aBytes) const;
       
    61 	virtual void Sent() const;
       
    62 	virtual void Reply(const TInetAddr& aFrom, TInt aBytes, TInt aSeq, TTimeIntervalMicroSeconds32 aTime) const;
       
    63 	virtual void Icmp4Message(const TInetAddr& aFrom, TInt aType, TInt aCode, const TDesC8& aRestOfIt) const;
       
    64 	virtual void Icmp6Message(const TInetAddr& aFrom, TInt aType, TInt aCode) const;
       
    65 	virtual void Finished(const TNameRecord& aRecord, TInt aNrTransmitted, TInt aNrReceived, TInt aNrDuplicates, TInt aMin, TInt aMax, TInt aSum, TInt aError);
       
    66 
       
    67 	void SetParams(TBool aFloodFormat);
       
    68 	void SetKeyStrokeActive();
       
    69 
       
    70 	void KeyStroke();
       
    71 	void KeyStrokeDoCancel();
       
    72 	void AddToHistory(const TDesC& aLine);
       
    73 	void DisplayHistory(TInt aLine, TInt aPromptLen, TDes& aDes) const;
       
    74 	inline CConsoleBase& Console() { return *iConsole; }
       
    75 
       
    76 	TInt NrReceivedPackets() const;
       
    77 
       
    78 private:
       
    79 	void ConstructL();
       
    80 	void InitialiseL();
       
    81 
       
    82 private:
       
    83 	TBool iFloodFormat;
       
    84 	CConsoleBase* iConsole;
       
    85 	CPingTestKeyStroke* iKeyHandler;
       
    86 	CCircList* iHistory;
       
    87 	CPingEng* iEngine;
       
    88 	TInt iNrReceivedPackets;
       
    89 	};
       
    90 
       
    91 inline TInt CPingTestUi::NrReceivedPackets() const
       
    92 	{
       
    93 	
       
    94 	return iNrReceivedPackets;
       
    95 	}
       
    96 
       
    97 class CPingTestKeyStroke : public CActive
       
    98 	{
       
    99 public:
       
   100 	CPingTestKeyStroke(CPingTestUi& aUi);
       
   101 	~CPingTestKeyStroke();
       
   102 	void ReStart();
       
   103 	void RunL();
       
   104 	void DoCancel();
       
   105 private:
       
   106 	CPingTestUi& iUi;
       
   107 	};
       
   108 
       
   109 class TPingTestParser
       
   110 	{
       
   111 public:
       
   112 	// returns EFalse if the user wants to exit, ETrue otherwise
       
   113 	TBool ParseCommandLine(CPingTestUi& aUI);
       
   114 	TInt ParseCLArguments(TDes& aCommandLine,CPingTestUi& aUI);
       
   115 	
       
   116 	const TPingOptions& Options() const;
       
   117 	const TPtrC& BadArgument() const;
       
   118 
       
   119 private:
       
   120 	TPingOptions iOptions;
       
   121 	TPtrC iCLArgument;
       
   122 	};
       
   123 
       
   124 const TPtrC& TPingTestParser::BadArgument() const
       
   125 	{
       
   126 	
       
   127 	return iCLArgument;
       
   128 	}
       
   129 
       
   130 
       
   131 GLDEF_C TInt E32Main()
       
   132 	{
       
   133 	
       
   134 	__UHEAP_MARK;
       
   135 	// Standard stuff
       
   136 	CTrapCleanup* trap = CTrapCleanup::New();
       
   137 	if(trap==NULL)
       
   138 		return KErrNoMemory;
       
   139 
       
   140 	TRAPD(err, ProgramL());
       
   141 	
       
   142 	delete trap;
       
   143 	__UHEAP_MARKEND;
       
   144 	return err;
       
   145 	}
       
   146 
       
   147 LOCAL_C TInt ProgramL()
       
   148 	{
       
   149 	
       
   150 	TInt ret = 0;
       
   151 
       
   152 	CActiveScheduler* as = new(ELeave) CActiveScheduler;
       
   153 	CleanupStack::PushL(as);
       
   154 	CActiveScheduler::Install(as);
       
   155 
       
   156 	HBufC *argsBuf = HBufC::NewMaxLC(512);
       
   157 	TPtr args(argsBuf->Des());
       
   158 	User::CommandLine(args);
       
   159 
       
   160 	CPingTestUi* ui=CPingTestUi::NewL();
       
   161 	CleanupStack::PushL(ui);
       
   162 
       
   163 	TPingTestParser parser;
       
   164 	if (args.Length()!=0)
       
   165 		{
       
   166 		TInt res = parser.ParseCLArguments(args,*ui);
       
   167 		if (res != KErrNone)
       
   168 			{
       
   169 			ui->Console().Printf(_L("Invalid argument %S - result %d\n"), &parser.BadArgument(), res);
       
   170 			}
       
   171 		else
       
   172 			{
       
   173 			ui->SetParams(parser.Options().iInterval.Int()<200000);
       
   174 			ui->SetKeyStrokeActive();
       
   175 			ui->StartL(parser.Options());
       
   176 			CActiveScheduler::Start();
       
   177 			}
       
   178 	    ui->Console().Getch();
       
   179 		}
       
   180 	else
       
   181 		for(;;)
       
   182 		{
       
   183 			ret = parser.ParseCommandLine(*ui);
       
   184 			if(ret == KQuitReturn)
       
   185 				{
       
   186 				break;
       
   187 				}
       
   188 			if(ret != KHelpReturn)
       
   189 			{
       
   190 				ui->SetParams(parser.Options().iInterval.Int()<200000);
       
   191 				ui->SetKeyStrokeActive();
       
   192 				ui->StartL(parser.Options());
       
   193 				CActiveScheduler::Start();
       
   194 			}
       
   195 		}
       
   196 
       
   197 	ret = ui->NrReceivedPackets() == 0;
       
   198 
       
   199 	CleanupStack::PopAndDestroy(ui);
       
   200 	CleanupStack::PopAndDestroy(argsBuf);
       
   201 	CleanupStack::PopAndDestroy(as);
       
   202 
       
   203 	return ret;
       
   204 	}
       
   205 
       
   206 CPingTestUi* CPingTestUi::NewL()
       
   207 //
       
   208 // Create new test UI
       
   209 //
       
   210 	{
       
   211 
       
   212 	CPingTestUi* ui = new (ELeave) CPingTestUi;
       
   213 	CleanupStack::PushL(ui);
       
   214 	ui->ConstructL();
       
   215 	CleanupStack::Pop(ui);
       
   216 	return ui;
       
   217 	}
       
   218 
       
   219 void CPingTestUi::InitialiseL()
       
   220 //
       
   221 // Ensure stuff is loaded etc
       
   222 //
       
   223 	{
       
   224 	
       
   225 #ifndef __EPOC32__
       
   226 	User::LoadPhysicalDevice(CDRV1_PATH);    
       
   227     	User::LoadLogicalDevice(COMM_PATH);
       
   228 #endif
       
   229 
       
   230 	
       
   231 //	User::LeaveIfError(Nifman::CheckIniConfig());
       
   232 	}
       
   233 
       
   234 
       
   235 void CPingTestUi::ConstructL()
       
   236 //
       
   237 // Contruct engine and console
       
   238 //
       
   239 	{
       
   240 	
       
   241 	iConsole = Console::NewL(_L("Ping"),TSize(KConsFullScreen,KConsFullScreen));
       
   242 	_LIT(KPhbkSyncCMI, "phbsync.cmi");
       
   243     	(void)StartC32WithCMISuppressions(KPhbkSyncCMI);
       
   244 	
       
   245 	iEngine=CPingEng::NewL(*this);
       
   246 	iKeyHandler = new (ELeave) CPingTestKeyStroke(*this);
       
   247 	iHistory = new (ELeave) CCircList(KHistoryBufferSize);
       
   248 		
       
   249 	InitialiseL();
       
   250 	}
       
   251 
       
   252 CPingTestUi::~CPingTestUi()
       
   253 //
       
   254 // Delete console
       
   255 //
       
   256 	{
       
   257 	
       
   258 	delete iHistory;
       
   259 	delete iKeyHandler;
       
   260 	delete iConsole;
       
   261 	delete iEngine;
       
   262 	}
       
   263 
       
   264 void CPingTestUi::StartL(const TPingOptions& aOptions)
       
   265 	{
       
   266 	
       
   267 	iEngine->StartL(aOptions);
       
   268 	}
       
   269 
       
   270 void CPingTestUi::AddToHistory(const TDesC& aLine)
       
   271 //
       
   272 // Add line to history buffer
       
   273 //
       
   274 	{
       
   275 	
       
   276 	iHistory->Add(aLine);
       
   277 	}
       
   278 	
       
   279 void CPingTestUi::DisplayHistory(TInt aLine, TInt aPromptLen, TDes& aDes) const
       
   280 	{
       
   281 	
       
   282 	aDes.SetLength(0);
       
   283 	if(!iHistory->Count())
       
   284 		{
       
   285 		return;
       
   286 		}
       
   287 	const HBufC& line=(*iHistory)[aLine];
       
   288 	iConsole->SetPos(aPromptLen);
       
   289 	iConsole->ClearToEndOfLine();
       
   290 	iConsole->Write(line);
       
   291 	aDes.Append(line);
       
   292 	}
       
   293 
       
   294 void CPingTestUi::Pinging(const TNameRecord& aRecord, TInt aBytes) const
       
   295 //
       
   296 //	Who are we pinging ??
       
   297 //
       
   298 	{
       
   299 
       
   300 	TName ipaddr;
       
   301 	TInetAddr& addr = (TInetAddr&)aRecord.iAddr;
       
   302 	addr.Output(ipaddr);
       
   303 
       
   304 	if(aRecord.iName.Length())
       
   305 		{
       
   306 		iConsole->Printf(_L("Pinging %S [%S] with %d bytes of data\n"), &aRecord.iName, &ipaddr, aBytes);
       
   307 		}
       
   308 	else
       
   309 		{
       
   310 		iConsole->Printf(_L("Pinging %S with %d bytes of data\n"), &ipaddr, aBytes);
       
   311 		}
       
   312 	}
       
   313 
       
   314 void CPingTestUi::Reply(const TInetAddr& aFrom, TInt aBytes, TInt aIcmpSeq, TTimeIntervalMicroSeconds32 aTime) const
       
   315 //
       
   316 // Reply from remote host
       
   317 //
       
   318 	{
       
   319 
       
   320 	if(iFloodFormat)
       
   321 		{
       
   322 		iConsole->Write(_L("."));
       
   323 		return;
       
   324 		}
       
   325 
       
   326 	TName inetaddr;
       
   327 	aFrom.Output(inetaddr);
       
   328 
       
   329 	if(aTime.Int()<15000)
       
   330 		{
       
   331 		iConsole->Printf(_L("Reply from %S  len=%d seq %d time<15ms\n"), &inetaddr, aBytes, aIcmpSeq);
       
   332 		}
       
   333 	else
       
   334 		{
       
   335 		iConsole->Printf(_L("Reply from %S  len=%d seq %d time=%dms\n"), &inetaddr, aBytes, aIcmpSeq, aTime.Int()/1000);
       
   336 		}
       
   337 	}
       
   338 
       
   339 void CPingTestUi::Sent() const
       
   340 //
       
   341 //
       
   342 //
       
   343 	{
       
   344 
       
   345 	if(iFloodFormat)
       
   346 		{
       
   347 		iConsole->Write(_L("\b"));
       
   348 		}
       
   349 	}
       
   350 
       
   351 void CPingTestUi::Icmp4Message(const TInetAddr& aAddr, TInt aType, TInt aCode, const TDesC8& aData) const
       
   352 //
       
   353 //
       
   354 //
       
   355 	{
       
   356 
       
   357 	TBuf<39> inetaddr;
       
   358 	aAddr.Output(inetaddr);
       
   359 	iConsole->Printf(_L("Reply from %S: len=%d "), &inetaddr, aData.Length()+4);
       
   360 
       
   361 	switch(aType)
       
   362 		{
       
   363 	case KIPv4PingTypeEchoReply:
       
   364 		iConsole->Printf(_L("Echo Reply\n"));
       
   365 		break;
       
   366 
       
   367 	case KIPv4PingTypeUnreachable:
       
   368 		switch(aCode)
       
   369 			{
       
   370 		case KIPv4PingCodeUnreachNet:
       
   371 			iConsole->Printf(_L("Destination Net Unreachable\n"));
       
   372 			break;
       
   373 		case KIPv4PingCodeUnreachHost:
       
   374 			iConsole->Printf(_L("Destination Host Unreachable\n"));
       
   375 			break;
       
   376 		case KIPv4PingCodeUnreachProtocol:
       
   377 			iConsole->Printf(_L("Destination Protocol Unreachable\n"));
       
   378 			break;
       
   379 		case KIPv4PingCodeUnreachPort:
       
   380 			iConsole->Printf(_L("Destination Port Unreachable\n"));
       
   381 			break;
       
   382 		case KIPv4PingCodeUnreachNeedFrag:
       
   383 			iConsole->Printf(_L("Fragmentation Needed and DF Set\n"));
       
   384 			break;
       
   385 		case KIPv4PingCodeUnreachSrcRouteFail:
       
   386 			iConsole->Printf(_L("Source Route Failed\n"));
       
   387 			break;
       
   388 		case KIPv4PingCodeUnreachNetUnknown:
       
   389 			iConsole->Printf(_L("Destination Network Unknown\n"));
       
   390 			break;
       
   391 		case KIPv4PingCodeUnreachHostUnknown:
       
   392 			iConsole->Printf(_L("Destination Host Unknown\n"));
       
   393 			break;
       
   394 		case KIPv4PingCodeUnreachSrcHostIsolated:
       
   395 			iConsole->Printf(_L("Source Host Isolated\n"));
       
   396 			break;
       
   397 		case KIPv4PingCodeUnreachNetProhibited:
       
   398 			iConsole->Printf(_L("Destination Network Prohibited\n"));
       
   399 			break;
       
   400 		case KIPv4PingCodeUnreachHostProhibited:
       
   401 			iConsole->Printf(_L("Destination Host Prohibited\n"));
       
   402 			break;
       
   403 		case KIPv4PingCodeUnreachNetTOS:
       
   404 			iConsole->Printf(_L("Network Unreachable for TOS\n"));
       
   405 			break;
       
   406 		case KIPv4PingCodeUnreachHostTOS:
       
   407 			iConsole->Printf(_L("Host Unreachable for TOS\n"));
       
   408 			break;
       
   409 		case KIPv4PingCodeUnreachProhibited:
       
   410 			iConsole->Printf(_L("Prohibited by Filtering\n"));
       
   411 			break;
       
   412 		case KIPv4PingCodeUnreachPrecVolation:
       
   413 			iConsole->Printf(_L("Precedence Violation\n"));
       
   414 			break;
       
   415 		case KIPv4PingCodeUnreachPrecCutoff:
       
   416 			iConsole->Printf(_L("Precedence Cutoff in Effect\n"));
       
   417 			break;
       
   418 
       
   419 		default:
       
   420 			iConsole->Printf(_L("Dest Unreachable, Bad Code: %d\n"), aCode);
       
   421 			break;
       
   422 			}
       
   423 		break;
       
   424 
       
   425 	case KIPv4PingTypeSourceQuench:
       
   426 		iConsole->Printf(_L("Source Quench\n"));
       
   427 		break;
       
   428 
       
   429 	case KIPv4PingTypeRedirect:
       
   430 		switch(aCode) 
       
   431 			{
       
   432 		case KIPv4PingCodeRedirectNet:
       
   433 			iConsole->Printf(_L("Redirect Network"));
       
   434 			break;
       
   435 		case KIPv4PingCodeRedirectHost:
       
   436 			iConsole->Printf(_L("Redirect Host"));
       
   437 			break;
       
   438 		case KIPv4PingCodeRedirectNetTOS:
       
   439 			iConsole->Printf(_L("Redirect Type of Service and Network"));
       
   440 			break;
       
   441 		case KIPv4PingCodeRedirectHostTOS:
       
   442 			iConsole->Printf(_L("Redirect Type of Service and Host"));
       
   443 			break;
       
   444 		default:
       
   445 			iConsole->Printf(_L("Redirect, Bad Code: %d"), aCode);
       
   446 			break;
       
   447 			}
       
   448 		iConsole->Printf(_L("(New addr: %d.%d.%d.%d)\n"), aData[0], aData[1], aData[2], aData[3]);
       
   449 		break;
       
   450 
       
   451 	case KIPv4PingTypeTimeExceeded:
       
   452 		switch(aCode)
       
   453 			{
       
   454 		case KIPv4PingCodeExceedInTransit:
       
   455 			iConsole->Printf(_L("Time to live exceeded\n"));
       
   456 			break;
       
   457 		case KIPv4PingCodeExceedInReasm:
       
   458 			iConsole->Printf(_L("Frag reassembly time exceeded\n"));
       
   459 			break;
       
   460 		default:
       
   461 			iConsole->Printf(_L("Time exceeded, Bad Code: %d\n"), aCode);
       
   462 			break;
       
   463 			}
       
   464 		break;
       
   465 	case KIPv4PingTypeBadParameter:
       
   466 		iConsole->Printf(_L("Parameter problem: pointer = 0x%02x\n"), *(TUint*)aData.Ptr());
       
   467 		break;
       
   468 	default:
       
   469 		iConsole->Printf(_L("Bad ICMPv4 type: %d\n"), aType);
       
   470 		}
       
   471 	}
       
   472 
       
   473 void CPingTestUi::Icmp6Message(const TInetAddr& aAddr, TInt aType, TInt aCode) const
       
   474 	{
       
   475 	
       
   476 	TBuf<39> inetaddr;
       
   477 	aAddr.Output(inetaddr);
       
   478 	iConsole->Printf(_L("Reply from %S: "), &inetaddr);
       
   479 
       
   480 	switch(aType)
       
   481 		{
       
   482 		case KIPv6PingTypeEchoReply:
       
   483 			iConsole->Printf(_L("Echo Reply\n"));
       
   484 			break;
       
   485 
       
   486 		case KIPv6PingTypeUnreachable:
       
   487 			switch (aType)
       
   488 				{
       
   489 			case KIPv6PingCodeNoRoute:
       
   490 				iConsole->Printf(_L("No route to destination\n"));
       
   491 				break;
       
   492 			case KIPv6PingCodeAdminProhibited:
       
   493 				iConsole->Printf(_L("Communication administratively prohibited\n"));
       
   494 				break;
       
   495 			case KIPv6PingCodeAddressUnreachable:
       
   496 				iConsole->Printf(_L("Address unreachable\n"));
       
   497 				break;
       
   498 			case KIPv6PingCodePortUnreachable:
       
   499 				iConsole->Printf(_L("Port unreachable\n"));
       
   500 				break;
       
   501 			default:
       
   502 				iConsole->Printf(_L("Unreachable, bad code: %d\n"), aCode);
       
   503 				break;
       
   504 				}
       
   505 			break;
       
   506 
       
   507 		case KIPv6PingTypePacketTooBig:
       
   508 			iConsole->Printf(_L("Packet too big"));
       
   509 			break;
       
   510 
       
   511 		case KIPv6PingTypeTimeExeeded:
       
   512 			switch (aType)
       
   513 				{
       
   514 			case KIPv6PingCodeHLExeeded:
       
   515 				iConsole->Printf(_L("Hop limit exceeded in transit\n"));
       
   516 				break;
       
   517 			case KIPv6PingCodeFragReassemblyExeeded:
       
   518 				iConsole->Printf(_L("Fragment reassembly time exceeded\n"));
       
   519 				break;
       
   520 			default:
       
   521 				iConsole->Printf(_L("Time exceeded, bad code: %d\n"), aCode);
       
   522 				}
       
   523 			break;
       
   524 
       
   525 		case KIPv6PingTypeParamProblem:
       
   526 			switch(aCode)
       
   527 				{
       
   528 			case KIPv6PingCodeErroneousHeader:
       
   529 				iConsole->Printf(_L("Erroneous header field encountered\n"));
       
   530 				break;
       
   531 			case KIPv6PingCodeNextHeaderUnrecognised:
       
   532 				iConsole->Printf(_L("Unrecognised next header type encountered\n"));
       
   533 				break;
       
   534 			case KIPv6PingCodeIPv6OptionUnrecognised:
       
   535 				iConsole->Printf(_L("Unrecognised IPv6 option encountered\n"));
       
   536 				break;
       
   537 			default:
       
   538 				iConsole->Printf(_L("Parameter problem, bad code: %d\n"), aCode);
       
   539 				break;
       
   540 				}
       
   541 			break;
       
   542 
       
   543 		default:
       
   544 			iConsole->Printf(_L("Bad ICMPv6 type, value: %d\n"), aType);
       
   545 		}
       
   546 	}
       
   547 
       
   548 void CPingTestUi::SetParams(TBool aFloodFormat)
       
   549 //
       
   550 // 
       
   551 //
       
   552 	{
       
   553 	
       
   554 	iFloodFormat=aFloodFormat;
       
   555 	}
       
   556 
       
   557 void CPingTestUi::Finished(const TNameRecord& aRecord, TInt aNrTransmitted, TInt aNrReceived, TInt, TInt aMin, TInt aMax, TInt aSum, TInt aError)
       
   558 //
       
   559 // Pinger finished
       
   560 //
       
   561 	{
       
   562 
       
   563 	iConsole->Printf(_L("\n"));
       
   564 	if(aError!=KErrNone)
       
   565 		{
       
   566 		iConsole->Printf(_L("Error %d\n"),aError);
       
   567 		}
       
   568 
       
   569 	if(aNrTransmitted)
       
   570 		{
       
   571 
       
   572 		
       
   573 		TInt loss = aNrReceived>=aNrTransmitted ? 0 : (aNrTransmitted-aNrReceived)*100/aNrTransmitted;
       
   574 
       
   575 		iConsole->Printf(_L("Statistics %S\n"), &aRecord.iName);
       
   576 		iConsole->Printf(_L("%d transmitted %d received %d%% packet loss\n"), aNrTransmitted, aNrReceived, loss);
       
   577 
       
   578 		if(aNrReceived)
       
   579 			{
       
   580 			TInt avg = aSum/aNrReceived/1000;
       
   581 			iConsole->Printf(_L("Round-trip %d min %d avg %d max\n"), aMin/1000, avg, aMax/1000);
       
   582 			}
       
   583 
       
   584 		iNrReceivedPackets = aNrReceived;
       
   585 		}
       
   586 	else
       
   587 		{
       
   588 		iNrReceivedPackets = 0;
       
   589 		}
       
   590 	iKeyHandler->Cancel();
       
   591 	CActiveScheduler::Stop();
       
   592 	}	
       
   593 
       
   594 void CPingTestUi::KeyStroke()
       
   595 //
       
   596 // Key was pressed
       
   597 //
       
   598 	{
       
   599 	
       
   600 	if(iKeyHandler->iStatus==KErrNone)
       
   601 		{
       
   602 
       
   603 	    	if(iConsole->KeyCode()==EKeyEscape)
       
   604 			{
       
   605 			iConsole->Printf(_L("\nAborted\n"));
       
   606 			iEngine->CancelAndFinished();
       
   607 			return;
       
   608 			}
       
   609 
       
   610 		}
       
   611 	SetKeyStrokeActive();
       
   612 	}
       
   613 
       
   614 void CPingTestUi::SetKeyStrokeActive()
       
   615 //
       
   616 //
       
   617 //
       
   618 	{
       
   619 	
       
   620 	iConsole->Read(iKeyHandler->iStatus);
       
   621 	iKeyHandler->ReStart();
       
   622 	}
       
   623 
       
   624 void CPingTestUi::KeyStrokeDoCancel()
       
   625 //
       
   626 // Cancel the read
       
   627 //
       
   628 	{
       
   629 
       
   630 	iConsole->ReadCancel();
       
   631 	}
       
   632 
       
   633 CPingTestKeyStroke::CPingTestKeyStroke(CPingTestUi& aUi)
       
   634 //
       
   635 // Key reader
       
   636 //
       
   637 	: CActive(0), iUi(aUi)
       
   638 	{
       
   639 	
       
   640 	CActiveScheduler::Add(this);
       
   641 	}
       
   642 
       
   643 CPingTestKeyStroke::~CPingTestKeyStroke()
       
   644 //
       
   645 // Destruct means cancel
       
   646 //
       
   647 	{
       
   648 
       
   649 	Cancel();
       
   650 	}
       
   651 	
       
   652 void CPingTestKeyStroke::RunL()
       
   653 //
       
   654 // Key pressed
       
   655 //
       
   656 	{
       
   657 
       
   658 	iUi.KeyStroke();
       
   659 	}
       
   660 
       
   661 void CPingTestKeyStroke::DoCancel()
       
   662 //
       
   663 // Cancel key stroke
       
   664 //
       
   665 	{
       
   666 
       
   667 	iUi.KeyStrokeDoCancel();
       
   668 	}
       
   669 
       
   670 void CPingTestKeyStroke::ReStart()
       
   671 	{
       
   672 	
       
   673 	SetActive();
       
   674 	}
       
   675 
       
   676 const TPingOptions& TPingTestParser::Options() const
       
   677 	{
       
   678 	
       
   679 	return iOptions;
       
   680 	}
       
   681 
       
   682 
       
   683 TInt TPingTestParser::ParseCLArguments(TDes& aCommandLine,CPingTestUi& aUi)
       
   684 	{
       
   685 	iOptions = TPingOptions();	//Reset iOptions;
       
   686 	iOptions.iDestname = _L("127.0.0.1");
       
   687 	TLex lex(aCommandLine);
       
   688 
       
   689 	for(iCLArgument.Set(lex.NextToken()); iCLArgument.Length(); iCLArgument.Set(lex.NextToken()))
       
   690 		{
       
   691 		if(iCLArgument.Length()==2)
       
   692 			{
       
   693 			if(!iCLArgument.CompareF(_L("-A")))
       
   694 				{
       
   695 				iOptions.iResolveAddress=ETrue;
       
   696 				}
       
   697 			else
       
   698 				{
       
   699 				iOptions.iDestname=iCLArgument;
       
   700 				}
       
   701 			if(!iCLArgument.CompareF(_L("-C")))
       
   702 				{
       
   703 				iOptions.iPrompt = ETrue;
       
   704 				}
       
   705 			else
       
   706 				{
       
   707 				iOptions.iPrompt = EFalse;
       
   708 				}
       
   709 			}
       
   710 		else if(iCLArgument.Length()>2)
       
   711 			{
       
   712 
       
   713 			TLex val(iCLArgument.Mid(2));
       
   714 			TInt num;
       
   715 
       
   716 			TPtrC cmd = iCLArgument.Mid(0,2);
       
   717 
       
   718 			if(!cmd.CompareF(_L("-N")))
       
   719 				{
       
   720 				if(val.Val(iOptions.iNumberOfPings) != KErrNone)
       
   721 					{
       
   722 					return KErrArgument;
       
   723 					}
       
   724 				else if(iOptions.iNumberOfPings<0)
       
   725 					{
       
   726 					return KErrUnderflow;
       
   727 					}
       
   728 				}
       
   729 			else if(!cmd.CompareF(_L("-I")))
       
   730 				{
       
   731 				if(val.Val(num) != KErrNone)
       
   732 					{
       
   733 					return KErrArgument;
       
   734 					}
       
   735 				else if(num<0)
       
   736 					{
       
   737 					return KErrUnderflow;
       
   738 					}
       
   739 				else
       
   740 					{
       
   741 					iOptions.iInterval=num;
       
   742 					}
       
   743 				}
       
   744 			else if(!cmd.CompareF(_L("-W")))
       
   745 				{
       
   746 				if(val.Val(num) != KErrNone)
       
   747 					{
       
   748 					return KErrArgument;
       
   749 					}
       
   750 				else if(num<0)
       
   751 					{
       
   752 					return KErrUnderflow;
       
   753 					}
       
   754 				else
       
   755 					{
       
   756 					iOptions.iWait=num;
       
   757 					}
       
   758 				}
       
   759 			else if(!cmd.CompareF(_L("-S")))
       
   760 				{
       
   761 				if(val.Val(iOptions.iPingSize) != KErrNone)
       
   762 					{
       
   763 					return KErrArgument;
       
   764 					}
       
   765 				else if(iOptions.iPingSize<8)
       
   766 					{
       
   767 					return KErrUnderflow;
       
   768 					}
       
   769 				}
       
   770 			else if(!cmd.CompareF(_L("-P")))
       
   771 				{
       
   772 				if(val.Val(iOptions.iPreload) != KErrNone)
       
   773 					{
       
   774 					return KErrArgument;
       
   775 					}
       
   776 				else if(iOptions.iPreload<0)
       
   777 					{
       
   778 					return KErrUnderflow;
       
   779 					}
       
   780 				}
       
   781 			else if(!cmd.CompareF(_L("-B")))
       
   782 				{
       
   783 				if(val.Val(iOptions.iBacklog) != KErrNone)
       
   784 					{
       
   785 					return KErrArgument;
       
   786 					}
       
   787 				else if(iOptions.iBacklog<0)
       
   788 					{
       
   789 					return KErrUnderflow;
       
   790 					}
       
   791 				}
       
   792 			else if(!cmd.CompareF(_L("-C")))
       
   793 				{
       
   794 				// barf if connection override already set:
       
   795 				if(iOptions.iConnSnap || iOptions.iConnIap)
       
   796 					{
       
   797 					return KErrArgument;
       
   798 					}
       
   799 
       
   800 				// extract values from argument
       
   801 				TInt snap=0;
       
   802 				TInt iap=0;
       
   803 								
       
   804 				if(iCLArgument.FindF(_L("SNAP")) == 2)
       
   805 					{
       
   806 					TLex val(iCLArgument.Mid(6));
       
   807 					if(val.Val(snap) != KErrNone)
       
   808 						{
       
   809 						return KErrArgument;
       
   810 						}
       
   811 					}
       
   812 				else if(iCLArgument.FindF(_L("IAP")) == 2)
       
   813 					{
       
   814 					TLex val(iCLArgument.Mid(5));
       
   815 					if(val.Val(iap) != KErrNone)
       
   816 						{
       
   817 						return KErrArgument;
       
   818 						}
       
   819 					}
       
   820 				else if(val.Val(iap) != KErrNone) // plain number to be interpreted as IAP
       
   821 					{
       
   822 					return KErrArgument;
       
   823 					}
       
   824 				
       
   825 				// apply value to options structure, and tell the user we're overriding connection
       
   826 				if(snap)
       
   827 					{
       
   828 					aUi.Console().Printf(_L("Will start connection with SNAP %d\n"), snap);
       
   829 					iOptions.iConnSnap = snap;
       
   830 					}
       
   831 				else if (iap)
       
   832 					{
       
   833 					aUi.Console().Printf(_L("Will start connection with IAP %d\n"), iap);
       
   834 					iOptions.iConnIap = iap;
       
   835 					}
       
   836 				else
       
   837 					{
       
   838 					// 0 was specified.. what were you thinking?
       
   839 					User::Leave(-1005);
       
   840 					return KErrArgument;
       
   841 					}
       
   842 				}
       
   843 			else
       
   844 				{
       
   845 				iOptions.iDestname = iCLArgument;
       
   846 				}
       
   847 			}
       
   848 		else
       
   849 			{
       
   850 			iOptions.iDestname = iCLArgument;	
       
   851 			}
       
   852 		}
       
   853 
       
   854 	return KErrNone;
       
   855 	}
       
   856 
       
   857 
       
   858 
       
   859 TBool TPingTestParser::ParseCommandLine(CPingTestUi& aUi)
       
   860 	{
       
   861 	
       
   862 	TInt res = KErrNone;
       
   863 
       
   864 	do
       
   865 		{
       
   866 		TBuf<0x100> command;
       
   867 		aUi.Console().Printf(KPrompt);
       
   868 		
       
   869 		TKeyCode key, was=EKeyNull;
       
   870 		TInt histpos=-1;
       
   871 		while((key=aUi.Console().Getch())!=EKeyEnter)
       
   872 			{
       
   873 			if(command.Length()>=0x100)
       
   874 				{
       
   875 				User::Beep(440, 500000);
       
   876 				}
       
   877 			else if(key==EKeyBackspace || key==EKeyLeftArrow || key==EKeyDelete)
       
   878 				{
       
   879 				if(command.Length())
       
   880 					{
       
   881 					aUi.Console().Printf(_L("\b \b"));
       
   882 					command.SetLength(command.Length()-1);
       
   883 					}
       
   884 				}
       
   885 			else if(key == EKeyUpArrow)
       
   886 				{
       
   887 				if(was==EKeyDownArrow)
       
   888 					{
       
   889 					histpos--;
       
   890 					}
       
   891 				was=key;
       
   892 				aUi.DisplayHistory(histpos--, KPromptLength, command);
       
   893 				}
       
   894 			else if(key == EKeyDownArrow)
       
   895 				{
       
   896 				if(was==EKeyUpArrow)
       
   897 					{
       
   898 					++histpos;
       
   899 					}
       
   900 				was=key;
       
   901 				aUi.DisplayHistory(++histpos, KPromptLength, command);
       
   902 				}
       
   903 			else if(key>=EKeySpace && key<=EKeyDelete)
       
   904 				{
       
   905 				aUi.Console().Printf(_L("%c"), key);
       
   906 				command.Append(TChar(key));
       
   907 				}
       
   908 			}
       
   909 
       
   910 		aUi.Console().Printf(_L("\n"));
       
   911 		aUi.AddToHistory(command);
       
   912 
       
   913 		_LIT(KQuitCommand, "quit");
       
   914 		_LIT(KQCommand, "q");
       
   915 		_LIT(KExitCommand, "exit");
       
   916 
       
   917 		if (command == KQuitCommand || command == KQCommand || command == KExitCommand)
       
   918 			{
       
   919 			return KQuitReturn;
       
   920 			}
       
   921 
       
   922 		_LIT(KHelpCommand, "help");
       
   923 		if (command == KHelpCommand)
       
   924 			{
       
   925 			aUi.Console().Printf(_L("Usage: [options] destination\n\nwhere options are\n"));
       
   926 			aUi.Console().Printf(_L("    -a         resolve address to hostname\n"));
       
   927 			aUi.Console().Printf(_L("    -c         prompt for interface choice\n"));
       
   928 			aUi.Console().Printf(_L("    -cIAP3     start connection with IAP 3\n"));
       
   929 			aUi.Console().Printf(_L("    -cSNAP77   start connection with SNAP 77\n"));
       
   930 			aUi.Console().Printf(_L("    -h         print out this screen\n"));
       
   931 			aUi.Console().Printf(_L("    -n<number> number of pings\n"));
       
   932 			aUi.Console().Printf(_L("    -i<number> interval between pings\n"));
       
   933 			aUi.Console().Printf(_L("    -s<number> number of bytes in request\n"));
       
   934 			aUi.Console().Printf(_L("    -p<number> preload\n"));
       
   935 			aUi.Console().Printf(_L("    -w<number> time to wait for replies\n"));
       
   936 			aUi.Console().Printf(_L("    -b<number> maximum number of unanswered requests\n"));	
       
   937 			aUi.Console().Printf(_L("    quit, q or exit to finish\n\n"));
       
   938 			return KHelpReturn;
       
   939 			}
       
   940 
       
   941 		res = ParseCLArguments(command,aUi);
       
   942 		if (res != KErrNone)
       
   943 			{
       
   944 			aUi.Console().Printf(_L("Invalid argument %S - result %d\n"), &BadArgument(), res);
       
   945 			break;
       
   946 			}
       
   947 		} while (res!=KErrNone);
       
   948 
       
   949     	return ETrue;
       
   950 	}
       
   951 
       
   952 //
       
   953 // Create new circular buffer for command line history
       
   954 //
       
   955 CCircList::CCircList(TInt aLength)
       
   956 : iMaxLength(aLength)
       
   957 {}
       
   958 
       
   959 CCircList::~CCircList()
       
   960 //
       
   961 // Delete contents
       
   962 //
       
   963 	{
       
   964 	
       
   965 	for(TInt i = iBufPtrArray.Count()-1; i>=0; --i)
       
   966 		{
       
   967 		delete iBufPtrArray[i];
       
   968 		}
       
   969 	iBufPtrArray.Close();
       
   970 	}
       
   971 
       
   972 TInt CCircList::Add(const TDesC& aLine)
       
   973 //
       
   974 // Add a new line to the buffer
       
   975 //
       
   976 	{
       
   977 
       
   978 	if(!aLine.Length())
       
   979 		{
       
   980 		return KErrNotFound;
       
   981 		}
       
   982 
       
   983 	HBufC* buf=NULL;
       
   984 
       
   985 	//Check to see if string matches last in array, if so return and don't add it.
       
   986 	if(iBufPtrArray.Count())
       
   987 		{
       
   988 		if(!iBufPtrArray[iBufPtrArray.Count()-1]->Compare(aLine))
       
   989 			{
       
   990 			return KErrNone;
       
   991 			}
       
   992 		}
       
   993 
       
   994 	buf=aLine.Alloc();
       
   995 	if(buf==NULL)
       
   996 		{
       
   997 		return KErrNoMemory;		//Ensures we can't put a null pointer into the arrray.
       
   998 		}
       
   999 	
       
  1000 	if (iBufPtrArray.Count()>=iMaxLength)	//Check to see if array is now too large,if so remove first element.
       
  1001 		{
       
  1002 		iBufPtrArray.Remove(0);
       
  1003 		}
       
  1004 
       
  1005 	return iBufPtrArray.Append(buf);
       
  1006 	}
       
  1007 
       
  1008 TInt CCircList::Count() const
       
  1009 	{
       
  1010 	
       
  1011 	return iBufPtrArray.Count();
       
  1012 	}
       
  1013 
       
  1014 const HBufC& CCircList::operator[](TInt aIndex) const
       
  1015 //
       
  1016 // Return index relative to last element added
       
  1017 // doesn't matter if index is out of range because we're wrapping it
       
  1018 //
       
  1019 	{
       
  1020 	
       
  1021 	aIndex=aIndex%iBufPtrArray.Count();
       
  1022 	if(aIndex<0)
       
  1023 		{
       
  1024 		aIndex=iBufPtrArray.Count()+aIndex;
       
  1025 		}
       
  1026 
       
  1027 	return *iBufPtrArray[aIndex];	//Dont need to check index, since RArray will do bounds checking.
       
  1028 	}