File: reaim.c

package info (click to toggle)
reaim 0.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 328 kB
  • ctags: 175
  • sloc: ansic: 1,796; sh: 225; makefile: 48
file content (3073 lines) | stat: -rw-r--r-- 90,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
/*
 * Project: ReAim
 *
 * Release: 0.8 pre
 *
 * Homepage:  http://reaim.sourceforge.net/
 *
 * Copyright: Mark Cooke, November 2001.  All Rights Reserved.
 * 
 * License: GNU General Public License, Version 2.
 *
 * CVS Data: $Id: reaim.c,v 1.30 2003/03/25 12:35:31 mark-c Exp $
 *
 * Description:
 *
 * ReAIM is a simple transparent AIM proxy.  It sits between a local area
 * network, and the internet, usually running on the firewall / NAT server.
 *
 * It also has a filter module for MSN Messenger file transfer support.
 *
 * It accepts transparently redirected port 5190 (AIM server communications)
 * traffic, and transparently forwards this to the usual destination with one
 * caveat.
 *
 * The direct-communications setup packet is mangled to use the (routable)
 * internet-visible IP address, rather than the client's NAT / private network
 * address.
 *
 * Given the change in more recent clients to allow the user to set a source
 * port, instead of just using 4443/5566, the proxy also forces the port to
 * be 4443 as part of the DCC address mangling.
 *
 * This allows the second part of this proxy - a port 4443/5566
 * forwarding service - to catch the remote party DCC connection, and forward
 * it to the internal host.  It is slightly cludgy, as the AIM clients do not
 * send a 'hello, it's me' packet when they direct-connect, so the proxy can't
 * tell where to forward the connection to until the remote party sends a
 * message.  Fortunately, just starting to type sends a message we can use
 * to make the onward part of the proxied connection.
 *
 * To make this less cludgy, it is possible to open a range of incoming ports,
 * and for the proxy to use the next unused port in this range for each DCC.
 * This is a 'TODO' high on my list of things to do to ReAIM.
 *
 * As a compile option, setting LAX_SECURITY will allow a DCC to match at the
 * initial connection point if there is only 1 pending DCC request.
 * 
 * As a compile option, setting LOOSE_CLIENT_IP will disable the paranoid
 * checking that the IP address being overwritten in the DCC setup packet is
 * the same as the source IP address of the client socket.  This allows
 * multi-homed hosts to work, and also network topologies that include
 * multiple-NAT devices.
 *
 * Usage:
 *
 * reaim [optional dotted quad for routable IP]
 *
 * Notes:
 *
 * This proxy requires knowledge of the external IP address, and also for the
 * original destination to be available.  Using iptables allows us to use
 * SO_ORIGINAL_DST to retrieve this.  Hence, this proxy is a 2.4.x / iptables
 * only proxy.  More specifically, it does -not- work with 2.4.x / ipchains
 * support.  Suitable modifications to the gettranssockname function may
 * permit this to work with ipchains on 2.2.x, where the getsockname() call
 * is overridden to be the original destination (not confirmed).
 *
 * Example usage where eth0 is LAN, ppp0 is internet:
 *
 * 1. Redirect eth0 traffic to eth0 port 5190, and make sure it's accepted.
 * iptables -t nat    -A PREROUTING -i eth0 -p tcp --dport 5190 -j REDIRECT --to-ports 5190
 * iptables -t filter -A INPUT      -i eth0 -p tcp --dport 5190 -j ACCEPT
 *
 * 2. Allow ppp0 to accept traffic for port 4443 (AIM DCC)
 * iptables -t filter -A INPUT      -i ppp0 -p tcp --dport 4443 -j ACCEPT
 *
 * 3. Allow ppp0 to accept traffic for port 5566 (AIM Sharing)
 * iptables -t filter -A INPUT      -i ppp0 -p tcp --dport 5566 -j ACCEPT
 *
 * 4. Redirect eth0 traffic to eth0 port 5190, and make sure it's accepted.
 * iptables -t nat    -A PREROUTING -i eth0 -p tcp --dport 1863 -j REDIRECT --to-ports 1863
 * iptables -t filter -A INPUT      -i eth0 -p tcp --dport 1863 -j ACCEPT
 *
 * 5. Allow ppp0 to accept traffic for port 1864 (MSN DCC)
 * iptables -t filter -A INPUT      -i ppp0 -p tcp --dport 1864 -j ACCEPT
 *
 * 6. Allow dynamic dcc traffic on ports 40000 - 40099
 * iptables -t filter -A INPUT      -i ppp0 -p tcp --dport 40000:40099 -j ACCEPT
 *
 * Detailed AIM Manglement Notes:
 *
 * The proxy keeps three lists of connections (socket pairs).
 *
 * 1. LAN clients connections to port 5190 (and the onward connection).
 * 2. Established connections on port 4443 (and the onward connection).
 * 3. Established connections on port 5566 (and the onward connection).
 *
 * When a client starts a connection, the proxy accepts the redirected
 * connection on it's port 5190.  It immediately attempts to open the
 * onward connection to the original destination, and any packets arriving
 * in either direction are inspected, optionally modified, and then passed
 * to the other half of the socket-pair.
 *
 * When a packet indicates a DC setup request, an entry is made in the
 * pending DC setup list, and appropriate parameters from the request are
 * recorded.  Note that the DC setup request is -mangled- to use the
 * externally addressable IP address, and a dynamic port, so the remote
 * party isn't trying to connect to a 10.x.x.x etc IP address.
 *
 * When a connection is received on the dynamic port (or 4443 for buggy
 * clients), the proxy accepts the connection, and searches for a match
 * in the pending DC list.  If a match is found, the onward portion of
 * the DC is established, and unfiltered direct communications between
 * the parties is established.
 *
 * Connections to port 5566 are handled virtually identically.
 *
 * DC Setup Packet Format:
 *
 * With many thanks to the people working on the reverse engineering of
 * the AIM OSCAR protocol.
 *
 *
 * EXTRA...EXTRA...
 *
 * Because of the nice flexibility of the filter scheme, I added support for
 * MSN proxying too.  The MSN Messenger uses a pseudo HTTP style scheme, with
 * headers indicating the message type, parameters, etc.
 *
 * For MSN DCC setup, we look for the text/xmsmsgsinvite message, and 'fix'
 * both the port and IP address, again with a dynamic dcc port from our range.
 *
 * TODO:
 *
 * A whole lot.  This is currently little more than a complete hack that
 * happens to work for me.  I'll be working on the code to do more error
 * checking, structure clean up, as time permits.
 *
 * I have plans to add a 'filter chain' type system to both the 5190 and 4443
 * port proxies, and eventually maybe generalise the code to be a more
 * generic, open source licensed, proxy-based firewalling system...maybe!
 *
 * Final words:
 *
 * No warranty.  Patches, bug reports, success stories, and other contributions
 * most welcome.
 */

/* NOTE:  NetBSD patch provided by David Vyskocil.  Thanks!!!
 *
 * Compile with USE_IPF defined for NetBSD/IPF support.
 */

 /* NetBSD proxy redirect(ipnat) and firewall(ipf) setup
 *
 * assuming 'ne0' is the LAN device name and 'tun0' the internet connection
 *
 * /etc/ipnat.conf rules... (redirect for transparent proxying)
 *
 * rdr ne0 0/0 port 1863 -> 127.0.0.1 port 1863 tcp
 * rdr ne0 0/0 port 5190 -> 127.0.0.1 port 5190 tcp
 *
 * /etc/ipf.conf rules... (firewall/logger)
 *
 * pass in quick log on ne0 proto tcp from any to any port = 1863
 * pass in quick log on ne0 proto tcp from any to any port = 5190
 * pass in quick log on tun0 proto tcp from any to any port = 1864
 * pass in quick log on tun0 proto tcp from any to any port = 4443
 * pass in quick log on tun0 proto tcp from any to any port = 5566
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <sys/time.h>
#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <time.h>
#include <ctype.h>

#include <getopt.h>
#include <sys/ioctl.h>

#include <unistd.h>

// Command line options
#define _GNU_SOURCE
#include <getopt.h>

static struct option long_options[] = {

  { "disable-msn", 1, 0, 'm' },
  { "disable-aim", 1, 0, 'a' },
  { "external-ip", 1, 0, 'e' },
  { "help",        1, 0, 'h' },
  { "logfile",     1, 0, 'l' },
  { 0,0,0,0 }
};

#ifdef USE_IPF

#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip_compat.h>
#include <netinet/ip_fil.h>
#include <netinet/ip_nat.h>
#define PLATFORM "/IPF"

#else

// Nasty kernel include in a userspace program
// but we need the original destination information....
#include <linux/netfilter_ipv4.h>
#define PLATFORM "/NetFilter"

#endif

#define REAIM_VERSION "0.8 pre"

#define OSCAR 5190
#define DCC   4443
#define SHARE 5566

#define MSN     1863   // Where we listen for client messages
#define MSNRX   1864   // Where we listen for MSN DCCs

#define INC_MIN     40000   // Minimum port for incoming connections
#define INC_MAX     40099   // Maximum port for incoming connections
#define INC_TIMEOUT 60      // 60 second timeout for dynamic DCCs

#define LAX_SECURITY     1
#define LOOSE_CLIENT_IP  1
#define LOOSE_PORT_SCAN  0
#define DEBUG_PKT_DUMPS  1
#define USE_DYNAMIC_DCC  1
#define PERIODIC_LOG     1

int enable_msn  = 1;
int enable_aim  = 1;
int enable_fork = 1;

// Trillian headers for MSN support don't end with two \n\r pairs,
// but only one, so are not distinguishable from the message easily.
// Define this to skip the strict end-of-headers check.
#define TRILLIAN_HEADER_SUPPORT 1

#define TS_CLIENT 0
#define TS_SERVER 1

// This defines the size of the buffers used when proxying
// connections.  Each proxied connection uses 2 of these
// buffers.
#define PROXY_BUFFER_SIZE   4096

#ifndef LOG_FILE
#define LOG_FILE "/var/log/reaim.log"
#endif

char *log_file = LOG_FILE;

// Filter group IDs - needed for stateful storage access
#define GRP_AIM    1
#define GRP_ICQ    2
#define GRP_MSN    3

// Set to 1 to enable additional logging for debugging purposes
#define VERBOSE_LOGGING 0

// Timeout for making onward connections
#define CONNECT_TIMEOUT 10

// Maximum number of filters on each open socket.
// TODO: remove this coded limit
#define MAX_FILTERS 4

// Logging routine.  Borrowed from the squirm proxy.
int write_log(const char *logfile, const char *format, ...);


typedef struct _filter_chain           filter_chain;
typedef struct _filter_state           filter_state;
typedef struct _proxy_track_struct     proxy_track_struct;
typedef struct _user_track_struct      user_track_struct;
typedef struct _pending_dc_struct      pending_dc_struct;
typedef struct _listen_track_struct    listen_track_struct;
typedef struct _pending_connect_struct pending_connect_struct;

/*----------------------------------------------------------------------------
  The filter scheme for the code is a simple procedure that takes a proxy
  data buffer, current and maximum length, and the connection tracking
  structure.

  Each filter is called sequentially, and can mangle the provided buffer.
----------------------------------------------------------------------------*/
typedef unsigned int (filter_proc)(unsigned char *buff,
				   unsigned int buff_len,
				   unsigned int max_len,
				   proxy_track_struct *pt);

typedef void (listen_proc)(listen_track_struct *lt);

typedef void (connect_proc)(pending_connect_struct *pc);


struct _filter_chain {
    filter_proc    *proc[MAX_FILTERS]; // Procedures for filtering
};

struct _filter_state {
    filter_state   *next;         // Next state storage
    unsigned int    filter_group; // Filter ID number
    void           *group_state;  // Persistent state
};

