00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 #ifndef __OPAL_SIPEP_H
00250 #define __OPAL_SIPEP_H
00251
00252 #ifdef P_USE_PRAGMA
00253 #pragma interface
00254 #endif
00255
00256
00257 #include <opal/endpoint.h>
00258 #include <sip/sippdu.h>
00259
00260
00261 class SIPConnection;
00262
00264
00265
00266
00267
00268
00269 class SIPInfo : public PSafeObject
00270 {
00271 PCLASSINFO(SIPInfo, PSafeObject);
00272 public:
00273 SIPInfo(
00274 SIPEndPoint & ep,
00275 const PString & name
00276 );
00277
00278 ~SIPInfo();
00279
00280 virtual BOOL CreateTransport(OpalTransportAddress & addr);
00281
00282 virtual OpalTransport *GetTransport()
00283 { PWaitAndSignal m(transportMutex); return registrarTransport; }
00284
00285 virtual SIPAuthentication & GetAuthentication()
00286 { return authentication; }
00287
00288 virtual const OpalTransportAddress & GetRegistrarAddress()
00289 { return registrarAddress; }
00290
00291 virtual const SIPURL & GetRegistrationAddress()
00292 { return registrationAddress; }
00293
00294 virtual void AppendTransaction(SIPTransaction * transaction)
00295 { registrations.Append (transaction); }
00296
00297 virtual void RemoveTransactions()
00298 { registrations.RemoveAll (); }
00299
00300 virtual BOOL IsRegistered()
00301 { return registered; }
00302
00303 virtual void SetRegistered(BOOL r)
00304 { registered = r; if (r) registrationTime = PTime ();}
00305
00306
00307
00308 virtual void SetExpire(int e)
00309 { expire = e; }
00310
00311 virtual int GetExpire()
00312 { return expire; }
00313
00314 virtual PString GetRegistrationID()
00315 { return registrationID; }
00316
00317 virtual BOOL HasExpired()
00318 { return (registered && (PTime () - registrationTime) >= PTimeInterval (0, expire)); }
00319
00320 virtual void SetAuthUser(const PString & u)
00321 { authUser = u;}
00322
00323 virtual void SetPassword(const PString & p)
00324 { password = p;}
00325
00326 virtual void SetAuthRealm(const PString & r)
00327 { authRealm = r;}
00328
00329 virtual void SetBody(const PString & b)
00330 { body = b;}
00331
00332 virtual SIPTransaction * CreateTransaction(
00333 OpalTransport & t,
00334 BOOL unregister
00335 ) = 0;
00336
00337 virtual SIP_PDU::Methods GetMethod() = 0;
00338
00339 virtual void OnSuccess() = 0;
00340
00341 virtual void OnFailed(
00342 SIP_PDU::StatusCodes
00343 ) = 0;
00344
00345 protected:
00346 SIPEndPoint & ep;
00347 SIPAuthentication authentication;
00348 OpalTransport * registrarTransport;
00349 OpalTransportAddress registrarAddress;
00350 SIPURL registrationAddress;
00351 PString registrationID;
00352 SIPTransactionList registrations;
00353 PTime registrationTime;
00354 BOOL registered;
00355 int expire;
00356 PString authRealm;
00357 PString authUser;
00358 PString password;
00359 PString body;
00360 PMutex transportMutex;
00361
00362 private:
00363 };
00364
00365 class SIPRegisterInfo : public SIPInfo
00366 {
00367 PCLASSINFO(SIPRegisterInfo, SIPInfo);
00368
00369 public:
00370 SIPRegisterInfo(SIPEndPoint & ep, const PString & adjustedUsername, const PString & authName, const PString & password, int expire);
00371 ~SIPRegisterInfo();
00372 virtual SIPTransaction * CreateTransaction(OpalTransport &, BOOL);
00373 virtual SIP_PDU::Methods GetMethod()
00374 { return SIP_PDU::Method_REGISTER; }
00375
00376 virtual void OnSuccess();
00377 virtual void OnFailed(SIP_PDU::StatusCodes r);
00378 };
00379
00380 class SIPMWISubscribeInfo : public SIPInfo
00381 {
00382 PCLASSINFO(SIPMWISubscribeInfo, SIPInfo);
00383 public:
00384 SIPMWISubscribeInfo (SIPEndPoint & ep, const PString & adjustedUsername, int expire);
00385 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00386 virtual SIP_PDU::Methods GetMethod ()
00387 { return SIP_PDU::Method_SUBSCRIBE; }
00388 virtual void OnSuccess ();
00389 virtual void OnFailed (SIP_PDU::StatusCodes);
00390 };
00391
00392 class SIPMessageInfo : public SIPInfo
00393 {
00394 PCLASSINFO(SIPMessageInfo, SIPInfo);
00395 public:
00396 SIPMessageInfo (SIPEndPoint & ep, const PString & adjustedUsername, const PString & body);
00397 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00398 virtual SIP_PDU::Methods GetMethod ()
00399 { return SIP_PDU::Method_MESSAGE; }
00400 virtual void OnSuccess ();
00401 virtual void OnFailed (SIP_PDU::StatusCodes);
00402 };
00403
00404
00406
00410 class SIPAuthInfo : public PObject
00411 {
00412 public:
00413 SIPAuthInfo()
00414 { }
00415
00416 SIPAuthInfo(const PString & u, const PString & p)
00417 { username = u; password = p; }
00418 PString username;
00419 PString password;
00420 };
00421
00423
00426 class SIPEndPoint : public OpalEndPoint
00427 {
00428 PCLASSINFO(SIPEndPoint, OpalEndPoint);
00429
00430 public:
00435 SIPEndPoint(
00436 OpalManager & manager
00437 );
00438
00441 ~SIPEndPoint();
00443
00449 virtual PStringArray GetDefaultListeners() const;
00450
00455 virtual BOOL NewIncomingConnection(
00456 OpalTransport * transport
00457 );
00458
00488 virtual BOOL MakeConnection(
00489 OpalCall & call,
00490 const PString & party,
00491 void * userData = NULL
00492 );
00493
00503 virtual OpalMediaFormatList GetMediaFormats() const;
00505
00511 virtual SIPConnection * CreateConnection(
00512 OpalCall & call,
00513 const PString & token,
00514 void * userData,
00515 const SIPURL & destination,
00516 OpalTransport * transport,
00517 SIP_PDU * invite
00518 );
00519
00522 virtual BOOL SetupTransfer(
00523 const PString & token,
00524 const PString & callIdentity,
00525 const PString & remoteParty,
00526 void * userData = NULL
00527 );
00528
00532 virtual BOOL ForwardConnection(
00533 SIPConnection & connection,
00534 const PString & forwardParty
00535 );
00536
00538
00541 OpalTransport * CreateTransport(
00542 const OpalTransportAddress & address
00543 );
00544
00545 virtual void HandlePDU(
00546 OpalTransport & transport
00547 );
00548
00551 virtual BOOL OnReceivedPDU(
00552 OpalTransport & transport,
00553 SIP_PDU * pdu
00554 );
00555
00558 virtual void OnReceivedResponse(
00559 SIPTransaction & transaction,
00560 SIP_PDU & response
00561 );
00562
00565 virtual BOOL OnReceivedINVITE(
00566 OpalTransport & transport,
00567 SIP_PDU * pdu
00568 );
00569
00572 virtual void OnReceivedAuthenticationRequired(
00573 SIPTransaction & transaction,
00574 SIP_PDU & response
00575 );
00576
00580 virtual void OnReceivedOK(
00581 SIPTransaction & transaction,
00582 SIP_PDU & response
00583 );
00584
00587 virtual BOOL OnReceivedNOTIFY(
00588 OpalTransport & transport,
00589 SIP_PDU & response
00590 );
00591
00594 virtual void OnReceivedMESSAGE(
00595 OpalTransport & transport,
00596 SIP_PDU & response
00597 );
00598
00606 virtual void OnRTPStatistics(
00607 const SIPConnection & connection,
00608 const RTP_Session & session
00609 ) const;
00611
00612
00617 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00618 const PString & token,
00619 PSafetyMode mode = PSafeReadWrite
00620 ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00621
00622 virtual BOOL IsAcceptedAddress(const SIPURL & toAddr);
00623
00624
00627 virtual void OnMessageReceived (const SIPURL & from,
00628 const PString & body);
00629
00630
00643 BOOL Register(
00644 const PString & host,
00645 const PString & username = PString::Empty(),
00646 const PString & autName = PString::Empty(),
00647 const PString & password = PString::Empty(),
00648 const PString & authRealm = PString::Empty(),
00649 int timeout = 0
00650 );
00651
00654 virtual void OnMWIReceived (
00655 const PString & host,
00656 const PString & user,
00657 SIPMWISubscribe::MWIType type,
00658 const PString & msgs);
00659
00660
00664 BOOL MWISubscribe(
00665 const PString & host,
00666 const PString & username,
00667 int timeout = 0
00668 );
00669
00670
00675 virtual void OnRegistrationFailed(
00676 const PString & host,
00677 const PString & userName,
00678 SIP_PDU::StatusCodes reason,
00679 BOOL wasRegistering);
00680
00681
00686 virtual void OnRegistered(
00687 const PString & host,
00688 const PString & userName,
00689 BOOL wasRegistering);
00690
00691
00695 BOOL IsRegistered(const PString & host);
00696
00697
00700 unsigned GetRegistrationsCount () { return activeSIPInfo.GetRegistrationsCount (); }
00701
00702
00705 BOOL IsSubscribed(
00706 const PString & host,
00707 const PString & user);
00708
00709
00713 BOOL Unregister(const PString & host,
00714 const PString & user);
00715
00716
00720 BOOL MWIUnsubscribe(
00721 const PString & host,
00722 const PString & user);
00723
00724
00729 virtual void OnMessageFailed(
00730 const SIPURL & messageUrl,
00731 SIP_PDU::StatusCodes reason);
00732
00733
00734 void SetMIMEForm(BOOL v) { mimeForm = v; }
00735 BOOL GetMIMEForm() const { return mimeForm; }
00736
00737 void SetMaxRetries(unsigned r) { maxRetries = r; }
00738 unsigned GetMaxRetries() const { return maxRetries; }
00739
00740 void SetRetryTimeouts(
00741 const PTimeInterval & t1,
00742 const PTimeInterval & t2
00743 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00744 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00745 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00746
00747 void SetNonInviteTimeout(
00748 const PTimeInterval & t
00749 ) { nonInviteTimeout = t; }
00750 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00751
00752 void SetPduCleanUpTimeout(
00753 const PTimeInterval & t
00754 ) { pduCleanUpTimeout = t; }
00755 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00756
00757 void SetInviteTimeout(
00758 const PTimeInterval & t
00759 ) { inviteTimeout = t; }
00760 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00761
00762 void SetAckTimeout(
00763 const PTimeInterval & t
00764 ) { ackTimeout = t; }
00765 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00766
00767 void SetRegistrarTimeToLive(
00768 const PTimeInterval & t
00769 ) { registrarTimeToLive = t; }
00770 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00771
00772 void SetNotifierTimeToLive(
00773 const PTimeInterval & t
00774 ) { notifierTimeToLive = t; }
00775 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00776
00777 void SetNATBindingTimeout(
00778 const PTimeInterval & t
00779 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00780 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00781
00782 void AddTransaction(
00783 SIPTransaction * transaction
00784 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), transaction); }
00785
00786 void RemoveTransaction(
00787 SIPTransaction * transaction
00788 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), NULL); }
00789
00790
00793 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00794
00795
00798 BOOL GetAuthentication(const PString & authRealm, SIPAuthentication &);
00799
00800
00806 virtual SIPURL GetRegisteredPartyName(const PString &);
00807
00808
00811 virtual SIPURL GetDefaultRegisteredPartyName();
00812
00813
00823 SIPURL GetLocalURL(
00824 const OpalTransport & transport,
00825 const PString & userName = PString::Empty()
00826 );
00827
00828
00831 const SIPURL & GetProxy() const { return proxy; }
00832
00833
00836 void SetProxy(const SIPURL & url);
00837
00838
00841 void SetProxy(
00842 const PString & hostname,
00843 const PString & username,
00844 const PString & password
00845 );
00846
00847
00850 virtual PString GetUserAgent() const;
00851
00852
00855 void SetUserAgent(const PString & str) { userAgentString = str; }
00856
00857
00860 BOOL SendMessage (const SIPURL & url, const PString & body);
00861
00862
00865 enum NATBindingRefreshMethod{
00866 None,
00867 Options,
00868 EmptyRequest,
00869 NumMethods
00870 };
00871
00872
00875 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00876
00877
00878 protected:
00879 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00880 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00881 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, RegistrationRefresh);
00882
00887 class RegistrationList : public PSafeList<SIPInfo>
00888 {
00889 public:
00890
00894 unsigned GetRegistrationsCount ()
00895 {
00896 unsigned count = 0;
00897 for (PSafePtr<SIPInfo> info(*this, PSafeReference); info != NULL; ++info)
00898 if (info->IsRegistered() && info->GetMethod() == SIP_PDU::Method_REGISTER)
00899 count++;
00900 return count;
00901 }
00902
00906 SIPInfo *FindSIPInfoByCallID (const PString & callID, PSafetyMode m)
00907 {
00908 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00909 if (callID == info->GetRegistrationID())
00910 return info;
00911 return NULL;
00912 }
00913
00917 SIPInfo *FindSIPInfoByAuthRealm (const PString & authRealm, const PString & userName, PSafetyMode m)
00918 {
00919 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00920 if (authRealm == info->GetAuthentication().GetAuthRealm() && (userName.IsEmpty() || userName == info->GetAuthentication().GetUsername()))
00921 return info;
00922 return NULL;
00923 }
00924
00932 SIPInfo *FindSIPInfoByUrl (const PString & url, SIP_PDU::Methods meth, PSafetyMode m)
00933 {
00934 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00935 if (SIPURL(url) == info->GetRegistrationAddress() && meth == info->GetMethod())
00936 return info;
00937 }
00938 return NULL;
00939 }
00940
00946 SIPInfo *FindSIPInfoByDomain (const PString & name, SIP_PDU::Methods meth, PSafetyMode m)
00947 {
00948 OpalTransportAddress addr = name;
00949 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00950 if (info->IsRegistered() && (name == info->GetRegistrationAddress().GetHostName() || (info->GetTransport() && addr.GetHostName() == info->GetTransport()->GetRemoteAddress().GetHostName())) && meth == info->GetMethod())
00951 return info;
00952 }
00953 return NULL;
00954 }
00955 };
00956
00957 static BOOL WriteSIPInfo(
00958 OpalTransport & transport,
00959 void * info
00960 );
00961
00962 BOOL TransmitSIPInfo (
00963 SIP_PDU::Methods method,
00964 const PString & host,
00965 const PString & username,
00966 const PString & authName = PString::Empty(),
00967 const PString & password = PString::Empty(),
00968 const PString & authRealm = PString::Empty(),
00969 const PString & body = PString::Empty(),
00970 int timeout = 0
00971 );
00972
00973 BOOL TransmitSIPUnregistrationInfo (
00974 const PString & host,
00975 const PString & username,
00976 SIP_PDU::Methods method
00977 );
00978
00979 void ParsePartyName(
00980 const PString & remoteParty,
00981 PString & party);
00982
00983 SIPURL proxy;
00984 PString userAgentString;
00985
00986 BOOL mimeForm;
00987 unsigned maxRetries;
00988 PTimeInterval retryTimeoutMin;
00989 PTimeInterval retryTimeoutMax;
00990 PTimeInterval nonInviteTimeout;
00991 PTimeInterval pduCleanUpTimeout;
00992 PTimeInterval inviteTimeout;
00993 PTimeInterval ackTimeout;
00994 PTimeInterval registrarTimeToLive;
00995 PTimeInterval notifierTimeToLive;
00996 PTimeInterval natBindingTimeout;
00997
00998 RegistrationList activeSIPInfo;
00999
01000 PTimer registrationTimer;
01001 SIPTransactionList messages;
01002 SIPTransactionDict transactions;
01003
01004 PTimer natBindingTimer;
01005 NATBindingRefreshMethod natMethod;
01006
01007 PMutex transactionsMutex;
01008 PMutex connectionsActiveInUse;
01009
01010 unsigned lastSentCSeq;
01011 };
01012
01013 #endif // __OPAL_SIPEP_H
01014
01015
01016