File: TransactionState.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 (3090 lines) | stat: -rw-r--r-- 105,988 bytes parent folder | download
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
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "resip/stack/AbandonServerTransaction.hxx"
#include "resip/stack/CancelClientInviteTransaction.hxx"
#include "resip/stack/AddTransport.hxx"
#include "resip/stack/RemoveTransport.hxx"
#include "resip/stack/TerminateFlow.hxx"
#include "resip/stack/EnableFlowTimer.hxx"
#include "resip/stack/ZeroOutStatistics.hxx"
#include "resip/stack/InvokeAfterSocketCreationFunc.hxx"
#include "resip/stack/PollStatistics.hxx"
#include "resip/stack/ConnectionTerminated.hxx"
#include "resip/stack/KeepAlivePong.hxx"
#include "resip/stack/DnsInterface.hxx"
#include "resip/stack/DnsResultMessage.hxx"
#include "resip/stack/DnsResult.hxx"
#include "resip/stack/Helper.hxx"
#include "resip/stack/MethodTypes.hxx"
#include "resip/stack/SipMessage.hxx"
#include "resip/stack/SendData.hxx"
#include "resip/stack/SipStack.hxx"
#include "resip/stack/StatisticsManager.hxx"
#include "resip/stack/TimerMessage.hxx"
#include "resip/stack/TransactionController.hxx"
#include "resip/stack/TransactionMessage.hxx"
#include "resip/stack/TransactionState.hxx"
#include "resip/stack/TransactionTerminated.hxx"
#include "resip/stack/TransportFailure.hxx"
#include "resip/stack/TransactionUserMessage.hxx"
#include "resip/stack/TransportFailure.hxx"
#include "resip/stack/TransportSelector.hxx"
#include "resip/stack/TransactionUser.hxx"
#include "resip/stack/TuSelector.hxx"
#include "resip/stack/InteropHelper.hxx"
#include "resip/stack/KeepAliveMessage.hxx"
#include "rutil/ResipAssert.h"
#include "rutil/DnsUtil.hxx"
#include "rutil/Logger.hxx"
#include "rutil/MD5Stream.hxx"
#include "rutil/Socket.hxx"
#include "rutil/Random.hxx"
#include "rutil/WinLeakCheck.hxx"

using namespace resip;

#define RESIPROCATE_SUBSYSTEM Subsystem::TRANSACTION

UInt32 TransactionState::StatelessIdCounter = 0;

TransactionState::TransactionState(TransactionController& controller, Machine m, 
                                   State s, const Data& id, MethodTypes method, const Data& methodText, TransactionUser* tu) : 
   mController(controller),
   mMachine(m), 
   mState(s),
   mIsAbandoned(false),
   mIsReliable(true), // !jf! 
   mNextTransmission(0),
   mDnsResult(0),
   mId(id),
   mMethod(method),
   mMethodText(method==UNKNOWN ? new Data(methodText) : 0),
   mCurrentMethodType(UNKNOWN),
   mCurrentResponseCode(0),
   mAckIsValid(false),
   mPendingOperation(None),
   mTransactionUser(tu),
   mFailureReason(TransportFailure::None),
   mFailureSubCode(0),
   mTcpConnectTimerStarted(false)
{
   StackLog (<< "Creating new TransactionState: " << *this);
}


TransactionState* 
TransactionState::makeCancelTransaction(TransactionState* tr, Machine machine, const Data& tid)
{
   TransactionState* cancel = new TransactionState(tr->mController, 
                                                   machine, 
                                                   Trying, 
                                                   tid, 
                                                   CANCEL, 
                                                   Data::Empty,
                                                   tr->mTransactionUser);
   // !jf! don't set this since it will be set by TransactionState::processReliability()
   //cancel->mIsReliable = tr->mIsReliable;  
   cancel->mResponseTarget = tr->mResponseTarget;
   cancel->mTarget = tr->mTarget;
   cancel->add(tid);

   // !jf! don't call processServerNonInvite since it will delete
   // the sip message which needs to get sent to the TU
   cancel->processReliability(tr->mTarget.getType());
   return cancel;
}

void 
TransactionState::handleInternalCancel(SipMessage* cancel,
                                       TransactionState& clientInvite)
{
   TransactionState* state = TransactionState::makeCancelTransaction(&clientInvite, ClientNonInvite, clientInvite.mId+"cancel");
   // Make sure the branch in the CANCEL matches the current 
   // branch of the INVITE, in case we have done a DNS failover (the transport 
   // sequences could be different by now)
   cancel->header(h_Vias).front().param(p_branch)=clientInvite.mNextTransmission->const_header(h_Vias).front().param(p_branch);
   state->processClientNonInvite(cancel);
   // for the INVITE in case we never get a 487
   clientInvite.mController.mTimers.add(Timer::TimerCleanUp, clientInvite.mId, 128*Timer::T1);
}

bool
TransactionState::handleBadRequest(const resip::SipMessage& badReq, TransactionController& controller)
{
   resip_assert(badReq.isRequest() && badReq.method() != ACK);
   try
   {
      SipMessage* error = Helper::makeResponse(badReq,400);
      if(badReq.getReason())
      {
         error->header(h_StatusLine).reason()+="(" + *(badReq.getReason())+ ")";
      }
      Tuple target(badReq.getSource());

      if(badReq.isExternal())
      {
         controller.mTransportSelector.transmit(error,target);
         delete error;
         return true;
      }
      else
      {
         // ?bwc? Should we put together a TransactionState here so we can
         // send a 400 to the TU?
         // TODO if we send the error to the TU, don't delete the error
         delete error;
         return false;
      }
   }
   catch(resip::BaseException& e)
   {
      ErrLog(<< "Exception thrown in TransactionState::handleBadRequest."
                  " This shouldn't happen. " << e);
      return false;
   }
}

TransactionState::~TransactionState()
{
   resip_assert(mState != Bogus);

   if (mDnsResult)
   {
      mDnsResult->destroy();
   }

   //StackLog (<< "Deleting TransactionState " << mId << " : " << this);
   erase(mId);
   
   delete mNextTransmission;
   delete mMethodText;
   mNextTransmission = 0;
   mMethodText = 0;

   mState = Bogus;
}

bool
TransactionState::processSipMessageAsNew(SipMessage* sip, TransactionController& controller, const Data& tid)
{
   MethodTypes method=sip->method();
   StackLog (<< "No matching transaction for " << sip->brief());
   TransactionUser* tu = 0;      
   if (sip->isExternal())
   {
      if (controller.mTuSelector.haveTransactionUsers() && sip->isRequest())
      {
         tu = controller.mTuSelector.selectTransactionUser(*sip);
         if (!tu)
         {
            //InfoLog (<< "Didn't find a TU for " << sip->brief());
            // !bwc! We really should do something other than a 500 here.
            // If none of the TUs liked the request because of the Request-
            // Uri scheme, we should be returning a 416, for example.
            // ?bwc? Um, should we _really_ be doing this statelessly?
            InfoLog( << "No TU found for message: " << sip->brief());               
            SipMessage* noMatch = Helper::makeResponse(*sip, 500);
            Tuple target(sip->getSource());

            controller.mTransportSelector.transmit(noMatch, target);
            delete noMatch;
            return false;
         }
         else
         {
            //InfoLog (<< "Found TU for " << sip->brief());
         }
      }
   }
   else
   {
      tu = sip->getTransactionUser();
      if (!tu)
      {
         //InfoLog (<< "No TU associated with " << sip->brief());
      }
   }
               
   if (sip->isRequest())
   {
      // create a new state object and insert in the TransactionMap
               
      if (sip->isExternal()) // new sip msg from transport
      {
         if (method == INVITE)
         {
            // !rk! This might be needlessly created.  Design issue.
            TransactionState* state = new TransactionState(controller, 
                                                            ServerInvite, 
                                                            Trying, 
                                                            tid, 
                                                            INVITE,
                                                            Data::Empty,
                                                            tu);

            state->mNextTransmission = state->make100(sip);
            state->mResponseTarget = sip->getSource(); // UACs source address
            // since we don't want to reply to the source port if rport present 
            state->mResponseTarget.setPort(Helper::getPortForReply(*sip));
            state->mIsReliable = isReliable(state->mResponseTarget.getType());
            state->add(tid);
               
            if (Timer::T100 == 0)
            {
               state->sendCurrentToWire(); // will get deleted when this is deleted
               state->mState = Proceeding;
            }
            else
            {
               //StackLog(<<" adding T100 timer (INV)");
               controller.mTimers.add(Timer::TimerTrying, tid, Timer::T100);
            }
            state->sendToTU(sip);
            return true;
         }
         else if (method == CANCEL)
         {
            // Note:  For cancel requests the tid member passed in will have the token "cancel" appended
            // to it, so that the cancel request can be treated as it's own transaction.  sip->getTransactionId()
            // will be the original tid from the wire and should match the tid of the INVITE request being 
            // cancelled.
            TransactionState* matchingInvite = 
               controller.mServerTransactionMap.find(sip->getTransactionId());
            if (matchingInvite == 0)
            {
               InfoLog (<< "No matching INVITE for incoming (from wire) CANCEL to uas");
               //was TransactionState::sendToTU(tu, controller, Helper::makeResponse(*sip, 481));
               SipMessage* response = Helper::makeResponse(*sip, 481);
               Tuple target(sip->getSource());
               controller.mTransportSelector.transmit(response, target);
               
               delete response;
               return false;
            }
            else
            {
               resip_assert(matchingInvite);
               TransactionState* state = TransactionState::makeCancelTransaction(matchingInvite, ServerNonInvite, tid);
               state->startServerNonInviteTimerTrying(*sip,tid);
               state->sendToTU(sip);
               return true;
            }
         }
         else if (method != ACK)
         {
            TransactionState* state = new TransactionState(controller, 
                                                            ServerNonInvite,
                                                            Trying, 
                                                            tid, 
                                                            method,
                                                            sip->methodStr(),
                                                            tu);
            state->mResponseTarget = sip->getSource();
            // since we don't want to reply to the source port if rport present 
            state->mResponseTarget.setPort(Helper::getPortForReply(*sip));
            state->add(tid);
            state->mIsReliable = isReliable(state->mResponseTarget.getType());
            state->startServerNonInviteTimerTrying(*sip,tid);
            state->sendToTU(sip);
            return true;
         }
            
         // Incoming ACK just gets passed to the TU
         //StackLog(<< "Adding incoming message to TU fifo " << tid);
         TransactionState::sendToTU(tu, controller, sip);
      }
      else // new sip msg from the TU
      {
         if (method == INVITE)
         {
            TransactionState* state = new TransactionState(controller, 
                                                            ClientInvite, 
                                                            Calling, 
                                                            tid, 
                                                            INVITE,
                                                            Data::Empty,
                                                            tu);
            state->add(state->mId);
            state->processClientInvite(sip);
         }
         else if (method == ACK)
         {
            TransactionState* state = new TransactionState(controller, 
                                                            Stateless, 
                                                            Calling, 
                                                            tid, 
                                                            ACK,
                                                            Data::Empty,
                                                            tu);
            state->add(state->mId);
            state->mController.mTimers.add(Timer::TimerStateless, state->mId, Timer::TS );
            state->processStateless(sip);
         }
         else if (method == CANCEL)
         {
            TransactionState* matchingInvite = controller.mClientTransactionMap.find(sip->getTransactionId());
               
            if (matchingInvite == 0)
            {
               InfoLog (<< "No matching INVITE for incoming (from TU) CANCEL to uac");
               TransactionState::sendToTU(tu, controller, Helper::makeResponse(*sip,481));
               return false;
            }
            else if (matchingInvite->mState == Calling) // CANCEL before 1xx received
            {
               WarningLog(<< "You can't CANCEL a request until a provisional has been received");
               StackLog (<< *matchingInvite);
               StackLog (<< *sip);

               matchingInvite->mIsAbandoned = true;
               return false;
            }
            else if (matchingInvite->mState == Completed)
            {
               // A final response was already seen for this INVITE transaction
               matchingInvite->sendToTU(Helper::makeResponse(*sip, 200));
               return false;
            }
            else
            {
               handleInternalCancel(sip, *matchingInvite);
            }
         }
         else 
         {
            TransactionState* state = new TransactionState(controller, 
                                                            ClientNonInvite, 
                                                            Trying, 
                                                            tid, 
                                                            method,
                                                            sip->methodStr(),
                                                            tu);
            state->add(tid);
            state->processClientNonInvite(sip);
         }
      }
   }
   else if (sip->isResponse()) // stray response
   {
      if (controller.mDiscardStrayResponses)
      {
         InfoLog (<< "discarding stray response: " << sip->brief());
         return false;
      }
      else
      {
         StackLog (<< "forwarding stateless response: " << sip->brief());
         TransactionState* state = 
            new TransactionState(controller, 
                                 Stateless, 
                                 Calling, 
                                 Data(StatelessIdCounter++), 
                                 method,
                                 sip->methodStr(),
                                 tu);
         state->add(state->mId);
         state->mController.mTimers.add(Timer::TimerStateless, state->mId, Timer::TS );
         state->processStateless(sip);
      }
   }
   else // wasn't a request or a response
   {
      ErrLog (<< "Got a SipMessage that was neither a request nor response!" 
               << sip->brief());
      return false;
   }

   return true;
}