/*
 * Generic proxied socket handler.
 *
 */
struct _proxy_track_struct {
    proxy_track_struct *next;  // Next proxy connection in linked list.
    
    unsigned int magic;        // Magic number linking other lists.
    unsigned int cleanup;      // Set to 1 to garbage collect this entry.
    unsigned int user;         // Set to 1 if user connection.
    
    int sockets[2];                  // The rx / tx sockets
    unsigned char *data_buff[2];     // Receive/Transmit pending data buffers
    unsigned int   data_offset[2];   // Offset to start of data in buffers
    unsigned int   data_len[2];      // Remaining bytes in pending buffers
    unsigned int   data_max[2];      // Buffer sizes
    filter_chain  *filter[2];        // Attached filters
    filter_state   state;            // Stateful storage buffers
};

struct  _user_track_struct {
    user_track_struct *next; // Next entry in the list

    unsigned int magic;      // Magic connection tracking number
    unsigned int cleanup;    // Set to 1 to clean up this entry

    unsigned char *msg;      // Connection setup message
    unsigned char *user;     // Screen name of the user
};

struct _pending_dc_struct {

    pending_dc_struct *next; // Next pending connection
    
    unsigned int cleanup;    // Set to 1 to clean up this entry

    unsigned short port;     // Port we're listenning on.
    unsigned short orig_port;// Original port the client was on.

    unsigned int magic;      // The user_track_struct magic number
    unsigned char id[8];     // The DC setup message we're waiting on

    struct sockaddr_in src;  // Original Client Source address
};

struct _listen_track_struct {
    
    listen_track_struct *next; // Next listenning socket
    listen_proc         *proc; // Procedure to call
    
    unsigned int      cleanup; // 1 if inactive
    int                  port; // Port this is listenning on.
    int                socket; // Listening socket handle.
    int               oneshot; // 1 if one-shot listener.
    long              timeout; // One-shot timeout (0 is infinite)
};

struct _pending_connect_struct {
  pending_connect_struct *next;  // Next pending connection
  unsigned int magic;            // user_track_struct magic number
  int          socket;           // Socket handle
  int          client_sock;      // Client socket
  connect_proc *proc;            // Bottom half handler
  void         *proc_data;       // Data for the bottom half handler
  filter_chain *filter[2];       // Establish connection filters
  long          timeout;         // Timeout time
  struct sockaddr_in src;        // Source address
  struct sockaddr_in dst;        // Destination address
};

#define AIM_CMDSTART(buff)      buff[0]
#define AIM_CHANNEL(buff)       buff[1]
#define AIM_SEQID(buff)         ((unsigned short) buff[2]  + (buff[3] << 8))
#define AIM_FNAC_LEN(buff)      ((unsigned short) buff[5]  + (buff[4] << 8))
#define AIM_FNAC_FAMILY(buff)   ((unsigned short) buff[7]  + (buff[6] << 8))
#define AIM_FNAC_SUBTYPE(buff)  ((unsigned short) buff[9]  + (buff[8] << 8))
#define AIM_FNAC_FLAGS(buff)    ((unsigned short) buff[11] + (buff[10] << 8))
#define AIM_SNAC_ID(buff)       ((unsigned int)   buff[15] + (buff[14] << 8) + (buff[13] << 16) + (buff[12] << 24))
#define AIM_SNAC_COOKIE(buff)   (&buff[16])   // The 8 byte SNAC cookie start ptr
#define AIM_SNAC_CHAN(buff)     ((unsigned short) buff[25] + 256*buff[24])
#define AIM_BUD_NAME_LEN(buff)  buff[26]
#define AIM_BUD_NAME(buff)      (&buff[27])
#define AIM_BUD_NEXT(buff)      (27+AIM_BUD_NAME_LEN(buff))
#define AIM_SNAC_TLV_TYPE(buff) ((unsigned short) buff[AIM_BUD_NEXT(buff)+1] + (buff[AIM_BUD_NEXT(buff)+0] << 8))
#define AIM_SNAC_TLV_NEXT(buff) ((unsigned short) buff[AIM_BUD_NEXT(buff)+3] + (buff[AIM_BUD_NEXT(buff)+2] << 8))
#define AIM_SNAC_DATA_OFF(buff) (AIM_BUD_NEXT(buff)+4)
#define DCC_SNAC_COOKIE(buff)   (&buff[12])
#define DCC_BUD_NAME(buff)      (&buff[44])  // Appears to be null terminated this time...

/*===========================================================================*/
// The linked lists used to track state
/*===========================================================================*/

proxy_track_struct pt_list;       // Generic proxy socket list-head.
user_track_struct  user_list;     // List head.  User tracking information
listen_track_struct lt_list;      // List head.  Listenning sockets
pending_connect_struct pc_list;   // List head.  Pending connect()'s
pending_dc_struct  dc_list;       // List head.  Pending incoming connections

unsigned int       ts_magic        = 1; // Next free magic number

char *             outside_ip      = NULL;

filter_chain       oscar_tx;   // LAN client to AIM filter
filter_chain       oscar_rx;   // AIM to LAN client filter
filter_chain       share_tx;   // LAN to Remote filter
filter_chain       share_rx;   // Remote to LAN filter
filter_chain       msn_tx;     // LAN to Remote filter
filter_chain       msn_rx;     // Remote to LAN filter
filter_chain       msnrx_tx;   // LAN to Remote filter
filter_chain       msnrx_rx;   // Remote to LAN filter

static int pending_hup = 0; // Not zero if a HUP was received.

listen_track_struct* create_listener_between(int port_min, int port_max, listen_proc lp);

/*===========================================================================*/


#ifdef USE_IPF

/* 
 * We use IP Filter on NetBSD to track original destination 
 *
 * Descriptions: use ioctl for /dev/ipnat to extract original destination
 *               from kernel.
 *    Arguments: (socket file descriptor, 
 *                current destination address, 
 *                the length)
 * Side Effects: replace sa to original destination
 * Return Value: 0 (success) or -1 (failure)
 */
int
get_realip_from_ipf(fd, sa, salen)
     int fd;
     struct sockaddr *sa;
     int *salen;
{

    struct	sockaddr_in	sin, sloc;
    struct	sockaddr_in	*sinp;
    natlookup_t	natlook;
    natlookup_t	*natlookp = &natlook;
    int	namelen, natfd;
    /*  
    * get IP# and port # of the remote end of the connection (at the
    * origin).
    */
    namelen = sizeof(sin);
    if (getpeername(fd, (struct sockaddr *)&sin, &namelen) == -1) {
	perror("getpeername");
	write_log(log_file, "getpeername failed: %s", strerror(errno));
	exit(-1);
	}
    /*
    * get IP# and port # of the local end of the connection (at the
    * man-in-the-middle).
    */
    namelen = sizeof(sin);
    if (getsockname(fd, (struct sockaddr *)&sloc, &namelen) == -1) {
	perror("getsockname");
	write_log(log_file, "getsockname failed: %s", strerror(errno));
	exit(-1);
	}
    /*
    * Build up the NAT natlookup structure.
    */
    bzero((char *)&natlook, sizeof(natlook));
    natlook.nl_outip = sin.sin_addr;
    natlook.nl_inip = sloc.sin_addr;
    natlook.nl_flags = IPN_TCP;
    natlook.nl_outport = sin.sin_port;
    natlook.nl_inport = sloc.sin_port;
    /*
    * Open the NAT device and lookup the mapping pair.
    */
    natfd = open(IPL_NAT, O_RDONLY);
    if (ioctl(natfd, SIOCGNATL, &natlookp) == -1) {
	perror("ioctl(SIOCGNATL)");
	write_log(log_file, "ioctl(SIOCGNATL) failed: %s", strerror(errno));
	close(natfd);
	exit(-1);
	}
    close(natfd);
/*
    fprintf(stderr, "from addr:%s port:%d\n", inet_ntoa(natlook.nl_inip), ntohs(natlook.nl_inport));
    fprintf(stderr, "as   addr:%s port:%d\n", inet_ntoa(natlook.nl_outip), ntohs(natlook.nl_outport));
    fprintf(stderr, "real addr:%s port:%d\n", inet_ntoa(natlook.nl_realip), ntohs(natlook.nl_realport));
*/
    sinp = (struct sockaddr_in *) sa;
    sinp->sin_addr = natlookp->nl_realip;
    sinp->sin_port = natlookp->nl_realport;
    sinp->sin_family = AF_INET;
    return(0);
}

#endif

/*===========================================================================*
 *  int gettranssockname(int fd, struct sockaddr *sa, socklen_t *salen)
 *
 *    Query the network stack for the original destination of the socket
 *    passed.  This is used for building the onward portion of the filter
 *    proxy.
 *===========================================================================*/

int gettranssockname(int fd, struct sockaddr *sa, socklen_t *salen)
{
    if (*salen != sizeof(struct sockaddr_in)) {
	errno = EINVAL;
	return -1;
    }
    
    // Try to ask for the original destination...
#ifdef USE_IPF
    if (!get_realip_from_ipf(fd, sa, salen))
	return 0;
#else
    if (!getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, sa, salen))
	return 0;
#endif
    
    // We have to explode if we can't read it back.
    return -1;
}

/*===========================================================================*
 *  void dump_packet(const char *buff, int len)
 *
 *    Print out the contents of the packet.
 *===========================================================================*/
void dump_packet(const char *buff, int len)
{
    char dump_start[32768], *dump_buff = dump_start;
    int i, j;

    for (i = 0; i < len; i ++) {
	
	if ((i % 16) == 0) {
	    sprintf(dump_buff, "%.4X    ", i);
	    dump_buff += 8;
	}
	
	sprintf(dump_buff, "%02x ", (unsigned int) buff[i] & 0xff);
	dump_buff += 3;
	
	if ((i % 16) == 15) {
	    
	    sprintf(dump_buff, "    ");
	    dump_buff += 4;
	    
	    for (j = -15; j < 1; j++) {
		if (isalnum(buff[i+j]))
		    *dump_buff = buff[i+j];
		else
		    *dump_buff = '.';

		dump_buff ++;
	    }
	    
	    *dump_buff++ = '\n';
	}
    }
    *dump_buff++ = '\n';
    *dump_buff++ = '\0';

    write_log(log_file, "Packet Dump:\n%s", dump_start);
}


/*===========================================================================*
 *  void dump_packet(const char *buff, int len)
 *
 *    Print out the contents of the packet.
 *===========================================================================*/

int write_log(const char *logfile, const char *format, ...)
{
    va_list args;
    FILE *log_handle;
    char *date_str;
    char msg_buff[32768];
    time_t current_time;
    
    va_start(args, format);
    
    if (vsprintf(msg_buff, format, args) > 32767)
	strcpy(msg_buff, "[WARNING] Insufficient log buffer size");
    
    va_end(args);
    
    current_time = time(NULL);
    date_str = ctime(&current_time);
    
    log_handle = fopen(logfile, "at");
    if (log_handle == NULL) {
      if (enable_fork == 0)
	fprintf(stderr, "[%.*s] %s\n", (int) strlen(date_str) - 1, date_str, msg_buff);
      return 0;
    }
    
    // strlen - 1 to kill the trailing \n in the data string.
    fprintf(log_handle, "[%.*s] %s\n", (int) strlen(date_str) - 1, date_str, msg_buff);
    fclose(log_handle);

    return 1;
}

#if PERIODIC_LOG
/*===========================================================================*
 *  void periodic_log()
 *
 *  Print out some stats to the reaim log periodically.
 *===========================================================================*/

