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
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
|
/*
Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <assert.h>
#include <iostream>
#include "call_history.h"
#include "call_script.h"
#include "dialog.h"
#include "exceptions.h"
#include "line.h"
#include "log.h"
#include "phone_user.h"
#include "sub_refer.h"
#include "util.h"
#include "userintf.h"
#include "audio/rtp_telephone_event.h"
#include "audits/memman.h"
#include "im/im_iscomposing_body.h"
#include "sdp/sdp.h"
#include "sockets/socket.h"
#include "stun/stun_transaction.h"
extern t_event_queue *evq_sender;
extern t_event_queue *evq_trans_mgr;
extern string user_host;
extern string local_hostname;
extern t_phone *phone;
// Protected
// Create a request within a dialog
// RFC 3261 12.2.1.1
t_request *t_dialog::create_request(t_method m) {
assert(state != DS_NULL);
t_user *user_config = phone_user->get_user_profile();
// RFC 3261 9.1
if (m == CANCEL) {
t_request *r = new t_request(m);
MEMMAN_NEW(r);
assert(req_out_invite);
t_request *orig_req = req_out_invite->get_request();
r->hdr_to = orig_req->hdr_to;
r->hdr_from = orig_req->hdr_from;
r->hdr_call_id = orig_req->hdr_call_id;
r->hdr_cseq.set_seqnr(orig_req->hdr_cseq.seqnr);
r->hdr_cseq.set_method(CANCEL);
r->hdr_via = orig_req->hdr_via; // RFC 3261 8.1.1.7
r->hdr_max_forwards.set_max_forwards(MAX_FORWARDS);
r->hdr_route = orig_req->hdr_route;
SET_HDR_USER_AGENT(r->hdr_user_agent);
r->uri = orig_req->uri;
// RFC 3263 4
// CANCEL for a particular SIP request MUST be sent to the same SIP
// server that the SIP request was delivered to.
t_ip_port ip_port;
orig_req->get_destination(ip_port, *user_config);
r->set_destination(ip_port);
return r;
}
t_request *r = t_abstract_dialog::create_request(m);
// CSeq header
if (m == ACK) {
assert(req_out_invite);
// Local sequence number was incremented by t_abstract_dialog.
// Decrement as it ACK does not take a new sequence number.
local_seqnr--;
// ACK has the same sequence number
// as the INVITE.
r->hdr_cseq.set_seqnr(req_out_invite->get_request()->hdr_cseq.seqnr);
// RFC 3261 22.1
// Authorization and Proxy-Authorization headers in INVITE
// must be repeated in ACK
r->hdr_authorization = req_out_invite->get_request()->
hdr_authorization;
r->hdr_proxy_authorization = req_out_invite->get_request()->
hdr_proxy_authorization;
}
// Contact header
t_contact_param contact;
switch (m) {
case REFER:
case SUBSCRIBE:
case NOTIFY:
// RFC 3265 7.1, RFC 3515 2.2
// Contact header is mandatory
contact.uri.set_url(line->create_user_contact(h_ip2str(r->get_local_ip())));
r->hdr_contact.add_contact(contact);
break;
default:
break;
}
// Privacy header
if (line->get_hide_user()) {
r->hdr_privacy.add_privacy(PRIVACY_ID);
}
return r;
}
// NULL state. Waiting for incoming INVITE
void t_dialog::state_null(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
if (r->method != INVITE) {
state = DS_TERMINATED;
return;
}
// Set local tag
if (r->hdr_to.tag.size() == 0) {
local_tag = NEW_TAG;
} else {
local_tag = r->hdr_to.tag;
}
// If STUN is enabled, then first send a STUN binding request to
// discover the IP adderss and port for media.
if (phone->use_stun(user_config)) {
// The STUN transaction may take a while.
// Send 100 Trying
resp = r->create_response(R_100_TRYING);
resp->hdr_to.set_tag("");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
if (!stun_bind_media()) {
// STUN request failed. Send a 500 on the INVITE.
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
state = DS_TERMINATED;
return;
}
}
call_id = r->hdr_call_id.call_id;
// Initialize local seqnr
local_seqnr = NEW_SEQNR;
local_resp_nr = NEW_SEQNR;
remote_tag = r->hdr_from.tag;
local_uri = r->hdr_to.uri;
local_display = r->hdr_to.display;
remote_uri = r->hdr_from.uri;
remote_display = r->hdr_from.display;
// Set remote target URI and display name
remote_target_uri = r->hdr_contact.contact_list.front().uri;
remote_target_display = r->
hdr_contact.contact_list.front().display;
// Set route set
if (r->hdr_record_route.is_populated()) {
route_set = r->hdr_record_route.route_list;
}
// RFC 3261 13.2.1
// An initial INVITE should list all supported extensions.
// Set supported extensions
if (r->hdr_supported.is_populated()) {
remote_extensions.insert(r->hdr_supported.features.begin(),
r->hdr_supported.features.end());
}
// Media information
int warn_code;
string warn_text;
if (r->body) {
switch(r->body->get_type()) {
case BODY_SDP:
if (session->process_sdp_offer((t_sdp*)r->body,
warn_code, warn_text)) {
session->recvd_offer = true;
break;
}
// Unsupported media
resp = r->create_response(
R_488_NOT_ACCEPTABLE_HERE);
resp->hdr_to.set_tag(local_tag);
resp->hdr_warning.add_warning(t_warning(LOCAL_HOSTNAME,
0, warn_code, warn_text));
line->send_response(resp, tuid, tid);
// Create call history record
line->call_hist_record.start_call(r, t_call_record::DIR_IN,
user_config->get_profile_name());
line->call_hist_record.fail_call(resp);
MEMMAN_DELETE(resp);
delete resp;
state = DS_TERMINATED;
return;
default:
// Unsupported body type. Reject call.
resp = r->create_response(
R_415_UNSUPPORTED_MEDIA_TYPE);
resp->hdr_to.set_tag(local_tag);
// RFC 3261 21.4.13
SET_HDR_ACCEPT(resp->hdr_accept);
// Create call history record
line->call_hist_record.start_call(r, t_call_record::DIR_IN,
user_config->get_profile_name());
line->call_hist_record.fail_call(resp);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
state = DS_TERMINATED;
return;
}
}
resp = r->create_response(R_180_RINGING);
resp->hdr_to.set_tag(local_tag);
// RFC 3261 13.3.1.1
// A provisional response creates an early dialog, so
// copy the Record-Route header and add a Contact
// header.
// Copy the Record-Route header from request to response
if (r->hdr_record_route.is_populated()) {
resp->hdr_record_route = r->hdr_record_route;
}
// Set Contact header
t_contact_param contact;
contact.uri.set_url(line->create_user_contact(h_ip2str(resp->get_local_ip())));
resp->hdr_contact.add_contact(contact);
// RFC 3262 3
// Send 180 response reliable if needed
if (r->hdr_require.contains(EXT_100REL) ||
(r->hdr_supported.contains(EXT_100REL) &&
(user_config->get_ext_100rel() == EXT_PREFERRED ||
user_config->get_ext_100rel() == EXT_REQUIRED)))
{
resp->hdr_require.add_feature(EXT_100REL);
resp->hdr_rseq.set_resp_nr(++local_resp_nr);
// RFC 3262 5
// Create SDP offer in first reliable response if no offer
// was received in INVITE.
// This implentation does not create an answer in an
// reliable 1xx response if an offer was received.
if (!session->recvd_offer) {
session->create_sdp_offer(resp, SDP_O_USER);
}
// Keep a copy of the response for retransmission
resp_1xx_invite = (t_response *)resp->copy();
// Start 100rel timeout and guard timers
line->start_timer(LTMR_100REL_GUARD, get_object_id());
line->start_timer(LTMR_100REL_TIMEOUT, get_object_id());
}
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
MEMMAN_DELETE(resp);
delete resp;
ui->cb_incoming_call(user_config, line->get_line_number(), r);
line->call_hist_record.start_call(r, t_call_record::DIR_IN,
user_config->get_profile_name());
state = DS_W4ANSWER;
}
// A provisional answer has been sent. Waiting for user to answer.
void t_dialog::state_w4answer(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
bool tear_down = false;
bool answer_call = false;
t_call_script script_in_call_failed(user_config, t_call_script::TRIGGER_IN_CALL_FAILED,
line->get_line_number() + 1);
switch (r->method) {
case CANCEL:
// Cancel the request and terminate the dialog.
// A response on the CANCEL is already given by dialog::recvd_cancel
resp = req_in_invite->get_request()->
create_response(R_487_REQUEST_TERMINATED);
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
// Trigger call script
script_in_call_failed.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
ui->cb_call_cancelled(line->get_line_number());
state = DS_TERMINATED;
break;
case BYE:
// Send 200 on the BYE request
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
// Send a 487 response to terminate the pending request
resp = req_in_invite->get_request()->create_response(
R_487_REQUEST_TERMINATED);
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
// Trigger call script
script_in_call_failed.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
ui->cb_far_end_hung_up(line->get_line_number());
state = DS_TERMINATED;
break;
case PRACK:
// RFC 3262 3
if (respond_prack(r, tuid, tid)) {
answer_call = answer_after_prack;
// RFC 3262 5
// If an offer was sent in the 1xx response, then PRACK must
// contain an answer
if (session->sent_offer && r->body) {
int warn_code;
string warn_text;
if (r->body->get_type() != BODY_SDP) {
// Only SDP bodies are supported
ui->cb_unsupported_content_type(
line->get_line_number(), r);
tear_down = true;
} else if (session->process_sdp_answer((t_sdp *)r->body,
warn_code, warn_text))
{
session->recvd_answer = true;
session->start_rtp();
} else {
// SDP answer is not supported.
// Tear down the call.
ui->cb_sdp_answer_not_supported(
line->get_line_number(), warn_text);
tear_down = true;
}
}
if (session->sent_offer && !r->body) {
ui->cb_sdp_answer_missing(line->get_line_number());
tear_down = true;
}
}
if (tear_down) {
resp = req_in_invite->get_request()->create_response(
R_400_BAD_REQUEST,
"SDP answer in PRACK missing or unsupported");
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_IN_CALL_FAILED,
line->get_line_number() + 1);
script.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
state = DS_TERMINATED;
} else if (answer_call) {
answer();
}
break;
default:
// INVITE transaction has not been completed. Deny
// other requests within the dialog.
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR,
"Session not yet established");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
}
void t_dialog::state_w4answer(t_line_timer timer) {
t_ip_port ip_port;
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
t_call_script script_in_call_failed(user_config, t_call_script::TRIGGER_IN_CALL_FAILED,
line->get_line_number() + 1);
// RFC 3262 3
switch(timer) {
case LTMR_100REL_TIMEOUT:
// Retransmit 1xx response.
// Send the response directly to the sender thread
// bypassing the transaction layer. As this is a retransmission
// from the TU, the transaction layer does not need to know.
resp_1xx_invite->get_destination(ip_port);
if (ip_port.ipaddr == 0) {
// This should not happen. The response has been
// sent before so it should be possible to sent
// it again. Ignore the timeout. When the 100rel
// guard timer expires, the dialog will be
// cleaned up.
break;
}
evq_sender->push_network(resp_1xx_invite, ip_port);
line->start_timer(LTMR_100REL_TIMEOUT, get_object_id());
break;
case LTMR_100REL_GUARD:
line->stop_timer(LTMR_100REL_TIMEOUT, get_object_id());
// PRACK was not received in time. Tear down the call.
resp = req_in_invite->get_request()->create_response(
R_500_INTERNAL_SERVER_ERROR, "100rel timeout");
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
// Trigger call script
script_in_call_failed.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
remove_client_request(&req_in_invite);
MEMMAN_DELETE(resp_1xx_invite);
delete resp_1xx_invite;
resp_1xx_invite = NULL;
state = DS_TERMINATED;
log_file->write_report("LTMR_100REL_GUARD expired.",
"t_dialog::state_w4answer");
ui->cb_100rel_timeout(line->get_line_number());
break;
default:
// Other timeouts are not expected. Ignore.
break;
}
}
// 200 OK has been sent. Waiting for ACK
void t_dialog::state_w4ack(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
bool tear_down = false;
t_client_request *cr;
t_call_script script_out_call_failed(user_config, t_call_script::TRIGGER_OUT_CALL_FAILED,
line->get_line_number() + 1);
switch(r->method) {
case ACK:
// Dialog is established now.
line->stop_timer(LTMR_ACK_TIMEOUT, get_object_id());
line->stop_timer(LTMR_ACK_GUARD, get_object_id());
remove_client_request(&req_in_invite);
MEMMAN_DELETE(resp_invite);
delete resp_invite;
resp_invite = NULL;
// If no offer was received in INVITE, then an offer
// has been sent in 200 OK or reliable 1xx (RFC 3262).
// Therefor an answer must be present in ACK
if (!session->recvd_offer && r->body) {
int warn_code;
string warn_text;
if (r->body->get_type() != BODY_SDP) {
// Only SDP bodies are supported
ui->cb_unsupported_content_type(
line->get_line_number(), r);
tear_down = true;
} else if (session->process_sdp_answer((t_sdp *)r->body,
warn_code, warn_text))
{
session->recvd_answer = true;
session->start_rtp();
} else {
// SDP answer is not supported.
// Tear down the call.
ui->cb_sdp_answer_not_supported(
line->get_line_number(), warn_text);
tear_down = true;
}
}
if (!session->recvd_offer && !r->body) {
ui->cb_sdp_answer_missing(line->get_line_number());
tear_down = true;
}
if (end_after_ack) {
ui->cb_far_end_hung_up(line->get_line_number());
state = DS_TERMINATED;
} else {
state = DS_CONFIRMED;
if (tear_down) {
send_bye();
}
}
ui->cb_call_established(line->get_line_number());
break;
case BYE:
// Send 200 on the BYE request
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
// Trigger call script
script_out_call_failed.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
line->call_hist_record.end_call(true);
// The session will be ended when an ACK has been
// received.
end_after_ack = true;
break;
case PRACK:
// RFC 3262 3
// This is a late PRACK as the call is answered already.
// Respond in a normal way to the PRACK
respond_prack(r, tuid, tid);
break;
default:
// Queue the request as ACK needs to be received first.
// Note that the tuid value is not stored in the queue.
// For an incoming request tuid is always 0.
cr = new t_client_request(user_config, r, tid);
MEMMAN_NEW(cr);
inc_req_queue.push_back(cr);
log_file->write_header("t_dialog::state_w4ack",
LOG_NORMAL, LOG_INFO);
log_file->write_raw("Waiting for ACK.\n");
log_file->write_raw("Queue incoming ");
log_file->write_raw(method2str(r->method, r->unknown_method));
log_file->write_endl();
log_file->write_footer();
break;
}
}
void t_dialog::state_w4ack_re_invite(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
bool tear_down = false;
t_call_script script_out_call_failed(user_config, t_call_script::TRIGGER_OUT_CALL_FAILED,
line->get_line_number() + 1);
switch(r->method) {
case ACK:
// re_INVITE is finished now
line->stop_timer(LTMR_ACK_TIMEOUT, get_object_id());
line->stop_timer(LTMR_ACK_GUARD, get_object_id());
remove_client_request(&req_in_invite);
MEMMAN_DELETE(resp_invite);
delete resp_invite;
resp_invite = NULL;
// If no offer was received in INVITE, then an offer
// has been sent in 200 OK or reliable 1xx (RFC 3262).
// Therefor an answer must be present in ACK
if (!session_re_invite->recvd_offer && r->body) {
int warn_code;
string warn_text;
if (r->body->get_type() != BODY_SDP) {
// Only SDP bodies are supported
ui->cb_unsupported_content_type(
line->get_line_number(), r);
tear_down = true;
} else if (session_re_invite->process_sdp_answer(
(t_sdp *)r->body, warn_code, warn_text))
{
session_re_invite->recvd_answer = true;
} else {
// SDP answer is not supported.
// Tear down the call.
ui->cb_sdp_answer_not_supported(
line->get_line_number(), warn_text);
tear_down = true;
}
}
if (!session_re_invite->recvd_offer && !r->body) {
ui->cb_sdp_answer_missing(line->get_line_number());
tear_down = true;
}
if (end_after_ack) {
ui->cb_far_end_hung_up(line->get_line_number());
state = DS_TERMINATED;
} else {
state = DS_CONFIRMED;
if (tear_down) {
send_bye();
} else {
// Make the new session description current
activate_new_session();
}
}
break;
case BYE:
// Send 200 on the BYE request
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
// Trigger call script
script_out_call_failed.exec_notify(resp);
MEMMAN_DELETE(resp);
delete resp;
line->call_hist_record.end_call(true);
// The session will be ended when an ACK has been
// received.
end_after_ack = true;
break;
default:
// ACK has not been received. Handle other incoming request
// as if we are in the confirmed state. These incoming requests
// should not change state.
state_confirmed(r, tuid, tid);
assert(state == DS_W4ACK_RE_INVITE);
break;
}
}
// RFC 3261 13.3.1.4
void t_dialog::state_w4ack(t_line_timer timer) {
t_ip_port ip_port;
// NOTE: this code is also executed for re-INVITE ACK time-outs
// timeout handling for INVITE/re-INVITE is the same
switch(timer) {
case LTMR_ACK_TIMEOUT:
// Retransmit 2xx response.
// Send the response directly to the sender thread
// as the INVITE transaction completed already.
// (see RFC 3261 17.2.1)
if (!resp_invite) break; // there is no response to send
resp_invite->get_destination(ip_port);
if (ip_port.ipaddr == 0) {
// This should not happen. The response has been
// sent before so it should be possible to sent
// it again. Ignore the timeout. When the ACK
// guard timer expires, the dialog will be
// cleaned up.
break;
}
evq_sender->push_network(resp_invite, ip_port);
line->start_timer(LTMR_ACK_TIMEOUT, get_object_id());
break;
case LTMR_ACK_GUARD:
line->stop_timer(LTMR_ACK_TIMEOUT, get_object_id());
// Consider dialog as established and tear down call
remove_client_request(&req_in_invite);
MEMMAN_DELETE(resp_invite);
delete resp_invite;
resp_invite = NULL;
state = DS_CONFIRMED;
log_file->write_report("LTMR_ACK_GUARD expired.",
"t_dialog::state_w4ack");
if (end_after_ack) {
state = DS_TERMINATED;
} else {
send_bye();
}
ui->cb_ack_timeout(line->get_line_number());
break;
default:
// Other timeouts are not expected. Ignore.
break;
}
}
void t_dialog::state_w4ack_re_invite(t_line_timer timer) {
state_w4ack(timer);
}
void t_dialog::state_w4re_invite_resp(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
t_call_script script_remote_release(user_config, t_call_script::TRIGGER_REMOTE_RELEASE,
line->get_line_number() + 1);
switch(r->method) {
case BYE:
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
// Trigger call script
script_remote_release.exec_notify(r);
MEMMAN_DELETE(resp);
delete resp;
ui->cb_far_end_hung_up(line->get_line_number());
line->call_hist_record.end_call(true);
if (!sub_refer) {
state = DS_TERMINATED;
} else {
state = DS_CONFIRMED_SUB;
if (sub_refer->get_role() == SR_SUBSCRIBER) {
// End subscription
sub_refer->unsubscribe();
}
}
break;
case ACK:
// Ignore ACK
break;
case OPTIONS:
resp = line->create_options_response(r, true);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
case PRACK:
// RFC 3262 3
// This is a late PRACK. Respond in a normal way.
respond_prack(r, tuid, tid);
break;
case SUBSCRIBE:
process_subscribe(r, tuid, tid);
break;
case NOTIFY:
process_notify(r, tuid, tid);
break;
default:
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR,
"Waiting for re-INVITE response");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
}
// In the confirmed state, requests will be responded.
void t_dialog::state_confirmed(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
t_call_script script_remote_release(user_config, t_call_script::TRIGGER_REMOTE_RELEASE,
line->get_line_number() + 1);
switch(r->method) {
case INVITE:
// re-INVITE
process_re_invite(r, tuid, tid);
break;
case BYE:
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
// Trigger call script
script_remote_release.exec_notify(r);
MEMMAN_DELETE(resp);
delete resp;
ui->cb_far_end_hung_up(line->get_line_number());
line->call_hist_record.end_call(true);
if (!sub_refer) {
state = DS_TERMINATED;
} else {
state = DS_CONFIRMED_SUB;
if (sub_refer->get_role() == SR_SUBSCRIBER) {
// End subscription
sub_refer->unsubscribe();
}
}
break;
case ACK:
// Ignore ACK
break;
case OPTIONS:
resp = line->create_options_response(r, true);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
case PRACK:
// RFC 3262 3
// This is a late PRACK. Respond in a normal way.
respond_prack(r, tuid, tid);
break;
case REFER:
process_refer(r, tuid, tid);
break;
case SUBSCRIBE:
process_subscribe(r, tuid, tid);
break;
case NOTIFY:
process_notify(r, tuid, tid);
break;
case INFO:
process_info(r, tuid, tid);
break;
case MESSAGE:
process_message(r, tuid, tid);
break;
default:
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
}
void t_dialog::state_confirmed(t_line_timer timer) {
switch(timer) {
case LTMR_GLARE_RETRY:
switch(reinvite_purpose) {
case REINVITE_HOLD:
hold();
break;
case REINVITE_RETRIEVE:
retrieve();
line->retry_retrieve_succeeded();
// Note that the re-INVITE is not completed here yet.
// If re-INVITE fails then line->failed_retrieve will
// be called later.
break;
default:
assert(false);
}
break;
default:
// Other timeouts are not exepcted. Ignore.
break;
}
}
void t_dialog::state_confirmed_sub(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
switch(r->method) {
case OPTIONS:
resp = line->create_options_response(r, true);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
case SUBSCRIBE:
process_subscribe(r, tuid, tid);
break;
case NOTIFY:
process_notify(r, tuid, tid);
break;
default:
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
if (!sub_refer) {
// The subscription has been terminated already.
state = DS_TERMINATED;
} else if (sub_refer->get_state() == SS_TERMINATED) {
MEMMAN_DELETE(sub_refer);
delete sub_refer;
sub_refer = NULL;
state = DS_TERMINATED;
}
}
void t_dialog::process_re_invite(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
session_re_invite = session->create_clean_copy();
// Media information
int warn_code;
string warn_text;
if (r->body) {
switch(r->body->get_type()) {
case BODY_SDP:
if (session_re_invite->
process_sdp_offer((t_sdp*)r->body,
warn_code, warn_text))
{
session_re_invite->recvd_offer = true;
break;
}
// Unsupported media
resp = r->create_response(
R_488_NOT_ACCEPTABLE_HERE);
resp->hdr_warning.add_warning(t_warning(LOCAL_HOSTNAME,
0, warn_code, warn_text));
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
MEMMAN_DELETE(session_re_invite);
delete session_re_invite;
session_re_invite = NULL;
// Stay in the confirmed state. The sender of the
// request has to determine if the dialog needs to
// be torn down by sending a BYE.
return;
default:
// Unsupported body type. Reject call.
resp = r->create_response(
R_415_UNSUPPORTED_MEDIA_TYPE);
// RFC 3261 21.4.13
SET_HDR_ACCEPT(resp->hdr_accept);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
MEMMAN_DELETE(session_re_invite);
delete session_re_invite;
session_re_invite = NULL;
// Stay in the confirmed state.
return;
}
}
// If STUN is enabled, then first send a STUN binding request to
// discover the IP adderss and port for media if no RTP stream
// is currently active.
if (phone->use_stun(user_config) && !session->is_rtp_active()) {
// The STUN transaction may take a while.
// Send 100 Trying
resp = r->create_response(R_100_TRYING);
resp->hdr_to.set_tag("");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
if (!stun_bind_media()) {
// STUN request failed. Send a 500 on the INVITE.
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
state = DS_TERMINATED;
return;
}
}
// Refresh target
if (r->hdr_contact.is_populated() &&
r->hdr_contact.contact_list.size() > 0)
{
remote_target_uri = r->hdr_contact.contact_list.front().uri;
remote_target_display = r->
hdr_contact.contact_list.front().display;
}
// Send 200 OK
resp_invite = r->create_response(R_200_OK);
resp_invite->hdr_to.set_tag(local_tag);
// Set Contact header
t_contact_param contact;
contact.uri.set_url(line->create_user_contact(h_ip2str(resp_invite->get_local_ip())));
resp_invite->hdr_contact.add_contact(contact);
// Set Allow and Supported headers
SET_HDR_ALLOW(resp_invite->hdr_allow, user_config);
SET_HDR_SUPPORTED(resp_invite->hdr_supported, user_config);
// RFC 3261 13.3.1.4
// Create SDP offer if no offer was received in INVITE and no offer
// was sent in a reliable 1xx response (RFC 3262 5).
// Otherwise create an SDP answer.
if (!session_re_invite->recvd_offer && !session_re_invite->sent_offer) {
session_re_invite->create_sdp_offer(resp_invite, SDP_O_USER);
} else {
session_re_invite->create_sdp_answer(resp_invite, SDP_O_USER);
}
line->send_response(resp_invite, tuid, tid);
line->start_timer(LTMR_ACK_GUARD, get_object_id());
line->start_timer(LTMR_ACK_TIMEOUT, get_object_id());
state = DS_W4ACK_RE_INVITE;
}
void t_dialog::process_refer(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
t_contact_param contact;
refer_accepted = true;
// RFC 3515
if (sub_refer || !user_config->get_allow_refer()) {
// A reference is already in progress or REFER is not
// allowed.
resp = r->create_response(R_603_DECLINE);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
refer_accepted = false;
return;
}
// Check if the URI scheme is supported
if (r->hdr_refer_to.uri.get_scheme() != "sip") {
resp = r->create_response(R_416_UNSUPPORTED_URI_SCHEME);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
refer_accepted = false;
return;
}
resp = r->create_response(R_202_ACCEPTED);
// RFC 3515 2.2
// Contact header is mandatory
contact.uri.set_url(line->create_user_contact(h_ip2str(resp->get_local_ip())));
resp->hdr_contact.add_contact(contact);
if (r->hdr_refer_sub.is_populated() && !r->hdr_refer_sub.create_refer_sub) {
// RFC 4488 4
resp->hdr_refer_sub.set_create_refer_sub(false);
}
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
if (r->hdr_refer_sub.is_populated() && !r->hdr_refer_sub.create_refer_sub) {
// RFC 4488
// The REFER-issuer requested not to create an implicit refer
// subscription.
log_file->write_report(
"REFER-issuer requested not to create a refer subscription.",
"t_dialog::process_refer");
} else {
// RFC 3515
// The event header of a NOTIFY to a first REFER MAY
// include the id paramter. NOTIFY's to subsequent
// REFERs MUST include the id parameter (CSeq from REFER).
sub_refer = new t_sub_refer(this, SR_NOTIFIER,
ulong2str(r->hdr_cseq.seqnr));
MEMMAN_NEW(sub_refer);
// Send immediate NOTIFY
resp = new t_response(R_100_TRYING);
MEMMAN_NEW(resp);
if (user_config->get_ask_user_to_refer()) {
// If the user has to grant permission, then the
// subscription is pending.
sub_refer->send_notify(resp, SUBSTATE_PENDING);
} else {
sub_refer->send_notify(resp, SUBSTATE_ACTIVE);
}
MEMMAN_DELETE(resp);
delete resp;
}
// Ask permission to refer
if (user_config->get_ask_user_to_refer()) {
if (r->hdr_referred_by.is_populated()) {
ui->cb_ask_user_to_refer(user_config,
r->hdr_refer_to.uri,
r->hdr_refer_to.display,
r->hdr_referred_by.uri,
r->hdr_referred_by.display);
} else {
ui->cb_ask_user_to_refer(user_config,
r->hdr_refer_to.uri,
r->hdr_refer_to.display,
t_url(), "");
}
} else {
ui->send_refer_permission(true);
}
// NOTE: refer_accepted = true, though the answer to permission
// is not given yet. So this means, that the refer is not
// rejected at this moment. It may be rejected by the user.
}
void t_dialog::recvd_refer_permission(bool permission, t_request *r) {
t_response *resp;
// NOTE: if the REFER-issuer requested not to create a refer
// subscription (RFC 4488), then no NOTIFY can be sent to signal
// the rejection.
if (!permission && sub_refer) {
// User denied REFER
// RFC 3515 2.4.5
resp = new t_response(R_603_DECLINE);
MEMMAN_NEW(resp);
sub_refer->send_notify(resp, SUBSTATE_TERMINATED,
EV_REASON_REJECTED);
MEMMAN_DELETE(resp);
delete resp;
}
refer_accepted = permission;
}
void t_dialog::process_subscribe(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
if (sub_refer && sub_refer->match(r)) {
sub_refer->recv_subscribe(r, tuid, tid);
if (sub_refer->get_state() == SS_TERMINATED) {
MEMMAN_DELETE(sub_refer);
delete sub_refer;
sub_refer = NULL;
}
return;
}
resp = r->create_response(R_481_TRANSACTION_NOT_EXIST,
REASON_481_SUBSCRIPTION_NOT_EXIST);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
}
void t_dialog::process_notify(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
if (!sub_refer &&
(refer_state == REFST_W4RESP || refer_state == REFST_W4NOTIFY))
{
// First NOTIFY after sending a REFER
sub_refer = new t_sub_refer(this, SR_SUBSCRIBER, r->hdr_event.id);
MEMMAN_NEW(sub_refer);
refer_state = REFST_PENDING;
}
if (sub_refer && sub_refer->match(r)) {
sub_refer->recv_notify(r, tuid, tid);
if (sub_refer->get_state() == SS_TERMINATED) {
// Set the refer state to NULL before calling the UI
// call back functions as the user interface might use
// the refer state to render the correct status to the
// user.
refer_state = REFST_NULL;
// Determine outcome of the reference
switch(sub_refer->get_sr_result()) {
case SRR_INPROG:
// The outcome of the reference is unknown.
// Treat it as a success as no new info will
// come to the referrer.
refer_succeeded = true;
ui->cb_refer_result_inprog(line->get_line_number());
break;
case SRR_FAILED:
refer_succeeded = false;
ui->cb_refer_result_failed(line->get_line_number());
break;
case SRR_SUCCEEDED:
refer_succeeded = true;
ui->cb_refer_result_success(line->get_line_number());
break;
default:
assert(false);
}
MEMMAN_DELETE(sub_refer);
delete sub_refer;
sub_refer = NULL;
} else if (!sub_refer->is_pending()) {
refer_state = REFST_ACTIVE;
}
return;
}
// RFC 3265 3.2.4
resp = r->create_response(R_481_TRANSACTION_NOT_EXIST,
REASON_481_SUBSCRIPTION_NOT_EXIST);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
}
void t_dialog::process_info(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
// RFC 2976 2.2
// A 200 OK response MUST be sent by a UAS for an INFO request with
// no message body if the INFO request was successfully received for
// an existing call.
if (!r->body) {
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
}
if (r->body->get_type() != BODY_DTMF_RELAY) {
resp = r->create_response(R_415_UNSUPPORTED_MEDIA_TYPE);
resp->hdr_accept.add_media(t_media("application", "dtmf-relay"));
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
}
char dtmf_signal = ((t_sip_body_dtmf_relay *)r->body)->signal;
if (!is_valid_dtmf_sym(dtmf_signal)) {
resp = r->create_response(R_400_BAD_REQUEST, "Invalid DTMF signal");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
}
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
ui->cb_dtmf_detected(line->get_line_number(), char2dtmf_ev(dtmf_signal));
}
void t_dialog::process_message(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
log_file->write_report("Received in-dialog MESSAGE.",
"t_dialog::process_message", LOG_NORMAL, LOG_DEBUG);
if (!r->body || !MESSAGE_CONTENT_TYPE_SUPPORTED(*r)) {
resp = r->create_response(R_415_UNSUPPORTED_MEDIA_TYPE);
// RFC 3261 21.4.13
SET_MESSAGE_HDR_ACCEPT(resp->hdr_accept);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
}
if (r->body && r->body->get_type() == BODY_IM_ISCOMPOSING_XML) {
// Message composing indication
t_im_iscomposing_xml_body *sb = dynamic_cast<t_im_iscomposing_xml_body *>(r->body);
im::t_composing_state state = im::string2composing_state(sb->get_state());
time_t refresh = sb->get_refresh();
ui->cb_im_iscomposing_request(line->get_user(), r, state, refresh);
resp = r->create_response(R_200_OK);
} else {
// Instant message
bool accepted = ui->cb_message_request(line->get_user(), r);
if (accepted) {
resp = r->create_response(R_200_OK);
} else {
if (user_config->get_im_max_sessions() == 0) {
resp = r->create_response(R_603_DECLINE);
} else {
resp = r->create_response(R_486_BUSY_HERE);
}
}
}
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
}
// INVITE sent. Waiting for a first non-100 response.
void t_dialog::state_w4invite_resp(t_response *r, t_tuid tuid, t_tid tid) {
if (r->hdr_cseq.method != INVITE) return;
t_user *user_config = phone_user->get_user_profile();
// 1XX (except 100) and 2XX establish the dialog.
// Update the state for dialog establishment.
// RFC 3261 12.1.2
switch (r->get_class()) {
case R_1XX:
if (r->code == R_100_TRYING) break;
// RFC 3262 4
// Discard retransmissions and out-of-sequence reliable
// provisional responses.
if (must_discard_100rel(r)) return;
// fall thru
case R_2XX:
// Set remote tag
remote_tag = r->hdr_to.tag;
create_route_set(r);
create_remote_target(r);
// Set remote URI and display name
remote_uri = r->hdr_to.uri;
remote_display = r->hdr_to.display;
process_1xx_2xx_invite_resp(r);
break;
default:
break;
}
// RFC 3262
// Send PRACK if required
send_prack_if_required(r);
t_call_script script_out_call_answered(user_config,
t_call_script::TRIGGER_OUT_CALL_ANSWERED,
line->get_line_number() + 1);
t_call_script script_out_call_failed(user_config,
t_call_script::TRIGGER_OUT_CALL_FAILED,
line->get_line_number() + 1);
switch (r->get_class()) {
case R_1XX:
// Provisional response received.
line->ci_set_last_provisional_reason(r->reason);
ui->cb_provisional_resp_invite(line->get_line_number(), r);
if (r->code > R_100_TRYING && r->hdr_to.tag.size() > 0) {
state = DS_EARLY;
} else {
state = DS_W4INVITE_RESP2;
}
// User indicated that the request should be cancelled.
// Now that the first provisional response has been received,
// a CANCEL can be sent.
if (request_cancelled) {
send_cancel(true);
}
break;
case R_2XX:
// Stop cancel guard timer if it was running
line->stop_timer(LTMR_CANCEL_GUARD, get_object_id());
// Success received.
ack_2xx_invite(r);
// Check for REFER support
// If the Allow header is not present then assume REFER
// is supported.
if (!r->hdr_allow.is_populated() ||
r->hdr_allow.contains_method(REFER))
{
line->ci_set_refer_supported(true);
}
// Trigger call script
script_out_call_answered.exec_notify(r);
ui->cb_call_answered(user_config, line->get_line_number(), r);
line->call_hist_record.answer_call(r);
state = DS_CONFIRMED;
if (request_cancelled) {
// User indicated that the request should be cancelled,
// but no response was received yet. A final response
// has been received. Instead of CANCEL a BYE will be
// sent now.
send_bye();
} else if (end_after_2xx_invite) {
// Or user cancelled the request already, but the 2XX
// glared with CANCEL.
log_file->write_report("CANCEL / 2XX INVITE glare.",
"t_dialog::state_w4invite_resp");
send_bye();
}
break;
case R_3XX:
case R_4XX:
case R_5XX:
case R_6XX:
default:
// Stop cancel guard timer if it was running
line->stop_timer(LTMR_CANCEL_GUARD, get_object_id());
// Final response (failure) received.
// Treat unknown response classes as failure.
// Trigger call script
script_out_call_failed.exec_notify(r);
ui->cb_stop_call_notification(line->get_line_number());
ui->cb_call_failed(user_config, line->get_line_number(), r);
line->call_hist_record.fail_call(r);
remove_client_request(&req_out_invite);
state = DS_TERMINATED;
break;
}
// Notify progress to the referror if this is a referred call
if (is_referred_call) {
get_phone()->notify_refer_progress(r, line->get_line_number());
}
}
void t_dialog::state_w4invite_resp(t_line_timer timer) {
switch (timer) {
case LTMR_CANCEL_GUARD:
log_file->write_report("Timer LTMR_CANCEL_GUARD expired.",
"t_dialog::state_w4invite_resp", LOG_NORMAL, LOG_WARNING);
// CANCEL has been responded to, but 487 on INVITE was never
// received. Abort the INVITE transaction.
if (req_out_invite) {
t_tid _tid = req_out_invite->get_tid();
if (_tid > 0) {
evq_trans_mgr->push_abort_trans(_tid);
}
}
break;
default:
// Ignore other timeouts
break;
}
}
// INVITE response sent. At least 1 provisional response (not 100 Trying)
// received.
void t_dialog::state_early(t_response *r, t_tuid tuid, t_tid tid) {
if (r->hdr_cseq.method != INVITE) return;
t_user *user_config = phone_user->get_user_profile();
switch (r->get_class()) {
case R_1XX:
// RFC 3262 4
// Discard retransmissiona and out-of-sequence reliable
// provisional responses.
if (must_discard_100rel(r)) return;
// fall thru
case R_2XX:
create_route_set(r);
create_remote_target(r);
process_1xx_2xx_invite_resp(r);
break;
default:
break;
}
// RFC 3262
// Send PRACK if required
send_prack_if_required(r);
t_call_script script_out_call_answered(user_config,
t_call_script::TRIGGER_OUT_CALL_ANSWERED,
line->get_line_number() + 1);
t_call_script script_out_call_failed(user_config,
t_call_script::TRIGGER_OUT_CALL_FAILED,
line->get_line_number() + 1);
switch (r->get_class()) {
case R_1XX:
// Provisional response received.
line->ci_set_last_provisional_reason(r->reason);
ui->cb_provisional_resp_invite(line->get_line_number(), r);
if (request_cancelled) {
send_cancel(true);
}
break;
case R_2XX:
// Stop cancel guard timer if it was running
line->stop_timer(LTMR_CANCEL_GUARD, get_object_id());
// Success received.
ack_2xx_invite(r);
// Check for REFER support
// If the Allow header is not present then assume REFER
// is supported.
if (!r->hdr_allow.is_populated() ||
r->hdr_allow.contains_method(REFER))
{
line->ci_set_refer_supported(true);
}
// Trigger call script
script_out_call_answered.exec_notify(r);
ui->cb_call_answered(user_config, line->get_line_number(), r);
line->call_hist_record.answer_call(r);
state = DS_CONFIRMED;
if (request_cancelled) {
// User indicated that the request should be cancelled,
// but no response was received yet. A final response
// has been received. Instead of CANCEL a BYE will be
// sent now.
send_bye();
} else if (end_after_2xx_invite) {
// Or user cancelled the request already, but the 2XX
// glared with CANCEL.
log_file->write_report("CANCEL / 2XX INVITE glare.",
"t_dialog::state_w4invite_resp");
send_bye();
}
break;
case R_3XX:
case R_4XX:
case R_5XX:
case R_6XX:
default:
// Stop cancel guard timer if it was running
line->stop_timer(LTMR_CANCEL_GUARD, get_object_id());
// Final response (failure) received.
// Treat unknown response classes as failure.
// Trigger call script
script_out_call_failed.exec_notify(r);
ui->cb_stop_call_notification(line->get_line_number());
ui->cb_call_failed(user_config, line->get_line_number(), r);
line->call_hist_record.fail_call(r);
remove_client_request(&req_out_invite);
state = DS_TERMINATED;
break;
}
// Notify progress to the referror if this is a referred call
if (is_referred_call) {
get_phone()->notify_refer_progress(r, line->get_line_number());
}
}
void t_dialog::state_early(t_line_timer timer) {
switch (timer) {
case LTMR_CANCEL_GUARD:
log_file->write_report("Timer LTMR_CANCEL_GUARD expired.",
"t_dialog::state_early", LOG_NORMAL, LOG_WARNING);
// CANCEL has been responded to, but 487 on INVITE was never
// received. Abort the INVITE transaction.
if (req_out_invite) {
t_tid _tid = req_out_invite->get_tid();
if (_tid > 0) {
evq_trans_mgr->push_abort_trans(_tid);
}
}
break;
default:
// Ignore other timeouts
break;
}
}
// BYE sent. Waiting for response.
void t_dialog::state_w4bye_resp(t_response *r, t_tuid tuid, t_tid tid) {
if (r->hdr_cseq.method != BYE) return;
switch (r->get_class()) {
case R_1XX:
// Provisional response received. Wait for final response.
break;
default:
// All final responses terminate the dialog.
remove_client_request(&req_out);
if (!sub_refer) {
state = DS_TERMINATED;
} else {
state = DS_CONFIRMED_SUB;
if (sub_refer->get_role() == SR_SUBSCRIBER) {
// End subscription
sub_refer->unsubscribe();
}
}
break;
}
}
void t_dialog::state_w4bye_resp(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
switch(r->method) {
case BYE:
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
// A BYE glare situation. Keep waiting for the BYE
// response.
break;
default:
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
}
// Confirmed dialog. Responses are for mid-dialog requests.
void t_dialog::state_confirmed_resp(t_response *r, t_tuid tuid, t_tid tid) {
// 1XX responses are not expected. If they are received
// then simply ignore them.
if (r->is_provisional()) return;
switch (r->hdr_cseq.method) {
case OPTIONS:
ui->cb_options_response(r);
remove_client_request(&req_out);
break;
case REFER:
remove_client_request(&req_refer);
if (refer_state != REFST_W4RESP) {
// NOTIFY has already been received. No need to
// process the REFER response anymore. Interesting
// issue might be: what if NOTIFY has been received and
// now a failure response comes in?
break;
}
if (!r->is_success()) {
// REFER failed
refer_state = REFST_NULL;
refer_succeeded = false;
// KLUDGE: only signal REFER failure in case of
// non-408/481 responses. These responses
// clear the line, so the upper layers should not
// take action on the failed refer.
if (r->code != R_408_REQUEST_TIMEOUT ||
r->code == R_481_TRANSACTION_NOT_EXIST)
{
out_refer_req_failed = true;
}
ui->cb_refer_failed(line->get_line_number(), r);
break;
}
refer_state = REFST_W4NOTIFY;
break;
case INFO:
remove_client_request(&req_info);
if (!dtmf_queue.empty()) {
char digit = dtmf_queue.front();
dtmf_queue.pop();
send_dtmf(digit, false, true);
}
break;
default:
// The received response should match the pending request.
// So this point should never be reached.
assert(false);
break;
}
// RFC 3261 12.2.1.2
// If a mid-dialog request is timed out, or the call/transaction
// does not exist anymore at the server, then terminate the
// dialog.
if (r->code == R_408_REQUEST_TIMEOUT ||
r->code == R_481_TRANSACTION_NOT_EXIST)
{
send_bye();
}
}
void t_dialog::state_w4re_invite_resp(t_response *r, t_tuid tuid, t_tid tid) {
if (r->hdr_cseq.method != INVITE) return;
switch (r->get_class()) {
case R_1XX:
if (r->code == R_100_TRYING) break;
// RFC 3262 4
// Discard retransmissiona and out-of-sequence reliable
// provisional responses.
if (must_discard_100rel(r)) return;
if (state == DS_W4RE_INVITE_RESP2) {
// RFC 3262
// Discard retransmissions and out-of-order
// reliable provisional responses.
if (must_discard_100rel(r)) return;
}
// RFC 3262
// Send PRACK if required
send_prack_if_required(r);
// fall thru
case R_2XX:
// Process SDP answer if answer is present and no
// answer has been received yet.
if (!session_re_invite->recvd_answer && r->body) {
int warn_code;
string warn_text;
if (r->body->get_type() != BODY_SDP) {
// Only SDP bodies are supported
ui->cb_unsupported_content_type(
line->get_line_number(), r);
request_cancelled = true;
} else if (session_re_invite->
process_sdp_answer((t_sdp *)r->body,
warn_code, warn_text))
{
session_re_invite->recvd_answer = true;
} else {
// SDP answer is not supported. Cancel
// the INVITE.
request_cancelled = true;
ui->cb_sdp_answer_not_supported(
line->get_line_number(), warn_text);
break;
}
}
// This implementation always sends an offer in
// INVITE. So an answer must be in a 2XX response
// as PRACK is not supported.
if (r->get_class() == R_2XX && !r->body) {
request_cancelled = true;
ui->cb_sdp_answer_missing(line->get_line_number());
break;
}
// Refresh target URI and display name
if (r->get_class() == R_2XX &&
r->hdr_contact.is_populated() &&
r->hdr_contact.contact_list.size() > 0)
{
remote_target_uri = r->
hdr_contact.contact_list.front().uri;
remote_target_display = r->
hdr_contact.contact_list.front().display;
}
break;
default:
break;
}
switch (r->get_class()) {
case R_1XX:
// Provisional response received.
state = DS_W4RE_INVITE_RESP2;
// Start re-INVITE guard timer (no RFC requirement)
line->start_timer(LTMR_RE_INVITE_GUARD, get_object_id());
// User indicated that the request should be cancelled.
// Now that the first provional response has been received,
// a CANCEL can be sent.
if (request_cancelled) {
send_cancel(true);
}
break;
case R_2XX:
// Success received.
line->stop_timer(LTMR_RE_INVITE_GUARD, get_object_id());
ack_2xx_invite(r);
ui->cb_reinvite_success(line->get_line_number(), r);
state = DS_CONFIRMED;
if (request_cancelled) {
// User indicated that the request should be cancelled,
// but no response was received yet. A final response
// has been received. Instead of CANCEL a BYE will be
// sent now.
send_bye();
} else if (end_after_2xx_invite) {
// Or user cancelled the request already, but the 2XX
// glared with CANCEL.
log_file->write_report("CANCEL / 2XX INVITE glare.",
"t_dialog::state_w4invite_resp");
send_bye();
} else {
// Make the re-INIVTE session info the current info
activate_new_session();
}
break;
case R_3XX:
case R_4XX:
case R_5XX:
case R_6XX:
default:
// Final response (failure) received.
// Treat unknown response classes as failure.
line->stop_timer(LTMR_RE_INVITE_GUARD, get_object_id());
ui->cb_reinvite_failed(line->get_line_number(), r);
remove_client_request(&req_out_invite);
// RFC 3261 14.1
// delete re-INVITE session info. Old session info
// stays as re-INVITE failed.
MEMMAN_DELETE(session_re_invite);
delete session_re_invite;
session_re_invite = NULL;
state = DS_CONFIRMED;
switch(reinvite_purpose) {
case REINVITE_HOLD:
// A call hold may not fail for the user as
// this cause problems with soundcard access and
// showing line status in the GUI. Even though re-INVITE
// failed, the RTP still stopped. So simply indicated
// that the hold failed, such that a subsequent retrieve
// can simply restart the RTP.
hold_failed = true;
break;
case REINVITE_RETRIEVE:
line->failed_retrieve();
if (r->code != R_491_REQUEST_PENDING) {
ui->cb_retrieve_failed(line->get_line_number(), r);
}
break;
default:
assert(false);
}
// RFC 3261 14.1
// Start wait timer before retrying a re-INVITE after a
// glare.
if (r->code == R_491_REQUEST_PENDING) {
line->start_timer(LTMR_GLARE_RETRY, get_object_id());
}
// RFC 3261 14.1
if (r->code == R_408_REQUEST_TIMEOUT ||
r->code == R_481_TRANSACTION_NOT_EXIST)
{
send_bye();
}
break;
}
}
void t_dialog::state_w4re_invite_resp(t_line_timer timer) {
switch(timer) {
case LTMR_RE_INVITE_GUARD:
// Abort the INVITE as the user cannot terminate
// it in a normal way.
if (req_out_invite) {
t_tid _tid = req_out_invite->get_tid();
if (_tid > 0) {
evq_trans_mgr->push_abort_trans(_tid);
}
} else {
// Consider this as if a 408 Timeout response has
// been received. Terminate the dialog.
send_bye();
}
break;
default:
break;
}
}
void t_dialog::activate_new_session(void) {
if (session->equal_audio(*session_re_invite)) {
log_file->write_report("SDP in re-INVITE is a noop.",
"t_dialog::activate_new_session");
MEMMAN_DELETE(session_re_invite);
delete session_re_invite;
session_re_invite = NULL;
return;
}
log_file->write_report("Renew session as specified by SDP in re-INVITE.",
"t_dialog::activate_new_session");
// Stop current session
MEMMAN_DELETE(session);
delete session;
// Create new session
session = session_re_invite;
session_re_invite = NULL;
session->start_rtp();
}
void t_dialog::process_1xx_2xx_invite_resp(t_response *r) {
t_user *user_config = phone_user->get_user_profile();
// Process SDP answer if answer is present and no
// answer has been received yet.
if (r->body) {
int warn_code;
string warn_text;
if (r->body->get_type() != BODY_SDP) {
// Only SDP bodies are supported
ui->cb_unsupported_content_type(line->get_line_number(), r);
request_cancelled = true;
} else if (!session->recvd_answer ||
(user_config->get_allow_sdp_change() &&
((t_sdp *)r->body)->origin.session_version !=
session->dst_sdp_version))
{
// Only process SDP if no SDP was received yet (RFC 3261
// 13.3.1. Or process SDP if overridden by the
// allow_sdp_change setting in the user profile.
// A changed SDP must have a new version number (RFC 3264)
if (session->process_sdp_answer((t_sdp *)r->body,
warn_code, warn_text))
{
// If this is a changed SDP, then stop the
// current RTP stream based on the previous SDP.
if (session->recvd_answer) session->stop_rtp();
session->recvd_answer = true;
// The following code part handles the ugly interaction
// between forking and early media (Vonage uses this).
// In case of forking 1xx responses with SDP may com
// from different destinations. Only the first 1xx will
// create a media stream. Media streams on other legs cannot
// be created as that would give sound conflicts.
// When a 2xx response with SDP is received, an early media
// stream on another leg must be killed.
// Due to forking multiple 2xx repsonses from different
// destinations may be received. Only the first 2xx response
// will create a media session. The other dialogs receiving
// a 2xx will be released immediately anyway (see line.cpp).
bool start_media = true;
t_dialog *d = line->get_dialog_with_active_session();
if (d != NULL) {
if (r->get_class() == R_2XX &&
d->get_state() != DS_CONFIRMED)
{
log_file->write_header(
"t_dialog::process_1xx_2xx_invite_resp");
log_file->write_raw(
"Kill early media on another dialog, id=");
log_file->write_raw(d->get_object_id());
log_file->write_endl();
log_file->write_footer();
d->kill_rtp();
} else {
log_file->write_header(
"t_dialog::process_1xx_2xx_invite_resp");
log_file->write_raw(
"Cannot start media as another dialog (id=");
log_file->write_raw(d->get_object_id());
log_file->write_raw(") already has media.\n");
log_file->write_footer();
start_media = false;
}
}
if (start_media) {
if (r->is_provisional()) {
log_file->write_report("Starting early media.",
"t_dialog::process_1xx_2xx_invite_resp");
}
// Stop locally played tones to free the soundcard
// for the voice stream
ui->cb_stop_call_notification(line->get_line_number());
session->start_rtp();
}
} else {
// SDP answer is not supported. Cancel
// the INVITE.
request_cancelled = true;
ui->cb_sdp_answer_not_supported(
line->get_line_number(), warn_text);
}
}
} else if (r->code == R_180_RINGING &&
!ringing_received && !session->recvd_answer)
{
// There is no SDP and far-end indicated that it is ringing
// so generate ring back tone locally.
ui->cb_play_ringback(user_config);
ringing_received = true;
}
// This implementation always sends an offer in
// INVITE. So an answer must be in a 2XX response if
// no answer has been received in a provisional response.
if (!session->recvd_answer && r->get_class() == R_2XX && !r->body) {
request_cancelled = true;
ui->cb_sdp_answer_missing(line->get_line_number());
}
// RFC 3261 13.3.1.4
// A 2XX response to an INVITE should contain a Supported header
// listing all supported extensions.
// Set extensions supported by remote party
if (r->get_class() == R_2XX && r->hdr_supported.is_populated()) {
remote_extensions.insert(r->hdr_supported.features.begin(),
r->hdr_supported.features.end());
}
}
void t_dialog::ack_2xx_invite(t_response *r) {
t_ip_port ip_port;
t_user *user_config = phone_user->get_user_profile();
if (ack) {
// delete previous cached ACK
MEMMAN_DELETE(ack);
delete ack;
}
ack = create_request(ACK);
ack->get_destination(ip_port, *user_config);
// If for some strange reason the destination could
// not be computed then wait for a retransmission of
// 2XX.
if (ip_port.ipaddr != 0 && ip_port.port != 0) {
evq_sender->push_network(ack, ip_port);
} else {
log_file->write_header("t_dialog::ack_2xx_invite", LOG_SIP, LOG_CRITICAL);
log_file->write_raw("Cannot determine destination IP address for ACK.\n\n");
log_file->write_raw(ack->encode());
log_file->write_footer();
}
remove_client_request(&req_out_invite);
}
void t_dialog::send_prack_if_required(t_response *r) {
t_user *user_config = phone_user->get_user_profile();
// RFC 3262
// Send PRACK if needed
if (r->get_class() == R_1XX && r->code != R_100_TRYING) {
// RFC 3262 4
// Send PRACK if the 1xx response is sent reliable and 100rel
// is enabled.
if (r->hdr_to.tag.size() > 0 &&
r->hdr_require.contains(EXT_100REL) &&
r->hdr_rseq.is_populated() &&
remote_target_uri.is_valid() &&
user_config->get_ext_100rel() != EXT_DISABLED)
{
t_request *prack = create_request(PRACK);
prack->hdr_rack.set_method(r->hdr_cseq.method);
prack->hdr_rack.set_cseq_nr(r->hdr_cseq.seqnr);
prack->hdr_rack.set_resp_nr(r->hdr_rseq.resp_nr);
// Delete previous PRACK request if it is still pending
if (req_prack) {
log_file->write_report("Previous PRACK still pending.",
"t_dialog::send_prack_if_needed");
remove_client_request(&req_prack);
}
req_prack = new t_client_request(user_config, prack, 0);
MEMMAN_NEW(req_prack);
line->send_request(prack, req_prack->get_tuid());
MEMMAN_DELETE(prack);
delete prack;
}
}
}
bool t_dialog::must_discard_100rel(t_response *r) {
t_user *user_config = phone_user->get_user_profile();
// RFC 3262 4
// Discard retransmissiona and out-of-sequence reliable
// provisional responses.
if (r->code > R_100_TRYING && r->hdr_to.tag.size() > 0 &&
r->hdr_require.contains(EXT_100REL) &&
r->hdr_rseq.is_populated() &&
user_config->get_ext_100rel() != EXT_DISABLED)
{
if (remote_resp_nr == 0) {
// This is the first response with a repsonse nr.
// Initialize the remote response nr
remote_resp_nr = r->hdr_rseq.resp_nr;
return false;
}
if (r->hdr_rseq.resp_nr <= remote_resp_nr) {
// This is a retransmission.
// PRACK has already been sent. The transaction
// layer takes care of retransmitting PRACK
// if PRACK got lost.
log_file->write_report("Discard 1xx retransmission.",
"t_dialog::must_discard_100rel");
return true;
}
if (r->hdr_rseq.resp_nr != remote_resp_nr + 1) {
// A provisional response has been lost.
// Discard this response and wait for a retransmission
// of the lost response.
log_file->write_report("Discard out-of-order 1xx",
"t_dialog::must_discard_100rel");
return true;
}
}
remote_resp_nr = r->hdr_rseq.resp_nr;
return false;
}
bool t_dialog::respond_prack(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
// RFC 3262 3
if (resp_1xx_invite &&
r->hdr_rack.method == resp_1xx_invite->hdr_cseq.method &&
r->hdr_rack.cseq_nr == resp_1xx_invite->hdr_cseq.seqnr &&
r->hdr_rack.resp_nr == resp_1xx_invite->hdr_rseq.resp_nr)
{
// The provisional response has been delivered now.
line->stop_timer(LTMR_100REL_TIMEOUT, get_object_id());
line->stop_timer(LTMR_100REL_GUARD, get_object_id());
MEMMAN_DELETE(resp_1xx_invite);
delete resp_1xx_invite;
resp_1xx_invite = NULL;
// Send 200 on the PRACK request
resp = r->create_response(R_200_OK);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return true;
} else {
// PRACK does not match pending 1xx response
// Send a 481 on the PRACK request
resp = r->create_response(R_481_TRANSACTION_NOT_EXIST);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return false;
}
}
void t_dialog::send_request(t_request *r, t_tuid tuid) {
line->send_request(r, tuid);
}
////////////
// Public
////////////
t_dialog::t_dialog(t_line *_line) :
t_abstract_dialog(_line->get_phone_user())
{
line = _line;
req_out = NULL;
req_out_invite = NULL;
req_in_invite = NULL;
req_cancel = NULL;
req_prack = NULL;
req_refer = NULL;
req_info = NULL;
req_stun = NULL;
request_cancelled = false;
end_after_ack = false;
end_after_2xx_invite = false;
answer_after_prack = false;
ringing_received = false;
resp_invite = NULL;
resp_1xx_invite = NULL;
ack = NULL;
state = DS_NULL;
// Timers
dur_ack_timeout = 0;
id_ack_timeout = 0;
id_ack_guard = 0;
id_re_invite_guard = 0;
id_glare_retry = 0;
id_cancel_guard = 0;
// RFC 3262
// Timers
dur_100rel_timeout = 0;
id_100rel_timeout = 0;
id_100rel_guard = 0;
t_user *user_config = phone_user->get_user_profile();
// Create session
session = new t_session(this, USER_HOST(user_config, AUTO_IP4_ADDRESS), line->get_rtp_port());
MEMMAN_NEW(session);
session_re_invite = NULL;
// Subscription
sub_refer = NULL;
is_referred_call = false;
refer_state = REFST_NULL;
refer_accepted = false;
refer_succeeded = false;
out_refer_req_failed = false;
}
t_dialog::~t_dialog() {
if (req_out) remove_client_request(&req_out);
if (req_out_invite) remove_client_request(&req_out_invite);
if (req_in_invite) remove_client_request(&req_in_invite);
if (req_cancel) remove_client_request(&req_cancel);
if (req_prack) remove_client_request(&req_prack);
if (req_refer) remove_client_request(&req_refer);
if (req_info) remove_client_request(&req_info);
if (req_stun) remove_client_request(&req_stun);
if (resp_invite) { MEMMAN_DELETE(resp_invite); delete resp_invite; }
if (resp_1xx_invite) {
MEMMAN_DELETE(resp_1xx_invite);
delete resp_1xx_invite;
}
if (ack) { MEMMAN_DELETE(ack); delete ack; }
if (session) { MEMMAN_DELETE(session); delete session; }
if (session_re_invite) {
MEMMAN_DELETE(session_re_invite);
delete session_re_invite;
}
if (sub_refer) { MEMMAN_DELETE(sub_refer); delete sub_refer; }
for (list<t_client_request *>::iterator i = inc_req_queue.begin();
i != inc_req_queue.end(); i++)
{
MEMMAN_DELETE(*i);
delete *i;
}
}
// Copy will only be used on the open dialog.
t_dialog *t_dialog::copy(void) {
t_dialog *d = new t_dialog(*this);
MEMMAN_NEW(d);
d->generate_new_id();
// Increment reference count on client request
if (req_out) d->req_out->inc_ref_count();
if (req_out_invite) d->req_out_invite->inc_ref_count();
if (req_in_invite) d->req_in_invite->inc_ref_count();
if (req_prack) d->req_prack->inc_ref_count();
if (req_refer) d->req_refer->inc_ref_count();
if (req_stun) d->req_stun->inc_ref_count();
// The open dialog will handle the CANCEL, so delete it
// from the copy.
if (req_cancel) d->req_cancel = NULL;
if (resp_invite) d->resp_invite = (t_response *)resp_invite->copy();
if (resp_1xx_invite) d->resp_1xx_invite = (t_response *)resp_1xx_invite->copy();
if (ack) d->ack = (t_request *)ack->copy();
dur_ack_timeout = 0;
id_ack_timeout = 0;
id_ack_guard = 0;
dur_100rel_timeout = 0;
id_100rel_timeout = 0;
id_100rel_guard = 0;
if (session) {
d->session = new t_session(*session);
MEMMAN_NEW(d->session);
d->session->set_owner(d);
// If an audio session was already created for early media
// then the audio session will be moved to the copy of the
// dialog. Only 1 dialog can have an audio session.
// See process_1xx_2xx_invite_resp for more information on
// early media problems.
// Clear a possible audio session in the open dialog.
t_audio_session *as = session->get_audio_session();
if (as) {
as->set_session(d->session);
session->set_audio_session(NULL);
log_file->write_report(
"An audio session was created on an open dialog.",
"t_dialog::copy",
LOG_NORMAL, LOG_DEBUG);
}
}
log_file->write_header("t_dialog::copy", LOG_NORMAL, LOG_DEBUG);
log_file->write_raw("Created dialog through copy, id=");
log_file->write_raw(d->get_object_id());
log_file->write_endl();
log_file->write_footer();
return d;
}
void t_dialog::send_invite(const t_url &to_uri, const string &to_display,
const string &subject, const t_hdr_referred_by &hdr_referred_by,
const t_hdr_replaces &hdr_replaces,
const t_hdr_require &hdr_require,
const t_hdr_request_disposition &hdr_request_disposition,
bool anonymous)
{
t_user *user_config = phone_user->get_user_profile();
if (state != DS_NULL) {
throw X_DIALOG_ALREADY_ESTABLISHED;
}
// If STUN is enabled, then first send a STUN binding request to
// discover the IP adderss and port for media.
if (phone->use_stun(user_config)) {
if (!stun_bind_media()) {
ui->cb_stun_failed_call_ended(line->get_line_number());
state = DS_TERMINATED;
return;
}
}
t_request invite(INVITE);
// RFC 3261 12.2.1.1
// Request URI and Route header
invite.set_route(to_uri, phone_user->get_service_route());
// Set Call-ID header
call_id = NEW_CALL_ID(user_config);
invite.hdr_call_id.set_call_id(call_id);
call_id_owner = true;
// Set To header
invite.hdr_to.set_uri(to_uri);
invite.hdr_to.set_display(to_display);
// Set From header
local_tag = NEW_TAG;
local_uri.set_url(line->create_user_uri());
local_display = user_config->get_display(anonymous);
invite.hdr_from.set_uri(local_uri);
invite.hdr_from.set_display(local_display);
invite.hdr_from.set_tag(local_tag);
// Privacy header
if (line->get_hide_user()) {
invite.hdr_privacy.add_privacy(PRIVACY_ID);
}
// Set P-Preferred-Identity header
if (anonymous && user_config->get_send_p_preferred_id()) {
t_identity identity;
identity.set_uri(user_config->create_user_uri(false));
identity.set_display(user_config->get_display(false));
invite.hdr_p_preferred_identity.add_identity(identity);
}
// Set CSeq header
local_seqnr = rand() % 1000 + 1;
invite.hdr_cseq.set_method(INVITE);
invite.hdr_cseq.set_seqnr(local_seqnr);
// Set Max-Forwards header
invite.hdr_max_forwards.set_max_forwards(MAX_FORWARDS);
// User-Agent
SET_HDR_USER_AGENT(invite.hdr_user_agent);
// RFC 3261 13.2.1
// Allow and Supported headers
SET_HDR_ALLOW(invite.hdr_allow, user_config);
SET_HDR_SUPPORTED(invite.hdr_supported, user_config);
// Extensions specific for INVITE
if (user_config->get_ext_100rel() != EXT_DISABLED) {
invite.hdr_supported.add_feature(EXT_100REL);
}
// Require header
switch (user_config->get_ext_100rel()) {
case EXT_PREFERRED:
case EXT_REQUIRED:
invite.hdr_require.add_feature(EXT_100REL);
break;
default:
break;
}
// Subject header
if (subject != "") {
invite.hdr_subject.set_subject(subject);
}
// Organization
if (!anonymous) {
SET_HDR_ORGANIZATION(invite.hdr_organization, user_config);
}
// RFC 3892 Referred-By header if a call is initated because
// of an incoming REFER.
invite.hdr_referred_by = hdr_referred_by;
// RFC 3891 Replaces header
invite.hdr_replaces = hdr_replaces;
// Add required extension passed by the upper layer
if (hdr_require.is_populated()) {
invite.hdr_require.add_features(hdr_require.features);
}
// RFC 3841 Request-Disposition header
invite.hdr_request_disposition = hdr_request_disposition;
// Calculate destinations
// See create_request() for more comments
invite.calc_destinations(*user_config);
// The Contatc, Via header and SDP can only be created after the destinations
// are calculated, because the destination deterimines which
// local IP address should be used.
// Create SDP offer
session->create_sdp_offer(&invite, SDP_O_USER);
// Set Via header
unsigned long local_ip = invite.get_local_ip();
t_via via(USER_HOST(user_config, h_ip2str(local_ip)), PUBLIC_SIP_PORT(user_config));
invite.hdr_via.add_via(via);
// Set Contact header
t_contact_param contact;
contact.uri.set_url(line->create_user_contact(h_ip2str(local_ip)));
invite.hdr_contact.add_contact(contact);
// Send INVITE
req_out_invite = new t_client_request(user_config, &invite, 0);
MEMMAN_NEW(req_out_invite);
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_OUT_CALL,
line->get_line_number() + 1);
script.exec_notify(&invite);
line->send_request(&invite, req_out_invite->get_tuid());
line->call_hist_record.start_call(&invite, t_call_record::DIR_OUT,
user_config->get_profile_name());
state = DS_W4INVITE_RESP;
}
bool t_dialog::resend_invite_auth(t_response *resp) {
t_user *user_config = phone_user->get_user_profile();
if (!req_out_invite) return false;
assert(state == DS_W4INVITE_RESP || state == DS_W4INVITE_RESP2);
t_request *req = req_out_invite->get_request();
// Add authorization header, increment CSeq and create new branch id
if (get_phone()->authorize(user_config, req, resp)) {
resend_request(req_out_invite);
// Reset state in case a 100 Trying was received
state = DS_W4INVITE_RESP;
return true;
}
return false;
}
bool t_dialog::resend_invite_unsupported(t_response *resp) {
t_user *user_config = phone_user->get_user_profile();
if (!req_out_invite) return false;
if (resp->code != R_420_BAD_EXTENSION) return false;
if (!resp->hdr_unsupported.is_populated()) return false;
if (resp->hdr_unsupported.features.empty()) return false;
t_request *req = req_out_invite->get_request();
// If no extensions were required then return.
if (!req->hdr_require.is_populated()) return false;
if (req->hdr_require.features.empty()) return false;
bool removed_ext = false;
for (list<string>::iterator i = resp->hdr_unsupported.features.begin();
i != resp->hdr_unsupported.features.end(); i++)
{
if (req->hdr_require.contains(*i)) {
if (*i == EXT_100REL) {
if (user_config->get_ext_100rel() == EXT_PREFERRED) {
req->hdr_require.del_feature(*i);
} else {
// The 100rel is required.
return false;
}
} else {
// There is no specific requirement for
// this extension so do not remove it.
return false;
}
removed_ext = true;
}
}
// Return if none of the unsupported extensions was required.
if (!removed_ext) return false;
if (req->hdr_require.features.empty()) {
// There are no required features anymore
req->hdr_require.unpopulate();
}
resend_request(req_out_invite);
// Reset state in case a 100 Trying was received
state = DS_W4INVITE_RESP;
return true;
}
bool t_dialog::redirect_invite(t_response *resp) {
t_contact_param contact;
t_user *user_config = phone_user->get_user_profile();
if (!req_out_invite) return false;
// If the response is a 3XX response then add redirection contacts
if (resp->get_class() == R_3XX && resp->hdr_contact.is_populated()) {
req_out_invite->redirector.add_contacts(
resp->hdr_contact.contact_list);
}
// Get next destination
if (!req_out_invite->redirector.get_next_contact(contact)) {
// There is no next destination
return false;
}
assert(state == DS_W4INVITE_RESP || state == DS_W4INVITE_RESP2);
t_request *req = req_out_invite->get_request();
// Ask user for permission to redirect if indicated by user config
if (user_config->get_ask_user_to_redirect()) {
if(!ui->cb_ask_user_to_redirect_invite(user_config,
contact.uri, contact.display))
{
// User did not permit to redirect
return false;
}
}
// Change the request URI to the new URI.
// As the URI changes the destination set must be recalculated
req->uri = contact.uri;
req->calc_destinations(*user_config);
ui->cb_redirecting_request(user_config, line->get_line_number(), contact);
resend_request(req_out_invite);
// Reset state in case a 100 Trying was received
state = DS_W4INVITE_RESP;
return true;
}
bool t_dialog::failover_invite(void) {
if (!req_out_invite) return false;
log_file->write_report("Failover to next destination.",
"t_dialog::failover_invite");
t_request *req = req_out_invite->get_request();
// Get next destination
if (!req->next_destination()) {
log_file->write_report("No next destination for failover.",
"t_dialog::failover_invite");
return false;
}
assert(state == DS_W4INVITE_RESP || state == DS_W4INVITE_RESP2);
resend_request(req_out_invite);
// Reset state in case a 100 Trying was received
state = DS_W4INVITE_RESP;
return true;
}
void t_dialog::send_bye(void) {
t_user *user_config = phone_user->get_user_profile();
switch (state) {
case DS_W4INVITE_RESP2:
case DS_EARLY:
case DS_CONFIRMED:
break;
case DS_W4RE_INVITE_RESP:
case DS_W4RE_INVITE_RESP2:
// send BYE after completion of re-INVITE
request_cancelled = true;
return;
case DS_W4ACK:
case DS_W4ACK_RE_INVITE:
// send BYE after completion of re-INVITE
request_cancelled = true;
return;
case DS_TERMINATED:
// Dialog has already been terminated. Do not send BYE.
return;
default:
log_file->write_header("t_dialog::failover_invite",
LOG_NORMAL, LOG_WARNING);
log_file->write_raw("Cannot send BYE on dialog in state ");
log_file->write_raw(state);
log_file->write_endl();
log_file->write_footer();
return;
}
// If a previous request is still pending then remove it.
if (req_out) { MEMMAN_DELETE(req_out); delete (req_out); }
t_request *bye = create_request(BYE);
req_out = new t_client_request(user_config, bye, 0);
MEMMAN_NEW(req_out);
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_LOCAL_RELEASE,
line->get_line_number() + 1);
script.exec_notify(bye);
line->send_request(bye, req_out->get_tuid());
line->call_hist_record.end_call(false);
MEMMAN_DELETE(bye);
delete bye;
state = DS_W4BYE_RESP;
ui->cb_call_ended(line->get_line_number());
}
void t_dialog::send_options(void) {
t_user *user_config = phone_user->get_user_profile();
// Request can only be sent in a confirmed dialog.
if (state != DS_CONFIRMED) return;
// If a previous request is still pending then remove it.
if (req_out) { MEMMAN_DELETE(req_out); delete (req_out); }
t_request *r = create_request(OPTIONS);
// Accept
r->hdr_accept.add_media(t_media("application","sdp"));
req_out = new t_client_request(user_config, r, 0);
MEMMAN_NEW(req_out);
line->send_request(r, req_out->get_tuid());
MEMMAN_DELETE(r);
delete r;
}
void t_dialog::send_cancel(bool early_dialog_exists) {
t_request *cancel;
t_user *user_config = phone_user->get_user_profile();
switch (state) {
case DS_W4INVITE_RESP:
case DS_W4RE_INVITE_RESP:
if (!early_dialog_exists) {
// wait for first response then send CANCEL or BYE
request_cancelled = true;
break;
}
// Fall through
case DS_W4INVITE_RESP2:
case DS_W4RE_INVITE_RESP2:
case DS_EARLY:
if (req_cancel) {
// CANCEL has been sent already
break;
}
cancel = create_request(CANCEL);
req_cancel = new t_client_request(user_config, cancel, 0);
MEMMAN_NEW(req_cancel);
line->send_request(cancel, req_cancel->get_tuid());
MEMMAN_DELETE(cancel);
delete cancel;
// Make sure dialog is terminated if CANCEL glares with
// 2XX on INVITE.
set_end_after_2xx_invite(true);
break;
default:
break;
}
ui->cb_call_ended(line->get_line_number());
}
void t_dialog::set_end_after_2xx_invite(bool on) {
end_after_2xx_invite = on;
}
void t_dialog::send_re_invite(void) {
assert(session_re_invite);
t_user *user_config = phone_user->get_user_profile();
// Request can only be sent in a confirmed dialog.
if (state != DS_CONFIRMED) return;
// Do nothing if a re-INVITE is already in progress
if (req_out_invite) return;
t_request *r = create_request(INVITE);
// Set Contact header
// INVITE must contain a contact header
t_contact_param contact;
contact.uri.set_url(line->create_user_contact(h_ip2str(r->get_local_ip())));
r->hdr_contact.add_contact(contact);
// RFC 3261 13.2.1
// Allow and Supported headers
SET_HDR_ALLOW(r->hdr_allow, user_config);
SET_HDR_SUPPORTED(r->hdr_supported, user_config);
// Extensions specific for INVITE
if (user_config->get_ext_100rel() != EXT_DISABLED) {
// If some weird far end implementation wants to send
// a reliable provisional then support it.
// As a provisional response not needed for a re-INVITE,
// do not require the 100rel.
r->hdr_supported.add_feature(EXT_100REL);
}
// Create SDP offer
session_re_invite->create_sdp_offer(r, SDP_O_USER);
// Send INVITE
req_out_invite = new t_client_request(user_config, r, 0);
MEMMAN_NEW(req_out_invite);
line->send_request(r, req_out_invite->get_tuid());
MEMMAN_DELETE(r);
delete r;
state = DS_W4RE_INVITE_RESP;
}
bool t_dialog::resend_request_auth(t_response *resp) {
t_client_request **current_cr;
switch (resp->hdr_cseq.method) {
case INVITE:
// re-INVITE
if (!req_out_invite) return false;
assert(state == DS_W4RE_INVITE_RESP ||
state == DS_W4RE_INVITE_RESP2);
current_cr = &req_out_invite;
break;
case PRACK:
if (!req_prack) return false;
current_cr = &req_prack;
break;
case REFER:
if (!req_refer) return false;
current_cr = &req_refer;
break;
case INFO:
if (!req_info) return false;
current_cr = &req_info;
break;
case SUBSCRIBE:
case NOTIFY:
if (!sub_refer) return false;
if (!sub_refer->req_out) return false;
current_cr = &(sub_refer->req_out);
break;
default:
// other requests
if (!req_out) return false;
current_cr = &req_out;
}
if (t_abstract_dialog::resend_request_auth(*current_cr, resp)) {
if (resp->hdr_cseq.method == INVITE) {
// Reset state in case a 100 Trying was received
state = DS_W4RE_INVITE_RESP;
}
return true;
}
return false;
}
bool t_dialog::redirect_request(t_response *resp) {
t_client_request **current_cr;
t_user *user_config = phone_user->get_user_profile();
if (resp->hdr_cseq.method == INVITE) {
// re-INVITE
if (!req_out_invite) return false;
assert(state == DS_W4RE_INVITE_RESP ||
state == DS_W4RE_INVITE_RESP2);
current_cr = &req_out_invite;
} else {
// non-INVITE
if (!req_out) return false;
current_cr = &req_out;
}
t_contact_param contact;
if (!t_abstract_dialog::redirect_request(*current_cr, resp, contact)) return false;
// Re-INVITE
if (resp->hdr_cseq.method == INVITE) {
// Reset state in case a 100 Trying was received
state = DS_W4RE_INVITE_RESP;
}
ui->cb_redirecting_request(user_config, line->get_line_number(), contact);
return true;
}
bool t_dialog::failover_request(t_response *resp) {
t_client_request **current_cr;
if (resp->hdr_cseq.method == INVITE) {
// re-INVITE
if (!req_out_invite) return false;
assert(state == DS_W4RE_INVITE_RESP ||
state == DS_W4RE_INVITE_RESP2);
current_cr = &req_out_invite;
} else {
// non-INVITE
if (!req_out) return false;
current_cr = &req_out;
}
if (!t_abstract_dialog::failover_request(*current_cr)) return false;
// Re-INVITE
if (resp->hdr_cseq.method == INVITE) {
// Reset state in case a 100 Trying was received
state = DS_W4RE_INVITE_RESP;
}
return true;
}
void t_dialog::hold(bool rtponly) {
assert(!session_re_invite);
// Stop glare retry timer
if (id_glare_retry) {
line->stop_timer(LTMR_GLARE_RETRY, get_object_id());
}
reinvite_purpose = REINVITE_HOLD;
if (rtponly) {
session->stop_rtp();
session->hold();
// Stopping the RTP only is like a full call hold where
// the re-INVITE failed. By setting the hold_failed flag,
// a subsequent retrieve will only start RTP.
hold_failed = true;
return;
}
hold_failed = false;
session_re_invite = session->create_call_hold();
send_re_invite();
// Stop the audio streams now. If we do not stop the stream now
// the stream will be stopped when a 200 OK is received on the
// re-INVITE. However, when the line is put on-hold because
// the user switches to another line that already has a held call
// a race condition might occur:
//
// 1. A re-INVITE on this line is sent to put it on-hold
// 2. A re-INVITE on the other line is sent to retrieve the call
// 3. If the 200 OK on the second re-INVITE comes in before the
// the 200 OK on the first re-INVITE, then the audio streams
// for the second line will be started already while the first
// line still has the audio device open. On some systems this
// causes a dead lock as the audio device may only be opened
// once.
//
// Also if the re-INVITE to put the line on-hold fails, the
// audio might not be stopped at all. It must be stopped however
// as the user has switched to the other line. So stopping the
// audio now will make sure the audio device is idle when the
// second call is retrieved.
session->stop_rtp();
// Prevent RTP stream from getting started even if the signaling
// for hold fails. After all the user has put the phone locally
// on-hold, so RTP should never be started.
session->hold();
}
void t_dialog::retrieve(void) {
assert(!session_re_invite);
t_user *user_config = phone_user->get_user_profile();
// Stop glare retry timer
if (id_glare_retry) {
line->stop_timer(LTMR_GLARE_RETRY, get_object_id());
}
// Allow RTP stream to be started again.
session->unhold();
// If the previous call-hold failed, then only RTP needs to
// be restarted. The session description did never change
// because of the failure.
if (hold_failed) {
session->start_rtp();
return;
}
// If STUN is enabled, then first send a STUN binding request to
// discover the IP adderss and port for media.
if (phone->use_stun(user_config)) {
if (!stun_bind_media()) {
// No re-INVITE can be sent. Simply return.
// User will decide if the call should be
// torn down.
return;
}
}
reinvite_purpose = REINVITE_RETRIEVE;
session_re_invite = session->create_call_retrieve();
send_re_invite();
}
void t_dialog::kill_rtp(void){
session->kill_rtp();
if (session_re_invite) session_re_invite->kill_rtp();
}
void t_dialog::send_refer(const t_url &uri, const string &display) {
t_user *user_config = phone_user->get_user_profile();
if (state != DS_CONFIRMED) return;
if (refer_state != REFST_NULL) return;
// If a previous refer is still in progress, then do nothing
if (req_refer) {
log_file->write_report("A REFER request is already in progress.",
"t_dialog::send_refer");
return;
}
// If a refer subscription already exists, then do nothing
if (sub_refer) {
log_file->write_report("Refer subscription exists already.",
"t_dialog::send_refer");
return;
}
t_request *refer = create_request(REFER);
// Refer-To header
refer->hdr_refer_to.set_uri(uri);
refer->hdr_refer_to.set_display(display);
// Referred-By header
refer->hdr_referred_by.set_uri(line->create_user_uri());
refer->hdr_referred_by.set_display(user_config->get_display(line->get_hide_user()));
req_refer = new t_client_request(user_config, refer, 0);
MEMMAN_NEW(req_refer);
line->send_request(refer, req_refer->get_tuid());
MEMMAN_DELETE(refer);
delete refer;
refer_succeeded = false;
out_refer_req_failed = false;
refer_state = REFST_W4RESP;
}
void t_dialog::send_dtmf(char digit, bool inband, bool info) {
t_user *user_config = phone_user->get_user_profile();
if (info) {
if (req_info) {
// An INFO request is still in progress, put the
// DTMF digit in the queue
dtmf_queue.push(digit);
} else {
t_request *info_request = create_request(INFO);
// Content-Type header
info_request->hdr_content_type.set_media(t_media("application", "dtmf-relay"));
// application/dtmf-relay body
info_request->body = new t_sip_body_dtmf_relay(digit,
user_config->get_dtmf_duration());
MEMMAN_NEW(info_request->body);
req_info = new t_client_request(user_config, info_request, 0);
MEMMAN_NEW(req_info);
line->send_request(info_request, req_info->get_tuid());
MEMMAN_DELETE(info_request);
delete info_request;
ui->cb_send_dtmf(line->get_line_number(), char2dtmf_ev(digit));
}
} else {
if (session) session->send_dtmf(digit, inband);
}
}
bool t_dialog::stun_bind_media(void) {
t_user *user_config = phone_user->get_user_profile();
try {
unsigned long mapped_ip;
unsigned short mapped_port;
int stun_err_code;
string stun_err_reason;
bool ret = get_stun_binding(user_config, line->get_rtp_port(),
mapped_ip, mapped_port,
stun_err_code, stun_err_reason);
if (!ret) {
// STUN request failed
ui->cb_stun_failed(user_config, stun_err_code, stun_err_reason);
log_file->write_header("t_dialog::stun_bind_media",
LOG_NORMAL, LOG_CRITICAL);
log_file->write_raw("STUN bind request for media failed.\n");
log_file->write_raw(stun_err_code);
log_file->write_raw(" ");
log_file->write_raw(stun_err_reason);
log_file->write_endl();
log_file->write_footer();
return false;
}
// STUN binding request succeeded.
session->receive_host = h_ip2str(mapped_ip);
session->receive_port = mapped_port;
} catch (int err) {
// STUN request failed
ui->cb_stun_failed(user_config);
log_file->write_header("t_dialog::stun_bind_media",
LOG_NORMAL, LOG_CRITICAL);
log_file->write_raw("STUN bind request for media failed.\n");
log_file->write_raw(get_error_str(err));
log_file->write_endl();
log_file->write_footer();
return false;
}
return true;
}
void t_dialog::recvd_response(t_response *r, t_tuid tuid, t_tid tid) {
t_user *user_config = phone_user->get_user_profile();
t_abstract_dialog::recvd_response(r, tuid, tid);
if (r->hdr_cseq.method == INVITE &&
tuid == 0 && tid == 0 && !req_out_invite)
{
t_ip_port ip_port;
// Only a retransmission of a 2XX INVITE is allowed.
if (r->get_class() != R_2XX) return;
if (!ack) return;
if (r->hdr_cseq.seqnr != ack->hdr_cseq.seqnr)
{
// The 2XX response does not match the ACK
return;
}
ack->get_destination(ip_port, *user_config);
if (ip_port.ipaddr != 0 && ip_port.port != 0) {
evq_sender->push_network(ack, ip_port);
}
return;
}
if (r->hdr_cseq.method == CANCEL) {
if (!req_cancel) return;
if (r->is_final()) {
remove_client_request(&req_cancel);
if (r->is_success()) {
line->start_timer(LTMR_CANCEL_GUARD, get_object_id());
} else {
// CANCEL request failed.
ui->cb_cancel_failed(line->get_line_number(), r);
// Abort the INVITE as the user cannot terminate
// it in a normal way.
if (req_out_invite) {
t_tid _tid = req_out_invite->get_tid();
if (_tid > 0) {
evq_trans_mgr->push_abort_trans(_tid);
}
}
}
}
return;
}
// No processing done for PRACK responses.
if (r->hdr_cseq.method == PRACK) {
if (!req_prack) return;
t_request *prack = req_prack->get_request();
if (r->hdr_cseq.seqnr != prack->hdr_cseq.seqnr) {
// The response does not match the latest sent PRACK.
// It might match a previous sent PRACK. However, when
// a previous PRACK fails, then the latest PRACK will also
// fail, so the failure will be handled in the end without
// the overhead to keep a list of all pending PRACKs which
// should be a rare case.
return;
}
if (r->is_final()) {
// PRACK is finished, so remove request
remove_client_request(&req_prack);
// Tear down the call if PRACK failed and call is
// not yet established.
if (!r->is_success() && state == DS_EARLY) {
log_file->write_header("t_dialog::recvd_response",
LOG_NORMAL, LOG_WARNING);
log_file->write_raw("PRACK failed: ");
log_file->write_raw(r->code);
log_file->write_raw(" ");
log_file->write_raw(r->reason);
log_file->write_endl();
log_file->write_raw("Call will be cancelled.\n");
log_file->write_footer();
ui->cb_prack_failed(line->get_line_number(), r);
send_cancel(true);
// Ignore the failure in other states.
// The call has been setup, so all seems fine.
}
}
return;
}
// Determine if this is an INVITE or non-INVITE response
t_client_request *req;
bool send_to_sub_refer = false;
switch(r->hdr_cseq.method) {
case INVITE:
req = req_out_invite;
break;
case SUBSCRIBE:
case NOTIFY:
if (!sub_refer) return;
req = sub_refer->req_out;
send_to_sub_refer = true;
break;
case REFER:
req = req_refer;
break;
case INFO:
req = req_info;
break;
default:
req = req_out;
}
// Discard response if no request is pending
if (!req) {
return;
}
// Check cseq
if (r->hdr_cseq.method != req->get_request()->method) {
return;
}
if (r->hdr_cseq.seqnr != req->get_request()->hdr_cseq.seqnr) return;
// Set the transaction identifier. This identifier is needed if the
// transaction must be aborted at a later time.
req->set_tid(tid);
if (send_to_sub_refer) {
sub_refer->recv_response(r, tuid, tid);
if (sub_refer->get_state() == SS_TERMINATED) {
MEMMAN_DELETE(sub_refer);
delete sub_refer;
sub_refer = NULL;
if (state == DS_CONFIRMED_SUB) {
state = DS_TERMINATED;
}
}
return;
}
switch (state) {
case DS_W4INVITE_RESP:
case DS_W4INVITE_RESP2:
state_w4invite_resp(r, tuid, tid);
break;
case DS_EARLY:
state_early(r, tuid, tid);
break;
case DS_W4BYE_RESP:
state_w4bye_resp(r, tuid, tid);
break;
case DS_CONFIRMED:
state_confirmed_resp(r, tuid, tid);
break;
case DS_W4RE_INVITE_RESP:
case DS_W4RE_INVITE_RESP2:
state_w4re_invite_resp(r, tuid, tid);
break;
default:
// No response expected in other states. Discard.
break;
}
}
void t_dialog::recvd_request(t_request *r, t_tuid tuid, t_tid tid) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
// CANCEL will be handled by recvd_cancel()
t_abstract_dialog::recvd_request(r, tuid, tid);
switch (r->method) {
case ACK:
// When ACK is received then the current incoming request
// must be INVITE.
if (!req_in_invite) return;
if (req_in_invite->get_request()->hdr_cseq.seqnr !=
r->hdr_cseq.seqnr)
{
log_file->write_header("t_dialog::recvd_request",
LOG_NORMAL, LOG_WARNING);
log_file->write_raw("ACK does not match a pending INVITE.\n");
log_file->write_raw("Discard ACK.\n");
log_file->write_footer();
return;
}
break;
case INVITE:
if (remote_seqnr_set && r->hdr_cseq.seqnr <= remote_seqnr) {
// Request received out of sequence. Discard.
log_file->write_header("t_dialog::recvd_request",
LOG_NORMAL, LOG_WARNING);
log_file->write_raw("INVITE is received out of order.\n");
log_file->write_raw("Remote seqnr = ");
log_file->write_raw(remote_seqnr);
log_file->write_endl();
log_file->write_raw("Received seqnr = ");
log_file->write_raw(r->hdr_cseq.seqnr);
log_file->write_endl();
log_file->write_raw("Discard INVITE.\n");
log_file->write_footer();
return;
}
remote_seqnr = r->hdr_cseq.seqnr;
remote_seqnr_set = true;
if (req_in_invite) {
// RFC 3261 14.2
// Another INVITE is received while the previous
// one is not finished.
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR,
"Previous INVITE still in progress");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
} else if (req_out_invite) {
// RFC 3261 14.2
// re-INVITE glare
resp = r->create_response(R_491_REQUEST_PENDING);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
} else {
req_in_invite = new t_client_request(user_config, r, tid);
MEMMAN_NEW(req_in_invite);
}
break;
case REFER:
// Reset refer_accepted indication.
refer_accepted = false;
// fall thru
default:
// Check cseq
// RFC 3261 12.2.2
if (remote_seqnr_set && r->hdr_cseq.seqnr <= remote_seqnr) {
// Request received out of order.
log_file->write_header("t_dialog::recvd_request",
LOG_NORMAL, LOG_WARNING);
log_file->write_raw("CSeq seqnr is out of sequence.\n");
log_file->write_raw("Reveived seqnr: ");
log_file->write_raw(r->hdr_cseq.seqnr);
log_file->write_endl();
log_file->write_raw("Remote seqnr: ");
log_file->write_raw(remote_seqnr);
log_file->write_endl();
log_file->write_footer();
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR,
"Request received out of order");
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
return;
}
remote_seqnr = r->hdr_cseq.seqnr;
remote_seqnr_set = true;
}
t_dialog_state old_state = state;
switch (state) {
case DS_NULL:
state_null(r, tuid, tid);
break;
case DS_W4ACK:
state_w4ack(r, tuid, tid);
break;
case DS_W4ACK_RE_INVITE:
state_w4ack_re_invite(r, tuid, tid);
break;
case DS_W4ANSWER:
state_w4answer(r, tuid, tid);
break;
case DS_W4RE_INVITE_RESP:
case DS_W4RE_INVITE_RESP2:
state_w4re_invite_resp(r, tuid, tid);
break;
case DS_W4BYE_RESP:
state_w4bye_resp(r, tuid, tid);
break;
case DS_CONFIRMED:
state_confirmed(r, tuid, tid);
break;
case DS_CONFIRMED_SUB:
state_confirmed_sub(r, tuid, tid);
break;
default:
// No request expected in other states. Discard.
resp = r->create_response(R_500_INTERNAL_SERVER_ERROR);
line->send_response(resp, tuid, tid);
MEMMAN_DELETE(resp);
delete resp;
break;
}
// If the state has changed, then waiting requests needs to be
// processed.
if (state != old_state && !inc_req_queue.empty()) {
t_client_request *queued_cr = inc_req_queue.front();
inc_req_queue.pop_front();
log_file->write_header("t_dialog::recvd_request",
LOG_NORMAL, LOG_INFO);
log_file->write_raw("Process queued ");
log_file->write_raw(method2str(r->method, r->unknown_method));
log_file->write_endl();
log_file->write_footer();
recvd_request(queued_cr->get_request(), 0, queued_cr->get_tid());
MEMMAN_DELETE(queued_cr);
delete queued_cr;
}
}
// RFC 3261 9.2
void t_dialog::recvd_cancel(t_request *r, t_tid cancel_tid,
t_tid target_tid)
{
t_response *resp;
assert(r->method == CANCEL);
// Send 200 as response to CANCEL
resp = r->create_response(R_200_OK);
// RFC 3261 9.2
// The To-tag in the response to the CANCEL should be the same
// as the To-tag in the original request.
resp->hdr_to.set_tag(local_tag);
line->send_response(resp, 0, cancel_tid);
MEMMAN_DELETE(resp);
delete resp;
switch (state) {
case DS_W4ANSWER:
state_w4answer(r, 0, cancel_tid);
break;
default:
// Ignore CANCEL in other states.
break;
}
}
void t_dialog::recvd_stun_resp(StunMessage *r, t_tuid tuid, t_tid tid) {
// Not used anymore.
// STUN requests are performed in a synchronous way.
}
// RFC 3261 13.3.1.4
void t_dialog::answer(void) {
t_user *user_config = phone_user->get_user_profile();
if (!req_in_invite) return;
t_request *invite_req = req_in_invite->get_request();
// RFC 3262 3
// Delay the final response if we are still waiting for a PRACK
// on a 1xx response containing SDP
if (resp_1xx_invite && resp_1xx_invite->body) {
answer_after_prack = true;
return;
}
if (state != DS_W4ANSWER) {
throw X_WRONG_STATE;
}
resp_invite = invite_req->create_response(R_200_OK);
resp_invite->hdr_to.set_tag(local_tag);
// Set Organization header
SET_HDR_ORGANIZATION(resp_invite->hdr_organization, user_config);
// RFC 3261 12.1.1
// Copy the Record-Route header from request to response
if (invite_req->hdr_record_route.is_populated()) {
resp_invite->hdr_record_route = invite_req->hdr_record_route;
}
// Set Contact header
t_contact_param contact;
contact.uri.set_url(line->create_user_contact(h_ip2str(resp_invite->get_local_ip())));
resp_invite->hdr_contact.add_contact(contact);
// Set Allow and Supported headers
SET_HDR_ALLOW(resp_invite->hdr_allow, user_config);
SET_HDR_SUPPORTED(resp_invite->hdr_supported, user_config);
// RFC 3261 13.3.1.4
// Create SDP offer if no offer was received in INVITE and no offer
// was sent in a reliable 1xx response (RFC 3262 5)
// Otherwise if no offer was sent in a reliable 1xx, create an SDP answer.
if (!session->sent_offer) {
if (!session->recvd_offer && !session->sent_offer) {
session->create_sdp_offer(resp_invite, SDP_O_USER);
} else {
session->create_sdp_answer(resp_invite, SDP_O_USER);
session->start_rtp();
}
}
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_IN_CALL_ANSWERED,
line->get_line_number() + 1);
script.exec_notify(resp_invite);
line->call_hist_record.answer_call(resp_invite);
line->send_response(resp_invite, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->start_timer(LTMR_ACK_GUARD, get_object_id());
line->start_timer(LTMR_ACK_TIMEOUT, get_object_id());
// Stop 100rel timers if they are running.
line->stop_timer(LTMR_100REL_GUARD, get_object_id());
line->stop_timer(LTMR_100REL_TIMEOUT, get_object_id());
state = DS_W4ACK;
}
void t_dialog::reject(int code, string reason) {
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
if (state != DS_W4ANSWER) {
throw X_WRONG_STATE;
}
assert(req_in_invite);
assert(code >= 400);
resp = req_in_invite->get_request()->create_response(code, reason);
resp->hdr_to.set_tag(local_tag);
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_IN_CALL_FAILED,
line->get_line_number() + 1);
script.exec_notify(resp);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
MEMMAN_DELETE(resp);
delete resp;
// Stop 100rel timers if they are running.
line->stop_timer(LTMR_100REL_GUARD, get_object_id());
line->stop_timer(LTMR_100REL_TIMEOUT, get_object_id());
state = DS_TERMINATED;
}
void t_dialog::redirect(const list<t_display_url> &destinations, int code, string reason)
{
t_response *resp;
t_user *user_config = phone_user->get_user_profile();
if (state != DS_W4ANSWER) {
throw X_WRONG_STATE;
}
assert(req_in_invite);
assert(code >= 300 && code <= 399);
resp = req_in_invite->get_request()->create_response(code, reason);
resp->hdr_to.set_tag(local_tag);
t_contact_param *contact;
float q = 0.9;
for (list<t_display_url>::const_iterator i = destinations.begin();
i != destinations.end(); i++)
{
contact = new t_contact_param();
MEMMAN_NEW(contact);
contact->display = i->display;
contact->uri = i->url;
contact->set_qvalue(q);
resp->hdr_contact.add_contact(*contact);
MEMMAN_DELETE(contact);
delete contact;
q = q - 0.1;
if (q < 0.1) q = 0.1;
}
// Trigger call script
t_call_script script(user_config, t_call_script::TRIGGER_IN_CALL_FAILED,
line->get_line_number() + 1);
script.exec_notify(resp);
line->send_response(resp, req_in_invite->get_tuid(),
req_in_invite->get_tid());
line->call_hist_record.fail_call(resp);
MEMMAN_DELETE(resp);
delete resp;
// Stop 100rel timers if they are running.
line->stop_timer(LTMR_100REL_GUARD, get_object_id());
line->stop_timer(LTMR_100REL_TIMEOUT, get_object_id());
state = DS_TERMINATED;
}
bool t_dialog::match_response(t_response *r, t_tuid tuid) {
if (tuid != 0) {
if (req_out && req_out->get_tuid() == tuid) return true;
if (req_out_invite && req_out_invite->get_tuid() == tuid) {
return true;
}
if (req_cancel && req_cancel->get_tuid() == tuid) {
return true;
}
return false;
}
// The implementation sends CANCEL on the open dialog.
// The tags of a CANCEL response will be identical to the tags of
// the INVITE, so it matches all pending dialogs as well.
// So a CANCEL should only match if the dialog has a CANCEL request
// pending.
if (r->hdr_cseq.method == CANCEL && !req_cancel) return false;
return t_abstract_dialog::match_response(r, tuid);
}
bool t_dialog::match_response(StunMessage *r, t_tuid tuid) {
if (tuid == 0) return false;
if (!req_stun) return false;
return (req_stun->get_tuid() == tuid);
}
bool t_dialog::match_cancel(t_request *r, t_tid target_tid) {
return (req_in_invite && req_in_invite->get_tid() == target_tid);
}
bool t_dialog::is_invite_retrans(t_request *r) {
assert(r->method == INVITE);
// An INVITE can only be a retransmission if an incoming INVITE is
// still in progress.
if (!req_in_invite) return false;
t_request *request = req_in_invite->get_request();
// RFC 3261 17.2.3
t_via &orig_top_via = request->hdr_via.via_list.front();
t_via &recv_top_via = r->hdr_via.via_list.front();
if (recv_top_via.rfc3261_compliant()) {
if (orig_top_via.branch != recv_top_via.branch) return false;
if (orig_top_via.host != recv_top_via.host) return false;
if (orig_top_via.port != recv_top_via.port) return false;
return (request->hdr_cseq.method == r->hdr_cseq.method);
}
// Matching rules for backward compatibiliy with RFC 2543
// TODO: verify rules for matching via headers
return (request->uri.sip_match(r->uri) &&
request->hdr_to.tag == r->hdr_to.tag &&
request->hdr_from.tag == r->hdr_from.tag &&
request->hdr_call_id.call_id == r->hdr_call_id.call_id &&
request->hdr_cseq.seqnr == r->hdr_cseq.seqnr &&
orig_top_via.host == recv_top_via.host &&
orig_top_via.port == recv_top_via.port);
}
void t_dialog::process_invite_retrans(void) {
t_ip_port ip_port;
// Retransmit 2xx response.
// Send the response directly to the sender thread
// as the INVITE transaction completed already.
// (see RFC 3261 17.2.1)
if (!resp_invite) return; // there is no response to send
resp_invite->get_destination(ip_port);
if (ip_port.ipaddr == 0) {
// This should not happen. The response has been
// sent before so it should be possible to sent
// it again. Ignore the timeout. When the ACK
// guard timer expires, the dialog will be
// cleaned up.
return;
}
evq_sender->push_network(resp_invite, ip_port);
}
t_dialog_state t_dialog::get_state(void) const {
return state;
}
void t_dialog::timeout(t_line_timer timer) {
switch(state) {
case DS_W4INVITE_RESP:
case DS_W4INVITE_RESP2:
state_w4invite_resp(timer);
break;
case DS_EARLY:
state_early(timer);
break;
case DS_W4ACK:
state_w4ack(timer);
break;
case DS_W4ACK_RE_INVITE:
state_w4ack_re_invite(timer);
break;
case DS_W4RE_INVITE_RESP2:
state_w4re_invite_resp(timer);
break;
case DS_W4ANSWER:
state_w4answer(timer);
break;
case DS_CONFIRMED:
state_confirmed(timer);
break;
default:
// Timeout not expected in other states. Ignore.
break;
}
}
void t_dialog::timeout_sub(t_subscribe_timer timer, const string &event_type,
const string &event_id)
{
if (sub_refer &&
sub_refer->get_event_type() == event_type &&
sub_refer->get_event_id() == event_id)
{
sub_refer->timeout(timer);
} else {
// Timeout does not match with the current subscription.
// Ignore.
return;
}
if (sub_refer->get_state() == SS_TERMINATED && state == DS_CONFIRMED_SUB) {
MEMMAN_DELETE(sub_refer);
delete sub_refer;
sub_refer = NULL;
state = DS_TERMINATED;
}
}
t_phone *t_dialog::get_phone(void) const {
return line->get_phone();
}
t_line *t_dialog::get_line(void) const {
return line;
}
t_session *t_dialog::get_session(void) const {
return session;
}
t_audio_session *t_dialog::get_audio_session(void) const {
if (!session) return NULL;
return session->get_audio_session();
}
bool t_dialog::has_active_session(void) const {
if (session) return session->is_rtp_active();
return false;
}
// RFC 3515
// Send a NOTIFY with reference progress to the referror
void t_dialog::notify_refer_progress(t_response *r) {
if (!sub_refer) return;
if (r->is_final()) {
sub_refer->send_notify(r, SUBSTATE_TERMINATED, EV_REASON_NORESOURCE);
} else {
sub_refer->send_notify(r, SUBSTATE_ACTIVE);
}
}
bool t_dialog::will_release(void) const {
return state == DS_W4BYE_RESP || request_cancelled ||
end_after_2xx_invite || end_after_ack;
}
|