zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/****************************************************************************
 *
 *  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 <strings.h>
#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<CRtspHdr*> 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<CRtspMsg*> 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<CRtspRequestMsg*> 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<CRtspResponseMsg*> CRtspResponseMsgList;
 
#endif //ndef _RTSPMSG_H