File: pcapsource.cc

package info (click to toggle)
kismet 2008-05-R1-4.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,236 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (3511 lines) | stat: -rw-r--r-- 109,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "config.h"

#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#ifdef HAVE_LINUX_WIRELESS
// Because some kernels include ethtool which breaks horribly...
// The stock ones don't but others seem to
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;

#include <asm/types.h>
#include <linux/if.h>
#include <linux/wireless.h>
#endif

#if defined(SYS_OPENBSD) || defined(SYS_NETBSD)
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#if defined(SYS_OPENBSD)
#include <dev/ic/if_wi_ieee.h>
#endif

#ifdef HAVE_RADIOTAP
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
#include <net80211/ieee80211_radiotap.h>
#endif

#endif

#ifdef SYS_DARWIN
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <sys/param.h>
#include <sys/sysctl.h>
extern "C" {
#include "apple80211.h"
#include <Carbon/Carbon.h>
#include "darwin_control_objc.h"
}
#endif

#ifdef SYS_FREEBSD
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>

#ifdef HAVE_RADIOTAP
#include <net80211/ieee80211_ioctl.h>
#include <net80211/ieee80211_radiotap.h>
#endif

#endif

#if (defined(SYS_LINUX) || defined(SYS_CYGWIN)) && defined(HAVE_RADIOTAP)
// We have to use a local include for now
#include "linux_ieee80211_radiotap.h"
#endif

#if (defined(SYS_DARWIN))
#include "osx_ieee80211_radiotap.h"
#endif

#ifdef HAVE_RADIOTAP
// Hack around some headers that don't seem to define all of these
#ifndef IEEE80211_CHAN_TURBO
#define IEEE80211_CHAN_TURBO    0x0010  /* Turbo channel */
#endif
#ifndef IEEE80211_CHAN_CCK
#define IEEE80211_CHAN_CCK      0x0020  /* CCK channel */ 
#endif
#ifndef IEEE80211_CHAN_OFDM
#define IEEE80211_CHAN_OFDM     0x0040  /* OFDM channel */
#endif
#ifndef IEEE80211_CHAN_2GHZ
#define IEEE80211_CHAN_2GHZ     0x0080  /* 2 GHz spectrum channel. */
#endif
#ifndef IEEE80211_CHAN_5GHZ
#define IEEE80211_CHAN_5GHZ     0x0100  /* 5 GHz spectrum channel */
#endif
#ifndef IEEE80211_CHAN_PASSIVE
#define IEEE80211_CHAN_PASSIVE  0x0200  /* Only passive scan allowed */
#endif
#ifndef IEEE80211_CHAN_DYN
#define IEEE80211_CHAN_DYN      0x0400  /* Dynamic CCK-OFDM channel */
#endif
#ifndef IEEE80211_CHAN_GFSK
#define IEEE80211_CHAN_GFSK     0x0800  /* GFSK channel (FHSS PHY) */
#endif

#include "tcpdump-extract.h"
#include <stdarg.h>
#endif

#include "pcapsource.h"
#include "util.h"

#include "madwifing_control.h"

#ifdef HAVE_LIBPCAP

// Work around broken pcap.h on cygwin... this is a TERRIBLE THING TO DO but
// libwpcap on the airpcap cd seems to come with a pcap.h header for standard
// pcap, while the lib contains this symbol.
#if defined(HAVE_PCAP_GETEVENT)
int pcap_event(pcap_t *);
#endif

// Inherit the global from kismet_server.cc for vap destruction.  This is handled
// much better in newcore, this is just a kluge to hold us over.
extern int vap_destroy;

// Pcap global callback structs
pcap_pkthdr callback_header;
u_char callback_data[MAX_PACKET_LEN];

// Open a source
int PcapSource::OpenSource() {
    channel = 0;

    errstr[0] = '\0';

    char *unconst = strdup(interface.c_str());

    pd = pcap_open_live(unconst, MAX_PACKET_LEN, 1, 1000, errstr);

    free(unconst);

    if (strlen(errstr) > 0)
        return -1; // Error is already in errstr

    #if defined (SYS_OPENBSD) || defined(SYS_NETBSD) || defined(SYS_FREEBSD) \
		|| defined(SYS_DARWIN)
	/* OSX hack which should work on other platforms still, cascade through
	 * desired DLTs and the "best one" should stick.  We try in the order we
	 * least want - 80211, avs, then radiotap. */
	pcap_set_datalink(pd, DLT_IEEE802_11);
	pcap_set_datalink(pd, DLT_IEEE802_11_RADIO_AVS);
	#if defined(HAVE_RADIOTAP)
    /* Request desired DLT on multi-DLT systems that default to EN10MB. 
	 * We do this later anyway but doing it here ensures we have the 
	 * desired DLT from the get go. */
	pcap_set_datalink(pd, DLT_IEEE802_11_RADIO);
	#endif
	// Hack to re-enable promisc mode since changing the DLT seems to make it
	// drop it on some bsd pcap implementations
	ioctl(pcap_get_selectable_fd(pd), BIOCPROMISC, NULL);
	// Hack to set the fd to IOIMMEDIATE, to solve problems with select() on bpf
	// devices on BSD
	int v = 1;
	ioctl(pcap_get_selectable_fd(pd), BIOCIMMEDIATE, &v);
    #endif

    paused = 0;

    errstr[0] = '\0';

    num_packets = 0;

    if (DatalinkType() < 0)
        return -1;

#ifdef HAVE_PCAP_NONBLOCK
    pcap_setnonblock(pd, 1, errstr);
#elif !defined(SYS_OPENBSD) && defined(HAVE_PCAP_GETSELFD)
    // do something clever  (Thanks to Guy Harris for suggesting this).
    int save_mode = fcntl(pcap_get_selectable_fd(pd), F_GETFL, 0);
    if (fcntl(pcap_get_selectable_fd(pd), F_SETFL, save_mode | O_NONBLOCK) < 0) {
        snprintf(errstr, 1024, "fcntl failed, errno %d (%s)",
                 errno, strerror(errno));
    }
#endif

    if (strlen(errstr) > 0)
        return -1; // Ditto
    
    return 1;
}

// Datalink, override as appropriate
carrier_type PcapSource::IEEE80211Carrier() {
    int ch = FetchChannel();

    if (ch > 0 && ch <= 14)
        return carrier_80211b;
    else if (ch > 34)
        return carrier_80211a;

    return carrier_unknown;
}

// Signal levels
int PcapSource::FetchSignalLevels(int *in_siglev, int *in_noiselev) {
    *in_siglev = 0;
    *in_noiselev = 0;
    return 0;
}

void PcapSource::SetSmartCRC(int in_smart) {
	if (in_smart && crc32_table == NULL) {
		crc32_table = new unsigned int[256];
		crc32_init_table_80211(crc32_table);
	}

	if (in_smart == 0 && crc32_table != NULL) {
		delete[] crc32_table;
		crc32_table = NULL;
	}

	decode_fcs = in_smart;
}

// Errorcheck the datalink type
int PcapSource::DatalinkType() {
    datalink_type = pcap_datalink(pd);

    // Blow up if we're not valid 802.11 headers
#if (defined(SYS_FREEBSD) || defined(SYS_OPENBSD)) || defined(SYS_NETBSD)
    if (datalink_type == DLT_EN10MB) {
        fprintf(stderr, "WARNING:  pcap reports link type of EN10MB but we'll fake "
                "it on BSD.\n"
                "This may not work the way we want it to.\n");
#if (defined(SYS_FREEBSD) || defined(SYS_NETBSD) && !defined(HAVE_RADIOTAP))
        fprintf(stderr, "WARNING:  Some Free- and Net- BSD drivers do not report "
                "rfmon packets\n"
                "correctly.  Kismet will probably not run correctly.  For better\n"
                "support, you should upgrade to a version of *BSD with Radiotap.\n");
#endif
        datalink_type = KDLT_BSD802_11;
    }
#else
    if (datalink_type == DLT_EN10MB) {
        snprintf(errstr, 1024, "pcap reported netlink type 1 (EN10MB) for %s.  "
                 "This probably means you're not in RFMON mode or your drivers are "
                 "reporting a bad value.  Make sure you have the correct drivers "
                 "and that entering monitor mode succeeded.", interface.c_str());
        return -1;
    }
#endif

    // Little hack to give an intelligent error report for radiotap
#ifndef HAVE_RADIOTAP
    if (datalink_type == DLT_IEEE802_11_RADIO) {
        snprintf(errstr, 1024, "FATAL: Radiotap link type reported but radiotap "
                 "support was not compiled into Kismet.");
        return -1;
    }
#endif
    
    if (datalink_type != KDLT_BSD802_11 && datalink_type != DLT_IEEE802_11 &&
        datalink_type != DLT_PRISM_HEADER && datalink_type != DLT_IEEE802_11_RADIO &&
		datalink_type != DLT_IEEE802_11_RADIO_AVS &&
		datalink_type != DLT_PPI) {
        fprintf(stderr, "WARNING:  Unknown link type %d reported.  Continuing on "
                "blindly...\n", datalink_type);
    }

    return 1;
}

int PcapSource::CloseSource() {
    pcap_close(pd);
    return 1;
}

int PcapSource::FetchDescriptor() {
#ifdef HAVE_PCAP_GETSELFD
    return pcap_get_selectable_fd(pd);
#elif defined(HAVE_PCAP_GETEVENT)
	return pcap_event(pd);
#else
	return -1;
#endif
}

void PcapSource::Callback(u_char *bp, const struct pcap_pkthdr *header,
                                 const u_char *in_data) {
    memcpy(&callback_header, header, sizeof(pcap_pkthdr));
    memcpy(callback_data, in_data, kismin(header->len, MAX_PACKET_LEN));
}

int PcapSource::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int ret;
    //unsigned char *udata = '\0';

    if ((ret = pcap_dispatch(pd, 1, PcapSource::Callback, NULL)) < 0) {
        // Is the interface still here and just not running?  Lets give a more intelligent
        // error if that looks to be the case.
        ret = 0;

        // Do something smarter here in the future
#ifdef SYS_LINUX
        int flags = 0;
        // Are we able to fetch the interface, and is it running?
        ret = Ifconfig_Get_Flags(interface.c_str(), errstr, &flags);
        if (ret >= 0 && (flags & IFF_UP) == 0) {
            snprintf(errstr, 1024, "Reading packet from pcap failed, interface "
					 "is no longer up.  Usually this happens when a DHCP client "
					 "times out and turns off the interface.  See the "
					 "Troubleshooting section of the README for more information.");
        } else {
#endif
            snprintf(errstr, 1024, "Reading packet from pcap failed, interface no "
					 "longer available.");
#ifdef SYS_LINUX
        }
        return -1;
#endif
    }

    if (ret == 0)
        return 0;

    if (paused || ManglePacket(packet, data, moddata) == 0) {
        return 0;
    }

    num_packets++;

    // Set the name
    snprintf(packet->sourcename, 32, "%s", name.c_str());
    
    // Set the parameters
    memcpy(&packet->parm, &parameters, sizeof(packet_parm));
    
    return(packet->caplen);
}

int PcapSource::ManglePacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int ret = 0;
    memset(packet, 0, sizeof(kis_packet));
    
    // packet->ts = callback_header.ts;
	packet->ts.tv_sec = callback_header.ts.tv_sec;
	packet->ts.tv_usec = callback_header.ts.tv_usec;
    packet->data = data;
    packet->moddata = moddata;
    packet->modified = 0;

    if (gpsd != NULL) {
        gpsd->FetchLoc(&packet->gps_lat, &packet->gps_lon, &packet->gps_alt,
                       &packet->gps_spd, &packet->gps_heading, &packet->gps_fix);
    }

    if (datalink_type == DLT_PRISM_HEADER || datalink_type == DLT_IEEE802_11_RADIO_AVS) {
        ret = Prism2KisPack(packet, data, moddata);
    } else if (datalink_type == KDLT_BSD802_11) {
        ret = BSD2KisPack(packet, data, moddata);
#ifdef HAVE_RADIOTAP
    } else if (datalink_type == DLT_IEEE802_11_RADIO) {
        ret = Radiotap2KisPack(packet, data, moddata);
#endif
	} else if (datalink_type == DLT_PPI) {
		ret = PPI2KisPack(packet, data, moddata);
    } else {
        unsigned int fcs = FCSBytes();
		if (callback_header.caplen <= fcs) {
			packet->error = 1;
			packet->caplen = 0;
			packet->len = 0;
			return 0;
		}

        packet->caplen = kismin(callback_header.caplen - fcs, 
				(uint32_t) MAX_PACKET_LEN);
        packet->len = packet->caplen;
        memcpy(packet->data, callback_data, packet->caplen);

		// If we're going to validate fcs, check it here */
		if (fcs && decode_fcs) {
			uint32_t *frame_crc = 
				(uint32_t *) &(callback_data[callback_header.caplen - 4]);
			uint32_t calc_crc = 
				crc32_le_80211(crc32_table, packet->data, packet->caplen);

			if (memcmp(frame_crc, &calc_crc, 4)) {
				packet->error = 1;
				// printf("debug - crc corrupt, got %08x expected %08x\n", calc_crc, *frame_crc);
				return 1;
			}

			// printf("debug - good - got crc %08x, expected %08x\n", calc_crc, *frame_crc);
		}

        ret = 1;
    }

    // Fetch the signal levels if we know how and it hasn't been already
    if (packet->signal == 0 && packet->noise == 0)
        FetchSignalLevels(&(packet->signal), &(packet->noise));
    
    // Fetch the channel if we know how and it hasn't been filled in already
    if (packet->channel == 0)
        packet->channel = FetchChannel();

