sipep.h

Go to the documentation of this file.
00001 /*
00002  * sipep.h
00003  *
00004  * Session Initiation Protocol endpoint.
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  * Formally known as the Open H323 project.
00008  *
00009  * Copyright (c) 2001 Equivalence Pty. Ltd.
00010  *
00011  * The contents of this file are subject to the Mozilla Public License
00012  * Version 1.0 (the "License"); you may not use this file except in
00013  * compliance with the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS"
00017  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00018  * the License for the specific language governing rights and limitations
00019  * under the License.
00020  *
00021  * The Original Code is Open Phone Abstraction Library.
00022  *
00023  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00024  *
00025  * Contributor(s): ______________________________________.
00026  *
00027  * $Revision: 20928 $
00028  * $Author: rjongbloed $
00029  * $Date: 2008-09-11 10:38:34 +0000 (Thu, 11 Sep 2008) $
00030  */
00031 
00032 #ifndef __OPAL_SIPEP_H
00033 #define __OPAL_SIPEP_H
00034 
00035 #ifdef P_USE_PRAGMA
00036 #pragma interface
00037 #endif
00038 
00039 #ifndef _PTLIB_H
00040 #include <ptlib.h>
00041 #endif
00042 
00043 #include <ptclib/sockagg.h>
00044 #include <opal/rtpep.h>
00045 #include <sip/sipcon.h>
00046 #include <sip/sippdu.h>
00047 #include <sip/handlers.h> 
00048 
00049 
00050 class SIPRegisterHandler;
00051 
00052 
00053 //
00054 //  provide flag so applications know if presence is available
00055 //
00056 #define OPAL_HAS_SIP_PRESENCE   1
00057 
00059 
00063 class SIPAuthInfo : public PObject
00064 {
00065   public:
00066     SIPAuthInfo()
00067     { }
00068 
00069     SIPAuthInfo(const PString & u, const PString & p)
00070     { username = u; password = p; }
00071     PString username;
00072     PString password;
00073 };
00074 
00076 
00079 class SIPEndPoint : public OpalRTPEndPoint
00080 {
00081   PCLASSINFO(SIPEndPoint, OpalRTPEndPoint);
00082 
00083   public:
00088     SIPEndPoint(
00089       OpalManager & manager
00090     );
00091 
00094     ~SIPEndPoint();
00096 
00103     virtual void ShutDown();
00104 
00108     virtual PString GetDefaultTransport() const;
00109 
00114     virtual PBoolean NewIncomingConnection(
00115       OpalTransport * transport  
00116     );
00117 
00147     virtual PBoolean MakeConnection(
00148       OpalCall & call,                         
00149       const PString & party,                   
00150       void * userData,                         
00151       unsigned int options,                    
00152       OpalConnection::StringOptions * stringOptions  
00153     );
00154 
00159     virtual PBoolean GarbageCollection();
00161 
00167     virtual SIPConnection * CreateConnection(
00168       OpalCall & call,                         
00169       const PString & token,                   
00170       void * userData,                         
00171       const SIPURL & destination,              
00172       OpalTransport * transport,               
00173       SIP_PDU * invite,                        
00174       unsigned int options = 0,                
00175       OpalConnection::StringOptions * stringOptions = NULL 
00176 
00177     );
00178     
00181     virtual PBoolean SetupTransfer(
00182       const PString & token,        
00183       const PString & callIdentity, 
00184       const PString & remoteParty,  
00185       void * userData = NULL        
00186     );
00187     
00191     virtual PBoolean ForwardConnection(
00192       SIPConnection & connection,     
00193       const PString & forwardParty    
00194     );
00195 
00197   
00200 
00204     OpalTransport * CreateTransport(
00205       const OpalTransportAddress & remoteAddress,
00206       const OpalTransportAddress & localAddress = OpalTransportAddress()
00207     );
00208 
00209     virtual void HandlePDU(
00210       OpalTransport & transport
00211     );
00212 
00215     virtual PBoolean OnReceivedPDU(
00216       OpalTransport & transport,
00217       SIP_PDU * pdu
00218     );
00219 
00222     virtual bool OnReceivedConnectionlessPDU(
00223       OpalTransport & transport, 
00224       SIP_PDU * pdu
00225     );
00226 
00229     virtual void OnReceivedResponse(
00230       SIPTransaction & transaction,
00231       SIP_PDU & response
00232     );
00233 
00236     virtual PBoolean OnReceivedINVITE(
00237       OpalTransport & transport,
00238       SIP_PDU * pdu
00239     );
00240 
00243     virtual void OnReceivedIntervalTooBrief(
00244       SIPTransaction & transaction, 
00245       SIP_PDU & response)
00246     ;
00247   
00250     virtual void OnReceivedAuthenticationRequired(
00251       SIPTransaction & transaction,
00252       SIP_PDU & response
00253     );
00254 
00258     virtual void OnReceivedOK(
00259       SIPTransaction & transaction,
00260       SIP_PDU & response
00261     );
00262     
00265     virtual PBoolean OnReceivedNOTIFY(
00266       OpalTransport & transport,
00267       SIP_PDU & response
00268     );
00269 
00272     virtual PBoolean OnReceivedREGISTER(
00273       OpalTransport & transport, 
00274       SIP_PDU & pdu
00275     );
00276 
00279     virtual PBoolean OnReceivedSUBSCRIBE(
00280       OpalTransport & transport, 
00281       SIP_PDU & pdu
00282     );
00283 
00286     virtual bool OnReceivedMESSAGE(
00287       OpalTransport & transport,
00288       SIP_PDU & response
00289     );
00290 
00293     virtual bool OnReceivedOPTIONS(
00294       OpalTransport & transport,
00295       SIP_PDU & response
00296     );
00297     
00300     virtual void OnTransactionFailed(
00301       SIPTransaction & transaction
00302     );
00303     
00311     virtual void OnRTPStatistics(
00312       const SIPConnection & connection,  
00313       const RTP_Session & session         
00314     ) const;
00316  
00317 
00322     PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00323       const PString & token,     
00324       PSafetyMode mode = PSafeReadWrite
00325     ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00326 
00327     virtual PBoolean IsAcceptedAddress(const SIPURL & toAddr);
00328 
00329 
00332     virtual void OnMessageReceived (const SIPURL & from,
00333                     const PString & body);
00334 
00335 
00349     bool Register(
00350       const SIPRegister::Params & params 
00351     );
00352 
00354     bool Register(
00355       const PString & host,
00356       const PString & user = PString::Empty(),
00357       const PString & autName = PString::Empty(),
00358       const PString & password = PString::Empty(),
00359       const PString & authRealm = PString::Empty(),
00360       unsigned expire = 0,
00361       const PTimeInterval & minRetryTime = PMaxTimeInterval, 
00362       const PTimeInterval & maxRetryTime = PMaxTimeInterval
00363     );
00364 
00369     bool Unregister(const PString & aor);
00370 
00373     bool UnregisterAll();
00374 
00378     PBoolean IsRegistered(const PString & aor);
00379 
00382     unsigned GetRegistrationsCount() const { return activeSIPHandlers.GetCount(SIP_PDU::Method_REGISTER); }
00383 
00385     struct RegistrationStatus {
00386       PString              m_addressofRecord;   
00387       bool                 m_wasRegistering;    
00388       bool                 m_reRegistering;     
00389       SIP_PDU::StatusCodes m_reason;            
00390       OpalProductInfo      m_productInfo;       
00391     };
00392 
00395     virtual void OnRegistrationStatus(
00396       const RegistrationStatus & status   
00397     );
00398 
00399     // For backward compatibility
00400     virtual void OnRegistrationStatus(
00401       const PString & aor,
00402       PBoolean wasRegistering,
00403       PBoolean reRegistering,
00404       SIP_PDU::StatusCodes reason
00405     );
00406 
00410     virtual void OnRegistrationFailed(
00411       const PString & aor,
00412       SIP_PDU::StatusCodes reason,
00413       PBoolean wasRegistering
00414     );
00415 
00419     virtual void OnRegistered(
00420       const PString & aor,
00421       PBoolean wasRegistering
00422     );
00423 
00424 
00428     bool Subscribe(
00429       SIPSubscribe::PredefinedPackages eventPackage,
00430       unsigned expire,
00431       const PString & to
00432     );
00433     bool Subscribe(
00434       const SIPSubscribe::Params & params 
00435     );
00436 
00437 
00438     bool Unsubscribe(
00439       SIPSubscribe::PredefinedPackages eventPackage,
00440       const PString & to
00441     );
00442     bool Unsubscribe(
00443       const PString & eventPackage,
00444       const PString & to
00445     );
00446 
00449     bool UnsubcribeAll(
00450       SIPSubscribe::PredefinedPackages eventPackage
00451     );
00452     bool UnsubcribeAll(
00453       const PString & eventPackage
00454     );
00455 
00459     PBoolean IsSubscribed(
00460       const PString & eventPackage,
00461       const PString & to
00462     );
00463 
00466     unsigned GetSubscriptionCount(const PString & eventPackage) { return activeSIPHandlers.GetCount(SIP_PDU::Method_SUBSCRIBE, eventPackage); }
00467 
00470     virtual void OnSubscriptionStatus(
00471       const PString & eventPackage, 
00472       const SIPURL & uri,           
00473       bool wasSubscribing,          
00474       bool reSubscribing,           
00475       SIP_PDU::StatusCodes reason   
00476     );
00477 
00478 
00481     PBoolean Message (
00482       const PString & to, 
00483       const PString & body
00484     );
00485     
00486 
00490     PBoolean Publish(
00491       const PString & to,
00492       const PString & body,
00493       unsigned expire = 0
00494     );
00495     
00496 
00499     PBoolean Ping(
00500       const PString & to
00501     );
00502 
00505     virtual void OnPresenceInfoReceived (
00506       const PString & user,
00507       const PString & basic,
00508       const PString & note
00509     );
00510 
00515     virtual void OnMessageFailed(
00516       const SIPURL & messageUrl,
00517       SIP_PDU::StatusCodes reason);
00518     
00519 
00520     void SetMIMEForm(PBoolean v) { mimeForm = v; }
00521     PBoolean GetMIMEForm() const { return mimeForm; }
00522 
00523     void SetMaxRetries(unsigned r) { maxRetries = r; }
00524     unsigned GetMaxRetries() const { return maxRetries; }
00525 
00526     void SetRetryTimeouts(
00527       const PTimeInterval & t1,
00528       const PTimeInterval & t2
00529     ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00530     const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00531     const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00532 
00533     void SetNonInviteTimeout(
00534       const PTimeInterval & t
00535     ) { nonInviteTimeout = t; }
00536     const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00537 
00538     void SetPduCleanUpTimeout(
00539       const PTimeInterval & t
00540     ) { pduCleanUpTimeout = t; }
00541     const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00542 
00543     void SetInviteTimeout(
00544       const PTimeInterval & t
00545     ) { inviteTimeout = t; }
00546     const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00547 
00548     void SetAckTimeout(
00549       const PTimeInterval & t
00550     ) { ackTimeout = t; }
00551     const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00552 
00553     void SetRegistrarTimeToLive(
00554       const PTimeInterval & t
00555     ) { registrarTimeToLive = t; }
00556     const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00557     
00558     void SetNotifierTimeToLive(
00559       const PTimeInterval & t
00560     ) { notifierTimeToLive = t; }
00561     const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00562     
00563     void SetNATBindingTimeout(
00564       const PTimeInterval & t
00565     ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00566     const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00567 
00568     void AddTransaction(
00569       SIPTransaction * transaction
00570     ) { transactions.SetAt(transaction->GetTransactionID(), transaction); }
00571 
00572     PSafePtr<SIPTransaction> GetTransaction(const PString & transactionID, PSafetyMode mode = PSafeReadWrite)
00573     { return transactions.FindWithLock(transactionID, mode); }
00574     
00577     unsigned GetNextCSeq() { return ++lastSentCSeq; }
00578 
00579     
00582     PBoolean GetAuthentication(const PString & authRealm, PString & realm, PString & user, PString & password); 
00583     
00589     virtual SIPURL GetRegisteredPartyName(const SIPURL &);
00590 
00591 
00594     virtual SIPURL GetDefaultRegisteredPartyName();
00595     
00596 
00608     SIPURL GetContactURL(const OpalTransport &transport, const PString & userName, const PString & host);
00609 
00610 
00620     virtual SIPURL GetLocalURL(
00621        const OpalTransport & transport,             
00622        const PString & userName = PString::Empty()  
00623     );
00624     
00625     
00628     const SIPURL & GetProxy() const { return proxy; }
00629 
00630     
00633     void SetProxy(const SIPURL & url);
00634     
00635     
00638     void SetProxy(
00639       const PString & hostname,
00640       const PString & username,
00641       const PString & password
00642     );
00643 
00644     
00651     virtual PString GetUserAgent() const;
00652         
00655     void SetUserAgent(const PString & str) { userAgentString = str; }
00656 
00657 
00660     virtual unsigned GetAllowedMethods() const;
00661 
00662     PBoolean SendResponse(
00663       SIP_PDU::StatusCodes code, 
00664       OpalTransport & transport, 
00665       SIP_PDU & pdu
00666     );
00667 
00670     enum NATBindingRefreshMethod{
00671       None,
00672       Options,
00673       EmptyRequest,
00674       NumMethods
00675     };
00676 
00677 
00680     void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00681 
00682     virtual SIPRegisterHandler * CreateRegisterHandler(const SIPRegister::Params & params);
00683 
00684   protected:
00685     PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00686     PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00687 
00688     SIPURL        proxy;
00689     PString       userAgentString;
00690 
00691     bool          mimeForm;
00692     unsigned      maxRetries;
00693     PTimeInterval retryTimeoutMin;   // T1
00694     PTimeInterval retryTimeoutMax;   // T2
00695     PTimeInterval nonInviteTimeout;  // T3
00696     PTimeInterval pduCleanUpTimeout; // T4
00697     PTimeInterval inviteTimeout;
00698     PTimeInterval ackTimeout;
00699     PTimeInterval registrarTimeToLive;
00700     PTimeInterval notifierTimeToLive;
00701     PTimeInterval natBindingTimeout;
00702     
00703     SIPHandlersList   activeSIPHandlers;
00704 
00705     PSafeDictionary<PString, SIPTransaction> transactions;
00706 
00707     PTimer                  natBindingTimer;
00708     NATBindingRefreshMethod natMethod;
00709     
00710     PAtomicInteger          lastSentCSeq;    
00711 
00712     struct SIP_PDU_Work
00713     {
00714       SIP_PDU_Work()
00715       { ep = NULL; pdu = NULL; }
00716 
00717       SIPEndPoint * ep;
00718       SIP_PDU * pdu;
00719       PString callID;
00720     };
00721 
00722     typedef std::queue<SIP_PDU_Work *> SIP_PDUWorkQueue;
00723 
00724     class SIP_PDU_Thread : public PThreadPoolWorkerBase
00725     {
00726       public:
00727         SIP_PDU_Thread(PThreadPoolBase & _pool);
00728         unsigned GetWorkSize() const;
00729         void OnAddWork(SIP_PDU_Work * work);
00730         void OnRemoveWork(SIP_PDU_Work *);
00731         void Shutdown();
00732         void Main();
00733 
00734       protected:
00735         PMutex mutex;
00736         PSyncPoint sync;
00737         SIP_PDUWorkQueue pduQueue;
00738     };
00739 
00740     typedef PThreadPool<SIP_PDU_Work, SIP_PDU_Thread> SIPMainThreadPool;
00741     SIPMainThreadPool threadPool;
00742 };
00743 
00744 #endif // __OPAL_SIPEP_H
00745 
00746 
00747 // End of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 11:49:15 2008 for OPAL by  doxygen 1.5.1