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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/****************************************************************************
 *
 *  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.
 *
 */
 
#include "RtspMsg.h"
 
#include <string.h> /* memset */
 
CAuthenticator::CAuthenticator()
{
    fRealm = NULL; 
    fNonce = NULL;
    fUsername = NULL; 
    fPassword = NULL;
    fPasswordIsMD5 = false;
}
 
CAuthenticator::~CAuthenticator()
{
    reset();
}
void CAuthenticator::reset() 
{
    resetRealmAndNonce();
      resetUsernameAndPassword();
}
 
void CAuthenticator::setRealmAndNonce(char const* realm, char const* nonce) 
{
      resetRealmAndNonce();
      assignRealmAndNonce(realm, nonce);
}
 
void CAuthenticator::setUsernameAndPassword(char const* username, char const* password, bool passwordIsMD5) 
{
      resetUsernameAndPassword();
      assignUsernameAndPassword(username, password, passwordIsMD5);
}
 
void CAuthenticator::reclaimDigestResponse(char const* responseStr) 
{
    if (responseStr != NULL)
    {
          delete[](char*)responseStr;
        responseStr = NULL;
    }
}
 
void CAuthenticator::resetRealmAndNonce() 
{
    if (fRealm != NULL)
    {
          delete[] fRealm; 
        fRealm = NULL;
    }
    if (fNonce != NULL)
    {
          delete[] fNonce; 
        fNonce = NULL;
    }
}
 
void CAuthenticator::resetUsernameAndPassword() 
{
    if (fUsername != NULL)
    {
         delete[] fUsername; 
        fUsername = NULL;
    }
    if (fPassword != NULL)
    {
          delete[] fPassword; 
        fPassword = NULL;
    }
      fPasswordIsMD5 = false;
}
 
void CAuthenticator::assignRealmAndNonce(char const* realm, char const* nonce) 
{
    if (realm != NULL && strlen(realm) > 0)
    {
        fRealm = strMyDup(realm);
    }
    if (nonce != NULL && strlen(nonce) > 0)
    {
        fNonce = strMyDup(nonce);
    }
}
 
void CAuthenticator::assignUsernameAndPassword(char const* username, char const* password, bool passwordIsMD5) 
{
    if (username == NULL) 
        username = "";
    if (password == NULL) 
        password = "";
 
    fUsername = strMyDup(username);
    fPassword = strMyDup(password);
    fPasswordIsMD5 = passwordIsMD5;
}
 
char const* CAuthenticator::computeDigestResponse(char* cmd, char *url)  
{
    // The "response" field is computed as:
    //    md5(md5(<username>:<realm>:<password>):<nonce>:md5(<cmd>:<url>))
    // or, if "fPasswordIsMD5" is True:
    //    md5(<password>:<nonce>:md5(<cmd>:<url>))
    char *pAlgorithm  = NULL;
    char *pUsername   = NULL;
    char *pRealm      = NULL;
    char *pPasswd     = NULL;
    char *pNonce      = NULL;
    char *pNonceCount = NULL;
    char *pCNonce     = NULL;
    char *pQop        = NULL;
    char *pMethod     = NULL;
    char *pUri        = NULL;
    char sNullAlg[]   = "";  //"MD5";
    char sNullQop[]   = "";  //"auth";    
    //char SessionKey[MD5_SESSION_KEY_LEN+1] = {0};
    char Response[MD5_RESPONSE_LEN] = {0};
    
    char SessionKey[33] = {0};
    if (fPasswordIsMD5) 
    {
        strncpy(SessionKey, fPassword, 32);
        SessionKey[32] = '\0'; // just in case
    } 
    else 
    {
        pUsername  = unquote(fUsername);
        pRealm       = unquote(fRealm);
        pPasswd    = unquote(fPassword);
        pNonce       = unquote(fNonce);
        pCNonce    = unquote(sNullAlg);
        pAlgorithm = unquote(sNullAlg);
        //calculate session key
        DigestCalcHA1(pAlgorithm, pUsername, pRealm, pPasswd, pNonce, pCNonce, SessionKey);
    }
    pQop = unquote(sNullQop);
    pMethod = cmd;
    pUri    = url;
 
    DigestCalcResponse(SessionKey, pNonce, pNonceCount, pCNonce, pQop, pMethod, pUri, (char*)"", Response);
 
    char const* result = strMyDup(unquote(Response));
    return result;
}
 
 
/**************************************
 *
 * CRtspHdr
 *
 **************************************/
 
CRtspHdr::CRtspHdr(const CString& strKey) : m_strKey(strKey)
{
    //Empty
}
 