#if 0
	// Maybe this isn't a great idea
    if (packet->carrier == carrier_unknown)
        packet->carrier = IEEE80211Carrier();
#endif

    return ret;
}

int PcapSource::Prism2KisPack(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int header_found = 0;
    unsigned int callback_offset = 0;

    // See if we have an AVS wlan header...
    avs_80211_1_header *v1hdr = (avs_80211_1_header *) callback_data;
    if (callback_header.caplen >= sizeof(avs_80211_1_header) &&
        ntohl(v1hdr->version) == 0x80211001 && header_found == 0) {

        if (ntohl(v1hdr->length) > callback_header.caplen) {
            snprintf(errstr, 1024, "pcap prism2 converter got corrupted AVS header length");
            packet->len = 0;
            packet->caplen = 0;
            return 0;
        }

        header_found = 1;

        // Get the FCS for this subclass
        int fcs = FCSBytes();

        // Subtract the packet FCS since kismet doesn't do anything terribly bright
        // with it right now

		if (callback_header.caplen < (ntohl(v1hdr->length) + fcs)) {
			fprintf(stderr, "*** WARNING - Strangeness with prism2 avs v1hdr->length "
					"and FCS size\n");
            snprintf(errstr, 1024, "pcap prism2 converter got corrupted AVS "
					 "header length");
            packet->len = 0;
            packet->caplen = 0;
            return 0;
        }
	
		// This should still be protected since a negative rollover on an unsigned
		// should go super-positive and overflow the max packet len and get trimmed
        packet->caplen = kismin(callback_header.caplen - ntohl(v1hdr->length) - fcs, 
                                (uint32_t) MAX_PACKET_LEN);
        packet->len = packet->caplen;

        /*
        packet->caplen = kismin(callback_header.caplen - 4 - ntohl(v1hdr->length), (uint32_t) MAX_PACKET_LEN);
        packet->len = packet->caplen;
        */

        callback_offset = ntohl(v1hdr->length);

        // We REALLY need to do something smarter about this and handle the RSSI
        // type instead of just copying
        packet->signal = ntohl(v1hdr->ssi_signal);
        packet->noise = ntohl(v1hdr->ssi_noise);

		// Attempt to correct RSSI with whats been reported as a proper conversion
		// method...
		if (packet->signal > 0) {
			packet->signal -= 0x100;
		}
		if (packet->noise > 0) {
			packet->noise -= 0x100;
		}

        packet->channel = ntohl(v1hdr->channel);

        switch (ntohl(v1hdr->phytype)) {
            case 1:
                packet->carrier = carrier_80211fhss;
				break;
            case 2:
                packet->carrier = carrier_80211dsss;
                break;
            case 4:
            case 5:
                packet->carrier = carrier_80211b;
                break;
            case 6:
            case 7:
                packet->carrier = carrier_80211g;
                break;
            case 8:
                packet->carrier = carrier_80211a;
                break;
            default:
                packet->carrier = carrier_unknown;
                break;
        }

        packet->encoding = (encoding_type) ntohl(v1hdr->encoding);

        packet->datarate = (int) ntohl(v1hdr->datarate);
    }

    // See if we have a prism2 header
    wlan_ng_prism2_header *p2head = (wlan_ng_prism2_header *) callback_data;
    if (callback_header.caplen >= sizeof(wlan_ng_prism2_header) &&
        header_found == 0) {

        header_found = 1;

        // Get the FCS
        int fcs = FCSBytes();

		/* We don't *really* have to pay attention to the wlanng-prism2 frame
		 * lengths, as long as we obey the capture frame lengths, so we'll
		 * ignore this for now.  Ethereal also ignores this length field. */
#if 0
        if (p2head->frmlen.data > callback_header.caplen) {
            snprintf(errstr, 1024, "pcap prism2 converter got corrupted prism2 "
					 "header length");
            packet->len = 0;
            packet->caplen = 0;
			printf("debug - corrupt avs header len %d vs %d\n",
				   p2head->frmlen.data, callback_header.caplen);
            return 0;
        }
#endif

		// Do a little more checking just in case
		if (callback_header.caplen < (sizeof(wlan_ng_prism2_header) + fcs)) {
			fprintf(stderr, "*** WARNING - Strangeness with prism2 header len "
					"and FCS size\n");
            snprintf(errstr, 1024, "pcap prism2 converter got corrupted prism2 "
					 "header length");
            packet->len = 0;
            packet->caplen = 0;
            return 0;
        }

		// We're safe here because of the initial test -- no matter what garbage
		// is in here, we're bigger than a prism2 header, so we can read the data
		// and set the offset.

        // Subtract the packet FCS since kismet doesn't do anything terribly bright
        // with it right now
        packet->caplen = kismin(callback_header.caplen - 
								sizeof(wlan_ng_prism2_header) - fcs,
								(uint32_t) MAX_PACKET_LEN);
        packet->len = packet->caplen;

        // Set our offset for extracting the actual data
        callback_offset = sizeof(wlan_ng_prism2_header);

        // packet->quality = p2head->sq.data;
        packet->signal = p2head->signal.data;
        packet->noise = p2head->noise.data;

		// Attempt to correct RSSI with whats been reported as a proper conversion
		// method...
		if (packet->signal > 0) {
			packet->signal -= 0x100;
		}
		if (packet->noise > 0) {
			packet->noise -= 0x100;
		}

        packet->channel = p2head->channel.data;

    }

    if (header_found == 0) {
        snprintf(errstr, 1024, "pcap prism2 converter saw undersized capture frame");
        packet->len = 0;
        packet->caplen = 0;
        return 0;
    }

	// This should be set up safely since all of the prior adjustments to caplen
	// take it into account
    memcpy(packet->data, callback_data + callback_offset, packet->caplen);

    return 1;
}

int PcapSource::PPI2KisPack(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
	ppi_packet_header *ppi_ph;
	ppi_field_header *ppi_fh;
	unsigned int ppi_fh_offt = sizeof(ppi_packet_header);
	unsigned int tuint, ph_len;
	int applyfcs = 0;

	if (callback_header.caplen < sizeof(ppi_packet_header)) {
		// printf("debug - too short for ppi header\n");
		snprintf(errstr, 1024, "pcap PPI converter got corrupt/invalid header "
				 "length");
		packet->len = 0;
		packet->caplen = 0;
		return 0;
	}

	ppi_ph = (ppi_packet_header *) callback_data;
	ph_len = kis_letoh16(ppi_ph->pph_len);
	if (ph_len > callback_header.caplen) {
		// printf("debug - ppi header len too short\n");
		snprintf(errstr, 1024, "pcap PPI converter got corrupt/invalid "
				 "header length");
		packet->len = 0;
		packet->caplen = 0;
		return 0;
	}

	// Ignore the DLT and treat it all as 802.11?  For now anyhow.
	while (ppi_fh_offt < callback_header.caplen && 
		   ppi_fh_offt < ph_len) {
		ppi_fh = (ppi_field_header *) &(callback_data[ppi_fh_offt]);
		unsigned int fh_len = kis_letoh16(ppi_fh->pfh_datalen);
		unsigned int fh_type = kis_letoh16(ppi_fh->pfh_datatype);
		// printf("debug - working on header, offset %u len %u\n", ppi_fh_offt, fh_len);
		if (fh_len > callback_header.caplen || fh_len > ph_len) {
			// printf("debug - field len too long %u for %u\n", fh_len, ph_len);
			snprintf(errstr, 1024, "pcap PPI converter got corrupt/invalid "
					 "field length");
			packet->len = 0;
			packet->caplen = 0;
			return 0;
		}

		ppi_fh_offt += fh_len + sizeof(ppi_field_header);

		if (fh_type == PPI_FIELD_11COMMON) {
			// printf("debug - 80211 common\n");
			ppi_80211_common *ppic = (ppi_80211_common *) ppi_fh;

			// Common flags
			tuint = kis_letoh16(ppic->flags);
			if ((tuint & PPI_80211_FLAG_INVALFCS) ||
				(tuint & PPI_80211_FLAG_PHYERROR)) {
				// Junk packets that are FCS or phy compromised
				packet->len = 0;
				packet->caplen = 0;
				return 0;
			}

			if (tuint & PPI_80211_FLAG_FCS)
				applyfcs = 1;

			// Channel flags
			tuint = kis_letoh16(ppic->chan_flags);
			if (tuint & PPI_80211_CHFLAG_CCK) {
				packet->encoding = encoding_cck;
			} else if (tuint & PPI_80211_CHFLAG_OFDM) {
				packet->encoding = encoding_ofdm;
			}

			packet->signal = ppic->signal_dbm;
			packet->noise = ppic->noise_dbm;

			packet->datarate = kis_letoh16(ppic->rate) * 5;
		} else if (fh_type == PPI_FIELD_11NMAC) {
			ppi_11n_mac *ppin = (ppi_11n_mac *) ppi_fh;

			// Decode greenfield notation
			tuint = kis_letoh16(ppin->flags);
			if (tuint & PPI_11NMAC_HT2040)
				packet->carrier = carrier_80211n20;
			else
				packet->carrier = carrier_80211n40;
		} else if (fh_type == PPI_FIELD_11NMACPHY) {
			ppi_11n_macphy *ppinp = (ppi_11n_macphy *) ppi_fh;

			// Decode greenfield notation
			tuint = kis_letoh16(ppinp->flags);
			if (tuint & PPI_11NMAC_HT2040)
				packet->carrier = carrier_80211n20;
			else
				packet->carrier = carrier_80211n40;
		}
	}

	if (applyfcs)
		applyfcs = FCSBytes();

	packet->caplen = kismin(callback_header.caplen - ph_len - applyfcs,
							(uint32_t) MAX_PACKET_LEN);
	packet->len = packet->caplen;

	memcpy(packet->data, callback_data + ph_len, packet->caplen);

	return 1;
}

int PcapSource::BSD2KisPack(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int callback_offset = 0;

    // Process our hacked in BSD type
    if (callback_header.caplen < sizeof(bsd_80211_header)) {
        snprintf(errstr, 1024, "pcap bsd converter saw undersized capture frame for bsd header.");
        packet->len = 0;
        packet->caplen = 0;
        return 0;
    }

    packet->caplen = kismin(callback_header.caplen - sizeof(bsd_80211_header), 
                            (uint32_t) MAX_PACKET_LEN);
    packet->len = packet->caplen;

    bsd_80211_header *bsdhead = (bsd_80211_header *) callback_data;

    packet->signal = bsdhead->wi_signal;
    packet->noise = bsdhead->wi_silence;

    // Set our offset
    callback_offset = sizeof(bsd_80211_header);
    memcpy(packet->data, callback_data + callback_offset, 24);

    // Adjust for driver appended headers
    if (packet->data[0] > 0x08) {
        packet->len -= 22;
        packet->caplen -= 22;
        memcpy(packet->data + 24, callback_data + callback_offset + 46, 
               packet->caplen - 2);
    } else {
        packet->len -= 30;
        packet->caplen -= 30;
        memcpy(packet->data + 24, callback_data + callback_offset + 46, 
               packet->caplen - 2);
    }

    return 1;
}

#ifdef HAVE_RADIOTAP
/*
 * Convert MHz frequency to IEEE channel number.
 */
static u_int ieee80211_mhz2ieee(u_int freq, u_int flags) {
    if (flags & IEEE80211_CHAN_2GHZ) {		/* 2GHz band */
	if (freq == 2484)
	    return 14;
	if (freq < 2484)
	    return (freq - 2407) / 5;
	else
	    return 15 + ((freq - 2512) / 20);
    } else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
	return (freq - 5000) / 5;
    } else {					/* either, guess */
	if (freq == 2484)
	    return 14;
	if (freq < 2484)
	    return (freq - 2407) / 5;
	if (freq < 5000)
	    return 15 + ((freq - 2512) / 20);
	return (freq - 5000) / 5;
    }
}

/*
 * Useful combinations of channel characteristics.
 */
#define	IEEE80211_CHAN_FHSS \
	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_GFSK)
#define	IEEE80211_CHAN_A \
	(IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM)
#define	IEEE80211_CHAN_B \
	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK)
#define	IEEE80211_CHAN_PUREG \
	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_OFDM)
#define	IEEE80211_CHAN_G \
	(IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_DYN)
#define	IEEE80211_CHAN_T \
	(IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM | IEEE80211_CHAN_TURBO)

#define	IEEE80211_IS_CHAN_FHSS(_flags) \
	((_flags & IEEE80211_CHAN_FHSS) == IEEE80211_CHAN_FHSS)
#define	IEEE80211_IS_CHAN_A(_flags) \
	((_flags & IEEE80211_CHAN_A) == IEEE80211_CHAN_A)
#define	IEEE80211_IS_CHAN_B(_flags) \
	((_flags & IEEE80211_CHAN_B) == IEEE80211_CHAN_B)
#define	IEEE80211_IS_CHAN_PUREG(_flags) \
	((_flags & IEEE80211_CHAN_PUREG) == IEEE80211_CHAN_PUREG)
#define	IEEE80211_IS_CHAN_G(_flags) \
	((_flags & IEEE80211_CHAN_G) == IEEE80211_CHAN_G)
#define	IEEE80211_IS_CHAN_T(_flags) \
	((_flags & IEEE80211_CHAN_T) == IEEE80211_CHAN_T)

