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
00250
00251
00252 #ifndef __OPAL_SIPEP_H
00253 #define __OPAL_SIPEP_H
00254
00255 #ifdef P_USE_PRAGMA
00256 #pragma interface
00257 #endif
00258
00259
00260 #include <opal/endpoint.h>
00261 #include <sip/sippdu.h>
00262
00263
00264 class SIPConnection;
00265
00267
00268
00269
00270
00271
00272 class SIPInfo : public PSafeObject
00273 {
00274 PCLASSINFO(SIPInfo, PSafeObject);
00275 public:
00276 SIPInfo(
00277 SIPEndPoint & ep,
00278 const PString & name
00279 );
00280
00281 ~SIPInfo();
00282
00283 virtual BOOL CreateTransport(OpalTransportAddress & addr);
00284
00285 virtual OpalTransport *GetTransport()
00286 { PWaitAndSignal m(transportMutex); return registrarTransport; }
00287
00288 virtual SIPAuthentication & GetAuthentication()
00289 { return authentication; }
00290
00291 virtual const OpalTransportAddress & GetRegistrarAddress()
00292 { return registrarAddress; }
00293
00294 virtual const SIPURL & GetRegistrationAddress()
00295 { return registrationAddress; }
00296
00297 virtual void AppendTransaction(SIPTransaction * transaction)
00298 { registrations.Append (transaction); }
00299
00300 virtual void RemoveTransactions()
00301 { registrations.RemoveAll (); }
00302
00303 virtual BOOL IsRegistered()
00304 { return registered; }
00305
00306 virtual void SetRegistered(BOOL r)
00307 { registered = r; if (r) registrationTime = PTime ();}
00308
00309
00310
00311 virtual void SetExpire(int e)
00312 { expire = e; }
00313
00314 virtual int GetExpire()
00315 { return expire; }
00316
00317 virtual PString GetRegistrationID()
00318 { return registrationID; }
00319
00320 virtual BOOL HasExpired()
00321 { return (registered && (PTime () - registrationTime) >= PTimeInterval (0, expire)); }
00322
00323 virtual void SetAuthUser(const PString & u)
00324 { authUser = u;}
00325
00326 virtual void SetPassword(const PString & p)
00327 { password = p;}
00328
00329 virtual void SetAuthRealm(const PString & r)
00330 { authRealm = r;}
00331
00332 virtual void SetBody(const PString & b)
00333 { body = b;}
00334
00335 virtual SIPTransaction * CreateTransaction(
00336 OpalTransport & t,
00337 BOOL unregister
00338 ) = 0;
00339
00340 virtual SIP_PDU::Methods GetMethod() = 0;
00341
00342 virtual void OnSuccess() = 0;
00343
00344 virtual void OnFailed(
00345 SIP_PDU::StatusCodes
00346 ) = 0;
00347
00348 int GetAuthenticationAttempts() { return authenticationAttempts; };
00349 void SetAuthenticationAttempts(unsigned attempts) { authenticationAttempts = attempts; };
00350
00351 protected:
00352 SIPEndPoint & ep;
00353 SIPAuthentication authentication;
00354 OpalTransport * registrarTransport;
00355 OpalTransportAddress registrarAddress;
00356 SIPURL registrationAddress;
00357 PString registrationID;
00358 SIPTransactionList registrations;
00359 PTime registrationTime;
00360 BOOL registered;
00361 int expire;
00362 PString authRealm;
00363 PString authUser;
00364 PString password;
00365 PString body;
00366 PMutex transportMutex;
00367 unsigned authenticationAttempts;
00368
00369 private:
00370 };
00371
00372 class SIPRegisterInfo : public SIPInfo
00373 {
00374 PCLASSINFO(SIPRegisterInfo, SIPInfo);
00375
00376 public:
00377 SIPRegisterInfo(SIPEndPoint & ep, const PString & adjustedUsername, const PString & authName, const PString & password, int expire);
00378 ~SIPRegisterInfo();
00379 virtual SIPTransaction * CreateTransaction(OpalTransport &, BOOL);
00380 virtual SIP_PDU::Methods GetMethod()
00381 { return SIP_PDU::Method_REGISTER; }
00382
00383 virtual void OnSuccess();
00384 virtual void OnFailed(SIP_PDU::StatusCodes r);
00385 };
00386
00387 class SIPMWISubscribeInfo : public SIPInfo
00388 {
00389 PCLASSINFO(SIPMWISubscribeInfo, SIPInfo);
00390 public:
00391 SIPMWISubscribeInfo (SIPEndPoint & ep, const PString & adjustedUsername, int expire);
00392 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00393 virtual SIP_PDU::Methods GetMethod ()
00394 { return SIP_PDU::Method_SUBSCRIBE; }
00395 virtual void OnSuccess ();
00396 virtual void OnFailed (SIP_PDU::StatusCodes);
00397 };
00398
00399 class SIPMessageInfo : public SIPInfo
00400 {
00401 PCLASSINFO(SIPMessageInfo, SIPInfo);
00402 public:
00403 SIPMessageInfo (SIPEndPoint & ep, const PString & adjustedUsername, const PString & body);
00404 virtual SIPTransaction * CreateTransaction (OpalTransport &, BOOL);
00405 virtual SIP_PDU::Methods GetMethod ()
00406 { return SIP_PDU::Method_MESSAGE; }
00407 virtual void OnSuccess ();
00408 virtual void OnFailed (SIP_PDU::StatusCodes);
00409 };
00410
00411
00413
00417 class SIPAuthInfo : public PObject
00418 {
00419 public:
00420 SIPAuthInfo()
00421 { }
00422
00423 SIPAuthInfo(const PString & u, const PString & p)
00424 { username = u; password = p; }
00425 PString username;
00426 PString password;
00427 };
00428
00430
00433 class SIPEndPoint : public OpalEndPoint
00434 {
00435 PCLASSINFO(SIPEndPoint, OpalEndPoint);
00436
00437 public:
00442 SIPEndPoint(
00443 OpalManager & manager
00444 );
00445
00448 ~SIPEndPoint();
00450
00456 virtual PStringArray GetDefaultListeners() const;
00457
00462 virtual BOOL NewIncomingConnection(
00463 OpalTransport * transport
00464 );
00465
00495 virtual BOOL MakeConnection(
00496 OpalCall & call,
00497 const PString & party,
00498 void * userData = NULL
00499 );
00500
00510 virtual OpalMediaFormatList GetMediaFormats() const;
00512
00518 virtual SIPConnection * CreateConnection(
00519 OpalCall & call,
00520 const PString & token,
00521 void * userData,
00522 const SIPURL & destination,
00523 OpalTransport * transport,
00524 SIP_PDU * invite
00525 );
00526
00529 virtual BOOL SetupTransfer(
00530 const PString & token,
00531 const PString & callIdentity,
00532 const PString & remoteParty,
00533 void * userData = NULL
00534 );
00535
00539 virtual BOOL ForwardConnection(
00540 SIPConnection & connection,
00541 const PString & forwardParty
00542 );
00543
00545
00548 OpalTransport * CreateTransport(
00549 const OpalTransportAddress & address
00550 );
00551
00552 virtual void HandlePDU(
00553 OpalTransport & transport
00554 );
00555
00558 virtual BOOL OnReceivedPDU(
00559 OpalTransport & transport,
00560 SIP_PDU * pdu
00561 );
00562
00565 virtual void OnReceivedResponse(
00566 SIPTransaction & transaction,
00567 SIP_PDU & response
00568 );
00569
00572 virtual BOOL OnReceivedINVITE(
00573 OpalTransport & transport,
00574 SIP_PDU * pdu
00575 );
00576
00579 virtual void OnReceivedAuthenticationRequired(
00580 SIPTransaction & transaction,
00581 SIP_PDU & response
00582 );
00583
00587 virtual void OnReceivedOK(
00588 SIPTransaction & transaction,
00589 SIP_PDU & response
00590 );
00591
00594 virtual BOOL OnReceivedNOTIFY(
00595 OpalTransport & transport,
00596 SIP_PDU & response
00597 );
00598
00601 virtual void OnReceivedMESSAGE(
00602 OpalTransport & transport,
00603 SIP_PDU & response
00604 );
00605
00613 virtual void OnRTPStatistics(
00614 const SIPConnection & connection,
00615 const RTP_Session & session
00616 ) const;
00618
00619
00624 PSafePtr<SIPConnection> GetSIPConnectionWithLock(
00625 const PString & token,
00626 PSafetyMode mode = PSafeReadWrite
00627 ) { return PSafePtrCast<OpalConnection, SIPConnection>(GetConnectionWithLock(token, mode)); }
00628
00629 virtual BOOL IsAcceptedAddress(const SIPURL & toAddr);
00630
00631
00634 virtual void OnMessageReceived (const SIPURL & from,
00635 const PString & body);
00636
00637
00650 BOOL Register(
00651 const PString & host,
00652 const PString & username = PString::Empty(),
00653 const PString & autName = PString::Empty(),
00654 const PString & password = PString::Empty(),
00655 const PString & authRealm = PString::Empty(),
00656 int timeout = 0
00657 );
00658
00661 virtual void OnMWIReceived (
00662 const PString & host,
00663 const PString & user,
00664 SIPMWISubscribe::MWIType type,
00665 const PString & msgs);
00666
00667
00671 BOOL MWISubscribe(
00672 const PString & host,
00673 const PString & username,
00674 int timeout = 0
00675 );
00676
00677
00682 virtual void OnRegistrationFailed(
00683 const PString & host,
00684 const PString & userName,
00685 SIP_PDU::StatusCodes reason,
00686 BOOL wasRegistering);
00687
00688
00693 virtual void OnRegistered(
00694 const PString & host,
00695 const PString & userName,
00696 BOOL wasRegistering);
00697
00698
00702 BOOL IsRegistered(const PString & host);
00703
00704
00707 unsigned GetRegistrationsCount () { return activeSIPInfo.GetRegistrationsCount (); }
00708
00709
00712 BOOL IsSubscribed(
00713 const PString & host,
00714 const PString & user);
00715
00716
00720 BOOL Unregister(const PString & host,
00721 const PString & user);
00722
00723
00727 BOOL MWIUnsubscribe(
00728 const PString & host,
00729 const PString & user);
00730
00731
00736 virtual void OnMessageFailed(
00737 const SIPURL & messageUrl,
00738 SIP_PDU::StatusCodes reason);
00739
00740
00741 void SetMIMEForm(BOOL v) { mimeForm = v; }
00742 BOOL GetMIMEForm() const { return mimeForm; }
00743
00744 void SetMaxRetries(unsigned r) { maxRetries = r; }
00745 unsigned GetMaxRetries() const { return maxRetries; }
00746
00747 void SetRetryTimeouts(
00748 const PTimeInterval & t1,
00749 const PTimeInterval & t2
00750 ) { retryTimeoutMin = t1; retryTimeoutMax = t2; }
00751 const PTimeInterval & GetRetryTimeoutMin() const { return retryTimeoutMin; }
00752 const PTimeInterval & GetRetryTimeoutMax() const { return retryTimeoutMax; }
00753
00754 void SetNonInviteTimeout(
00755 const PTimeInterval & t
00756 ) { nonInviteTimeout = t; }
00757 const PTimeInterval & GetNonInviteTimeout() const { return nonInviteTimeout; }
00758
00759 void SetPduCleanUpTimeout(
00760 const PTimeInterval & t
00761 ) { pduCleanUpTimeout = t; }
00762 const PTimeInterval & GetPduCleanUpTimeout() const { return pduCleanUpTimeout; }
00763
00764 void SetInviteTimeout(
00765 const PTimeInterval & t
00766 ) { inviteTimeout = t; }
00767 const PTimeInterval & GetInviteTimeout() const { return inviteTimeout; }
00768
00769 void SetAckTimeout(
00770 const PTimeInterval & t
00771 ) { ackTimeout = t; }
00772 const PTimeInterval & GetAckTimeout() const { return ackTimeout; }
00773
00774 void SetRegistrarTimeToLive(
00775 const PTimeInterval & t
00776 ) { registrarTimeToLive = t; }
00777 const PTimeInterval & GetRegistrarTimeToLive() const { return registrarTimeToLive; }
00778
00779 void SetNotifierTimeToLive(
00780 const PTimeInterval & t
00781 ) { notifierTimeToLive = t; }
00782 const PTimeInterval & GetNotifierTimeToLive() const { return notifierTimeToLive; }
00783
00784 void SetNATBindingTimeout(
00785 const PTimeInterval & t
00786 ) { natBindingTimeout = t; natBindingTimer.RunContinuous (natBindingTimeout); }
00787 const PTimeInterval & GetNATBindingTimeout() const { return natBindingTimeout; }
00788
00789 void AddTransaction(
00790 SIPTransaction * transaction
00791 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), transaction); }
00792
00793 void RemoveTransaction(
00794 SIPTransaction * transaction
00795 ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), NULL); }
00796
00797
00800 unsigned GetNextCSeq() { return ++lastSentCSeq; }
00801
00802
00805 BOOL GetAuthentication(const PString & authRealm, SIPAuthentication &);
00806
00807
00813 virtual SIPURL GetRegisteredPartyName(const PString &);
00814
00815
00818 virtual SIPURL GetDefaultRegisteredPartyName();
00819
00820
00830 SIPURL GetLocalURL(
00831 const OpalTransport & transport,
00832 const PString & userName = PString::Empty()
00833 );
00834
00835
00838 const SIPURL & GetProxy() const { return proxy; }
00839
00840
00843 void SetProxy(const SIPURL & url);
00844
00845
00848 void SetProxy(
00849 const PString & hostname,
00850 const PString & username,
00851 const PString & password
00852 );
00853
00854
00857 virtual PString GetUserAgent() const;
00858
00859
00862 void SetUserAgent(const PString & str) { userAgentString = str; }
00863
00864
00867 BOOL SendMessage (const SIPURL & url, const PString & body);
00868
00869
00872 enum NATBindingRefreshMethod{
00873 None,
00874 Options,
00875 EmptyRequest,
00876 NumMethods
00877 };
00878
00879
00882 void SetNATBindingRefreshMethod(const NATBindingRefreshMethod m) { natMethod = m; }
00883
00884
00885 protected:
00886 PDECLARE_NOTIFIER(PThread, SIPEndPoint, TransportThreadMain);
00887 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, NATBindingRefresh);
00888 PDECLARE_NOTIFIER(PTimer, SIPEndPoint, RegistrationRefresh);
00889
00894 class RegistrationList : public PSafeList<SIPInfo>
00895 {
00896 public:
00897
00901 unsigned GetRegistrationsCount ()
00902 {
00903 unsigned count = 0;
00904 for (PSafePtr<SIPInfo> info(*this, PSafeReference); info != NULL; ++info)
00905 if (info->IsRegistered() && info->GetMethod() == SIP_PDU::Method_REGISTER)
00906 count++;
00907 return count;
00908 }
00909
00913 SIPInfo *FindSIPInfoByCallID (const PString & callID, PSafetyMode m)
00914 {
00915 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00916 if (callID == info->GetRegistrationID())
00917 return info;
00918 return NULL;
00919 }
00920
00924 SIPInfo *FindSIPInfoByAuthRealm (const PString & authRealm, const PString & userName, PSafetyMode m)
00925 {
00926 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info)
00927 if (authRealm == info->GetAuthentication().GetAuthRealm() && (userName.IsEmpty() || userName == info->GetAuthentication().GetUsername()))
00928 return info;
00929 return NULL;
00930 }
00931
00939 SIPInfo *FindSIPInfoByUrl (const PString & url, SIP_PDU::Methods meth, PSafetyMode m)
00940 {
00941 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00942 if (SIPURL(url) == info->GetRegistrationAddress() && meth == info->GetMethod())
00943 return info;
00944 }
00945 return NULL;
00946 }
00947
00953 SIPInfo *FindSIPInfoByDomain (const PString & name, SIP_PDU::Methods meth, PSafetyMode m)
00954 {
00955 OpalTransportAddress addr = name;
00956 for (PSafePtr<SIPInfo> info(*this, m); info != NULL; ++info) {
00957 if (info->IsRegistered() && (name == info->GetRegistrationAddress().GetHostName() || (info->GetTransport() && addr.GetHostName() == info->GetTransport()->GetRemoteAddress().GetHostName())) && meth == info->GetMethod())
00958 return info;
00959 }
00960 return NULL;
00961 }
00962 };
00963
00964 static BOOL WriteSIPInfo(
00965 OpalTransport & transport,
00966 void * info
00967 );
00968
00969 BOOL TransmitSIPInfo (
00970 SIP_PDU::Methods method,
00971 const PString & host,
00972 const PString & username,
00973 const PString & authName = PString::Empty(),
00974 const PString & password = PString::Empty(),
00975 const PString & authRealm = PString::Empty(),
00976 const PString & body = PString::Empty(),
00977 int timeout = 0
00978 );
00979
00980 BOOL TransmitSIPUnregistrationInfo (
00981 const PString & host,
00982 const PString & username,
00983 SIP_PDU::Methods method
00984 );
00985
00986 void ParsePartyName(
00987 const PString & remoteParty,
00988 PString & party);
00989
00990 SIPURL proxy;
00991 PString userAgentString;
00992
00993 BOOL mimeForm;
00994 unsigned maxRetries;
00995 PTimeInterval retryTimeoutMin;
00996 PTimeInterval retryTimeoutMax;
00997 PTimeInterval nonInviteTimeout;
00998 PTimeInterval pduCleanUpTimeout;
00999 PTimeInterval inviteTimeout;
01000 PTimeInterval ackTimeout;
01001 PTimeInterval registrarTimeToLive;
01002 PTimeInterval notifierTimeToLive;
01003 PTimeInterval natBindingTimeout;
01004
01005 RegistrationList activeSIPInfo;
01006
01007 PTimer registrationTimer;
01008 SIPTransactionList messages;
01009 SIPTransactionDict transactions;
01010
01011 PTimer natBindingTimer;
01012 NATBindingRefreshMethod natMethod;
01013
01014 PMutex transactionsMutex;
01015 PMutex connectionsActiveInUse;
01016
01017 unsigned lastSentCSeq;
01018 };
01019
01020 #endif // __OPAL_SIPEP_H
01021
01022
01023