void periodic_log() {

  int n_pc, n_pt, n_lt, n_pdc;
  listen_track_struct *lt;
  pending_dc_struct *pdc;
  proxy_track_struct *pt;
  pending_connect_struct *pc;
  
  // Count listen_track_struct
  n_lt = 0;
  lt = lt_list.next;
  while(lt != NULL) {
    n_lt ++;
    lt = lt->next;
  }

  // Count pending_dc_struct
  n_pdc = 0;
  pdc = dc_list.next;
  while(pdc != NULL) {
    n_pdc ++;
    pdc = pdc->next;
  }

  // Count proxy_track_struct
  n_pt = 0;
  pt = pt_list.next;
  while(pt != NULL) {
    n_pt ++;
    pt = pt->next;
  }

  // Count pending_connect_struct
  n_pc = 0;
  pc = pc_list.next;
  while(pc != NULL) {
    n_pc ++;
    pc = pc->next;
  }

  write_log(log_file, "[STATS] Proxied Connections: %d, Pending DCCs: %d, Pending connect()s: %d, Listening Sockets: %d",
	    n_pt, n_pdc, n_pc, n_lt);
}

#endif

/*===========================================================================*
 *  int net_listen(int port, char *ifs)
 *
 *    Open a network socket listenning on the port requested.  This function
 *    returns the socket handle, or -1 if the socket could not be openned for
 *    some reason.
 *
 *    The function binds to the address specified in the ifs parameter.
 *===========================================================================*/

int net_listen(int port, const char *ifs)
{
    int hSock;
    struct sockaddr_in server;
    int option;
    
    hSock = socket(PF_INET, SOCK_STREAM, 0);
    if (hSock == -1) {
        write_log(log_file, "[FATAL] Could create a socket for %s:%d",
		  ifs, port);
	return -1;
    }
    
    option = 1;
    if (setsockopt(hSock, SOL_SOCKET, SO_REUSEADDR, (void*) &option,
		   sizeof(option)) < 0) {
        write_log(log_file, "[FATAL] Could not set socket options for %s:%d",
		  ifs, port);
	return -1;
    }
    
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ifs);
    server.sin_port = htons(port);
    if (bind(hSock, (struct sockaddr*) &server, sizeof(server)) == -1) {
        write_log(log_file, "[FATAL] Could not create listening socket on %s:%d",
		  ifs, port);
	close(hSock);
	return -1;
    }
    
    if (listen(hSock, 5) == -1) {
        write_log(log_file, "[FATAL] Could not listen on %s:%d",
		  ifs, port);
	close(hSock);
	return -1;
    }
    
    /* Set the socket to non blocking, and close it if we can't */
    if (fcntl (hSock, F_SETFL, O_NONBLOCK) == -1) {
        write_log(log_file, "[FATAL] Could not make socket non-blocking for %s:%d",
		  ifs, port);
	close(hSock);
	return -1;
    }
    
    return hSock;
}

/*===========================================================================*
 *  int net_listen_between(int port_min, int port_max)
 *
 *    Open a network socket listenning on a port in the range requested.
 *    This function returns the socket handle, or -1 if the socket could not
 *    be openned for some reason.
 *
 *    The function binds to the address specified in the ifs parameter, and
 *    returns the port bound.
 *===========================================================================*/
int net_listen_between(int port_min, int port_max, const char *ifs, int *port)
{
    int hSock;
    struct sockaddr_in server;
    int option, i;
    
    hSock = socket(PF_INET, SOCK_STREAM, 0);
    if (hSock == -1) {
        write_log(log_file, "[FATAL] Could create a socket for %s:%d",
		  ifs, port);
	return -1;
    }
    
    option = 1;
    if (setsockopt(hSock, SOL_SOCKET, SO_REUSEADDR, (void*) &option,
		   sizeof(option)) < 0) {
        write_log(log_file, "[FATAL] Could not set socket options for %s:%d",
		  ifs, port);
	return -1;
    }
    
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ifs);
    
    /* Look for an unused port in our permitted range. */
    for (i = port_min ; i <= port_max; i++) {
      server.sin_port = htons(i);
      if (bind(hSock, (struct sockaddr*) &server, sizeof(server)) != -1)
	break;
    }
    
    // Didn't find one.
    if (i > port_max) {
      write_log(log_file, "[FATAL] Could not create listening socket on %s:%d-%d",
		ifs, port_min, port_max);
      close(hSock);
      return -1;
    }
    
    if (listen(hSock, 5) == -1) {
        write_log(log_file, "[FATAL] Could not listen on %s:%d",
		  ifs, port);
	close(hSock);
	return -1;
    }
    
    /* Set the socket to non blocking, and close it if we can't */
    if (fcntl (hSock, F_SETFL, O_NONBLOCK) == -1) {
        write_log(log_file, "[FATAL] Could not make socket non-blocking for %s:%d",
		  ifs, port);
	close(hSock);
	return -1;
    }
    
    // Inform caller of the port we bound to.
    *port = i;
    
    return hSock;
}


/*===========================================================================*
 *  int net_connect_to(int port, const char *ifs)
 *
 *    Open a network socket to the port and dotted-quad address string given.
 *    The returned socket is set to non-blocking and keep-alive if the socket
 *    was created.  -1 is returned on failure.
 *
 * DEPRECIATED: This function can block.  Should be using either
 *              net_connect_outbound_nb or net_connect_inbound_nb
 *===========================================================================*/

int net_connect_to(int port, const char *ifs)
{
    int hSock;
    struct sockaddr_in server;
    int option;
    
    hSock = socket(PF_INET, SOCK_STREAM, 0);
    if (hSock == -1) {
	write_log(log_file, "Connection [%s:%d] failed [create]: %s [%d]",
		  ifs, port, strerror(errno), errno);
	return -1;
    }
    
    option = 1;
    if (setsockopt(hSock, SOL_SOCKET, SO_REUSEADDR, (void*) &option,
		   sizeof(option)) < 0) {
	write_log(log_file, "Connection [%s:%d] failed [options]: %s [%d]",
		  ifs, port, strerror(errno), errno);
	close(hSock);
	return -1;
    }
    
    option = 1;
    if (setsockopt(hSock, SOL_SOCKET, SO_KEEPALIVE, (void*) &option,
		   sizeof(option)) < 0) {
	write_log(log_file, "Connection [%s:%d] failed [options]: %s [%d]",
		  ifs, port, strerror(errno), errno);
	close(hSock);
	return -1;
    }
    
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ifs);
    server.sin_port = htons(port);
    if (connect(hSock, (struct sockaddr*) &server, sizeof(server)) == -1) {
	write_log(log_file, "Connection [%s:%d] failed [conn]: %s [%d]",
		  ifs, port, strerror(errno), errno);
	close(hSock);
	return -1;
    }
    
    /* Set the socket to non blocking, and close it if we can't */
    if (fcntl (hSock, F_SETFL, O_NONBLOCK) == -1) {
	write_log(log_file, "Connection [%s:%d] closed [options]: %s [%d]",
		  ifs, port, strerror(errno), errno);
	close(hSock);
	return -1;
    }
    
    return hSock;
}

/*-------------------------------------------------------------------------
  void* net_open_connection(int hSock)
  
  Open the next connection to the socket specified.  The socket supplied
  must be in a listenning state.  This function returns a newly created
  socket structure with information about the new connection, or -1 if
  the socket could not be openned for some reason.
  
  The returned socket is setup for non-blocking, keep-alive.
-------------------------------------------------------------------------*/
int net_open_connection(int hSock, struct sockaddr_in *addr, int *salen)
{
    int hNewSock;
    int option;
    
    hNewSock = accept(hSock, (struct sockaddr *) addr, salen);
    if (hNewSock == -1) {
	write_log(log_file, "Accept failed [accept]: %s [%d]",
		  strerror(errno), errno);
	return -1;
    }
    
    /* Set the socket to non blocking, and close it if we can't */
    if (fcntl (hNewSock, F_SETFL, O_NONBLOCK) == -1) {
	write_log(log_file, "Accept failed [options] [From %s:%d]: %s [%d]",
		  inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
		  strerror(errno), errno);
	close(hNewSock);
	return -1;
    }
    
    option = 1;
    if (setsockopt(hNewSock, SOL_SOCKET, SO_KEEPALIVE, (void*) &option,
		   sizeof(option)) < 0) {
	write_log(log_file, "Accept failed [options] [From %s:%d]: %s [%d]",
		  inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
		  strerror(errno), errno);
	close(hNewSock);
	return -1;
    }
    
    return hNewSock;
}

/*-------------------------------------------------------------------------
  filter_state* find_state(proxy_track_struct *pts,
                           unsigned int  id)
  
  Search the list of filter state structures for the PTS, and return a
  pointer to the appropriate one.

  Return NULL if it does not exist.
-------------------------------------------------------------------------*/

filter_state *find_pts_state(proxy_track_struct *pts, unsigned int group_id)
{
    filter_state *scan;
    
    scan = pts->state.next;
    
    while((scan) && (scan->filter_group != group_id))
	scan = scan->next;
    
    return scan;
}

/*-------------------------------------------------------------------------
  int add_pts_state(proxy_track_struct *pts,
                    unsigned int  id,
	            filter_state *add_state)
  
  Search the list of filter state structures for the PTS, and if a filter
  state structure doesn't already exist, add the provided one.

  Return 0 if the new data was not added.  1 if it was.
-------------------------------------------------------------------------*/

int add_pts_state(proxy_track_struct *pts, unsigned int group_id, filter_state *add_state)
{
    if (find_pts_state(pts, group_id))
	return 0;

    add_state->next = pts->state.next;
    pts->state.next = add_state;

    return 1;
}

/*-------------------------------------------------------------------------
  filter_state* del_pts_state(proxy_track_struct *pts,
                              unsigned int  id)
  
  Search the list of filter state structures for the PTS, and if a filter
  state structure exists, remove it from the list and return it.

  Return NULL if there was no pts data.
-------------------------------------------------------------------------*/

filter_state * del_pts_state(proxy_track_struct *pts, unsigned int group_id)
{
    filter_state *scan, *prev;
    
    prev = &pts->state;
    scan = pts->state.next;
    
    while(scan) {
	
	if (scan->filter_group == group_id) {
	    prev->next = scan->next;
	    return scan;
	}
	
	prev = scan;
	scan = scan->next;
    }
    
    return NULL;
}

/*-------------------------------------------------------------------------
  proxy_track_struct* pts_allocate()
  
  Allocate the buffers, setup counters, sizes, empty filters for a new
  proxy tracking structure.

  Return NULL if we can't allocate memory for some reason.
-------------------------------------------------------------------------*/

proxy_track_struct *pts_allocate()
{
    proxy_track_struct *pt;
    int sock;
    
    // Setup the tracking structure
    pt = calloc(sizeof(proxy_track_struct),1);
    if (!pt) {
	write_log(log_file, "[WARNING] Memory allocation failed for "
		  "proxy tracking structure.");
	return NULL;
    }
    
    pt->next       = NULL;
    pt->cleanup    = 0;
    pt->user       = 0;
    pt->state.next = NULL;
    
    // Setup the new user socket list.
    for (sock = 0; sock < 2; sock ++) {
	pt->sockets[sock]      = -1;
	pt->data_max[sock]     = PROXY_BUFFER_SIZE;
	pt->data_offset[sock]  = 0;
	pt->data_len[sock]     = 0;
	pt->data_buff[sock]    = malloc(pt->data_max[sock]);

	if (!pt->data_buff[sock]) {

	    int fs;
	    
	    for (fs = 0; fs < sock; fs ++)
		free(pt->data_buff[fs]);
	    free(pt);
	    
	    write_log(log_file, "[WARNING] Memory allocation failed for "
		      "proxy tracking data buffers.");
	    
	    return NULL;
	}

	pt->filter[sock] = NULL;
    }
    
    return pt;
}