int PcapSource::Radiotap2KisPack(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
#define BITNO_32(x) (((x) >> 16) ? 16 + BITNO_16((x) >> 16) : BITNO_16((x)))
#define BITNO_16(x) (((x) >> 8) ? 8 + BITNO_8((x) >> 8) : BITNO_8((x)))
#define BITNO_8(x) (((x) >> 4) ? 4 + BITNO_4((x) >> 4) : BITNO_4((x)))
#define BITNO_4(x) (((x) >> 2) ? 2 + BITNO_2((x) >> 2) : BITNO_2((x)))
#define BITNO_2(x) (((x) & 2) ? 1 : 0)
#define BIT(n)	(1 << n)
    union {
        int8_t	i8;
        int16_t	i16;
        u_int8_t	u8;
        u_int16_t	u16;
        u_int32_t	u32;
        u_int64_t	u64;
    } u;
    union {
        int8_t		i8;
        int16_t		i16;
        u_int8_t	u8;
        u_int16_t	u16;
        u_int32_t	u32;
        u_int64_t	u64;
    } u2;
    struct ieee80211_radiotap_header *hdr;
    u_int32_t present, next_present;
    u_int32_t *presentp, *last_presentp;
    enum ieee80211_radiotap_type bit;
    int bit0;
    const u_char *iter;
	// do we cut the FCS?  this is influenced by the radiotap headers and 
	// by the class fcsbytes value in case of forced fcs settings (like openbsd
	// atheros and Ralink USB at the moment)
	int fcs_cut = 0;

    if (callback_header.caplen < sizeof(*hdr)) {
        packet->len = 0;
        packet->caplen = 0;
        return 0;
    }
    hdr = (struct ieee80211_radiotap_header *) callback_data;
    if (callback_header.caplen < EXTRACT_LE_16BITS(&hdr->it_len)) {
        packet->len = 0;
        packet->caplen = 0;
        return 0;
    }

    for (last_presentp = &hdr->it_present;
         (EXTRACT_LE_32BITS(last_presentp) & BIT(IEEE80211_RADIOTAP_EXT)) != 0 &&
         (u_char*)(last_presentp + 1) <= data + EXTRACT_LE_16BITS(&hdr->it_len);
         last_presentp++);

    /* are there more bitmap extensions than bytes in header? */
    if ((EXTRACT_LE_32BITS(last_presentp) & BIT(IEEE80211_RADIOTAP_EXT)) != 0) {
        packet->len = 0;
        packet->caplen = 0;
        return 0;
    }

    packet->caplen = packet->len = callback_header.caplen;

    iter = (u_char*)(last_presentp + 1);

    for (bit0 = 0, presentp = &hdr->it_present; presentp <= last_presentp;
         presentp++, bit0 += 32) {
        for (present = EXTRACT_LE_32BITS(presentp); present; present = next_present) {
            /* clear the least significant bit that is set */
            next_present = present & (present - 1);

            /* extract the least significant bit that is set */
            bit = (enum ieee80211_radiotap_type)
                (bit0 + BITNO_32(present ^ next_present));

            switch (bit) {
                case IEEE80211_RADIOTAP_FLAGS:
                case IEEE80211_RADIOTAP_RATE:
                case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
                case IEEE80211_RADIOTAP_DB_ANTNOISE:
                case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
                case IEEE80211_RADIOTAP_DBM_ANTNOISE:
                case IEEE80211_RADIOTAP_ANTENNA:
                    u.u8 = *iter++;
                    break;
                case IEEE80211_RADIOTAP_DBM_TX_POWER:
                    u.i8 = *iter++;
                    break;
                case IEEE80211_RADIOTAP_CHANNEL:
                    u.u16 = EXTRACT_LE_16BITS(iter);
                    iter += sizeof(u.u16);
                    u2.u16 = EXTRACT_LE_16BITS(iter);
                    iter += sizeof(u2.u16);
                    break;
                case IEEE80211_RADIOTAP_FHSS:
                case IEEE80211_RADIOTAP_LOCK_QUALITY:
                case IEEE80211_RADIOTAP_TX_ATTENUATION:
                case IEEE80211_RADIOTAP_DB_TX_ATTENUATION:
                    u.u16 = EXTRACT_LE_16BITS(iter);
                    iter += sizeof(u.u16);
                    break;
                case IEEE80211_RADIOTAP_TSFT:
                    u.u64 = EXTRACT_LE_64BITS(iter);
                    iter += sizeof(u.u64);
                    break;
#if defined(SYS_OPENBSD)
                case IEEE80211_RADIOTAP_RSSI:
                    u.u8 = EXTRACT_LE_8BITS(iter);
                    iter += sizeof(u.u8);
                    u2.u8 = EXTRACT_LE_8BITS(iter);
                    iter += sizeof(u2.u8);
                    break;
#endif
                default:
                    /* this bit indicates a field whose
                     * size we do not know, so we cannot
                     * proceed.
                     */
                    next_present = 0;
                    continue;
            }

            switch (bit) {
                case IEEE80211_RADIOTAP_CHANNEL:
                    packet->channel = ieee80211_mhz2ieee(u.u16, u2.u16);
                    if (IEEE80211_IS_CHAN_FHSS(u2.u16))
                        packet->carrier = carrier_80211dsss;
                    else if (IEEE80211_IS_CHAN_A(u2.u16))
                        packet->carrier = carrier_80211a;
                    else if (IEEE80211_IS_CHAN_B(u2.u16))
                        packet->carrier = carrier_80211b;
                    else if (IEEE80211_IS_CHAN_PUREG(u2.u16))
                        packet->carrier = carrier_80211g;
                    else if (IEEE80211_IS_CHAN_G(u2.u16))
                        packet->carrier = carrier_80211g;
                    else if (IEEE80211_IS_CHAN_T(u2.u16))
                        packet->carrier = carrier_80211a;/*XXX*/
                    else
                        packet->carrier = carrier_unknown;
                    break;
                case IEEE80211_RADIOTAP_RATE:
		    /* strip basic rate bit & convert to kismet units */
                    packet->datarate = ((u.u8 &~ 0x80) / 2) * 10;
                    break;
                case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
                    packet->signal = u.i8;
                    break;
                case IEEE80211_RADIOTAP_DB_ANTNOISE:
                    packet->noise = u.i8;
                    break;
				case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
					packet->signal = u.i8;
					break;
				case IEEE80211_RADIOTAP_DBM_ANTNOISE:
					packet->noise = u.i8;
					break;
                case IEEE80211_RADIOTAP_FLAGS:
                    if (u.u8 & IEEE80211_RADIOTAP_F_FCS)
                         fcs_cut = 4;
                    break;
#if defined(SYS_OPENBSD)
                case IEEE80211_RADIOTAP_RSSI:
                    /* Convert to Kismet units */
                    packet->signal = int((float(u.u8) / float(u2.u8) * 255));
                    break;
#endif
#if 0
                case IEEE80211_RADIOTAP_FHSS:
                    printf("fhset %d fhpat %d ", u.u16 & 0xff,
                           (u.u16 >> 8) & 0xff);
                    break;
                case IEEE80211_RADIOTAP_LOCK_QUALITY:
                    printf("%u sq ", u.u16);
                    break;
                case IEEE80211_RADIOTAP_TX_ATTENUATION:
                    printf("%d tx power ", -(int)u.u16);
                    break;
                case IEEE80211_RADIOTAP_DB_TX_ATTENUATION:
                    printf("%ddB tx power ", -(int)u.u16);
                    break;
                case IEEE80211_RADIOTAP_DBM_TX_POWER:
                    printf("%ddBm tx power ", u.i8);
                    break;
                case IEEE80211_RADIOTAP_FLAGS:
                    if (u.u8 & IEEE80211_RADIOTAP_F_CFP)
                        printf("cfp ");
                    if (u.u8 & IEEE80211_RADIOTAP_F_SHORTPRE)
                        printf("short preamble ");
                    if (u.u8 & IEEE80211_RADIOTAP_F_WEP)
                        printf("wep ");
                    if (u.u8 & IEEE80211_RADIOTAP_F_FRAG)
                        printf("fragmented ");
                    break;
                case IEEE80211_RADIOTAP_ANTENNA:
                    printf("antenna %u ", u.u8);
                    break;
                case IEEE80211_RADIOTAP_TSFT:
                    printf("%llus tsft ", u.u64);
                    break;
#endif
                default:
                    break;
            }
        }
    }

	/* Check the fcs for the source */
	if (FCSBytes() != 0)
		fcs_cut = FCSBytes();

    /* copy data down over radiotap header */
    packet->caplen -= (EXTRACT_LE_16BITS(&hdr->it_len) + fcs_cut);
    packet->len -= (EXTRACT_LE_16BITS(&hdr->it_len) + fcs_cut);
    memcpy(packet->data, callback_data + EXTRACT_LE_16BITS(&hdr->it_len), packet->caplen);

    return 1;
#undef BITNO_32
#undef BITNO_16
#undef BITNO_8
#undef BITNO_4
#undef BITNO_2
#undef BIT
}
#endif

// Open an offline file with pcap
int PcapSourceFile::OpenSource() {
    channel = 0;

    errstr[0] = '\0';

    pd = pcap_open_offline(interface.c_str(), errstr);

    if (strlen(errstr) > 0)
        return -1; // Error is already in errstr

    paused = 0;

    errstr[0] = '\0';

    num_packets = 0;

    if (DatalinkType() < 0)
        return -1;
    
    return 1;
}

// Nasty hack into pcap priv functions to get the file descriptor.  This
// most likely is a bad idea.
#if 0
int PcapSourceFile::FetchDescriptor() {
    return fileno(pd->sf.rfile);
}
#endif

int PcapSourceFile::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int ret;
    //unsigned char *udata = '\0';

    ret = pcap_dispatch(pd, 1, PcapSource::Callback, NULL);

    if (ret < 0) {
        snprintf(errstr, 1024, "Pcap Get Packet pcap_dispatch() failed");
        return -1;
    } else if (ret == 0) {
        snprintf(errstr, 1024, "Pcap file reached end of capture.");
        return -1;
    }

    if (paused || ManglePacket(packet, data, moddata) == 0) {
        return 0;
    }

    // Set the name
    snprintf(packet->sourcename, 32, "%s", name.c_str());
    
    // Set the parameters
    memcpy(&(packet->parm), &parameters, sizeof(packet_parm));
    

    num_packets++;

    return(packet->caplen);
}

#ifdef HAVE_LINUX_WIRELESS
int PcapSourceWext::FetchSignalLevels(int *in_siglev, int *in_noiselev) {
    int raw_siglev, raw_noiselev, ret;

    if ((ret = Iwconfig_Get_Levels(interface.c_str(), errstr, 
                                   &raw_siglev, &raw_noiselev)) < 0)
        return ret;

    (*in_siglev) = raw_siglev;
    (*in_noiselev) = raw_noiselev;

    return 0;
}

// Carrier override
carrier_type PcapSource11G::IEEE80211Carrier() {
    int ch = FetchChannel();

    if (ch > 0 && ch <= 14)
        return carrier_80211g;
    else if (ch > 34)
        return carrier_80211a;

    return carrier_unknown;
}

#endif

#ifdef SYS_LINUX

// Handle badly formed jumbo packets from the drivers
int PcapSourceWrt54g::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    int ret;
    //unsigned char *udata = '\0';

    if ((ret = pcap_dispatch(pd, 1, PcapSource::Callback, NULL)) < 0) {
        snprintf(errstr, 1024, "Pcap Get Packet pcap_dispatch() failed");
        return -1;
    }

    if (ret == 0)
        return 0;

    if (paused || ManglePacket(packet, data, moddata) == 0) {
        return 0;
    }

    // Junk packets that are too big, this is the only real way to detect crap
    // packets...
    if (packet->caplen == MAX_PACKET_LEN) {
        // printf("debug - dropping large wrt54g packet\n");
        return 0;
    }
    
    num_packets++;

    // Set the name
    snprintf(packet->sourcename, 32, "%s", name.c_str());
    
    // Set the parameters
    memcpy(&packet->parm, &parameters, sizeof(packet_parm));
    
    return(packet->caplen);
}

carrier_type PcapSourceWrt54g::IEEE80211Carrier() {
    int ch = FetchChannel();

    if (ch > 0 && ch <= 14)
        return carrier_80211g;
    else if (ch > 34)
        return carrier_80211a;

    return carrier_unknown;
}
#endif

#if (defined(HAVE_RADIOTAP) && (defined(SYS_NETBSD) || defined(SYS_OPENBSD) || defined(SYS_FREEBSD)))
int PcapSourceRadiotap::OpenSource() {
	// XXX this is a hack to avoid duplicating code
	int s = PcapSource::OpenSource();
	if (s < 0)
		return s;
	if (!CheckForDLT(DLT_IEEE802_11_RADIO)) {
		snprintf(errstr, 1024, "No support for radiotap data link");
		return -1;
	} else {
		(void) pcap_set_datalink(pd, DLT_IEEE802_11_RADIO);
		datalink_type = DLT_IEEE802_11_RADIO;
		return s;
	}
}

// Check for data link type support
bool PcapSourceRadiotap::CheckForDLT(int dlt)
{
    bool found = false;
    int i, n, *dl;
    n = pcap_list_datalinks(pd, &dl);
    for (i = 0; i < n; i++)
	if (dl[i] == dlt) {
	    found = true;
	    break;
	}
    free(dl);
    return found;
}

#endif

// ----------------------------------------------------------------------------
// Registrant and control functions outside of the class

KisPacketSource *pcapsource_registrant(string in_name, string in_device,
                                       char *in_err) {
    return new PcapSource(in_name, in_device);
}

KisPacketSource *pcapsourcefcs_registrant(string in_name, string in_device,
                                       char *in_err) {
	KisPacketSource *nsrc = new PcapSource(in_name, in_device);
	nsrc->fcsbytes = 4;
	return nsrc;
}

KisPacketSource *pcapsource_file_registrant(string in_name, string in_device,
                                            char *in_err) {
	PcapSourceFile *src = new PcapSourceFile(in_name, in_device);

#if 0
	src->SetSmartCRC(1);
	src->fcsbytes = 4;
#endif

	return src;
}