void
TransactionState::process(TransactionController& controller, 
                           TransactionMessage* message)
{ 
   // Note:  KeepAliveMessage is a special SipMessage - check for it first
   KeepAliveMessage* keepAlive = dynamic_cast<KeepAliveMessage*>(message);
   if (keepAlive)
   {
      StackLog ( << "Sending keep alive to: " << keepAlive->getDestination());      
      controller.mTransportSelector.transmit(keepAlive, keepAlive->getDestination());
      delete keepAlive;
      return;
   }

   SipMessage* sip = dynamic_cast<SipMessage*>(message);
   if(!sip)
   {
      KeepAlivePong* pong = dynamic_cast<KeepAlivePong*>(message);
      if (pong)
      {
         controller.mTuSelector.add(pong);
         delete pong;
         return;
      }

      ConnectionTerminated* term = dynamic_cast<ConnectionTerminated*>(message);
      if (term)
      {
         controller.mTuSelector.add(term);
         delete term;
         return;
      }

      TerminateFlow* termFlow = dynamic_cast<TerminateFlow*>(message);
      if(termFlow)
      {
         controller.mTransportSelector.terminateFlow(termFlow->getFlow());
         delete termFlow;
         return;
      }

      EnableFlowTimer* enableFlowTimer = dynamic_cast<EnableFlowTimer*>(message);
      if(enableFlowTimer)
      {
         controller.mTransportSelector.enableFlowTimer(enableFlowTimer->getFlow());
         delete enableFlowTimer;
         return;
      }

      ZeroOutStatistics* zeroOutStatistics = dynamic_cast<ZeroOutStatistics*>(message);
      if(zeroOutStatistics)
      {
         controller.mStatsManager.zeroOut();
         delete zeroOutStatistics;
         return;
      }

      PollStatistics* pollStatistics = dynamic_cast<PollStatistics*>(message);
      if(pollStatistics)
      {
         controller.mStatsManager.poll();
         delete pollStatistics;
         return;
      }

      AddTransport* addTransport = dynamic_cast<AddTransport*>(message);
      if(addTransport)
      {
         controller.mTransportSelector.addTransport(addTransport->getTransport(), true /* isStackRunning */);
         delete addTransport;
         return;
      }

      RemoveTransport* removeTransport = dynamic_cast<RemoveTransport*>(message);
      if(removeTransport)
      {
         controller.mTransportSelector.removeTransport(removeTransport->getTransportKey());
         delete removeTransport;
         return;
      }

      InvokeAfterSocketCreationFunc* invokeAfterSocketCreationFunc = dynamic_cast<InvokeAfterSocketCreationFunc*>(message);
      if(invokeAfterSocketCreationFunc)
      {
          controller.mTransportSelector.invokeAfterSocketCreationFunc(invokeAfterSocketCreationFunc->getTransportType());
      }
   }
   
   // .bwc. We can't do anything without a tid here. Check this first.
   Data tid;   
   try
   {
      tid = message->getTransactionId();
   }
   catch(resip::BaseException&)
   {
      // .bwc This is not our error. Do not ErrLog.
      DebugLog( << "TransactionState::process dropping message with invalid tid " << message->brief());
      delete message;
      return;
   }
   
   MethodTypes method = UNKNOWN;

   if(sip)
   {
      method=sip->method();
      // ?bwc? Should this come after checking for error conditions?
      if(controller.mStack.statisticsManagerEnabled() && sip->isExternal())
      {
         controller.mStatsManager.received(sip);
      }
      
      // .bwc. Check for error conditions we can respond to.
      if(sip->isRequest() && method != ACK)
      {
         // .bwc. If we are going to statelessly send a 503, we should do it
         // in the transport, before we do expensive stuff like basicCheck and
         // stampReceived. If the TU fifo is backed up, we should send a 
         // _stateful_ 503 below. (And only if the specific TU that can handle
         // this message is backed up; if other TUs are congested, we should let 
         // it pass.)
         /*
         if(sip->isExternal() && (controller.isTUOverloaded() || controller.mStateMacFifo.isRejecting()))
         {
            SipMessage* tryLater = Helper::makeResponse(*sip, 503);
            if( controller.mStateMacFifo.isRejecting() )
              tryLater->header(h_RetryAfter).value() = controller.mStateMacFifo.getRetryAfter();
            else
              tryLater->header(h_RetryAfter).value() = 32 + (Random::getRandom() % 32);
              
            //tryLater->header(h_RetryAfter).comment() = "Server busy TRANS";
            Tuple target(sip->getSource());
            delete sip;
            controller.mTransportSelector.transmit(tryLater, target);
            delete tryLater;
            return;
         }
         */
         
         if(sip->isInvalid())
         {
            handleBadRequest(*sip,controller);
            delete sip;
            return;
         }         
      }

#ifdef PEDANTIC_STACK
      try
      {
         sip->parseAllHeaders();
      }
      catch(resip::BaseException& e)
      {
         if(sip->isRequest() && method!=ACK)
         {
            handleBadRequest(*sip,controller);
         }
         
         InfoLog(<< "Exception caught by pedantic stack: " << e);
      }
#endif      
      
      // This ensures that CANCEL requests form unique transactions
      if (method == CANCEL) 
      {
         tid += "cancel";
      }
   }
      
   TransactionState* state = 0;
   if (message->isClientTransaction()) 
   {
      state = controller.mClientTransactionMap.find(tid);
   }
   else 
   {
      state = controller.mServerTransactionMap.find(tid);
   }
   
   if (state && sip && sip->isExternal())
   {
      // Various kinds of response fixup.
      if(sip->isResponse() &&
         state->mNextTransmission)
      {
         // .bwc. This code (if enabled) ensures that responses have the same
         // CallId and tags as the request did (excepting the introduction of a 
         // remote tag). This is to protect dialog-stateful TUs that don't react 
         // gracefully when a stupid/malicious endpoint fiddles with the tags 
         // and/or CallId when it isn't supposed to. (DUM is one such TU)
         if(state->mController.getFixBadDialogIdentifiers())
         {
            if(sip->const_header(h_CallId).isWellFormed())
            {
               if(!(sip->const_header(h_CallId) == 
                           state->mNextTransmission->const_header(h_CallId)))
               {
                  InfoLog(<< "Other end modified our Call-Id... correcting.");
                  sip->header(h_CallId) = state->mNextTransmission->const_header(h_CallId);
               }
            }
            else
            {
               InfoLog(<< "Other end corrupted our CallId... correcting.");
               sip->header(h_CallId) = state->mNextTransmission->const_header(h_CallId);
            }
   
            const NameAddr& from = state->mNextTransmission->const_header(h_From);
            if(sip->const_header(h_From).isWellFormed())
            {
               // Overwrite tag.
               if(from.exists(p_tag))
               {
                  if(!sip->const_header(h_From).exists(p_tag) ||
                     sip->const_header(h_From).param(p_tag) != from.param(p_tag))
                  {
                     InfoLog(<<"Other end modified our local tag... correcting.");
                     sip->header(h_From).param(p_tag) = from.param(p_tag);
                  }
               }
               else if(sip->const_header(h_From).exists(p_tag))
               {
                  InfoLog(<<"Other end added a local tag for us... removing.");
                  sip->header(h_From).remove(p_tag);
               }
            }
            else
            {
               InfoLog(<<"Other end corrupted our From header... replacing.");
               // Whole header is hosed, overwrite.
               sip->header(h_From) = from;
            }
   
            const NameAddr& to = state->mNextTransmission->const_header(h_To);
            if(sip->const_header(h_To).isWellFormed())
            {
               // Overwrite tag.
               if(to.exists(p_tag))
               {
                  if(!sip->const_header(h_To).exists(p_tag) ||
                     sip->const_header(h_To).param(p_tag) != to.param(p_tag))
                  {
                     InfoLog(<<"Other end modified the (existing) remote tag... "
                                 "correcting.");
                     sip->header(h_To).param(p_tag) = to.param(p_tag);
                  }
               }
            }
            else
            {
               InfoLog(<<"Other end corrupted our To header... replacing.");
               // Whole header is hosed, overwrite.
               sip->header(h_To) = to;
            }
         }

         // .bwc. This code (if enabled) ensures that responses have the same
         // CSeq number as the request did. This is to protect TUs that don't 
         // react gracefully when a stupid/malicious endpoint fiddles with the 
         // CSeq number. (This is a very cheap check; we already parse the CSeq
         // for incoming messages)
         if(state->mController.getFixBadCSeqNumbers())
         {
            unsigned int old=state->mNextTransmission->const_header(h_CSeq).sequence();
            if(sip->const_header(h_CSeq).sequence() != old)
            {
               InfoLog(<<"Other end changed our CSeq number... replacing.");
               sip->header(h_CSeq).sequence()=old;
            }
         }
      }
      // .bwc. This code ensures that the transaction state-machine can recover
      // from ACK/200 with the same tid as the original INVITE. This problem is
      // stupidly common. 
      if(sip->isRequest() && method == ACK && !state->mAckIsValid)
      {
         // Must have received an ACK to a 200;
         // We will never respond to this, so nothing will need this tid for
         // driving transaction state. Additionally, 
         InfoLog(<<"Someone sent us an ACK/200 with the same tid as the "
                     "original INVITE. This is bad behavior, and should be "
                     "corrected in the client.");
         sip->mIsBadAck200=true;
         // .bwc. This is a new stateless transaction, despite its tid.
         state=0;
      }
   }

   if(state && sip)
   {
      switch(state->mMethod)
      {
         case INVITE:
            if(method != INVITE && method != ACK)
            {
               // Maybe respond if a request?
               delete sip;
               return;
            }
            break;
         case UNKNOWN:
            if(!state->mMethodText || *(state->mMethodText) != sip->methodStr())
            {
               // Maybe respond if a request?
               delete sip;
               return;
            }
            break;
         default:
            if(state->mMethod != method)
            {
               // Maybe respond if a request?
               delete sip;
               return;
            }
            break;
      }

      // .bwc. in private email 1 Feb 2013:
      // According to the spec, there is no such thing as a reliable NIT
      // retransmission; what we have just observed is a transaction id collision
      // technically. Maybe a reliable NIT transaction collision needs special
      // handling? It is probably a lot more common that this is a confused client,
      // than a client that has innocently used the same tid as some other client,
      // though. Maybe we should just ignore such requests?
      if(sip->isExternal() && sip->isRequest() &&
         method != ACK &&
         state->mIsReliable)
      {
         InfoLog(<<"Someone sent us a request with a repeated transaction ID "
                     "over a reliable transport.  Discarding the request.");
         delete sip;
         return;
      }
   }

   if (state) // found transaction for sip msg
   {
      StackLog (<< "Found matching transaction for " << message->brief() << " -> " << *state);

      switch (state->mMachine)
      {
         case ClientNonInvite:
            state->processClientNonInvite(message);
            break;
         case ClientInvite:
            // ACK from TU will be Stateless
            resip_assert (!sip || !(state->isFromTU(sip) &&  sip->isRequest() && method == ACK));
            state->processClientInvite(message);
            break;
         case ServerNonInvite:
            state->processServerNonInvite(message);
            break;
         case ServerInvite:
            state->processServerInvite(message);
            break;
         case Stateless:
            state->processStateless(message);
            break;
         case ClientStale:
            state->processClientStale(message);
            break;
         case ServerStale:
            state->processServerStale(message);
            break;
         default:
            CritLog(<<"internal state error");
            resip_assert(0);
            return;
      }
   }
   else if (sip)  // new transaction
   {
      try
      {
         bool processed = processSipMessageAsNew(sip, controller, tid);
         if (!processed)
         {
            delete sip;      
         }
      }
      catch(resip::BaseException& e)   
      {
         StackLog ( << "Got badly formatted sip message, error: " << e.what());      
         if(sip->isRequest() && sip->method()!=ACK)
         {
            handleBadRequest(*sip, controller);
         }         
         delete sip;      
      }
      catch(std::exception& err)      
      {
         StackLog ( << "Got error: " << err.what());
         delete sip;
      }
   } 
   else // timer or other non-sip msg
   {
      //StackLog (<< "discarding non-sip message: " << message->brief());
      delete message;
   }
}

