File: SipMessage.cxx

package info (click to toggle)
resiprocate 1%3A1.11.0~beta1-3%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 37,944 kB
  • sloc: cpp: 206,325; xml: 12,515; sh: 12,418; ansic: 6,973; makefile: 2,315; php: 1,150; python: 355; sql: 142; objc: 91; perl: 21; csh: 5
file content (1853 lines) | stat: -rw-r--r-- 57,295 bytes parent folder | download | duplicates (2)
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "resip/stack/Contents.hxx"
#include "resip/stack/Embedded.hxx"
#include "resip/stack/OctetContents.hxx"
#include "resip/stack/HeaderFieldValueList.hxx"
#include "resip/stack/SipMessage.hxx"
#include "resip/stack/ExtensionHeader.hxx"
#include "rutil/Coders.hxx"
#include "rutil/CountStream.hxx"
#include "rutil/Logger.hxx"
#include "rutil/MD5Stream.hxx"
#include "rutil/compat.hxx"
#include "rutil/vmd5.hxx"
#include "rutil/Coders.hxx"
#include "rutil/Random.hxx"
#include "rutil/ParseBuffer.hxx"
#include "resip/stack/MsgHeaderScanner.hxx"
//#include "rutil/WinLeakCheck.hxx"  // not compatible with placement new used below

using namespace resip;
using namespace std;

#define RESIPROCATE_SUBSYSTEM Subsystem::SIP

bool SipMessage::checkContentLength=true;

SipMessage::SipMessage(const Tuple *receivedTransportTuple)
   : mIsDecorated(false),
     mIsBadAck200(false),     
     mIsExternal(receivedTransportTuple != 0),  // may be modified later by setFromTU or setFromExternal
     mHeaders(StlPoolAllocator<HeaderFieldValueList*, PoolBase >(&mPool)),
#ifndef __SUNPRO_CC
     mUnknownHeaders(StlPoolAllocator<std::pair<Data, HeaderFieldValueList*>, PoolBase >(&mPool)),
#else
     mUnknownHeaders(),
#endif
     mRequest(false),
     mResponse(false),
     mInvalid(false),
     mCreatedTime(Timer::getTimeMicroSec()),
     mTlsDomain(Data::Empty)
{
   if(receivedTransportTuple)
   {
       mReceivedTransportTuple = *receivedTransportTuple;
   }
   // !bwc! TODO make this tunable
   mHeaders.reserve(16);
   clear();
}

SipMessage::SipMessage(const SipMessage& from)
   : mHeaders(StlPoolAllocator<HeaderFieldValueList*, PoolBase >(&mPool)),
#ifndef __SUNPRO_CC
     mUnknownHeaders(StlPoolAllocator<std::pair<Data, HeaderFieldValueList*>, PoolBase >(&mPool)),
#else
     mUnknownHeaders(),
#endif
     mCreatedTime(Timer::getTimeMicroSec())
{
   init(from);
}

Message*
SipMessage::clone() const
{
   return new SipMessage(*this);
}

SipMessage& 
SipMessage::operator=(const SipMessage& rhs)
{
   if (this != &rhs)
   {
      freeMem();
      init(rhs);
   }
   return *this;
}

SipMessage::~SipMessage()
{
//#define DINKYPOOL_PROFILING
#ifdef DINKYPOOL_PROFILING
   if (mPool.getHeapBytes() > 0)
   {
       InfoLog(<< "SipMessage mPool filled up and used " << mPool.getHeapBytes() << " bytes on the heap, consider increasing the mPool size (sizeof SipMessage is " << sizeof(SipMessage) << " bytes): msg="
           << std::endl << *this);
   }
   else
   {
       InfoLog(<< "SipMessage mPool used " << mPool.getPoolBytes() << " bytes of a total " << mPool.getPoolSizeBytes() << " bytes (sizeof SipMessage is " << sizeof(SipMessage) << " bytes): msg="
           << std::endl << *this);
   }
#endif
   freeMem();
}

void
SipMessage::clear(bool leaveResponseStuff)
{
   if(!leaveResponseStuff)
   {
      memset(mHeaderIndices,0,sizeof(mHeaderIndices));
      clearHeaders();
      
      // !bwc! The "invalid" 0 index.
      mHeaders.push_back(getEmptyHfvl());
      mBufferList.clear();
   }

   mUnknownHeaders.clear();

   mStartLine = 0;
   mContents = 0;
   mContentsHfv.clear();
   mForceTarget = 0;
   mReason=0;
   mOutboundDecorators.clear();
}

void
SipMessage::init(const SipMessage& rhs)
{
   clear();
   mIsDecorated = rhs.mIsDecorated;
   mIsBadAck200 = rhs.mIsBadAck200;
   mIsExternal = rhs.mIsExternal;
   mReceivedTransportTuple = rhs.mReceivedTransportTuple;
   mSource = rhs.mSource;
   mDestination = rhs.mDestination;
   mRFC2543TransactionId = rhs.mRFC2543TransactionId;
   mRequest = rhs.mRequest;
   mResponse = rhs.mResponse;
   mInvalid = rhs.mInvalid;
   if(!rhs.mReason)
   {
      mReason=0;
   }
   else
   {
      mReason = new Data(*rhs.mReason);
   }
   mTlsDomain = rhs.mTlsDomain;

   memcpy(&mHeaderIndices,&rhs.mHeaderIndices,sizeof(mHeaderIndices));

   // .bwc. Clear out the pesky invalid 0 index.
   clearHeaders();
   mHeaders.reserve(rhs.mHeaders.size());
   for (TypedHeaders::const_iterator i = rhs.mHeaders.begin();
        i != rhs.mHeaders.end(); i++)
   {
      mHeaders.push_back(getCopyHfvl(**i));
   }

   for (UnknownHeaders::const_iterator i = rhs.mUnknownHeaders.begin();
        i != rhs.mUnknownHeaders.end(); i++)
   {
      mUnknownHeaders.push_back(pair<Data, HeaderFieldValueList*>(
                                   i->first,
                                   getCopyHfvl(*i->second)));
   }
   if (rhs.mStartLine != 0)
   {
      mStartLine = rhs.mStartLine->clone(mStartLineMem);
   }
   if (rhs.mContents != 0)
   {
      mContents = rhs.mContents->clone();
   }
   else if (rhs.mContentsHfv.getBuffer() != 0)
   {
      mContentsHfv.copyWithPadding(rhs.mContentsHfv);
   }
   else
   {
      // no body to copy
   }
   if (rhs.mForceTarget != 0)
   {
      mForceTarget = new Uri(*rhs.mForceTarget);
   }

   if (rhs.mSecurityAttributes.get())
   {

      if (!mSecurityAttributes.get())
      {
         SecurityAttributes* attr = new SecurityAttributes();
         mSecurityAttributes.reset(attr);
      }

      if (rhs.mSecurityAttributes->isEncrypted())
      {
         mSecurityAttributes->setEncrypted();
      }
      mSecurityAttributes->setSignatureStatus(rhs.mSecurityAttributes->getSignatureStatus());
      mSecurityAttributes->setIdentity(rhs.mSecurityAttributes->getIdentity());
      mSecurityAttributes->setIdentityStrength(rhs.mSecurityAttributes->getIdentityStrength());
      mSecurityAttributes->setSigner(rhs.mSecurityAttributes->getSigner());
      mSecurityAttributes->setOutgoingEncryptionLevel(rhs.mSecurityAttributes->getOutgoingEncryptionLevel());
      mSecurityAttributes->setEncryptionPerformed(rhs.mSecurityAttributes->encryptionPerformed());
   }
   else
   {
      if (mSecurityAttributes.get())
      {
         mSecurityAttributes.reset();
      }
   }

   for(std::vector<MessageDecorator*>::const_iterator i=rhs.mOutboundDecorators.begin(); i!=rhs.mOutboundDecorators.end();++i)
   {
      mOutboundDecorators.push_back((*i)->clone());
   }
}