/*-------------------------------------------------------------------------
  int net_connect_outbound_nb()
  
  Setup an outbound non-blocking connection attempt.

  Return 0 on a failure.  Note that is it upto the caller to free
  proc_data if this function fails.
-------------------------------------------------------------------------*/

int net_connect_outbound_nb(listen_track_struct *lt,
		            connect_proc         proc,
		            void                *proc_data,
		            filter_chain        *tx_filter,
		            filter_chain        *rx_filter,
		            int                  force_dst_port)
{
    int new_client, new_server;
    int salen, option;
    pending_connect_struct *pc;
    char *tmpstr;
    struct timeval tv_now;
    
    // Allocate pending connection tracker
    pc = calloc(sizeof(pending_connect_struct), 1);
    if (!pc) {
      return 0;
    }

    gettimeofday(&tv_now, NULL);

    // Add to the pc list.  This will do garbage collection
    pc->socket      = -1;
    pc->client_sock = -1;
    pc->proc        = proc;
    pc->proc_data   = proc_data;
    pc->filter[0]   = tx_filter;
    pc->filter[1]   = rx_filter;
    pc->timeout     = tv_now.tv_sec + CONNECT_TIMEOUT;
    pc->next        = pc_list.next;
    pc_list.next    = pc;
    
    // Open the pending connection (and log any errors)
    salen = sizeof(pc->src);
    new_client = net_open_connection(lt->socket, &pc->src, &salen);
    if (new_client == -1) {
	return 0;
    }
    
    // Read the original destination...
    salen = sizeof(pc->dst);
    if (gettranssockname(new_client, (struct sockaddr*) &pc->dst, &salen) == -1) {
	write_log(log_file, "[CONN_NB] Connection [%s:%d -> ip.ip.ip.ip:port via proxy] failed "
		  "[Could not determine original destination]: %s [%d]",
		  inet_ntoa(pc->src.sin_addr), ntohs(pc->src.sin_port),
		  strerror(errno), errno);
	close(new_client);
	return 0;
    }

    if (force_dst_port != -1) {
      if (force_dst_port != ntohs(pc->dst.sin_port)) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
        write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy]  "
		  "[Forcing target port to %d]",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  force_dst_port);
	free(tmpstr);
	
	pc->dst.sin_port = htons(force_dst_port);
      }
    }
    
    // Create the onward socket
    new_server = socket(PF_INET, SOCK_STREAM, 0);
    if (new_server == -1) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
        write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] failed "
		  "[Could not create onward socket]: %s [%d]",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  strerror(errno), errno);
	free(tmpstr);
	close(new_client);
	return 0;
    }

    // Record the socket handles in the track struct
    pc->socket      = new_server;
    pc->client_sock = new_client;
    
    option = 1;
    setsockopt(new_server, SOL_SOCKET, SO_REUSEADDR, (void*) &option, sizeof(option));
    
    option = 1;
    setsockopt(new_server, SOL_SOCKET, SO_KEEPALIVE, (void*) &option, sizeof(option));
    
    fcntl(new_server, F_SETFL, O_NONBLOCK);
    
    if (connect(new_server, (struct sockaddr*) &pc->dst, sizeof(pc->dst)) == -1) {
      
      // If it's pending, we're done, and the bottom half will be called from the
      // main select() loop
      if (errno == EINPROGRESS) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
	write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] created [pending connection] (%d %d)",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  pc->client_sock, pc->socket);
	free(tmpstr);
	return 1;
      }
      
      // On other errors, kill the connection.
      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Connect returned error]: %s [%d]",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno);
      free(tmpstr);
      
      // Cleanup of the pending connect struct is done in the main loop
      close(new_client);
      close(new_server);
      pc->socket = -1;
      pc->client_sock = -1;
      return 0;
    }
    
    // The connection succeeded immediately.  Aren't we lucky.
    tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
    write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] created [connection made]",
	      tmpstr,                      ntohs(pc->src.sin_port),
	      inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port));
    free(tmpstr);
    
    // As we connected immediately, call the processing routine directly.
    pc->proc(pc);
    
    return 1;
}

/*-------------------------------------------------------------------------
  int net_connect_inbound_nb()
  
  Setup an inbound non-blocking connection attempt.
  
  Return 0 on a failure.  Note that is it upto the caller to free
  proc_data if this function fails.
-------------------------------------------------------------------------*/

int net_connect_inbound_nb(listen_track_struct *lt,
		           connect_proc         proc,
		           pending_dc_struct   *pdc,
		           filter_chain        *tx_filter,
		           filter_chain        *rx_filter,
			   int                  force_dst_port)
{
    int new_client, new_server;
    int salen, option;
    pending_connect_struct *pc;
    char *tmpstr;
    struct timeval tv_now;

    // We have to clean up the pdc in here.
    pdc->cleanup = 1;
    
    // Allocate pending connection tracker
    pc = calloc(sizeof(pending_connect_struct), 1);
    if (!pc) {
      return 0;
    }

    gettimeofday(&tv_now, NULL);

    // Add to the pc list.  This will do garbage collection
    pc->socket      = -1;
    pc->client_sock = -1;
    pc->proc        = proc;
    pc->proc_data   = NULL;
    pc->filter[0]   = tx_filter;
    pc->filter[1]   = rx_filter;
    pc->timeout     = tv_now.tv_sec + CONNECT_TIMEOUT;
    pc->next        = pc_list.next;
    pc_list.next    = pc;
    
    // Open the pending connection (and log any errors)
    salen = sizeof(pc->src);
    new_client = net_open_connection(lt->socket, &pc->src, &salen);
    if (new_client == -1) {
	return 0;
    }

    // The pdc contains the destination address / port

    memcpy(&pc->dst, &pdc->src, sizeof(pdc->src));
    pc->dst.sin_port = htons(pdc->orig_port);
    
    if (force_dst_port != -1) {
      if (force_dst_port != ntohs(pc->dst.sin_port)) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
        write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy]  "
		  "[Forcing target port to %d]",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  force_dst_port);
	free(tmpstr);
	
	pc->dst.sin_port = htons(force_dst_port);
      }
    }
    
    // Create the onward socket
    new_server = socket(PF_INET, SOCK_STREAM, 0);
    if (new_server == -1) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
        write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] failed "
		  "[Could not create onward socket]: %s [%d]",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  strerror(errno), errno);
	free(tmpstr);
	close(new_client);
	return 0;
    }

    // Record the socket handles in the track struct
    pc->socket      = new_server;
    pc->client_sock = new_client;
    
    option = 1;
    setsockopt(new_server, SOL_SOCKET, SO_REUSEADDR, (void*) &option, sizeof(option));
    
    option = 1;
    setsockopt(new_server, SOL_SOCKET, SO_KEEPALIVE, (void*) &option, sizeof(option));
    
    fcntl(new_server, F_SETFL, O_NONBLOCK);
    
    if (connect(new_server, (struct sockaddr*) &pc->dst, sizeof(pc->dst)) == -1) {
      
      // If it's pending, we're done, and the bottom half will be called from the
      // main select() loop
      if (errno == EINPROGRESS) {

	tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
	write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] created [pending connection] (%d %d)",
		  tmpstr,                      ntohs(pc->src.sin_port),
		  inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		  pc->client_sock, pc->socket);
	free(tmpstr);
	return 1;
      }
      
      // On other errors, kill the connection.
      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Connect returned error]: %s [%d]",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno);
      free(tmpstr);
      
      // Cleanup of the pending connect struct is done in the main loop
      close(new_client);
      close(new_server);
      pc->socket = -1;
      pc->client_sock = -1;
      return 0;
    }
    
    // The connection succeeded immediately.  Aren't we lucky.
    tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
    write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] created [connection made]",
	      tmpstr,                      ntohs(pc->src.sin_port),
	      inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port));
    free(tmpstr);
    
    // As we connected immediately, call the processing routine directly.
    pc->proc(pc);
    
    return 1;
}

/*-------------------------------------------------------------------------
  void* conn_open_outbound_generic(pending_connect_struct *pc)
  
  The onward connection has completed.  This function cleans up if it
  failed, or allocates the tracking and filter structures as needed if
  the connection is established.

  Either way, we have to set the pending connection structure to the
  cleanup state, so we can garbage collect it.
-------------------------------------------------------------------------*/
void conn_open_outbound_generic(pending_connect_struct *pc)
{
  int se, sel;
  user_track_struct  *ts;
  proxy_track_struct *pt;
  char *tmpstr;

  // Check if the connection succeeded or failed as we did an
  // asynchronous connect.
  if (getsockopt(pc->socket, SOL_SOCKET, SO_ERROR, &se, &sel) == -1) {

      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Unable to get connect status]: %s [%d] (%d:%d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno,
		pc->client_sock, pc->socket);
      free(tmpstr);

      close(pc->client_sock);
      close(pc->socket);
      pc->socket = -1;
      pc->client_sock = -1;
      return;
  }
  
  // se is non zero if the connection failed.
  if (se != 0) {

      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Connect returned error]: %s [%d] (%d %d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno,
		pc->client_sock, pc->socket);
      free(tmpstr);
      close(pc->client_sock);
      close(pc->socket);
      pc->socket      = -1;
      pc->client_sock = -1;
      return;
  }
  
  // Setup the tracking structure
  ts = calloc(sizeof(user_track_struct), 1);
  ts->magic = ts_magic ++;
  ts->next = user_list.next;
  user_list.next = ts;
  
  pt = pts_allocate();
  if (!pt) {
    
      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Failed to allocate tracking struct] (%d %d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		pc->client_sock, pc->socket);
      free(tmpstr);
    
      ts->cleanup = 1;
      close(pc->socket);
      close(pc->client_sock);
      pc->socket = -1;
      pc->client_sock = -1;
      return;
  }
  
  pt->sockets[0] = pc->client_sock;
  pt->sockets[1] = pc->socket;
  pt->filter[0] = pc->filter[0];
  pt->filter[1] = pc->filter[1];
  pt->user  = 1;
  pt->magic = ts->magic;
  
  pt->next = pt_list.next;
  pt_list.next = pt;
  
  tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
  write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] established "
	    "[magic = %d] (%d %d)",
	    tmpstr,                      ntohs(pc->src.sin_port),
	    inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
	    pt->magic, pc->client_sock, pc->socket);
  free(tmpstr);
  
  // Flag the pending connection for garbage collect without
  // closing the connections (which we just handed off to the
  // pt tracking struct)
  pc->socket = -1;
  pc->client_sock = -1;
}


/*-------------------------------------------------------------------------
  void* conn_open_inbound_generic(pending_connect_struct *pc)
  
  The onward connection has completed.  This function cleans up if it
  failed, or allocates the tracking and filter structures as needed if
  the connection is established.

  Either way, we have to set the pending connection structure to the
  cleanup state, so we can garbage collect it.
-------------------------------------------------------------------------*/
void conn_open_inbound_generic(pending_connect_struct *pc)
{
  int se, sel;
  proxy_track_struct *pt;
  char *tmpstr;

  // Check if the connection succeeded or failed as we did an
  // asynchronous connect.
  if (getsockopt(pc->socket, SOL_SOCKET, SO_ERROR, &se, &sel) == -1) {

      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Unable to get connect status]: %s [%d] (%d:%d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno,
		pc->client_sock, pc->socket);
      free(tmpstr);

      close(pc->client_sock);
      close(pc->socket);
      pc->socket = -1;
      pc->client_sock = -1;
      return;
  }
  
  // se is non zero if the connection failed.
  if (se != 0) {

      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Connect returned error]: %s [%d] (%d %d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		strerror(errno), errno,
		pc->client_sock, pc->socket);
      free(tmpstr);
      close(pc->client_sock);
      close(pc->socket);
      pc->socket      = -1;
      pc->client_sock = -1;
      return;
  }
  
  pt = pts_allocate();
  if (!pt) {
    
      tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
      write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] failed "
		"[Failed to allocate tracking struct] (%d %d)",
		tmpstr,                      ntohs(pc->src.sin_port),
		inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
		pc->client_sock, pc->socket);
      free(tmpstr);
    
      close(pc->socket);
      close(pc->client_sock);
      pc->socket = -1;
      pc->client_sock = -1;
      return;
  }
  
  pt->sockets[0] = pc->client_sock;
  pt->sockets[1] = pc->socket;
  pt->filter[0] = pc->filter[0];
  pt->filter[1] = pc->filter[1];
  
  pt->next = pt_list.next;
  pt_list.next = pt;
  
  tmpstr = strdup(inet_ntoa(pc->src.sin_addr));
  write_log(log_file, "[CONN_BH] Connection [%s:%d -> %s:%d via proxy] established "
	    "(%d %d)",
	    tmpstr,                      ntohs(pc->src.sin_port),
	    inet_ntoa(pc->dst.sin_addr), ntohs(pc->dst.sin_port),
	    pc->client_sock, pc->socket);
  free(tmpstr);
  
  // Flag the pending connection for garbage collect without
  // closing the connections (which we just handed off to the
  // pt tracking struct)
  pc->socket = -1;
  pc->client_sock = -1;
}


