Yate
|
00001 /* 00002 * yatesip.h 00003 * Yet Another SIP Stack 00004 * This file is part of the YATE Project http://YATE.null.ro 00005 * 00006 * Yet Another Telephony Engine - a fully featured software PBX and IVR 00007 * Copyright (C) 2004-2006 Null Team 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 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 /* _WINDOWS */ 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(Mutex* mutex = 0); 00063 SIPParty(bool reliable, Mutex* mutex = 0); 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 virtual void* getTransport() = 0; 00069 void setAddr(const String& addr, int port, bool local); 00070 void getAddr(String& addr, int& port, bool local); 00071 inline Mutex* mutex() 00072 { return m_mutex; } 00073 inline const String& getLocalAddr() const 00074 { return m_local; } 00075 inline const String& getPartyAddr() const 00076 { return m_party; } 00077 inline int getLocalPort() const 00078 { return m_localPort; } 00079 inline int getPartyPort() const 00080 { return m_partyPort; } 00081 inline bool isReliable() const 00082 { return m_reliable; } 00083 protected: 00084 Mutex* m_mutex; 00085 bool m_reliable; 00086 String m_local; 00087 String m_party; 00088 int m_localPort; 00089 int m_partyPort; 00090 }; 00091 00098 class YSIP_API SIPMessage : public RefObject 00099 { 00100 public: 00104 enum Flags { 00105 Defaults = 0, 00106 NotReqRport = 0x0001, 00107 NotAddAllow = 0x0002, 00108 NotAddAgent = 0x0004, 00109 RportAfterBranch = 0x0008, 00110 NotSetRport = 0x0010, 00111 NotSetReceived = 0x0020, 00112 NoConnReuse = 0x0040, // Don't add 'alias' parameter to Via header (reliable only) 00113 }; 00114 00118 SIPMessage(const SIPMessage& original); 00119 00123 SIPMessage(const char* _method, const char* _uri, const char* _version = "SIP/2.0"); 00124 00134 SIPMessage(SIPParty* ep, const char* buf, int len = -1, unsigned int* bodyLen = 0); 00135 00139 SIPMessage(const SIPMessage* message, int _code, const char* _reason = 0); 00140 00144 SIPMessage(const SIPMessage* original, const SIPMessage* answer); 00145 00149 virtual ~SIPMessage(); 00150 00161 static SIPMessage* fromParsing(SIPParty* ep, const char* buf, int len = -1, 00162 unsigned int* bodyLen = 0); 00163 00170 void buildBody(const char* buf, int len = -1); 00171 00180 void complete(SIPEngine* engine, const char* user = 0, const char* domain = 0, const char* dlgTag = 0, int flags = -1); 00181 00189 bool copyHeader(const SIPMessage* message, const char* name, const char* newName = 0); 00190 00198 int copyAllHeaders(const SIPMessage* message, const char* name, const char* newName = 0); 00199 00204 inline SIPParty* getParty() const 00205 { return m_ep; } 00206 00211 void setParty(SIPParty* ep = 0); 00212 00216 inline bool isValid() const 00217 { return m_valid; } 00218 00222 inline bool isAnswer() const 00223 { return m_answer; } 00224 00229 inline bool isOutgoing() const 00230 { return m_outgoing; } 00231 00236 inline bool isACK() const 00237 { return m_ack; } 00238 00243 inline bool isReliable() const 00244 { return m_ep ? m_ep->isReliable() : false; } 00245 00250 inline int getCSeq() const 00251 { return m_cseq; } 00252 00257 inline int getFlags() const 00258 { return m_flags; } 00259 00265 const MimeHeaderLine* getHeader(const char* name) const; 00266 00272 const MimeHeaderLine* getLastHeader(const char* name) const; 00273 00279 int countHeaders(const char* name) const; 00280 00288 const NamedString* getParam(const char* name, const char* param, bool last = false) const; 00289 00296 const String& getHeaderValue(const char* name, bool last = false) const; 00297 00305 const String& getParamValue(const char* name, const char* param, bool last = false) const; 00306 00312 inline void addHeader(const char* name, const char* value = 0) 00313 { header.append(new MimeHeaderLine(name,value)); } 00314 00319 inline void addHeader(MimeHeaderLine* line) 00320 { header.append(line); } 00321 00326 void clearHeaders(const char* name); 00327 00331 inline void setHeader(const char* name, const char* value = 0) 00332 { clearHeaders(name); addHeader(name,value); } 00333 00344 MimeAuthLine* buildAuth(const String& username, const String& password, 00345 const String& meth, const String& uri, bool proxy = false, SIPEngine* engine = 0) const; 00346 00353 MimeAuthLine* buildAuth(const SIPMessage& original, SIPEngine* engine = 0) const; 00354 00360 inline void setAutoAuth(const char* username = 0, const char* password = 0) 00361 { m_authUser = username; m_authPass = password; } 00362 00367 inline const String& getAuthUsername() const 00368 { return m_authUser; } 00369 00374 inline const String& getAuthPassword() const 00375 { return m_authPass; } 00376 00381 ObjList* getRoutes() const; 00382 00387 void addRoutes(const ObjList* routes); 00388 00392 const DataBlock& getBuffer() const; 00393 00397 const String& getHeaders() const; 00398 00402 void setBody(MimeBody* newbody = 0); 00403 00407 String version; 00408 00412 String method; 00413 00417 String uri; 00418 00422 int code; 00423 00427 String reason; 00428 00432 ObjList header; 00433 00438 MimeBody* body; 00439 00440 protected: 00441 bool parse(const char* buf, int len, unsigned int* bodyLen); 00442 bool parseFirst(String& line); 00443 SIPParty* m_ep; 00444 bool m_valid; 00445 bool m_answer; 00446 bool m_outgoing; 00447 bool m_ack; 00448 int m_cseq; 00449 int m_flags; 00450 mutable String m_string; 00451 mutable DataBlock m_data; 00452 String m_authUser; 00453 String m_authPass; 00454 private: 00455 SIPMessage(); // no, thanks 00456 }; 00457 00462 class YSIP_API SIPDialog : public String 00463 { 00464 public: 00468 SIPDialog(); 00469 00474 SIPDialog(const SIPDialog& original); 00475 00480 explicit SIPDialog(const SIPMessage& message); 00481 00486 inline explicit SIPDialog(const String& callid) 00487 : String(callid) 00488 { } 00489 00495 SIPDialog& operator=(const SIPDialog& original); 00496 00502 SIPDialog& operator=(const SIPMessage& message); 00503 00509 SIPDialog& operator=(const String& callid); 00510 00517 bool matches(const SIPDialog& other, bool ignoreURIs) const; 00518 00524 inline bool operator==(const SIPDialog& other) const 00525 { return matches(other,false); } 00526 00532 inline bool operator!=(const SIPDialog& other) const 00533 { return !matches(other,false); } 00534 00540 inline bool operator&=(const SIPDialog& other) const 00541 { return matches(other,true); } 00542 00548 inline bool operator|=(const SIPDialog& other) const 00549 { return !matches(other,true); } 00550 00556 inline const String& fromURI(bool outgoing) const 00557 { return outgoing ? localURI : remoteURI; } 00558 00564 inline const String& fromTag(bool outgoing) const 00565 { return outgoing ? localTag : remoteTag; } 00566 00572 inline const String& toURI(bool outgoing) const 00573 { return outgoing ? remoteURI : localURI; } 00574 00580 inline const String& toTag(bool outgoing) const 00581 { return outgoing ? remoteTag : localTag; } 00582 00586 String localURI; 00587 00591 String localTag; 00592 00596 String remoteURI; 00597 00601 String remoteTag; 00602 }; 00603 00608 class YSIP_API SIPTransaction : public RefObject 00609 { 00610 public: 00614 enum State { 00615 // Invalid state - before constructor or after destructor 00616 Invalid, 00617 00618 // Initial state - after the initial message was inserted 00619 Initial, 00620 00621 // Trying state - got the message but no decision made yet 00622 Trying, 00623 00624 // Process state - while locally processing the event 00625 Process, 00626 00627 // Retrans state - retransmits latest message until getting ACK 00628 Retrans, 00629 00630 // Finish state - transmits the last message on client retransmission 00631 Finish, 00632 00633 // Cleared state - removed from engine, awaiting destruction 00634 Cleared 00635 }; 00636 00640 enum Processed { 00641 // Not matched at all 00642 NoMatch, 00643 00644 // Belongs to another dialog - probably result of a fork 00645 NoDialog, 00646 00647 // Matched to transaction/dialog and processed 00648 Matched 00649 }; 00650 00658 SIPTransaction(SIPMessage* message, SIPEngine* engine, bool outgoing = true); 00659 00665 SIPTransaction(const SIPTransaction& original, const String& tag); 00666 00670 virtual ~SIPTransaction(); 00671 00675 static const char* stateName(int state); 00676 00681 inline int getState() const 00682 { return m_state; } 00683 00688 inline bool isActive() const 00689 { return (Invalid < m_state) && (m_state < Finish); } 00690 00694 inline const SIPMessage* initialMessage() const 00695 { return m_firstMessage; } 00696 00700 inline const SIPMessage* latestMessage() const 00701 { return m_lastMessage; } 00702 00706 inline const SIPMessage* recentMessage() const 00707 { return m_lastMessage ? m_lastMessage : m_firstMessage; } 00708 00712 inline SIPEngine* getEngine() const 00713 { return m_engine; } 00714 00719 inline bool isOutgoing() const 00720 { return m_outgoing; } 00721 00726 inline bool isIncoming() const 00727 { return !m_outgoing; } 00728 00733 inline bool isInvite() const 00734 { return m_invite; } 00735 00740 inline bool isReliable() const 00741 { return m_firstMessage ? m_firstMessage->isReliable() : false; } 00742 00746 inline const String& getMethod() const 00747 { return m_firstMessage ? m_firstMessage->method : String::empty(); } 00748 00752 inline const String& getURI() const 00753 { return m_firstMessage ? m_firstMessage->uri : String::empty(); } 00754 00759 inline const String& getBranch() const 00760 { return m_branch; } 00761 00766 inline const String& getCallID() const 00767 { return m_callid; } 00768 00773 inline const String& getDialogTag() const 00774 { return m_tag; } 00775 00780 void setDialogTag(const char* tag = 0); 00781 00786 inline void setTransmit() 00787 { m_transmit = true; } 00788 00793 inline void setCleared() 00794 { changeState(Cleared); } 00795 00803 void requestAuth(const String& realm, const String& domain, bool stale, bool proxy = false); 00804 00813 int authUser(String& user, bool proxy = false, GenObject* userData = 0); 00814 00823 virtual Processed processMessage(SIPMessage* message, const String& branch); 00824 00830 virtual void processMessage(SIPMessage* message); 00831 00840 virtual SIPEvent* getEvent(bool pendingOnly = false); 00841 00846 bool setResponse() const; 00847 00854 bool setResponse(int code, const char* reason = 0); 00855 00859 void setResponse(SIPMessage* message); 00860 00865 inline int getResponseCode() const 00866 { return m_response; } 00867 00871 inline void setUserData(void* data) 00872 { m_private = data; } 00873 00877 inline void* getUserData() const 00878 { return m_private; } 00879 00880 protected: 00886 SIPTransaction(SIPTransaction& original, SIPMessage* answer); 00887 00891 virtual void destroyed(); 00892 00898 bool tryAutoAuth(SIPMessage* answer); 00899 00907 virtual SIPEvent* getClientEvent(int state, int timeout); 00908 00916 virtual SIPEvent* getServerEvent(int state, int timeout); 00917 00924 virtual void processClientMessage(SIPMessage* message, int state); 00925 00932 virtual void processServerMessage(SIPMessage* message, int state); 00933 00939 bool changeState(int newstate); 00940 00945 void setLatestMessage(SIPMessage* message = 0); 00946 00952 void setPendingEvent(SIPEvent* event = 0, bool replace = false); 00953 00958 inline bool isPendingEvent() const 00959 { return (m_pending != 0); } 00960 00966 void setTimeout(u_int64_t delay = 0, unsigned int count = 1); 00967 00968 bool m_outgoing; 00969 bool m_invite; 00970 bool m_transmit; 00971 int m_state; 00972 int m_response; 00973 unsigned int m_timeouts; 00974 u_int64_t m_delay; 00975 u_int64_t m_timeout; 00976 SIPMessage* m_firstMessage; 00977 SIPMessage* m_lastMessage; 00978 SIPEvent* m_pending; 00979 SIPEngine* m_engine; 00980 String m_branch; 00981 String m_callid; 00982 String m_tag; 00983 void *m_private; 00984 }; 00985 00990 class YSIP_API SIPEvent 00991 { 00992 friend class SIPTransaction; 00993 public: 00994 00995 SIPEvent() 00996 : m_message(0), m_transaction(0), m_state(SIPTransaction::Invalid) 00997 { } 00998 00999 SIPEvent(SIPMessage* message, SIPTransaction* transaction = 0); 01000 01001 ~SIPEvent(); 01002 01007 inline SIPEngine* getEngine() const 01008 { return m_transaction ? m_transaction->getEngine() : 0; } 01009 01014 inline SIPMessage* getMessage() const 01015 { return m_message; } 01016 01021 inline SIPTransaction* getTransaction() const 01022 { return m_transaction; } 01023 01028 inline bool isOutgoing() const 01029 { return m_message && m_message->isOutgoing(); } 01030 01035 inline bool isIncoming() const 01036 { return m_message && !m_message->isOutgoing(); } 01037 01041 inline SIPParty* getParty() const 01042 { return m_message ? m_message->getParty() : 0; } 01043 01047 inline void* getUserData() const 01048 { return m_transaction ? m_transaction->getUserData() : 0; } 01049 01053 inline int getState() const 01054 { return m_state; } 01055 01060 inline bool isActive() const 01061 { return (SIPTransaction::Invalid < m_state) && (m_state < SIPTransaction::Finish); } 01062 01063 protected: 01064 SIPMessage* m_message; 01065 SIPTransaction* m_transaction; 01066 int m_state; 01067 }; 01068 01073 class YSIP_API SIPEngine : public DebugEnabler, public Mutex 01074 { 01075 public: 01079 SIPEngine(const char* userAgent = 0); 01080 01084 virtual ~SIPEngine(); 01085 01091 virtual bool buildParty(SIPMessage* message) = 0; 01092 01106 virtual bool checkUser(String& username, const String& realm, const String& nonce, 01107 const String& method, const String& uri, const String& response, 01108 const SIPMessage* message, const MimeHeaderLine* authLine, GenObject* userData); 01109 01120 virtual bool checkAuth(bool noUser, String& username, const SIPMessage* message, 01121 const MimeHeaderLine* authLine, GenObject* userData); 01122 01132 int authUser(const SIPMessage* message, String& user, bool proxy = false, GenObject* userData = 0); 01133 01141 SIPTransaction* addMessage(SIPParty* ep, const char* buf, int len = -1); 01142 01149 SIPTransaction* addMessage(SIPMessage* message); 01150 01158 SIPEvent *getEvent(); 01159 01165 bool process(); 01166 01173 virtual void processEvent(SIPEvent *event); 01174 01181 virtual SIPTransaction* forkInvite(SIPMessage* answer, SIPTransaction* trans); 01182 01189 virtual u_int64_t getUserTimeout() const; 01190 01197 u_int64_t getTimer(char which, bool reliable = false) const; 01198 01204 inline unsigned int getReqTransCount() const 01205 { return m_reqTransCount; } 01206 01212 inline unsigned int getRspTransCount() const 01213 { return m_rspTransCount; } 01214 01219 inline unsigned int getMaxForwards() const 01220 { return m_maxForwards; } 01221 01225 inline const String& getUserAgent() const 01226 { return m_userAgent; } 01227 01231 inline int getNextCSeq() 01232 { return ++m_cseq; } 01233 01238 inline bool lazyTrying() const 01239 { return m_lazyTrying; } 01240 01245 inline void lazyTrying(bool lazy100) 01246 { m_lazyTrying = lazy100; } 01247 01252 inline int flags() const 01253 { return m_flags; } 01254 01259 void nonceGet(String& nonce); 01260 01266 long nonceAge(const String& nonce); 01267 01272 void ncGet(String& nc); 01273 01285 static void buildAuth(const String& username, const String& realm, const String& passwd, 01286 const String& nonce, const String& method, const String& uri, String& response, 01287 const NamedList& qop = NamedList::empty()); 01288 01296 static void buildAuth(const String& hash_a1, const String& nonce, const String& hash_a2, 01297 String& response); 01298 01304 bool isAllowed(const char* method) const; 01305 01310 void addAllowed(const char* method); 01311 01316 inline const String& getAllowed() const 01317 { return m_allowed; } 01318 01323 inline void remove(SIPTransaction* transaction) 01324 { lock(); m_transList.remove(transaction,false); unlock(); } 01325 01330 inline void append(SIPTransaction* transaction) 01331 { lock(); m_transList.append(transaction); unlock(); } 01332 01337 inline void insert(SIPTransaction* transaction) 01338 { lock(); m_transList.insert(transaction); unlock(); } 01339 01340 protected: 01344 ObjList m_transList; 01345 01346 u_int64_t m_t1; 01347 u_int64_t m_t4; 01348 int m_reqTransCount; 01349 int m_rspTransCount; 01350 unsigned int m_maxForwards; 01351 int m_cseq; 01352 int m_flags; 01353 bool m_lazyTrying; 01354 String m_userAgent; 01355 String m_allowed; 01356 u_int32_t m_nc; 01357 String m_nonce; 01358 String m_nonce_secret; 01359 u_int32_t m_nonce_time; 01360 Mutex m_nonce_mutex; 01361 }; 01362 01363 } 01364 01365 #endif /* __YATESIP_H */ 01366 01367 /* vi: set ts=8 sw=4 sts=4 noet: */