void
SipMessage::freeMem(bool leaveResponseStuff)
{
   for (UnknownHeaders::iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {
      freeHfvl(i->second);
   }

   if(!leaveResponseStuff)
   {
      clearHeaders();

      for (vector<char*>::iterator i = mBufferList.begin();
           i != mBufferList.end(); i++)
      {
         delete [] *i;
      }
   }

   if(mStartLine)
   {
      mStartLine->~StartLine();
      mStartLine=0;
   }

   delete mContents;
   delete mForceTarget;
   delete mReason;

   for(std::vector<MessageDecorator*>::iterator i=mOutboundDecorators.begin();
         i!=mOutboundDecorators.end();++i)
   {
      delete *i;
   }
}

void
SipMessage::clearHeaders()
{
    for (TypedHeaders::iterator i = mHeaders.begin(); i != mHeaders.end(); i++)
    {
        freeHfvl(*i);
    }
    mHeaders.clear();
}

SipMessage*
SipMessage::make(const Data& data, bool isExternal)
{
   Tuple fakeWireTuple;
   fakeWireTuple.setType(UDP);
   SipMessage* msg = new SipMessage(isExternal ? &fakeWireTuple : 0);

   size_t len = data.size();
   char *buffer = new char[len + 5];

   msg->addBuffer(buffer);
   memcpy(buffer,data.data(), len);
   MsgHeaderScanner msgHeaderScanner;
   msgHeaderScanner.prepareForMessage(msg);
   
   char *unprocessedCharPtr;
   if (msgHeaderScanner.scanChunk(buffer, (unsigned int)len, &unprocessedCharPtr) != MsgHeaderScanner::scrEnd)
   {
      DebugLog(<<"Scanner rejecting buffer as unparsable / fragmented.");
      DebugLog(<< data);
      delete msg; 
      msg = 0; 
      return 0;
   }

   // no pp error
   unsigned int used = (unsigned int)(unprocessedCharPtr - buffer);

   if (used < len)
   {
      // body is present .. add it up.
      // NB. The Sip Message uses an overlay (again)
      // for the body. It ALSO expects that the body
      // will be contiguous (of course).
      // it doesn't need a new buffer in UDP b/c there
      // will only be one datagram per buffer. (1:1 strict)

      msg->setBody(buffer+used,UInt32(len-used));
      //DebugLog(<<"added " << len-used << " byte body");
   }

   return msg;
}

void
SipMessage::parseAllHeaders()
{
   for (int i = 0; i < Headers::MAX_HEADERS; i++)
   {
      ParserContainerBase* pc=0;
      if(mHeaderIndices[i]>0)
      {
         HeaderFieldValueList* hfvl = ensureHeaders((Headers::Type)i);
         if(!Headers::isMulti((Headers::Type)i) && hfvl->parsedEmpty())
         {
            hfvl->push_back(0,0,false);
         }

         if(!(pc=hfvl->getParserContainer()))
         {
            pc = HeaderBase::getInstance((Headers::Type)i)->makeContainer(hfvl);
            hfvl->setParserContainer(pc);
         }
      
         pc->parseAll();
      }
   }

   for (UnknownHeaders::iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {
      ParserContainerBase* scs=0;
      if(!(scs=i->second->getParserContainer()))
      {
         scs=makeParserContainer<StringCategory>(i->second,Headers::RESIP_DO_NOT_USE);
         i->second->setParserContainer(scs);
      }
      
      scs->parseAll();
   }
   
   resip_assert(mStartLine);

   mStartLine->checkParsed();
   
   getContents();
}

const Data& 
SipMessage::getTransactionId() const
{
   if (empty(h_Vias))
   {
      InfoLog (<< "Bad message with no Vias: " << *this);
      throw Exception("No Via in message", __FILE__,__LINE__);
   }
   
   resip_assert(exists(h_Vias) && !header(h_Vias).empty());
   if( exists(h_Vias) && header(h_Vias).front().exists(p_branch) 
       && header(h_Vias).front().param(p_branch).hasMagicCookie() 
       && (!header(h_Vias).front().param(p_branch).getTransactionId().empty())
     )
   {
      return header(h_Vias).front().param(p_branch).getTransactionId();
   }
   else
   {
      if (mRFC2543TransactionId.empty())
      {
         compute2543TransactionHash();
      }
      return mRFC2543TransactionId;
   }
}

void
SipMessage::compute2543TransactionHash() const
{
   resip_assert (mRFC2543TransactionId.empty());
   
   /*  From rfc3261, 17.2.3
       The INVITE request matches a transaction if the Request-URI, To tag,
       From tag, Call-ID, CSeq, and top Via header field match those of the
       INVITE request which created the transaction.  In this case, the
       INVITE is a retransmission of the original one that created the
       transaction.  

       The ACK request matches a transaction if the Request-URI, From tag,
       Call-ID, CSeq number (not the method), and top Via header field match
       those of the INVITE request which created the transaction, and the To
       tag of the ACK matches the To tag of the response sent by the server
       transaction.  

       Matching is done based on the matching rules defined for each of those
       header fields.  Inclusion of the tag in the To header field in the ACK
       matching process helps disambiguate ACK for 2xx from ACK for other
       responses at a proxy, which may have forwarded both responses (This
       can occur in unusual conditions.  Specifically, when a proxy forked a
       request, and then crashes, the responses may be delivered to another
       proxy, which might end up forwarding multiple responses upstream).  An
       ACK request that matches an INVITE transaction matched by a previous
       ACK is considered a retransmission of that previous ACK.

       For all other request methods, a request is matched to a transaction
       if the Request-URI, To tag, From tag, Call-ID, CSeq (including the
       method), and top Via header field match those of the request that
       created the transaction.  Matching is done based on the matching
   */

   // If it is here and isn't a request, leave the transactionId empty, this
   // will cause the Transaction to send it statelessly

   if (isRequest())
   {
      MD5Stream strm;
      // See section 17.2.3 Matching Requests to Server Transactions in rfc 3261

//#define VONAGE_FIX
#ifndef VONAGE_FIX         
      strm << header(h_RequestLine).uri().scheme();
      strm << header(h_RequestLine).uri().user();
      strm << header(h_RequestLine).uri().host();
      strm << header(h_RequestLine).uri().port();
      strm << header(h_RequestLine).uri().password();
      strm << header(h_RequestLine).uri().commutativeParameterHash();
#endif
      if (!empty(h_Vias))
      {
         strm << header(h_Vias).front().protocolName();
         strm << header(h_Vias).front().protocolVersion();
         strm << header(h_Vias).front().transport();
         strm << header(h_Vias).front().sentHost();
         strm << header(h_Vias).front().sentPort();
         strm << header(h_Vias).front().commutativeParameterHash();
      }
         
      if (header(h_From).exists(p_tag))
      {
         strm << header(h_From).param(p_tag);
      }

      // Only include the totag for non-invite requests
      if (header(h_RequestLine).getMethod() != INVITE && 
          header(h_RequestLine).getMethod() != ACK && 
          header(h_RequestLine).getMethod() != CANCEL &&
          header(h_To).exists(p_tag))
      {
         strm << header(h_To).param(p_tag);
      }

      strm << header(h_CallID).value();

      if (header(h_RequestLine).getMethod() == ACK || 
          header(h_RequestLine).getMethod() == CANCEL)
      {
         strm << INVITE;
         strm << header(h_CSeq).sequence();
      }
      else
      {
         strm << header(h_CSeq).method();
         strm << header(h_CSeq).sequence();
      }
           
      mRFC2543TransactionId = strm.getHex();
   }
   else
   {
      InfoLog (<< "Trying to compute a transaction id on a 2543 response. Drop the response");
      DebugLog (<< *this);
      throw Exception("Drop invalid 2543 response", __FILE__, __LINE__);
   }
}

const Data&
SipMessage::getRFC2543TransactionId() const
{
   if(empty(h_Vias) ||
      !header(h_Vias).front().exists(p_branch) ||
      !header(h_Vias).front().param(p_branch).hasMagicCookie() ||
      header(h_Vias).front().param(p_branch).getTransactionId().empty())
   {
      if (mRFC2543TransactionId.empty())
      {
         compute2543TransactionHash();
      }
   }
   return mRFC2543TransactionId;
}


Data
SipMessage::getCanonicalIdentityString() const
{
   Data result;
   DataStream strm(result);
   
   // digest-string = addr-spec ":" addr-spec ":" callid ":" 1*DIGIT SP method ":"
   //             SIP-Date ":" [ addr-spec ] ":" message-body
  
   strm << header(h_From).uri();
   strm << Symbols::BAR;
   
   strm << header(h_To).uri();
   strm << Symbols::BAR;
   
   strm << header(h_CallId).value();
   strm << Symbols::BAR;
   
   header(h_CSeq).sequence(); // force parsed
   header(h_CSeq).encodeParsed( strm );
   strm << Symbols::BAR;
   
   // if there is no date, it will throw 
   if ( empty(h_Date) )
   {
      WarningLog( << "Computing Identity on message with no Date header" );
      // TODO FIX - should it have a throw here ???? Help ???
   }
   header(h_Date).dayOfMonth(); // force it to be parsed 
   header(h_Date).encodeParsed( strm );
   strm << Symbols::BAR;
   
   if ( !empty(h_Contacts) )
   { 
      if ( header(h_Contacts).front().isAllContacts() )
      {
         strm << Symbols::STAR;
      }
      else
      {
         strm << header(h_Contacts).front().uri();
      }
   }
   strm << Symbols::BAR;
   
   // bodies 
   if (mContents != 0)
   {
      mContents->encode(strm);
   }
   else if (mContentsHfv.getBuffer() != 0)
   {
      mContentsHfv.encode(strm);
   }

   strm.flush();

   DebugLog( << "Indentity Canonical String is: " << result );
   
   return result;
}


void
SipMessage::setRFC2543TransactionId(const Data& tid)
{
   mRFC2543TransactionId = tid;
}

resip::MethodTypes
SipMessage::method() const
{
   resip::MethodTypes res=UNKNOWN;
   try
   {
      if(isRequest())
      {
         res=header(h_RequestLine).getMethod();
      }
      else if(isResponse())
      {
         res=header(h_CSeq).method();
      }
      else
      {
         resip_assert(0);
      }
   }
   catch(resip::ParseException&)
   {
   }
   
   return res;
}

const Data&
SipMessage::methodStr() const
{
   if(method()!=UNKNOWN)
   {
      return getMethodName(method());
   }
   else
   {
      try
      {
         if(isRequest())
         {
            return header(h_RequestLine).unknownMethodName();
         }
         else if(isResponse())
         {
            return header(h_CSeq).unknownMethodName();
         }
         else
         {
            resip_assert(0);
         }
      }
      catch(resip::ParseException&)
      {
      }
   }
   return Data::Empty;
}

static const Data requestEB("SipReq:  ");
static const Data responseEB("SipResp: ");
static const Data tidEB(" tid=");
static const Data contactEB(" contact=");
static const Data cseqEB(" cseq=");
static const Data slashEB(" / ");
static const Data wireEB(" from(wire)");
static const Data ftuEB(" from(tu)");
static const Data tlsdEB(" tlsd=");
EncodeStream&
SipMessage::encodeBrief(EncodeStream& str) const
{
   if (isRequest()) 
   {
      str << requestEB;
      MethodTypes meth = header(h_RequestLine).getMethod();
      if (meth != UNKNOWN)
      {
         str << getMethodName(meth);
      }
      else
      {
         str << header(h_RequestLine).unknownMethodName();
      }
      
      str << Symbols::SPACE;
      str << header(h_RequestLine).uri().getAor();
   }
   else if (isResponse())
   {
      str << responseEB;
      str << header(h_StatusLine).responseCode();
   }
   if (!empty(h_Vias))
   {
      str << tidEB;
      try
      {
         str << getTransactionId();
      }
      catch(BaseException&)  // Could be SipMessage::Exception or ParseException
      {
         str << "BAD-VIA";
      }
   }
   else
   {
      str << " NO-VIAS ";
   }

   str << cseqEB;
   str << header(h_CSeq);

   try
   {
      if (!empty(h_Contacts))
      {
         str << contactEB;
         str << header(h_Contacts).front().uri().getAor();
      }
   }
   catch(resip::ParseException&)
   {
      str << " MALFORMED CONTACT ";
   }
   
   str << slashEB;
   str << header(h_CSeq).sequence();
   str << (mIsExternal ? wireEB : ftuEB);
   if (!mTlsDomain.empty())
   {
      str << tlsdEB << mTlsDomain;
   }
   
   return str;
}

bool
SipMessage::isClientTransaction() const
{
   resip_assert(mRequest || mResponse);
   return ((mIsExternal && mResponse) || (!mIsExternal && mRequest));
}

EncodeStream&
SipMessage::encode(EncodeStream& str) const
{
   return encode(str, false);
}

EncodeStream&
SipMessage::encodeSipFrag(EncodeStream& str) const
{
   return encode(str, true);
}

// dynamic_cast &str to DataStream* to avoid CountStream?

EncodeStream&
SipMessage::encode(EncodeStream& str, bool isSipFrag) const
{
   if (mStartLine != 0)
   {
      mStartLine->encode(str);
      str << "\r\n";
   }

   Data contents;
   if (mContents != 0)
   {
      oDataStream temp(contents);
      mContents->encode(temp);
   }
   else if (mContentsHfv.getBuffer() != 0)
   {
#if 0
      // !bwc! This causes an additional copy; sure would be nice to have a way
      // to get a data to take on a buffer with Data::Share _after_ construction
      contents.append(mContentsHfv.getBuffer(), mContentsHfv.getLength());
#else
      // .kw. Your wish is granted
      mContentsHfv.toShareData(contents);
#endif
   }

   for (UInt8 i = 0; i < Headers::MAX_HEADERS; i++)
   {
      if (i != Headers::ContentLength) // !dlb! hack...
      {
         if (mHeaderIndices[i] > 0)
         {
            mHeaders[mHeaderIndices[i]]->encode(i, str);
         }
      }
   }

   for (UnknownHeaders::const_iterator i = mUnknownHeaders.begin(); 
        i != mUnknownHeaders.end(); i++)
   {
      i->second->encode(i->first, str);
   }

   if(!isSipFrag || !contents.empty())
   {
      str << "Content-Length: " << contents.size() << "\r\n";
   }

   str << Symbols::CRLF;
   
   str << contents;
   return str;
}

EncodeStream&
SipMessage::encodeSingleHeader(Headers::Type type, EncodeStream& str) const
{
   if (mHeaderIndices[type] > 0)
   {
      mHeaders[mHeaderIndices[type]]->encode(type, str);
   }
   return str;
}

EncodeStream& 
SipMessage::encodeEmbedded(EncodeStream& str) const
{
   bool first = true;
   for (UInt8 i = 0; i < Headers::MAX_HEADERS; i++)
   {
      if (i != Headers::ContentLength)
      {
         if (mHeaderIndices[i] > 0)
         {
            if (first)
            {
               str << Symbols::QUESTION;
               first = false;
            }
            else
            {
               str << Symbols::AMPERSAND;
            }
            mHeaders[mHeaderIndices[i]]->encodeEmbedded(Headers::getHeaderName(i), str);
         }
      }
   }

   for (UnknownHeaders::const_iterator i = mUnknownHeaders.begin(); 
        i != mUnknownHeaders.end(); i++)
   {
      if (first)
      {
         str << Symbols::QUESTION;
         first = false;
      }
      else
      {
         str << Symbols::AMPERSAND;
      }
      i->second->encodeEmbedded(i->first, str);
   }

   if (mContents != 0 || mContentsHfv.getBuffer() != 0)
   {
      if (first)
      {
         str << Symbols::QUESTION;
      }
      else
      {
         str << Symbols::AMPERSAND;
      }
      str << "body=";
      Data contents;
      // !dlb! encode escaped for characters
      // .kw. what does that mean? what needs to be escaped?
      if(mContents != 0)
      {
         DataStream s(contents);
         mContents->encode(s);
      }
      else
      {
         // .kw. Early code did:
         // DataStream s(contents);
         // mContentsHfv->encode(str);
         // str << Embedded::encode(contents);
         // .kw. which I think is buggy b/c Hfv was written directly
         // to str and skipped the encode step via contents
         mContentsHfv.toShareData(contents);
      }
      str << Embedded::encode(contents);
   }
   return str;
}

void
SipMessage::addBuffer(char* buf)
{
   mBufferList.push_back(buf);
}

void 
SipMessage::setStartLine(const char* st, int len)
{
   if(len >= 4 && !strncasecmp(st,"SIP/",4))
   {
      // Response
      mStartLine = new (mStartLineMem) StatusLine(st, len);
      //!dcm! should invoke the statusline parser here once it does limited validation
      mResponse = true;
   }
   else
   {
      // Request
      mStartLine = new (mStartLineMem) RequestLine(st, len);
      //!dcm! should invoke the responseline parser here once it does limited validation
      mRequest = true;
   }


// .bwc. This stuff is so needlessly complicated. Much, much simpler, faster,
// and more robust code above.
//   ParseBuffer pb(st, len);
//   const char* start;
//   start = pb.skipWhitespace();
//   pb.skipNonWhitespace();
//   MethodTypes method = getMethodType(start, pb.position() - start);
//   if (method == UNKNOWN) //probably a status line
//   {
//      start = pb.skipChar(Symbols::SPACE[0]);
//      pb.skipNonWhitespace();
//      if ((pb.position() - start) == 3)
//      {
//         mStartLine = new (mStartLineMem) StatusLine(st, len ,Headers::NONE);
//         //!dcm! should invoke the statusline parser here once it does limited validation
//         mResponse = true;
//      }
//   }
//   if (!mResponse)
//   {
//      mStartLine = new (mStartLineMem) RequestLine(st, len, Headers::NONE);
//      //!dcm! should invoke the responseline parser here once it does limited validation
//      mRequest = true;
//   }
}

void 
SipMessage::setBody(const char* start, UInt32 len)
{
   if(checkContentLength)
   {
      if(exists(h_ContentLength))
      {
         try
         {
            const_header(h_ContentLength).checkParsed();
         }
         catch(resip::ParseException& e)
         {
            if(!mReason)
            {
               mReason=new Data;
            }
            
            if(mInvalid)
            {
               mReason->append(",",1);
            }

            mInvalid=true; 
            mReason->append("Malformed Content-Length",24);
            InfoLog(<< "Malformed Content-Length. Ignoring. " << e);
            header(h_ContentLength).value()=len;
         }
         
         UInt32 contentLength=const_header(h_ContentLength).value();
         
         if(len > contentLength)
         {
            InfoLog(<< (len-contentLength) << " extra bytes after body. Ignoring these bytes.");
         }
         else if(len < contentLength)
         {
            InfoLog(<< "Content Length (" << contentLength << ") is "
                    << (contentLength-len) << " bytes larger than body (" << len << ")!"
                    << " (We are supposed to 400 this) ");

            if(!mReason)
            {
               mReason=new Data;
            }

            if(mInvalid)
            {
               mReason->append(",",1);
            }

            mInvalid=true; 
            mReason->append("Bad Content-Length (larger than datagram)",41);
            header(h_ContentLength).value()=len;
            contentLength=len;
                     
         }
         
         mContentsHfv.init(start,contentLength, false);
      }
      else
      {
         InfoLog(<< "Message has a body, but no Content-Length header.");
         mContentsHfv.init(start,len, false);
      }
   }
   else
   {
      mContentsHfv.init(start,len, false);
   }
}

void
SipMessage::setRawBody(const HeaderFieldValue& body)
{
   setContents(0);
   mContentsHfv = body;
}


void
SipMessage::setContents(auto_ptr<Contents> contents)
{
   Contents* contentsP = contents.release();

   delete mContents;
   mContents = 0;
   mContentsHfv.clear();

   if (contentsP == 0)
   {
      // The semantics of setContents(0) are to delete message contents
      remove(h_ContentType);
      remove(h_ContentDisposition);
      remove(h_ContentTransferEncoding);
      remove(h_ContentLanguages);
      return;
   }

   mContents = contentsP;

   // copy contents headers into message
   if (mContents->exists(h_ContentDisposition))
   {
      header(h_ContentDisposition) = mContents->header(h_ContentDisposition);
   }
   if (mContents->exists(h_ContentTransferEncoding))
   {
      header(h_ContentTransferEncoding) = mContents->header(h_ContentTransferEncoding);
   }
   if (mContents->exists(h_ContentLanguages))
   {
      header(h_ContentLanguages) = mContents->header(h_ContentLanguages);
   }
   if (mContents->exists(h_ContentType))
   {
      header(h_ContentType) = mContents->header(h_ContentType);
      resip_assert( header(h_ContentType).type() == mContents->getType().type() );
      resip_assert( header(h_ContentType).subType() == mContents->getType().subType() );
   }
   else
   {
      header(h_ContentType) = mContents->getType();
   }
}

void 
SipMessage::setContents(const Contents* contents)
{ 
   if (contents)
   {
      setContents(auto_ptr<Contents>(contents->clone()));
   }
   else
   {
      setContents(auto_ptr<Contents>(0));
   }
}

Contents*
SipMessage::getContents() const
{
   if (mContents == 0 && mContentsHfv.getBuffer() != 0)
   {
      if (empty(h_ContentType) ||
            !const_header(h_ContentType).isWellFormed())
      {
         StackLog(<< "SipMessage::getContents: ContentType header does not exist - implies no contents");
         return 0;
      }
      DebugLog(<< "SipMessage::getContents: " 
               << const_header(h_ContentType).type()
               << "/"
               << const_header(h_ContentType).subType());

      if ( ContentsFactoryBase::getFactoryMap().find(const_header(h_ContentType)) == ContentsFactoryBase::getFactoryMap().end() )
      {
         InfoLog(<< "SipMessage::getContents: got content type ("
                 << const_header(h_ContentType).type()
                 << "/"
                 << const_header(h_ContentType).subType()
                 << ") that is not known, "
                 << "returning as opaque application/octet-stream");
         mContents = ContentsFactoryBase::getFactoryMap()[OctetContents::getStaticType()]->create(mContentsHfv, OctetContents::getStaticType());
      }
      else
      {
         mContents = ContentsFactoryBase::getFactoryMap()[const_header(h_ContentType)]->create(mContentsHfv, const_header(h_ContentType));
      }
      resip_assert( mContents );
      
      // copy contents headers into the contents
      if (!empty(h_ContentDisposition))
      {
         mContents->header(h_ContentDisposition) = const_header(h_ContentDisposition);
      }
      if (!empty(h_ContentTransferEncoding))
      {
         mContents->header(h_ContentTransferEncoding) = const_header(h_ContentTransferEncoding);
      }
      if (!empty(h_ContentLanguages))
      {
         mContents->header(h_ContentLanguages) = const_header(h_ContentLanguages);
      }
      if (!empty(h_ContentType))
      {
         mContents->header(h_ContentType) = const_header(h_ContentType);
      }
      // !dlb! Content-Transfer-Encoding?
   }
   return mContents;
}

auto_ptr<Contents>
SipMessage::releaseContents()
{
   Contents* c=getContents();
   // .bwc. auto_ptr owns the Contents. No other references allowed!
   auto_ptr<Contents> ret(c ? c->clone() : 0);
   setContents(std::auto_ptr<Contents>(0));

   if (ret.get() != 0 && !ret->isWellFormed())
   {
      ret.reset(0);
   }

   return ret;
}

// unknown header interface
const StringCategories& 
SipMessage::header(const ExtensionHeader& headerName) const
{
   for (UnknownHeaders::const_iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {      
      if (isEqualNoCase(i->first, headerName.getName()))
      {
         HeaderFieldValueList* hfvs = i->second;
         if (hfvs->getParserContainer() == 0)
         {
            SipMessage* nc_this(const_cast<SipMessage*>(this));
            hfvs->setParserContainer(nc_this->makeParserContainer<StringCategory>(hfvs, Headers::RESIP_DO_NOT_USE));
         }
         return *dynamic_cast<ParserContainer<StringCategory>*>(hfvs->getParserContainer());
      }
   }
   // missing extension header
   resip_assert(false);

   return *(StringCategories*)0;
}

StringCategories& 
SipMessage::header(const ExtensionHeader& headerName)
{
   for (UnknownHeaders::iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {
      if (isEqualNoCase(i->first, headerName.getName()))
      {
         HeaderFieldValueList* hfvs = i->second;
         if (hfvs->getParserContainer() == 0)
         {
            hfvs->setParserContainer(makeParserContainer<StringCategory>(hfvs, Headers::RESIP_DO_NOT_USE));
         }
         return *dynamic_cast<ParserContainer<StringCategory>*>(hfvs->getParserContainer());
      }
   }

   // create the list empty
   HeaderFieldValueList* hfvs = getEmptyHfvl();
   hfvs->setParserContainer(makeParserContainer<StringCategory>(hfvs, Headers::RESIP_DO_NOT_USE));
   mUnknownHeaders.push_back(make_pair(headerName.getName(), hfvs));
   return *dynamic_cast<ParserContainer<StringCategory>*>(hfvs->getParserContainer());
}

bool
SipMessage::exists(const ExtensionHeader& symbol) const
{
   for (UnknownHeaders::const_iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {
      if (isEqualNoCase(i->first, symbol.getName()))
      {
         return true;
      }
   }
   return false;
}

void
SipMessage::remove(const ExtensionHeader& headerName)
{
   for (UnknownHeaders::iterator i = mUnknownHeaders.begin();
        i != mUnknownHeaders.end(); i++)
   {
      if (isEqualNoCase(i->first, headerName.getName()))
      {
         freeHfvl(i->second);
         mUnknownHeaders.erase(i);
         return;
      }
   }
}

void
SipMessage::addHeader(Headers::Type header, const char* headerName, int headerLen, 
                      const char* start, int len)
{
   if (header != Headers::UNKNOWN)
   {
      resip_assert(header >= Headers::UNKNOWN && header < Headers::MAX_HEADERS);
      HeaderFieldValueList* hfvl=0;
      if (mHeaderIndices[header] == 0)
      {
         mHeaderIndices[header] = mHeaders.size();
         mHeaders.push_back(getEmptyHfvl());
         hfvl=mHeaders.back();
      }
      else
      {
         if(mHeaderIndices[header]<0)
         {
            // Adding to a previously removed header type; there is already an 
            // empty HeaderFieldValueList in mHeaders for this type, all we 
            // need to do is flip the sign to re-enable it.
            mHeaderIndices[header] *= -1;
         }
         hfvl=mHeaders[mHeaderIndices[header]];
      }

      if(Headers::isMulti(header))
      {
         if (len)
         {
            hfvl->push_back(start, len, false);
         }
      }
      else
      {
         if(hfvl->size()==1)
         {
            if(!mReason)
            {
               mReason=new Data;
            }
            
            if(mInvalid)
            {
               mReason->append(",",1);
            }
            mInvalid=true;
            mReason->append("Multiple values in single-value header ",39);
            (*mReason)+=Headers::getHeaderName(header);
            return;
         }
         hfvl->push_back(start ? start : Data::Empty.data(), len, false);
      }

   }
   else
   {
      resip_assert(headerLen >= 0);
      for (UnknownHeaders::iterator i = mUnknownHeaders.begin();
           i != mUnknownHeaders.end(); i++)
      {
         if (i->first.size() == (unsigned int)headerLen &&
             strncasecmp(i->first.data(), headerName, headerLen) == 0)
         {
            // add to end of list
            if (len)
            {
               i->second->push_back(start, len, false);
            }
            return;
         }
      }

      // didn't find it, add an entry
      HeaderFieldValueList *hfvs = getEmptyHfvl();
      if (len)
      {
         hfvs->push_back(start, len, false);
      }
      mUnknownHeaders.push_back(pair<Data, HeaderFieldValueList*>(Data(headerName, headerLen),
                                                                  hfvs));
   }
}

RequestLine& 
SipMessage::header(const RequestLineType& l)
{
   resip_assert (!isResponse());
   if (mStartLine == 0 )
   { 
      mStartLine = new (mStartLineMem) RequestLine;
      mRequest = true;
   }
   return *static_cast<RequestLine*>(mStartLine);
}

const RequestLine& 
SipMessage::header(const RequestLineType& l) const
{
   resip_assert (!isResponse());
   if (mStartLine == 0 )
   { 
      // request line missing
      resip_assert(false);
   }
   return *static_cast<RequestLine*>(mStartLine);
}

StatusLine& 
SipMessage::header(const StatusLineType& l)
{
   resip_assert (!isRequest());
   if (mStartLine == 0 )
   { 
      mStartLine = new (mStartLineMem) StatusLine;
      mResponse = true;
   }
   return *static_cast<StatusLine*>(mStartLine);
}

const StatusLine& 
SipMessage::header(const StatusLineType& l) const
{
   resip_assert (!isRequest());
   if (mStartLine == 0 )
   { 
      // status line missing
      resip_assert(false);
   }
   return *static_cast<StatusLine*>(mStartLine);
}

HeaderFieldValueList* 
SipMessage::ensureHeaders(Headers::Type type)
{
   HeaderFieldValueList* hfvl=0;
   if(mHeaderIndices[type]!=0)
   {
      if(mHeaderIndices[type]<0)
      {
         // Accessing a previously removed header type; there is already an 
         // empty HeaderFieldValueList in mHeaders for this type, all we 
         // need to do is flip the sign to re-enable it.
         mHeaderIndices[type] *= -1;
      }
      hfvl = mHeaders[mHeaderIndices[type]];
   }
   else
   {
      // create the list with a new component
      mHeaders.push_back(getEmptyHfvl());
      hfvl=mHeaders.back();
      mHeaderIndices[type]=mHeaders.size()-1;
   }

   return hfvl;
}

HeaderFieldValueList* 
SipMessage::ensureHeader(Headers::Type type)
{
   HeaderFieldValueList* hfvl=0;
   if(mHeaderIndices[type]!=0)
   {
      if(mHeaderIndices[type]<0)
      {
         // Accessing a previously removed header type; there is already an 
         // empty HeaderFieldValueList in mHeaders for this type, all we 
         // need to do is flip the sign to re-enable it.
         mHeaderIndices[type] *= -1;
         hfvl = mHeaders[mHeaderIndices[type]];
         hfvl->push_back(0,0,false);
      }
      hfvl = mHeaders[mHeaderIndices[type]];
   }
   else
   {
      // create the list with a new component
      mHeaders.push_back(getEmptyHfvl());
      hfvl=mHeaders.back();
      mHeaderIndices[type]=mHeaders.size()-1;
      mHeaders.back()->push_back(0,0,false);
   }

   return hfvl;
}

void
SipMessage::throwHeaderMissing(Headers::Type type) const
{
   // header missing
   // assert(false);
   InfoLog( << "Missing Header [" << Headers::getHeaderName(type) << "]");      
   DebugLog (<< *this);
   throw Exception("Missing header " + Headers::getHeaderName(type), __FILE__, __LINE__);
}

// type safe header accessors
bool    
SipMessage::exists(const HeaderBase& headerType) const 
{
   return mHeaderIndices[headerType.getTypeNum()] > 0;
};

bool
SipMessage::empty(const HeaderBase& headerType) const
{
   return (mHeaderIndices[headerType.getTypeNum()] <= 0) || mHeaders[mHeaderIndices[headerType.getTypeNum()]]->parsedEmpty();
}

void
SipMessage::remove(Headers::Type type)
{
   if(mHeaderIndices[type] > 0)
   {
      // .bwc. The entry in mHeaders still remains after we do this; we retain 
      // our index (as a negative number, indicating that this header should 
      // not be encoded), in case this header type needs to be used later.
      mHeaders[mHeaderIndices[type]]->clear();
      mHeaderIndices[type] *= -1;
   }
};

#ifndef PARTIAL_TEMPLATE_SPECIALIZATION

#undef defineHeader
#define defineHeader(_header, _name, _type, _rfc)                                                       \
const H_##_header::Type&                                                                                \
SipMessage::header(const H_##_header& headerType) const                                                 \
{                                                                                                       \
   HeaderFieldValueList* hfvs = ensureHeader(headerType.getTypeNum());                           \
   if (hfvs->getParserContainer() == 0)                                                                 \
   {                                                                                                    \
      SipMessage* nc_this(const_cast<SipMessage*>(this)); \
      hfvs->setParserContainer(nc_this->makeParserContainer<H_##_header::Type>(hfvs, headerType.getTypeNum()));  \
   }                                                                                                    \
   return static_cast<ParserContainer<H_##_header::Type>*>(hfvs->getParserContainer())->front();       \
}                                                                                                       \
                                                                                                        \
H_##_header::Type&                                                                                      \
SipMessage::header(const H_##_header& headerType)                                                       \
{                                                                                                       \
   HeaderFieldValueList* hfvs = ensureHeader(headerType.getTypeNum());                           \
   if (hfvs->getParserContainer() == 0)                                                                 \
   {                                                                                                    \
      hfvs->setParserContainer(makeParserContainer<H_##_header::Type>(hfvs, headerType.getTypeNum()));  \
   }                                                                                                    \
   return static_cast<ParserContainer<H_##_header::Type>*>(hfvs->getParserContainer())->front();       \
}

#undef defineMultiHeader
#define defineMultiHeader(_header, _name, _type, _rfc)                                          \
const H_##_header##s::Type&                                                                     \
SipMessage::header(const H_##_header##s& headerType) const                                      \
{                                                                                               \
   HeaderFieldValueList* hfvs = ensureHeaders(headerType.getTypeNum());                  \
   if (hfvs->getParserContainer() == 0)                                                         \
   {                                                                                            \
      SipMessage* nc_this(const_cast<SipMessage*>(this)); \
      hfvs->setParserContainer(nc_this->makeParserContainer<H_##_header##s::ContainedType>(hfvs, headerType.getTypeNum()));        \
   }                                                                                            \
   return *static_cast<H_##_header##s::Type*>(hfvs->getParserContainer());                     \
}                                                                                               \
                                                                                                \
H_##_header##s::Type&                                                                           \
SipMessage::header(const H_##_header##s& headerType)                                            \
{                                                                                               \
   HeaderFieldValueList* hfvs = ensureHeaders(headerType.getTypeNum());                  \
   if (hfvs->getParserContainer() == 0)                                                         \
   {                                                                                            \
      hfvs->setParserContainer(makeParserContainer<H_##_header##s::ContainedType>(hfvs, headerType.getTypeNum()));        \
   }                                                                                            \
   return *static_cast<H_##_header##s::Type*>(hfvs->getParserContainer());                     \
}

defineHeader(ContentDisposition, "Content-Disposition", Token, "RFC 3261");
defineHeader(ContentEncoding, "Content-Encoding", Token, "RFC 3261");
defineHeader(MIMEVersion, "Mime-Version", Token, "RFC 3261");
defineHeader(Priority, "Priority", Token, "RFC 3261");
defineHeader(Event, "Event", Token, "RFC 3265");
defineHeader(SubscriptionState, "Subscription-State", Token, "RFC 3265");
defineHeader(SIPETag, "SIP-ETag", Token, "RFC 3903");
defineHeader(SIPIfMatch, "SIP-If-Match", Token, "RFC 3903");
defineHeader(ContentId, "Content-ID", Token, "RFC 2045");
defineMultiHeader(AllowEvents, "Allow-Events", Token, "RFC 3265");
defineHeader(Identity, "Identity", StringCategory, "RFC 4474");
defineMultiHeader(AcceptEncoding, "Accept-Encoding", Token, "RFC 3261");
defineMultiHeader(AcceptLanguage, "Accept-Language", Token, "RFC 3261");
defineMultiHeader(Allow, "Allow", Token, "RFC 3261");
defineMultiHeader(ContentLanguage, "Content-Language", Token, "RFC 3261");
defineMultiHeader(ProxyRequire, "Proxy-Require", Token, "RFC 3261");
defineMultiHeader(Require, "Require", Token, "RFC 3261");
defineMultiHeader(Supported, "Supported", Token, "RFC 3261");
defineMultiHeader(Unsupported, "Unsupported", Token, "RFC 3261");
defineMultiHeader(SecurityClient, "Security-Client", Token, "RFC 3329");
defineMultiHeader(SecurityServer, "Security-Server", Token, "RFC 3329");
defineMultiHeader(SecurityVerify, "Security-Verify", Token, "RFC 3329");
defineMultiHeader(RequestDisposition, "Request-Disposition", Token, "RFC 3841");
defineMultiHeader(Reason, "Reason", Token, "RFC 3326");
defineMultiHeader(Privacy, "Privacy", PrivacyCategory, "RFC 3323");
defineMultiHeader(PMediaAuthorization, "P-Media-Authorization", Token, "RFC 3313");
defineHeader(ReferSub, "Refer-Sub", Token, "RFC 4488");
defineHeader(AnswerMode, "Answer-Mode", Token, "draft-ietf-answermode-01");
defineHeader(PrivAnswerMode, "Priv-Answer-Mode", Token, "draft-ietf-answermode-01");

defineMultiHeader(Accept, "Accept", Mime, "RFC 3261");
defineHeader(ContentType, "Content-Type", Mime, "RFC 3261");

defineMultiHeader(CallInfo, "Call-Info", GenericUri, "RFC 3261");
defineMultiHeader(AlertInfo, "Alert-Info", GenericUri, "RFC 3261");
defineMultiHeader(ErrorInfo, "Error-Info", GenericUri, "RFC 3261");
defineHeader(IdentityInfo, "Identity-Info", GenericUri, "RFC 4474");

defineMultiHeader(RecordRoute, "Record-Route", NameAddr, "RFC 3261");
defineMultiHeader(Route, "Route", NameAddr, "RFC 3261");
defineMultiHeader(Contact, "Contact", NameAddr, "RFC 3261");
defineHeader(From, "From", NameAddr, "RFC 3261");
defineHeader(To, "To", NameAddr, "RFC 3261");
defineHeader(ReplyTo, "Reply-To", NameAddr, "RFC 3261");
defineHeader(ReferTo, "Refer-To", NameAddr, "RFC 3515");
defineHeader(ReferredBy, "Referred-By", NameAddr, "RFC 3892");
defineMultiHeader(Path, "Path", NameAddr, "RFC 3327");
defineMultiHeader(AcceptContact, "Accept-Contact", NameAddr, "RFC 3841");
defineMultiHeader(RejectContact, "Reject-Contact", NameAddr, "RFC 3841");
defineMultiHeader(PAssertedIdentity, "P-Asserted-Identity", NameAddr, "RFC 3325");
defineMultiHeader(PPreferredIdentity, "P-Preferred-Identity", NameAddr, "RFC 3325");
defineHeader(PCalledPartyId, "P-Called-Party-ID", NameAddr, "RFC 3455");
defineMultiHeader(PAssociatedUri, "P-Associated-URI", NameAddr, "RFC 3455");
defineMultiHeader(ServiceRoute, "Service-Route", NameAddr, "RFC 3608");

defineHeader(ContentTransferEncoding, "Content-Transfer-Encoding", StringCategory, "RFC ?");
defineHeader(Organization, "Organization", StringCategory, "RFC 3261");
defineHeader(SecWebSocketKey, "Sec-WebSocket-Key", StringCategory, "RFC 6455");
defineHeader(SecWebSocketKey1, "Sec-WebSocket-Key1", StringCategory, "draft-hixie- thewebsocketprotocol-76");
defineHeader(SecWebSocketKey2, "Sec-WebSocket-Key2", StringCategory, "draft-hixie- thewebsocketprotocol-76");
defineHeader(Origin, "Origin", StringCategory, "draft-hixie- thewebsocketprotocol-76");
defineHeader(Host, "Host", StringCategory, "draft-hixie- thewebsocketprotocol-76");
defineHeader(SecWebSocketAccept, "Sec-WebSocket-Accept", StringCategory, "RFC 6455");
defineMultiHeader(Cookie, "Cookie", StringCategory, "RFC 6265");
defineHeader(Server, "Server", StringCategory, "RFC 3261");
defineHeader(Subject, "Subject", StringCategory, "RFC 3261");
defineHeader(UserAgent, "User-Agent", StringCategory, "RFC 3261");
defineHeader(Timestamp, "Timestamp", StringCategory, "RFC 3261");

defineHeader(ContentLength, "Content-Length", UInt32Category, "RFC 3261");
defineHeader(MaxForwards, "Max-Forwards", UInt32Category, "RFC 3261");
defineHeader(MinExpires, "Min-Expires", Uint32Category, "RFC 3261");
defineHeader(RSeq, "RSeq", UInt32Category, "RFC 3261");

// !dlb! this one is not quite right -- can have (comment) after field value
defineHeader(RetryAfter, "Retry-After", UInt32Category, "RFC 3261");
defineHeader(FlowTimer, "Flow-Timer", UInt32Category, "RFC 5626");

defineHeader(Expires, "Expires", ExpiresCategory, "RFC 3261");
defineHeader(SessionExpires, "Session-Expires", ExpiresCategory, "RFC 4028");
defineHeader(MinSE, "Min-SE", ExpiresCategory, "RFC 4028");

defineHeader(CallID, "Call-ID", CallID, "RFC 3261");
defineHeader(Replaces, "Replaces", CallID, "RFC 3891");
defineHeader(InReplyTo, "In-Reply-To", CallID, "RFC 3261");
defineHeader(Join, "Join", CallId, "RFC 3911");
defineHeader(TargetDialog, "Target-Dialog", CallId, "RFC 4538");

defineHeader(AuthenticationInfo, "Authentication-Info", Auth, "RFC 3261");
defineMultiHeader(Authorization, "Authorization", Auth, "RFC 3261");
defineMultiHeader(ProxyAuthenticate, "Proxy-Authenticate", Auth, "RFC 3261");
defineMultiHeader(ProxyAuthorization, "Proxy-Authorization", Auth, "RFC 3261");
defineMultiHeader(WWWAuthenticate, "Www-Authenticate", Auth, "RFC 3261");

defineHeader(CSeq, "CSeq", CSeqCategory, "RFC 3261");
defineHeader(Date, "Date", DateCategory, "RFC 3261");
defineMultiHeader(Warning, "Warning", WarningCategory, "RFC 3261");
defineMultiHeader(Via, "Via", Via, "RFC 3261");
defineHeader(RAck, "RAck", RAckCategory, "RFC 3262");
defineMultiHeader(RemotePartyId, "Remote-Party-ID", NameAddr, "draft-ietf-sip-privacy-04"); // ?bwc? Not in 3323, should we keep?
defineMultiHeader(HistoryInfo, "History-Info", NameAddr, "RFC 4244");

defineHeader(PAccessNetworkInfo, "P-Access-Network-Info", Token, "RFC 3455");
defineHeader(PChargingVector, "P-Charging-Vector", Token, "RFC 3455");
defineHeader(PChargingFunctionAddresses, "P-Charging-Function-Addresses", Token, "RFC 3455");
defineMultiHeader(PVisitedNetworkID, "P-Visited-Network-ID", TokenOrQuotedStringCategory, "RFC 3455");

defineMultiHeader(UserToUser, "User-to-User", TokenOrQuotedStringCategory, "draft-ietf-cuss-sip-uui-17");

#endif

const HeaderFieldValueList*
SipMessage::getRawHeader(Headers::Type headerType) const
{
   if(mHeaderIndices[headerType]>0)
   {
      return mHeaders[mHeaderIndices[headerType]];
   }
   
   return 0;
}

void
SipMessage::setRawHeader(const HeaderFieldValueList* hfvs, Headers::Type headerType)
{
   HeaderFieldValueList* copy=0;
   if (mHeaderIndices[headerType] == 0)
   {
      mHeaderIndices[headerType]=mHeaders.size();
      copy=getCopyHfvl(*hfvs);
      mHeaders.push_back(copy);
   }
   else
   {
      if(mHeaderIndices[headerType]<0)
      {
         // Setting a previously removed header type; there is already an 
         // empty HeaderFieldValueList in mHeaders for this type, all we 
         // need to do is flip the sign to re-enable it.
         mHeaderIndices[headerType]=-mHeaderIndices[headerType];
      }
      copy = mHeaders[mHeaderIndices[headerType]];
      *copy=*hfvs;
   }
   if(!Headers::isMulti(headerType) && copy->parsedEmpty())
   {
      copy->push_back(0,0,false);
   }
}

void
SipMessage::setForceTarget(const Uri& uri)
{
   if (mForceTarget)
   {
      *mForceTarget = uri;
   }
   else
   {
      mForceTarget = new Uri(uri);
   }
}

void
SipMessage::clearForceTarget()
{
   delete mForceTarget;
   mForceTarget = 0;
}

const Uri&
SipMessage::getForceTarget() const
{
   resip_assert(mForceTarget);
   return *mForceTarget;
}

bool
SipMessage::hasForceTarget() const
{
   return (mForceTarget != 0);
}

SipMessage& 
SipMessage::mergeUri(const Uri& source)
{
   header(h_RequestLine).uri() = source;
   header(h_RequestLine).uri().removeEmbedded();

   if (source.exists(p_method))
   {
      header(h_RequestLine).method() = getMethodType(source.param(p_method));
      header(h_RequestLine).uri().remove(p_method);      
   }           
   
   //19.1.5
   //dangerous headers not included in merge:
   // From, Call-ID, Cseq, Via, Record Route, Route, Accept, Accept-Encoding,
   // Accept-Langauge, Allow, Contact, Organization, Supported, User-Agent

   //from the should-verify section, remove for now, some never seem to make
   //sense:  
   // Content-Encoding, Content-Language, Content-Length, Content-Type, Date,
   // Mime-Version, and TimeStamp

   if (source.hasEmbedded())
   {
      h_AuthenticationInfo.merge(*this, source.embedded());
      h_ContentTransferEncoding.merge(*this, source.embedded());
      h_Event.merge(*this, source.embedded());
      h_Expires.merge(*this, source.embedded());
      h_SessionExpires.merge(*this, source.embedded());
      h_MinSE.merge(*this, source.embedded());
      h_InReplyTo.merge(*this, source.embedded());
      h_MaxForwards.merge(*this, source.embedded());
      h_MinExpires.merge(*this, source.embedded());
      h_Priority.merge(*this, source.embedded());
      h_ReferTo.merge(*this, source.embedded());
      h_ReferredBy.merge(*this, source.embedded());
      h_Replaces.merge(*this, source.embedded());
      h_ReplyTo.merge(*this, source.embedded());
      h_RetryAfter.merge(*this, source.embedded());
      h_Server.merge(*this, source.embedded());
      h_SIPETag.merge(*this, source.embedded());
      h_SIPIfMatch.merge(*this, source.embedded());
      h_Subject.merge(*this, source.embedded());
      h_SubscriptionState.merge(*this, source.embedded());
      h_To.merge(*this, source.embedded());
      h_Warnings.merge(*this, source.embedded());

      h_SecurityClients.merge(*this, source.embedded());
      h_SecurityServers.merge(*this, source.embedded());
      h_SecurityVerifys.merge(*this, source.embedded());

      h_Authorizations.merge(*this, source.embedded());
      h_ProxyAuthenticates.merge(*this, source.embedded());
      h_WWWAuthenticates.merge(*this, source.embedded());
      h_ProxyAuthorizations.merge(*this, source.embedded());

      h_AlertInfos.merge(*this, source.embedded());
      h_AllowEvents.merge(*this, source.embedded());
      h_CallInfos.merge(*this, source.embedded());
      h_ErrorInfos.merge(*this, source.embedded());
      h_ProxyRequires.merge(*this, source.embedded());
      h_Requires.merge(*this, source.embedded());
      h_Unsupporteds.merge(*this, source.embedded());
      h_AnswerMode.merge(*this, source.embedded());
      h_PrivAnswerMode.merge(*this, source.embedded());

      h_RSeq.merge(*this, source.embedded());
      h_RAck.merge(*this, source.embedded());
   }   
   //unknown header merge
   return *this;   
}

void 
SipMessage::setSecurityAttributes(auto_ptr<SecurityAttributes> sec)
{
   mSecurityAttributes = sec;
}

void
SipMessage::callOutboundDecorators(const Tuple &src, 
                                    const Tuple &dest,
                                    const Data& sigcompId)
{
   if(mIsDecorated)
   {
      rollbackOutboundDecorators();
   }

  std::vector<MessageDecorator*>::iterator i;
  for (i = mOutboundDecorators.begin();
       i != mOutboundDecorators.end(); i++)
  {
    (*i)->decorateMessage(*this, src, dest, sigcompId);
  }
  mIsDecorated = true;
}

void 
SipMessage::clearOutboundDecorators()
{
   while(!mOutboundDecorators.empty())
   {
      delete mOutboundDecorators.back();
      mOutboundDecorators.pop_back();
   }
}

void 
SipMessage::rollbackOutboundDecorators()
{
   std::vector<MessageDecorator*>::reverse_iterator r;
   for(r=mOutboundDecorators.rbegin(); r!=mOutboundDecorators.rend(); ++r)
   {
      (*r)->rollbackMessage(*this);
   }
   mIsDecorated = false;
}

void 
SipMessage::copyOutboundDecoratorsToStackCancel(SipMessage& cancel)
{
  std::vector<MessageDecorator*>::iterator i;
  for (i = mOutboundDecorators.begin();
       i != mOutboundDecorators.end(); i++)
  {
     if((*i)->copyToStackCancels())
     {
        cancel.addOutboundDecorator(std::auto_ptr<MessageDecorator>((*i)->clone()));
     }    
  }
}

void 
SipMessage::copyOutboundDecoratorsToStackFailureAck(SipMessage& ack)
{
  std::vector<MessageDecorator*>::iterator i;
  for (i = mOutboundDecorators.begin();
       i != mOutboundDecorators.end(); i++)
  {
     if((*i)->copyToStackFailureAcks())
     {
        ack.addOutboundDecorator(std::auto_ptr<MessageDecorator>((*i)->clone()));
     }    
  }
}

/* ====================================================================
 * The Vovida Software License, Version 1.0 
 * 
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact vocal@vovida.org.
 *
 * 4. Products derived from this software may not be called "VOCAL", nor
 *    may "VOCAL" appear in their name, without prior written
 *    permission of Vovida Networks, Inc.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by Vovida
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 * Inc.  For more information on Vovida Networks, Inc., please see
 * <http://www.vovida.org/>.
 *
 * vi: set shiftwidth=3 expandtab:
 */