CRtspHdr::CRtspHdr(const CString& strKey, const CString& strVal) : m_strKey(strKey), m_strVal(strVal)
{
    //Empty
}
 
const CString& CRtspHdr::GetKey(void) const
{
    return m_strKey;
}
 
const CString& CRtspHdr::GetVal(void) const
{
    return m_strVal;
}
 
void CRtspHdr::SetVal(const CString& strVal)
{
    m_strVal = strVal;
}
 
/**************************************
 *
 * CRtspMsg
 *
 **************************************/
CRtspMsg::CRtspMsg(void) : m_nRtspVer(0), m_nSeq(0), m_nBufLen(0), m_pbuf(NULL)
{
    //Empty
}
 
CRtspMsg::CRtspMsg(const CRtspMsg& other)
{
    CRtspHdrList::ConstIterator itr(other.m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        m_listHdrs.InsertTail(new CRtspHdr(*pHdr) );
        itr++;
    }
    
    m_nBufLen = other.m_nBufLen;
    m_pbuf = NULL;
    if (m_nBufLen)
    {
        m_pbuf = new Byte[m_nBufLen+1];
        memcpy(m_pbuf, other.m_pbuf, m_nBufLen);
    }
}
 
CRtspMsg::~CRtspMsg(void)
{
    while (!m_listHdrs.IsEmpty() )
    {
        CRtspHdr* pHdr = m_listHdrs.RemoveHead();
        delete pHdr;
    }
    
    delete[] m_pbuf; m_pbuf = NULL;
}
 
const CRtspMsg& CRtspMsg::operator=(const CRtspMsg& other)
{
    m_nRtspVer = other.m_nRtspVer;
    m_nSeq = other.m_nSeq;
    
    while (!m_listHdrs.IsEmpty() )
    {
        CRtspHdr* pHdr = m_listHdrs.RemoveHead();
        delete pHdr;
    }
    CRtspHdrList::ConstIterator itr(other.m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        m_listHdrs.InsertTail(new CRtspHdr(*pHdr) );
        itr++;
    }
    
    m_nBufLen = other.m_nBufLen;
    delete[] m_pbuf; m_pbuf = NULL;
    if (m_nBufLen)
    {
        m_pbuf = new Byte[m_nBufLen+1];
        memcpy(m_pbuf, other.m_pbuf, m_nBufLen);
    }
    
    return *this;
}
 
CRtspMsg::operator CPByte(void) const
{
    return m_pbuf;
}
 
Byte CRtspMsg::operator[](UINT32 nPos) const
{
    return GetAt(nPos);
}
 
RtspMsgType CRtspMsg::GetType(void) const
{
    return RTSP_TYPE_NONE;
}
 
size_t CRtspMsg::GetHdrLen(void) const
{
    size_t nLen = 0;
    CRtspHdrList::ConstIterator itr(m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        nLen += (pHdr->GetKey().GetLength() + 2 + pHdr->GetVal().GetLength() + 2);
        itr++;
    }
    
    return nLen;
}
 
 
 
size_t CRtspMsg::GetBufLen(void) const
{
    return m_nBufLen;
}
 
Byte CRtspMsg::GetAt(UINT32 nPos) const
{
    return m_pbuf[nPos];
}
 
void CRtspMsg::SetAt(UINT32 nPos, Byte by)
{
    m_pbuf[nPos] = by;
}
 
void CRtspMsg::GetRtspVer(UINT32* puMajor, UINT32* puMinor) const
{
    assert(puMajor && puMinor);
    
    *puMajor = HIWORD(m_nRtspVer);
    *puMinor = LOWORD(m_nRtspVer);
}
 
void CRtspMsg::SetRtspVer(UINT32 uMajor, UINT32 uMinor)
{
    assert(uMajor < 10 && uMinor < 10);
    
    m_nRtspVer = MAKEDWORD(uMajor,uMinor);
}
 
size_t CRtspMsg::GetHdrCount(void) const
{
    return m_listHdrs.GetCount();
}
 
CString CRtspMsg::GetHdr(const CString& strKey) const
{
    CString strVal;
    
    CRtspHdrList::ConstIterator itr(m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        if (0 == strcasecmp(strKey, pHdr->GetKey() ) )
        {
            strVal = pHdr->GetVal();
            break;
        }
        itr++;
    }
    
    return strVal;
}
 
CRtspHdr* CRtspMsg::GetHdr(UINT32 nIndex) const
{
    CRtspHdrList::ConstIterator itr(m_listHdrs.Begin() );
    for (UINT32 n = 0; n < nIndex; n++)
    {
        itr++;
    }
    return *itr;
}
 
