/**************************************************************************** * * Copyright (C) 2000-2001 RealNetworks, Inc. All rights reserved. * * This program is free software. It may be distributed under the terms * in the file LICENSE, found in the top level of the source distribution. * */ #ifndef _RTSPMSG_H #define _RTSPMSG_H #include "types.h" #include "str.h" #include "tlist.h" #include "TypeDef.h" #include #include "MD5Calc.h" #include "tool.h" // These must correspond with s_pVerbs indices enum RtspVerb { VERB_NONE, VERB_ANNOUNCE, VERB_DESCRIBE, VERB_GETPARAM, VERB_OPTIONS, VERB_PAUSE, VERB_PLAY, VERB_RECORD, VERB_REDIRECT, VERB_SETUP, VERB_SETPARAM, VERB_TEARDOWN, // extensions here VERB_LAST }; enum RtspMsgType { RTSP_TYPE_NONE, RTSP_TYPE_REQUEST, RTSP_TYPE_RESPONSE, RTSP_TYPE_LAST }; class CAuthenticator: public CMD5Calc { public: CAuthenticator(void); ~CAuthenticator(void); void reset(); void setRealmAndNonce(char const* realm, char const* nonce); void setUsernameAndPassword(char const* username, char const* password, bool passwordIsMD5 = false); void reclaimDigestResponse(char const* responseStr); char const* realm() const { return fRealm; } char const* nonce() const { return fNonce; } char const* username() const { return fUsername; } char const* password() const { return fPassword; } char const* computeDigestResponse(char *cmd, char *url); private: void resetRealmAndNonce(); void resetUsernameAndPassword(); void assignRealmAndNonce(char const* realm, char const* nonce); void assignUsernameAndPassword(char const* username, char const* password, bool passwordIsMD5); private: char *fRealm; char *fNonce; char *fUsername; char *fPassword; bool fPasswordIsMD5; }; class CUserData { public: CUserData(void) { HostIp[0] = '\0'; Port = 0; SockFd = 0; ServerPort = 0; MulticastIp[0] = '\0'; MulticastPort = 0; RtpSSrc = 0; AuthRealm[0] = '\0'; AuthNonce[0] = '\0'; } ~CUserData(void) {} //Remote host ip and port char HostIp[16+1]; UINT16 Port; CString TransID; UINT16 ServerPort; char MulticastIp[16+1]; UINT16 MulticastPort; unsigned int RtpSSrc; int SockFd; char AuthRealm[64+1]; char AuthNonce[64+1]; }; class CRtspHdr { private: CRtspHdr(void); public: CRtspHdr(const CString& strKey); CRtspHdr(const CString& strKey, const CString& strVal); const CString& GetKey(void) const; const CString& GetVal(void) const; void SetVal(const CString& strVal); protected: CString m_strKey; CString m_strVal; }; typedef TDoubleList CRtspHdrList; class CRtspMsg { private: bool operator==(const CRtspMsg& other) const; bool operator!=(const CRtspMsg& other) const; public: CRtspMsg(void); CRtspMsg(const CRtspMsg& other); virtual ~CRtspMsg(void); const CRtspMsg& operator=(const CRtspMsg& other); operator CPByte(void) const; Byte operator[](UINT32 nPos) const; virtual RtspMsgType GetType(void) const; // Total header length for key/val pairs (incl. ": " and CRLF) // but NOT separator CRLF size_t GetHdrLen(void) const; size_t GetBufLen(void) const; Byte GetAt(UINT32 nPos) const; void SetAt(UINT32 nPos, Byte by); void GetRtspVer(UINT32* puMajor, UINT32* puMinor) const; void SetRtspVer(UINT32 uMajor, UINT32 uMinor); size_t GetHdrCount(void) const; CString GetHdr(const CString& strKey) const; CRtspHdr* GetHdr(UINT32 nIndex) const; void SetHdr(const CString& strKey, const CString& strVal); void SetHdr(const CRtspHdr& hdrNew); PByte GetBuf(void) const; void SetBuf(CPByte buf, size_t nLen); public: void SetRemoteHost(const char FromIP[], UINT16 FromPort) { snprintf(mUserData.HostIp, 16, FromIP); mUserData.Port = FromPort; } void SetSockFd(int sockfd) { mUserData.SockFd = sockfd; } void SetRealmAndNonce(char *realm, char *nonce) { if (realm != NULL && strlen(realm) > 0) { strcpy(mUserData.AuthRealm, realm); } if (nonce != NULL && strlen(nonce) > 0) { strcpy(mUserData.AuthNonce, nonce); } } CUserData mUserData; protected: UINT32 m_nRtspVer; // RTSP version (hiword.loword) UINT32 m_nSeq; CRtspHdrList m_listHdrs; size_t m_nBufLen; PByte m_pbuf; }; typedef TDoubleList CRtspMsgList; class CRtspRequestMsg : public CRtspMsg { public: class CUrlParam { public: CUrlParam(void) { DevAor[0] = '\0'; Type = -1; BeginTime[0] = '\0'; EndTime[0] = '\0'; StreamID = -1; SessionID[0] = '\0'; StreamType = 0; } ~CUrlParam(void) {} //Get parameter to save by different keywords from input line void LineTo(char Line[]) { if (0 == strlen(Line) ) return; char* p = Line; if (0 == strncmp(p, "DevAor", 6) ) { p += strlen("DevAor"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { strncpy2(DevAor, p, SIP_AOR_MAX_SIZE); } } else if (0 == strncasecmp(p, "Type", 4) ) { p += strlen("Type"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { Type = atoi(p); } } else if (0 == strncasecmp(p, "sessionid", 9)) { p += strlen("sessionid"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { strncpy2(SessionID, p, HTTP_URL_MAX_LEN); } } else if (0 == strncasecmp(p, "BeginTime", 9) ) { p += strlen("BeginTime"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { strncpy2(BeginTime, p, DATE_TIME_MAX_LEN); //Delete '.' from time string p = BeginTime; while ( (*p != '\0') && (*p != '.') ) p++; if ('.' == *p) *p = 0x20; //0x2E = ' ' } } else if (0 == strncasecmp(p, "EndTime", 7) ) { p += strlen("EndTime"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { strncpy2(EndTime, p, DATE_TIME_MAX_LEN); //Delete '.' from time string p = EndTime; while ( (*p != '\0') && (*p != '.') ) p++; if ('.' == *p) *p = 0x20; //0x2E = ' ' } } else if (0 == strncasecmp(p, "trackID", 7) ) { p += strlen("trackID"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { StreamID = atoi(p); } } else if (0 == strncasecmp(p, "StreamType", 10)) { p += strlen("StreamType"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { StreamType = atoi(p); } } else if (0 == strncasecmp(p, "PlayType", 8)) { p += strlen("PlayType"); while ( (*p != '\0') && ( ('=' == *p) || (' ' == *p) ) ) p++; if (*p != '\0') { Type = atoi(p); } } else { printf("%s: Failed to check keywords Line<%s>! \n", __FUNCTION__, Line); } } public: char DevAor[SIP_AOR_MAX_SIZE+1]; int Type; char BeginTime[DATE_TIME_MAX_LEN+1]; char EndTime[DATE_TIME_MAX_LEN+1]; int StreamID; char SessionID[HTTP_URL_MAX_LEN+1]; int StreamType; }; public: CRtspRequestMsg(void); CRtspRequestMsg(const CRtspRequestMsg& other); virtual ~CRtspRequestMsg(void); const CRtspRequestMsg& operator=(const CRtspRequestMsg& other); virtual RtspMsgType GetType(void) const; RtspVerb GetVerb(void) const; CPCHAR GetVerbStr(void) const; void SetVerb(RtspVerb verb); void SetVerb(CPCHAR szVerb); CPCHAR GetUrl(void) const; void SetUrl(const CString& strUrl); void GetFileName(char filename[64]); public: void ParserUri(void); SOCKETTYPE_E ParserClientStreamTransType(void); bool ParserClientAddr(char * ClientAddr); UINT16 ParserClientPort(void); bool ParserClientMulticast(void); UINT16 ParserClientMulticastPort(void); CUrlParam UrlParam; protected: enum RtspVerb m_verb; CString m_strUrl; }; typedef TDoubleList CRtspRequestMsgList; class CRtspResponseMsg : public CRtspMsg { public: CRtspResponseMsg(void); CRtspResponseMsg(const CRtspResponseMsg& other); virtual ~CRtspResponseMsg(void); const CRtspResponseMsg& operator=(const CRtspResponseMsg& other); virtual RtspMsgType GetType(void) const; UINT32 GetStatusCode(void) const; const CString& GetStatusMsg(void) const; void SetStatus(UINT32 nCode, CPCHAR szMsg = NULL); protected: UINT32 m_nCode; CString m_strStatusMsg; }; typedef TDoubleList CRtspResponseMsgList; #endif //ndef _RTSPMSG_H