void
TransactionState::processTimer(TransactionController& controller,
                                 TimerMessage* message)
{
   Data tid = message->getTransactionId();

   if(controller.getRejectionBehavior()==CongestionManager::REJECTING_NON_ESSENTIAL)
   {
      // .bwc. State machine fifo is backed up; we probably should not be 
      // retransmitting anything right now. If we have a retransmit timer, 
      // reschedule for later, but don't retransmit.
      switch(message->getType())
      {
         case Timer::TimerA: // doubling
            controller.mTimers.add(Timer::TimerA, 
                                    tid, 
                                    message->getDuration()*2);
            delete message;
            return;
         case Timer::TimerE1:// doubling, until T2
         case Timer::TimerG: // doubling, until T2
            controller.mTimers.add(message->getType(), 
                                    tid, 
                                    resipMin(message->getDuration()*2,
                                             Timer::T2));
            delete message;
            return;
         case Timer::TimerE2:// just reset
            controller.mTimers.add(Timer::TimerE2, 
                                    tid, 
                                    Timer::T2);
            delete message;
            return;
         default:
            ; // let it through
      }
   }

   TransactionState* state = 0;
   if (message->isClientTransaction()) state = controller.mClientTransactionMap.find(tid);
   else state = controller.mServerTransactionMap.find(tid);
   
   if (state) // found transaction for timer
   {
      StackLog (<< "Found matching transaction for " << message->brief() << " -> " << *state);

      switch (state->mMachine)
      {
         case ClientNonInvite:
            state->processClientNonInvite(message);
            break;
         case ClientInvite:
            state->processClientInvite(message);
            break;
         case ServerNonInvite:
            state->processServerNonInvite(message);
            break;
         case ServerInvite:
            state->processServerInvite(message);
            break;
         case Stateless:
            state->processStateless(message);
            break;
         case ClientStale:
            state->processClientStale(message);
            break;
         case ServerStale:
            state->processServerStale(message);
            break;
         default:
            CritLog(<<"internal state error");
            resip_assert(0);
            return;
      }
   }
   else
   {
      delete message;
   }

}

void
TransactionState::startServerNonInviteTimerTrying(SipMessage& sip, const Data& tid)
{
   unsigned int duration = 3500;
   if(Timer::T1 != 500) // optimzed for T1 == 500
   {
      // Iteratively calculate how much time before TimerE reaches T2 (RFC4320) - could be improved
      duration = Timer::T1;
      while(duration*2<Timer::T2) duration = duration * 2;
   }
   resetNextTransmission(make100(&sip));  // Store for use when timer expires
   mController.mTimers.add(Timer::TimerTrying, tid, duration );  // Start trying timer so that we can send 100 to NITs as recommened in RFC4320
}

void
TransactionState::processStateless(TransactionMessage* message)
{
   // for ACK messages from the TU, there is no transaction, send it directly
   // to the wire // rfc3261 17.1 Client Transaction
   SipMessage* sip = dynamic_cast<SipMessage*>(message);
   StackLog (<< "TransactionState::processStateless: " << message->brief());

   // !jf! There is a leak for Stateless transactions associated with ACK to 200
   if (isFromTU(message))
   {
      resetNextTransmission(sip);
      sendCurrentToWire();
   }
   else if(sip && isFromWire(sip))
   {
      InfoLog (<< "Received message from wire on a stateless transaction");
      StackLog (<< *sip);
      //assert(0);
      sendToTU(sip);
   }
   else if (isTransportError(message))
   {
      processTransportFailure(message);
      
      delete message;
      delete this;
   }
   else if (isTcpConnectState(message))
   {
       // stateless mode is not supported
       //processTcpConnectState(message);
       delete message;
   }
   else if (isTimer(message))
   {
      TimerMessage* timer = dynamic_cast<TimerMessage*>(message);
      if (timer->getType() == Timer::TimerStateless)
      {
         delete message;
         delete this;
      }
      else
      {
         delete timer;
         resip_assert(0);
      }
   }
   else if(dynamic_cast<DnsResultMessage*>(message))
   {
      handleSync(mDnsResult);
      delete message;
   }
   else if (isAbandonServerTransaction(message))
   {
      // ?
      delete message;
   }
   else
   {
      delete message;
      resip_assert(0);
   }
}

void 
TransactionState::saveOriginalContactAndVia(const SipMessage& sip)
{
   if(sip.exists(h_Contacts) && sip.const_header(h_Contacts).size() == 1 &&
      sip.const_header(h_Contacts).front().isWellFormed())
   {
      mOriginalContact = std::auto_ptr<NameAddr>(new NameAddr(sip.header(h_Contacts).front()));
   }
   mOriginalVia = std::auto_ptr<Via>(new Via(sip.header(h_Vias).front()));
}

void TransactionState::restoreOriginalContactAndVia()
{
   if (mOriginalContact.get())
   {
      mNextTransmission->header(h_Contacts).front() = *mOriginalContact;
   }                  
   if (mOriginalVia.get())
   {
      mOriginalVia->param(p_branch).incrementTransportSequence();
      mNextTransmission->header(h_Vias).front() = *mOriginalVia;
   }
}