#ifdef HAVE_LINUX_WIRELESS
KisPacketSource *pcapsource_wext_registrant(string in_name, string in_device, 
                                            char *in_err) {
    return new PcapSourceWext(in_name, in_device);
}

KisPacketSource *pcapsource_wextfcs_registrant(string in_name, string in_device,
                                               char *in_err) {
    return new PcapSourceWextFCS(in_name, in_device);
}

KisPacketSource *pcapsource_ciscowifix_registrant(string in_name, string in_device, char *in_err) {
    vector<string> devbits = StrTokenize(in_device, ":");

    if (devbits.size() < 2) {
        snprintf(in_err, STATUS_MAX, "Invalid device pair '%s'", in_device.c_str());
        return NULL;
    }

    return new PcapSourceWext(in_name, devbits[1]);
}

KisPacketSource *pcapsource_11g_registrant(string in_name, string in_device,
                                           char *in_err) {
    return new PcapSource11G(in_name, in_device);
}

KisPacketSource *pcapsource_11gfcs_registrant(string in_name, string in_device,
                                              char *in_err) {
    return new PcapSource11GFCS(in_name, in_device);
}

KisPacketSource *pcapsource_11gfcschk_registrant(string in_name, string in_device,
												 char *in_err) {
	PcapSource11GFCS *src = new PcapSource11GFCS(in_name, in_device);

	src->SetSmartCRC(1);

	return src;
}

#endif

#ifdef SYS_LINUX
KisPacketSource *pcapsource_wlanng_registrant(string in_name, string in_device,
                                              char *in_err) {
    return new PcapSourceWlanng(in_name, in_device);
}

KisPacketSource *pcapsource_wrt54g_registrant(string in_name, string in_device,
                                              char *in_err) {
	return new PcapSourceWrt54g(in_name, in_device);
}
#endif

#ifdef SYS_OPENBSD
KisPacketSource *pcapsource_openbsdprism2_registrant(string in_name, string in_device,
                                                     char *in_err) {
    return new PcapSourceOpenBSDPrism(in_name, in_device);
}
#endif

#if (defined(HAVE_RADIOTAP) && (defined(SYS_NETBSD) || defined(SYS_OPENBSD) || defined(SYS_FREEBSD)))
KisPacketSource *pcapsource_radiotap_registrant(string in_name, string in_device,
                                                     char *in_err) {
    return new PcapSourceRadiotap(in_name, in_device);
}
#endif

int unmonitor_pcapfile(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    return 0;
}

// Monitor commands
#ifdef HAVE_LINUX_WIRELESS
// Cisco uses its own config file in /proc to control modes
int monitor_cisco(const char *in_dev, int initch, char *in_err, 
				  void **in_if, void *in_ext) {
    FILE *cisco_config;
    char cisco_path[128];

    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Get_SSID(in_dev, in_err, ifparm->essid) < 0)
        return -1;

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    if (Ifconfig_Delta_Flags(in_dev, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

    // Try the iwpriv
    if (Iwconfig_Set_IntPriv(in_dev, "setRFMonitor", 1, 0, in_err) >= 0) {
        return 0;
    }

    // Zero the ssid - nonfatal
    Iwconfig_Set_SSID(in_dev, in_err, NULL);
   
    // Build the proc control path
    snprintf(cisco_path, 128, "/proc/driver/aironet/%s/Config", in_dev);

    if ((cisco_config = fopen(cisco_path, "w")) == NULL) {
        snprintf(in_err, STATUS_MAX, "Unable to open cisco control file '%s' %d:%s",
                 cisco_path, errno, strerror(errno));
        return -1;
    }

    fprintf(cisco_config, "Mode: r\n");
    fprintf(cisco_config, "Mode: y\n");
    fprintf(cisco_config, "XmitPower: 1\n");

    fclose(cisco_config);

    // Channel can't be set on cisco with these drivers.

    return 0;
}

int unmonitor_cisco(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);
    int ret = -1;

    // Try the iwpriv
    if (Iwconfig_Set_IntPriv(in_dev, "setRFMonitor", 0, 0, in_err) >= 0) {
        // If we're the new drivers, unmonitor
        if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
            return -1;
        }

        // Reset the SSID since monitor mode nukes it
        if (Iwconfig_Set_SSID(in_dev, in_err, ifparm->essid) < 0)
            return -1;

        if (ifparm->channel > 0) {
            if (Iwconfig_Set_Channel(in_dev, ifparm->channel, in_err) < 0)
                return -1;
        }

        ret = 1;
    }

    free(ifparm);

    return ret;
}

// Cisco uses its own config file in /proc to control modes
//
// I was doing this with ioctls but that seems to cause lockups while
// this method doesn't.  I don't think I like these drivers.
int monitor_cisco_wifix(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    FILE *cisco_config;
    char cisco_path[128];
    vector<string> devbits = StrTokenize(in_dev, ":");

    if (devbits.size() < 2) {
        snprintf(in_err, STATUS_MAX, "Invalid device pair '%s'.  Proper device "
				 "for cisco_wifix is eth?:wifi?.", in_dev);
        return -1;
    }

    // Bring the device up, zero its ip, and set promisc
    if (Ifconfig_Delta_Flags(devbits[0].c_str(), in_err, 
                             IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;
    if (Ifconfig_Delta_Flags(devbits[1].c_str(), in_err, 
                             IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

    // Zero the ssid, nonfatally
    Iwconfig_Set_SSID(devbits[0].c_str(), in_err, NULL);
    Iwconfig_Set_SSID(devbits[1].c_str(), in_err, NULL);
    
    // Build the proc control path
    snprintf(cisco_path, 128, "/proc/driver/aironet/%s/Config", devbits[0].c_str());

    if ((cisco_config = fopen(cisco_path, "w")) == NULL) {
        snprintf(in_err, STATUS_MAX, "Unable to open cisco control file '%s' %d:%s",
                 cisco_path, errno, strerror(errno));
        return -1;
    }

    fprintf(cisco_config, "Mode: r\n");
    fprintf(cisco_config, "Mode: y\n");
    fprintf(cisco_config, "XmitPower: 1\n");

    fclose(cisco_config);

    // Channel can't be set on cisco with these drivers.

    return 0;
}

// Hostap uses iwpriv and iwcontrol settings to control monitor mode
int monitor_hostap(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    int ret;
  
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0)
        return -1;

    // Don't try to fetch the channel or mode if we're not configured to be up,
    // hostap doesn't like this.  silly hostap.
    if ((ifparm->flags & IFF_UP)) {
        if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
            return -1;

        if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
            return -1;
    } else {
        ifparm->channel = -1;
        ifparm->mode = -1;
    }
    
    // Try to use the iwpriv command to set monitor mode.  Some versions of
    // hostap require this, some don't, so don't fail on the monitor ioctl
    // if we can't find it, it might get removed in the future.
    if ((ret = Iwconfig_Set_IntPriv(in_dev, "monitor", 3, 0, in_err)) < 0) {
        if (ret != -2)
            return -1;
    }
   
    // Try to set wext monitor mode.  We're good if one of these succeeds...
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0 && ret < 0)
        return -1;

    // If we didn't set wext mode, set the channel manually
    if (chancontrol_wext(in_dev, initch, in_err, NULL) < 0)
        return -1;

    return 0;
}

int unmonitor_hostap(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {

    // Restore the IP settings
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    if (ifparm->channel > 0) {
        if (Iwconfig_Set_Channel(in_dev, ifparm->channel, in_err) < 0)
            return -1;
    }

    // Ignore errors from both of these, since one might fail with other versions
    // of hostap
    Iwconfig_Set_IntPriv(in_dev, "monitor", 0, 0, in_err);
    if (ifparm->mode > 0) {
        Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode);
    }

    free(ifparm);

    return 1;
}

// Orinoco uses iwpriv and iwcontrol settings to control monitor mode
int monitor_orinoco(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    int ret;
    
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;
    
    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // Bring the device up and set promisc
    if (Ifconfig_Delta_Flags(in_dev, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

    // Socket lowpower cards seem to need a little time for the firmware to settle
    // down between these calls, so we'll just sleep for everyone.  It won't hurt
    // to add a few more ms onto an indefinitely blocking ioctl setup
    usleep(5000);

    // Set monitor mode with iwpriv for orinoco_cs 0.13 with Snax patches
    if ((ret = Iwconfig_Set_IntPriv(in_dev, "monitor", 1, initch, in_err)) < 0) {
        if (ret != -2)
            return -1;
    }

    // Try to turn on my patches to the new orinoco drivers to give us some signal
    // levels if they're available.  We don't care if we fail at this.
    if (ret < 0) {
        usleep(5000);
        Iwconfig_Set_IntPriv(in_dev, "set_prismheader", 2, 0, in_err);
    }
   
    usleep(5000);
    // Try to set wext monitor mode.  We're good if one of these succeeds...
    if (ret < 0 && monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0) {
        snprintf(in_err, 1024, "Could not find 'monitor' private ioctl or use "
                 "the newer style 'mode monitor' command.  This typically means "
                 "that the drivers have not been patched or the "
                 "correct drivers are being loaded. See the troubleshooting "
                 "section of the README for more information.");
        return -1;
    }

    usleep(5000);
    // If we didn't use iwpriv, set the channel directly
    if (ret < 0 && chancontrol_wext(in_dev, initch, in_err, NULL) < 0)
        return -1;
    
    return 0;
}

int unmonitor_orinoco(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Restore the IP settings
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    // Ignore errors from both of these, since one might fail with other versions
    // of orinoco_cs
    Iwconfig_Set_IntPriv(in_dev, "monitor", 0, ifparm->channel, in_err);
    Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode);

    free(ifparm);

    return 1;

}

// Acx100 uses the packhdr iwpriv control to set link state, rest is normal
int monitor_acx100(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    int ret;

    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Get_SSID(in_dev, in_err, ifparm->essid) < 0)
        return -1;

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // it looks like an orinoco now, apparently
    if ((ret = Iwconfig_Set_IntPriv(in_dev, "monitor", 1, initch, in_err)) < 0) {
        if (ret == -2)
            snprintf(in_err, 1024, "Could not find 'monitor' private ioctl "
                     "Make sure you have the latest ACX100 development release.");
        return -1;
    }

    if (chancontrol_wext(in_dev, initch, in_err, NULL) < 0)
        return -1;

    return 0;
}

int unmonitor_acx100(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Restore the IP settings
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    Iwconfig_Set_IntPriv(in_dev, "monitor", 0, ifparm->channel, in_err);
    Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode);

    if (Iwconfig_Set_SSID(in_dev, in_err, ifparm->essid) < 0)
        return -1;
    
    free(ifparm);

    return 1;
}

int monitor_admtek(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    // Try to figure out the name so we know if we have fcs bytes or not
    char iwname[IFNAMSIZ+1];
    if (Iwconfig_Get_Name(in_dev, in_err, iwname) < 0)
        return -1;

    if (strncmp(iwname, "IEEE 802.11b", IFNAMSIZ) == 0) {
        // Looks like the GPL driver, we need to adjust the fcsbytes
        PcapSource *psrc = (PcapSource *) in_ext;
        psrc->fcsbytes = 4;
    }

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if ((ifparm->flags & IFF_UP)) {
        if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
            return -1;

        if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
            return -1;

        if (Iwconfig_Get_SSID(in_dev, in_err, ifparm->essid) < 0)
            return -1;
    
    } else {
        ifparm->channel = -1;
        ifparm->mode = -1;
    }

	// Non-fatal ssid zero
    Iwconfig_Set_SSID(in_dev, in_err, NULL);

    int ret = monitor_wext(in_dev, initch, in_err, in_if, in_ext);

    if (ret < 0 && ret != -2)
        return ret;

    return 0;
}

int unmonitor_admtek(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (unmonitor_wext(in_dev, initch, in_err, in_if, in_ext))
        return -1;

    if (Iwconfig_Set_SSID(in_dev, in_err, ifparm->essid) < 0)
        return -1;
   
    return 1;
}
// vtar5k iwpriv control to set link state, rest is normal
int monitor_vtar5k(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Set the prism iwpriv control to 1
    if (Iwconfig_Set_IntPriv(in_dev, "prism", 1, 0, in_err) < 0) {
        return -1;
    }
    
    // The rest is standard wireless extensions
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0)
        return -1;

    return 0;
}

