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
|
#include <cassert>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <errno.h>
#ifdef WIN32
#include <winsock2.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#else
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <net/if.h>
#endif
#if defined(__sparc__) || defined(WIN32)
#define NOSSL
#endif
#define NOSSL
#include "udp.h"
#include "stun.h"
// Twinkle
#include "util.h"
#include "sockets/socket.h"
#include "audits/memman.h"
using namespace std;
static void
computeHmac(char* hmac, const char* input, int length, const char* key, int keySize);
static bool
stunParseAtrAddress( char* body, unsigned int hdrLen, StunAtrAddress4& result )
{
if ( hdrLen != 8 )
{
clog << "hdrLen wrong for Address" <<endl;
return false;
}
result.pad = *body++;
result.family = *body++;
if (result.family == IPv4Family)
{
UInt16 nport;
memcpy(&nport, body, 2); body+=2;
result.ipv4.port = ntohs(nport);
UInt32 naddr;
memcpy(&naddr, body, 4); body+=4;
result.ipv4.addr = ntohl(naddr);
return true;
}
else if (result.family == IPv6Family)
{
clog << "ipv6 not supported" << endl;
}
else
{
clog << "bad address family: " << result.family << endl;
}
return false;
}
static bool
stunParseAtrChangeRequest( char* body, unsigned int hdrLen, StunAtrChangeRequest& result )
{
if ( hdrLen != 4 )
{
clog << "hdr length = " << hdrLen << " expecting " << sizeof(result) << endl;
clog << "Incorrect size for ChangeRequest" << endl;
return false;
}
else
{
memcpy(&result.value, body, 4);
result.value = ntohl(result.value);
return true;
}
}
static bool
stunParseAtrError( char* body, unsigned int hdrLen, StunAtrError& result )
{
if ( hdrLen >= sizeof(result) )
{
clog << "head on Error too large" << endl;
return false;
}
else
{
memcpy(&result.pad, body, 2); body+=2;
result.pad = ntohs(result.pad);
result.errorClass = *body++;
result.number = *body++;
result.sizeReason = hdrLen - 4;
memcpy(&result.reason, body, result.sizeReason);
result.reason[result.sizeReason] = 0;
return true;
}
}
static bool
stunParseAtrUnknown( char* body, unsigned int hdrLen, StunAtrUnknown& result )
{
if ( hdrLen >= sizeof(result) )
{
return false;
}
else
{
if (hdrLen % 4 != 0) return false;
result.numAttributes = hdrLen / 4;
for (int i=0; i<result.numAttributes; i++)
{
memcpy(&result.attrType[i], body, 2); body+=2;
result.attrType[i] = ntohs(result.attrType[i]);
}
return true;
}
}
static bool
stunParseAtrString( char* body, unsigned int hdrLen, StunAtrString& result )
{
if ( hdrLen >= STUN_MAX_STRING )
{
clog << "String is too large" << endl;
return false;
}
else
{
if (hdrLen % 4 != 0)
{
clog << "Bad length string " << hdrLen << endl;
return false;
}
result.sizeValue = hdrLen;
memcpy(&result.value, body, hdrLen);
result.value[hdrLen] = 0;
return true;
}
}
static bool
stunParseAtrIntegrity( char* body, unsigned int hdrLen, StunAtrIntegrity& result )
{
if ( hdrLen != 20)
{
clog << "MessageIntegrity must be 20 bytes" << endl;
return false;
}
else
{
memcpy(&result.hash, body, hdrLen);
return true;
}
}
bool
stunParseMessage( char* buf, unsigned int bufLen, StunMessage& msg, bool verbose)
{
if (verbose) clog << "Received stun message: " << bufLen << " bytes" << endl;
memset(&msg, 0, sizeof(msg));
if (sizeof(StunMsgHdr) > bufLen)
{
return false;
}
memcpy(&msg.msgHdr, buf, sizeof(StunMsgHdr));
msg.msgHdr.msgType = ntohs(msg.msgHdr.msgType);
msg.msgHdr.msgLength = ntohs(msg.msgHdr.msgLength);
if (msg.msgHdr.msgLength + sizeof(StunMsgHdr) != bufLen)
{
clog << "Message header length doesn't match message size: " << msg.msgHdr.msgLength << " - " << bufLen << endl;
return false;
}
char* body = buf + sizeof(StunMsgHdr);
unsigned int size = msg.msgHdr.msgLength;
//clog << "bytes after header = " << size << endl;
while ( size > 0 )
{
// !jf! should check that there are enough bytes left in the buffer
StunAtrHdr* attr = reinterpret_cast<StunAtrHdr*>(body);
unsigned int attrLen = ntohs(attr->length);
int atrType = ntohs(attr->type);
//if (verbose) clog << "Found attribute type=" << AttrNames[atrType] << " length=" << attrLen << endl;
if ( attrLen+4 > size )
{
clog << "claims attribute is larger than size of message " <<"(attribute type="<<atrType<<")"<< endl;
return false;
}
body += 4; // skip the length and type in attribute header
size -= 4;
switch ( atrType )
{
case MappedAddress:
msg.hasMappedAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.mappedAddress )== false )
{
clog << "problem parsing MappedAddress" << endl;
return false;
}
else
{
if (verbose) clog << "MappedAddress = " << msg.mappedAddress.ipv4 << endl;
}
break;
case ResponseAddress:
msg.hasResponseAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.responseAddress )== false )
{
clog << "problem parsing ResponseAddress" << endl;
return false;
}
else
{
if (verbose) clog << "ResponseAddress = " << msg.responseAddress.ipv4 << endl;
}
break;
case ChangeRequest:
msg.hasChangeRequest = true;
if (stunParseAtrChangeRequest( body, attrLen, msg.changeRequest) == false)
{
clog << "problem parsing ChangeRequest" << endl;
return false;
}
else
{
if (verbose) clog << "ChangeRequest = " << msg.changeRequest.value << endl;
}
break;
case SourceAddress:
msg.hasSourceAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.sourceAddress )== false )
{
clog << "problem parsing SourceAddress" << endl;
return false;
}
else
{
if (verbose) clog << "SourceAddress = " << msg.sourceAddress.ipv4 << endl;
}
break;
case ChangedAddress:
msg.hasChangedAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.changedAddress )== false )
{
clog << "problem parsing ChangedAddress" << endl;
return false;
}
else
{
if (verbose) clog << "ChangedAddress = " << msg.changedAddress.ipv4 << endl;
}
break;
case Username:
msg.hasUsername = true;
if (stunParseAtrString( body, attrLen, msg.username) == false)
{
clog << "problem parsing Username" << endl;
return false;
}
else
{
if (verbose) clog << "Username = " << msg.username.value << endl;
}
break;
case Password:
msg.hasPassword = true;
if (stunParseAtrString( body, attrLen, msg.password) == false)
{
clog << "problem parsing Password" << endl;
return false;
}
else
{
if (verbose) clog << "Password = " << msg.password.value << endl;
}
break;
case MessageIntegrity:
msg.hasMessageIntegrity = true;
if (stunParseAtrIntegrity( body, attrLen, msg.messageIntegrity) == false)
{
clog << "problem parsing MessageIntegrity" << endl;
return false;
}
else
{
//if (verbose) clog << "MessageIntegrity = " << msg.messageIntegrity.hash << endl;
}
// read the current HMAC
// look up the password given the user of given the transaction id
// compute the HMAC on the buffer
// decide if they match or not
break;
case ErrorCode:
msg.hasErrorCode = true;
if (stunParseAtrError(body, attrLen, msg.errorCode) == false)
{
clog << "problem parsing ErrorCode" << endl;
return false;
}
else
{
if (verbose) clog << "ErrorCode = " << int(msg.errorCode.errorClass)
<< " " << int(msg.errorCode.number)
<< " " << msg.errorCode.reason << endl;
}
break;
case UnknownAttribute:
msg.hasUnknownAttributes = true;
if (stunParseAtrUnknown(body, attrLen, msg.unknownAttributes) == false)
{
clog << "problem parsing UnknownAttribute" << endl;
return false;
}
break;
case ReflectedFrom:
msg.hasReflectedFrom = true;
if ( stunParseAtrAddress( body, attrLen, msg.reflectedFrom ) == false )
{
clog << "problem parsing ReflectedFrom" << endl;
return false;
}
break;
case XorMappedAddress:
msg.hasXorMappedAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.xorMappedAddress ) == false )
{
clog << "problem parsing XorMappedAddress" << endl;
return false;
}
else
{
if (verbose) clog << "XorMappedAddress = " << msg.mappedAddress.ipv4 << endl;
}
break;
case XorOnly:
msg.xorOnly = true;
if (verbose)
{
clog << "xorOnly = true" << endl;
}
break;
case ServerName:
msg.hasServerName = true;
if (stunParseAtrString( body, attrLen, msg.serverName) == false)
{
clog << "problem parsing ServerName" << endl;
return false;
}
else
{
if (verbose) clog << "ServerName = " << msg.serverName.value << endl;
}
break;
case SecondaryAddress:
msg.hasSecondaryAddress = true;
if ( stunParseAtrAddress( body, attrLen, msg.secondaryAddress ) == false )
{
clog << "problem parsing secondaryAddress" << endl;
return false;
}
else
{
if (verbose) clog << "SecondaryAddress = " << msg.secondaryAddress.ipv4 << endl;
}
break;
default:
if (verbose) clog << "Unknown attribute: " << atrType << endl;
if ( atrType <= 0x7FFF )
{
return false;
}
}
body += attrLen;
size -= attrLen;
}
return true;
}
static char*
encode16(char* buf, UInt16 data)
{
UInt16 ndata = htons(data);
memcpy(buf, reinterpret_cast<void*>(&ndata), sizeof(UInt16));
return buf + sizeof(UInt16);
}
static char*
encode32(char* buf, UInt32 data)
{
UInt32 ndata = htonl(data);
memcpy(buf, reinterpret_cast<void*>(&ndata), sizeof(UInt32));
return buf + sizeof(UInt32);
}
static char*
encode(char* buf, const char* data, unsigned int length)
{
memcpy(buf, data, length);
return buf + length;
}
static char*
encodeAtrAddress4(char* ptr, UInt16 type, const StunAtrAddress4& atr)
{
ptr = encode16(ptr, type);
ptr = encode16(ptr, 8);
*ptr++ = atr.pad;
*ptr++ = IPv4Family;
ptr = encode16(ptr, atr.ipv4.port);
ptr = encode32(ptr, atr.ipv4.addr);
return ptr;
}
static char*
encodeAtrChangeRequest(char* ptr, const StunAtrChangeRequest& atr)
{
ptr = encode16(ptr, ChangeRequest);
ptr = encode16(ptr, 4);
ptr = encode32(ptr, atr.value);
return ptr;
}
static char*
encodeAtrError(char* ptr, const StunAtrError& atr)
{
ptr = encode16(ptr, ErrorCode);
ptr = encode16(ptr, 6 + atr.sizeReason);
ptr = encode16(ptr, atr.pad);
*ptr++ = atr.errorClass;
*ptr++ = atr.number;
ptr = encode(ptr, atr.reason, atr.sizeReason);
return ptr;
}
static char*
encodeAtrUnknown(char* ptr, const StunAtrUnknown& atr)
{
ptr = encode16(ptr, UnknownAttribute);
ptr = encode16(ptr, 2+2*atr.numAttributes);
for (int i=0; i<atr.numAttributes; i++)
{
ptr = encode16(ptr, atr.attrType[i]);
}
return ptr;
}
static char*
encodeXorOnly(char* ptr)
{
ptr = encode16(ptr, XorOnly );
return ptr;
}
static char*
encodeAtrString(char* ptr, UInt16 type, const StunAtrString& atr)
{
assert(atr.sizeValue % 4 == 0);
ptr = encode16(ptr, type);
ptr = encode16(ptr, atr.sizeValue);
ptr = encode(ptr, atr.value, atr.sizeValue);
return ptr;
}
static char*
encodeAtrIntegrity(char* ptr, const StunAtrIntegrity& atr)
{
ptr = encode16(ptr, MessageIntegrity);
ptr = encode16(ptr, 20);
ptr = encode(ptr, atr.hash, sizeof(atr.hash));
return ptr;
}
unsigned int
stunEncodeMessage( const StunMessage& msg,
char* buf,
unsigned int bufLen,
const StunAtrString& password,
bool verbose)
{
assert(bufLen >= sizeof(StunMsgHdr));
char* ptr = buf;
ptr = encode16(ptr, msg.msgHdr.msgType);
char* lengthp = ptr;
ptr = encode16(ptr, 0);
ptr = encode(ptr, reinterpret_cast<const char*>(msg.msgHdr.id.octet), sizeof(msg.msgHdr.id));
if (verbose) clog << "Encoding stun message: " << endl;
if (msg.hasMappedAddress)
{
if (verbose) clog << "Encoding MappedAddress: " << msg.mappedAddress.ipv4 << endl;
ptr = encodeAtrAddress4 (ptr, MappedAddress, msg.mappedAddress);
}
if (msg.hasResponseAddress)
{
if (verbose) clog << "Encoding ResponseAddress: " << msg.responseAddress.ipv4 << endl;
ptr = encodeAtrAddress4(ptr, ResponseAddress, msg.responseAddress);
}
if (msg.hasChangeRequest)
{
if (verbose) clog << "Encoding ChangeRequest: " << msg.changeRequest.value << endl;
ptr = encodeAtrChangeRequest(ptr, msg.changeRequest);
}
if (msg.hasSourceAddress)
{
if (verbose) clog << "Encoding SourceAddress: " << msg.sourceAddress.ipv4 << endl;
ptr = encodeAtrAddress4(ptr, SourceAddress, msg.sourceAddress);
}
if (msg.hasChangedAddress)
{
if (verbose) clog << "Encoding ChangedAddress: " << msg.changedAddress.ipv4 << endl;
ptr = encodeAtrAddress4(ptr, ChangedAddress, msg.changedAddress);
}
if (msg.hasUsername)
{
if (verbose) clog << "Encoding Username: " << msg.username.value << endl;
ptr = encodeAtrString(ptr, Username, msg.username);
}
if (msg.hasPassword)
{
if (verbose) clog << "Encoding Password: " << msg.password.value << endl;
ptr = encodeAtrString(ptr, Password, msg.password);
}
if (msg.hasErrorCode)
{
if (verbose) clog << "Encoding ErrorCode: class="
<< int(msg.errorCode.errorClass)
<< " number=" << int(msg.errorCode.number)
<< " reason="
<< msg.errorCode.reason
<< endl;
ptr = encodeAtrError(ptr, msg.errorCode);
}
if (msg.hasUnknownAttributes)
{
if (verbose) clog << "Encoding UnknownAttribute: ???" << endl;
ptr = encodeAtrUnknown(ptr, msg.unknownAttributes);
}
if (msg.hasReflectedFrom)
{
if (verbose) clog << "Encoding ReflectedFrom: " << msg.reflectedFrom.ipv4 << endl;
ptr = encodeAtrAddress4(ptr, ReflectedFrom, msg.reflectedFrom);
}
if (msg.hasXorMappedAddress)
{
if (verbose) clog << "Encoding XorMappedAddress: " << msg.xorMappedAddress.ipv4 << endl;
ptr = encodeAtrAddress4 (ptr, XorMappedAddress, msg.xorMappedAddress);
}
if (msg.xorOnly)
{
if (verbose) clog << "Encoding xorOnly: " << endl;
ptr = encodeXorOnly( ptr );
}
if (msg.hasServerName)
{
if (verbose) clog << "Encoding ServerName: " << msg.serverName.value << endl;
ptr = encodeAtrString(ptr, ServerName, msg.serverName);
}
if (msg.hasSecondaryAddress)
{
if (verbose) clog << "Encoding SecondaryAddress: " << msg.secondaryAddress.ipv4 << endl;
ptr = encodeAtrAddress4 (ptr, SecondaryAddress, msg.secondaryAddress);
}
if (password.sizeValue > 0)
{
if (verbose) clog << "HMAC with password: " << password.value << endl;
StunAtrIntegrity integrity;
computeHmac(integrity.hash, buf, int(ptr-buf) , password.value, password.sizeValue);
ptr = encodeAtrIntegrity(ptr, integrity);
}
if (verbose) clog << endl;
encode16(lengthp, UInt16(ptr - buf - sizeof(StunMsgHdr)));
return int(ptr - buf);
}
int
stunRand()
{
// return 32 bits of random stuff
assert( sizeof(int) == 4 );
static bool init=false;
if ( !init )
{
init = true;
UInt64 tick;
// Twinkle: removed platform dependent code (except WIN32 code)
#if defined(WIN32)
volatile unsigned int lowtick=0,hightick=0;
__asm
{
rdtsc
mov lowtick, eax
mov hightick, edx
}
tick = hightick;
tick <<= 32;
tick |= lowtick;
#else
tick = time(NULL);
#endif
int seed = int(tick);
#ifdef WIN32
srand(seed);
#else
srandom(seed);
#endif
}
#ifdef WIN32
assert( RAND_MAX == 0x7fff );
int r1 = rand();
int r2 = rand();
int ret = (r1<<16) + r2;
return ret;
#else
return random();
#endif
}
/// return a random number to use as a port
static int
randomPort()
{
int min=0x4000;
int max=0x7FFF;
int ret = stunRand();
ret = ret|min;
ret = ret&max;
return ret;
}
#ifdef NOSSL
static void
computeHmac(char* hmac, const char* input, int length, const char* key, int sizeKey)
{
strncpy(hmac,"hmac-not-implemented",20);
}
#else
#include <openssl/hmac.h>
static void
computeHmac(char* hmac, const char* input, int length, const char* key, int sizeKey)
{
unsigned int resultSize=0;
HMAC(EVP_sha1(),
key, sizeKey,
reinterpret_cast<const unsigned char*>(input), length,
reinterpret_cast<unsigned char*>(hmac), &resultSize);
assert(resultSize == 20);
}
#endif
static void
toHex(const char* buffer, int bufferSize, char* output)
{
static char hexmap[] = "0123456789abcdef";
const char* p = buffer;
char* r = output;
for (int i=0; i < bufferSize; i++)
{
unsigned char temp = *p++;
int hi = (temp & 0xf0)>>4;
int low = (temp & 0xf);
*r++ = hexmap[hi];
*r++ = hexmap[low];
}
*r = 0;
}
void
stunCreateUserName(const StunAddress4& source, StunAtrString* username)
{
UInt64 time = stunGetSystemTimeSecs();
time -= (time % 20*60);
//UInt64 hitime = time >> 32;
UInt64 lotime = time & 0xFFFFFFFF;
char buffer[1024];
sprintf(buffer,
"%08x:%08x:%08x:",
UInt32(source.addr),
UInt32(stunRand()),
UInt32(lotime));
assert( strlen(buffer) < 1024 );
assert(strlen(buffer) + 41 < STUN_MAX_STRING);
char hmac[20];
char key[] = "Jason";
computeHmac(hmac, buffer, strlen(buffer), key, strlen(key) );
char hmacHex[41];
toHex(hmac, 20, hmacHex );
hmacHex[40] =0;
strcat(buffer,hmacHex);
int l = strlen(buffer);
assert( l+1 < STUN_MAX_STRING );
assert( l%4 == 0 );
username->sizeValue = l;
memcpy(username->value,buffer,l);
username->value[l]=0;
//if (verbose) clog << "computed username=" << username.value << endl;
}
void
stunCreatePassword(const StunAtrString& username, StunAtrString* password)
{
char hmac[20];
char key[] = "Fluffy";
//char buffer[STUN_MAX_STRING];
computeHmac(hmac, username.value, strlen(username.value), key, strlen(key));
toHex(hmac, 20, password->value);
password->sizeValue = 40;
password->value[40]=0;
//clog << "password=" << password->value << endl;
}
UInt64
stunGetSystemTimeSecs()
{
UInt64 time=0;
#if defined(WIN32)
SYSTEMTIME t;
// CJ TODO - this probably has bug on wrap around every 24 hours
GetSystemTime( &t );
time = (t.wHour*60+t.wMinute)*60+t.wSecond;
#else
struct timeval now;
gettimeofday( &now , NULL );
//assert( now );
time = now.tv_sec;
#endif
return time;
}
ostream& operator<< ( ostream& strm, const UInt128& r )
{
strm << int(r.octet[0]);
for ( int i=1; i<16; i++ )
{
strm << ':' << int(r.octet[i]);
}
return strm;
}
ostream&
operator<<( ostream& strm, const StunAddress4& addr)
{
UInt32 ip = addr.addr;
strm << ((int)(ip>>24)&0xFF) << ".";
strm << ((int)(ip>>16)&0xFF) << ".";
strm << ((int)(ip>> 8)&0xFF) << ".";
strm << ((int)(ip>> 0)&0xFF) ;
strm << ":" << addr.port;
return strm;
}
// returns true if it scucceeded
bool
stunParseHostName( char* peerName,
UInt32& ip,
UInt16& portVal,
UInt16 defaultPort )
{
in_addr sin_addr;
char host[512];
strncpy(host,peerName,512);
host[512-1]='\0';
char* port = NULL;
int portNum = defaultPort;
// pull out the port part if present.
char* sep = strchr(host,':');
if ( sep == NULL )
{
portNum = defaultPort;
}
else
{
*sep = '\0';
port = sep + 1;
// set port part
char* endPtr=NULL;
portNum = strtol(port,&endPtr,10);
if ( endPtr != NULL )
{
if ( *endPtr != '\0' )
{
portNum = defaultPort;
}
}
}
if ( portNum < 1024 ) return false;
if ( portNum >= 0xFFFF ) return false;
// figure out the host part
struct hostent* h;
#ifdef WIN32
assert( strlen(host) >= 1 );
if ( isdigit( host[0] ) )
{
// assume it is a ip address
unsigned long a = inet_addr(host);
//cerr << "a=0x" << hex << a << dec << endl;
ip = ntohl( a );
}
else
{
// assume it is a host name
h = gethostbyname( host );
if ( h == NULL )
{
int err = getErrno();
std::cerr << "error was " << err << std::endl;
assert( err != WSANOTINITIALISED );
ip = ntohl( 0x7F000001L );
return false;
}
else
{
sin_addr = *(struct in_addr*)h->h_addr;
ip = ntohl( sin_addr.s_addr );
}
}
#else
h = gethostbyname( host );
if ( h == NULL )
{
int err = getErrno();
std::cerr << "error was " << err << std::endl;
ip = ntohl( 0x7F000001L );
return false;
}
else
{
sin_addr = *(struct in_addr*)h->h_addr;
ip = ntohl( sin_addr.s_addr );
}
#endif
portVal = portNum;
return true;
}
bool
stunParseServerName( char* name, StunAddress4& addr)
{
assert(name);
// TODO - put in DNS SRV stuff.
bool ret = stunParseHostName( name, addr.addr, addr.port, 3478);
if ( ret != true )
{
addr.port=0xFFFF;
}
return ret;
}
static void
stunCreateErrorResponse(StunMessage& response, int cl, int number, const char* msg)
{
response.msgHdr.msgType = BindErrorResponseMsg;
response.hasErrorCode = true;
response.errorCode.errorClass = cl;
response.errorCode.number = number;
strcpy(response.errorCode.reason, msg);
}
// Twinkle
StunMessage *stunBuildError(const StunMessage &m, int code, const char *reason) {
StunMessage *err = new StunMessage();
MEMMAN_NEW(err);
stunCreateErrorResponse(*err, code / 100, code % 100, reason);
for ( int i=0; i<16; i++ )
{
err->msgHdr.id.octet[i] = m.msgHdr.id.octet[i];
}
return err;
}
string stunMsg2Str(const StunMessage &m) {
string s = "STUN ";
// Message type
if (m.msgHdr.msgType == BindRequestMsg) {
s += "Bind Request";
} else if (m.msgHdr.msgType == BindResponseMsg) {
s += "Bind Response";
} else if (m.msgHdr.msgType == BindErrorResponseMsg) {
s += "Bind Error Response";
} else if (m.msgHdr.msgType == SharedSecretRequestMsg) {
s += "Shared Secret Request";
} else if (m.msgHdr.msgType == SharedSecretResponseMsg) {
s += "Shared Secret Response";
} else if (m.msgHdr.msgType == SharedSecretErrorResponseMsg) {
s += "Shared Secret Error Response";
}
s += "\n";
// Message ID
s += "ID = ";
for ( int i=0; i<16; i++ ) {
char buf[3];
snprintf(buf, 3, "%X", m.msgHdr.id.octet[i]);
s += buf;
}
s += "\n";
if (m.hasMappedAddress) {
s += "Mapped Address = ";
s += h_ip2str(m.mappedAddress.ipv4.addr);
s += ':';
s += int2str(m.mappedAddress.ipv4.port);
s += "\n";
}
if (m.hasResponseAddress) {
s += "Response Address = ";
s += h_ip2str(m.responseAddress.ipv4.addr);
s += ':';
s += int2str(m.responseAddress.ipv4.port);
s += "\n";
}
if (m.hasChangeRequest) {
s += "Change Request = ";
bool change_flags = false;
if (m.changeRequest.value & ChangeIpFlag) {
s += "change IP";
change_flags = true;
}
if (m.changeRequest.value & ChangePortFlag) {
if (change_flags) s += ", ";
s += "change port";
change_flags = true;
}
if (!change_flags) s += "none";
s += "\n";
}
if (m.hasSourceAddress) {
s += "Source Address = ";
s += h_ip2str(m.sourceAddress.ipv4.addr);
s += ':';
s += int2str(m.sourceAddress.ipv4.port);
s += "\n";
}
if (m.hasChangedAddress) {
s += "Changed Address = ";
s += h_ip2str(m.changedAddress.ipv4.addr);
s += ':';
s += int2str(m.changedAddress.ipv4.port);
s += "\n";
}
if (m.hasErrorCode) {
s += "Error Code = ";
s += int2str(m.errorCode.errorClass * 100 + m.errorCode.number);
s += ' ';
s += m.errorCode.reason;
s += "\n";
}
return s;
}
bool stunEqualId(const StunMessage &m1, const StunMessage &m2) {
for ( int i=0; i<16; i++ )
{
if (m1.msgHdr.id.octet[i] != m2.msgHdr.id.octet[i]) {
return false;
}
}
return true;
}
string stunNatType2Str(NatType t) {
switch (t) {
case StunTypeUnknown:
return "Unknow";
case StunTypeOpen:
return "Open";
case StunTypeConeNat:
return "Full cone";
case StunTypeRestrictedNat:
return "Restriced cone";
case StunTypePortRestrictedNat:
return "Port restricted cone";
case StunTypeSymNat:
return "Symmetric";
case StunTypeSymFirewall:
return "Symmetric firewall;";
case StunTypeBlocked:
return "Blocked.";
case StunTypeFailure:
return "Failed";
default:
return "NULL";
}
}
// Twinkle
#if 0
static void
stunCreateSharedSecretErrorResponse(StunMessage& response, int cl, int number, const char* msg)
{
response.msgHdr.msgType = SharedSecretErrorResponseMsg;
response.hasErrorCode = true;
response.errorCode.errorClass = cl;
response.errorCode.number = number;
strcpy(response.errorCode.reason, msg);
}
#endif
static void
stunCreateSharedSecretResponse(const StunMessage& request, const StunAddress4& source, StunMessage& response)
{
response.msgHdr.msgType = SharedSecretResponseMsg;
response.msgHdr.id = request.msgHdr.id;
response.hasUsername = true;
stunCreateUserName( source, &response.username);
response.hasPassword = true;
stunCreatePassword( response.username, &response.password);
}
// This funtion takes a single message sent to a stun server, parses
// and constructs an apropriate repsonse - returns true if message is
// valid
bool
stunServerProcessMsg( char* buf,
unsigned int bufLen,
StunAddress4& from,
StunAddress4& secondary,
StunAddress4& myAddr,
StunAddress4& altAddr,
StunMessage* resp,
StunAddress4* destination,
StunAtrString* hmacPassword,
bool* changePort,
bool* changeIp,
bool verbose)
{
// set up information for default response
memset( resp, 0 , sizeof(*resp) );
*changeIp = false;
*changePort = false;
StunMessage req;
bool ok = stunParseMessage( buf,bufLen, req, verbose);
if (!ok) // Complete garbage, drop it on the floor
{
if (verbose) clog << "Request did not parse" << endl;
return false;
}
if (verbose) clog << "Request parsed ok" << endl;
StunAddress4 mapped = req.mappedAddress.ipv4;
StunAddress4 respondTo = req.responseAddress.ipv4;
UInt32 flags = req.changeRequest.value;
switch (req.msgHdr.msgType)
{
case SharedSecretRequestMsg:
if(verbose) clog << "Received SharedSecretRequestMsg on udp. send error 433." << endl;
// !cj! - should fix so you know if this came over TLS or UDP
stunCreateSharedSecretResponse(req, from, *resp);
//stunCreateSharedSecretErrorResponse(*resp, 4, 33, "this request must be over TLS");
return true;
case BindRequestMsg:
if (!req.hasMessageIntegrity)
{
if (verbose) clog << "BindRequest does not contain MessageIntegrity" << endl;
if (0) // !jf! mustAuthenticate
{
if(verbose) clog << "Received BindRequest with no MessageIntegrity. Sending 401." << endl;
stunCreateErrorResponse(*resp, 4, 1, "Missing MessageIntegrity");
return true;
}
}
else
{
if (!req.hasUsername)
{
if (verbose) clog << "No UserName. Send 432." << endl;
stunCreateErrorResponse(*resp, 4, 32, "No UserName and contains MessageIntegrity");
return true;
}
else
{
if (verbose) clog << "Validating username: " << req.username.value << endl;
// !jf! could retrieve associated password from provisioning here
if (strcmp(req.username.value, "test") == 0)
{
if (0)
{
// !jf! if the credentials are stale
stunCreateErrorResponse(*resp, 4, 30, "Stale credentials on BindRequest");
return true;
}
else
{
if (verbose) clog << "Validating MessageIntegrity" << endl;
// need access to shared secret
unsigned char hmac[20];
#ifndef NOSSL
unsigned int hmacSize=20;
HMAC(EVP_sha1(),
"1234", 4,
reinterpret_cast<const unsigned char*>(buf), bufLen-20-4,
hmac, &hmacSize);
assert(hmacSize == 20);
#endif
if (memcmp(buf, hmac, 20) != 0)
{
if (verbose) clog << "MessageIntegrity is bad. Sending " << endl;
stunCreateErrorResponse(*resp, 4, 3, "Unknown username. Try test with password 1234");
return true;
}
// need to compute this later after message is filled in
resp->hasMessageIntegrity = true;
assert(req.hasUsername);
resp->hasUsername = true;
resp->username = req.username; // copy username in
}
}
else
{
if (verbose) clog << "Invalid username: " << req.username.value << "Send 430." << endl;
}
}
}
// TODO !jf! should check for unknown attributes here and send 420 listing the
// unknown attributes.
if ( respondTo.port == 0 ) respondTo = from;
if ( mapped.port == 0 ) mapped = from;
*changeIp = ( flags & ChangeIpFlag )?true:false;
*changePort = ( flags & ChangePortFlag )?true:false;
if (verbose)
{
clog << "Request is valid:" << endl;
clog << "\t flags=" << flags << endl;
clog << "\t changeIp=" << *changeIp << endl;
clog << "\t changePort=" << *changePort << endl;
clog << "\t from = " << from << endl;
clog << "\t respond to = " << respondTo << endl;
clog << "\t mapped = " << mapped << endl;
}
// form the outgoing message
resp->msgHdr.msgType = BindResponseMsg;
for ( int i=0; i<16; i++ )
{
resp->msgHdr.id.octet[i] = req.msgHdr.id.octet[i];
}
if ( req.xorOnly == false )
{
resp->hasMappedAddress = true;
resp->mappedAddress.ipv4.port = mapped.port;
resp->mappedAddress.ipv4.addr = mapped.addr;
}
if (1) // do xorMapped address or not
{
resp->hasXorMappedAddress = true;
UInt16 id16 = req.msgHdr.id.octet[7]<<8
| req.msgHdr.id.octet[6];
UInt32 id32 = req.msgHdr.id.octet[7]<<24
| req.msgHdr.id.octet[6]<<16
| req.msgHdr.id.octet[5]<<8
| req.msgHdr.id.octet[4];;
resp->xorMappedAddress.ipv4.port = mapped.port^id16;
resp->xorMappedAddress.ipv4.addr = mapped.addr^id32;
}
resp->hasSourceAddress = true;
resp->sourceAddress.ipv4.port = (*changePort) ? altAddr.port : myAddr.port;
resp->sourceAddress.ipv4.addr = (*changeIp) ? altAddr.addr : myAddr.addr;
resp->hasChangedAddress = true;
resp->changedAddress.ipv4.port = altAddr.port;
resp->changedAddress.ipv4.addr = altAddr.addr;
if ( secondary.port != 0 )
{
resp->hasSecondaryAddress = true;
resp->secondaryAddress.ipv4.port = secondary.port;
resp->secondaryAddress.ipv4.addr = secondary.addr;
}
if ( req.hasUsername && req.username.sizeValue > 0 )
{
// copy username in
resp->hasUsername = true;
assert( req.username.sizeValue % 4 == 0 );
assert( req.username.sizeValue < STUN_MAX_STRING );
memcpy( resp->username.value, req.username.value, req.username.sizeValue );
resp->username.sizeValue = req.username.sizeValue;
}
if (1) // add ServerName
{
resp->hasServerName = true;
const char serverName[] = "Vovida.org " STUN_VERSION; // must pad to mult of 4
assert( sizeof(serverName) < STUN_MAX_STRING );
//cerr << "sizeof serverName is " << sizeof(serverName) << endl;
assert( sizeof(serverName)%4 == 0 );
memcpy( resp->serverName.value, serverName, sizeof(serverName));
resp->serverName.sizeValue = sizeof(serverName);
}
if ( req.hasMessageIntegrity & req.hasUsername )
{
// this creates the password that will be used in the HMAC when then
// messages is sent
stunCreatePassword( req.username, hmacPassword );
}
if (req.hasUsername && (req.username.sizeValue > 64 ) )
{
UInt32 source;
assert( sizeof(int) == sizeof(UInt32) );
sscanf(req.username.value, "%x", &source);
resp->hasReflectedFrom = true;
resp->reflectedFrom.ipv4.port = 0;
resp->reflectedFrom.ipv4.addr = source;
}
destination->port = respondTo.port;
destination->addr = respondTo.addr;
return true;
default:
if (verbose) clog << "Unknown or unsupported request " << endl;
return false;
}
assert(0);
return false;
}
bool
stunInitServer(StunServerInfo& info, const StunAddress4& myAddr, const StunAddress4& altAddr, int startMediaPort, bool verbose )
{
assert( myAddr.port != 0 );
assert( altAddr.port!= 0 );
assert( myAddr.addr != 0 );
//assert( altAddr.addr != 0 );
info.myAddr = myAddr;
info.altAddr = altAddr;
info.myFd = INVALID_SOCKET;
info.altPortFd = INVALID_SOCKET;
info.altIpFd = INVALID_SOCKET;
info.altIpPortFd = INVALID_SOCKET;
memset(info.relays, 0, sizeof(info.relays));
if (startMediaPort > 0)
{
info.relay = true;
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
relay->relayPort = startMediaPort+i;
relay->fd = 0;
relay->expireTime = 0;
}
}
else
{
info.relay = false;
}
if ((info.myFd = openPort(myAddr.port, myAddr.addr,verbose)) == INVALID_SOCKET)
{
clog << "Can't open " << myAddr << endl;
stunStopServer(info);
return false;
}
//if (verbose) clog << "Opened " << myAddr.addr << ":" << myAddr.port << " --> " << info.myFd << endl;
if ((info.altPortFd = openPort(altAddr.port,myAddr.addr,verbose)) == INVALID_SOCKET)
{
clog << "Can't open " << myAddr << endl;
stunStopServer(info);
return false;
}
//if (verbose) clog << "Opened " << myAddr.addr << ":" << altAddr.port << " --> " << info.altPortFd << endl;
info.altIpFd = INVALID_SOCKET;
if ( altAddr.addr != 0 )
{
if ((info.altIpFd = openPort( myAddr.port, altAddr.addr,verbose)) == INVALID_SOCKET)
{
clog << "Can't open " << altAddr << endl;
stunStopServer(info);
return false;
}
//if (verbose) clog << "Opened " << altAddr.addr << ":" << myAddr.port << " --> " << info.altIpFd << endl;;
}
info.altIpPortFd = INVALID_SOCKET;
if ( altAddr.addr != 0 )
{ if ((info.altIpPortFd = openPort(altAddr.port, altAddr.addr,verbose)) == INVALID_SOCKET)
{
clog << "Can't open " << altAddr << endl;
stunStopServer(info);
return false;
}
//if (verbose) clog << "Opened " << altAddr.addr << ":" << altAddr.port << " --> " << info.altIpPortFd << endl;;
}
return true;
}
void
stunStopServer(StunServerInfo& info)
{
if (info.myFd > 0) closesocket(info.myFd);
if (info.altPortFd > 0) closesocket(info.altPortFd);
if (info.altIpFd > 0) closesocket(info.altIpFd);
if (info.altIpPortFd > 0) closesocket(info.altIpPortFd);
if (info.relay)
{
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
if (relay->fd)
{
closesocket(relay->fd);
relay->fd = 0;
}
}
}
}
bool
stunServerProcess(StunServerInfo& info, bool verbose)
{
char msg[STUN_MAX_MESSAGE_SIZE];
int msgLen = sizeof(msg);
bool ok = false;
bool recvAltIp =false;
bool recvAltPort = false;
fd_set fdSet;
#ifdef WIN32
unsigned int maxFd=0;
#else
int maxFd=0;
#endif
FD_ZERO(&fdSet);
FD_SET(info.myFd,&fdSet);
if ( info.myFd >= maxFd ) maxFd=info.myFd+1;
FD_SET(info.altPortFd,&fdSet);
if ( info.altPortFd >= maxFd ) maxFd=info.altPortFd+1;
if ( info.altIpFd != INVALID_SOCKET )
{
FD_SET(info.altIpFd,&fdSet);
if (info.altIpFd>=maxFd) maxFd=info.altIpFd+1;
}
if ( info.altIpPortFd != INVALID_SOCKET )
{
FD_SET(info.altIpPortFd,&fdSet);
if (info.altIpPortFd>=maxFd) maxFd=info.altIpPortFd+1;
}
if (info.relay)
{
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
if (relay->fd)
{
FD_SET(relay->fd, &fdSet);
if (relay->fd >= maxFd) maxFd=relay->fd+1;
}
}
}
if ( info.altIpFd != INVALID_SOCKET )
{
FD_SET(info.altIpFd,&fdSet);
if (info.altIpFd>=maxFd) maxFd=info.altIpFd+1;
}
if ( info.altIpPortFd != INVALID_SOCKET )
{
FD_SET(info.altIpPortFd,&fdSet);
if (info.altIpPortFd>=maxFd) maxFd=info.altIpPortFd+1;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000;
int e = select( maxFd, &fdSet, NULL,NULL, &tv );
if (e < 0)
{
int err = getErrno();
clog << "Error on select: " << strerror(err) << endl;
}
else if (e >= 0)
{
StunAddress4 from;
// do the media relaying
if (info.relay)
{
time_t now = time(0);
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
if (relay->fd)
{
if (FD_ISSET(relay->fd, &fdSet))
{
char msg[MAX_RTP_MSG_SIZE];
int msgLen = sizeof(msg);
StunAddress4 rtpFrom;
ok = getMessage( relay->fd, msg, &msgLen, &rtpFrom.addr, &rtpFrom.port ,verbose);
if (ok)
{
sendMessage(info.myFd, msg, msgLen, relay->destination.addr, relay->destination.port, verbose);
relay->expireTime = now + MEDIA_RELAY_TIMEOUT;
if ( verbose ) clog << "Relay packet on "
<< relay->fd
<< " from " << rtpFrom
<< " -> " << relay->destination
<< endl;
}
}
else if (now > relay->expireTime)
{
closesocket(relay->fd);
relay->fd = 0;
}
}
}
}
if (FD_ISSET(info.myFd,&fdSet))
{
if (verbose) clog << "received on A1:P1" << endl;
recvAltIp = false;
recvAltPort = false;
ok = getMessage( info.myFd, msg, &msgLen, &from.addr, &from.port,verbose );
}
else if (FD_ISSET(info.altPortFd, &fdSet))
{
if (verbose) clog << "received on A1:P2" << endl;
recvAltIp = false;
recvAltPort = true;
ok = getMessage( info.altPortFd, msg, &msgLen, &from.addr, &from.port,verbose );
}
else if ( (info.altIpFd!=INVALID_SOCKET) && FD_ISSET(info.altIpFd,&fdSet))
{
if (verbose) clog << "received on A2:P1" << endl;
recvAltIp = true;
recvAltPort = false;
ok = getMessage( info.altIpFd, msg, &msgLen, &from.addr, &from.port ,verbose);
}
else if ( (info.altIpPortFd!=INVALID_SOCKET) && FD_ISSET(info.altIpPortFd, &fdSet))
{
if (verbose) clog << "received on A2:P2" << endl;
recvAltIp = true;
recvAltPort = true;
ok = getMessage( info.altIpPortFd, msg, &msgLen, &from.addr, &from.port,verbose );
}
else
{
return true;
}
int relayPort = 0;
if (info.relay)
{
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
if (relay->destination.addr == from.addr &&
relay->destination.port == from.port)
{
relayPort = relay->relayPort;
relay->expireTime = time(0) + MEDIA_RELAY_TIMEOUT;
break;
}
}
if (relayPort == 0)
{
for (int i=0; i<MAX_MEDIA_RELAYS; ++i)
{
StunMediaRelay* relay = &info.relays[i];
if (relay->fd == 0)
{
if ( verbose ) clog << "Open relay port " << relay->relayPort << endl;
relay->fd = openPort(relay->relayPort, info.myAddr.addr, verbose);
relay->destination.addr = from.addr;
relay->destination.port = from.port;
relay->expireTime = time(0) + MEDIA_RELAY_TIMEOUT;
relayPort = relay->relayPort;
break;
}
}
}
}
if ( !ok )
{
if ( verbose ) clog << "Get message did not return a valid message" <<endl;
return true;
}
if ( verbose ) clog << "Got a request (len=" << msgLen << ") from " << from << endl;
if ( msgLen <= 0 )
{
return true;
}
bool changePort = false;
bool changeIp = false;
StunMessage resp;
StunAddress4 dest;
StunAtrString hmacPassword;
hmacPassword.sizeValue = 0;
StunAddress4 secondary;
secondary.port = 0;
secondary.addr = 0;
if (info.relay && relayPort)
{
secondary = from;
from.addr = info.myAddr.addr;
from.port = relayPort;
}
ok = stunServerProcessMsg( msg, msgLen, from, secondary,
recvAltIp ? info.altAddr : info.myAddr,
recvAltIp ? info.myAddr : info.altAddr,
&resp,
&dest,
&hmacPassword,
&changePort,
&changeIp,
verbose );
if ( !ok )
{
if ( verbose ) clog << "Failed to parse message" << endl;
return true;
}
char buf[STUN_MAX_MESSAGE_SIZE];
int len = sizeof(buf);
len = stunEncodeMessage( resp, buf, len, hmacPassword,verbose );
if ( dest.addr == 0 ) ok=false;
if ( dest.port == 0 ) ok=false;
if ( ok )
{
assert( dest.addr != 0 );
assert( dest.port != 0 );
StunSocket sendFd;
bool sendAltIp = recvAltIp; // send on the received IP address
bool sendAltPort = recvAltPort; // send on the received port
if ( changeIp ) sendAltIp = !sendAltIp; // if need to change IP, then flip logic
if ( changePort ) sendAltPort = !sendAltPort; // if need to change port, then flip logic
if ( !sendAltPort )
{
if ( !sendAltIp )
{
sendFd = info.myFd;
}
else
{
sendFd = info.altIpFd;
}
}
else
{
if ( !sendAltIp )
{
sendFd = info.altPortFd;
}
else
{
sendFd = info.altIpPortFd;
}
}
if ( sendFd != INVALID_SOCKET )
{
sendMessage( sendFd, buf, len, dest.addr, dest.port, verbose );
}
}
}
return true;
}
int
stunFindLocalInterfaces(UInt32* addresses,int maxRet)
{
#if defined(WIN32) || defined(__sparc__)
return 0;
#else
struct ifconf ifc;
int s = socket( AF_INET, SOCK_DGRAM, 0 );
int len = 100 * sizeof(struct ifreq);
char buf[ len ];
ifc.ifc_len = len;
ifc.ifc_buf = buf;
int e = ioctl(s,SIOCGIFCONF,&ifc);
char *ptr = buf;
int tl = ifc.ifc_len;
int count=0;
while ( (tl > 0) && ( count < maxRet) )
{
struct ifreq* ifr = (struct ifreq *)ptr;
int si = sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
tl -= si;
ptr += si;
//char* name = ifr->ifr_ifrn.ifrn_name;
//cerr << "name = " << name << endl;
struct ifreq ifr2;
ifr2 = *ifr;
e = ioctl(s,SIOCGIFADDR,&ifr2);
if ( e == -1 )
{
break;
}
//cerr << "ioctl addr e = " << e << endl;
struct sockaddr a = ifr2.ifr_addr;
struct sockaddr_in* addr = (struct sockaddr_in*) &a;
UInt32 ai = ntohl( addr->sin_addr.s_addr );
if (int((ai>>24)&0xFF) != 127)
{
addresses[count++] = ai;
}
#if 0
cerr << "Detected interface "
<< int((ai>>24)&0xFF) << "."
<< int((ai>>16)&0xFF) << "."
<< int((ai>> 8)&0xFF) << "."
<< int((ai )&0xFF) << endl;
#endif
}
closesocket(s);
return count;
#endif
}
void
stunBuildReqSimple( StunMessage* msg,
const StunAtrString& username,
bool changePort, bool changeIp, unsigned int id )
{
assert( msg );
memset( msg , 0 , sizeof(*msg) );
msg->msgHdr.msgType = BindRequestMsg;
for ( int i=0; i<16; i=i+4 )
{
assert(i+3<16);
int r = stunRand();
msg->msgHdr.id.octet[i+0]= r>>0;
msg->msgHdr.id.octet[i+1]= r>>8;
msg->msgHdr.id.octet[i+2]= r>>16;
msg->msgHdr.id.octet[i+3]= r>>24;
}
if ( id != 0 )
{
msg->msgHdr.id.octet[0] = id;
}
msg->hasChangeRequest = true;
msg->changeRequest.value =(changeIp?ChangeIpFlag:0) |
(changePort?ChangePortFlag:0);
if ( username.sizeValue > 0 )
{
msg->hasUsername = true;
msg->username = username;
}
}
static void
stunSendTest( StunSocket myFd, StunAddress4& dest,
const StunAtrString& username, const StunAtrString& password,
int testNum, bool verbose )
{
assert( dest.addr != 0 );
assert( dest.port != 0 );
bool changePort=false;
bool changeIP=false;
bool discard=false;
switch (testNum)
{
case 1:
case 10:
case 11:
break;
case 2:
//changePort=true;
changeIP=true;
break;
case 3:
changePort=true;
break;
case 4:
changeIP=true;
break;
case 5:
discard=true;
break;
default:
cerr << "Test " << testNum <<" is unknown\n";
assert(0);
}
StunMessage req;
memset(&req, 0, sizeof(StunMessage));
stunBuildReqSimple( &req, username,
changePort , changeIP ,
testNum );
char buf[STUN_MAX_MESSAGE_SIZE];
int len = STUN_MAX_MESSAGE_SIZE;
len = stunEncodeMessage( req, buf, len, password,verbose );
if ( verbose )
{
clog << "About to send msg of len " << len << " to " << dest << endl;
}
sendMessage( myFd, buf, len, dest.addr, dest.port, verbose );
// add some delay so the packets don't get sent too quickly
#ifdef WIN32 // !cj! TODO - should fix this up in windows
clock_t now = clock();
assert( CLOCKS_PER_SEC == 1000 );
while ( clock() <= now+10 ) { };
#else
usleep(10*1000);
#endif
}
void
stunGetUserNameAndPassword( const StunAddress4& dest,
StunAtrString* username,
StunAtrString* password)
{
// !cj! This is totally bogus - need to make TLS connection to dest and get a
// username and password to use
stunCreateUserName(dest, username);
stunCreatePassword(*username, password);
}
void
stunTest( StunAddress4& dest, int testNum, bool verbose, StunAddress4* sAddr )
{
assert( dest.addr != 0 );
assert( dest.port != 0 );
int port = randomPort();
UInt32 interfaceIp=0;
if (sAddr)
{
interfaceIp = sAddr->addr;
if ( sAddr->port != 0 )
{
port = sAddr->port;
}
}
StunSocket myFd = openPort(port,interfaceIp,verbose);
StunAtrString username;
StunAtrString password;
username.sizeValue = 0;
password.sizeValue = 0;
#ifdef USE_TLS
stunGetUserNameAndPassword( dest, username, password );
#endif
stunSendTest( myFd, dest, username, password, testNum, verbose );
char msg[STUN_MAX_MESSAGE_SIZE];
int msgLen = STUN_MAX_MESSAGE_SIZE;
StunAddress4 from;
getMessage( myFd,
msg,
&msgLen,
&from.addr,
&from.port,verbose );
StunMessage resp;
memset(&resp, 0, sizeof(StunMessage));
if ( verbose ) clog << "Got a response" << endl;
bool ok = stunParseMessage( msg,msgLen, resp,verbose );
if ( verbose )
{
clog << "\t ok=" << ok << endl;
clog << "\t id=" << resp.msgHdr.id << endl;
clog << "\t mappedAddr=" << resp.mappedAddress.ipv4 << endl;
clog << "\t changedAddr=" << resp.changedAddress.ipv4 << endl;
clog << endl;
}
if (sAddr)
{
sAddr->port = resp.mappedAddress.ipv4.port;
sAddr->addr = resp.mappedAddress.ipv4.addr;
}
}
NatType
stunNatType( StunAddress4& dest,
bool verbose,
bool* preservePort, // if set, is return for if NAT preservers ports or not
bool* hairpin, // if set, is the return for if NAT will hairpin packets
int port, // port to use for the test, 0 to choose random port
StunAddress4* sAddr // NIC to use
)
{
assert( dest.addr != 0 );
assert( dest.port != 0 );
if ( hairpin )
{
*hairpin = false;
}
if ( port == 0 )
{
port = randomPort();
}
UInt32 interfaceIp=0;
if (sAddr)
{
interfaceIp = sAddr->addr;
}
StunSocket myFd1 = openPort(port,interfaceIp,verbose);
StunSocket myFd2 = openPort(port+1,interfaceIp,verbose);
if ( ( myFd1 == INVALID_SOCKET) || ( myFd2 == INVALID_SOCKET) )
{
cerr << "Some problem opening port/interface to send on" << endl;
return StunTypeFailure;
}
assert( myFd1 != INVALID_SOCKET );
assert( myFd2 != INVALID_SOCKET );
bool respTestI=false;
bool isNat=true;
StunAddress4 testIchangedAddr;
StunAddress4 testImappedAddr;
bool respTestI2=false;
bool mappedIpSame = true;
StunAddress4 testI2mappedAddr;
StunAddress4 testI2dest=dest;
bool respTestII=false;
bool respTestIII=false;
bool respTestHairpin=false;
memset(&testImappedAddr,0,sizeof(testImappedAddr));
StunAtrString username;
StunAtrString password;
username.sizeValue = 0;
password.sizeValue = 0;
#ifdef USE_TLS
stunGetUserNameAndPassword( dest, username, password );
#endif
//stunSendTest( myFd1, dest, username, password, 1, verbose );
int count=0;
while ( count < 7 )
{
struct timeval tv;
fd_set fdSet;
#ifdef WIN32
unsigned int fdSetSize;
#else
int fdSetSize;
#endif
FD_ZERO(&fdSet); fdSetSize=0;
FD_SET(myFd1,&fdSet); fdSetSize = (myFd1+1>fdSetSize) ? myFd1+1 : fdSetSize;
FD_SET(myFd2,&fdSet); fdSetSize = (myFd2+1>fdSetSize) ? myFd2+1 : fdSetSize;
tv.tv_sec=0;
tv.tv_usec=150*1000; // 150 ms
if ( count == 0 ) tv.tv_usec=0;
int err = select(fdSetSize, &fdSet, NULL, NULL, &tv);
int e = getErrno();
if ( err == SOCKET_ERROR )
{
// error occured
cerr << "Error " << e << " " << strerror(e) << " in select" << endl;
closesocket(myFd1);
closesocket(myFd2);
return StunTypeFailure;
}
else if ( err == 0 )
{
// timeout occured
count++;
if ( !respTestI )
{
stunSendTest( myFd1, dest, username, password, 1 ,verbose );
}
if ( (!respTestI2) && respTestI )
{
// check the address to send to if valid
if ( ( testI2dest.addr != 0 ) &&
( testI2dest.port != 0 ) )
{
stunSendTest( myFd1, testI2dest, username, password, 10 ,verbose);
}
}
if ( !respTestII )
{
stunSendTest( myFd2, dest, username, password, 2 ,verbose );
}
if ( !respTestIII )
{
stunSendTest( myFd2, dest, username, password, 3 ,verbose );
}
if ( respTestI && (!respTestHairpin) )
{
if ( ( testImappedAddr.addr != 0 ) &&
( testImappedAddr.port != 0 ) )
{
stunSendTest( myFd1, testImappedAddr, username, password, 11 ,verbose );
}
}
}
else
{
//if (verbose) clog << "-----------------------------------------" << endl;
assert( err>0 );
// data is avialbe on some fd
for ( int i=0; i<2; i++)
{
StunSocket myFd;
if ( i==0 )
{
myFd=myFd1;
}
else
{
myFd=myFd2;
}
if ( myFd!=INVALID_SOCKET )
{
if ( FD_ISSET(myFd,&fdSet) )
{
char msg[STUN_MAX_MESSAGE_SIZE];
int msgLen = sizeof(msg);
StunAddress4 from;
getMessage( myFd,
msg,
&msgLen,
&from.addr,
&from.port,verbose );
StunMessage resp;
memset(&resp, 0, sizeof(StunMessage));
stunParseMessage( msg,msgLen, resp,verbose );
if ( verbose )
{
clog << "Received message of type " << resp.msgHdr.msgType
<< " id=" << (int)(resp.msgHdr.id.octet[0]) << endl;
}
switch( resp.msgHdr.id.octet[0] )
{
case 1:
{
if ( !respTestI )
{
testIchangedAddr.addr = resp.changedAddress.ipv4.addr;
testIchangedAddr.port = resp.changedAddress.ipv4.port;
testImappedAddr.addr = resp.mappedAddress.ipv4.addr;
testImappedAddr.port = resp.mappedAddress.ipv4.port;
if ( preservePort )
{
*preservePort = ( testImappedAddr.port == port );
}
testI2dest.addr = resp.changedAddress.ipv4.addr;
if (sAddr)
{
sAddr->port = testImappedAddr.port;
sAddr->addr = testImappedAddr.addr;
}
count = 0;
}
respTestI=true;
}
break;
case 2:
{
respTestII=true;
}
break;
case 3:
{
respTestIII=true;
}
break;
case 10:
{
if ( !respTestI2 )
{
testI2mappedAddr.addr = resp.mappedAddress.ipv4.addr;
testI2mappedAddr.port = resp.mappedAddress.ipv4.port;
mappedIpSame = false;
if ( (testI2mappedAddr.addr == testImappedAddr.addr ) &&
(testI2mappedAddr.port == testImappedAddr.port ))
{
mappedIpSame = true;
}
}
respTestI2=true;
}
break;
case 11:
{
if ( hairpin )
{
*hairpin = true;
}
respTestHairpin = true;
}
break;
}
}
}
}
}
}
// see if we can bind to this address
//cerr << "try binding to " << testImappedAddr << endl;
StunSocket s = openPort( 0/*use ephemeral*/, testImappedAddr.addr, false );
if ( s != INVALID_SOCKET )
{
closesocket(s);
isNat = false;
//cerr << "binding worked" << endl;
}
else
{
isNat = true;
//cerr << "binding failed" << endl;
}
closesocket(myFd1);
closesocket(myFd2);
if (verbose)
{
clog << "test I = " << respTestI << endl;
clog << "test II = " << respTestII << endl;
clog << "test III = " << respTestIII << endl;
clog << "test I(2) = " << respTestI2 << endl;
clog << "is nat = " << isNat <<endl;
clog << "mapped IP same = " << mappedIpSame << endl;
}
// implement logic flow chart from draft RFC
if ( respTestI )
{
if ( isNat )
{
if (respTestII)
{
return StunTypeConeNat;
}
else
{
if ( mappedIpSame )
{
if ( respTestIII )
{
return StunTypeRestrictedNat;
}
else
{
return StunTypePortRestrictedNat;
}
}
else
{
return StunTypeSymNat;
}
}
}
else
{
if (respTestII)
{
return StunTypeOpen;
}
else
{
return StunTypeSymFirewall;
}
}
}
else
{
return StunTypeBlocked;
}
return StunTypeUnknown;
}
int
stunOpenStunSocket( StunAddress4& dest, StunAddress4* mapAddr,
int port, StunAddress4* srcAddr,
bool verbose )
{
assert( dest.addr != 0 );
assert( dest.port != 0 );
assert( mapAddr );
if ( port == 0 )
{
port = randomPort();
}
unsigned int interfaceIp = 0;
if ( srcAddr )
{
interfaceIp = srcAddr->addr;
}
StunSocket myFd = openPort(port,interfaceIp,verbose);
if (myFd == INVALID_SOCKET)
{
return myFd;
}
char msg[STUN_MAX_MESSAGE_SIZE];
int msgLen = sizeof(msg);
StunAtrString username;
StunAtrString password;
username.sizeValue = 0;
password.sizeValue = 0;
#ifdef USE_TLS
stunGetUserNameAndPassword( dest, username, password );
#endif
stunSendTest(myFd, dest, username, password, 1, 0/*false*/ );
StunAddress4 from;
getMessage( myFd, msg, &msgLen, &from.addr, &from.port,verbose );
StunMessage resp;
memset(&resp, 0, sizeof(StunMessage));
bool ok = stunParseMessage( msg, msgLen, resp,verbose );
if (!ok)
{
return -1;
}
StunAddress4 mappedAddr = resp.mappedAddress.ipv4;
StunAddress4 changedAddr = resp.changedAddress.ipv4;
//clog << "--- stunOpenStunSocket --- " << endl;
//clog << "\treq id=" << req.id << endl;
//clog << "\tresp id=" << id << endl;
//clog << "\tmappedAddr=" << mappedAddr << endl;
*mapAddr = mappedAddr;
return myFd;
}
bool
stunOpenStunSocketPair( StunAddress4& dest, StunAddress4* mapAddr,
int* fd1, int* fd2,
int port, StunAddress4* srcAddr,
bool verbose )
{
assert( dest.addr!= 0 );
assert( dest.port != 0 );
assert( mapAddr );
const int NUM=3;
if ( port == 0 )
{
port = randomPort();
}
*fd1=-1;
*fd2=-1;
char msg[STUN_MAX_MESSAGE_SIZE];
int msgLen =sizeof(msg);
StunAddress4 from;
int fd[NUM];
int i;
unsigned int interfaceIp = 0;
if ( srcAddr )
{
interfaceIp = srcAddr->addr;
}
for( i=0; i<NUM; i++)
{
fd[i] = openPort( (port == 0) ? 0 : (port + i),
interfaceIp, verbose);
if (fd[i] < 0)
{
while (i > 0)
{
closesocket(fd[--i]);
}
return false;
}
}
StunAtrString username;
StunAtrString password;
username.sizeValue = 0;
password.sizeValue = 0;
#ifdef USE_TLS
stunGetUserNameAndPassword( dest, username, password );
#endif
for( i=0; i<NUM; i++)
{
stunSendTest(fd[i], dest, username, password, 1/*testNum*/, verbose );
}
StunAddress4 mappedAddr[NUM];
for( i=0; i<NUM; i++)
{
msgLen = sizeof(msg)/sizeof(*msg);
getMessage( fd[i],
msg,
&msgLen,
&from.addr,
&from.port ,verbose);
StunMessage resp;
memset(&resp, 0, sizeof(StunMessage));
bool ok = stunParseMessage( msg, msgLen, resp, verbose );
if (!ok)
{
return false;
}
mappedAddr[i] = resp.mappedAddress.ipv4;
StunAddress4 changedAddr = resp.changedAddress.ipv4;
}
if (verbose)
{
clog << "--- stunOpenStunSocketPair --- " << endl;
for( i=0; i<NUM; i++)
{
clog << "\t mappedAddr=" << mappedAddr[i] << endl;
}
}
if ( mappedAddr[0].port %2 == 0 )
{
if ( mappedAddr[0].port+1 == mappedAddr[1].port )
{
*mapAddr = mappedAddr[0];
*fd1 = fd[0];
*fd2 = fd[1];
closesocket( fd[2] );
return true;
}
}
else
{
if (( mappedAddr[1].port %2 == 0 )
&& ( mappedAddr[1].port+1 == mappedAddr[2].port ))
{
*mapAddr = mappedAddr[1];
*fd1 = fd[1];
*fd2 = fd[2];
closesocket( fd[0] );
return true;
}
}
// something failed, close all and return error
for( i=0; i<NUM; i++)
{
closesocket( fd[i] );
}
return false;
}
/* ====================================================================
* The Vovida Software License, Version 1.0
*
* Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The names "VOCAL", "Vovida Open Communication Application Library",
* and "Vovida Open Communication Application Library (VOCAL)" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact vocal@vovida.org.
*
* 4. Products derived from this software may not be called "VOCAL", nor
* may "VOCAL" appear in their name, without prior written
* permission of Vovida Networks, Inc.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
* NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
* IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by Vovida
* Networks, Inc. and many individuals on behalf of Vovida Networks,
* Inc. For more information on Vovida Networks, Inc., please see
* <http://www.vovida.org/>.
*
*/
// Local Variables:
// mode:c++
// c-file-style:"ellemtel"
// c-file-offsets:((case-label . +))
// indent-tabs-mode:nil
// End:
|