void CRtspMsg::SetHdr(const CString& strKey, const CString& strVal)
{
    CRtspHdrList::Iterator itr(m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        if (0 == strcasecmp(strKey, pHdr->GetKey() ) )
        {
            pHdr->SetVal(strVal);
            return;
        }
        itr++;
    }
    m_listHdrs.InsertTail(new CRtspHdr(strKey, strVal) );
}
 
void CRtspMsg::SetHdr(const CRtspHdr& hdrNew)
{
    CRtspHdrList::Iterator itr(m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        if (hdrNew.GetKey() == pHdr->GetKey() )
        {
            pHdr->SetVal(hdrNew.GetVal() );
            return;
        }
        itr++;
    }
    m_listHdrs.InsertTail(new CRtspHdr(hdrNew) );
}
 
PByte CRtspMsg::GetBuf(void) const
{
    return m_pbuf;
}
 
void CRtspMsg::SetBuf(CPByte buf, size_t nLen)
{
    delete[] m_pbuf; m_pbuf = NULL;
    m_nBufLen = nLen;
    if (m_nBufLen)
    {
        m_pbuf = new Byte[m_nBufLen+1];
        memcpy(m_pbuf, buf, m_nBufLen);
    }
}
 
/**************************************
 *
 * CRtspRequestMsg
 *
 **************************************/
 
// These correspond with enum RtspVerb and must be sorted
static CPCHAR s_pVerbs[] = {
    "-NONE-",
    "ANNOUNCE",
    "DESCRIBE",
    "GET_PARAMETER",
    "OPTIONS",
    "PAUSE",
    "PLAY",
    "RECORD",
    "REDIRECT",
    "SETUP",
    "SET_PARAMETER",
    "TEARDOWN",
    NULL
};
static const UINT32 s_nVerbs = sizeof(s_pVerbs)/sizeof(CPCHAR) - 1;
 
CRtspRequestMsg::CRtspRequestMsg(void) : CRtspMsg()
{
    m_verb = VERB_NONE;
    
    //Empty
}
 
CRtspRequestMsg::CRtspRequestMsg(const CRtspRequestMsg& other) : CRtspMsg(other)
{
    m_verb = other.m_verb;
    m_strUrl = other.m_strUrl;
    
    UrlParam = other.UrlParam;
}
 
CRtspRequestMsg::~CRtspRequestMsg( void )
{
    //Empty
}
 
const CRtspRequestMsg& CRtspRequestMsg::operator=(const CRtspRequestMsg& other)
{
    m_verb = other.m_verb;
    UrlParam = other.UrlParam;
    m_strUrl = other.m_strUrl;
    
    m_nRtspVer = other.m_nRtspVer;
    m_nSeq = other.m_nSeq;
    
    while (!m_listHdrs.IsEmpty() )
    {
        CRtspHdr* pHdr = m_listHdrs.RemoveHead();
        delete pHdr;
    }
    CRtspHdrList::ConstIterator itr(other.m_listHdrs.Begin() );
    while (itr)
    {
        CRtspHdr* pHdr = *itr;
        m_listHdrs.InsertTail(new CRtspHdr(*pHdr) );
        itr++;
    }
    
    m_nBufLen = other.m_nBufLen;
    delete[] m_pbuf; m_pbuf = NULL;
    if (m_nBufLen)
    {
        m_pbuf = new Byte[m_nBufLen+1];
        memcpy(m_pbuf, other.m_pbuf, m_nBufLen);
    }
    
    return *this;
}
 
RtspMsgType CRtspRequestMsg::GetType( void ) const
{
    return RTSP_TYPE_REQUEST;
}
 
RtspVerb CRtspRequestMsg::GetVerb( void ) const
{
    return m_verb;
}
 
CPCHAR CRtspRequestMsg::GetVerbStr( void ) const
{
    return s_pVerbs[m_verb];
}
 
void CRtspRequestMsg::SetVerb( RtspVerb verb )
{
    assert( verb > VERB_NONE && verb < VERB_LAST );
    
    m_verb = verb;
}
void CRtspRequestMsg::GetFileName(char filename[64])
{
    char* url = (char*)(CPCHAR)m_strUrl;
    char* temp;
 
    filename[0] = 0;
 
    temp = rindex(url,'/');
    if(temp == NULL)
    {
        return;
    }
    if(strlen(temp) <= 1)
    {
        *temp = 0;
        temp = rindex(url,'/');
        if(temp == NULL)
        {
            return;
        }
    }
    temp ++;
    snprintf(filename,64,"%s",temp);
}
 