int monitor_madwifi_ng(const char *in_dev, char *in_err, void **in_if, 
					   void *in_ext) {
	char newdev[IFNAMSIZ];
	vector<string> vaplist;
	int nvaps;
	int warned = 0;
	int existing_rfmon = -1;

	nvaps = madwifing_list_vaps(in_dev, &vaplist);

	for (unsigned int x = 0; x < vaplist.size(); x++) {
		int iwmode;

		if (Iwconfig_Get_Mode(vaplist[x].c_str(), in_err, &iwmode) < 0) {
			fprintf(stderr, "WARNING:  Could not get mode of vap %s::%s, skipping\n",
					in_dev, vaplist[x].c_str());
			continue;
		}

		if (iwmode != LINUX_WLEXT_MONITOR) {
			fprintf(stderr, "WARNING: Kismet found a non-monitor VAP %s "
					"on %s.  If your drivers stop reporting packets "
					"after a short while, try reloading madwifi with "
					"the module parameter 'autocreate=none' as this "
					"often resolves this problem.\n", vaplist[x].c_str(),
					in_dev);
		}

		if (iwmode != LINUX_WLEXT_MONITOR && vap_destroy == 0) {
			fprintf(stderr, "WARNING:  Found a non-monitor VAP %s::%s.  "
					"Madwifi-ng has historically had problems with normal-mode "
					"VAPs combined with monitor-mode VAPs.  Kismet has been "
					"configured to NOT destroy VAPs.  To automatically destroy " 
					"non-rfmon VAPs, set vapdestroy=true in kismet.conf.  Kismet "
					"likely WILL NOT WORK with non-rfmon VAPs running.\n", 
					in_dev, vaplist[x].c_str());
			if (warned == 0) {
				warned = 1;
				sleep(2);
			}
			break;
		} else if (iwmode != LINUX_WLEXT_MONITOR && vap_destroy == 1) {
			fprintf(stderr, "NOTICE:  Found a non-monitor VAP %s::%s.  Because "
					"Kismet was configured to remove non-rfmon vaps "
					"automatically, it will be destroyed.  Once Kismet has "
					"exited, you must manually restore the VAP and associated "
					"network state.  If you wish to NOT remove VAPs "
					"automatically, set vapdestroy=false in kismet.conf.\n", 
					in_dev, vaplist[x].c_str());

			if (warned == 0) {
				warned = 1;
				sleep(2);
			}

			if (madwifing_destroy_vap(vaplist[x].c_str(), in_err) < 0) {
				fprintf(stderr, "FATAL:  Failed to destroy madwifi-ng VAP: %s\n",
						in_err);
				return -1;
				break;
			}
		} else if (iwmode == LINUX_WLEXT_MONITOR) {
			existing_rfmon = x;
		}
	}

	if (existing_rfmon >= 0) {
		fprintf(stderr, "NOTICE:  Found existing monitor mode vap %s::%s, Kismet "
				"will use that instead of creating its own VAP\n",
				in_dev, vaplist[existing_rfmon].c_str());

		((KisPacketSource *) in_ext)->SetInterface(vaplist[existing_rfmon].c_str());

		// Set "something" flag to indicate we don't clean up
		((KisPacketSource *) in_ext)->SetFlag(1);
	} else {
		if (madwifing_build_vap(in_dev, in_err, "kis", newdev, IEEE80211_M_MONITOR,
								IEEE80211_CLONE_BSSID) < 0) {
			fprintf(stderr, "ERROR:  %s\n", in_err);
			fprintf(stderr, "ERROR:  Unable to create monitor-mode VAP\n");
			return -1;
		}

		((KisPacketSource *) in_ext)->SetInterface(newdev);

		fprintf(stderr, "NOTICE:  Created Madwifi-NG RFMON VAP %s\n", newdev);

		FILE *controlf;
		char cpath[256];

		snprintf(cpath, 255, "/proc/sys/net/%s/dev_type", newdev);

		if ((controlf = fopen(cpath, "w")) == NULL) {
			fprintf(stderr, "WARNING:  Could not open /proc/sys/net control "
					"interface to set radiotap mode.  This may indicate a deeper "
					"problem but is not a fatal error.\n");
		} else {
			fprintf(controlf, "803\n");
			fclose(controlf);
		}
	}

	return 1;
}

int unmonitor_madwifi_ng(const char *in_dev, char *in_err, void **in_if, 
						 void *in_ext) {

	if (((KisPacketSource *) in_ext)->FetchFlag()) {
		return 1;
	}

	if (madwifing_destroy_vap(in_dev, in_err) < 0) {
		fprintf(stderr, "WARNING:  Unable to destroy madwifi-ng interface %s during "
				"cleanup: %s\n", in_dev, in_err);
		return -1;
	}

	return 1;
}

// Madwifi stuff uses iwpriv mode
int monitor_madwifi_a(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
	// Try to enable the madwifi-ng mode
	if (monitor_madwifi_ng(in_dev, in_err, in_if, in_ext) < 0) {
		fprintf(stderr, "WARNING: %s appears to not accept the Madwifi-NG controls. "
				"Will attempt to configure it as a standard Madwifi-old interface. "
				"If you are using madwifi-ng, be sure to set the source interface "
				"to the wifiX control interface, NOT athX\n",
				in_dev);

		// Allocate a tracking record for the interface settings and remember our
		// setup
		linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
		(*in_if) = ifparm;

		if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
			return -1;
		}

		if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
			return -1;

		if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
			return -1;

		if (Iwconfig_Get_IntPriv(in_dev, "get_mode", &ifparm->privmode, in_err) < 0)
			return -1;

		if (Iwconfig_Set_IntPriv(in_dev, "mode", 1, 0, in_err) < 0)
			return -1;
	} else {
		fprintf(stderr, "WARNING: %s appears to be using Madwifi-NG.  Some versions "
				"of the Madwifi-NG drivers have problems in monitor mode, especially "
				"if non-monitor VAPs are active.  If you experience problems, be "
				"sure to try the latest versions of Madwifi-NG and remove other "
				"VAPs\n", in_dev);

		in_dev = ((KisPacketSource *) in_ext)->FetchInterface();

		(*in_if) = NULL;

		((KisPacketSource *) in_ext)->fcsbytes = 0;

		sleep(1);
	}

    // The rest is standard wireless extensions
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0) {
		snprintf(in_err, STATUS_MAX, "Unable to enter monitor mode.  This can "
				 "happen if your drivers have been compiled without the proper "
				 "wireless extensions support or if you are running a very old "
				 "version of the drivers or kernels.  Please see the troubleshooting "
				 "section of the README for more information.");
        return -1;
    }

    return 0;
}

int monitor_madwifi_b(const char *in_dev, int initch, char *in_err, 
					  void **in_if, void *in_ext) {
	// Try to enable the madwifi-ng mode
	if (monitor_madwifi_ng(in_dev, in_err, in_if, in_ext) < 0) {
		fprintf(stderr, "WARNING: %s appears to not accept the Madwifi-NG controls. "
				"Will attempt to configure it as a standard Madwifi-old interface. "
				"If you are using madwifi-ng, be sure to set the source interface "
				"to the wifiX control interface, NOT athX\n",
				in_dev);
		// Allocate a tracking record for the interface settings and remember our
		// setup
		linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
		(*in_if) = ifparm;

		if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
			return -1;
		}

		if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
			return -1;

		if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
			return -1;

		if (Iwconfig_Get_IntPriv(in_dev, "get_mode", &ifparm->privmode, in_err) < 0)
			return -1;

		if (Iwconfig_Set_IntPriv(in_dev, "mode", 2, 0, in_err) < 0)
			return -1;
	} else {
		fprintf(stderr, "WARNING: %s appears to be using Madwifi-NG.  Some versions "
				"of the Madwifi-NG drivers have problems in monitor mode, especially "
				"if non-monitor VAPs are active.  If you experience problems, be "
				"sure to try the latest versions of Madwifi-NG and remove other "
				"VAPs\n", in_dev);

		in_dev = ((KisPacketSource *) in_ext)->FetchInterface();

		(*in_if) = NULL;
		
		((KisPacketSource *) in_ext)->fcsbytes = 0;

		sleep(1);
	}

    // The rest is standard wireless extensions
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0)
        return -1;

    return 0;
}

int monitor_madwifi_g(const char *in_dev, int initch, char *in_err, 
					  void **in_if, void *in_ext) {
	// Try to enable the madwifi-ng mode
	if (monitor_madwifi_ng(in_dev, in_err, in_if, in_ext) < 0) {
		fprintf(stderr, "WARNING: %s appears to not accept the Madwifi-NG controls. "
				"Will attempt to configure it as a standard Madwifi-old interface. "
				"If you are using madwifi-ng, be sure to set the source interface "
				"to the wifiX control interface, NOT athX\n",
				in_dev);
		// Allocate a tracking record for the interface settings and remember our
		// setup
		linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
		(*in_if) = ifparm;

		if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
			return -1;
		}

		if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
			return -1;

		if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
			return -1;

		if (Iwconfig_Get_IntPriv(in_dev, "get_mode", &ifparm->privmode, in_err) < 0)
			return -1;

		if (Iwconfig_Set_IntPriv(in_dev, "mode", 3, 0, in_err) < 0)
			return -1;
	} else {
		fprintf(stderr, "WARNING: %s appears to be using Madwifi-NG.  Some versions "
				"of the Madwifi-NG drivers have problems in monitor mode, especially "
				"if non-monitor VAPs are active.  If you experience problems, be "
				"sure to try the latest versions of Madwifi-NG and remove other "
				"VAPs\n", in_dev);

		in_dev = ((KisPacketSource *) in_ext)->FetchInterface();

		(*in_if) = NULL;

		((KisPacketSource *) in_ext)->fcsbytes = 0;

		sleep(1);
	}

    // The rest is standard wireless extensions
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0)
        return -1;

    return 0;
}

int monitor_madwifi_comb(const char *in_dev, int initch, char *in_err, 
						 void **in_if, void *in_ext) {
	// Try to enable the madwifi-ng mode
	if (monitor_madwifi_ng(in_dev, in_err, in_if, in_ext) < 0) {
		fprintf(stderr, "WARNING: %s appears to not accept the Madwifi-NG controls. "
				"Will attempt to configure it as a standard Madwifi-old interface. "
				"If you are using madwifi-ng, be sure to set the source interface "
				"to the wifiX control interface, NOT athX\n",
				in_dev);

		// Allocate a tracking record for the interface settings and remember our
		// setup
		linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
		(*in_if) = ifparm;

		if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
			return -1;
		}

		if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
			return -1;

		if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
			return -1;

		if (Iwconfig_Get_IntPriv(in_dev, "get_mode", &ifparm->privmode, in_err) < 0)
			return -1;

		if (Iwconfig_Set_IntPriv(in_dev, "mode", 0, 0, in_err) < 0)
			return -1;
	} else {
		fprintf(stderr, "WARNING: %s appears to be using Madwifi-NG.  Some versions "
				"of the Madwifi-NG drivers have problems in monitor mode, especially "
				"if non-monitor VAPs are active.  If you experience problems, be "
				"sure to try the latest versions of Madwifi-NG and remove other "
				"VAPs\n", in_dev);

		in_dev = ((KisPacketSource *) in_ext)->FetchInterface();

		(*in_if) = NULL;

		((KisPacketSource *) in_ext)->fcsbytes = 0;

		sleep(1);
	}

    // The rest is standard wireless extensions
    if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0)
        return -1;

    return 0;
}

// Unmonitor madwifi (shared)
int unmonitor_madwifi(const char *in_dev, int initch, char *in_err, 
					  void **in_if, void *in_ext) {
    // Restore the stored mode
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);
	int ret = 0;

	// Try the ng unmonitor
	if ((ret = unmonitor_madwifi_ng(in_dev, in_err, in_if, in_ext)) < 0) {
		if ((*in_if) == NULL) 
			return -1;

		if (Iwconfig_Set_IntPriv(in_dev, "mode", ifparm->privmode, 0, in_err) < 0) {
			return -1;
		}

		// Call the standard unmonitor
		ret = unmonitor_wext(in_dev, initch, in_err, in_if, in_ext);
	}

	return ret;
}

// Call the standard monitor but ignore error codes since channel
// setting won't work.  This is a temp kluge.
int monitor_prism54g(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    // Remember monitor header setting if we can
    if (Iwconfig_Get_IntPriv(in_dev, "get_prismhdr", &ifparm->prismhdr, in_err) >= 0) {
        // Select AVS monitor header
        if (Iwconfig_Set_IntPriv(in_dev, "set_prismhdr", 1, 0, in_err) < 0)
            return -1;
    } else {
        ifparm->prismhdr = -1;
    }

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // Call the normal monitor mode
    return (monitor_wext(in_dev, initch, in_err, in_if, in_ext));

}

int unmonitor_prism54g(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Restore initial monitor header
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (ifparm->prismhdr >= 0)
        Iwconfig_Set_IntPriv(in_dev, "set_prismhdr", ifparm->prismhdr, 0, in_err);

    return unmonitor_wext(in_dev, initch, in_err, in_if, in_ext);
}

int monitor_ipw2100(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // Call the normal monitor mode
    return (monitor_wext(in_dev, initch, in_err, in_if, in_ext));
}

int unmonitor_ipw2100(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Restore initial monitor header
    // linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode) < 0)
        return -1;

    free(ifparm);

    return 1;
}

int monitor_ipw2200(const char *in_dev, int initch, char *in_err, 
					void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // Call the normal monitor mode
    return (monitor_wext(in_dev, initch, in_err, in_if, in_ext));
}

int unmonitor_ipw2200(const char *in_dev, int initch, char *in_err, 
					  void **in_if, void *in_ext) {
    // Restore initial monitor header
    // linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode) < 0)
        return -1;

	// James says this wants to be set to channel 0 for proper scanning operation
	if (Iwconfig_Set_Channel(in_dev, 0, in_err) < 0)
		return -1;

    free(ifparm);

    return 1;
}

// (Unless we learn different) the 3945 in full rfmon acts the same as
// an ipw2200, so we'll use the same control mechanisms
int monitor_ipw3945(const char *in_dev, int initch, char *in_err, 
					void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if ((ifparm->channel = Iwconfig_Get_Channel(in_dev, in_err)) < 0)
        return -1;

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

    // Call the normal monitor mode
    return (monitor_wext(in_dev, initch, in_err, in_if, in_ext));
}

int unmonitor_ipw3945(const char *in_dev, int initch, char *in_err, 
					  void **in_if, void *in_ext) {
    // Restore initial monitor header
    // linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

    if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode) < 0)
        return -1;

	// James says this wants to be set to channel 0 for proper scanning operation
	if (Iwconfig_Set_Channel(in_dev, 0, in_err) < 0)
		return -1;

    free(ifparm);

    return 1;
}

