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 #ifndef __OPAL_SIPEP_H
00209 #define __OPAL_SIPEP_H
00210
00211 #ifdef P_USE_PRAGMA
00212 #pragma interface
00213 #endif
00214
00215
00216 #include <opal/endpoint.h>
00217 #include <sip/sippdu.h>
00218
00219
00220 class SIPConnection;
00221
00223
00224
00225
00226
00227
00228 class SIPInfo : public PSafeObject
00229 {
00230 PCLASSINFO(SIPInfo, PSafeObject);
00231 public:
00232 SIPInfo(
00233 SIPEndPoint & ep,
00234 const PString & name
00235 );
00236
00237 ~SIPInfo();
00238
00239 virtual BOOL CreateTransport(OpalTransportAddress & addr);
00240
00241 virtual void Cancel(SIPTransaction & transaction);
00242
00243 virtual OpalTransport *GetTransport()
00244 { PWaitAndSignal m(transportMutex); return registrarTransport; }
00245
00246 virtual SIPAuthentication & GetAuthentication()
00247 { return authentication; }
00248
00249 virtual const SIPURL & GetRegistrationAddress()
00250 { return registrationAddress; }
00251
00252 virtual void AppendTransaction(SIPTransaction * transaction)
00253 { registrations.Append (transaction); }
00254
00255 virtual void RemoveTransactions()
00256 { registrations.RemoveAll (); }
00257
00258 virtual BOOL IsRegistered()
00259 { return registered; }
00260
00261 virtual void SetRegistered(BOOL r)
00262 { registered = r; if (r) registrationTime = PTime ();}
00263
00264
00265
00266 virtual void SetExpire(int e)
00267 { expire = e; }
00268
00269 virtual int GetExpire()
00270 { return expire; }
00271
00272 virtual PString GetRegistrationID()
00273 { return registrationID; }
00274
00275 virtual BOOL HasExpired()
00276 { return (registered && (PTime () - registrationTime) >= PTimeInterval (0, expire)); }
00277
00278 virtual void SetAuthUser(const PString & u)
00279 { authUser = u;}
00280
00281 virtual void SetPassword(const PString & p)
00282 { password = p;}
00283
00284 virtual void SetAuthRealm(const PString & r)
00285 { authRealm = r;}
00286
00287 virtual void SetBody(const PString & b)
00288 { body = b;}
00289
00290 virtual SIPTransaction * CreateTransaction(
00291 OpalTransport & t,
00292 BOOL unregister
00293 ) = 0;
00294
00295 virtual SIP_PDU::Methods GetMethod() = 0;
00296
00297 virtual void OnSuccess() = 0;
00298
00299 virtual void OnFailed(
00300 SIP_PDU::StatusCodes
00301 ) = 0;
00302
00303 protected:
00304 SIPEndPoint & ep;
00305 SIPAuthentication authentication;
00306 OpalTransport * registrarTransport;
00307 SIPURL registrationAddress;
00308 PString registrationID;
00309 SIPTransactionList registrations;
00310 PTime registrationTime;
00311 BOOL registered;
00312 int expire;
00313 PString authRealm;
00314 PString authUser;
00315 PString password;
00316 PString body;
00317 PMutex transportMutex;
00318
00319 private:
00320 };
00321
00322 class SIPRegisterInfo : public SIPInfo
00323 {
00324 PCLASSINFO(SIPRegisterInfo, SIPInfo);
00325
00326 public:
00327 SIPRegisterInfo(SIPEndPoint & ep, const PString & adjustedUsername, const PString & authName, const PString & password, int expire);
00328 ~SIPRegisterInfo();
00329 virtual SIPTransaction * CreateTransaction(OpalTransport &, BOOL);
00330 virtual SIP_PDU::Methods GetMethod()
00331 { return SIP_PDU::Method_REGISTER; }
00332
00333 virtual void OnSuccess();
00334 virtual void OnFailed(SIP_PDU::StatusCodes r);
00335 };
00336
00337 class SIPMWISubscribeInfo : public SIPInfo
00338 {
00339 PCLASSINFO(SIPMWISubscribeInfo, SIPInfo);
00340 public:
00341 SIPMWISubscribeInfo (SIPEndPoint & ep, const PString & adjustedUsername, int expire);
00342 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00343 virtual SIP_PDU::Methods GetMethod ()
00344 { return SIP_PDU::Method_SUBSCRIBE; }
00345 virtual void OnSuccess ();
00346 virtual void OnFailed (SIP_PDU::StatusCodes);
00347 };
00348
00349 class SIPMessageInfo : public SIPInfo
00350 {
00351 PCLASSINFO(SIPMessageInfo, SIPInfo);
00352 public:
00353 SIPMessageInfo (SIPEndPoint & ep, const PString & adjustedUsername, const PString & body);
00354 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00355 virtual SIP_PDU::Methods GetMethod ()
00356 { return SIP_PDU::Method_MESSAGE; }
00357 virtual void OnSuccess ();
00358 virtual void OnFailed (SIP_PDU::StatusCodes);
00359 };
00360
00361
00363
00367 class SIPAuthInfo : public PObject
00368 {
00369 public:
00370 SIPAuthInfo()
00371 { }
00372
00373 SIPAuthInfo(const PString & u, const PString & p)
00374 { username = u; password = p; }
00375 PString username;
00376 PString password;
00377 };
00378
00380
00383 class SIPEndPoint : public OpalEndPoint
00384 {
00385 PCLASSINFO(SIPEndPoint, OpalEndPoint);
00386
00387 public:
00392 SIPEndPoint(
00393 OpalManager & manager
00394 );
00395
00398 ~SIPEndPoint();
00400
00406 virtual PStringArray GetDefaultListeners() const;
00407
00412 virtual BOOL NewIncomingConnection(
00413 OpalTransport * transport
00414 );
00415
00445 virtual BOOL MakeConnection(
00446 OpalCall & call,
00447 const PString & party,
00448 void * userData = NULL
00449 );
00450
00460 virtual OpalMediaFormatList GetMediaFormats() const;
00462
00468 virtual SIPConnection * CreateConnection(
00469 OpalCall & call,
00470 const PString & token,
00471 void * userData,
00472 const SIPURL & destination,
00473 OpalTransport * transport,
00474 SIP_PDU * invite
00475 );
00476
00479 virtual BOOL SetupTransfer(
00480 const PString & token,
00481 const PString & callIdentity,
00482 const PString & remoteParty,
00483 void * userData = NULL
00484 );
00485
00489 virtual BOOL ForwardConnection(
00490 SIPConnection & connection,
00491 const PString & forwardParty
00492 );
00493
00495
00498 OpalTransport * CreateTransport(
00499 const OpalTransportAddress & address
00500 );
00501
00502 virtual void HandlePDU(
00503 OpalTransport & transport
00504 );
00505
00508 virtual BOOL OnReceivedPDU(
00509 OpalTransport & transport,
00510 SIP_PDU * pdu
00511 );
00512
00515 virtual void OnReceivedResponse(
00516 SIPTransaction & transaction,
00517 SIP_PDU & response
00518 );
00519
00522 virtual BOOL OnReceivedINVITE(
00523 OpalTransport & transport,
00524 SIP_PDU * pdu
00525 );
00526
00529 virtual void OnReceivedAuthenticationRequired(
00530 SIPTransaction & transaction,
00531 SIP_PDU & response
00532 );
00533
00537 virtual void OnReceivedOK(
00538 SIPTransaction & transaction,
00539 SIP_PDU & response
00540 );
00541
00544 virtual BOOL OnReceivedNOTIFY(
00545 OpalTransport & transport,
00546 SIP_PDU & response
00547 );
00548
00551 virtual void OnReceivedMESSAGE(
00552 OpalTransport & transport,
00553 SIP_PDU & response
00554 );
00555
00563 virtual void OnRTPStatistics(
00564 const SIPConnection & connection,
00565 const RTP_Session & session
00566 ) const;
00568
00569
00574 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00575 const PString & token,
00576 PSafetyMode mode = PSafeReadWrite
00577 ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00578
00579 virtual BOOL IsAcceptedAddress(const SIPURL & toAddr);
00580
00581
00584 virtual void OnMessageReceived (const SIPURL & from,
00585 const PString & body);
00586
00587
00600 BOOL Register(
00601 const PString & host,
00602 const PString & username = PString::Empty(),
00603 const PString & autName = PString::Empty(),
00604 const PString & password = PString::Empty(),
00605 const PString & authRealm = PString::Empty(),
00606 int timeout = 0
00607 );
00608
00611 virtual void OnMWIReceived (
00612 const PString & host,
00613 const PString & user,
00614 SIPMWISubscribe::MWIType type,
00615 const PString & msgs);
00616
00617
00621 BOOL MWISubscribe(
00622 const PString & host,
00623 const PString & username,
00624 int timeout = 0
00625 );
00626
00627
00632 virtual void OnRegistrationFailed(
00633 const PString & host,
00634 const PString & userName,
00635 SIP_PDU::StatusCodes reason,
00636 BOOL wasRegistering);
00637
00638
00643 virtual void OnRegistered(
00644 const PString & host,
00645 const PString & userName,
00646 BOOL wasRegistering);
00647
00648
00652 BOOL IsRegistered(const PString & host);
00653
00654
00657 unsigned GetRegistrationsCount () { return activeSIPInfo.GetRegistrationsCount (); }
00658
00659
00662 BOOL IsSubscribed(
00663 const PString & host,
00664 const PString & user);
00665
00666
00670 BOOL Unregister(const PString & host,
00671 const PString & user);
00672
00673
00677 BOOL MWIUnsubscribe(
00678 const PString & host,
00679 const PString & user);
00680
00681
00686 virtual void OnMessageFailed(
00687 const SIPURL & messageUrl,
00688 SIP_PDU::StatusCodes reason);
00689
00690
00691 void SetMIMEForm(BOOL v) { mimeForm = v; }
00692 BOOL GetMIMEForm() const { return mimeForm; }
00693
00694 void SetMaxRetries(unsigned r) { maxRetries = r; }
00695 unsigned GetMaxRetries() const { return maxRetries; }
00696
00697 void SetRetryTimeouts(
00698 const PTimeInterval & t1,
00699 const PTimeInterval & t2
00700 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00701 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00702 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00703
00704 void SetNonInviteTimeout(
00705 const PTimeInterval & t
00706 ) { nonInviteTimeout = t; }
00707 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00708
00709 void SetPduCleanUpTimeout(
00710 const PTimeInterval & t
00711 ) { pduCleanUpTimeout = t; }
00712 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00713
00714 void SetInviteTimeout(
00715 const PTimeInterval & t
00716 ) { inviteTimeout = t; }
00717 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00718
00719 void SetAckTimeout(
00720 const PTimeInterval & t
00721 ) { ackTimeout = t; }
00722 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00723
00724 void SetRegistrarTimeToLive(
00725 const PTimeInterval & t
00726 ) { registrarTimeToLive = t; }
00727 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00728
00729 void SetNotifierTimeToLive(
00730 const PTimeInterval & t
00731 ) { notifierTimeToLive = t; }
00732 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00733
00734 void SetNATBindingTimeout(
00735 const PTimeInterval & t
00736 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00737 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00738
00739 void AddTransaction(
00740 SIPTransaction * transaction
00741 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), transaction); }
00742
00743 void RemoveTransaction(
00744 SIPTransaction * transaction
00745 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), NULL); }
00746
00747
00750 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00751
00752
00755 BOOL GetAuthentication(const PString & authRealm, SIPAuthentication &);
00756
00757
00763 const SIPURL GetRegisteredPartyName(const PString &);
00764
00765
00775 const SIPURL GetLocalURL(
00776 const OpalTransport & transport,
00777 const PString & userName = PString::Empty()
00778 );
00779
00780
00783 const SIPURL & GetProxy() const { return proxy; }
00784
00785
00788 void SetProxy(const SIPURL & url);
00789
00790
00793 void SetProxy(
00794 const PString & hostname,
00795 const PString & username,
00796 const PString & password
00797 );
00798
00799
00802 virtual PString GetUserAgent() const;
00803
00804
00807 void SetUserAgent(const PString & str) { userAgentString = str; }
00808
00809
00812 BOOL SendMessage (const SIPURL & url, const PString & body);
00813
00814
00817 enum NATBindingRefreshMethod{
00818 None,
00819 Options,
00820 EmptyRequest,
00821 NumMethods
00822 };
00823
00824
00827 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00828
00829
00830 protected:
00831 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00832 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00833 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, RegistrationRefresh);
00834
00839 class RegistrationList : public PSafeList<SIPInfo>
00840 {
00841 public:
00842
00846 unsigned GetRegistrationsCount ()
00847 {
00848 unsigned count = 0;
00849 for (PSafePtr<SIPInfo> info(*this, PSafeReference); info != NULL; ++info)
00850 if (info->IsRegistered() && info->GetMethod() == SIP_PDU::Method_REGISTER)
00851 count++;
00852 return count;
00853 }
00854
00858 SIPInfo *FindSIPInfoByCallID (const PString & callID, PSafetyMode m)
00859 {
00860 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00861 if (callID == info->GetRegistrationID())
00862 return info;
00863 return NULL;
00864 }
00865
00869 SIPInfo *FindSIPInfoByAuthRealm (const PString & authRealm, const PString & userName, PSafetyMode m)
00870 {
00871 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00872 if (authRealm == info->GetAuthentication().GetAuthRealm() && (userName.IsEmpty() || userName == info->GetAuthentication().GetUsername()))
00873 return info;
00874 return NULL;
00875 }
00876
00884 SIPInfo *FindSIPInfoByUrl (const PString & url, SIP_PDU::Methods meth, PSafetyMode m)
00885 {
00886 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00887 if (SIPURL(url) == info->GetRegistrationAddress() && meth == info->GetMethod())
00888 return info;
00889 }
00890 return NULL;
00891 }
00892
00898 SIPInfo *FindSIPInfoByDomain (const PString & name, SIP_PDU::Methods meth, PSafetyMode m)
00899 {
00900 OpalTransportAddress addr = name;
00901 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00902 if ((name == info->GetRegistrationAddress().GetHostName() || (info->GetTransport() && addr.GetHostName() == info->GetTransport()->GetRemoteAddress().GetHostName()) && meth == info->GetMethod()))
00903 return info;
00904 }
00905 return NULL;
00906 }
00907 };
00908
00909 static BOOL WriteSIPInfo(
00910 OpalTransport & transport,
00911 void * info
00912 );
00913
00914 BOOL TransmitSIPInfo (
00915 SIP_PDU::Methods method,
00916 const PString & host,
00917 const PString & username,
00918 const PString & authName = PString::Empty(),
00919 const PString & password = PString::Empty(),
00920 const PString & authRealm = PString::Empty(),
00921 const PString & body = PString::Empty(),
00922 int timeout = 0
00923 );
00924
00925 BOOL TransmitSIPUnregistrationInfo (
00926 const PString & host,
00927 const PString & username,
00928 SIP_PDU::Methods method
00929 );
00930
00931 void ParsePartyName(
00932 const PString & remoteParty,
00933 PString & party);
00934
00935 SIPURL proxy;
00936 PString userAgentString;
00937
00938 BOOL mimeForm;
00939 unsigned maxRetries;
00940 PTimeInterval retryTimeoutMin;
00941 PTimeInterval retryTimeoutMax;
00942 PTimeInterval nonInviteTimeout;
00943 PTimeInterval pduCleanUpTimeout;
00944 PTimeInterval inviteTimeout;
00945 PTimeInterval ackTimeout;
00946 PTimeInterval registrarTimeToLive;
00947 PTimeInterval notifierTimeToLive;
00948 PTimeInterval natBindingTimeout;
00949
00950 RegistrationList activeSIPInfo;
00951
00952 PTimer registrationTimer;
00953 SIPTransactionList messages;
00954 SIPTransactionDict transactions;
00955
00956 PMutex natTransportMutex;
00957 PTimer natBindingTimer;
00958 NATBindingRefreshMethod natMethod;
00959 PList<OpalTransport> natTransports;
00960
00961 PMutex transactionsMutex;
00962
00963 unsigned lastSentCSeq;
00964 };
00965
00966 #endif // __OPAL_SIPEP_H
00967
00968
00969