/*-------------------------------------------------------------------------
  void net_open_aim_oscar(listen_track_struct *lt)

  Setup a generic open and callback pair which will attach the oscar filter
  if the connection is successfully established.

  This should only be called for connections heading out of the LAN to
  the off-site servers.
-------------------------------------------------------------------------*/

void net_open_aim_oscar(listen_track_struct *lt)
{

  net_connect_outbound_nb(lt, conn_open_outbound_generic, NULL, &oscar_tx, &oscar_rx, lt->port);

}


/*-------------------------------------------------------------------------
  void net_open_log_attempt(listen_track_struct *lt)
  
  This function is used for ports we are listenning to but never expecting
  to be connected to.  We log the attempt for diagnostic purposes.
-------------------------------------------------------------------------*/
void net_open_log_attempt(listen_track_struct *lt)
{
  struct sockaddr_in src_addr;
  int salen;
  int new_remote;
  
  // Open the pending connection....
  salen = sizeof(src_addr);
  new_remote = net_open_connection(lt->socket, &src_addr, &salen);
  if (new_remote == -1)
    return;
  close(new_remote);
  
  write_log(log_file, "[WARNING] Unexpected connection or attempted port scan [%s:%d -> proxy:%d]",
	    inet_ntoa(src_addr.sin_addr),
	    ntohs(src_addr.sin_port),
	    lt->port);
}


/*-------------------------------------------------------------------------
  void net_open_aim(listen_track_struct *lt)
  
  Open the direct connection attempt from the remote party, and alloc
  the generic proxy_track struct.
  
  This routine should be able to open a connection back to the inside as
  there should be only 1 pending connection for each listening port.

  A warning is logged if this isn't the case.

  This should only ever be called for incoming requests from offsite hosts.
-------------------------------------------------------------------------*/
void net_open_aim(listen_track_struct *lt)
{
    pending_dc_struct *pms, *first;
    int count;

    // Look for a pending connection for this port
    pms = dc_list.next;
    first = NULL;
    count = 0;
    
    while(pms) {
      
      if (pms->cleanup == 0) {
	
	if (pms->port == lt->port) {
	  count ++;
	  first = pms;
	}
	
      }
      
      pms = pms->next;
    }
    
    switch(count) {
    case 1:
      // Okay.  All is well...

      net_connect_inbound_nb(lt, conn_open_inbound_generic, first, &share_tx, &share_rx, -1);

      return;
      
    case 0:
      // We are probably being scanned...but we shouldn't be listening if
      // there is no pending connection...  Just flag a warning.
      net_open_log_attempt(lt);
      break;
      
    default:
      // Too many pending connections in the list.

      // Shouldn't ever get here.  Should never have more than 1 pending connection
      // per port.
      write_log(log_file, "[WARNING] Couldn't find exactly 1 connection in pending list. [proxy:%d]",
		lt->port);
      break;
    }
}


/*-------------------------------------------------------------------------
  void net_open_buggy_aim_dcc(listen_track_struct *lt)
  
  AIM appears to ignore the port specified in a direct connection setup.
  This breaks the dynamic dcc mapping, so we have this function to trap
  the buggy connection attempts to port 4443, and to do a fuzzy search
  of the pending connections list for a match.

  This routine can only open a connection back to the inside if
  there is only 1 pending connection for an inside machine on port 4443.

  A warning is logged if this isn't the case.

  This should only ever be called for incoming requests from offsite hosts.
-------------------------------------------------------------------------*/
void net_open_buggy_aim_dcc(listen_track_struct *lt)
{
    pending_dc_struct *pms, *first, *loose;
    listen_track_struct *ls;
    int count, loose_count;

    // Look for a pending connection for this port
    pms = dc_list.next;
    first = NULL;
    count = 0;
    loose = NULL;
    loose_count = 0;
    
    while(pms) {
      
      if (pms->cleanup == 0) {
	
	if (pms->orig_port == lt->port) {
	  count ++;
	  first = pms;
	}

	// Buggy client hack
	loose_count++;
	loose = pms;
	
      }
      
      pms = pms->next;
    }
    
    switch(count) {
    case 1:
      // Okay.  All is well...apart from the buggy AIM client
      
      write_log(log_file, "[WARNING] Unexpected connection to port %d.  Single match for buggy DCC found.",
		lt->port);
      
      // Note we have to manually clean up the dynamic dcc listener as
      // we picked up the DCC on port 4443 rather than the one we were
      // supposed to be contacted on.

      // Search for the listen tracker on the port in the pending_dc struct.
      ls = lt_list.next;
      while(ls) {
	if (ls->port == first->port)
	  ls->cleanup = 1;

	ls = ls->next;
      }

      net_connect_inbound_nb(lt, conn_open_inbound_generic, first, &share_tx, &share_rx, -1);

      return;
      
    case 0:
      // Didn't find any matches with strict port checking.  Try loose!
      if (loose_count == 1) {
	
	// Note we have to manually clean up the dynamic dcc listener as
	// we picked up the DCC on 4443 rather than the one we were
	// supposed to be contacted on.
	
	// Search for the listen tracker on the port in the pending_dc struct.
	ls = lt_list.next;
	while(ls) {
	  if (ls->port == loose->port)
	    ls->cleanup = 1;
	  
	  ls = ls->next;
	}
	
	write_log(log_file, "[WARNING] Unexpected connection to port %d.  Loose single match for buggy DCC. (Expected %d)",
		  lt->port, loose->port);
	
	net_connect_inbound_nb(lt, conn_open_inbound_generic, loose, &share_tx, &share_rx, DCC);
	
	return;
	
      }
    default:
      // Log the attempt.  We can get here if there is more than one DCC pending
      // or we're being port scanned.
      net_open_log_attempt(lt);
      break;
    }
}


/*-------------------------------------------------------------------------
  void net_open_msn(listen_track_struct *lt)
  
  Open the connection attempt from the LAN client, create the onward
  socket for the proxying, and allocate a tracking structure.
  
  If any steps fail, the client connection is terminated.
-------------------------------------------------------------------------*/

void net_open_msn(listen_track_struct *lt)
{

  net_connect_outbound_nb(lt, conn_open_outbound_generic, NULL, &msn_tx, &msn_rx, lt->port);

} 

/*-------------------------------------------------------------------------
  void net_open_msnrx(listen_track_struct *lt)
  
  Open the direct connection attempt from the remote party, and allocate
  the connection tracking structure.
  
  We know exactly which host to connect to as we are using dynamic dcc
  ports - one per connection.  Anything else causes an error to be
  flagged.
-------------------------------------------------------------------------*/
void net_open_msnrx(listen_track_struct *lt)
{
    pending_dc_struct *pms, *first;
    int count;

    // Look for a pending connection for this port
    pms = dc_list.next;
    first = NULL;
    count = 0;
    
    while(pms) {
      
      if (pms->cleanup == 0) {
	
	if (pms->port == lt->port) {
	  count ++;
	  first = pms;
	}
	
      }
      
      pms = pms->next;
    }
    
    switch(count) {
    case 1:
      // Okay.  All is well...

      net_connect_inbound_nb(lt, conn_open_inbound_generic, first, &msnrx_tx, &msnrx_rx, -1);

      return;
      
    case 0:
    default:
      // No/too many pending connections in the list.

      // Shouldn't ever get here.  Should never have more than 1 pending connection
      // per port.
      write_log(log_file, "[WARNING] Couldn't find exactly 1 connection in pending list. [proxy:%d]",
		lt->port);
      break;
    }
}

/*-------------------------------------------------------------------------
  unsigned int fp_process(unsigned char *buff,
                          unsigned int len,
			  unsigned int max,
			  filter_chain *fc,
			  proxy_track_struct *pt)

  Pass the packet through the filter chain.  Nice and simple really.

  A NULL filter chain is valid, and mean 'pass unchanged'
  
-------------------------------------------------------------------------*/

unsigned int fp_process(unsigned char *buff,
                        unsigned int len, unsigned int max,
			filter_chain *fc,
			proxy_track_struct *pt)
{
    int i;
    
    // Pass the data unchanged if there's no filter chain.
    if (!fc)
        return len;
    
    // Should be a little less lazy about using a fixed upper size limit
    // but there's no point making it complex for now.
    for (i = 0; (i < MAX_FILTERS) && fc->proc[i]; i++)
	len = fc->proc[i](buff, len, max, pt);
    
    return len;
}

/*-------------------------------------------------------------------------
  int fp_locate(unsigned char *data_buff, unsigned int data_len,
		unsigned char *find_buff, unsigned int find_len,
		unsigned char *mask_buff)
			  
  Filter helper:  Binary search for find_buff in data_buff, and return the
                  offset.  The buffers are masked with the mask_buff.
  
  Returns -1 if the sub-string can't be found.
-------------------------------------------------------------------------*/

int fp_locate(unsigned char *data_buff, unsigned int data_len,
	      unsigned char *find_buff, unsigned int find_len,
	      unsigned char *mask_buff)
{
  unsigned int s, i;

  if (find_len > data_len)
    return -1;
  
  for (s = 0; s < data_len - find_len; s++) {
    
    for (i = 0; i < find_len; i++) {
      
      if ((data_buff[s+i] & mask_buff[i]) != (find_buff[i] & mask_buff[i]))
	break;
    }
    
    if (i == find_len)
      return s;
  }
  
  return -1;
}

/*-------------------------------------------------------------------------
  unsigned int fp_oscar_tx(unsigned char *buff,
                           unsigned int len,
			   unsigned int max,
			   proxy_track_struct *pt)
			  
  Filter procedure: Search for and modify OSCAR IP/Port specifications in
                    passing packets.
  
  If the packet does not appear to be a file transfer setup, return it
  unchanged.
  
  If it appears to have an IP/port setting, setup a listen socket for
  the anticipated return packet.

-------------------------------------------------------------------------*/