// The 3945 in "parasite" mode (until James names it) is a different
// beast entirely.  It uses a dynamically added tap interface to give us
// realtime rtap formatted frames off the interface, so we need to
// turn it on via sysfs and then push the new rtapX interface into the source
// before the open happens
int monitor_ipwlivetap(const char *in_dev, int initch, char *in_err, 
					   void **in_if, void *in_ext) {
	// We don't try to remember settings because we aren't going to do
	// anything with them, we're leeching off a dynamic interface made
	// just for us.
	char dynif[32];
	FILE *sysf;
	char path[1024];
	int ifflags;

	// Try to get the flags off the master interface
    if (Ifconfig_Get_Flags(in_dev, in_err, &ifflags) < 0) {
        return -1;
    }

	// If the master interface isn't even up, blow up.
	if ((ifflags & IFF_UP) == 0) {
		snprintf(in_err, 1024, "The ipw control interface (%s) is not "
				 "configured as 'up'.  The ipwlivetap mode reports "
				 "traffic from a currently running interface.  For pure "
				 "rfmon monitor mode, use ipwXXXX instead.", in_dev);
		return -1;
	}

	// Use the .../net/foo/device symlink into the .../bus/pci/drivers/
	// ipw3945/foo/ pci bus interface
	snprintf(path, 1024, "/sys/class/net/%s/device/rtap_iface",
			 in_dev);

	// Open it in RO mode first and get the current state.  I'm not sure
	// how well frewind works on a dynamic system file so we'll just
	// close it off and re-open it when we go to set modes, if we need
	// to.
	if ((sysf = fopen(path, "r")) == NULL) {
		snprintf(in_err, 1024, "Failed to open ipw sysfs tap control file, "
				 "check that the version of the ipw drivers you are running "
				 "is recent enough, and that your system has sysfs properly "
				 "set up.");
		return -1;
	}

	if (fgets(dynif, 32, sysf) == NULL) {
	        fclose(sysf);
	        return -1;
	}

	// We're done with the RO 
	fclose(sysf);

	// If it's -1, we aren't turned on and we need to.
	if (strncmp(dynif, "-1", 32) == 0) {
		if ((sysf = fopen(path, "w")) == NULL) {
			snprintf(in_err, 1024, "Failed to open the ipw sysfs tap control "
					 "file for writing (%s).  Check that Kismet has the proper "
					 "privilege levels and that you are running a version of the "
					 "ipw drivers which supports associated rfmon.", strerror(errno));
			return -1;
		}

		fprintf(sysf, "1\n");
		fclose(sysf);

		// Reopen it again for reading for the last time, and get the
		// interface we changed to.  Do some minor error checking to make
		// sure the new interface isn't called -1, 0, or 1, which I'm going
		// to guess would imply an older driver
		if ((sysf = fopen(path, "r")) == NULL) {
			snprintf(in_err, 1024, "Failed to open the ipw sysfs tap "
					 "control to find the interface allocated.  Something strange "
					 "has happened, because the control file was available "
					 "previously for setting.  Check your system messages.");
			return -1;
		}

		if (fgets(dynif, 32, sysf) == NULL) {
		        fclose(sysf);
			return -1;
		}

		fclose(sysf);

		// Wait for the distro to settle if its going to rename an interface
		sleep(1);
	}

	// Sanity check the interface we were told to use.  A 0, 1, -1 probably
	// means a bad driver version.
	if (strncmp(dynif, "-1", 32) == 0 || strncmp(dynif, "0", 32) == 0 ||
		strncmp(dynif, "1", 32) == 0) {
		snprintf(in_err, 1024, "Got a nonsense interface from the ipw "
				 "sysfs tap control file.  This probably means your ipw "
				 "drivers are out of date, or that there is something strange "
				 "happening in the drivers.  Check your system messages.");
		return -1;
	}

	// Now that we've gone through that nonsense, make sure the
	// dynamic rtap interface is up
	if (Ifconfig_Delta_Flags(dynif, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

	// And push the config into the packetsoure
	((KisPacketSource *) in_ext)->SetInterface(dynif);

	return 1;
}

int unmonitor_ipwlivetap(const char *in_dev, int initch, char *in_err, 
						 void **in_if, void *in_ext) {
	// Actually there isn't anything to do here.  Right now, I don't
	// think I care if we leave the parasite rtap interface hanging around.
	// Newcore might do this better, but this isn't newcore.

    return 1;
}

#ifdef HAVE_HILDON
int monitor_nokia(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Allocate a tracking record for the interface settings and remember our
    // setup
    linux_ifparm *ifparm = (linux_ifparm *) malloc(sizeof(linux_ifparm));
    (*in_if) = ifparm;

    if (Ifconfig_Get_Flags(in_dev, in_err, &ifparm->flags) < 0) {
        return -1;
    }

    if (Iwconfig_Get_Mode(in_dev, in_err, &ifparm->mode) < 0)
        return -1;

	// Force us into normal mode
	fprintf(stderr, "INFO - Setting Nokia device to online/normal mode...\n");
	system("/usr/bin/dbus-send --type=signal --system /com/nokia/mce/signal "
		   "com.nokia.mce.signal.sig_device_mode_ind string:normal");
	fprintf(stderr, "INFO - Waiting for normal mode... ");
	fflush(stderr);
	sleep(1);
	fprintf(stderr, "done\n");

	/*
	fprintf(stderr, "INFO - Killing wlancond to get it to leave us alone...\n");
	system("/etc/init.d/wlancond stop");
	fprintf(stderr, "INFO - Waiting for wlancond to stop... ");
	fflush(stderr);
	sleep(1);
	fprintf(stderr, "done\n");

	// Get our power save data, ignore errors
	Iwconfig_Get_Power(in_dev, in_err, &(ifparm->power));

	// Disable power save mode, ignore errors
	Iwconfig_Disable_Power(in_dev, in_err);
	*/

    // Call the normal monitor mode
    return (monitor_wext(in_dev, initch, in_err, in_if, in_ext));
}

int unmonitor_nokia(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Restore initial monitor header
    linux_ifparm *ifparm = (linux_ifparm *) (*in_if);
	/*
	void *power = NULL;

	// Preserve the pointer to power, which won't get freed inside ifparm
	power = ifparm->power;
	*/

	// Bring it out of monitor mode
	if (unmonitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0)
		return -1;

	/*
	// Restore power mode, ignore errors
	if (Iwconfig_Restore_Power(in_dev, in_err, power) < 0) {
		fprintf(stderr, "WARNING: Could not restore power save settings (%s), your "
				"battery life is likely to suffer significantly.\n", in_err);
	}

	free(power);

	// Bring wlancond back 
	system("/etc/init.d/wlancond start");
	*/

	return 1;
}
#endif


// "standard" wireless extension monitor mode
int monitor_wext(const char *in_dev, int initch, char *in_err, 
				 void **in_if, void *in_ext) {
	int mode;
	int fail = 0;
	char fcode[256];

	// Bring the device up, zero its ip, and set promisc
	if (Ifconfig_Delta_Flags(in_dev, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
		return -1;

	// Get mode and see if we're already in monitor, don't try to go in
	// if we are (cisco doesn't like rfmon rfmon)
	if (Iwconfig_Get_Mode(in_dev, in_err, &mode) < 0)
		return -1;

	if (mode != LINUX_WLEXT_MONITOR) {
		// Set it
		if (Iwconfig_Set_Mode(in_dev, in_err, LINUX_WLEXT_MONITOR) < 0) {
			fail = 1;
			snprintf(fcode, 256, "%s", strerror(errno));
		}
	}

	// Try again with the interface down, dscape devices need to be down
	// to change mode
	if (fail) {
		int oldflags;
		fail = 0;
		if (Ifconfig_Get_Flags(in_dev, in_err, &oldflags) < 0)
			return -1;

		if (Ifconfig_Set_Flags(in_dev, in_err, 
							   oldflags & ~(IFF_UP | IFF_RUNNING)) < 0)
			return -1;

		if (Iwconfig_Set_Mode(in_dev, in_err, LINUX_WLEXT_MONITOR) < 0) {
			fail = 1;
			snprintf(fcode, 256, "%s", strerror(errno));
		} else if (Ifconfig_Delta_Flags(in_dev, in_err, 
										IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0) {
			return -1;
		}
	}

	if (fail) {
		snprintf(in_err, STATUS_MAX, "Failed to set monitor mode: %s.  "
				 "This usually means your drivers either do not support "
				 "monitor mode, or use a different mechanism for getting "
				 "to it.  Make sure you have a version of your drivers "
				 "that support monitor mode, and consult the troubleshooting "
				 "section of the README.", fcode);
		return -1;
	}


	// Set the initial channel - if we ever get a pcapsource that needs a hook
	// back into the class, this will have to be rewritten
	if (chancontrol_wext(in_dev, initch, in_err, NULL) < 0) {
		return -2;
	}

	return 0;
}

int unmonitor_wext(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
	if (*in_if != NULL) {
		// Restore the IP settings
		linux_ifparm *ifparm = (linux_ifparm *) (*in_if);

		if (Ifconfig_Set_Flags(in_dev, in_err, ifparm->flags) < 0) {
			return -1;
		}

		if (ifparm->mode >= 0) {
			if (Iwconfig_Set_Mode(in_dev, in_err, ifparm->mode) < 0)
				return -1;
		}

		if (ifparm->channel > 0) {
			if (Iwconfig_Set_Channel(in_dev, ifparm->channel, in_err) < 0)
				return -1;
		}

		free(ifparm);
	}

    return 1;
}

#endif

#ifdef SYS_LINUX
// wlan-ng modern standard
int monitor_wlanng(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // I really didn't want to do this...
    char cmdline[2048];

    // Sanitize the device just to be safe.  The ifconfig should fail if
    // the device is invalid, but why take risks
    for (unsigned int x = 0; x < strlen(in_dev); x++) {
        if (!isalnum(in_dev[x])) {
            snprintf(in_err, STATUS_MAX, "Invalid device '%s'", in_dev);
            return -1;
        }
    }
    
    if (Ifconfig_Delta_Flags(in_dev, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

    // Enable the interface
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_ifstate ifstate=enable >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Turn off WEP
    snprintf(cmdline, 2048, "wlanctl-ng %s dot11req_mibset "
             "mibattribute=dot11PrivacyInvoked=false >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Don't exclude packets
    snprintf(cmdline, 2048, "wlanctl-ng %s dot11req_mibset "
             "mibattribute=dot11ExcludeUnencrypted=false >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Turn on rfmon on the initial channel
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_wlansniff channel=%d "
             "enable=true prismheader=true >/dev/null 2>/dev/null", in_dev, initch);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }
    
    return 0;
}

// wlan-ng avs
int monitor_wlanng_avs(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // I really didn't want to do this...
    char cmdline[2048];

    // Sanitize the device just to be safe.  The ifconfig should fail if
    // the device is invalid, but why take risks
    for (unsigned int x = 0; x < strlen(in_dev); x++) {
        if (!isalnum(in_dev[x])) {
            snprintf(in_err, STATUS_MAX, "Invalid device '%s'", in_dev);
            return -1;
        }
    }

    if (Ifconfig_Delta_Flags(in_dev, in_err, IFF_UP | IFF_RUNNING | IFF_PROMISC) < 0)
        return -1;

    // Enable the interface
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_ifstate ifstate=enable >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Turn off WEP
    snprintf(cmdline, 2048, "wlanctl-ng %s dot11req_mibset "
             "mibattribute=dot11PrivacyInvoked=false >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Don't exclude packets
    snprintf(cmdline, 2048, "wlanctl-ng %s dot11req_mibset "
             "mibattribute=dot11ExcludeUnencrypted=false >/dev/null 2>/dev/null", in_dev);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // Turn on rfmon on the initial channel
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_wlansniff channel=%d prismheader=false "
             "wlanheader=true stripfcs=false keepwepflags=false enable=true >/dev/null 2>/dev/null", in_dev, initch);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }
    
    return 0;
}

int monitor_wrt54g(const char *in_dev, int initch, char *in_err, void **in_if, 
				   void *in_ext) {
	int flags;
	char errstr[STATUS_MAX];
	PcapSourceWrt54g *psrc = (PcapSourceWrt54g *) in_ext;

	if (Iwconfig_Set_IntPriv(in_dev, "monitor", 1, 0, in_err) < 0) {
		if (monitor_wext(in_dev, initch, in_err, in_if, in_ext) < 0) {
			snprintf(in_err, 1024, "Could not find 'monitor' private ioctl or use "
					 "the newer style 'mode monitor' command.  Different firmwares "
					 "have used different methods of entering monitor mode on the "
					 "wrt54, make sure you are running the latest open-source "
					 "firmware for your device.");
			return -1;
		}
	
		fprintf(stderr, "NOTICE: Wrt54, checking for prism0 being made by rfmon...\n");
        if (Ifconfig_Get_Flags("prism0", errstr, &flags) >= 0) {
			// Use throwaway errstr here so we don't contaminate the 
			// return error
			fprintf(stderr, "NOTICE:  Wrt54g detected a prism0 "
					"interface after switching to monitor mode.  Moving the "
					"capture source to use prism0 instead of wl0.\n");
			psrc->SetInterface("prism0");
		}
	} 
	
	// Remember the incoming control interface
	psrc->SetControlInt(in_dev);

	return 1;
}


int unmonitor_wrt54g(const char *in_dev, int initch, char *in_err, void **in_if, 
				   void *in_ext) {
    char cmdline[2048];

	snprintf(cmdline, 2048, "/usr/sbin/iwpriv %s set_monitor 0", in_dev);
	if (RunSysCmd(cmdline) < 0) {
		snprintf(in_err, 1024, "Unable to set mode using 'iwpriv %s set_monitor 0'. "
				 "Some custom firmware images require you to specify the origial "
				 "device and a new dynamic device and use the iwconfig controls. "
				 "see the README for how to configure your capture source.",
				 in_dev);
		return -1;
	}

	return 1;
}

#endif

#ifdef SYS_OPENBSD
int monitor_openbsd_prism2(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    struct wi_req wreq;
    struct ifreq ifr;
    int s, flags;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s == -1) {
        snprintf(in_err, 1024, "Failed to create AF_INET socket: %s",
                 strerror(errno));
        return -1;
    }
    //Make sure our interface is up
    strlcpy(ifr.ifr_name, in_dev, sizeof(ifr.ifr_name));
    if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
        close(s);
        snprintf(in_err, 1024, "Failed to get interface flags: %s",
                 strerror(errno));
        return -1;
    }
    flags = ifr.ifr_flags;
    if ((flags & IFF_UP) == 0) {
        ifr.ifr_flags = (flags | IFF_UP);
        strlcpy(ifr.ifr_name, in_dev, sizeof(ifr.ifr_name));
        if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
            close(s);
            snprintf(in_err, 1024, "Failed to ioctl interface up: %s",
                     strerror(errno));
            return -1;
        }
        usleep(5000); // Allow interface to settle
    }

    // Set our initial channel
    bzero((char *)&wreq, sizeof(wreq));
    wreq.wi_len = WI_MAX_DATALEN;
    wreq.wi_type = WI_DEBUG_CHAN;
    wreq.wi_val[0] = htole16(initch);

    bzero((char *)&ifr, sizeof(ifr));
    strlcpy(ifr.ifr_name, in_dev, sizeof(ifr.ifr_name));
    ifr.ifr_data = (caddr_t)&wreq;

    if (ioctl(s, SIOCSPRISM2DEBUG, &ifr) < 0) {
        close(s);
        snprintf(in_err, 1024, "Channel set ioctl failed: %s",
                 strerror(errno));
        return -1;
    }

    // Enable driver processing of 802.11b frames
    bzero((char *)&wreq, sizeof(wreq));
    wreq.wi_len = WI_MAX_DATALEN;
    wreq.wi_type = WI_RID_PROCFRAME;
    wreq.wi_val[0] = 1;

    if (ioctl(s, SIOCSWAVELAN, &ifr) < 0) {
        close(s);
        snprintf(in_err, 1024, "Driver processing ioctl failed: %s",
                 strerror(errno));
        return -1;
    }

    /*
     * Disable roaming, we don't want the card to probe
     * If this fails, don't consider it fatal.
     */
    bzero((char *)&wreq, sizeof(wreq));
    wreq.wi_len = WI_MAX_DATALEN;
    wreq.wi_type = WI_RID_ROAMING_MODE;
    wreq.wi_val[0] = 3;

    if (ioctl(s, SIOCSWAVELAN, &ifr) < 0) {
        snprintf(in_err, 1024, "Roaming disable ioctl failed: %s",
                 strerror(errno));
    }

    // Enable monitor mode
    bzero((char *)&wreq, sizeof(wreq));
    wreq.wi_len = WI_MAX_DATALEN;
    wreq.wi_type = WI_DEBUG_MONITOR;
    wreq.wi_val[0] = 1;

    if (ioctl(s, SIOCSPRISM2DEBUG, &ifr) < 0) {
        close(s);
        snprintf(in_err, 1024, "Monitor mode ioctl failed: %s",
                 strerror(errno));
        return -1;
    }

    close(s);

    return 0;
}
#endif

// Channel change commands
#ifdef HAVE_LINUX_WIRELESS
// Generic wireless "iwconfig channel x" 
int chancontrol_wext(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    return(Iwconfig_Set_Channel(in_dev, in_ch, in_err));
}

// Use iwpriv to control the channel for orinoco
int chancontrol_orinoco(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    int ret;
    PcapSourceWext *source = (PcapSourceWext *) in_ext;
 
    // Learn how to control our channel
    if (source->modern_chancontrol == -1) {
        if ((ret = Iwconfig_Set_IntPriv(in_dev, "monitor", 1, in_ch, in_err)) == -2) {
	  usleep(5000);
            if (Iwconfig_Set_Channel(in_dev, in_ch, in_err) < 0) {
                snprintf(in_err, 1024, "Could not find 'monitor' private ioctl or use "
                         "the newer style 'channel X' command.  This typically means "
                         "that the drivers have not been patched or the "
                         "correct drivers are being loaded. See the troubleshooting "
                         "section of the README for more information.");
                return -1;
            }
            source->modern_chancontrol = 1;
        } else if (ret >= 0) {
            source->modern_chancontrol = 0;
        } else {
            return ret;
        }
        usleep(5000);
    } 
    
    if (source->modern_chancontrol == 0) {
        // Set the monitor mode iwpriv controls.  Explain more if we fail on monitor.
        if ((ret = Iwconfig_Set_IntPriv(in_dev, "monitor", 1, in_ch, in_err)) < 0) {
            if (ret == -2) 
                snprintf(in_err, 1024, "Could not find 'monitor' private ioctl.  This "
                         "typically means that the drivers have not been patched or the "
                         "patched drivers are being loaded.  See the troubleshooting "
                         "section of the README for more information.");
            return -1;
        }

        // The channel is set by the iwpriv so we're done.
        return 0;
    } 

    // Otherwise use iwconfig to set the channel
    return(Iwconfig_Set_Channel(in_dev, in_ch, in_err));
}

// Madwifi needs to change modes accordinly
int chancontrol_madwifi_ab(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    /*
    if (in_ch > 0 && in_ch <= 14) {
        if (Iwconfig_Set_IntPriv(in_dev, "mode", 2, 0, in_err) < 0)
            return -1;
    } else {
        if (Iwconfig_Set_IntPriv(in_dev, "mode", 1, 0, in_err) < 0)
            return -1;
    }
    */
    return chancontrol_wext(in_dev, in_ch, in_err, in_ext);
}

int chancontrol_madwifi_ag(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
	/* This seems to not be needed 
    if (in_ch > 0 && in_ch <= 14) {
        if (Iwconfig_Set_IntPriv(in_dev, "mode", 3, 0, in_err) < 0)
            return -1;
    } else {
        if (Iwconfig_Set_IntPriv(in_dev, "mode", 1, 0, in_err) < 0)
            return -1;
    }
	*/

    return chancontrol_wext(in_dev, in_ch, in_err, in_ext);
}

int chancontrol_prism54g(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    // Run the wireless extention stuff and scrap the error codes
    chancontrol_wext(in_dev, in_ch, in_err, in_ext);

    return 0;
}

int chancontrol_ipw2100(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    // Introduce a slight delay to let the driver settle, a la orinoco.  I don't
    // like doing this at all since it introduces hiccups into the channel control
    // process, but....

    int ret = 0;

    ret = chancontrol_wext(in_dev, in_ch, in_err, in_ext);
    usleep(5000);

    return ret;
}

int chancontrol_ipw2200(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
	// Lets see if this really needs the channel change delay like the 2100 did
    int ret = 0;

    ret = chancontrol_wext(in_dev, in_ch, in_err, in_ext);
	// Drop a tiny sleep in here to let the channel set settle, otherwise we
	// run the risk of the card freaking out
	usleep(7000);

    return ret;
}

int chancontrol_wrt54g(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
	PcapSourceWrt54g *psrc = (PcapSourceWrt54g *) in_ext;

	if (strcmp(in_dev, psrc->FetchControlInt().c_str()) == 0) {
		return chancontrol_wext(in_dev, in_ch, in_err, in_ext);
	} else {
		// prism0 can't seem to change the channel while up, so we have to down
		// wl0 (presumed) and re-up it after the channel set
		int ret;
		int oldflags;

		if ((ret = chancontrol_wext(psrc->FetchControlInt().c_str(), in_ch, 
									in_err, in_ext)) < 0)
			return ret;

		if (Ifconfig_Get_Flags(psrc->FetchControlInt().c_str(), in_err, &oldflags) < 0)
			return -1;

		if (Ifconfig_Set_Flags(psrc->FetchControlInt().c_str(), in_err, 
							   oldflags & ~(IFF_UP | IFF_RUNNING)) < 0)
			return -1;

		if (Ifconfig_Set_Flags(psrc->FetchControlInt().c_str(), in_err, 
							   oldflags & (IFF_UP | IFF_RUNNING | IFF_PROMISC)) < 0)
			return -1;

	}
}
#endif

#ifdef SYS_LINUX
int chancontrol_wlanng(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    // I really didn't want to do this...
    char cmdline[2048];

    // Turn on rfmon on the initial channel
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_wlansniff channel=%d enable=true "
             "prismheader=true >/dev/null 2>&1", in_dev, in_ch);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    if (in_ext != NULL) {
        PcapSourceWlanng *src = (PcapSourceWlanng *) in_ext;
        src->last_channel = in_ch;
    }
    
    return 0;
}

int chancontrol_wlanng_avs(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    // I really didn't want to do this...
    char cmdline[2048];

    // Turn on rfmon on the initial channel
    snprintf(cmdline, 2048, "wlanctl-ng %s lnxreq_wlansniff channel=%d "
             "prismheader=false wlanheader=true stripfcs=false keepwepflags=false "
             "enable=true >/dev/null 2>&1", in_dev, in_ch);

    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    if (in_ext != NULL) {
        PcapSourceWlanng *src = (PcapSourceWlanng *) in_ext;
        src->last_channel = in_ch;
    }
    
    return 0;
}
#endif

#ifdef SYS_OPENBSD
int chancontrol_openbsd_prism2(const char *in_dev, int in_ch, char *in_err, 
                               void *in_ext) {

	struct wi_req		wreq;
	struct ifreq		ifr;
	int		s;

	bzero((char *)&wreq, sizeof(wreq));
	wreq.wi_len = WI_MAX_DATALEN;
	wreq.wi_type = WI_DEBUG_CHAN;
	wreq.wi_val[0] = htole16(in_ch);

	bzero((char *)&ifr, sizeof(ifr));
	strlcpy(ifr.ifr_name, in_dev, sizeof(ifr.ifr_name));
	ifr.ifr_data = (caddr_t)&wreq;

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s == -1) {
		snprintf(in_err, 1024, "Failed to create AF_INET socket: %s",
                 strerror(errno));
		return -1;
	}

	if (ioctl(s, SIOCSPRISM2DEBUG, &ifr) < 0) {
        close(s);
		snprintf(in_err, 1024, "Channel set ioctl failed: %s", strerror(errno));
		return -1;
	}

	close(s);

	return 0;
}
#endif

#if (defined(HAVE_RADIOTAP) && (defined(SYS_NETBSD) || defined(SYS_OPENBSD) || defined(SYS_FREEBSD)))
RadiotapBSD::RadiotapBSD(const char *name) : ifname(name) {
    s = -1;
}

RadiotapBSD::~RadiotapBSD() {
    if (s >= 0)
        close(s);
}

const char *RadiotapBSD::geterror() const {
	return errstr;
}

void RadiotapBSD::perror(const char *fmt, ...) {
	char *cp;

	snprintf(errstr, sizeof(errstr), "%s: ", ifname.c_str());
	cp = strchr(errstr, '\0');
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(cp, sizeof(errstr) - (cp - errstr), fmt, ap);
	va_end(ap);
	cp = strchr(cp, '\0');
	snprintf(cp, sizeof(errstr) - (cp - errstr), ": %s", strerror(errno));
}

void RadiotapBSD::seterror(const char *fmt, ...) {
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(errstr, sizeof(errstr), fmt, ap);
	va_end(ap);
}

bool RadiotapBSD::checksocket() {
    if (s < 0) {
        s = socket(AF_INET, SOCK_DGRAM, 0);
        if (s < 0) {
            perror("Failed to create AF_INET socket");
            return false;
        }
    }
    return true;
}

bool RadiotapBSD::getmediaopt(int& options, int& mode) {
    struct ifmediareq ifmr;

    if (!checksocket())
        return false;

    memset(&ifmr, 0, sizeof(ifmr));
    strncpy(ifmr.ifm_name, ifname.c_str(), sizeof(ifmr.ifm_name)-1);

    /*
     * We must go through the motions of reading all
     * supported media because we need to know both
     * the current media type and the top-level type.
     */
    if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
        perror("cannot get ifmedia");
        return false;
    }
    options = IFM_OPTIONS(ifmr.ifm_current);
    mode = IFM_MODE(ifmr.ifm_current);
    return true;
}

