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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // This file contains the API definition for the class CImTextServerSession.
15 // This class creates sockets and is responsible for transmitting data between client and the server
39 #define __IMSK_SIMULATION
42 // Enable Scripting in Debug Builds only
44 #define __IMSK_SCRIPTING
47 const TInt EActivePriorityHigh = 1;
48 const TInt KCarriageLineFeedLength=2;
49 /** Max numeric IP address len = "0:0:0:0:0:0:194.217.242.23" = 45 chars */
50 const TInt KImskIPAddressLen=45; // Max numeric IP address len = "255.255.255.255" = 15 chars
52 /** Maximum buffer size of the received data
56 typedef TBuf8<KImMailMaxBufferSize> TImMailBuffer;
58 /** received buffer data line type
72 class CImTextServerScript;
73 class CImIAPPreferences;
76 class CImSocketIdleTimer;
79 Creates/opens socket and sends and receives data.
84 class CImTextServerSession : public CMsgActive
87 enum TImOperationMode{
95 Intended Usage : Static factory constructor. Uses two phase construction and
96 leaves nothing on the CleanupStack.
99 @return A pointer to the newly created CImTextServerSession object.
101 @post A fully constructed and initialised CImTextServerSession object.
104 IMPORT_C static CImTextServerSession *NewL();
110 IMPORT_C static CImTextServerSession* NewL(RSocketServ& aSocketServ, CImConnect& aConnect);
114 @fn NewL(TInt aSendIdleTime, TInt aReceiveIdleTime)
115 Intended Usage : Static factory constructor. Uses two phase construction and
116 leaves nothing on the CleanupStack.
118 @param aSendIdleTime is the time for which the idle timer runs (for a send)
119 @param aReceiveIdleTime is the time for which the idle timer runs (for a recieve)
121 @return A pointer to the newly created CImTextServerSession object.
123 @post A fully constructed and initialised CImTextServerSession object.
125 IMPORT_C static CImTextServerSession* NewL(TInt aSendIdleTime, TInt aReceiveIdleTime);
131 IMPORT_C static CImTextServerSession* NewL(TInt aSendIdleTime, TInt aReceiveIdleTime, RSocketServ& aSocketServ, CImConnect& aConnect);
135 @fn ~CImTextServerSession()
136 Intended Usage : Destructor.
139 ~CImTextServerSession();
142 @fn QueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TBool aEnableTimeout = ETrue)
143 Intended Usage : Queue a connect assuming the socket is successfully opened.
144 Error Condition : KErrNoMemory
146 @param aStatus Asynchronous completion status
147 @param anAddressDesc is the IP address
148 @param aIAPPreferences is the IAP connection preference to be used
149 @param aPortNum is the port number eg. 143, 25, 110.
150 @param aEnableTimeout Not used
152 @post connection is ready to send and receive data.
154 IMPORT_C void QueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TBool aEnableTimeout = ETrue);
156 IMPORT_C void QueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, const TDesC8& aSSLDomainName);
159 @fn SSLQueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TBool aEnableTimeout = ETrue)
160 Intended Usage : Queue a wrapped SSL connect on an socket assuming the socket is successfully opened.
161 Error Condition : KErrNoMemory, EImskSocketOpen
163 @param aStatus Asynchronous completion status
164 @param anAddressDesc is the IP address
165 @param aIAPPreferences is the IAP connection preference to be used
166 @param aPortNum is the port number eg. 993, 465, 995.
167 @param aEnableTimeout Not used
169 @post connection is ready to send and receive data.
171 IMPORT_C void SSLQueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TBool aEnableTimeout = ETrue);
173 IMPORT_C void SSLQueueConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, const TDesC8& aSSLDomainName);
177 Intended Usage : To disconnect and close socket connection.
178 Error Condition : None
181 @post connection is closed.
183 IMPORT_C void Disconnect();
184 IMPORT_C void Disconnect(TRequestStatus &aStatus);
188 IMPORT_C const TDesC& LocalName();
190 This method should be called before Send/SendQueueReceive()
192 @fn SetSSLTLSResponseL(const TDesC8& aDesc)
193 Intended Usage : To use for secure connection.
194 Error Condition : KErrNoMemory
196 @param aDesc is the positive response the MTM expects from the connected
197 server for TLS command. e.g. OK, +OK, 220 etc..
198 @pre aDesc cannot be zero length
199 @leave EImskNoTLSResponseString
200 @post iTLSResponse is filled with aDesc contents
203 IMPORT_C void SetSSLTLSResponseL(const TDesC8& aDesc);
206 @fn Send(TRequestStatus &aStatus, const TDesC8& aDesc)
207 Intended Usage : To send data.
208 Error Condition : None
210 @param aStatus Asynchronous completion status
211 @param aDesc is the aData to be sent
217 IMPORT_C void Send(TRequestStatus &aStatus, const TDesC8& aDesc);
218 IMPORT_C void SendWithTimeout(TRequestStatus& aStatus, TInt aIdleTime, const TDesC8& aDesc);
219 IMPORT_C void SendQueueReceiveWithTimeout(TRequestStatus& aStatus, TInt aIdleTime, const TDesC8& aDesc);
220 IMPORT_C void Send(TRequestStatus &aStatus, TRefByValue<const TDesC8> aFmt,...);
222 @fn SendQueueReceive(TRequestStatus &aStatus, const TDesC8& aDesc)
223 Intended Usage : To send data and queue a receive.
224 Error Condition : None
226 @param aStatus Asynchronous completion status
227 @param aDesc is the aData to be sent
228 @post Sends the data and queues a receive.
229 GetCurrentTextLine should be called after this method to read received data
231 IMPORT_C void SendQueueReceive(TRequestStatus &aStatus, const TDesC8& aDesc);
232 IMPORT_C void SendQueueReceive(TRequestStatus &aStatus, TRefByValue<const TDesC8> aFmt,...);
236 Returns first full line of data received from socket to user
237 @fn GetCurrentTextLine(TDes8& aDesc)
238 Intended Usage : To return received data.
239 Error Condition : None
241 @param aDesc will be filled with received buffer
242 @post returns the received buffer in the form of aDesc
243 @return EReceiveBufferEmpty when received data is empty
244 EBufferTooSmall when aDesc length is smaller than received data
245 ECRLFTerminated when received data is complete (full line)
246 EReceiveBufferFull when received data length is more than aDesc length
247 GetCurrentTextLine should be called after Send methods
249 IMPORT_C TImLineType GetCurrentTextLine(TDes8& aDesc);
251 User queues a new request from the socket (unless there's a full line of data in buffer
252 then signal user and there's no need to make a receive request)
253 @fn QueueReceiveNextTextLine(TRequestStatus &aStatus)
254 Intended Usage : User queues a new request from the socket.
255 Error Condition : None
257 @param aStatus for asynchronous request
260 IMPORT_C void QueueReceiveNextTextLine(TRequestStatus &aStatus);
262 @fn ReceiveBinaryData(TRequestStatus &aStatus, TDes8& aDes,TInt aLen)
263 Intended Usage : Users queue a request for received data
264 Error Condition : None
266 @param aStatus Asynchronous completion status
267 @param aDes is the aData to be received
268 @param aLen is the length of the data the user wants to read
269 @post aDes will be filled with received data of aLen.
270 ReceiveBinaryData should be called after Send.
272 IMPORT_C void ReceiveBinaryData(TRequestStatus &aStatus, TDes8& aDes,TInt aLen);
274 IMPORT_C void LogText(const TDesC8& aString); // write string into log file
275 IMPORT_C void LogError(const TDesC8& aString,const TInt aError); // write string and integer into log file
278 @fn GetIAPValue(TUint32 &aIAP)
279 Intended Usage : Returns the IAP we are connecting/connected with in aIAP or returns an error code
281 @param aIAP will be the current IAP
282 @post aIAP will be filled with current IAP connected.
283 @return will return genconn errors if any
286 IMPORT_C TInt GetIAPValue(TUint32 &aIAP);
288 @fn GetRConnectionName(TName &aName)
289 Intended Usage : Returns the name of the RConnection.
291 @param aName Buffer in which the name is returned.
292 @post aName will be filled with current name of the RConnection.
293 @return KErrNone if successful, or another of the system-wide error codes.
296 IMPORT_C TInt GetRConnectionName(TName &aName);
298 /** Returns the bearer type we are connected to with in aBearer or returns an error code */
299 IMPORT_C TInt GetIAPBearer(TUint32 &aBearer);
304 @fn GetLastSocketActivityTimeout(TUint32& aTimeout)
305 Intended Usage : Returns the last socket activity timeout value
307 @param aTimeout is a return argument containing the timeout if it was found
308 @post aTimeout will be filled with the timeout value
309 @return Returns KErrNone, KErrNotFound or KErrBadHandle
311 IMPORT_C TInt GetLastSocketActivityTimeout(TUint32& aTimeout);
315 Intended Usage : Returns the Socket Server
317 @return Socket Server
319 RSocketServ& GetSocketServ();
322 @fn GetConnectionStage()
323 Intended Usage : Gets the stage of the connection process obtained from RConnection
325 @return The current connection stage from RConnection or a system-wide error code.
328 IMPORT_C TInt GetConnectionStage();
332 Intended Usage : disconnects the socket and notifies the observer when the socket has been idle
339 @fn SetPrimaryTextServerSession(CImTextServerSession& aPrimaryTextServerSession)
340 Intended Usage : Users set only on the secondary session
342 @param aPrimaryTextServerSession is PrimarySession's TextServerSession
344 @post Going to be use on the secondary session
346 IMPORT_C void SetPrimaryTextServerSession(CImTextServerSession* aPrimaryTextServerSession);
350 Intended Usage : Returns the CImConnect object
353 @post Going to be use on the secondary session
355 CImConnect* GetCImConnect();
357 // Depreciated functions - do not use.
358 IMPORT_C void QueueConnect(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, TBool aEnableTimeout = ETrue);
359 IMPORT_C void QueueConnect(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TInt aType, TBool aEnableTimeout = ETrue);
360 IMPORT_C void QueueConnect(TRequestStatus& aStatus,const TDesC& anAddressDesc, TInt aPortNum, TCallBack aDisconnectFunction,const TUint32 aIntraddress,TInt aType, TBool aEnableTimeout = ETrue);
361 IMPORT_C void QueueConnect(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum,const TUint32 aIAPaddress, TInt aType, TBool aEnableTimeout = ETrue);
362 IMPORT_C void QueueConnect(TRequestStatus& aStatus,const TDesC& anAddressDesc, TInt aPortNum, TCallBack aDisconnectFunction, TBool aEnableTimeout = ETrue);
363 IMPORT_C void QueueConnect(TRequestStatus& aStatus,const TDesC& anAddressDesc, TInt aPortNum, TCallBack aDisconnectFunction, const CImIAPPreferences& aIAPPreferences,TInt aType, TBool aEnableTimeout = ETrue);
364 IMPORT_C TInt Send(const TDesC8& aDesc);
365 IMPORT_C TInt Send(TRefByValue<const TDesC8> aFmt,...);
366 IMPORT_C TInt SendReceive(const TDesC8& aDesc);
367 IMPORT_C TInt Receive(TDes8& rDes);
368 IMPORT_C void Receive(TRequestStatus &aStatus, TDes8& rDes);
369 IMPORT_C static CImTextServerSession *NewLC (TImOperationMode aMode, RSocketServ &aServerServ);
370 IMPORT_C static CImTextServerSession *NewL(RSocketServ &aServerServ);
371 //Do not call SetSecurity. Call SetSSLTLSResponseL
372 IMPORT_C TInt SetSecurity(TBool aSecurityOn, TBool aUnattendedMode = FALSE);
373 IMPORT_C void PerformLogging(TBool aLogging);
377 //cat Construction and Destruction
378 @fn CImTextServerSession()
379 Intended Usage : Constructor. First phase of two-phase construction method. Does
380 non-allocating construction.
384 CImTextServerSession();
386 CImTextServerSession(RSocketServ& aSocketServ, CImConnect& aConnect);
389 @fn CImTextServerSession(TInt aSendIdleTime, TInt aReceiveIdleTime)
390 Intended Usage : Constructor. First phase of two-phase construction method. Does
391 non-allocating construction.
393 @param aSendIdleTime is the time for which the idle timer runs (for a send)
394 @param aReceiveIdleTime is the time for which the idle timer runs (for a receive)
396 CImTextServerSession(TInt aSendIdleTime, TInt aReceiveIdleTime);
398 CImTextServerSession(TInt aSendIdleTime, TInt aReceiveIdleTime, RSocketServ& aSocketServ, CImConnect& aConnect);
401 //cat Construction and Destruction.
403 Intended Usage : Second phase of two-phase construction method. Does any
404 allocations required to fully construct the object.
407 @pre First phase of construction is complete
408 @post The object is fully constructed and initialised.
415 void CreateLogFile(TInt aPortNum);
416 void OpenScriptFile(TInt aPortNum);
419 void DoComplete(TInt& aStatusValue); // Cleanup code
422 // these called from DoRunL;
423 void SocketConnect();
424 void DoConnectedToSocketL();
425 void DoQueueConnect();
427 void RealReceive(TDes8& aDesc);
428 void RealSend(const TDesC8& aDesc);
430 @fn ParseSSLTLSResponseL()
431 Intended Usage : session will compare the response(for TLS command)
432 with the MTMs response
434 @leave KImskSSLTLSNegotiateFailed if the response doesn't match
435 @post CSecureSocket object is created
437 void ParseSSLTLSResponseL();
440 @fn CreateSecureSocketL()
441 Intended Usage : to use secure socket connection
444 @post CSecureSocket object is created and iSecurityState is set to EImSecurityStateOn;
446 void CreateSecureSocketL();
449 @fn QueueGenericConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, TBool aEnableTimeout = ETrue)
450 Intended Usage : Queue a standard or wrapped SSL connect on an socket assuming the socket is successfully opened. Called by SSLQueueConnectL and QueueConnectL
451 Error Condition : KErrNoMemory, EImskSocketOpen
453 @param aStatus Asynchronous completion status
454 @param anAddressDesc is the IP address
455 @param aIAPPreferences is the IAP connection preference to be used
456 @param aPortNum is the port number eg. 143, 993
457 @param aSSLDomainName SSL domain name to use for secure sockets
459 @post connection is ready to send and receive data.
461 void QueueGenericConnectL(TRequestStatus &aStatus,const TDesC& anAddressDesc, TInt aPortNum, const CImIAPPreferences& aIAPPreferences, const TDesC8& aSSLDomainName);
463 #if defined(__IMSK_SIMULATION)
464 void ReadNextPeriod();
465 TUint32 GetTokenValue(TInt aTokenLen, const TPtrC8& aBuffer);
467 TBool SuspendPeriodSet();
468 void ResetSuspendPeriod();
469 void SetAfterTimer();
470 TInt ReadConfigNum(const TDesC& aName);
479 EImDialUsingOverride,
480 EImTLSHandShakeStarted,
481 EImSendReceiveTimedOut
484 enum TImSendReceiveState
489 EImReceivingBinaryData,
490 EImSendingQueueReceive,
491 EImSuspended, //used when we are in a simulated GPRS suspend.
494 enum TImSecurityState
498 EImSecurityStateFailed
501 /** handle to the socket */
503 /** handle to RSocketServ */
505 /** handle to Hostresolver */
506 RHostResolver iHostResolver;
507 /** handle to secure socket for SSL/TLS connection */
508 CSecureSocket* iSecureSocket;
510 TUint32 iCurrentIAPcache;
511 /** Used to find if IAP is cached */
512 TBool iIAPCached; // true if the above value can be used.
513 /** result of the host resolver */
515 /** port number e.g. 25 for SMTP, 143 for IMAP and 110 for POP */
517 /** server address */
519 /** data returned by the socket */
520 TImMailBuffer iReceive;
522 TImSocketState iState;
523 TImSendReceiveState iSendReceive;
526 TImLineType iCurrentLineType;
528 /** to log connection data */
530 /** IP address name */
531 TBuf<KImskIPAddressLen> iLocalName;
532 /** secure state i.e to use Secure socket */
534 TImSecurityState iSecurityState;
535 const CImIAPPreferences* iIAPPreferences;
537 CImConnect* iConnect;
539 TDes8* iReceiveData; // buffer we just recieved data into
543 /** response for different mail protocols like 220,+OK, OK, BAD */
544 HBufC8* iTLSResponse; //response for different mail protocols like 220,+OK, OK, BAD
545 /** internal flag to read tls response */
546 TBool iReadTLSResponse;
547 TBool iPerformLogging;
548 TBool iWrappedSocket;
550 CImSocketIdleTimer* iSocketIdleTimer;
551 TTimeIntervalMicroSeconds32 iSendIdleTime;
552 TTimeIntervalMicroSeconds32 iReceiveIdleTime;
554 /** A shorter idle timeout, used when a fast response is expected */
555 TTimeIntervalMicroSeconds32 iSendShortIdleTime;
556 TTimeIntervalMicroSeconds32 iReceiveShortIdleTime;
559 #ifdef __IMSK_SCRIPTING
560 CImTextServerScript* iScript;
563 #if defined(__IMSK_SIMULATION)
568 TUint32 iScriptedIAP; // value to return for iap when scripting
569 TUint32 iBearerIAP; // value to return for bearer when scripting
572 TImSendReceiveState iSuspendedState;
574 RTimer iSuspendTimer;
578 TBool iGprsConfigExists;
582 TTime iLastSuspend; // time last suspension took place
585 TTimeIntervalSeconds iStart; // number of seconds since last delay
586 // before the following delay occurs.
587 // if 0 no suspend is required.
588 TTimeIntervalSeconds iDuration; // how long to delay. If 0 no suspend is
590 TBool iRepeat; // repeat the above delay for an
593 HBufC8* iSendData; // suspended data
594 #endif // __IMSK_SIMULATION
596 CImTextServerSession* iPrimaryTextServerSession; //Only going to be set on the secondary session
598 TBool iClientOwnsConnection;
600 /** SSL domain name for secure sockets */
601 HBufC8* iSSLDomainName;
602 TBool iSocketIdleTimeSet; // iSocketIdleTimeSet=ETrue, if smtp server did not responds within 10 seconds after sending "."