unsigned int fp_oscar_tx(unsigned char *buff,
			 unsigned int len, unsigned int max,
			 proxy_track_struct *pt)
{
#if USE_DYNAMIC_DCC
    listen_track_struct *lt;
    struct timeval tv_now;
#endif
    
    // Not long enough for us to check properly...
    if (len < 26)
	return len;
    
    // Check its long enough for the TLV_TYPE
    if (len < AIM_BUD_NEXT(buff) + 2)
	return len;
    
    // Does it look like a potential Direct Connection setup packet ?
    if ((AIM_CMDSTART(buff)        == 0x2a) &&
	(AIM_CHANNEL(buff)         == 0x02) &&
	(AIM_FNAC_FAMILY(buff)     == 0x0004) &&
	(AIM_FNAC_SUBTYPE(buff)    == 0x0006) &&
	(AIM_FNAC_FLAGS(buff)      == 0x0000) &&
	(AIM_SNAC_CHAN(buff)       == 0x0002) &&
	(AIM_SNAC_TLV_TYPE(buff)   == 0x0005)) {

        int alloc_port, orig_dcc_port;
	int ip[4], ip_offset, src_ip[4], offset;
	struct sockaddr_in src_addr;
	int salen;
	unsigned char scan[12], mask[12];
	pending_dc_struct *pdc;

	// Read the source IP for the client connection.
	// We use this for extra sanity checks in locating the client IP
	// in the setup packet
	salen = sizeof(struct sockaddr_in);
	if (getpeername(pt->sockets[TS_CLIENT], (struct sockaddr*) &src_addr, &salen) == -1) {
	    return len;
	}
	
	sscanf(inet_ntoa(src_addr.sin_addr), "%d.%d.%d.%d", &src_ip[0], &src_ip[1], &src_ip[2], &src_ip[3]);

	// Look for a sequence in the packet that is 00 03 00 04 IP IP IP IP 00 05 00 02 ?? ??
	scan[0] = 0x00;
	scan[1] = 0x03;
	scan[2] = 0x00;
	scan[3] = 0x04;
	scan[4] = src_ip[0];
	scan[5] = src_ip[1];
	scan[6] = src_ip[2];
	scan[7] = src_ip[3];
	scan[8] = 0x00;
	scan[9] = 0x05;
	scan[10] = 0x00;
	scan[11] = 0x02;

	// Setup the mask
	memset(mask, 255, sizeof(mask));
	mask[4] = mask[5] = mask[6] = mask[7] = 0;
	
	// Search for the IP/port address sequence
	offset = fp_locate(buff, len, scan, 12, mask);
	if (offset == -1)
	  return len;

	// Extract the original DCC port on the inside host
	orig_dcc_port = (buff[offset + 12] << 8) | buff[offset + 13];

	// Point to the actual address by adjusting over the search preamble.
	ip_offset = offset + 4;
	
	// Okay.  Determine which address to overwrite the client IP with...
	if (outside_ip == NULL) {
	    
	    // Make a best guess here because we weren't told....
	    struct sockaddr_in svr_addr;
	    
	    // Read the source address of the connection to the server
	    // and give up if we can't...
	    salen = sizeof(struct sockaddr_in);
	    if (getsockname(pt->sockets[TS_SERVER], (struct sockaddr*) &svr_addr, &salen) == -1) {
		write_log(log_file, "Found a DCC F setup, but couldn't get the external IP - dodo mode.");
		return len;
	    }
	    
	    sscanf(inet_ntoa(svr_addr.sin_addr), "%d.%d.%d.%d",
		   &ip[0], &ip[1], &ip[2], &ip[3]);
	    
	} else {
	    
	    if (sscanf(outside_ip, "%d.%d.%d.%d",
		       &ip[0], &ip[1], &ip[2], &ip[3]) != 4) {
		write_log(log_file, "Bad external proxy IP address specified - [%s] Dodo mode",
			  outside_ip);
		return len;
	    }
	}
	
#if USE_DYNAMIC_DCC
	lt = create_listener_between(INC_MIN, INC_MAX, net_open_aim);
	// Couldn't create onward, so can't mangle this request.
	if (!lt)
	  return len;
	
	// Setup as a 1 shot with a 30 second limit.
	lt->oneshot = 1;
	gettimeofday(&tv_now, NULL);
	lt->timeout = tv_now.tv_sec + INC_TIMEOUT;
	
	alloc_port = lt->port;
#else
	alloc_port = SHARE;
#endif
	
	// Mangle the data so it has the outside IP address
	// and port in the right place.
	buff[ip_offset + 0] = ip[0];
	buff[ip_offset + 1] = ip[1];
	buff[ip_offset + 2] = ip[2];
	buff[ip_offset + 3] = ip[3];
	buff[ip_offset + 8] = (alloc_port >> 8) & 0xff;
	buff[ip_offset + 9] = alloc_port & 0xff;
	
	// Report the mangling we just did
	write_log(log_file,"[MASQ] DCC F Setup IP Changed "
		  "[%d.%d.%d.%d:%d -> %d.%d.%d.%d:%d] [offset=%d] [buddy=%.*s]",
		  src_ip[0], src_ip[1], src_ip[2], src_ip[3], orig_dcc_port,
		  ip[0], ip[1], ip[2], ip[3], alloc_port,
		  ip_offset,
		  AIM_BUD_NAME_LEN(buff), AIM_BUD_NAME(buff));
	
	// Allocate a pending_dc_struct here.
	pdc = malloc(sizeof(pending_dc_struct));
	memset(pdc, 0, sizeof(pending_dc_struct));

	pdc->magic = pt->magic;
	pdc->id[0] = AIM_SNAC_COOKIE(buff)[0];
	pdc->id[1] = AIM_SNAC_COOKIE(buff)[1];
	pdc->id[2] = AIM_SNAC_COOKIE(buff)[2];
	pdc->id[3] = AIM_SNAC_COOKIE(buff)[3];
	pdc->id[4] = AIM_SNAC_COOKIE(buff)[4];
	pdc->id[5] = AIM_SNAC_COOKIE(buff)[5];
	pdc->id[6] = AIM_SNAC_COOKIE(buff)[6];
	pdc->id[7] = AIM_SNAC_COOKIE(buff)[7];

	pdc->port = alloc_port;
	pdc->orig_port = orig_dcc_port;

	// Copy the original source address information into the DC struct
	// We need this to establish the back connection.
	memcpy(&pdc->src, &src_addr, sizeof(src_addr));
	
	pdc->next = dc_list.next;
	dc_list.next = pdc;
    }
    
    return len;
}


/*-------------------------------------------------------------------------
  unsigned int fp_msn_tx_dcc_setup(unsigned char *buff,
                                   unsigned int len, unsigned int max,
		                   proxy_track_struct *pt)
  
  This function examines data flowing out from this machine for answers
  to direct connection setup queries.
  
  If it looks like a DCC request, we search for the IP address and port
  being sent to the remote party, and we overwrite it with the external
  IP address, and a port from the dynamic dcc range.

  Having done that, we setup a pending connection structure, and create
  the listenner that will process the incoming request from the remote
  party. (See net_open_msnrx for the incoming request processing)
-------------------------------------------------------------------------*/