bool RadiotapBSD::setmediaopt(int options, int mode) {
    struct ifmediareq ifmr;
    struct ifreq ifr;
    int *mwords;

    if (!checksocket())
        return false;

    memset(&ifmr, 0, sizeof(ifmr));
    strncpy(ifmr.ifm_name, ifname.c_str(), sizeof(ifmr.ifm_name)-1);

    /*
     * We must go through the motions of reading all
     * supported media because we need to know both
     * the current media type and the top-level type.
     */
    if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
        perror("cannot get ifmedia");
        return false;
    }
    if (ifmr.ifm_count == 0) {
        seterror("%s: no media types?", ifname.c_str());
        return false;
    }
    mwords = new int[ifmr.ifm_count];
    if (mwords == NULL) {
        seterror("cannot malloc");
        return false;
    }
    ifmr.ifm_ulist = mwords;
    if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
        perror("cannot get ifmedia");
        return false;
    }
    delete mwords;

    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name)-1);
    ifr.ifr_media = (ifmr.ifm_current &~ IFM_OMASK) | options;
    ifr.ifr_media = (ifr.ifr_media &~ IFM_MMASK) | IFM_MAKEMODE(mode);

    if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0) {
        perror("cannot set ifmedia");
        return false;
    }
    return true;
}

#if defined(SYS_OPENBSD) || defined(SYS_NETBSD)

     /* A simple 802.11 ioctl replacement for OpenBSD/NetBSD
        Only used for channel set/get.
        This should be re-written to be *BSD agnostic.  */