void CRtspRequestMsg::SetVerb( CPCHAR szVerb )
{
    m_verb = VERB_NONE;
    
    int hi = s_nVerbs;
    int lo = -1;
    int mid;
    while( hi-lo > 1 )
    {
        mid = (hi+lo)/2;
        if( strcmp( szVerb, s_pVerbs[mid] ) <= 0 )
            hi = mid;
        else
            lo = mid;
    }
    if( 0 == strcmp( szVerb, s_pVerbs[hi] ) )
    {
        m_verb = (RtspVerb)hi;
    }
    
    //assert(VERB_NONE == m_verb);
}
 
CPCHAR CRtspRequestMsg::GetUrl( void ) const
{
    return (CPCHAR)m_strUrl;
}
 
void CRtspRequestMsg::SetUrl( const CString& strUrl )
{
    m_strUrl = strUrl;
}
 
void CRtspRequestMsg::ParserUri(void)
{
    //For example: URL=rtsp://192.168.1.245:554/DevAor=100@192.168.1.245/Type=1/BeginTime=2011-3-16.13:06:12/EndTime=2011-3-16.13:06:23/StreamID=1
    //URL=rtsp://172.30.12.93:8554/DevAor=44011188001310000001/StreamID=1
    char* p = (char*)(CPCHAR)m_strUrl;
    if ( ('\0' == *p) || (strcmp(p, "rtsp://") <= 0) )
    {
        printf("%s: url=<%s>, no \"rtsp://\" protocol header!\n", __FUNCTION__, p);
        return;    
    }
    
    p += 7;  //7 = strlen("rtsp://");
    while (*p != '\0')
    {
        if (*p == '/')
        {
            p++;
            
            char buf1[64+1] = {0}, buf2[64+1] = {0}, buf3[64+1] = {0}, buf4[64+1] = {0}, buf5[64+1] = {0}, buf6[64+1] = {0}, buf7[64+1] = {0}, buf8[64+1] = {0};
            sscanf(p, "%64[^/]/%64[^/]/%64[^/]/%64[^/]/%64[^/]/%64[^/]/%64[^/]/%64[^/]/", buf1, buf2, buf3, buf4, buf5, buf6, buf7, buf8);
            if (strlen(buf1) > 0)  //The URL is right if 4th parameter is no empty.
            {
                UrlParam.LineTo(buf1);
                UrlParam.LineTo(buf2);
                UrlParam.LineTo(buf3);
                UrlParam.LineTo(buf4);
                UrlParam.LineTo(buf5);
                UrlParam.LineTo(buf6);
                UrlParam.LineTo(buf7);
                UrlParam.LineTo(buf8);
            }
            else
            {
                printf("%s: url=<%s>, some errors!\n", __FUNCTION__, (char*)(CPCHAR)m_strUrl);
            }
            
            break;
        }
        p++;
    }
}
 
//Transfer protocol
SOCKETTYPE_E CRtspRequestMsg::ParserClientStreamTransType(void)
{
    CString Transport = GetHdr("Transport");
    char* p = strstr( (char *)(CPCHAR)Transport, "RTP/AVP/TCP");
    if(p != NULL)
    {
        return SOCKETTYPE_TCPSERVER;
    }
    //UDP
    return SOCKETTYPE_UDP;
}
 
bool CRtspRequestMsg::ParserClientAddr(char * ClientAddr)
{
    if(ClientAddr == NULL)
    {
        return false;
    }
    CString Transport = GetHdr("Transport");
 
    char* p = strstr( (char *)(CPCHAR)Transport, "destination=");
    if(p == NULL)
    {
        return false;
    }
    p += strlen("destination=");
    char * temp = strchr(p,';');
    if(temp == NULL)
    {
        return false;
    }
    if(temp - p < 7 || temp - p > 15)//"1.1.1.1 255.255.255.255"
    {
        return false;
    }
    memcpy(ClientAddr,p,temp - p);
    ClientAddr[temp - p] = 0;
 
    return true;
}
 
UINT16 CRtspRequestMsg::ParserClientPort(void)
{
    CString Transport = GetHdr("Transport");
    UINT16 port = 0;
    
    char* p = strstr( (char*)(CPCHAR)Transport, "client_port");
    if (p != NULL)
    {
        p += 11;  //11 = strlen(client_port=)
    
        while ( (*p != '\0') && ( (*p == ' ') || (*p == '=') ) ) p++;
        
        port = (*p != '\0') ? atoi(p) : 0;
    }
    
    return port;
}
 
bool CRtspRequestMsg::ParserClientMulticast(void)
{
    CString Transport = GetHdr("Transport");
    char* p = strstr( (char *)(CPCHAR)Transport, "multicast");
    if(p != NULL)
    {
        return true;
    }
 
    return false;
}
 
