00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __YATESIP_H
00025 #define __YATESIP_H
00026
00027 #include <yateclass.h>
00028 #include <yatemime.h>
00029
00030 #ifdef _WINDOWS
00031
00032 #ifdef LIBYSIP_EXPORTS
00033 #define YSIP_API __declspec(dllexport)
00034 #else
00035 #ifndef LIBYSIP_STATIC
00036 #define YSIP_API __declspec(dllimport)
00037 #endif
00038 #endif
00039
00040 #endif
00041
00042 #ifndef YSIP_API
00043 #define YSIP_API
00044 #endif
00045
00049 namespace TelEngine {
00050
00054 extern YSIP_API TokenDict* SIPResponses;
00055
00056 class SIPEngine;
00057 class SIPEvent;
00058
00059 class YSIP_API SIPParty : public RefObject
00060 {
00061 public:
00062 SIPParty();
00063 SIPParty(bool reliable);
00064 virtual ~SIPParty();
00065 virtual void transmit(SIPEvent* event) = 0;
00066 virtual const char* getProtoName() const = 0;
00067 virtual bool setParty(const URI& uri) = 0;
00068 inline const String& getLocalAddr() const
00069 { return m_local; }
00070 inline const String& getPartyAddr() const
00071 { return m_party; }
00072 inline int getLocalPort() const
00073 { return m_localPort; }
00074 inline int getPartyPort() const
00075 { return m_partyPort; }
00076 inline bool isReliable() const
00077 { return m_reliable; }
00078 protected:
00079 bool m_reliable;
00080 bool m_init;
00081 String m_local;
00082 String m_party;
00083 int m_localPort;
00084 int m_partyPort;
00085 };
00086
00093 class YSIP_API SIPMessage : public RefObject
00094 {
00095 public:
00099 SIPMessage(const SIPMessage& original);
00100
00104 SIPMessage(const char* _method, const char* _uri, const char* _version = "SIP/2.0");
00105
00109 SIPMessage(SIPParty* ep, const char* buf, int len = -1);
00110
00114 SIPMessage(const SIPMessage* message, int _code, const char* _reason = 0);
00115
00119 SIPMessage(const SIPMessage* original, const SIPMessage* answer);
00120
00124 virtual ~SIPMessage();
00125
00130 static SIPMessage* fromParsing(SIPParty* ep, const char* buf, int len = -1);
00131
00135 void complete(SIPEngine* engine, const char* user = 0, const char* domain = 0, const char* dlgTag = 0);
00136
00144 bool copyHeader(const SIPMessage* message, const char* name, const char* newName = 0);
00145
00153 int copyAllHeaders(const SIPMessage* message, const char* name, const char* newName = 0);
00154
00159 inline SIPParty* getParty() const
00160 { return m_ep; }
00161
00166 void setParty(SIPParty* ep = 0);
00167
00171 inline bool isValid() const
00172 { return m_valid; }
00173
00177 inline bool isAnswer() const
00178 { return m_answer; }
00179
00184 inline bool isOutgoing() const
00185 { return m_outgoing; }
00186
00191 inline bool isACK() const
00192 { return m_ack; }
00193
00198 inline bool isReliable() const
00199 { return m_ep ? m_ep->isReliable() : false; }
00200
00204 inline int getCSeq() const
00205 { return m_cseq; }
00206
00212 const MimeHeaderLine* getHeader(const char* name) const;
00213
00219 const MimeHeaderLine* getLastHeader(const char* name) const;
00220
00226 int countHeaders(const char* name) const;
00227
00234 const NamedString* getParam(const char* name, const char* param) const;
00235
00241 const String& getHeaderValue(const char* name) const;
00242
00249 const String& getParamValue(const char* name, const char* param) const;
00250
00256 inline void addHeader(const char* name, const char* value = 0)
00257 { header.append(new MimeHeaderLine(name,value)); }
00258
00263 inline void addHeader(MimeHeaderLine* line)
00264 { header.append(line); }
00265
00270 void clearHeaders(const char* name);
00271
00275 inline void setHeader(const char* name, const char* value = 0)
00276 { clearHeaders(name); addHeader(name,value); }
00277
00287 MimeAuthLine* buildAuth(const String& username, const String& password,
00288 const String& meth, const String& uri, bool proxy = false) const;
00289
00295 MimeAuthLine* buildAuth(const SIPMessage& original) const;
00296
00302 inline void setAutoAuth(const char* username = 0, const char* password = 0)
00303 { m_authUser = username; m_authPass = password; }
00304
00309 inline const String& getAuthUsername() const
00310 { return m_authUser; }
00311
00316 inline const String& getAuthPassword() const
00317 { return m_authPass; }
00318
00323 ObjList* getRoutes() const;
00324
00329 void addRoutes(const ObjList* routes);
00330
00334 const DataBlock& getBuffer() const;
00335
00339 const String& getHeaders() const;
00340
00344 void setBody(MimeBody* newbody = 0);
00345
00349 String version;
00350
00354 String method;
00355
00359 String uri;
00360
00364 int code;
00365
00369 String reason;
00370
00374 ObjList header;
00375
00380 MimeBody* body;
00381
00382 protected:
00383 bool parse(const char* buf, int len);
00384 bool parseFirst(String& line);
00385 SIPParty* m_ep;
00386 bool m_valid;
00387 bool m_answer;
00388 bool m_outgoing;
00389 bool m_ack;
00390 int m_cseq;
00391 mutable String m_string;
00392 mutable DataBlock m_data;
00393 String m_authUser;
00394 String m_authPass;
00395 private:
00396 SIPMessage();
00397 };
00398
00403 class YSIP_API SIPDialog : public String
00404 {
00405 public:
00409 SIPDialog();
00410
00415 SIPDialog(const SIPDialog& original);
00416
00421 explicit SIPDialog(const SIPMessage& message);
00422
00427 inline explicit SIPDialog(const String& callid)
00428 : String(callid)
00429 { }
00430
00436 SIPDialog& operator=(const SIPDialog& original);
00437
00443 SIPDialog& operator=(const SIPMessage& message);
00444
00450 SIPDialog& operator=(const String& callid);
00451
00458 bool matches(const SIPDialog& other, bool ignoreURIs) const;
00459
00465 inline bool operator==(const SIPDialog& other) const
00466 { return matches(other,false); }
00467
00473 inline bool operator!=(const SIPDialog& other) const
00474 { return !matches(other,false); }
00475
00481 inline bool operator&=(const SIPDialog& other) const
00482 { return matches(other,true); }
00483
00489 inline bool operator|=(const SIPDialog& other) const
00490 { return !matches(other,true); }
00491
00497 inline const String& fromURI(bool outgoing) const
00498 { return outgoing ? localURI : remoteURI; }
00499
00505 inline const String& fromTag(bool outgoing) const
00506 { return outgoing ? localTag : remoteTag; }
00507
00513 inline const String& toURI(bool outgoing) const
00514 { return outgoing ? remoteURI : localURI; }
00515
00521 inline const String& toTag(bool outgoing) const
00522 { return outgoing ? remoteTag : localTag; }
00523
00527 String localURI;
00528
00532 String localTag;
00533
00537 String remoteURI;
00538
00542 String remoteTag;
00543 };
00544
00549 class YSIP_API SIPTransaction : public RefObject
00550 {
00551 public:
00555 enum State {
00556
00557 Invalid,
00558
00559
00560 Initial,
00561
00562
00563 Trying,
00564
00565
00566 Process,
00567
00568
00569 Retrans,
00570
00571
00572 Finish,
00573
00574
00575 Cleared
00576 };
00577
00581 enum Processed {
00582
00583 NoMatch,
00584
00585
00586 NoDialog,
00587
00588
00589 Matched
00590 };
00591
00599 SIPTransaction(SIPMessage* message, SIPEngine* engine, bool outgoing = true);
00600
00606 SIPTransaction(const SIPTransaction& original, const String& tag);
00607
00611 virtual ~SIPTransaction();
00612
00616 static const char* stateName(int state);
00617
00622 inline int getState() const
00623 { return m_state; }
00624
00629 inline bool isActive() const
00630 { return (Invalid < m_state) && (m_state < Finish); }
00631
00635 inline const SIPMessage* initialMessage() const
00636 { return m_firstMessage; }
00637
00641 inline const SIPMessage* latestMessage() const
00642 { return m_lastMessage; }
00643
00647 inline const SIPMessage* recentMessage() const
00648 { return m_lastMessage ? m_lastMessage : m_firstMessage; }
00649
00653 inline SIPEngine* getEngine() const
00654 { return m_engine; }
00655
00660 inline bool isOutgoing() const
00661 { return m_outgoing; }
00662
00667 inline bool isIncoming() const
00668 { return !m_outgoing; }
00669
00674 inline bool isInvite() const
00675 { return m_invite; }
00676
00681 inline bool isReliable() const
00682 { return m_firstMessage ? m_firstMessage->isReliable() : false; }
00683
00687 inline const String& getMethod() const
00688 { return m_firstMessage ? m_firstMessage->method : String::empty(); }
00689
00693 inline const String& getURI() const
00694 { return m_firstMessage ? m_firstMessage->uri : String::empty(); }
00695
00700 inline const String& getBranch() const
00701 { return m_branch; }
00702
00707 inline const String& getCallID() const
00708 { return m_callid; }
00709
00714 inline const String& getDialogTag() const
00715 { return m_tag; }
00716
00721 void setDialogTag(const char* tag = 0);
00722
00727 inline void setTransmit()
00728 { m_transmit = true; }
00729
00730
00738 void requestAuth(const String& realm, const String& domain, bool stale, bool proxy = false);
00739
00748 int authUser(String& user, bool proxy = false, GenObject* userData = 0);
00749
00758 virtual Processed processMessage(SIPMessage* message, const String& branch);
00759
00765 virtual void processMessage(SIPMessage* message);
00766
00775 virtual SIPEvent* getEvent(bool pendingOnly = false);
00776
00783 bool setResponse(int code, const char* reason = 0);
00784
00788 void setResponse(SIPMessage* message);
00789
00794 inline int getResponseCode() const
00795 { return m_response; }
00796
00800 inline void setUserData(void* data)
00801 { m_private = data; }
00802
00806 inline void* getUserData() const
00807 { return m_private; }
00808
00809 protected:
00815 SIPTransaction(SIPTransaction& original, SIPMessage* answer);
00816
00820 virtual void destroyed();
00821
00827 bool tryAutoAuth(SIPMessage* answer);
00828
00836 virtual SIPEvent* getClientEvent(int state, int timeout);
00837
00845 virtual SIPEvent* getServerEvent(int state, int timeout);
00846
00853 virtual void processClientMessage(SIPMessage* message, int state);
00854
00861 virtual void processServerMessage(SIPMessage* message, int state);
00862
00868 bool changeState(int newstate);
00869
00874 void setLatestMessage(SIPMessage* message = 0);
00875
00881 void setPendingEvent(SIPEvent* event = 0, bool replace = false);
00882
00887 inline bool isPendingEvent() const
00888 { return (m_pending != 0); }
00889
00895 void setTimeout(u_int64_t delay = 0, unsigned int count = 1);
00896
00897 bool m_outgoing;
00898 bool m_invite;
00899 bool m_transmit;
00900 int m_state;
00901 int m_response;
00902 unsigned int m_timeouts;
00903 u_int64_t m_delay;
00904 u_int64_t m_timeout;
00905 SIPMessage* m_firstMessage;
00906 SIPMessage* m_lastMessage;
00907 SIPEvent* m_pending;
00908 SIPEngine* m_engine;
00909 String m_branch;
00910 String m_callid;
00911 String m_tag;
00912 void *m_private;
00913 };
00914
00919 class YSIP_API SIPEvent
00920 {
00921 friend class SIPTransaction;
00922 public:
00923
00924 SIPEvent()
00925 : m_message(0), m_transaction(0), m_state(SIPTransaction::Invalid)
00926 { }
00927
00928 SIPEvent(SIPMessage* message, SIPTransaction* transaction = 0);
00929
00930 ~SIPEvent();
00931
00936 inline SIPEngine* getEngine() const
00937 { return m_transaction ? m_transaction->getEngine() : 0; }
00938
00943 inline SIPMessage* getMessage() const
00944 { return m_message; }
00945
00950 inline SIPTransaction* getTransaction() const
00951 { return m_transaction; }
00952
00957 inline bool isOutgoing() const
00958 { return m_message && m_message->isOutgoing(); }
00959
00964 inline bool isIncoming() const
00965 { return m_message && !m_message->isOutgoing(); }
00966
00970 inline SIPParty* getParty() const
00971 { return m_message ? m_message->getParty() : 0; }
00972
00976 inline void* getUserData() const
00977 { return m_transaction ? m_transaction->getUserData() : 0; }
00978
00982 inline int getState() const
00983 { return m_state; }
00984
00989 inline bool isActive() const
00990 { return (SIPTransaction::Invalid < m_state) && (m_state < SIPTransaction::Finish); }
00991
00992 protected:
00993 SIPMessage* m_message;
00994 SIPTransaction* m_transaction;
00995 int m_state;
00996 };
00997
01002 class YSIP_API SIPEngine : public DebugEnabler, public Mutex
01003 {
01004 public:
01008 SIPEngine(const char* userAgent = 0);
01009
01013 virtual ~SIPEngine();
01014
01020 virtual bool buildParty(SIPMessage* message) = 0;
01021
01034 virtual bool checkUser(const String& username, const String& realm, const String& nonce,
01035 const String& method, const String& uri, const String& response,
01036 const SIPMessage* message, GenObject* userData);
01037
01046 virtual bool checkAuth(bool noUser, const SIPMessage* message, GenObject* userData);
01047
01057 int authUser(const SIPMessage* message, String& user, bool proxy = false, GenObject* userData = 0);
01058
01066 SIPTransaction* addMessage(SIPParty* ep, const char* buf, int len = -1);
01067
01074 SIPTransaction* addMessage(SIPMessage* message);
01075
01083 SIPEvent *getEvent();
01084
01090 bool process();
01091
01098 virtual void processEvent(SIPEvent *event);
01099
01106 virtual SIPTransaction* forkInvite(SIPMessage* answer, SIPTransaction* trans);
01107
01114 virtual u_int64_t getUserTimeout() const;
01115
01122 u_int64_t getTimer(char which, bool reliable = false) const;
01123
01128 inline unsigned int getMaxForwards() const
01129 { return m_maxForwards; }
01130
01134 inline const String& getUserAgent() const
01135 { return m_userAgent; }
01136
01140 inline int getNextCSeq()
01141 { return ++m_cseq; }
01142
01147 inline bool lazyTrying() const
01148 { return m_lazyTrying; }
01149
01154 inline void lazyTrying(bool lazy100)
01155 { m_lazyTrying = lazy100; }
01156
01161 void nonceGet(String& nonce);
01162
01168 long nonceAge(const String& nonce);
01169
01180 static void buildAuth(const String& username, const String& realm, const String& passwd,
01181 const String& nonce, const String& method, const String& uri, String& response);
01182
01190 static void buildAuth(const String& hash_a1, const String& nonce, const String& hash_a2,
01191 String& response);
01192
01198 bool isAllowed(const char* method) const;
01199
01204 void addAllowed(const char* method);
01205
01210 inline const String& getAllowed() const
01211 { return m_allowed; }
01212
01217 inline void remove(SIPTransaction* transaction)
01218 { lock(); m_transList.remove(transaction,false); unlock(); }
01219
01224 inline void append(SIPTransaction* transaction)
01225 { lock(); m_transList.append(transaction); unlock(); }
01226
01231 inline void insert(SIPTransaction* transaction)
01232 { lock(); m_transList.insert(transaction); unlock(); }
01233
01234 protected:
01238 ObjList m_transList;
01239
01240 u_int64_t m_t1;
01241 u_int64_t m_t4;
01242 unsigned int m_maxForwards;
01243 int m_cseq;
01244 bool m_lazyTrying;
01245 String m_userAgent;
01246 String m_allowed;
01247 String m_nonce;
01248 String m_nonce_secret;
01249 u_int32_t m_nonce_time;
01250 Mutex m_nonce_mutex;
01251 };
01252
01253 }
01254
01255 #endif
01256
01257