applayerprotocols/ftpengine/ftpprot/PASVANS.CPP
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 1998-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 // PASV command answer parser
       
    15 // Author:	Philippe Gabriel
       
    16 // RFC 959 defines the syntax for answer to the PASV command
       
    17 // We must parse an IP+Port number (decimal, big endian)
       
    18 // (ip1,ip2,ip3,ip4,ip5,port1,port2)
       
    19 // We parse these regular expressions using an FSM
       
    20 // Use:
       
    21 // TFtpPASVAnswerParser::Reset 
       
    22 // to Reset the parser before we begin to parse a new answer
       
    23 // TFtpPASVAnswerParser::Parse 
       
    24 // to parse an answer
       
    25 // returns:
       
    26 // TRUE: we parsed an answer (answer might be correct or inccorect)
       
    27 // FALSE: An answer has not been completely parsed yet
       
    28 // TFtpPASVAnswerParser::Fetch 
       
    29 // Fetch an answer
       
    30 // returns:
       
    31 // TRUE: We got a valid answer
       
    32 // FALSE: The answer we got had an invalid syntax
       
    33 // 
       
    34 //
       
    35 
       
    36 /**
       
    37  @file PASVANS.CPP
       
    38  @internalComponent
       
    39 */
       
    40 
       
    41 #include "DEBUG.H"
       
    42 #include "PASVANS.H"
       
    43 #include <e32base.h>
       
    44 #include <e32test.h>
       
    45 #include <in_sock.h>
       
    46 
       
    47 //
       
    48 // Definitions
       
    49 //
       
    50 
       
    51 TBool TFtpPASVAnswerParser::Fetch(TInetAddr& aFTPServerAddress)
       
    52 	{
       
    53 	__ASSERT_DEBUG((iState == ESuccess) || (iState == EFailed), User::Panic(_L("TFtpPASVAnswerParser::Fetch incorrect state"), 0));
       
    54 	if(iState != ESuccess)
       
    55 		return FALSE;
       
    56 	aFTPServerAddress = iFTPServerAddress;
       
    57 	return TRUE;
       
    58 	}
       
    59 
       
    60 void	TFtpPASVAnswerParser::Reset(void)
       
    61 	{
       
    62 	iState = EIdle;	
       
    63 	iFTPServerAddress.SetFamily(KAFUnspec);
       
    64 	}
       
    65 
       
    66 TBool	TFtpPASVAnswerParser::Parse(const TDesC8& aBuffer, const TInetAddr& aAddr)
       
    67 	{
       
    68 	if (aAddr.Family() == KAfInet)
       
    69 		return ParsePASV(aBuffer);
       
    70 	else
       
    71 		{
       
    72 		__ASSERT_DEBUG(aAddr.Family() == KAfInet6, User::Panic(_L("TFtpPASVAnswerParser"), 0));
       
    73 		iFTPServerAddress.SetAddress(aAddr.Ip6Address());
       
    74 		return ParseEPSV(aBuffer);
       
    75 		}
       
    76 	}
       
    77 
       
    78 TBool TFtpPASVAnswerParser::ParsePASV(const TDesC8& aBuffer)
       
    79 	{
       
    80 	const TUint8*	c;
       
    81 	TInt			bufLen;
       
    82 	TUint		aIPAndPort[6];
       
    83 	
       
    84 	if ((iState == ESuccess) || (iState == EFailed))
       
    85 		// Already parsed an answer
       
    86 		return TRUE;
       
    87 	for (c= aBuffer.Ptr(),bufLen = aBuffer.Length();(bufLen>0);bufLen--,c++)
       
    88 		{
       
    89 		switch (iState)
       
    90 			{
       
    91 			case EIdle:
       
    92 				if (*c=='(')
       
    93 					{
       
    94 					/* We fetched a (, begin parsing*/
       
    95 					iNumberCounter=0;
       
    96 					iState++;
       
    97 					}
       
    98 				break;
       
    99 			case EBeginParse:
       
   100 				if ((*c>='0') && (*c<='9'))
       
   101 					{
       
   102 					iDigit.Zero();
       
   103 					iDigit.Append(*c);
       
   104 					iState++;
       
   105 					}
       
   106 				break;
       
   107 			case EParsing:
       
   108 				if ((*c>='0') && (*c<='9'))
       
   109 					if(iDigit.Length() == 3)
       
   110 						{
       
   111 						// We're stuffed, we got more than 3 digits
       
   112 						iState = EFailed;
       
   113 						return TRUE;
       
   114 						}
       
   115 					else
       
   116 						{
       
   117 						iDigit.Append(*c);
       
   118 						}
       
   119 				else // We should have a , or a ) I accept everything
       
   120 					{
       
   121 					
       
   122 					//[1] Translate the last number that we parsed
       
   123 					TUint	tempValue;
       
   124 					TLex input(iDigit);
       
   125 					input.Val(tempValue,EDecimal);
       
   126 					if(tempValue>255)
       
   127 						{
       
   128 						// If Value out of bound, we're stuffed
       
   129 						// Error bail out
       
   130 						iState = EFailed;
       
   131 						return TRUE;
       
   132 						}
       
   133 					aIPAndPort[iNumberCounter++] = tempValue;
       
   134 					// [2] Are we waiting for more numbers?
       
   135 					if(iNumberCounter == 6)
       
   136 						{
       
   137 						// OKay We've finished
       
   138 						iFTPServerAddress.SetAddress(INET_ADDR(aIPAndPort[0],aIPAndPort[1],aIPAndPort[2],aIPAndPort[3]));
       
   139 						iFTPServerAddress.SetPort(aIPAndPort[4]<<8 | aIPAndPort[5]);
       
   140 						iState = ESuccess;
       
   141 						return TRUE;
       
   142 						}
       
   143 					else
       
   144 						// Fetch next number
       
   145 						iState = EBeginParse;
       
   146 					}
       
   147 				break;
       
   148 			default:
       
   149 				break;
       
   150 			}
       
   151 		}
       
   152 	return FALSE;
       
   153 	}
       
   154 
       
   155 TBool TFtpPASVAnswerParser::ParseEPSV(const TDesC8& aBuffer)
       
   156 	{
       
   157 	TInt			bufLen = aBuffer.Length();
       
   158 	TUint           port = 0;
       
   159 
       
   160 	if ((iState == ESuccess) || (iState == EFailed))
       
   161 		// Already parsed an answer
       
   162 		return ETrue;
       
   163 
       
   164 	for (const TUint8* c = aBuffer.Ptr();bufLen>0;bufLen--,c++)
       
   165 		{
       
   166 		switch (iState)
       
   167 			{
       
   168 			case EIdle:
       
   169 				if (*c=='(')
       
   170 					{
       
   171 					/* We fetched a (, begin parsing*/
       
   172 					iState = EBeginParse;
       
   173 					}
       
   174 				break;
       
   175 			case EBeginParse:
       
   176 				if (TChar(*c).IsDigit())
       
   177 					{
       
   178 					port = *c - '0';
       
   179 					iState = EParsing;
       
   180 					}
       
   181 				break;
       
   182 			case EParsing:
       
   183 				if (TChar(*c).IsDigit())
       
   184 					{
       
   185 					port = port*10 + (*c - '0');
       
   186 					}
       
   187 				else // We should have a "|", end the parsing
       
   188 					{
       
   189 					if(port>(1<<16) || *c != '|')
       
   190 						{
       
   191 						// If Value out of bound or  the last character is not a '|' 
       
   192 						// then the parsing failed
       
   193 						iState = EFailed;
       
   194 						return ETrue;
       
   195 						}
       
   196 					
       
   197 					iFTPServerAddress.SetPort(port);
       
   198 					iState = ESuccess;
       
   199 					return ETrue;
       
   200 					}
       
   201 				break;
       
   202 			default:
       
   203 				break;
       
   204 			}
       
   205 		}
       
   206 	return EFalse;
       
   207 	}