linklayerprotocols/pppnif/SPPP/PPPPAP.CPP
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 1997-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 // Protocol (PAP) - RFC 1334.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @brief Source file for the implementation of Password Authentication
       
    21  @internalComponent 
       
    22 */
       
    23 
       
    24 #include "ppppap.h"
       
    25 #include "PppProg.h"
       
    26 #include "PPPAUTH.H"
       
    27 #include "PPPConfig.h"
       
    28 
       
    29 
       
    30 //
       
    31 CPppPap::CPppPap()
       
    32 	{
       
    33 	}
       
    34 
       
    35 CPppPap::~CPppPap()
       
    36 	{
       
    37 	if (iPppLcp != 0)
       
    38 		TimerDelete();
       
    39 	}
       
    40 
       
    41 void CPppPap::InitL(CPppLcp* aLcp)
       
    42 	{
       
    43 	CPppAuthentication::InitL(aLcp);
       
    44 	TimerConstructL(KPppFsmTimerPriority);
       
    45 	Register();
       
    46 	}
       
    47 
       
    48 void CPppPap::AuthenticateComplete(TInt aStatus)
       
    49 	{
       
    50 	ASSERT(iPppLcp != 0);
       
    51 
       
    52 	if (aStatus==KErrNone)
       
    53 		{
       
    54 		//ignore Error, if fails it will time out 
       
    55 		//and try again anyway.
       
    56 		TRAP_IGNORE(SendAuthRequestL(ETrue));
       
    57 		}
       
    58 	else
       
    59 		DoFail(aStatus);
       
    60 	}
       
    61 
       
    62 void CPppPap::LowerLayerUp()
       
    63 	{
       
    64 	ASSERT(iPppLcp != 0);
       
    65 
       
    66 	if (iFlags & KPppApIsClient)
       
    67 		{
       
    68 		if (IsAuthenticateRequestDone())
       
    69 			{
       
    70 			//ignore Error, if fails it will time out 
       
    71 			//and try again anyway.
       
    72 			TRAP_IGNORE(SendAuthRequestL(ETrue));
       
    73 			}
       
    74 		else
       
    75 			AuthenticateRequest();
       
    76 		}
       
    77 	}
       
    78 
       
    79 void CPppPap::LowerLayerDown(TInt /*aStatus*/)
       
    80 	{
       
    81 	ASSERT(iPppLcp != 0);
       
    82 
       
    83 	TimerCancel();
       
    84 	}
       
    85 
       
    86 void CPppPap::TimerComplete(TInt /*aStatus*/)
       
    87 	{
       
    88 	ASSERT(iPppLcp != 0);
       
    89 
       
    90 	if (iTryCount>0)
       
    91 		{
       
    92 		//ignore Error, if fails it will time out 
       
    93 		//and try again anyway.
       
    94 		TRAP_IGNORE(SendAuthRequestL());
       
    95 		}
       
    96 	}
       
    97 
       
    98 void CPppPap::SendAuthRequestL(TBool aNewRequest)
       
    99 	{
       
   100     const CCredentialsConfig* credentials = iPppLcp->GetCredentials();
       
   101 #ifdef _UNICODE
       
   102 	const TDesC& username = credentials->GetUserName();
       
   103 	HBufC8& user = *HBufC8::NewLC(username.Length());
       
   104 	user.Des().Copy(username);
       
   105 
       
   106 	const TDesC& password = credentials->GetPassword();
       
   107 	HBufC8& pass = *HBufC8::NewLC(password.Length());
       
   108  	pass.Des().Copy(password);
       
   109 #else
       
   110 	TPtrC8 user(credentials->GetUserName()), pass(credentials->GetPassword());
       
   111 #endif
       
   112 
       
   113 	RMBufPacket pkt;
       
   114 	RMBufPktInfo* info = NULL;
       
   115 	TInt len = 4+user.Length()+1+pass.Length()+1;
       
   116 	TRAPD(err, pkt.AllocL(len));
       
   117 	if (err!=KErrNone)
       
   118 		return;
       
   119 	TRAP(err, info = pkt.NewInfoL());
       
   120 	if (err!=KErrNone)
       
   121 		{
       
   122 		pkt.Free();
       
   123 		return;
       
   124 		}
       
   125 	TUint8* ptr = pkt.First()->Ptr();
       
   126 	*ptr++ = KPppPapRequest;
       
   127 	if (aNewRequest)
       
   128 		{
       
   129 		iTryCount = KPppPapRetries;
       
   130 		if (++iCurrentId==0)
       
   131 			++iCurrentId;
       
   132 		}
       
   133 	*ptr++ = iCurrentId;
       
   134 	BigEndian::Put16(ptr, (TUint16)len);
       
   135 	ptr += 2;
       
   136 	*ptr++ = (TUint8)user.Length();	
       
   137 	Mem::Copy(ptr, (TUint8*)user.Ptr(), user.Length());
       
   138 	ptr += user.Length();
       
   139 	*ptr++ = (TUint8)pass.Length();	
       
   140 	Mem::Copy(ptr, (TUint8*)pass.Ptr(), pass.Length());
       
   141 	ptr += pass.Length();
       
   142 	info->iLength = len;	
       
   143 	TPppAddr::Cast((info->iDstAddr)).SetProtocol(iPppId);
       
   144 	pkt.Pack();
       
   145 	SendFrame(pkt);
       
   146 	TimerCancel();
       
   147 	TimerAfter(KPppPapWaitTime*1000);
       
   148 	--iTryCount;
       
   149 
       
   150 #ifdef _UNICODE
       
   151 	CleanupStack::PopAndDestroy(&pass);
       
   152 	CleanupStack::PopAndDestroy(&user);
       
   153 #endif
       
   154 	}
       
   155 
       
   156 
       
   157 TBool CPppPap::RecvFrame(RMBufChain& aPacket)
       
   158 	{
       
   159 	ASSERT(iPppLcp != 0);
       
   160 
       
   161 	RMBufPacket pkt;
       
   162 	pkt.Assign(aPacket);
       
   163 	pkt.Unpack();
       
   164 	RMBufPktInfo* info = pkt.Info();
       
   165 
       
   166 	TUint8 op;
       
   167 	TUint8 id;
       
   168 	TInt len;
       
   169 
       
   170 	if (IsInactive())
       
   171 		{
       
   172 		pkt.Free();
       
   173 		return EFalse;
       
   174 		}
       
   175 	
       
   176 	// Extract and drop LCP header
       
   177 	pkt.Align(4);
       
   178 	TUint8* ptr = pkt.First()->Ptr();
       
   179 	op = *ptr++;
       
   180 	id = *ptr++;
       
   181 	len = BigEndian::Get16(ptr);
       
   182 	
       
   183 	// Check packet length is OK
       
   184 	if (info->iLength<len)
       
   185 		{
       
   186 		// Too short!
       
   187 		pkt.Free();
       
   188 		return EFalse;
       
   189 		}
       
   190 	else if (info->iLength>len)
       
   191 		pkt.TrimEnd(len);
       
   192 	
       
   193 	pkt.TrimStart(4);
       
   194 
       
   195 	switch (op)
       
   196 		{
       
   197 	case KPppPapRequest:
       
   198 		{
       
   199 		TInt n;
       
   200 		TPtrC8 user;
       
   201 		TPtrC8 pass;
       
   202 		n = *ptr++;
       
   203 		user.Set(ptr, n);  ptr += n;
       
   204 		n = *ptr++;
       
   205 		pass.Set(ptr, n);  ptr += n;
       
   206 		}
       
   207 	case KPppPapAck:
       
   208 		if (id==iCurrentId)
       
   209 			{
       
   210 			TimerCancel();
       
   211 			DoSucceed();
       
   212 			if(iPppLcp->CallbackEnabled() && iPppLcp->CallbackRequestType() != ECallbackIETFRequestTypeMSCBCP)
       
   213 				{
       
   214 				iPppLcp->CallbackGrantedAndAuthenticated();
       
   215 				iPppLcp->TerminateLink(MNifIfNotify::ECallBack);	// all done, shut it all down
       
   216 				}
       
   217 			}
       
   218 		break;
       
   219 	case KPppPapNak:
       
   220 		if (id==iCurrentId)
       
   221 			{
       
   222 			TimerCancel();
       
   223 			DoFail(KErrIfAuthenticationFailure);
       
   224 			}
       
   225 		break;
       
   226 		}
       
   227 	pkt.Free();
       
   228 	return EFalse;
       
   229 	}