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
00092 class YSIP_API SIPMessage : public RefObject
00093 {
00094 public:
00098 SIPMessage(const SIPMessage& original);
00099
00103 SIPMessage(const char* _method, const char* _uri, const char* _version = "SIP/2.0");
00104
00108 SIPMessage(SIPParty* ep, const char* buf, int len = -1);
00109
00113 SIPMessage(const SIPMessage* message, int _code, const char* _reason = 0);
00114
00118 SIPMessage(const SIPMessage* original, const SIPMessage* answer);
00119
00123 virtual ~SIPMessage();
00124
00129 static SIPMessage* fromParsing(SIPParty* ep, const char* buf, int len = -1);
00130
00134 void complete(SIPEngine* engine, const char* user = 0, const char* domain = 0, const char* dlgTag = 0);
00135
00143 bool copyHeader(const SIPMessage* message, const char* name, const char* newName = 0);
00144
00152 int copyAllHeaders(const SIPMessage* message, const char* name, const char* newName = 0);
00153
00158 inline SIPParty* getParty() const
00159 { return m_ep; }
00160
00165 void setParty(SIPParty* ep = 0);
00166
00170 inline bool isValid() const
00171 { return m_valid; }
00172
00176 inline bool isAnswer() const
00177 { return m_answer; }
00178
00183 inline bool isOutgoing() const
00184 { return m_outgoing; }
00185
00190 inline bool isACK() const
00191 { return m_ack; }
00192
00197 inline bool isReliable() const
00198 { return m_ep ? m_ep->isReliable() : false; }
00199
00203 inline int getCSeq() const
00204 { return m_cseq; }
00205
00211 const MimeHeaderLine* getHeader(const char* name) const;
00212
00218 const MimeHeaderLine* getLastHeader(const char* name) const;
00219
00225 int countHeaders(const char* name) const;
00226
00233 const NamedString* getParam(const char* name, const char* param) const;
00234
00240 const String& getHeaderValue(const char* name) const;
00241
00248 const String& getParamValue(const char* name, const char* param) const;
00249
00255 inline void addHeader(const char* name, const char* value = 0)
00256 { header.append(new MimeHeaderLine(name,value)); }
00257
00262 inline void addHeader(MimeHeaderLine* line)
00263 { header.append(line); }
00264
00269 void clearHeaders(const char* name);
00270
00274 inline void setHeader(const char* name, const char* value = 0)
00275 { clearHeaders(name); addHeader(name,value); }
00276
00286 MimeAuthLine* buildAuth(const String& username, const String& password,
00287 const String& meth, const String& uri, bool proxy = false) const;
00288
00294 MimeAuthLine* buildAuth(const SIPMessage& original) const;
00295
00301 inline void setAutoAuth(const char* username = 0, const char* password = 0)
00302 { m_authUser = username; m_authPass = password; }
00303
00308 inline const String& getAuthUsername() const
00309 { return m_authUser; }
00310
00315 inline const String& getAuthPassword() const
00316 { return m_authPass; }
00317
00322 ObjList* getRoutes() const;
00323
00328 void addRoutes(const ObjList* routes);
00329
00333 const DataBlock& getBuffer() const;
00334
00338 const String& getHeaders() const;
00339
00343 void setBody(MimeBody* newbody = 0);
00344
00348 String version;
00349
00353 String method;
00354
00358 String uri;
00359
00363 int code;
00364
00368 String reason;
00369
00373 ObjList header;
00374
00379 MimeBody* body;
00380
00381 protected:
00382 bool parse(const char* buf, int len);
00383 bool parseFirst(String& line);
00384 SIPParty* m_ep;
00385 bool m_valid;
00386 bool m_answer;
00387 bool m_outgoing;
00388 bool m_ack;
00389 int m_cseq;
00390 mutable String m_string;
00391 mutable DataBlock m_data;
00392 String m_authUser;
00393 String m_authPass;
00394 private:
00395 SIPMessage();
00396 };
00397
00401 class YSIP_API SIPDialog : public String
00402 {
00403 public:
00404 SIPDialog();
00405 SIPDialog(const SIPDialog& original);
00406 SIPDialog(const SIPMessage& message);
00407 SIPDialog& operator=(const SIPDialog& original);
00408 SIPDialog& operator=(const SIPMessage& message);
00409 SIPDialog& operator=(const String& callid);
00410 bool operator==(const SIPDialog& other) const;
00411 bool operator!=(const SIPDialog& other) const;
00412 String localURI;
00413 String localTag;
00414 String remoteURI;
00415 String remoteTag;
00416 };
00417
00421 class YSIP_API SIPTransaction : public RefObject
00422 {
00423 public:
00427 enum State {
00428
00429 Invalid,
00430
00431
00432 Initial,
00433
00434
00435 Trying,
00436
00437
00438 Process,
00439
00440
00441 Retrans,
00442
00443
00444 Finish,
00445
00446
00447 Cleared
00448 };
00449
00453 enum Processed {
00454
00455 NoMatch,
00456
00457
00458 NoDialog,
00459
00460
00461 Matched
00462 };
00463
00471 SIPTransaction(SIPMessage* message, SIPEngine* engine, bool outgoing = true);
00472
00478 SIPTransaction(const SIPTransaction& original, const String& tag);
00479
00483 virtual ~SIPTransaction();
00484
00488 static const char* stateName(int state);
00489
00494 inline int getState() const
00495 { return m_state; }
00496
00501 inline bool isActive() const
00502 { return (Invalid < m_state) && (m_state < Finish); }
00503
00507 inline const SIPMessage* initialMessage() const
00508 { return m_firstMessage; }
00509
00513 inline const SIPMessage* latestMessage() const
00514 { return m_lastMessage; }
00515
00519 inline const SIPMessage* recentMessage() const
00520 { return m_lastMessage ? m_lastMessage : m_firstMessage; }
00521
00525 inline SIPEngine* getEngine() const
00526 { return m_engine; }
00527
00532 Mutex* mutex();
00533
00538 inline bool isOutgoing() const
00539 { return m_outgoing; }
00540
00545 inline bool isIncoming() const
00546 { return !m_outgoing; }
00547
00552 inline bool isInvite() const
00553 { return m_invite; }
00554
00559 inline bool isReliable() const
00560 { return m_firstMessage ? m_firstMessage->isReliable() : false; }
00561
00565 inline const String& getMethod() const
00566 { return m_firstMessage ? m_firstMessage->method : String::empty(); }
00567
00571 inline const String& getURI() const
00572 { return m_firstMessage ? m_firstMessage->uri : String::empty(); }
00573
00578 inline const String& getBranch() const
00579 { return m_branch; }
00580
00585 inline const String& getCallID() const
00586 { return m_callid; }
00587
00592 inline const String& getDialogTag() const
00593 { return m_tag; }
00594
00599 void setDialogTag(const char* tag = 0);
00600
00605 inline void setTransmit()
00606 { m_transmit = true; }
00607
00608
00616 void requestAuth(const String& realm, const String& domain, bool stale, bool proxy = false);
00617
00626 int authUser(String& user, bool proxy = false, GenObject* userData = 0);
00627
00636 virtual Processed processMessage(SIPMessage* message, const String& branch);
00637
00646 virtual SIPEvent* getEvent(bool pendingOnly = false);
00647
00654 bool setResponse(int code, const char* reason = 0);
00655
00659 void setResponse(SIPMessage* message);
00660
00665 inline int getResponseCode() const
00666 { return m_response; }
00667
00671 inline void setUserData(void* data)
00672 { m_private = data; }
00673
00677 inline void* getUserData() const
00678 { return m_private; }
00679
00680 protected:
00686 SIPTransaction(SIPTransaction& original, SIPMessage* answer);
00687
00693 bool tryAutoAuth(SIPMessage* answer);
00694
00702 virtual SIPEvent* getClientEvent(int state, int timeout);
00703
00711 virtual SIPEvent* getServerEvent(int state, int timeout);
00712
00719 virtual void processClientMessage(SIPMessage* message, int state);
00720
00727 virtual void processServerMessage(SIPMessage* message, int state);
00728
00734 bool changeState(int newstate);
00735
00740 void setLatestMessage(SIPMessage* message = 0);
00741
00747 void setPendingEvent(SIPEvent* event = 0, bool replace = false);
00748
00753 inline bool isPendingEvent() const
00754 { return (m_pending != 0); }
00755
00761 void setTimeout(u_int64_t delay = 0, unsigned int count = 1);
00762
00763 bool m_outgoing;
00764 bool m_invite;
00765 bool m_transmit;
00766 int m_state;
00767 int m_response;
00768 unsigned int m_timeouts;
00769 u_int64_t m_delay;
00770 u_int64_t m_timeout;
00771 SIPMessage* m_firstMessage;
00772 SIPMessage* m_lastMessage;
00773 SIPEvent* m_pending;
00774 SIPEngine* m_engine;
00775 String m_branch;
00776 String m_callid;
00777 String m_tag;
00778 void *m_private;
00779 };
00780
00784 class YSIP_API SIPEvent
00785 {
00786 friend class SIPTransaction;
00787 public:
00788
00789 SIPEvent()
00790 : m_message(0), m_transaction(0), m_state(SIPTransaction::Invalid)
00791 { }
00792
00793 SIPEvent(SIPMessage* message, SIPTransaction* transaction = 0);
00794
00795 ~SIPEvent();
00796
00800 inline SIPEngine* getEngine() const
00801 { return m_transaction ? m_transaction->getEngine() : 0; }
00802
00806 inline const SIPMessage* getMessage() const
00807 { return m_message; }
00808
00812 inline SIPTransaction* getTransaction() const
00813 { return m_transaction; }
00814
00819 inline bool isOutgoing() const
00820 { return m_message && m_message->isOutgoing(); }
00821
00826 inline bool isIncoming() const
00827 { return m_message && !m_message->isOutgoing(); }
00828
00832 inline SIPParty* getParty() const
00833 { return m_message ? m_message->getParty() : 0; }
00834
00838 inline void* getUserData() const
00839 { return m_transaction ? m_transaction->getUserData() : 0; }
00840
00844 inline int getState() const
00845 { return m_state; }
00846
00851 inline bool isActive() const
00852 { return (SIPTransaction::Invalid < m_state) && (m_state < SIPTransaction::Finish); }
00853
00854 protected:
00855 SIPMessage* m_message;
00856 SIPTransaction* m_transaction;
00857 int m_state;
00858 };
00859
00863 class YSIP_API SIPEngine : public DebugEnabler
00864 {
00865 public:
00869 SIPEngine(const char* userAgent = 0);
00870
00874 virtual ~SIPEngine();
00875
00881 virtual bool buildParty(SIPMessage* message) = 0;
00882
00895 virtual bool checkUser(const String& username, const String& realm, const String& nonce,
00896 const String& method, const String& uri, const String& response,
00897 const SIPMessage* message, GenObject* userData);
00898
00907 virtual bool checkAuth(bool noUser, const SIPMessage* message, GenObject* userData);
00908
00918 int authUser(const SIPMessage* message, String& user, bool proxy = false, GenObject* userData = 0);
00919
00927 SIPTransaction* addMessage(SIPParty* ep, const char* buf, int len = -1);
00928
00935 SIPTransaction* addMessage(SIPMessage* message);
00936
00944 SIPEvent *getEvent();
00945
00951 bool process();
00952
00959 virtual void processEvent(SIPEvent *event);
00960
00967 virtual SIPTransaction* forkInvite(SIPMessage* answer, SIPTransaction* trans);
00968
00974 virtual u_int64_t getUserTimeout() const;
00975
00982 u_int64_t getTimer(char which, bool reliable = false) const;
00983
00988 inline unsigned int getMaxForwards() const
00989 { return m_maxForwards; }
00990
00994 inline const String& getUserAgent() const
00995 { return m_userAgent; }
00996
01000 inline int getNextCSeq()
01001 { return ++m_cseq; }
01002
01007 void nonceGet(String& nonce);
01008
01014 long nonceAge(const String& nonce);
01015
01026 static void buildAuth(const String& username, const String& realm, const String& passwd,
01027 const String& nonce, const String& method, const String& uri, String& response);
01028
01036 static void buildAuth(const String& hash_a1, const String& nonce, const String& hash_a2,
01037 String& response);
01038
01044 bool isAllowed(const char* method) const;
01045
01050 void addAllowed(const char* method);
01051
01056 inline const String& getAllowed() const
01057 { return m_allowed; }
01058
01063 inline Mutex* mutex()
01064 { return &m_mutex; }
01065
01070 ObjList TransList;
01071
01072 protected:
01073 Mutex m_mutex;
01074 u_int64_t m_t1;
01075 u_int64_t m_t4;
01076 unsigned int m_maxForwards;
01077 int m_cseq;
01078 String m_userAgent;
01079 String m_allowed;
01080 String m_nonce;
01081 String m_nonce_secret;
01082 u_int32_t m_nonce_time;
01083 Mutex m_nonce_mutex;
01084 };
01085
01086 }
01087
01088 #endif
01089
01090