void
TransactionState::processClientNonInvite(TransactionMessage* msg)
{ 
   StackLog (<< "TransactionState::processClientNonInvite: " << msg->brief());

   if (isRequest(msg) && isFromTU(msg))
   {
      //StackLog (<< "received new non-invite request");
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      resetNextTransmission(sip);
      saveOriginalContactAndVia(*sip);
      mController.mTimers.add(Timer::TimerF, mId, Timer::TF);
      sendCurrentToWire();
   }
   else if (isResponse(msg) && isFromWire(msg)) // from the wire
   {
      //StackLog (<< "received response from wire");

      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      int code = sip->const_header(h_StatusLine).responseCode();
      if (code >= 100 && code < 200) // 1XX
      {
         if (mState == Trying || mState == Proceeding)
         {
            //?slg? if we set the timer in Proceeding, then every 1xx response will cause another TimerE2 to be set and many retransmissions will occur - which is not correct
            // Should we restart the E2 timer though?  If so, we need to use somekind of timer sequence number so that previous E2 timers get discarded.
            if (!mIsReliable && mState == Trying)
            {
               mController.mTimers.add(Timer::TimerE2, mId, Timer::T2 );
            }
            mState = Proceeding;
            sendToTU(msg); // don't delete            
         }
         else
         {
            // ignore
            delete msg;
         }
      }
      else if (code >= 200)
      {
         // don't notify the TU of retransmissions
         if (mState == Trying || mState == Proceeding)
         {
            sendToTU(msg); // don't delete
         }
         else if (mState == Completed)
         {
            delete msg;
         }
         else
         {
            resip_assert(0);
            delete sip;
         }
         
         if (mIsReliable)
         {
            terminateClientTransaction(mId);
            delete this;
         }
         else if (mState != Completed) // prevent TimerK reproduced
         {
            mState = Completed;
            mController.mTimers.add(Timer::TimerK, mId, Timer::T4 );
            // !bwc! Got final response in NIT. We don't need to do anything
            // except quietly absorb retransmissions. Dump all state.
            if(mDnsResult)
            {
               mDnsResult->destroy();
               mDnsResult=0;
               mPendingOperation=None;
            }
            resetNextTransmission(0);
         }
      }
      else
      {
         resip_assert(0);
         delete sip;
      }
   }
   else if (isTimer(msg))
   {
      //StackLog (<< "received timer in client non-invite transaction");

      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      switch (timer->getType())
      {
         case Timer::TimerE1:
            if (mState == Trying)
            {
               unsigned long d = timer->getDuration();
               if (d < Timer::T2) d *= 2;
               mController.mTimers.add(Timer::TimerE1, mId, d);
               StackLog (<< "Transmitting current message");
               sendCurrentToWire();
               delete timer;
            }
            else
            {
               // ignore
               delete msg;
            }
            break;

         case Timer::TimerE2:
            if (mState == Proceeding)
            {
               mController.mTimers.add(Timer::TimerE2, mId, Timer::T2);
               StackLog (<< "Transmitting current message");
               sendCurrentToWire();
               delete timer;
            }
            else 
            {
               // ignore
               delete msg;
            }
            break;

         case Timer::TcpConnectTimer:
             if (mTcpConnectTimerStarted) // Ignore timer if we went connected (note: when we connect we set mTcpConnectTimerStarted to false)
             {
                 TransportFailure failure(mId, TransportFailure::ConnectionException);
                 processTransportFailure(&failure);
             }
             delete msg;
             break;

         case Timer::TimerF:
            if (mState == Trying || mState == Proceeding)
            {
               // !bwc! We hold onto this until we get a response from the wire
               // in client transactions, for this contingency.
               resip_assert(mNextTransmission);
               if(mPendingOperation == Dns)
               {
                  WarningLog(<< "Transaction timed out while waiting for DNS "
                              "result uri=" << 
                              mNextTransmission->const_header(h_RequestLine).uri());
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
               }
               else
               {
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
               }
               terminateClientTransaction(mId);
               delete this;
            }
            
            delete msg;
            break;

         case Timer::TimerK:
            terminateClientTransaction(mId);
            delete msg;
            delete this;
            break;

         default:
            //InfoLog (<< "Ignoring timer: " << *msg);
            delete msg;
            break;
      }
   }
   else if (isTransportError(msg))
   {
      processTransportFailure(msg);
      delete msg;
   }
   else if (isTcpConnectState(msg))
   {
      processTcpConnectState(msg);
      delete msg;
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else if (isAbandonServerTransaction(msg))
   {
      // ?
      delete msg;
   }
   else
   {
      //StackLog (<< "TransactionState::processClientNonInvite: message unhandled");
      delete msg;
   }
}

void
TransactionState::processClientInvite(TransactionMessage* msg)
{
   StackLog(<< "TransactionState::processClientInvite: " << msg->brief() << " " << *this);
   if (isRequest(msg) && isFromTU(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      switch (sip->method())
      {
         // Received INVITE request from TU="Transaction User", Start Timer B which controls
         // transaction timeouts. 
         case INVITE:
            if(mState==Calling && !mNextTransmission && mMsgToRetransmit.empty())
            {
               resetNextTransmission(sip);
               saveOriginalContactAndVia(*sip);
               mController.mTimers.add(Timer::TimerB, mId, Timer::TB );
               sendCurrentToWire();
            }
            else
            {
               WarningLog(<< "TU sent us a duplicate INVITE: fix this!");
               delete sip;
            }
            break;
            
         case CANCEL:
            resip_assert(0);
            delete msg;
            break;

         default:
            WarningLog(<< "TU sent us an erroneous request inside a Client"
                        " INVITE transaction: fix this!");
            delete msg;
            break;
      }
   }
   else if (isResponse(msg) && isFromWire(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      int code = sip->const_header(h_StatusLine).responseCode();
      switch (sip->method())
      {
         case INVITE:
            /* If the client transaction receives a provisional response while in
               the "Calling" state, it transitions to the "Proceeding" state. In the
               "Proceeding" state, the client transaction SHOULD NOT retransmit the
               request any longer (this will be Handled in  "else if (isTimer(msg))")
               The Retransmissions will be stopped, Not by Cancelling Timers but
               by Ignoring the fired Timers depending upon the State which stack is in.   
            */
            if (code >= 100 && code < 200) // 1XX
            {
               if (mState == Calling || mState == Proceeding)
               {
                  mState = Proceeding;
                  if(mIsAbandoned)
                  {
                     SipMessage* cancel = Helper::makeCancel(*mNextTransmission);
                     // Iterate through message decorators on the INVITE and see if any need to be copied to the CANCEL
                     mNextTransmission->copyOutboundDecoratorsToStackCancel(*cancel);
                     handleInternalCancel(cancel, *this);
                     mIsAbandoned=false;
                  }
                  // !bwc! We have gotten a response. We don't need to
                  // retransmit the original INVITE anymore (so we clear mMsgToRetransmit), 
                  // but we do need to retain the full original INVITE until we get a final 
                  // response, in case we need to forge an ACK.
                  mMsgToRetransmit.clear();
                  sendToTU(sip); // don't delete msg
               }
               else
               {
                  delete msg;
               }
            }

            /* When in either the "Calling" or "Proceeding" states, reception of a
               2xx response MUST cause the client transaction to enter the
               "Terminated" state, and the response MUST be passed up to the TU 
               State Machine is changed to Stale since, we wanted to ensure that 
               all 2xx gets to TU
            */
            else if (code >= 200 && code < 300)
            {
               mIsAbandoned=false;
               sendToTU(sip); // don't delete msg
               //terminateClientTransaction(mId);
               mMachine = ClientStale;
               mState = Completed;
               // !bwc! We have a final response. We don't need either of
               // mMsgToRetransmit or mNextTransmission. We ignore further
               // traffic.
               resetNextTransmission(0);
               if(mDnsResult)
               {
                  mDnsResult->destroy();
                  mDnsResult=0;
                  mPendingOperation=None;
               }
               StackLog (<< "Received 2xx on client invite transaction");
               StackLog (<< *this);
               mController.mTimers.add(Timer::TimerStaleClient, mId, Timer::TS );
            }
            else if (code >= 300)
            {
               mIsAbandoned=false;
               // When in either the "Calling" or "Proceeding" states, reception of a
               // response with status code from 300-699 MUST cause the client
               // transaction to transition to "Completed".
               if (mIsReliable)
               {
                  // Stack MUST pass the received response up to the TU, and the client
                  // transaction MUST generate an ACK request, even if the transport is
                  // reliable
                  SipMessage* ack = Helper::makeFailureAck(*mNextTransmission, *sip);
                  mNextTransmission->copyOutboundDecoratorsToStackFailureAck(*ack);
                  resetNextTransmission(ack);
                  
                  // want to use the same transport as was selected for Invite
                  resip_assert(mTarget.getType() != UNKNOWN_TRANSPORT);
                  sendCurrentToWire();
                  sendToTU(sip); // don't delete msg
                  terminateClientTransaction(mId);
                  
                  // !bwc! We only do this because we are assured the ACK
                  // will make it to the other end; if we are using an 
                  // unreliable transport, we need to stick around to absorb
                  // retransmissions of the response.
                  delete this;
               }
               else
               {
                  if (mState == Calling || mState == Proceeding)
                  {
                     // MUST pass the received response up to the TU, and the client
                     // transaction MUST generate an ACK request, even if the transport is
                     // reliable, if transport is Unreliable then Fire the Timer D which 
                     // take care of re-Transmission of ACK 
                     mState = Completed;
                     mController.mTimers.add(Timer::TimerD, mId, Timer::TD );
                     SipMessage* ack = Helper::makeFailureAck(*mNextTransmission, *sip);
                     mNextTransmission->copyOutboundDecoratorsToStackFailureAck(*ack);
                     resetNextTransmission(ack);
                     sendCurrentToWire();
                     if(mDnsResult)
                     {
                        mDnsResult->destroy();
                        mDnsResult=0;
                        mPendingOperation=None;
                     }
                     sendToTU(sip); // don't delete msg
                  }
                  else if (mState == Completed)
                  {
                     // Any retransmissions of the final response that
                     // are received while in the "Completed" state MUST
                     // cause the ACK to be re-passed to the transport
                     // layer for retransmission.
                     sendCurrentToWire();
                     delete sip;
                  }
                  else
                  {
                     /* This should never Happen if it happens we should have a plan
                        what to do here?? for now assert will work
                     */
                     CritLog(  << "State invalid");
                     // !ah! syslog
                     resip_assert(0);
                     delete sip;
                  }
               }
            }
            else
            {
               delete sip;
               resip_assert(0);
            }
            break;
            
         case CANCEL:
            resip_assert(0);
            delete sip;
            break;

         default:
            delete msg;
            break;
      }
   }
   else if (isTimer(msg))
   {
      /* Handle Transaction Timers , Retransmission Timers which were set and Handle
         Cancellation of Timers for Re-transmissions here */

      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      StackLog (<< "timer fired: " << *timer);
      
      switch (timer->getType())
      {
         case Timer::TimerA:
            if (mState == Calling && !mIsAbandoned)
            {
               unsigned long d = timer->getDuration()*2;
               // TimerA is supposed to double with each retransmit RFC3261 17.1.1          

               mController.mTimers.add(Timer::TimerA, mId, d);
               DebugLog (<< "Retransmitting INVITE ");
               sendCurrentToWire();
            }
            delete msg;
            break;

         case Timer::TcpConnectTimer:
             if (mTcpConnectTimerStarted) // Ignore timer if we went connected (note: when we connect we set mTcpConnectTimerStarted to false)
             {
                 TransportFailure failure(mId, TransportFailure::ConnectionException);
                 processTransportFailure(&failure);
             }
             delete msg;
             break;

         case Timer::TimerB:
            if (mState == Calling)
            {
               resip_assert(mNextTransmission && mNextTransmission->isRequest() &&
                        mNextTransmission->method()==INVITE);
               if(mPendingOperation == Dns)
               {
                  WarningLog(<< "Transaction timed out while waiting for DNS "
                              "result uri=" << 
                              mNextTransmission->const_header(h_RequestLine).uri());
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
               }
               else
               {
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
               }
               terminateClientTransaction(mId);
               delete this;
            }
            delete msg;
            break;

         case Timer::TimerD:
            terminateClientTransaction(mId);
            delete msg;
            delete this;
            break;

         case Timer::TimerCleanUp:
            // !ah! Cancelled Invite Cleanup Timer fired.
            if (mState == Proceeding)
            {
               resip_assert(mNextTransmission && mNextTransmission->isRequest() && 
                        mNextTransmission->method() == INVITE);
               StackLog (<< "Timer::TimerCleanUp: " << *this << std::endl << *mNextTransmission);
               InfoLog(<<"Making 408 for canceled invite that received no response: "<< mNextTransmission->brief());
               if(mPendingOperation == Dns)
               {
                  WarningLog(<< "Transaction timed out while waiting for DNS "
                              "result uri=" << 
                              mNextTransmission->const_header(h_RequestLine).uri());
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
               }
               else
               {
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
               }
               terminateClientTransaction(msg->getTransactionId());
               delete this;
            }
            delete msg;
            break;

         default:
            delete msg;
            break;
      }
   }
   else if (isTransportError(msg))
   {
      processTransportFailure(msg);
      delete msg;
   }
   else if (isTcpConnectState(msg))
   {
       processTcpConnectState(msg);
       delete msg;
   }
   else if (isCancelClientTransaction(msg))
   {
      // TU wants to CANCEL this transaction. See if we can...
      if(mState==Proceeding)
      {
         // We can send the CANCEL now.
         SipMessage* cancel=Helper::makeCancel(*mNextTransmission);
         mNextTransmission->copyOutboundDecoratorsToStackCancel(*cancel);
         TransactionState::handleInternalCancel(cancel, *this);
      }
      else if(mState==Calling)
      {
         // We can't send the CANCEL yet, remember to.
         mIsAbandoned = true;
      }
      delete msg;
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else
   {
      //StackLog ( << "TransactionState::processClientInvite: message unhandled");
      delete msg;
   }
}

void
TransactionState::processServerNonInvite(TransactionMessage* msg)
{
   StackLog (<< "TransactionState::processServerNonInvite: " << msg->brief());

   if (isRequest(msg) && !isInvite(msg) && isFromWire(msg)) // retransmission from the wire
   {
      if (mState == Trying)
      {
         // ignore
         delete msg;
      }
      else if (mState == Proceeding || mState == Completed)
      {
         if(mIsAbandoned)
         {
            resip_assert(mState == Completed);
            mIsAbandoned=false;
            // put a 500 in mNextTransmission
            SipMessage* req = dynamic_cast<SipMessage*>(msg);
            resetNextTransmission(Helper::makeResponse(*req, 500));
            sendCurrentToWire();
         }
         else
         {
            // We have already sent a 100, but we have just received a retransmission.  Requests 
            // likely crossed on the wire.  We need to respond with another 100, but the last one was
            // cleared so re-create the 100 now. 
            SipMessage* sip = dynamic_cast<SipMessage*>(msg);
            if (sip && mMsgToRetransmit.empty() && !mNextTransmission)
            {
               resetNextTransmission(make100(sip));
            }
            sendCurrentToWire();
         }
         delete msg;
      }
      else
      {
         CritLog (<< "Fatal error in TransactionState::processServerNonInvite " 
                  << msg->brief()
                  << " state=" << *this);
         resip_assert(0);
         delete msg;
         return;
      }
   }
   else if (isResponse(msg) && isFromTU(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      int code = sip->const_header(h_StatusLine).responseCode();
      if (code >= 100 && code < 200) // 1XX
      {
         if (mState == Trying || mState == Proceeding)
         {
            resetNextTransmission(sip);
            mState = Proceeding;
            sendCurrentToWire(); // don't delete msg
         }
         else
         {
            // ignore
            delete msg;
         }
      }
      else if (code >= 200 && code <= 699)
      {
         if (mIsReliable)
         {
            resetNextTransmission(sip);
            sendCurrentToWire();
            terminateServerTransaction(mId);
            
            // !bwc! We can only do this because we are in a reliable
            // transport, and do not need to hang around to soak up 
            // retransmissions.
            delete this;
         }
         else
         {
            if (mState == Trying || mState == Proceeding)
            {
               mState = Completed;
               mController.mTimers.add(Timer::TimerJ, mId, 64*Timer::T1 );
               resetNextTransmission(sip);
               sendCurrentToWire();
            }
            else if (mState == Completed)
            {
               // ignore
               delete sip;
            }
            else
            {
               CritLog (<< "Fatal error in TransactionState::processServerNonInvite " 
                        << msg->brief()
                        << " state=" << *this);
               resip_assert(0);
               delete sip;
               return;
            }
         }
      }
      else
      {
         // ignore
         delete msg;
      }
   }
   else if (isTimer(msg))
   {
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      resip_assert(timer);
      switch (timer->getType())
      {
         case Timer::TimerJ:
            if (mState == Completed)
            {
               terminateServerTransaction(mId);
               delete this;
            }
            delete msg;
            break;

         case Timer::TimerTrying:
            if (mState == Trying)
            {
               // Timer E has reached T2 - send a 100 as recommended by RFC4320 NIT-Problem-Actions
               sendCurrentToWire();
               mState = Proceeding;
            }
            delete msg;
            break;

         default:
            delete msg;
            break;
      }
   }
   else if (isTransportError(msg))
   {
      // Failed to send response - transport has likely been removed
      WarningLog (<< "Failed to send response to server transaction (transport was likely removed)." << *this);
      delete msg;
      terminateServerTransaction(mId);
      delete this;
   }
   else if (isAbandonServerTransaction(msg))
   {
      if(mState==Trying || mState==Proceeding)
      {
         mIsAbandoned = true;

         // !bwc! We could check to see if we have a 100 lying around, and 
         // convert it into a 500 for immediate transmission, but it is not 
         // clear that this is a good idea, especially if the TU has abandoned 
         // this transaction after the remote endpoint has stopped 
         // retransmitting. Maybe we could use a time-stamp to help here? Would
         // it be worth the extra memory footprint?

         if (mIsReliable)
         {
            // If we haven't sent a 500 yet, we never will (no retransmissions 
            // to make the response with).
            terminateServerTransaction(mId);
            delete this;
         }
         else
         {
            // If we haven't sent a 500 yet, we'll do so when the next
            // retransmission comes in. In the meantime, set up timers for
            // transaction termination.
            mState = Completed;
            mController.mTimers.add(Timer::TimerJ, mId, 64*Timer::T1 );
         }
      }
      delete msg;
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else
   {
      //StackLog (<< "TransactionState::processServerNonInvite: message unhandled");
      delete msg;
   }
}


void
TransactionState::processServerInvite(TransactionMessage* msg)
{
   StackLog (<< "TransactionState::processServerInvite: " << msg->brief());
   if (isRequest(msg) && isFromWire(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      switch (sip->method())
      {
         case INVITE:
            // note: handling of initial INVITE message is done in TransactionState:process
            if(mIsAbandoned)
            {
               mIsAbandoned=false;
               mAckIsValid=true;
               resetNextTransmission(Helper::makeResponse(*sip, 500));
               mState = Completed;
               mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
               if (!mIsReliable)
               {
                  mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
               }
               sendCurrentToWire();
               delete msg;
               return;
            }

            if (mState == Proceeding || mState == Completed)
            {
               /*
                 The server transaction has already been constructed so this
                 message is a retransmission.  The server transaction must
                 respond with a 100 Trying _or_ the last provisional response
                 passed from the TU for this transaction.
               */
               //StackLog (<< "Received invite from wire - forwarding to TU state=" << mState);
               
               // !bwc! If we have nothing to respond with, make something.
               if (mMsgToRetransmit.empty() && !mNextTransmission)
               {
                  resetNextTransmission(make100(sip));
               }
               delete sip;
               sendCurrentToWire();
            }
            else
            {
               //StackLog (<< "Received invite from wire - ignoring state=" << mState);
               delete msg;
            }
            break;
            
         case ACK:
            /*
              If an ACK is received while the server transaction is in the
              "Completed" state, the server transaction MUST transition to the
              "Confirmed" state.
            */
            if (mState == Completed)
            {
               if (mIsReliable)
               {
                  //StackLog (<< "Received ACK in Completed (reliable) - delete transaction");
                  terminateServerTransaction(mId);
                  delete this; 
                  delete msg;
               }
               else
               {
                  //StackLog (<< "Received ACK in Completed (unreliable) - confirmed, start Timer I");
                  mState = Confirmed;
                  mController.mTimers.add(Timer::TimerI, mId, Timer::T4 );
                  // !bwc! Got an ACK/failure; we can stop retransmitting
                  // our failure response now.
                  resetNextTransmission(0);
                  delete sip;
               }
            }
            else
            {
               //StackLog (<< "Ignore ACK not in Completed state");
               delete msg;
            }
            break;

         case CANCEL:
            resip_assert(0);
            delete sip;
            break;

         default:
            //StackLog (<< "Received unexpected request. Ignoring message");
            delete msg;
            break;
      }
   }
   else if (isResponse(msg, 100, 699) && isFromTU(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      int code = sip->const_header(h_StatusLine).responseCode();
      switch (sip->method())
      {
         case INVITE:
            if (code == 100)
            {
               if (mState == Trying || mState == Proceeding)
               {
                  //StackLog (<< "Received 100 in Trying or Proceeding. Send over wire");
                  resetNextTransmission(sip); // may be replacing the 100
                  mState = Proceeding;
                  sendCurrentToWire(); // don't delete msg
               }
               else
               {
                  //StackLog (<< "Ignoring 100 - not in Trying or Proceeding.");
                  delete msg;
               }
            }
            else if (code > 100 && code < 200)
            {
               if (mState == Trying || mState == Proceeding)
               {
                  //StackLog (<< "Received 1xx in Trying or Proceeding. Send over wire");
                  resetNextTransmission(sip); // may be replacing the 100
                  mState = Proceeding;
                  sendCurrentToWire(); // don't delete msg
               }
               else
               {
                  //StackLog (<< "Received 100 when not in Trying State. Ignoring");
                  delete msg;
               }
            }
            else if (code >= 200 && code < 300)
            {
               if (mState == Trying || mState == Proceeding)
               {
                  StackLog (<< "Received 2xx when in Trying or Proceeding State of server invite transaction");
                  StackLog (<< *this);
                  resetNextTransmission(sip); // may be replacing the 100
                  sendCurrentToWire();
                  
                  // Keep the StaleServer transaction around, so we can keep the
                  // source Tuple that the request was received on. 
                  //terminateServerTransaction(mId);
                  mMachine = ServerStale;
                  mController.mTimers.add(Timer::TimerStaleServer, mId, Timer::TS );
               }
               else
               {
                  //StackLog (<< "Received 2xx when not in Trying or Proceeding State. Ignoring");
                  delete msg;
               }
            }
            else if (code >= 300)
            {
               /*
                 While in the "Proceeding" state, if the TU passes a response with
                 status code from 300 to 699 to the server transaction, For unreliable 
                 transports,timer G is set to fire in T1 seconds, and is not set to 
                 fire for reliable transports.when the "Completed" state is entered, 
                 timer H MUST be set to fire in 64*T1 seconds for all transports.  
                 Timer H determines when the server transaction abandons retransmitting 
                 the response
               */

               if (mState == Trying || mState == Proceeding)
               {
                  mAckIsValid=true;
                  StackLog (<< "Received failed response in Trying or Proceeding. Start Timer H, move to completed." << *this);
                  resetNextTransmission(sip);
                  mState = Completed;
                  mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
                  if (!mIsReliable)
                  {
                     mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
                  }
                  sendCurrentToWire(); // don't delete msg
               }
               else
               {
                  //StackLog (<< "Received Final response when not in Trying or Proceeding State. Ignoring");
                  delete msg;
               }
            }
            else
            {
               //StackLog (<< "Received Invalid response line. Ignoring");
               delete msg;
            }
            break;
            
         case CANCEL:
            resip_assert(0);
            delete sip;
            break;
            
         default:
            //StackLog (<< "Received response to non invite or cancel. Ignoring");
            delete msg;
            break;
      }
   }
   else if (isTimer(msg))
   {
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      switch (timer->getType())
      {
         case Timer::TimerG:
            if (mState == Completed)
            {
               StackLog (<< "TimerG fired. retransmit, and re-add TimerG");
               sendCurrentToWire();
               mController.mTimers.add(Timer::TimerG, mId, resipMin(Timer::T2, timer->getDuration()*2) );  //  TimerG is supposed to double - up until a max of T2 RFC3261 17.2.1
            }
            break;

            /*
              If timer H fires while in the "Completed" state, it implies that the
              ACK was never received.  In this case, the server transaction MUST
              transition to the "Terminated" state, and MUST indicate to the TU
              that a transaction failure has occurred. WHY we need to inform TU
              for Failure cases ACK ? do we really need to do this ???       

              !jf! this used to re-add TimerH if there was an associated CANCEL
              transaction. Don't know why. 
            */
         case Timer::TimerH:
         case Timer::TimerI:
            if (timer->getType() == Timer::TimerH)
            {
               InfoLog (<< "No ACK was received on a server transaction (Timer H)");
            }
            terminateServerTransaction(mId);
            delete this;
            break;

         case Timer::TimerTrying:
            if (mState == Trying)
            {
               //StackLog (<< "TimerTrying fired. Send a 100");
               sendCurrentToWire(); // will get deleted when this is deleted
               mState = Proceeding;
            }
            else
            {
               //StackLog (<< "TimerTrying fired. Not in Trying state. Ignoring");
            }
            break;
            
         default:
            CritLog(<<"unexpected timer fired: " << timer->getType());
            resip_assert(0); // programming error if any other timer fires
            break;
      }
      delete timer;
   }
   else if (isTransportError(msg))
   {
      // Failed to send response - transport has likely been removed
      WarningLog (<< "Failed to send response to server transaction (transport was likely removed)." << *this);
      delete msg;
      terminateServerTransaction(mId);
      delete this;
   }
   else if (isAbandonServerTransaction(msg))
   {
      if((mState == Trying || mState == Proceeding) && !mIsAbandoned)
      {
         // We need to schedule teardown, and 500 the next retransmission.
         if(mNextTransmission)
         {
            mMsgToRetransmit.clear();
            // hey, we had a 1xx laying around! Turn it into a 500 and send.
            resip_assert(mNextTransmission->isResponse());
            resip_assert(mNextTransmission->const_header(h_StatusLine).statusCode()/100==1);
            mNextTransmission->header(h_StatusLine).statusCode()=500;
            mNextTransmission->header(h_StatusLine).reason()="Server Error";
            sendCurrentToWire();
            mAckIsValid=true;
            StackLog (<< "Received failed response in Trying or Proceeding. Start Timer H, move to completed." << *this);
            mState = Completed;
            mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
            if (!mIsReliable)
            {
               mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
            }
         }
         else
         {
            // !bwc! TODO try to convert mMsgToRetransmit if present.
            if(mIsReliable)
            {
               // We will never see another retransmission of the INVITE. We
               // need to bail.
               terminateServerTransaction(mId);
               delete this;
            }
            else
            {
               // We should see a retransmission of the INVITE shortly, or we
               // will time out eventually. Be patient...
               mIsAbandoned = true;
            }
         }
      }
      delete msg;
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else
   {
      //StackLog (<< "TransactionState::processServerInvite: message unhandled");
      delete msg;
   }
}


void
TransactionState::processClientStale(TransactionMessage* msg)
{
   StackLog (<< "TransactionState::processClientStale: " << msg->brief());

   if (isTimer(msg))
   {
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      if (timer->getType() == Timer::TimerStaleClient)
      {
         terminateClientTransaction(mId);
         delete this;
         delete msg;
      }
      else
      {
         delete msg;
      }
   }
   else if (isTransportError(msg))
   {
      WarningLog (<< "Got a transport error in Stale Client state");
      StackLog (<< *this);
      processTransportFailure(msg);
      delete msg;
   }
   else if(isResponse(msg, 200, 299))
   {
      resip_assert(isFromWire(msg));
      sendToTU(msg);
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else if (isAbandonServerTransaction(msg))
   {
      // ?
      delete msg;
   }
   else if (isCancelClientTransaction(msg))
   {
      // ?
      delete msg;
   }
   else
   {
      // might have received some other response because a downstream UAS is
      // misbehaving. For instance, sending a 487/INVITE after already
      // sending a 200/INVITE. It could also be some other message type.
      StackLog (<< "Discarding extra message: " << *msg);
      delete msg;
   }
}

void
TransactionState::processServerStale(TransactionMessage* msg)
{
   StackLog (<< "TransactionState::processServerStale: " << msg->brief());

   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
   if (isTimer(msg))
   {
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
      if (timer->getType() == Timer::TimerStaleServer)
      {
         delete msg;
         terminateServerTransaction(mId);
         delete this;
      }
      else
      {
         delete msg;
      }
   }
   else if (isTransportError(msg))
   {
      WarningLog (<< "Got a transport error in Stale Server state");
      StackLog (<< *this);
      processTransportFailure(msg);
      delete msg;
   }
   else if (sip && isRequest(sip) && sip->method() == ACK)
   {
      // .bwc. We should never fall into this block. There is code in process
      // that should prevent it.
      resip_assert(isFromWire(msg));
      InfoLog (<< "Passing ACK directly to TU: " << sip->brief());
      sendToTU(msg);
   }
   else if (sip && isRequest(sip) && sip->method() == INVITE)
   {
      // this can happen when an upstream UAC never received the 200 and
      // retransmits the INVITE when using unreliable transport
      // Drop the INVITE since the 200 will get retransmitted by the downstream UAS
      StackLog (<< "Dropping retransmitted INVITE in stale server transaction" << sip->brief());
      delete msg;
   }
   else if (isResponse(msg) && isFromTU(msg))
   {
      resetNextTransmission(sip);
      sendCurrentToWire(); 
   }
   else if(dynamic_cast<DnsResultMessage*>(msg))
   {
      handleSync(mDnsResult);
      delete msg;
   }
   else if (isAbandonServerTransaction(msg))
   {
      // ?
      delete msg;
   }
   else
   {
      // .bwc. This can very easily be triggered by a stupid/malicious 
      // endpoint. This is not an error in our code. Do not ErrLog this.
      InfoLog(<<"ServerStale unexpected condition, dropping message.");
      if (sip)
      {
         InfoLog(<<sip->brief());
      }
      delete msg;
   }
}

void
TransactionState::processNoDnsResults()
{
   if(!mNextTransmission || mNextTransmission->method()==ACK)
   {
      // This is probably an ACK; since we know we will never need to send a 
      // response to an ACK, we delete mNextTransmission as soon as we 
      // serialize it.
      return;
   }

   WarningCategory warning;
   SipMessage* response = Helper::makeResponse(*mNextTransmission, 503);
   warning.hostname() = mController.mHostname;
   warning.code() = 399;
   warning.text().reserve(100);

   if(mDnsResult)
   {
      InfoLog (<< "Ran out of dns entries for " << mDnsResult->target() << ". Send 503");
      resip_assert(mDnsResult->available() == DnsResult::Finished);
      oDataStream warnText(warning.text());
      warnText << "No other DNS entries to try ("
               << mFailureReason << "," << mFailureSubCode << ")";
   }
   else
   {
      oDataStream warnText(warning.text());
      warnText << "Transport failure ("
               << mFailureReason << "," << mFailureSubCode << ")";
   }

   switch(mFailureReason)
   {
      case TransportFailure::None:
         response->header(h_StatusLine).reason() = "No DNS results";
         break;

      case TransportFailure::Failure:
      case TransportFailure::TransportNoSocket:
      case TransportFailure::TransportBadConnect:
      case TransportFailure::ConnectionUnknown:
      case TransportFailure::ConnectionException:
         response->header(h_StatusLine).reason() = "Transport failure: no transports left to try";
         break;
      case TransportFailure::NoTransport:
         response->header(h_StatusLine).reason() = "No matching transport found";
         break;
      case TransportFailure::NoRoute:
         response->header(h_StatusLine).reason() = "No route to host";
         break;
      case TransportFailure::CertNameMismatch:
         response->header(h_StatusLine).reason() = "Certificate Name Mismatch";
         break;
      case TransportFailure::CertValidationFailure:
         response->header(h_StatusLine).reason() = "Certificate Validation Failure";
         break;
      case TransportFailure::TransportNoExistConn:
         if(InteropHelper::getOutboundVersion() >= 5)
         {
            response->header(h_StatusLine).statusCode() = 430;
         }
         else
         {
            response->header(h_StatusLine).statusCode() = 410;
         }
         response->header(h_StatusLine).reason() = "Flow failed";
         warning.text() = "Flow no longer exists";
         break;
      case TransportFailure::TransportShutdown:
         response->header(h_StatusLine).reason() = "Transport shutdown: no transports left to try";
         break;
   }

   response->header(h_Warnings).push_back(warning);

   sendToTU(response); 
   terminateClientTransaction(mId);
   if (mMachine != Stateless)
   {
      delete this;
   }
}

void
TransactionState::processTransportFailure(TransactionMessage* msg)
{
   TransportFailure* failure = dynamic_cast<TransportFailure*>(msg);
   resip_assert(failure);
   resip_assert(mState!=Bogus);

   // We come here if the tcp connect timer expires, so we reset the flag incase we are
   // going to try another DNS entry that is also TCP.
   mTcpConnectTimerStarted = false;

   // Store failure reasons
   if (failure->getFailureReason() > mFailureReason)
   {
      mFailureReason = failure->getFailureReason();
      mFailureSubCode = failure->getFailureSubCode();
   }

   if (mNextTransmission &&  // Note:  If we just transmitted an ACK then mNextTransmission is cleared, so this check is necessary
       mNextTransmission->isRequest() && 
       mNextTransmission->method() == CANCEL &&
       mState != Completed && 
       mState != Terminated)
   {
      WarningLog (<< "Failed to deliver a CANCEL request");
      StackLog (<< *this);
      resip_assert(mMethod==CANCEL);

      // In the case of a client-initiated CANCEL, we don't want to
      // try other transports in the case of transport error as the
      // CANCEL MUST be sent to the same IP/PORT as the orig. INVITE.
      //?dcm? insepct failure enum?
      SipMessage* response = Helper::makeResponse(*mNextTransmission, 503);
      WarningCategory warning;
      warning.hostname() = mController.mHostname;
      warning.code() = 399;
      warning.text() = "Failed to deliver CANCEL using the same transport as the INVITE was used";
      response->header(h_Warnings).push_back(warning);
         
      sendToTU(response);
      return;
   }

   if(!mDnsResult)
   {
      InfoLog(<< "Transport failure on send that did not use DNS.");
      processNoDnsResults();
   }
   // else If we did DNS resolution, then check if we should try to failover to another DNS entry
   else if(mDnsResult)
   {
      // .bwc. Greylist for 32s
      // !bwc! TODO make this duration configurable.
      mDnsResult->greylistLast(Timer::getTimeMs()+32000);

      // .bwc. We should only try multiple dns results if we are originating a
      // request. Additionally, there are (potential) cases where it would not
      // be appropriate to fail over even then.
      bool shouldFailover=false;
      if(mMachine==ClientNonInvite)
      {
         if(mState==Completed || mState==Terminated)
         {
            WarningLog(<<"Got a TransportFailure message in a " << mState <<
                         " ClientNonInvite transaction. How did this happen? Since we have"
                         " already completed the transaction, we shouldn't try"
                         " additional DNS results.");
         }
         else
         {
            shouldFailover=true;
         }
      }
      else if(mMachine==ClientInvite)
      {
         if(mState==Completed || mState==Terminated)
         {
            // .bwc. Perhaps the attempted transmission of the ACK failed here.
            // (assuming this transaction got a failure response; not sure what
            // might have happened if this is not the case)
            // In any case, we should not try sending the INVITE anywhere else.
            InfoLog(<<"Got a TransportFailure message in a " << mState <<
                      " ClientInvite transaction. Since we have"
                      " already completed the transaction, we shouldn't try"
                      " additional DNS results.");
         }
         else
         {
            if(mState==Proceeding)
            {
               // .bwc. We need to revert our state back to Calling, since we are
               // going to be sending the INVITE to a new endpoint entirely.

               // !bwc!
               // An interesting consequence occurs if our failover ultimately
               // sends to the same instance of a resip stack; we increment the 
               // transport sequence in our branch parameter, but any resip-based
               // stack will ignore this change, and process this "new" request as
               // a retransmission! Furthermore, our state will be out of phase
               // with the state at the remote endpoint, and if we have sent a
               // PRACK, it will know (and stuff will break)!
               // TODO What else needs to be done here to safely revert our state?
               mState=Calling;
            }
            shouldFailover=true;
         }
      }
   
      if(shouldFailover)
      {
         InfoLog (<< "Try sending request to a different dns result");
         resip_assert(mMethod!=CANCEL);

         switch (mDnsResult->available())
         {
            case DnsResult::Available:
               InfoLog(<< "We have another DNS result to try.");
               restoreOriginalContactAndVia();
               mTarget = mDnsResult->next();
               mMsgToRetransmit.clear();
               processReliability(mTarget.getType());
               sendCurrentToWire();
               break;
            
            case DnsResult::Pending:
               InfoLog(<< "We have a DNS query pending.");
               mPendingOperation=Dns;
               restoreOriginalContactAndVia();
               mMsgToRetransmit.clear();
               break;

            case DnsResult::Finished:
               InfoLog(<< "No DNS results remain.");
               processNoDnsResults();
               break;

            case DnsResult::Destroyed:
            default:
               InfoLog (<< "Bad state: " << *this);
               resip_assert(0);
         }
      }
      else
      {
         InfoLog(<< "Transport failure on send, and failover is disabled.");
         processNoDnsResults();
      }
   }
}

void 
TransactionState::processTcpConnectState(TransactionMessage* msg)
{
   TcpConnectState* tcpConnectState = dynamic_cast<TcpConnectState*>(msg);
   resip_assert(tcpConnectState);

   if (tcpConnectState->getState() == TcpConnectState::ConnectStarted &&
       !mTcpConnectTimerStarted && Timer::TcpConnectTimeout != 0 &&
       (mState == Trying || mState == Calling))
   {
      // Start Timer
      mController.mTimers.add(Timer::TcpConnectTimer, mId, Timer::TcpConnectTimeout);
      mTcpConnectTimerStarted = true;
   }
   else if (tcpConnectState->getState() == TcpConnectState::Connected &&
       (mState == Trying || mState == Calling))
   {
      mTcpConnectTimerStarted = false;
   }
}

// called by DnsResult
void
TransactionState::rewriteRequest(const Uri& rewrite)
{
   // !bwc! TODO We need to address the race-conditions caused by callbacks
   // into a class whose thread-safety is accomplished through message-passing.
   // This function could very easily be called while other processing is
   // taking place due to a message from the state-machine fifo. In the end, I
   // imagine that we will need to have the callback place a message onto the
   // queue, and move all the code below into a function that handles that
   // message.

   resip_assert(mNextTransmission->isRequest());
   if (mNextTransmission->const_header(h_RequestLine).uri() != rewrite)
   {
      InfoLog (<< "Rewriting request-uri to " << rewrite);
      mNextTransmission->header(h_RequestLine).uri() = rewrite;
      // !bwc! Changing mNextTransmission invalidates mMsgToRetransmit.
      mMsgToRetransmit.clear();
   }
}

void
TransactionState::handle(DnsResult* result)
{
   // ?bwc? Maybe optmize this to use handleSync() directly when running in
   // single-threaded mode?
   DnsResultMessage* dns = new DnsResultMessage(mId,isClient());
   mController.mStateMacFifo.add(static_cast<TransactionMessage*>(dns));
}

void
TransactionState::handleSync(DnsResult* result)  // !slg! it is strange that we pass the result in here, then more or less ignore it in the method
{
   StackLog (<< *this << " got DNS result: " << *result);
   
   // .bwc. Were we expecting something from mDnsResult?
   if (mPendingOperation == Dns)
   {
      resip_assert(mDnsResult);
      switch (mDnsResult->available())
      {
         case DnsResult::Available:
            mPendingOperation=None;
            mTarget = mDnsResult->next();
            // below allows TU to know which transport we send on
            // (The Via mechanism for setting transport doesn't work for TLS)
            mTarget.mTransportKey = mNextTransmission->getDestination().mTransportKey;
            processReliability(mTarget.getType());
            sendCurrentToWire();
            break;
            
         case DnsResult::Finished:
            mPendingOperation=None;
            processNoDnsResults();
            break;

         case DnsResult::Pending:
            break;
            
         case DnsResult::Destroyed:
         default:
            resip_assert(0);
            break;
      }
   }
}

void
TransactionState::processReliability(TransportType type)
{
   switch (type)
   {
      case UDP:
      case DCCP:
         if (mIsReliable)
         {
            mIsReliable = false;
            StackLog (<< "Unreliable transport: " << *this);
            switch (mMachine)
            {
               case ClientNonInvite:
                  mController.mTimers.add(Timer::TimerE1, mId, Timer::T1 );
                  break;
                  
               case ClientInvite:
                  mController.mTimers.add(Timer::TimerA, mId, Timer::T1 );
                  break;

               default:
                  break;
            }
         }
         break;
         
      default:
         if (!mIsReliable)
         {
            mIsReliable = true;
         }
         break;
   }
}

// !ah! only used one place, so leaving it here instead of making a helper.
// !ah! broken out for clarity -- only used for forceTargets.
// Expects that host portion is IP address notation.

static const Tuple
simpleTupleForUri(const Uri& uri)
{
   const Data& host = uri.host();
   int port = uri.port();

   resip::TransportType transport = UNKNOWN_TRANSPORT;
 
  if (uri.exists(p_transport))
   {
      transport = Tuple::toTransport(uri.param(p_transport));
   }

   if (transport == UNKNOWN_TRANSPORT)
   {
      transport = UDP;
   }
   if (port == 0)
   {
      switch(transport)
      {
         case TLS:
            port = Symbols::DefaultSipsPort;
            break;
         case UDP:
         case TCP:
         default:
            port = Symbols::DefaultSipPort;
            break;
         // !ah! SCTP?

      }
   }

   return Tuple(host,port,transport);
}

void
TransactionState::sendCurrentToWire() 
{
   if(!mMsgToRetransmit.empty())
   {
      if(mController.mStack.statisticsManagerEnabled())
      {
         mController.mStatsManager.retransmitted(mCurrentMethodType, 
                                                   isClient(), 
                                                   mCurrentResponseCode);
      }

      mController.mTransportSelector.retransmit(mMsgToRetransmit);
   }
   else if(mNextTransmission) // initial transmission; need to determine target
   {
      SipMessage* sip=mNextTransmission;
      TransportSelector::TransmitState transmitState = TransportSelector::Unsent;

      if(isClient())
      {
         if(mTarget.getType() != UNKNOWN_TRANSPORT) // mTarget is set, so just send.
         {
            transmitState=mController.mTransportSelector.transmit(
                        sip, 
                        mTarget,
                        mIsReliable ? 0 : &mMsgToRetransmit);
         }
         else // mTarget isn't set...
         {
            if (sip->getDestination().mFlowKey) //...but sip->getDestination() will work
            {
               // ?bwc? Maybe we should be nice to the TU and do DNS in this case?
               resip_assert(sip->getDestination().getType() != UNKNOWN_TRANSPORT);

               // .bwc. We have the FlowKey. This completely specifies our 
               // Transport (and Connection, if applicable). No DNS required.
               DebugLog(<< "Sending to tuple: " << sip->getDestination());
               mTarget = sip->getDestination();
               processReliability(mTarget.getType());
               transmitState=mController.mTransportSelector.transmit(
                           sip, 
                           mTarget,
                           mIsReliable ? 0 : &mMsgToRetransmit);
            }
            else // ...so DNS is required...
            {
               if(mDnsResult == 0) // ... and we haven't started a DNS query yet.
               {
                  StackLog (<< "sendToWire with no dns result: " << *this);
                  resip_assert(sip->isRequest());
                  resip_assert(mMethod!=CANCEL); // .bwc. mTarget should be set in this case.
                  mDnsResult = mController.mTransportSelector.createDnsResult(this);
                  mPendingOperation=Dns;
                  mController.mTransportSelector.dnsResolve(mDnsResult, sip);
               }
               else // ... but our DNS query isn't done yet.
               {
                  // .bwc. While the resolver was attempting to find a target, another
                  // request came down from the TU. This could be a bug in the TU, or 
                  // could be a retransmission of an ACK/200. Either way, we cannot
                  // expect to ever be able to send this request (nowhere to store it
                  // temporarily).
                  // ?bwc? Higher log-level?
                  DebugLog(<< "Received a second request from the TU for a transaction"
                           " that already existed, before the DNS subsystem was done "
                           "resolving the target for the first request. Either the TU"
                           " has messed up, or it is retransmitting ACK/200 (the only"
                           " valid case for this to happen)");
               }
            }
         }
      }
      else // server transaction
      {
         resip_assert(mDnsResult == 0);
         resip_assert(sip->exists(h_Vias));
         resip_assert(!sip->const_header(h_Vias).empty());

         // .bwc. Code that tweaks mResponseTarget based on stuff in the SipMessage.
         // ?bwc? Why?
         if (sip->hasForceTarget())
         {
            // ?bwc? Override the target for a single response? Should we even
            // allow this? What about client transactions? Should we overwrite 
            // mResponseTarget here? I don't think this has been thought out properly.
            Tuple target = simpleTupleForUri(sip->getForceTarget());
            StackLog(<<"!ah! response with force target going to : "<<target);
            transmitState=mController.mTransportSelector.transmit(
                        sip, 
                        target,
                        mIsReliable ? 0 : &mMsgToRetransmit);
         }
         else
         {
            if (sip->const_header(h_Vias).front().exists(p_rport) && sip->const_header(h_Vias).front().param(p_rport).hasValue())
            {
               // ?bwc? This was not setting the port in mResponseTarget before. Why would
               // the rport be different than the port in mResponseTarget? Didn't we 
               // already set this? Maybe the TU messed with it? If so, why should we pay 
               // attention to it? Again, this hasn't been thought out.
               mResponseTarget.setPort(sip->const_header(h_Vias).front().param(p_rport).port());
               StackLog(<< "rport present in response: " << mResponseTarget.getPort());
            }
   
            StackLog(<< "tid=" << sip->getTransactionId() << " sending to : " << mResponseTarget);
            transmitState=mController.mTransportSelector.transmit(
                        sip, 
                        mResponseTarget,
                        mIsReliable ? 0 : &mMsgToRetransmit);
         }
      }

      // !bwc! If we don't have DNS results yet, or TransportSelector::transmit
      // fails, we hang on to the full original SipMessage, in the hope that 
      // next time it works.
      if (transmitState == TransportSelector::Sent)
      {
         onSendSuccess();
      }
   }
   else
   {
      resip_assert(0);
   }
}

void
TransactionState::onSendSuccess()
{
   SipMessage* sip=mNextTransmission;

   if(mController.mStack.statisticsManagerEnabled())
   {
      mController.mStatsManager.sent(sip);
   }

   mCurrentMethodType = sip->method();
   if(sip->isResponse())
   {
      mCurrentResponseCode = sip->const_header(h_StatusLine).statusCode();
   }

   // !bwc! If mNextTransmission is a non-ACK request, we need to save the
   // initial request in case we need to send a simulated 408 or a 503 to
   // the TU (at least, until we get a response back)
   if(!mNextTransmission->isRequest() || mNextTransmission->method()==ACK)
   {
      delete mNextTransmission;
      mNextTransmission=0;
   }
}

void
TransactionState::sendToTU(TransactionMessage* msg)
{
   SipMessage* sipMsg = dynamic_cast<SipMessage*>(msg);
   if (sipMsg && sipMsg->isResponse() && mDnsResult)
   {
      // whitelisting rules.
      switch (sipMsg->const_header(h_StatusLine).statusCode())
      {
         case 503:
            // blacklist last target.
            // .bwc. If there is no Retry-After, we do not blacklist
            // (see RFC 3261 sec 21.5.4 para 1)
            if(sipMsg->exists(resip::h_RetryAfter) && 
               sipMsg->const_header(resip::h_RetryAfter).isWellFormed())
            {
               unsigned int relativeExpiry= sipMsg->const_header(resip::h_RetryAfter).value();
               
               if(relativeExpiry!=0)
               {
                  mDnsResult->blacklistLast(resip::Timer::getTimeMs()+relativeExpiry*1000);
               }
            }
            
            break;
         case 408:
            if(!sipMsg->isFromWire() && (mState == Trying || mState==Calling))  // only greylist if internally generated and we haven't received any responses yet
            {
               // greylist last target.
               // ?bwc? How long do we greylist this for? Probably should make
               // this configurable. TODO
               mDnsResult->greylistLast(resip::Timer::getTimeMs() + 32000);
            }

            break;
         default:
            // !bwc! Debatable.
            mDnsResult->whitelistLast();
            break;
      }
   }

   CongestionManager::RejectionBehavior behavior=CongestionManager::NORMAL;
   behavior=mController.mTuSelector.getRejectionBehavior(mTransactionUser);

   if(behavior!=CongestionManager::NORMAL)
   {
      if(sipMsg)
      {
         resip_assert(sipMsg->isExternal());
         if(sipMsg->isRequest())
         {
            // .bwc. This could be an initial request, or an ACK/200.
            if(sipMsg->method()==ACK)
            {
               // ACK/200 is a continuation of old work. We only reject if
               // we're really hosed.
               if(behavior==CongestionManager::REJECTING_NON_ESSENTIAL)
               {
                  delete msg;
                  return;
               }
            }
            else
            {
               // .bwc. This is new work. Reject.
               SipMessage* response(Helper::makeResponse(*sipMsg, 503));
               delete sipMsg;
               
               UInt16 retryAfter=mController.mTuSelector.getExpectedWait(mTransactionUser);
               response->header(h_RetryAfter).value()=retryAfter;
               response->setFromTU();
               if(mMethod==INVITE)
               {
                  processServerInvite(response);
               }
               else
               {
                  processServerNonInvite(response);
               }
               return;
            }
         }
         else
         {
            // .bwc. This could be a response from the wire, or an internally
            // generated pseudo-response. This is always a continuation of
            // old work.
            if(behavior==CongestionManager::REJECTING_NON_ESSENTIAL &&
               mTransactionUser &&
               !mTransactionUser->responsesMandatory())
            {
               delete sipMsg;
               return;
            }
         }
      }
      else
      {
         // .bwc. This is some sort of timer, or other message. If we don't know 
         // any better, we need to assume this is essential for the safe
         // operation of the TU.
      }
   }
   
   TransactionState::sendToTU(mTransactionUser, mController, msg);
}

void
TransactionState::sendToTU(TransactionUser* tu, TransactionController& controller, TransactionMessage* msg) 
{   
   msg->setTransactionUser(tu);
   controller.mTuSelector.add(msg, TimeLimitFifo<Message>::InternalElement);
}

SipMessage*
TransactionState::make100(SipMessage* request) const
{
   return (Helper::makeResponse(*request, 100));
}

void
TransactionState::add(const Data& tid)
{
   if (isClient())
   {
      mController.mClientTransactionMap.add(tid, this);
   }
   else
   {
      mController.mServerTransactionMap.add(tid, this);
   }
}

void
TransactionState::erase(const Data& tid)
{
   if (isClient())
   {
      mController.mClientTransactionMap.erase(tid);
   }
   else
   {
      mController.mServerTransactionMap.erase(tid);
   }
}

bool
TransactionState::isRequest(TransactionMessage* msg) const
{
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);   
   return sip && sip->isRequest();
}

bool
TransactionState::isInvite(TransactionMessage* msg) const
{
   if (isRequest(msg))
   {
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
      return (sip->method()) == INVITE;
   }
   return false;
}

bool
TransactionState::isResponse(TransactionMessage* msg, int lower, int upper) const
{
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
   if (sip && sip->isResponse())
   {
      int c = sip->const_header(h_StatusLine).responseCode();
      return (c >= lower && c <= upper);
   }
   return false;
}

bool
TransactionState::isTimer(TransactionMessage* msg) const
{
   return dynamic_cast<TimerMessage*>(msg) != 0;
}

bool
TransactionState::isFromTU(TransactionMessage* msg) const
{
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
   return sip && !sip->isExternal();
}

bool
TransactionState::isFromWire(TransactionMessage* msg) const
{
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
   return sip && sip->isExternal();
}

bool
TransactionState::isTransportError(TransactionMessage* msg) const
{
   return dynamic_cast<TransportFailure*>(msg) != 0;
}

bool
TransactionState::isTcpConnectState(TransactionMessage* msg) const
{
    return dynamic_cast<TcpConnectState*>(msg) != 0;
}

bool 
TransactionState::isAbandonServerTransaction(TransactionMessage* msg) const
{
   return dynamic_cast<AbandonServerTransaction*>(msg) != 0;
}

bool 
TransactionState::isCancelClientTransaction(TransactionMessage* msg) const
{
   return dynamic_cast<CancelClientInviteTransaction*>(msg) != 0;
}


const Data&
TransactionState::tid(SipMessage* sip) const
{
   resip_assert(0);
   resip_assert (mMachine != Stateless || (mMachine == Stateless && !mId.empty()));
   resip_assert (mMachine == Stateless || (mMachine != Stateless && sip));
   return (mId.empty() && sip) ? sip->getTransactionId() : mId;
}

void
TransactionState::terminateClientTransaction(const Data& tid)
{
   mState = Terminated;
   if (mController.mTuSelector.isTransactionUserStillRegistered(mTransactionUser) && 
       mTransactionUser->isRegisteredForTransactionTermination())
   {
      //StackLog (<< "Terminate client transaction " << tid);
      sendToTU(new TransactionTerminated(tid, true, mTransactionUser));
   }
}

void
TransactionState::terminateServerTransaction(const Data& tid)
{
   mState = Terminated;
   if (mController.mTuSelector.isTransactionUserStillRegistered(mTransactionUser) && 
       mTransactionUser->isRegisteredForTransactionTermination())
   {
      //StackLog (<< "Terminate server transaction " << tid);
      sendToTU(new TransactionTerminated(tid, false, mTransactionUser));
   }
}

bool 
TransactionState::isClient() const
{
   switch(mMachine)
   {
      case ClientNonInvite:
      case ClientInvite:
      case ClientStale:
      case Stateless:
         return true;
      case ServerNonInvite:
      case ServerInvite:
      case ServerStale:
         return false;
      default:
         resip_assert(0);
   }
   return false;
}

EncodeStream& 
resip::operator<<(EncodeStream& strm, const resip::TransactionState& state)
{
   strm << "tid=" << state.mId << " [ ";
   switch (state.mMachine)
   {
      case TransactionState::ClientNonInvite:
         strm << "ClientNonInvite";
         break;
      case TransactionState::ClientInvite:
         strm << "ClientInvite";
         break;
      case TransactionState::ServerNonInvite:
         strm << "ServerNonInvite";
         break;
      case TransactionState::ServerInvite:
         strm << "ServerInvite";
         break;
      case TransactionState::Stateless:
         strm << "Stateless";
         break;
      case TransactionState::ClientStale:
         strm << "ClientStale";
         break;
      case TransactionState::ServerStale:
         strm << "ServerStale";
         break;
   }
   
   strm << "/";
   switch (state.mState)
   {
      case TransactionState::Calling:
         strm << "Calling";
         break;
      case TransactionState::Trying:
         strm << "Trying";
         break;
      case TransactionState::Proceeding:
         strm << "Proceeding";
         break;
      case TransactionState::Completed:
         strm << "Completed";
         break;
      case TransactionState::Confirmed:
         strm << "Confirmed";
         break;
      case TransactionState::Terminated:
         strm << "Terminated";
         break;
      case TransactionState::Bogus:
         strm << "Bogus";
         break;
   }
   
   strm << (state.mIsReliable ? " reliable" : " unreliable");
   strm << " target=" << state.mResponseTarget;
   //if (state.mTransactionUser) strm << " tu=" << *state.mTransactionUser;
   //else strm << "default TU";
   strm << "]";
   return strm;
}


/* Local Variables: */
/* c-file-style: "ellemtel" */
/* End: */

/* ====================================================================
 * 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:
 */