unsigned int fp_msn_tx_dcc_setup(unsigned char *buff,
			 unsigned int len, unsigned int max,
			 proxy_track_struct *pt)
{
    int port, cookie;

    int ip[4], src_ip[4];
    struct sockaddr_in src_addr;
    int salen;
    pending_dc_struct *pdc;
    char copy_msg[2048], *pHdrs[32], *pMsgs[32], *scan, tmpbuf[256];
    int hdr_fields, msg_fields, find, dcc_port;
#if USE_DYNAMIC_DCC
    listen_track_struct *lt;
    struct timeval tv_now;
#endif
    
    // Not long enough for a dcc setup request or too long...
    if (len < 85 || len >= sizeof(copy_msg))
	return len;
    
    memcpy(copy_msg, buff, len);
    copy_msg[len] = '\0';

    hdr_fields = 0;
    
    for (scan = copy_msg;
	 (scan != NULL) &&
	 (scan[0] != '\0') &&
	     (scan[0] != '\r') &&
	     (scan[1] != '\n');) {

	pHdrs[hdr_fields] = scan;
	hdr_fields ++;
	if (hdr_fields == 32)
	    return len;
	
	scan = strstr(scan, "\r\n");
	if (scan) {
	    *scan = '\0'; // NULL terminate the pHdr
	    scan += 2;    // Shuffle past the \r\n
	}
    }
    
    // Badly formed buffer
    if (!scan || scan[0] == '\0')
	return len;

    scan += 2; // Skip the \r\n separator

    msg_fields = 0;
    
    for (;
	 (scan != NULL) &&
	 (scan[0] != '\0') &&
	     (scan[0] != '\r') &&
	     (scan[1] != '\n');) {

	pMsgs[msg_fields] = scan;
	msg_fields ++;
	if (msg_fields == 32)
	    return len;
	
	scan = strstr(scan, "\r\n");
	if (scan) {
	    *scan = '\0'; // NULL terminate the pMsg
	    scan += 2;    // Shuffle past the \r\n
	}
    }

#if TRILLIAN_HEADER_SUPPORT
    // Trillian doesn't end the headers with two \n\r pairs as
    // it should, so we do actually reach the end of the buffer
    // when looking for headers above.  If we're compiling with
    // TRILLIAN support, skip this check, even though it potentially
    // confuses the header parsing with the user message.

#else
    // Badly formed buffer without two \n\r pairs at the end of the headers.
    if (!scan || scan[0] == '\0')
	return len;
#endif

    // Look for the invite / accepted message....

    for (find = 0; find < hdr_fields; find ++) {
	if (strcmp(pHdrs[find],
		   "Content-Type: text/x-msmsgsinvite; charset=UTF-8") == 0)
	    break;
    }
    
    if (find == hdr_fields)
	return len;

    for (find = 0; find < msg_fields; find ++) {
	if (strcmp(pMsgs[find],
		   "Invitation-Command: ACCEPT") == 0)
	    break;
    }

    if (find == msg_fields)
	return len;

    // Find the IP address header in the packet
    for (find = 0; find < msg_fields; find ++) {
	if (strncmp(pMsgs[find], "IP-Address: ",
		    strlen("IP-Address: ")) == 0) {

	    if (sscanf(pMsgs[find], "IP-Address: %d.%d.%d.%d",
		       &ip[0], &ip[1], &ip[2], &ip[3]) != 4) {
		write_log(log_file, "MSN DCC: Badly formed IP Address - dodo mode");
		return len;
	    }
	    
	    break;
	}
    }
    
    if (find == msg_fields)
	return len;
    
    // Find the IP address header in the packet
    for (find = 0; find < msg_fields; find ++) {
	if (strncmp(pMsgs[find], "Port: ",
		    strlen("Port: ")) == 0) {
	    
	    if (sscanf(pMsgs[find], "Port: %d",
		       &port) != 1) {
		write_log(log_file, "MSN DCC: Badly formed IP port specifier - dodo mode");
		return len;
	    }
	    
	    break;
	}
    }
    
    if (find == msg_fields)
	return len;
    
    // Find the AuthCookie in the packet
    for (find = 0; find < msg_fields; find ++) {
	if (strncmp(pMsgs[find], "AuthCookie: ",
		    strlen("AuthCookie: ")) == 0) {

	    if (sscanf(pMsgs[find], "AuthCookie: %d",
		       &cookie) != 1) {
		write_log(log_file, "MSN DCC: no cookie found - dodo mode");
		return len;
	    }
	    
	    break;
	}
    }
    
    if (find == msg_fields)
	return len;
    
    // Read the source IP for the client connection.
    // We use this for extra sanity checks in locating the client IP in
    // the setup packet
    salen = sizeof(struct sockaddr_in);
    if (getpeername(pt->sockets[TS_CLIENT], (struct sockaddr*) &src_addr, &salen) == -1) {
	return len;
    }
    
    sscanf(inet_ntoa(src_addr.sin_addr), "%d.%d.%d.%d",
	   &src_ip[0], &src_ip[1], &src_ip[2], &src_ip[3]);
    
    if ((ip[0] != src_ip[0]) || (ip[1] != src_ip[1]) ||
	(ip[2] != src_ip[2]) || (ip[3] != src_ip[3])) {
#if LOOSE_CLIENT_IP
	// If strict checking is on, just note the discrepency...
	write_log(log_file, "MSN Loose DCC Source. Expecting %d.%d.%d.%d, found %d.%d.%d.%d. Continuing mangle.", 
		  src_ip[0], src_ip[1], src_ip[2], src_ip[3],
		  ip[0], ip[1], ip[2], ip[3]);
#else
	// If strict checking is on, complain and don't mangle.
	write_log(log_file, "MSN Loose DCC Source. Expecting %d.%d.%d.%d, found %d.%d.%d.%d. Not overwriting.", 
		  src_ip[0], src_ip[1], src_ip[2], src_ip[3],
		  ip[0], ip[1], ip[2], ip[3]);
	return len;
#endif
    }
    
    // Okay.  Now to determine which address to overwrite the client IP with...
    if (outside_ip == NULL) {
	    
	// Make a best guess here because we weren't told....
	struct sockaddr_in svr_addr;
	
	// Read the source address of the server connection...
	salen = sizeof(struct sockaddr_in);
	if (getsockname(pt->sockets[TS_SERVER], (struct sockaddr*) &svr_addr, &salen) == -1) {
	    write_log(log_file, "MSN Found a DC setup, but couldn't work out the external IP - dodo mode");
	    return len;
	}
	
	sscanf(inet_ntoa(svr_addr.sin_addr), "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
	
    } else {
	sscanf(outside_ip, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
    }
    
#if USE_DYNAMIC_DCC
    lt = create_listener_between(INC_MIN, INC_MAX, net_open_msnrx);
    // Couldn't create onward, so can't mangle this request.
    if (!lt)
      return len;
    
    // Setup as a 1 shot with a 30 second limit.
    lt->oneshot = 1;
    gettimeofday(&tv_now, NULL);
    lt->timeout = tv_now.tv_sec + INC_TIMEOUT;
    
    dcc_port = lt->port;
#else
    dcc_port = MSNRX;
#endif

    // Create the new packet, now we have all the data.
    
    write_log(log_file, "MSN DCC Setup IP Changed...%d.%d.%d.%d:%d -> %d.%d.%d.%d:%d",
	      src_ip[0], src_ip[1], src_ip[2], src_ip[3], port,
	      ip[0], ip[1], ip[2], ip[3], dcc_port);
    
    // copy over the headers
    for (find = 0, scan = buff, len = 0; find < hdr_fields; find++) {
	
	sprintf(scan, "%s\r\n", pHdrs[find]);
	scan = &scan[(strlen(pHdrs[find]) + 2)];
	len += (strlen(pHdrs[find]) + 2);
    }
    
    sprintf(scan, "\r\n");
    scan = &scan[2];
    len += 2;
    
    // Copy over and mangle as required the msg fields
    for (find = 0; find < msg_fields; find ++) {
	
	int done = 0;
	
	if (strncmp(pMsgs[find], "IP-Address: ",
		    strlen("IP-Address: ")) == 0) {
	    
	    sprintf(tmpbuf, "IP-Address: %d.%d.%d.%d",
		    ip[0], ip[1], ip[2], ip[3]);
	    
	    sprintf(scan, "%s\r\n", tmpbuf);
	    scan = &scan[(strlen(tmpbuf) + 2)];
	    len += (strlen(tmpbuf) + 2);
	    done = 1;
	    
	} else if (strncmp(pMsgs[find], "Port: ",
		    strlen("Port: ")) == 0) {
	    
	    sprintf(tmpbuf, "Port: %d", dcc_port);
	    
	    sprintf(scan, "%s\r\n", tmpbuf);
	    scan = &scan[(strlen(tmpbuf) + 2)];
	    len += (strlen(tmpbuf) + 2);
	    done = 1;
	    
	}
	
	if (!done) {
	    // Just copy over the field unchanged
	    sprintf(scan, "%s\r\n", pMsgs[find]);
	    scan = &scan[(strlen(pMsgs[find]) + 2)];
	    len += (strlen(pMsgs[find]) + 2);
	    
	}
    }

    // Finish off the command building
    sprintf(scan, "\r\n");
    scan = &scan[2];
    len += 2;
    
    // Allocate a pending_dc_struct here.
    pdc = calloc(sizeof(pending_dc_struct), 1);
    if (!pdc) {
      // Damn.  Ran out of RAM.  All hell is about to break loose.
      // There won't be a direct connect struct for this DCC, so
      // the DCC will fail, but it should only fail with an error
      // in the net_open_msnrx procedure.
      return len;
    }
    
    pdc->orig_port  = port;
    pdc->port       = dcc_port;
    
    // Copy the original source address information into the DC struct
    // We need this to establish the back connection.
    memcpy(&pdc->src, &src_addr, sizeof(src_addr));
    
    // Add to pending DC list
    pdc->next    = dc_list.next;
    dc_list.next = pdc;
    
    return len;
}

/*
 * Read in a filter configuration / control file
 *
 * Also dynamic link any modules when I get that far...
 *
 * For now, just do it statically.
 */
void setup_filters(const char *ini_file)
{
    // AIM FILTERS
    memset(&oscar_tx, 0, sizeof(oscar_tx));
#if USE_DYNAMIC_DCC
    oscar_tx.proc[0] = fp_oscar_tx;                    // LAN client to AIM filter
#else
    oscar_tx.proc[0] = fp_oscar_tx;                    // LAN client to AIM filter
#endif

    memset(&oscar_rx, 0, sizeof(oscar_rx));

    //share_tx;   // LAN to Remote filter
    memset(&share_tx, 0, sizeof(share_tx));

    //share_rx;   // Remote to LAN filter
    memset(&share_rx, 0, sizeof(share_rx));

    // MSN FILTERS
    memset(&msn_tx, 0, sizeof(msn_tx));
    msn_tx.proc[0] = fp_msn_tx_dcc_setup;  // LAN client to MSG filter

    //msn_rx;
    memset(&msn_rx, 0, sizeof(msn_rx));

    //msnrx_tx;   // MSN file xfer things
    memset(&msnrx_tx, 0, sizeof(msnrx_tx));

    //msnrx_rx;   
    memset(&msnrx_rx, 0, sizeof(msnrx_rx));
}

/*-------------------------------------------------------------------------
 * Open a new listen socket, and attach a track struct to the
 * linked list of tracking structures.
 *
 * Return NULL on failure, or the newly allocated and added
 * track struct on success.
 *-----------------------------------------------------------------------*/
listen_track_struct* create_listener_on(int port, listen_proc lp)
{
  listen_track_struct *lt;
  
  // Allocate a new track structure if possible.
  lt = malloc(sizeof(listen_track_struct));
  if (!lt)
    return NULL;
  memset(lt, 0, sizeof(listen_track_struct));
  
  lt->proc = lp;
  lt->port = port;
  lt->socket = net_listen(port, "0.0.0.0");
  if (lt->socket == -1) {
    free(lt);
    return NULL;
  }
  
  // Add to the tracked socket list.
  lt->next = lt_list.next;
  lt_list.next = lt;
  
  return lt;
}

/*-------------------------------------------------------------------------
 * Open a new listen socket, and attach a track struct to the
 * linked list of tracking structures.
 *
 * Return NULL on failure, or the newly allocated and added
 * track struct on success.
 *
 * This call searches the specified port range for an open port to use.
 * The port allocated is returned in the listen_track_struct for the
 * caller.
 *-----------------------------------------------------------------------*/
listen_track_struct* create_listener_between(int port_min, int port_max, listen_proc lp)
{
  listen_track_struct *lt;
  
  // Allocate a new track structure if possible.
  lt = malloc(sizeof(listen_track_struct));
  if (!lt)
    return NULL;
  memset(lt, 0, sizeof(listen_track_struct));
  
  lt->oneshot = 1;
  lt->proc = lp;
  lt->socket = net_listen_between(port_min, port_max, "0.0.0.0", &lt->port);
  if (lt->socket == -1) {
    free(lt);
    return NULL;
  }
  
  // Add to the tracked socket list.
  lt->next = lt_list.next;
  lt_list.next = lt;
  
  return lt;
}

/*
 * Read in a filter configuration / control file
 *
 * Also dynamic link any modules when I get that far...
 *
 * For now, just do it statically.
 */
int setup_listenners(const char *ini_file)
{
  int n_lt;
  listen_track_struct *lt;
  
  //
  // Interception for data heading out to external hosts.
  //
  // These procedures attach the appropriate filtering
  // routines for that type of connection.
  //

  if (enable_msn) {
    create_listener_on(MSN,     net_open_msn);
#if USE_DYNAMIC_DCC
    // Log invalid attempts
    create_listener_on(MSNRX,   net_open_log_attempt);
#else
    create_listener_on(MSNRX,   net_open_msnrx);
#endif
  } else {
    write_log(log_file, "[STARTUP] Support for MSN is disabled");
  }
  
  if (enable_aim) {
    create_listener_on(OSCAR,   net_open_aim_oscar);
#if USE_DYNAMIC_DCC
    // Log invalid shares
    create_listener_on(SHARE,   net_open_log_attempt);
    // Some AIM clients are buggy, so watch for that
    create_listener_on(DCC,     net_open_buggy_aim_dcc);
#else
    create_listener_on(SHARE,   net_open_aim);
    create_listener_on(DCC,     net_open_aim);
#endif
  } else {
    write_log(log_file, "[STARTUP] Support for AIM is disabled");
  }

  // Count listen_track_struct
  n_lt = 0;
  lt = lt_list.next;
  while(lt != NULL) {
    n_lt ++;
    lt = lt->next;
  }
  
  return n_lt;
}

void close_listenners(const char *ini_file)
{
    listen_track_struct *lt;
    
    // Run around the listenners list and free any listenners
    while (lt_list.next) {

      lt = lt_list.next;
      lt_list.next = lt->next;

      close(lt->socket);
      free(lt);
    }
}

/* HUP Signal Handler */
void sig_hup(int i)
{
  pending_hup = 1;
}

/* Setup as a demon.  Parent process is terminated */
void demonize()
{
    pid_t pid;
    int sockets, max_sockets;

    /* Fork and close the parent */
    pid = fork();
    
    /* Parent has pid of child.  Child has pid == 0 */
    if (pid != 0) {
      _exit(0); // close the parent
    }
    
    // Setup as a process leader...
    setsid();
    
    /* Fork and close the process leader */
    pid = fork();

    /* Parent has pid of child.  Child has pid == 0 */
    if (pid != 0) {
      _exit(0); // close the parent
    }
    
    /* Change to root directory */
    chdir("/");

    /* Close any open sockets.  Including stdout/stderr/stdin.
     * Perhaps I should reopen 0/1/2, but ReAIM doesn't use them
     */
    max_sockets = sysconf(_SC_OPEN_MAX);
    for (sockets = 0; sockets < max_sockets; sockets ++)
      close(sockets);
}

void usage()
{
  printf("Usage: reaim [-h] [-m] [-a] [-e <ip>] [-l log] [-d]\n"
	 "\n"
	 "  -h | --help                   : Show this text.\n"
	 "  -m | --disable-msn            : Disable MSN support.\n"
	 "  -a | --disable-aim            : Disable AIM support.\n"
	 "  -e <ip> | --external-ip <ip>  : Manually set the override ip address.\n"
	 "  -l <log> | --logfile <log>    : Set logfile location.\n"
	 "                                    Default: %s\n"
	 "  -d                            : Do not fork into the background.\n",
	 LOG_FILE);
}

int parse_options(int argc, char * argv[])
{
  int option_index = 0;
  int c;
  
  while (1) {
    
    c = getopt_long(argc, argv, "mae:hl:d",
                    long_options, &option_index);
    
    if (c == -1)
      break;
    
    switch(c) {
    case 'm':
      enable_msn = 0;
      break;
    case 'a':
      enable_aim = 0;
      break;
    case 'e':
      outside_ip = strdup(optarg);
      break;
    case 'h':
      usage();
      return 0;
      break;
    case 'l':
      log_file = strdup(optarg);
      break;
    case 'd':
      enable_fork = 0;
      break;
    }
  }
  
  return 1;
}

int main(int argc, char *argv[])
{
    fd_set rfds, efds, wfds;
    struct timeval tv_sel, tv_now;
    int sv;
    struct sigaction set_act, old_act;
    proxy_track_struct *pt, *prev_pt;
    listen_track_struct *lt, *plt;
    pending_connect_struct *pc;
    int max_fd, sock;
    pending_dc_struct *pdc, *prev_pdc;
#if PERIODIC_LOG
    struct timeval tv_stats;
    tv_stats.tv_sec = 0;
#endif
    
    if (!parse_options(argc, argv))
      exit(-5);
    
    if (write_log(log_file,
		  "[STARTUP] Started AIM-Fix Proxy [%s%s]",
		  REAIM_VERSION, PLATFORM) == 0) {
      if (enable_fork) {
	fprintf(stderr,
		"[STARTUP] Started AIM-Fix Proxy [%s%s]\n"
		"[WARNING] Unable to write log to %s.\n"
		"[WARNING] Proxy will continue, but logging will not work.\n",
		REAIM_VERSION, PLATFORM, log_file);
      } else {
	// In the case of invalid log file but not forking, write_log
	// writes to stderr
	write_log(log_file,
		  "[WARNING] Unable to write log to %s.\n", log_file);
	write_log(log_file,
		  "[WARNING] Proxy will run, but logging will not work.\n");
      }
    };
    
    if (enable_fork)
      demonize();
    
    /* Setup the action table for SIGPIPE - ignore. */
    memset(&set_act,0,sizeof(set_act));
    set_act.sa_handler  = SIG_IGN;
    sigemptyset(&set_act.sa_mask);
    set_act.sa_flags    = 0;
    sigaction(SIGPIPE, &set_act, &old_act);
    
    /* Setup the action table for SIGHUP - rebind the listenning sockets. */
    memset(&set_act,0,sizeof(set_act));
    set_act.sa_handler  = sig_hup;
    sigemptyset(&set_act.sa_mask);
    set_act.sa_flags    = 0;
    sigaction(SIGHUP, &set_act, &old_act);

    if (outside_ip)
      write_log(log_file, "[STARTUP] Using %s for the external IP address.", outside_ip);
    else
      write_log(log_file, "[STARTUP] Auto-detecting the external IP address.");
    
    setup_filters("");
    
    if (setup_listenners("") == 0) {
      write_log(log_file, "[WARNING] Not listenning on any sockets.");
      write_log(log_file, "[WARNING] Already running / misconfigured ?");
    }
    
    // - Wait for connections to the redirected port
    // - Setup the onward connection to listen
    // - Make onward connection to source
    // - Push around any packets
    
    while(1) {

      // Check for HUP...
      if (pending_hup) {
	write_log(log_file, "[SIGNAL] SIGHUP received.  Rebinding listenning sockets.");
	close_listenners("");
	setup_listenners("");
	pending_hup = 0;
      }
      
      // Setup the list of sockets we are monitoring
      FD_ZERO(&rfds);
      FD_ZERO(&efds);
      FD_ZERO(&wfds);
      
      max_fd = 0;
      
      // Get the current time
      gettimeofday(&tv_now, NULL);
      
#if PERIODIC_LOG
      // Periodic stats.
      // 1 line every 5 minutes.
      // Useful for finding leaks in the list handling.
      if (tv_stats.tv_sec < tv_now.tv_sec) {
	periodic_log();
	tv_stats.tv_sec = tv_now.tv_sec + 60*5;
      }
#endif


      // Process the listen_track list.
      // - Timeout processing
      // - Cleanup processing
      // - Add any listenners to the socket list.
      plt = &lt_list;
      lt = lt_list.next;
      while(lt != NULL) {
	
	if ((lt->timeout != 0) && (lt->timeout < tv_now.tv_sec)) {
	  write_log(log_file, "[TIMEOUT] Listen on proxy:%d timed out.", lt->port);
	  lt->cleanup = 1;
	}
	
	if (lt->cleanup) {
	  
	  plt->next = lt->next;
	  
	  close(lt->socket);
	  
	  // As we just stopped listening to lt->port, kill any
	  // pending connects for that port too.
	  pdc = dc_list.next;
	  while(pdc) {
	    if (pdc->port == lt->port)
	      pdc->cleanup = 1;
	    
	    pdc = pdc->next;
	  }
	  
	  free(lt);
	  
	} else {
	  
	  if (lt->socket > max_fd)
	    max_fd = lt->socket;
	  
	  FD_SET(lt->socket, &rfds);
	  
	  plt = lt;
	}
	
	// Advance loop
	lt = plt->next;
      }
      
      // Cleanup any pending_dc_list entries that are flagged for cleanup
      prev_pdc = &dc_list;
      pdc = dc_list.next;
      while(pdc) {
	if (pdc->cleanup) {
	  pending_dc_struct *next_dc;
	  
	  prev_pdc->next = pdc->next;

	  next_dc = pdc->next;
	  free(pdc);
	  pdc = next_dc;
	} else {
	  prev_pdc = pdc;
	  pdc = pdc->next;
	}
      }
      
	pt = pt_list.next;
	prev_pt = &pt_list;
	while(pt != NULL) {

	    // Cleanup any proxy connections that are flagged...
	    if (pt->cleanup) {

		proxy_track_struct *clean_pt = pt;
		
		write_log(log_file, "[Cleanup] proxy_track_struct [magic=%d] [user=%d]",
			  pt->magic, pt->user);

		// Adjust the linked list
		prev_pt->next = pt->next;
		pt = pt->next;

		// Clean up the proxy track structure data
		
		if (clean_pt->data_buff[0])
		    free(clean_pt->data_buff[0]);

		if (clean_pt->data_buff[1])
		    free(clean_pt->data_buff[1]);

		if (clean_pt->sockets[0] != -1)
		    close(clean_pt->sockets[0]);
		if (clean_pt->sockets[1] != -1)
		    close(clean_pt->sockets[1]);

		// If it's the user-associated socket, zap the user
		if (clean_pt->user == 1) {

		    user_track_struct *user_ut = user_list.next;

		    while(user_ut) {
			if (user_ut->magic == clean_pt->magic)
			    user_ut->cleanup = 1;
			
			user_ut = user_ut->next;
		    }
		}
		
		free(clean_pt);
		
		continue;
	    }

	    // Add the sockets to the read/write selection arrays
	    // as appropriate.
	    for (sock = 0; sock < 2; sock ++) {

		if (pt->sockets[sock] != -1) {
		    FD_SET(pt->sockets[sock], &rfds);
		    
		    if (pt->data_len[sock] > 0)
			FD_SET(pt->sockets[sock], &wfds);
		    
		    if (pt->sockets[sock] > max_fd)
			max_fd = pt->sockets[sock];
		}
	    }

	    prev_pt = pt;
	    pt = pt->next;
	}
	
	// Add any pending connection sockets to the write check

	pc = &pc_list;
	while(pc->next) {
	  if (pc->next->socket == -1) {

	    // Free the finished up socket
	    pending_connect_struct *pct;

	    pct = pc->next;
	    pc->next = pct->next;
	    free(pct);

	  } else {

	    // Check for a timeout.
	    if (pc->next->timeout < tv_now.tv_sec) {

	      char *tmpstr;
	      tmpstr = strdup(inet_ntoa(pc->next->src.sin_addr));
	      write_log(log_file, "[CONN_NB] Connection [%s:%d -> %s:%d via proxy] failed "
			"[timeout reached] (%d %d)",
			tmpstr,                      ntohs(pc->next->src.sin_port),
			inet_ntoa(pc->next->dst.sin_addr), ntohs(pc->next->dst.sin_port),
			pc->next->client_sock, pc->next->socket);
	      free(tmpstr);
	      
	      close(pc->next->socket);
	      close(pc->next->client_sock);
	      pc->next->client_sock = -1;
	      pc->next->socket = -1;

	    } else {
	      
	      // Add the socket
	      FD_SET(pc->next->socket, &wfds);
	      if (pc->next->socket > max_fd)
		max_fd = pc->next->socket;
	    }
	    
	    pc = pc->next;
	  }
	}
	
	// Wait for some activity
	tv_sel.tv_usec = 0;
	tv_sel.tv_sec  = 1;
	
	sv = select(max_fd+1,&rfds,&wfds,&efds,&tv_sel);
	switch(sv) {
	  
	case -1:
	    /* Some socket error, maybe */
	    if (errno != EINTR) {
		perror("[FATAL] Server socket died: ");
		exit(1);
	    }
	    break;
	case 0:
	    // Just timed out with nothing going on.
	    break;
	    
	default:
	    
	    // Process listenners...
	    lt = lt_list.next;
	    while(lt != NULL) {
	      
	      if ((lt->socket != -1) &&
		  (FD_ISSET(lt->socket, &rfds))) {
		lt->proc(lt);
		if (lt->oneshot)
		  lt->cleanup = 1;
	      }
	      
	      lt = lt->next;
	    }
	    
	    // Check pending connection sockets
	    pc = &pc_list;
	    while(pc->next) {
	      if (pc->next->socket != -1) {
		
		if (FD_ISSET(pc->next->socket, &wfds)) {
		  pc->next->proc(pc->next);
		}
	      }
	      
	      pc = pc->next;
	    }
	    
	    // Check generic proxied port pairs
            pt = pt_list.next;
	    while(pt) {

		for (sock = 0; sock < 2; sock ++) {
		    
		    // On Write list ?  We must have pending data for it...
		    if ((pt->sockets[sock] != -1) &&
			(pt->cleanup == 0) &&
			FD_ISSET(pt->sockets[sock], &wfds)) {
			
			int txs;
			
			txs = write(pt->sockets[sock],
				    &pt->data_buff[sock][pt->data_offset[sock]],
				    pt->data_len[sock]);
			if (txs < 0) {
			    if ((errno != EINTR) && (errno != EAGAIN)) {
				write_log(log_file, "Socket write failure! cleaning up!");
				pt->cleanup = 1;
			    }
			} else {
			    pt->data_len[sock] -= txs;
			    if (pt->data_len[sock] == 0)
				pt->data_offset[sock] = 0;
			    else
				pt->data_offset[sock] += txs;
			}
		    }
		    
		    // On read list, we have data for the other half...
		    //
		    // We only read it if the other half's write buffer is
		    // completely empty.
		    //
		    if ((pt->sockets[sock] != -1) &&
			(pt->cleanup == 0) &&
			FD_ISSET(pt->sockets[sock], &rfds) &&
			(pt->data_len[sock ^ 1] == 0)) {
			
			int rxs;

			// Read into the other half's data buffer.
			rxs = read(pt->sockets[sock],
				   pt->data_buff[sock^1],
				   pt->data_max[sock^1]);
			if (rxs == -1) {
			    if (errno == EAGAIN)
				write_log(log_file, "FOOIE! FD_ISSET, but read() returned EAGAIN [%d]!",
					  pt->sockets[sock]);
			    if ((errno != EINTR) && (errno != EAGAIN)) {
				pt->cleanup = 1;
			    }
			}
			
			if (rxs == 0) {
			    write_log(log_file, "socket closed.  cleaning up.....");
			    pt->cleanup = 1;
			}
			
			if (rxs > 0) {
			    
			    pt->data_len[sock^1] =
				fp_process(pt->data_buff[sock^1],
					   rxs,
					   pt->data_max[sock^1],
					   pt->filter[sock],
					   pt);
			}
		    }
		    
		} // End of for(sock...)
		
		pt = pt->next;
		
	    } // End of while(pt)

	    break;
	} // End of switch(sv)
	
    } // End of while(1)
    
    return 0;
}