UINT16 CRtspRequestMsg::ParserClientMulticastPort(void)
{
    CString Transport = GetHdr("Transport");
    UINT16 port = 0;
    
    char* p = strstr( (char*)(CPCHAR)Transport, "port");
    if (p != NULL)
    {
        p += 11;  //11 = strlen(client_port=)
    
        while ( (*p != '\0') && ( (*p == ' ') || (*p == '=') ) ) p++;
        
        port = (*p != '\0') ? atoi(p) : 0;
    }
    
    return port;
}
 
/**************************************
 *
 * CRtspResponseMsg
 *
 **************************************/
 
struct StatusMapEntry {UINT32 nCode; CPCHAR szName;};
 
// These must be sorted
static StatusMapEntry s_mapStatus[] =
{
    { 100, "Continue" },
    
    { 200, "OK" },
    { 201, "Created" },
    { 250, "Low on Storage Space" },
    
    { 300, "Multiple Choices" },
    { 301, "Moved Permanently" },
    { 302, "Moved Temporarily" },
    { 303, "See Other" },
    { 304, "Not Modified" },
    { 305, "Use Proxy" },
    
    { 400, "Bad Request" },
    { 401, "Unauthorized" },
    { 402, "Payment Required" },
    { 403, "Forbidden" },
    { 404, "Not Found" },
    { 405, "Method Not Allowed" },
    { 406, "Not Acceptable" },
    { 407, "Proxy Authentication Required" },
    { 408, "Request Time-out" },
    { 410, "Gone" },
    { 411, "Length Required" },
    { 412, "Precondition Failed" },
    { 413, "Request Entity Too Large" },
    { 414, "Request-URI Too Large" },
    { 415, "Unsupported Media Type" },
    { 451, "Parameter Not Understood" },
    { 452, "Conference Not Found" },
    { 453, "Not Enough Bandwidth" },
    { 454, "Session Not Found" },
    { 455, "Method Not Valid in This State" },
    { 456, "Header Field Not Valid for Resource" },
    { 457, "Invalid Range" },
    { 458, "Parameter Is Read-Only" },
    { 459, "Aggregate operation not allowed" },
    { 460, "Only aggregate operation allowed" },
    { 461, "Unsupported transport" },
    { 462, "Destination unreachable" },
    
    { 500, "Internal Server Error" },
    { 501, "Not Implemented" },
    { 502, "Bad Gateway" },
    { 503, "Service Unavailable" },
    { 504, "Gateway Time-out" },
    { 505, "RTSP Version not supported" },
    { 551, "Option not supported" },
    {   0, NULL }
};
static const UINT32 s_nStatusEntries = sizeof(s_mapStatus)/sizeof(StatusMapEntry) - 1;
 
CRtspResponseMsg::CRtspResponseMsg(void) : CRtspMsg(), m_nCode(0)
{
    //Empty
}
 
CRtspResponseMsg::CRtspResponseMsg(const CRtspResponseMsg& other) : CRtspMsg(other)
{
    m_nCode = other.m_nCode;
    m_strStatusMsg = other.m_strStatusMsg;
}
 
CRtspResponseMsg::~CRtspResponseMsg( void )
{
    //Empty
}
 
const CRtspResponseMsg& CRtspResponseMsg::operator=(const CRtspResponseMsg& other)
{
    m_nCode = other.m_nCode;
    m_strStatusMsg = other.m_strStatusMsg;
    
    return *this;
}
 
RtspMsgType CRtspResponseMsg::GetType(void) const
{
    return RTSP_TYPE_RESPONSE;
}
 
UINT32 CRtspResponseMsg::GetStatusCode(void) const
{
    return m_nCode;
}
 
const CString& CRtspResponseMsg::GetStatusMsg(void) const
{
    return m_strStatusMsg;
}
 
void CRtspResponseMsg::SetStatus( UINT32 nCode, CPCHAR szMsg /* = NULL */ )
{
    assert( nCode >= 100 && nCode <= 999 );
    
    m_nCode = nCode;
    if( !szMsg )
    {
        szMsg = "Unknown";
        
        int hi = s_nStatusEntries;
        int lo = -1;
        int mid;
        while( hi-lo > 1 )
        {
            mid = (hi+lo)/2;
            if( nCode <= s_mapStatus[mid].nCode )
                hi = mid;
            else
                lo = mid;
        }
        if( nCode == s_mapStatus[hi].nCode )
        {
            szMsg = s_mapStatus[hi].szName;
        }
    }
    m_strStatusMsg = szMsg;
}