bool RadiotapBSD::get80211(int type, int& val, int len, u_int8_t *data) {
    struct ieee80211chanreq channel;

    if (!checksocket())
        return false;
    memset(&channel, 0, sizeof(channel));
    strlcpy(channel.i_name, ifname.c_str(), sizeof(channel.i_name));
    if (ioctl(s, SIOCG80211CHANNEL, (caddr_t)&channel) < 0) {
        perror("SIOCG80211CHANNEL ioctl failed");
        return false;
    }
    val = channel.i_channel;
    return true;
}

bool RadiotapBSD::set80211(int type, int val, int len, u_int8_t *data) {
    struct ieee80211chanreq channel;

    if (!checksocket())
        return false;
    strlcpy(channel.i_name, ifname.c_str(), sizeof(channel.i_name));
    channel.i_channel = (u_int16_t)val;
    if (ioctl(s, SIOCS80211CHANNEL, (caddr_t)&channel) == -1) {
        perror("SIOCS80211CHANNEL ioctl failed");
        return false;
    }
    return true;
}

#elif defined(SYS_FREEBSD) /* FreeBSD has a generic 802.11 ioctl */

bool RadiotapBSD::get80211(int type, int& val, int len, u_int8_t *data) {
    struct ieee80211req ireq;

    if (!checksocket())
        return false;
    memset(&ireq, 0, sizeof(ireq));
    strncpy(ireq.i_name, ifname.c_str(), sizeof(ireq.i_name)-1);
    ireq.i_type = type;
    ireq.i_len = len;
    ireq.i_data = data;
    if (ioctl(s, SIOCG80211, &ireq) < 0) {
        perror("SIOCG80211 ioctl failed");
        return false;
    }
    val = ireq.i_val;
    return true;
}

bool RadiotapBSD::set80211(int type, int val, int len, u_int8_t *data) {
    struct ieee80211req ireq;

    if (!checksocket())
	return false;
    memset(&ireq, 0, sizeof(ireq));
    strncpy(ireq.i_name, ifname.c_str(), sizeof(ireq.i_name)-1);
    ireq.i_type = type;
    ireq.i_val = val;
    ireq.i_len = len;
    ireq.i_data = data;
    return (ioctl(s, SIOCS80211, &ireq) >= 0);
}

#endif

bool RadiotapBSD::getifflags(int& flags) {
    struct ifreq ifr;

    if (!checksocket())
        return false;

    strncpy(ifr.ifr_name, ifname.c_str(), sizeof (ifr.ifr_name));
    ifr.ifr_name[sizeof (ifr.ifr_name)-1] = '\0';
    if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
        perror("SIOCGIFFLAGS ioctl failed");
        return false;
    }
#if defined(SYS_FREEBSD)
    flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
#elif defined(SYS_OPENBSD) || defined(SYS_NETBSD)
    flags = ifr.ifr_flags;
#endif
    return true;
}

bool RadiotapBSD::setifflags(int flags) {
    struct ifreq ifr;

    if (!checksocket())
        return false;

    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, ifname.c_str(), sizeof (ifr.ifr_name));
#if defined(SYS_FREEBSD)
    ifr.ifr_flags = flags & 0xffff;
    ifr.ifr_flagshigh = flags >> 16;
#elif defined(SYS_OPENBSD) || (SYS_NETBSD)
    ifr.ifr_flags = flags;
#endif
    if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
        perror("SIOCSIFFLAGS ioctl failed");
        return false;
    }
    return true;
}

bool RadiotapBSD::monitor_enable(int initch) {
    /*
     * Collect current state.
     */
    (void) getmediaopt(prev_options, prev_mode);
    (void) get80211(IEEE80211_IOC_CHANNEL, prev_chan, 0, NULL);
    (void) getifflags(prev_flags);
    /*
     * Enter monitor mode, set the specified channel,
     * enable promiscuous reception, and force the
     * interface up since otherwise bpf won't work.
     */
    if (!setmediaopt(IFM_IEEE80211_MONITOR, IFM_AUTO))
        return false;
    if (!set80211(IEEE80211_IOC_CHANNEL, initch, 0, NULL)) {
	perror("failed to set channel %u", initch);
	(void) setmediaopt(prev_options, prev_mode);
        return false;
    }
#if defined(SYS_FREEBSD)
    if (!setifflags(prev_flags | IFF_PPROMISC | IFF_UP)) {
#elif defined(SYS_OPENBSD) || defined(SYS_NETBSD)
    if (!setifflags(prev_flags | IFF_PROMISC | IFF_UP)) {
#endif
	(void) set80211(IEEE80211_IOC_CHANNEL, prev_chan, 0, NULL);
	(void) setmediaopt(prev_options, prev_mode);
        return false;
    }
    return true;
}

bool RadiotapBSD::monitor_reset(int initch) {
    (void) setifflags(prev_flags);
    /* NB: reset the current channel before switching modes */
    (void) set80211(IEEE80211_IOC_CHANNEL, prev_chan, 0, NULL);
    (void) setmediaopt(prev_options, prev_mode);
    return true;
}

bool RadiotapBSD::chancontrol(int in_ch) {
    if (!set80211(IEEE80211_IOC_CHANNEL, in_ch, 0, NULL)) {
	perror("failed to set channel %u", in_ch);
	return false;
    } else
	return true;
}

int monitor_bsd(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
	RadiotapBSD *bsd = new RadiotapBSD(in_dev);
	if (!bsd->monitor_enable(initch)) {
		strlcpy(in_err, bsd->geterror(), 1024);
		delete bsd;
		return -1;
	} else {
		*(RadiotapBSD **)in_if = bsd;
#ifdef SYS_OPENBSD
		// Temporary hack around OpenBSD drivers not standardising on whether FCS
		// bytes are appended, nor having any method to indicate their presence. 
		if (strncmp(in_dev, "ath", 3) == 0 || strncmp(in_dev, "ural", 4) == 0) {
			PcapSource *psrc = (PcapSource *) in_ext;
			psrc->fcsbytes = 4;
		}
#endif
		return 0;
	}
}

int unmonitor_bsd(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    RadiotapBSD *bsd = *(RadiotapBSD **)in_if;
    if (!bsd->monitor_reset(initch)) {
        strlcpy(in_err, bsd->geterror(), 1024);
        delete bsd;
        return -1;
    } else {
        delete bsd;
        return 1;
    }
}

int chancontrol_bsd(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    RadiotapBSD bsd(in_dev);
    if (!bsd.chancontrol(in_ch)) {
	strlcpy(in_err, bsd.geterror(), 1024);
	return -1;
    } else {
	return 0;
    }
}
#endif /* HAVE_RADIOTAP */

#ifdef SYS_DARWIN
/* From Macstumber rev-eng darwin headers */
WIErr wlc_ioctl(WirelessContextPtr ctx, int command, int bufsize, 
				void *buffer, int outsize,  void *out) {
	if (!buffer) 
		bufsize = 0;

	int *buf = (int *) malloc(bufsize+8);

	buf[0] = 3;
	buf[1] = command;

	if (bufsize && buffer) {
		memcpy(&buf[2], buffer, bufsize);
	}

	return WirelessPrivate(ctx, buf, bufsize+8, out, outsize);
}

/* Iterate over the IO registry, look for a specific type of card
 * thanks to Kevin Finisterre */
int darwin_cardcheck(char *service) {
	mach_port_t masterPort;
	io_iterator_t iterator;
	io_object_t sdev;
	kern_return_t err;

	if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS) {
		return -1;
	}

	if (IORegistryCreateIterator(masterPort, kIOServicePlane,
								 kIORegistryIterateRecursively, &iterator) == 
		KERN_SUCCESS) {
		while ((sdev = IOIteratorNext(iterator))) {
			if (sdev != MACH_PORT_NULL) {
				io_name_t thisClassName;
				io_name_t name;

				err = IOObjectGetClass(sdev, thisClassName);
				err = IORegistryEntryGetName(sdev, name);

				if (IOObjectConformsTo(sdev, service)) {
					IOObjectRelease(iterator);
					return 0;
				}
			}
		}

		IOObjectRelease(iterator);
	}

	return 1;
}

KisPacketSource *pcapsource_darwin_registrant(string in_name, string in_device, 
											  char *in_err) {
	char devname[16];
	int devnum;
	int mib[2];
	size_t miblen;
	char *kernelversion;
	int kernmaj, kernmin, kerntiny;

	// Get the Darwin kernel version, we use that to determine if we should
	// yell at the user, and if we should transform the array
	mib[0] = CTL_KERN;
	mib[1] = KERN_OSRELEASE;
	sysctl(mib, 2, NULL, &miblen, NULL, 0);
	kernelversion = (char *) malloc(miblen * sizeof(char));
	sysctl(mib, 2, kernelversion, &miblen, NULL, 0);

	if (sscanf(kernelversion, "%d.%d.%d", &kernmaj, &kernmin, &kerntiny) != 3) {
		fprintf(stderr, "WARNING:  Couldn't get Darwin kernel version, behavior "
				"may not be correct but we'll keep trying.\n");
		sleep(1);
	}

	free(kernelversion);

	// If they gave us enX, convert it to wltX, if we're not on Leopard or newer.
	if (strncmp(in_device.c_str(), "en", 2) == 0 && (kernmaj < 9)) {
		if (sscanf(in_device.c_str(), "%16[^0-9]%d", devname, &devnum) != 2) {
			fprintf(stderr, "FATAL:  Looks like 'en' was passed for Darwin device "
					"instead of 'wlt', but could not parse it into en#\n");
			return NULL;
		}

		snprintf(devname, 16, "wlt%d", devnum);
	} else {
		snprintf(devname, 16, "%s", in_device.c_str());
	}

	// Look for card types we understand
	if (darwin_cardcheck("AirPort_Brcm43xx") == 0 ||
			   darwin_cardcheck("AirPortPCI_MM") == 0) {
		fprintf(stderr, "INFO:  %s looks like a Broadcom card running "
				"under Darwin.\n", devname);
	} else if (darwin_cardcheck("AirPort_Athr5424ab") == 0) {
		fprintf(stderr, "INFO:  %s looks like an Atheros card running "
				"under Darwin.\n", devname);
	} else {
		fprintf(stderr, "WARNING:  %s didn't look like a Broadcom OR Atheros card. "
				"We'll treat it like an Atheros card and hope for the best, however "
				"it may not work properly.\n", devname);
	}

#if 0
	if (kernmaj >= 9) {
		fprintf(stderr, "\nWARNING: We're running under Darwin/OSX >= 9.1.0 "
				"(Leopard).  If your libpcap is NOT EXTREMELY CURRENT (as in "
				"CVS from December, 2007 or newer) things will likely NOT WORK. "
				"If you get an error configuring the card, make sure your libpcap "
				"is up to date and understands the DLT enumeration used in "
				"Leopard.\n\n");
	}
#endif

	// Everything we don't understand looks like an atheros in the end
    return pcapsourcefcs_registrant(devname, devname, in_err);
}

int chancontrol_darwin(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
	WirelessContextPtr gWCtxt = NULL;

	if (WirelessAttach(&gWCtxt, 0) != 0) {
		snprintf(in_err, STATUS_MAX, "Darwin WirelessAttach() failed "
				 "for channel set");
		return -1;
	}
	wlc_ioctl(gWCtxt, 52, 0, NULL, 0, NULL); // Disassociate
	wlc_ioctl(gWCtxt, 30, 8, &in_ch, 0, NULL); // Set channel

	WirelessDetach(gWCtxt);

	return 0;
}

int monitor_darwin(const char *in_dev, int initch, char *in_err, 
				   void **in_if, void *in_ext) {
	char devname[16];
	int devnum;

	// Get the enX number of the owner interface to set promisc mode
	if (sscanf(in_dev, "%16[^0-9]%d", devname, &devnum) != 2) {
		fprintf(stderr, "FATAL: Could not parse '%s' into wlt#, malformed interface "
				"name.\n", in_dev);
		return -1;
	}

	// Set the master device up, running, and promisc
	snprintf(devname, 16, "en%d", devnum);

	if (darwin_cardcheck("AirPort_Brcm43xx") == 0 ||
			   darwin_cardcheck("AirPortPCI_MM") == 0) {
		if (darwin_bcom_testmonitor() < 0) {
			fprintf(stderr, "INFO: %s looks like a Broadcom card running under "
					"Darwin and does not appear to have monitor mode enabled in "
					"the kernel.  Kismet will attempt to enable monitor in "
					"5 seconds.\n", devname);
			sleep(5);
			if (darwin_bcom_enablemonitor() < 0) {
				fprintf(stderr, "FATAL: Failed to enable monitor mode in the kernel "
						"for Darwin Broadcom.\n");
				return -1;
			}
		} else {
			fprintf(stderr, "INFO: %s looks like a Broadcom card running under "
					"Darwin and already has monitor mode enabled in the kernel.\n",
					devname);
		}
	}

	// Darwin seems to hiccup sometimes, so do this once and don't care,
	// then fail if it dies a second time
	Ifconfig_Delta_Flags(devname, in_err, (IFF_UP | IFF_PROMISC));

	if (Ifconfig_Delta_Flags(devname, in_err, (IFF_UP | IFF_PROMISC)) < 0) {
		fprintf(stderr, "FATAL:  Failed to set %s interface up and promisc: %s\n",
				devname, in_err);
		return -1;
	}

    return 0;
}

int unmonitor_darwin(const char *in_dev, int initch, char *in_err, 
					 void **in_if, void *in_ext) {
    return 1;
}
#endif

#endif