File: SOURCE

package info (click to toggle)
firewalk 5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: sh: 1,754; ansic: 1,349; makefile: 26
file content (2260 lines) | stat: -rw-r--r-- 81,058 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
/*
 *  $Id: firewalk.h,v 1.5 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  firewalk.h - Interface
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _FIREWALK_H
#define _FIREWALK_H

#include <setjmp.h>
#include <ctype.h>
#include <pcap.h>
#include <dnet.h>
#include <libnet.h>

#define FW_BANNER  "Firewalk (c) 2002 Mike D. Schiffman \
<mike@infonexus.com>\nhttp://www.packetfactory.net/firewalk\n\
for more information.\n"

/* responses for the ramping phase */
#define FW_NO_REPLY                     0x00
#define FW_PACKET_IS_BORING             0x01
#define FW_PACKET_IS_TTL_EX_EN_ROUTE    0x02
#define FW_PACKET_IS_UNREACH_EN_ROUTE   0x03
#define FW_PACKET_IS_TERMINAL_TTL_EX    0x04
#define FW_PACKET_IS_TERMINAL_UNREACH   0x05
#define FW_PACKET_IS_TERMINAL_SYNACK    0x06
#define FW_PACKET_IS_TERMINAL_RST       0x07

/* responses for the scanning phase */
#define FW_PORT_IS_OPEN_SYNACK          0x08
#define FW_PORT_IS_OPEN_RST             0x09
#define FW_PORT_IS_OPEN_UNREACH         0x0a
#define FW_PORT_IS_OPEN_TTL_EX          0x0b

/* misc responses */
#define FW_ABORT_SCAN                   0xfd
#define FW_USER_INTERRUPT               0xfe
#define FW_SERIOUS_ERROR                0xff

/* default libpcap timeout */
#define FW_REPLY_TIMEOUT                0x02   

/* snapshot length */
#define FW_SNAPLEN                      0x60
#define FW_DEFAULT_PORT_LIST           "1-130,139,1025"

/* various minimums and maximums */
#define FW_PORT_MAX                     0xffff
#define FW_PORT_MIN                     0x00
#define FW_PCAP_TIMEOUT_MAX             0x3e8
#define FW_PCAP_TIMEOUT_MIN             0x01
#define FW_IP_HOP_MAX                   0x19
#define FW_IP_HOP_MIN                   0x01
#define FW_XV_MAX                       0x08
#define FW_XV_MIN                       0x01

/* BPF filter strings */
#define FW_BPF_FILTER_UDP  "icmp[0] == 11 or icmp[0] == 3 or udp"
#define FW_BPF_FILTER_TCP  "icmp[0] == 11 or icmp[0] == 3 or tcp[13] ==\
                            0x12 or tcp[13] == 0x4 or tcp[13] == 0x14"

/* checks if an IP packet inside of ICMP error message is ours */
#define FW_IS_OURS(ip, fp)                  \
        (ntohs(ip->ip_id) ==                \
        (*fp)->id && ip->ip_src.s_addr ==   \
        (*fp)->sin.sin_addr.s_addr) !=0

/* firewalk statistics structure */
struct firepack_stats
{
    u_short ports_total;            /* number of ports scanned */
    u_short ports_open;             /* open ports */
    u_short ports_unknown;          /* unknown ports */
    u_long packets_sent;            /* packets sent */
    u_long packets_err;             /* packets errors */
    u_long packets_caught;          /* packets we caught total */
    u_short packets_caught_interesting; /* packets we cared about */
};

/* main monolithic firewalk context structure */
struct firepack
{
    char *device;                   /* interface */
    u_char *packet;                 /* packet captured from the wire */
    pcap_t *p;                      /* libpcap context */
    libnet_t *l;                    /* libnet context */
    libnet_plist_t *plist;          /* linked list of ports */
    u_short ttl;                    /* starting IP TTL */
    u_short sport;                  /* source port */
    u_short dport;                  /* ramping destination port */
    u_short id;                     /* firepack packet ID */
    u_short packet_size;            /* outgoing packet size */
    u_char xv;                      /* expiry vector */
    u_char flags;                   /* internal flags used by the program */
#define FW_RESOLVE      0x01        /* resolve IP addresses */
#define FW_STRICT_RFC   0x02        /* strict RFC 793 compliance */
#define FW_BOUND        0x04        /* bound scan */
#define FW_FINGERPRINT  0x08        /* fingerprint (TCP only) */
    int packet_offset;              /* IP packet offset */
    int protocol;                   /* firewalking protocol to use */
    int pcap_timeout;               /* packet capturing timeout */
    u_long gateway;                 /* gateway to probe */
    u_long metric;                  /* metric host */
    u_long seq;                     /* TCP sequence number used */
    libnet_ptag_t ip;               /* ip ptag */
    libnet_ptag_t udp;              /* udp ptag */
    libnet_ptag_t tcp;              /* tcp ptag */
    libnet_ptag_t icmp;             /* icmp ptag */
    struct sockaddr_in sin;         /* socket address structure */
    struct firepack_stats stats;    /* stats */
#define FW_ERRBUF_SIZE  0x100       /* 256 bytes */
    char errbuf[FW_ERRBUF_SIZE];    /* errors here */
};

/* initializes firewalk context */
int                                 /* 1 on success -1 or failure */
fw_init_context(
    struct firepack **,             /* firewalk context */
    char *
    );

/* initialize firewalk networking primitives */
int                                 /* 1 on success -1 or failure */
fw_init_net(
    struct firepack **,             /* firewalk context */
    char *,                         /* target gateway */
    char *,                         /* metric */
    char *                          /* port list or NULL */
    );

/* ramping/scanning driver */
int
firewalk(
    struct firepack **              /* firewalk context */
    );

/* build initial probe template */
int                                 /* 1 on success -1 or failure */
fw_packet_build_probe(
    struct firepack **              /* firewalk context */
    );

/* build UDP header */
int                                 /* 1 on success -1 or failure */
fw_packet_build_udp(
    struct firepack **              /* firewalk context */
    );

/* build TCP header */
int                                 /* 1 on success -1 or failure */
fw_packet_build_tcp(
    struct firepack **              /* firewalk context */
    );

/* build ICMP header */
int                                 /* 1 on success -1 or failure */
fw_packet_build_icmp(
    struct firepack **              /* firewalk context */
    );

/* capture packet from network */
int                                 /* -1 on failure or packet code */
fw_packet_capture(
    struct firepack **              /* firewalk context */
    );

/* sets libpcap BPF filter */
int                                 /* 1 on success -1 or failure */
fw_set_pcap_filter(
    char *,             /* filter code to install */
    struct firepack **              /* firewalk context */
    );

/* injects packet to network */
int                                 /* 1 on success -1 or failure */
fw_packet_inject(
    struct firepack **              /* firewalk context */
    );

/* updates packet template */
int                                 /* 1 on success -1 or failure */
fw_packet_update_probe(
    struct firepack **,             /* firewalk context */
    u_short                         /* 0 for ramping, cport for scanning */
    );

/* verifies a ramping response */
int                                 /* packet code */
fw_packet_verify_ramp(
    struct firepack **              /* firewalk context */
    );

/* verifies a scanning response */
int                                 /* packet code */
fw_packet_verify_scan(
    struct firepack **              /* firewalk context */
    );

/* writes info to the user */
void
fw_report(
    int,                            /* packet class */
    struct firepack **              /* firewalk context */
    );


/* looks up the ICMP unreachable code of a response */
char *                              /* unreachable code */
fw_get_unreach_code(
    struct firepack **              /* firewalk context */
    );

/* report statistics to the user */
void
fw_report_stats(
    struct firepack **              /* firewalk context */
    );

/* installs a new signal handler for a specified signal */
int                                 /* 1 on success -1 or failure */
catch_sig(
    int,                            /* signal to catch */
    void (*)()                      /* new signal handler */
    );

/* handles SIGINT from user */
void
catch_sigint(
    int                             /* unused */
    );

/* converts a string to an int within the bounds specified */
int
fw_str2int(
    register const char *,          /* value to convert */
    register const char *,          /* canonical definition */
    register int,                   /* minimum */
    register int);                  /* maximum */

/* coverts canonical protocol to integer representation */
int                                 /* -1 on failure or protocol */
fw_prot_select(
    char *                          /* protocol */
    );

/* shutdown firewalk */
void
fw_shutdown(
    struct firepack **              /* firewalk context */
    );

/* dump usage */
void
usage(
    u_char *                        /* argv[0] */
    );

#endif /* _FIREWALK_H */

/* EOF */
/*
 *  $Id: unreachables.h,v 1.2 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  unreachables.h - ICMP unreachable codes
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _FW_UNREACHABLES_H
#define _FW_UNREACHABLES_H

char *unreachables[] =
{
    "ICMP_UNREACH_NET",
    "ICMP_UNREACH_HOST",
    "ICMP_UNREACH_PROTOCOL",
    "ICMP_UNREACH_PORT",
    "ICMP_UNREACH_NEEDFRAG",
    "ICMP_UNREACH_SRCFAIL",
    "ICMP_UNREACH_NET_UNKNOWN",
    "ICMP_UNREACH_HOST_UNKNOWN",
    "ICMP_UNREACH_ISOLATED",
    "ICMP_UNREACH_NET_PROHIB",
    "ICMP_UNREACH_HOST_PROHIB",
    "ICMP_UNREACH_TOSNET",
    "ICMP_UNREACH_TOSHOST",
    "ICMP_UNREACH_FILTER_PROHIB",
    "ICMP_UNREACH_HOST_PRECEDENCE",
    "ICMP_UNREACH_PRECEDENCE_CUTOFF",
    0
};

#endif /* _FW_UNREACHABLES_H */

/* EOF */
/*
 *  $Id: firewalk.c,v 1.2 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  firewalk.c - Scanning driver
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
firewalk(struct firepack **fp)
{
    int done, i, j;
    u_short bport, cport, eport;

    /* inform the user what's what */
    printf("%s-based scan.\n",
            (*fp)->protocol == IPPROTO_TCP ? "TCP" : "UDP");
    printf("Ramping phase source port: %d, destination port: %d\n",
            (*fp)->sport, (*fp)->dport);
    if ((*fp)->flags & FW_STRICT_RFC && (*fp)->protocol == IPPROTO_TCP)
    {
        printf("Using strict RFC adherence.\n");
    }
    printf("Hotfoot through %s using %s as a metric.\n",
            libnet_addr2name4(((*fp)->gateway),
            ((*fp)->flags) & FW_RESOLVE),
            libnet_addr2name4(((*fp)->metric),
            ((*fp)->flags) & FW_RESOLVE));

    /*
     *  PHASE ONE: Firewalk hopcount ramping
     *  A standard Traceroute-style IP expiry scan is initiated towards
     *  the metric, with the intent being to find how many hops away the
     *  target gateway is from the scanning host.  We'll increment the
     *  hopcounter and update packet template each pass through the loop.
     */
    printf("Ramping Phase:\n");
    for (done = 0, i = 0; !done && i < FW_IP_HOP_MAX; i++)
    {
        /* send a series of probes (currently only one) */
        for (j = 0; j < 1; j++)
        {
            fprintf(stderr, "%2d (TTL %2d): ", i + 1, (*fp)->ttl);
            if (fw_packet_inject(fp) == -1)
            {
                /*
                 *  Perhaps this write error was transient.  We'll hope
                 *  for the best.  Inform the user and continue.
                 */
                fprintf(stderr, "fw_packet_inject(): %s\n",
                        (*fp)->errbuf);
                continue;
            }
            switch (fw_packet_capture(fp))
            {
                case FW_PACKET_IS_UNREACH_EN_ROUTE:
                case FW_PACKET_IS_TTL_EX_EN_ROUTE:
                    if ((*fp)->flags & FW_BOUND)
                    {
                        printf("Binding host reached.\n");
                        done = 1;
                    }
                    break;
                case FW_PACKET_IS_TERMINAL_TTL_EX:
                case FW_PACKET_IS_TERMINAL_UNREACH:
                case FW_PACKET_IS_TERMINAL_SYNACK:
                case FW_PACKET_IS_TERMINAL_RST:
                    /* any terminal response will end phase one */
                    done = 1;
                    break;
                case -1:
                case FW_SERIOUS_ERROR:
                    /* err msg set in fw_packet_capture() */
                    return (FW_SERIOUS_ERROR);
                case FW_USER_INTERRUPT:
                    /* user hit ctrl-c */
                    return (FW_USER_INTERRUPT);
            }
        }
        if (!done)
        {
            if (fw_packet_update_probe(fp, 0) == -1)
            {
                /* error msg set in fw_packet_update_probe */
                return (-1);
            }
        }
    }
    if (done && !((*fp)->flags & FW_BOUND))
    {
        /*
         *  If we're "done" but not "bound" then we hit the metric
         *  before we hit the target gateway.  This means the target
         *  gateway is not en route to the metric.  Game's over kids.
         */
        sprintf((*fp)->errbuf, 
                "metric responded before target; must not be en route");
        return (FW_ABORT_SCAN);
    }
    if (!done)
    {
        /* if we fall through down here, we've exceeded our hopcount */
        sprintf((*fp)->errbuf, "hopcount exceeded");
        return (FW_ABORT_SCAN);
    }

    /*
     *  PHASE TWO: Firewalk scanning
     *  A series of probes are sent from to the metric with the bound IP
     *  TTL. If a given probe is accepted through the target gateway's
     *  ACL, we will receive an ICMP TTL expired in transit from the
     *  binding host If we receive no response after the timeout expires,
     *  it is assumed the probe violated the ACL on the target and was
     *  dropped.
     */
    (*fp)->ttl += (*fp)->xv;
    printf("Scan bound at %d hops.\n", (*fp)->ttl);
    printf("Scanning Phase: \n");
    for (done = 0, i = 0; !done; i++)
    {
        if (!libnet_plist_chain_next_pair((*fp)->plist, &bport, &eport))
        {
            /* we've exhausted our portlist and we're done */
            done = 1;
            continue;
        }
        while (!(bport > eport) && bport != 0)
        {
            cport = bport++;
            if (fw_packet_update_probe(fp, cport) == -1)
            {
                /* error msg set in fw_packet_update_probe */
                return (-1);
            }

            /* send a series of probes (currently only one) */
            for (j = 0; j < 1; j++)
            {
                fprintf(stderr, "port %3d: ", cport);
                (*fp)->stats.ports_total++;
                if (fw_packet_inject(fp) == -1)
                {
                    /*
                     *  Perhaps this write error was transient.  We'll
                     *  hope for the best.  Inform the user and continue.
                     */
                    fprintf(stderr, "fw_packet_inject(): %s\n",
                            (*fp)->errbuf);
                    continue;
                }
                /* we don't care what the return value is this time */
                switch(fw_packet_capture(fp))
                {
                    case FW_USER_INTERRUPT:
                        return (FW_USER_INTERRUPT);
                    case -1:
                    case FW_SERIOUS_ERROR:
                        /* err msg set in fw_packet_capture() */
                        return (FW_SERIOUS_ERROR);
                    default:
                        /* empty */
                }
            }
        }
    }
    return (1);
}

/* EOF */
/*
 *  $Id: init.c,v 1.4 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  init.c - Main loop driver initialization
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_init_context(struct firepack **fp, char *errbuf)
{
    *fp = (struct firepack *)malloc(sizeof(struct firepack));
    if (*fp == NULL)
    {
        snprintf(errbuf, FW_ERRBUF_SIZE, "malloc(): %s", strerror(errno));
        return (-1);
    }
    memset(*fp, 0, sizeof(struct firepack));

    /* set defaults here */
    (*fp)->ttl          = 1;       /* initial probe IP TTL */
    (*fp)->sport        = 53;      /* source port (TCP and UDP) */
    (*fp)->dport        = 33434;   /* ala traceroute */
    (*fp)->protocol     = IPPROTO_UDP;
    (*fp)->id           = getpid();
    (*fp)->pcap_timeout = FW_REPLY_TIMEOUT;
    (*fp)->xv           = 1;
    (*fp)->flags        |= FW_RESOLVE;

    /* setup our signal handler to handle a ctrl-c */
    if (catch_sig(SIGINT, catch_sigint) == -1)
    {
        snprintf(errbuf, FW_ERRBUF_SIZE, "catch_sig(): %s", strerror(errno));
        return (-1);
    }

    return (1);
}


int
fw_init_net(struct firepack **fp, char *gw, char *m, char *port_list)
{
#if HAVE_BPF
    int one;
#endif
    char errbuf[PCAP_ERRBUF_SIZE];

    /* get a libnet context */
    (*fp)->l = libnet_init(LIBNET_LINK, (*fp)->device, errbuf);
    if ((*fp)->l == NULL)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "libnet_init(): %s",
                errbuf);
        return (-1);
    }

    /* get our device if the user didn't specify one*/
    if ((*fp)->device == NULL)
    {
        (*fp)->device = libnet_getdevice((*fp)->l);
    }

    /* get the source address of our outgoing interface */
    (*fp)->sin.sin_addr.s_addr = libnet_get_ipaddr4((*fp)->l);

    /* setup the target gateway */
    if (((*fp)->gateway = libnet_name2addr4((*fp)->l, gw, 1)) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                "libnet_name2addr4(): %s (target gateway: %s)",
                libnet_geterror((*fp)->l), gw);
        return (-1);
    }

    /* setup the metric */
    if (((*fp)->metric = libnet_name2addr4((*fp)->l, m, 1)) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                "libnet_name2addr4(): %s (metric: %s)",
                libnet_geterror((*fp)->l), m);
        return (-1);
    }

    /* sanity check */
    if ((*fp)->gateway == (*fp)->metric)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                "target gateway and metric cannot be the same");
        return (-1);
    }

    /* get our port list stuff situated */
    if (libnet_plist_chain_new((*fp)->l, &(*fp)->plist,
        port_list == NULL ? strdup(FW_DEFAULT_PORT_LIST) :
        port_list) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
            "libnet_plist_chain_new(): %s\n", libnet_geterror((*fp)->l));
        return (-1);
    }

    /* get a pcap context */
    (*fp)->p = pcap_open_live((*fp)->device, FW_SNAPLEN, 0, 0, errbuf);
    if (((*fp)->p) == NULL)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "pcap_open_live(): %s",
                errbuf);
        return (-1);
    }

#if HAVE_BPF
    /*
     *  BPF, by default, will buffer packets inside the kernel until
     *  either the timer expires (which we do not use) or when the buffer
     *  fills up.  This is not sufficient for us since we could miss
     *  responses to our probes.  So we set BIOCIMMEDIATE to tell BPF
     *  to return immediately when it gets a packet.  This is pretty much
     *  the same behavior we see with Linux which returns every time it
     *  sees a packet.  This is less than efficient since we're spending
     *  more time interrupting the kernel, but hey, we gotta get our
     *  work done!
     */
    one = 1;
    if (ioctl(pcap_fileno((*fp)->p), BIOCIMMEDIATE, &one) < 0)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                "ioctl(): BIOCIMMEDIATE: %s", strerror(errno));
        return (-1);
    }
#endif

    /* get the datalink size */
    switch (pcap_datalink((*fp)->p))
    {
        case DLT_SLIP:
            (*fp)->packet_offset = 0x10;
            break;
        case DLT_RAW:
            (*fp)->packet_offset = 0x00;
            break;             
        case DLT_PPP:
            (*fp)->packet_offset = 0x04;
            break;
        case DLT_EN10MB:
        default:
            (*fp)->packet_offset = 0x0e;
            break;
    }

    /*
     *  Set pcap filter and determine outgoing packet size.  The filter
     *  will be determined by the scanning protocol: 
     *  UDP scan:
     *  icmp[0] == 11 or icmp[0] == 3 or udp
     *  TCP scan:
     *  icmp[0] == 11 or icmp[0] == 3 or tcp[14] == 0x12 or tcp[14] \
     *  == 0x4 or tcp[14] == 0x14
     */
    switch ((*fp)->protocol)
    {
        case IPPROTO_UDP:
            if (fw_set_pcap_filter(FW_BPF_FILTER_UDP, fp) == -1)
            {
                /* err msg set in fw_set_pcap_filter() */
                return (-1);
            }
            /* IP + UDP */
            (*fp)->packet_size = LIBNET_IPV4_H + LIBNET_UDP_H;
            break;
        case IPPROTO_TCP:
            if (fw_set_pcap_filter(FW_BPF_FILTER_TCP, fp) == -1)
            {
                /* err msg set in fw_set_pcap_filter() */
                return (-1);
            }
            /* IP + TCP */
            (*fp)->packet_size = LIBNET_IPV4_H + LIBNET_TCP_H;

            /* randomize the TCP sequence number */
            libnet_seed_prand((*fp)->l);
            (*fp)->seq = libnet_get_prand(LIBNET_PRu32);
            break;
        default:
            sprintf((*fp)->errbuf,
                    "fw_init_network(): unsupported protocol");
            return (-1);
    }

    /*
     *  Build a probe packet template.  We'll use this packet template
     *  over and over for each write to the network, modifying certain
     *  fields (IP TTL, UDP/TCP ports and of course checksums as we go).
     */
    if (fw_packet_build_probe(fp) == -1)
    {
        /* error msg set in fw_packet_build_probe() */
        return (-1);
    }
    return (1);
}


void
fw_shutdown(struct firepack **fp)
{
    if (*fp)
    {
        if ((*fp)->p)
        {
            pcap_close((*fp)->p);
        }
        if ((*fp)->l)
        {
            libnet_destroy((*fp)->l);
        }
        if ((*fp)->plist)
        {
            libnet_plist_chain_free((*fp)->plist);
        }

        free(*fp);
        *fp = NULL;
    }
}

/* EOF */
/*
 *  $Id: main.c,v 1.5 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  main.c - Main control logic
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"
#include "../version.h"

int
main(int argc, char *argv[])
{
    int c;
    struct firepack *fp;
    char *port_list = NULL;
    char errbuf[FW_ERRBUF_SIZE];

    printf("Firewalk 5.0 [gateway ACL scanner]\n");

    /*
     *  Initialize the main control context.  We keep all of our
     *  program state here and this is used by just about every
     *  function in the program.
     */
    if (fw_init_context(&fp, errbuf) == -1)
    {
        fprintf(stderr, "fw_init_control(): %s\n", errbuf);
        goto done;
    }

    /* process commandline arguments */
    while ((c = getopt(argc, argv, "d:fhi:no:p:rS:s:T:t:vx:")) != EOF)
    {
        switch (c)
        {
            case 'd':
                /* destination port to use during ramping phase */
                fp->dport = fw_str2int(optarg, "ramping destination port",
                        FW_PORT_MIN, FW_PORT_MAX);
                break;                
            case 'f':
                /* stack fingerprint of each host */
                fp->flags |= FW_FINGERPRINT;
                break;
            case 'h':
                /* program help */
                usage(argv[0]);
                break;
            case 'i':
                /* interface */
                fp->device = optarg;
                break;
            case 'n':
                /* do not use names */
                fp->flags &= ~FW_RESOLVE;
                break;
            case 'p':
                /* select firewalk protocol */
                fp->protocol = fw_prot_select(optarg);
                break;
            case 'r':
                /* Strict RFC adherence */
                fp->flags |= FW_STRICT_RFC;
                break;
            case 'S':
                /* scan these ports */
                port_list = optarg;
                break;
            case 's':
                /* source port */
                fp->sport = fw_str2int(optarg, "source port",
                        FW_PORT_MIN, FW_PORT_MAX);
                break;
            case 'T':
                /* time to wait for packets from other end */
                    fp->pcap_timeout = fw_str2int(optarg, "read timer",
                        FW_PCAP_TIMEOUT_MIN, FW_PCAP_TIMEOUT_MAX);
                break;
            case 't':
                /* set initial IP TTL */
                fp->ttl = fw_str2int(optarg, "initial TTL",
                        FW_IP_HOP_MIN, FW_IP_HOP_MAX);
                break;
            case 'v':
                /* version */
                printf(FW_BANNER "version : %s\n", VERSION);
                goto done;
            case 'x':
                /* expire vector */
                fp->xv = fw_str2int(optarg, "expire vector",
                        FW_XV_MIN, FW_XV_MAX);
                break;
            default:
                usage(argv[0]);
        }
    }

    c = argc - optind;
    if (c != 2)
    {
        /*
         *  We should only have two arguments at this point, the target
         *  gateway and the metric.
         */
        usage(argv[0]);
    }
    
    /* initialize the network components */
    if (fw_init_net(&fp, argv[optind], argv[optind + 1], port_list) == -1)
    {
        fprintf(stderr, "fw_init_network(): %s\n", fp->errbuf);
        goto done;
    }
    printf("Firewalk state initialization completed successfully.\n");

    /* execute scan: phase one, and hopefully phase two */
    switch (firewalk(&fp))
    {
        case -1:
        case FW_SERIOUS_ERROR:
            /* grievous error of some sort */
            fprintf(stderr, "firewalk(): %s\n", fp->errbuf);
            break;
        case FW_ABORT_SCAN:
            /* hop count exceeded or metric en route */
            fprintf(stderr, "Scan aborted: %s.\n", fp->errbuf);
            break;
        case FW_USER_INTERRUPT:
            fprintf(stderr, "Scan aborted by user.\n");
            break;
        default:
            printf("\nScan completed successfully.\n");
            break;
    }
done:
    fw_report_stats(&fp);
    fw_shutdown(&fp);
    /* we should probably record proper exit status */
    return (EXIT_SUCCESS);
}


void
usage(u_char *argv0)
{
    fprintf(stderr, "Usage : %s [options] target_gateway metric\n"
        "\t\t   [-d %d - %d] destination port to use (ramping phase)\n"
        "\t\t   [-h] program help\n"
        "\t\t   [-i device] interface\n"
        "\t\t   [-n] do not resolve IP addresses into hostnames\n"
        "\t\t   [-p TCP | UDP] firewalk protocol\n"
        "\t\t   [-r] strict RFC adherence\n"
        "\t\t   [-S x - y, z] port range to scan\n"
        "\t\t   [-s %d - %d] source port\n"
        "\t\t   [-T 1 - 1000] packet read timeout in ms\n"
        "\t\t   [-t 1 - %d] IP time to live\n"
        "\t\t   [-v] program version\n"
        "\t\t   [-x 1 - %d] expire vector\n"
        "\n",   argv0, FW_PORT_MIN, FW_PORT_MAX, FW_PORT_MIN, FW_PORT_MAX,
                FW_IP_HOP_MAX, FW_XV_MAX);
        exit(EXIT_SUCCESS);
}


/* EOF */
/*
 *  $Id: packet_build.c,v 1.2 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  packet_build.c - Packet construction code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_packet_build_probe(struct firepack **fp)
{
    arp_t *a;
    route_t *r;
    struct arp_entry arp;
    struct route_entry route;

    /* first build our transport layer header */
    switch ((*fp)->protocol)
    {
        case IPPROTO_UDP:
            if (fw_packet_build_udp(fp) == -1)
            {
                /* error msg set in fw_packet_build_udp() */
                return (-1);
            }
            break;
        case IPPROTO_TCP:
            if (fw_packet_build_tcp(fp) == -1)
            {
                /* error msg set in fw_packet_build_tcp() */
                return (-1);
            }
            break;
        default:
            sprintf((*fp)->errbuf,
                    "fw_packet_build_probe(): unknown protocol");
            return (-1);
    }

    /* build our IPv4 header */
    (*fp)->ip = libnet_build_ipv4(
            (*fp)->packet_size,                 /* packetlength */
            0,                                  /* IP tos */
            (*fp)->id,                          /* IP id */
            0,                                  /* IP frag bits */
            (*fp)->ttl,                         /* IP time to live */
            (*fp)->protocol,                    /* transport protocol */
            0,                                  /* checksum */
            (*fp)->sin.sin_addr.s_addr,         /* IP source */
            (*fp)->metric,                      /* IP destination */
            NULL,                               /* IP payload */
            0,                                  /* IP payload size */
            (*fp)->l,                           /* libnet context */
            0);                                 /* No saved ptag */

    if ((*fp)->ip == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "libnet_build_ipv4() %s",
                libnet_geterror((*fp)->l));
        return (-1);
    }


    /*
     *  Now we need to get the MAC address of our first hop gateway.
     *  Dnet to the rescue!  We start by doing a route table lookup
     *  to determine the IP address we use to get to the
     *  destination host (the metric).
     */
    r = route_open();
    if (r == NULL)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "route_open()");
        route_close(r);
        return (-1);
    }

    /* convert the metric address to dnet's native addr_t format */
    if (addr_aton(libnet_addr2name4((*fp)->metric, 0),
            &route.route_dst) < 0)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "addr_aton()");
        route_close(r);
        return (-1);
    }
    /* get the route entry telling us how to reach the metric */
    if (route_get(r, &route) < 0)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "route_get()");
        route_close(r);
        return (-1);
    }
    route_close(r);

    a = arp_open();
    if (a == NULL)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "arp_open()");
        return (-1);        
    }
    /* get the MAC of the first hop gateway */
    arp.arp_pa = route.route_gw;
    if (arp_get(a, &arp) < 0)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "route_get()");
        arp_close(a);
        return (-1);
    }
    arp_close(a);

    /* build our ethernet header */
    if (libnet_autobuild_ethernet(
            (u_char *)&arp.arp_ha.addr_eth,
            ETHERTYPE_IP,
            (*fp)->l) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                "libnet_autobuild_ethernet() %s",
                libnet_geterror((*fp)->l));
        arp_close(a);
        return (-1);
    }

    return (1);
}

int
fw_packet_build_udp(struct firepack **fp)
{
    /* build a UDP header */
    (*fp)->udp = libnet_build_udp(
            (*fp)->sport,                       /* source UDP port */
            (*fp)->dport,                       /* dest UDP port */
            (*fp)->packet_size - LIBNET_IPV4_H, /* UDP size */
            0,                                  /* checksum */
            NULL,                               /* IP payload */
            0,                                  /* IP payload size */
            (*fp)->l,                           /* libnet context */
            0);                                 /* No saved ptag */

    if ((*fp)->udp == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "libnet_build_udp() %s",
                libnet_geterror((*fp)->l));
        return (-1);
    }
    return (1);
}

int
fw_packet_build_tcp(struct firepack **fp)
{
    /* build a TCP header */
    (*fp)->tcp = libnet_build_tcp(
            (*fp)->sport,                       /* source TCP port */
            (*fp)->dport,                       /* dest TCP port */
            (*fp)->seq,                         /* sequence number */
            0L,                                 /* ACK number */
            TH_SYN,                             /* control flags */
            1024,                               /* window size */
            0,                                  /* checksum */
            0,                                  /* urgent */
            (*fp)->packet_size - LIBNET_IPV4_H, /* TCP size */
            NULL,                               /* IP payload */
            0,                                  /* IP payload size */
            (*fp)->l,                           /* libnet context */
            0);                                 /* No saved ptag */

    if ((*fp)->tcp == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "libnet_build_tcp() %s",
                libnet_geterror((*fp)->l));
        return (-1);
    }
    return (1);
}


/* EOF */
/*
 *  $Id: packet_capture.c,v 1.4 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  packet_capture.c - Packet capturing routines
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int loop = 1;

int
fw_packet_capture(struct firepack **fp)
{
    int pcap_fd, c, timed_out;
    fd_set read_set;
    struct timeval timeout;
    struct pcap_pkthdr pc_hdr;

    timeout.tv_sec = (*fp)->pcap_timeout;
    timeout.tv_usec = 0;

    pcap_fd = pcap_fileno((*fp)->p);
    FD_ZERO(&read_set);
    FD_SET(pcap_fd, &read_set);

    for (timed_out = 0; !timed_out && loop; )
    {
        c = select(pcap_fd + 1, &read_set, 0, 0, &timeout);
        switch (c)
        {
            case -1:
                snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                        "select() %s", strerror(errno));
                return (-1);
            case 0:
                timed_out = 1;
                continue;
            default:
                if (FD_ISSET(pcap_fd, &read_set) == 0)
                {
                    timed_out = 1;
                    continue;
                }
                /* fall through to read the packet */
        }
        (*fp)->packet = (u_char *)pcap_next((*fp)->p, &pc_hdr);
        if ((*fp)->packet == NULL)
        {
            /* no NULL packets please */
            continue;
        }
        (*fp)->stats.packets_caught++;

        /*
         *  Submit the packet for verification first based on scan type,
         *  If we're not bound, we're still in phase one and need to
         *  verify the ramping response.  If we are bound, we're in
         *  phase two and we need to verify the terminal response.
         *  Then process the response from the verification engine.
         *  Report to the user if necessary and update the packet
         *  statistics.
         */
        switch (!(((*fp)->flags) & FW_BOUND) ? fw_packet_verify_ramp(fp) :
                fw_packet_verify_scan(fp))
        {
            case FW_PACKET_IS_TTL_EX_EN_ROUTE:
                /* RAMPING: TTL expired en route to gateway (standard) */
                fw_report(FW_PACKET_IS_TTL_EX_EN_ROUTE, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TTL_EX_EN_ROUTE);
            case FW_PACKET_IS_UNREACH_EN_ROUTE:
                /* RAMPING: Unreachable en route to gateway (uncommon) */
                fw_report(FW_PACKET_IS_UNREACH_EN_ROUTE, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TTL_EX_EN_ROUTE);
            case FW_PACKET_IS_TERMINAL_TTL_EX:
                /* RAMPING: TTL expired at destination (rare) */
                fw_report(FW_PACKET_IS_TERMINAL_TTL_EX, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TERMINAL_TTL_EX);
            case FW_PACKET_IS_TERMINAL_UNREACH:
                /* RAMPING: Unreachable at destination (uncommon) */
                fw_report(FW_PACKET_IS_TERMINAL_UNREACH, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TERMINAL_UNREACH);
            case FW_PACKET_IS_TERMINAL_SYNACK:
                fw_report(FW_PACKET_IS_TERMINAL_SYNACK, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TERMINAL_SYNACK);
            case FW_PACKET_IS_TERMINAL_RST:
                fw_report(FW_PACKET_IS_TERMINAL_RST, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PACKET_IS_TERMINAL_RST);
            case FW_PORT_IS_OPEN_SYNACK:
                /* SCANNING: A response from an open TCP port */
                fw_report(FW_PORT_IS_OPEN_SYNACK, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PORT_IS_OPEN_SYNACK);
            case FW_PORT_IS_OPEN_RST:
                /* SCANNING: A response from a closed TCP port */
                fw_report(FW_PORT_IS_OPEN_RST, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PORT_IS_OPEN_RST);
            case FW_PORT_IS_OPEN_UNREACH:
                /* SCANNING: A port unreachable response */
                fw_report(FW_PORT_IS_OPEN_UNREACH, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PORT_IS_OPEN_UNREACH);
            case FW_PORT_IS_OPEN_TTL_EX:
                /* SCANNING: A TTL expired */
                fw_report(FW_PORT_IS_OPEN_TTL_EX, fp);
                (*fp)->stats.packets_caught_interesting++;
                return (FW_PORT_IS_OPEN_TTL_EX);
            case FW_PACKET_IS_BORING:
            default:
                continue;
        }
    }
    if (!loop)
    {
        return (FW_USER_INTERRUPT);
    }
    /*
     *  If we get here, the scan timed out.  We either dropped a packet
     *  somewhere or there is some filtering going on.
     */
    printf("*no response*\n");
    fflush(stdout);
    return (FW_NO_REPLY);
}

/* EOF */
/*
 *  $Id: packet_filter.c,v 1.2 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  packet_filter.c - Packet filtering code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_set_pcap_filter(char *filter, struct firepack **fp)
{
    struct bpf_program filter_code;
    bpf_u_int32 local_net, netmask;
    char errbuf[PCAP_ERRBUF_SIZE];

    /* get the subnet mask of the interface */
    if (pcap_lookupnet((*fp)->device, &local_net, &netmask, errbuf) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "pcap_lookupnet(): %s",
                    errbuf);
        return (-1);
    } 

    /* compile the BPF filter code */
    if (pcap_compile((*fp)->p, &filter_code, filter, 1, netmask) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "pcap_compile(): %s",
                pcap_geterr((*fp)->p));
        return (-1);
    }

    /* apply the filter to the interface */
    if (pcap_setfilter((*fp)->p, &filter_code) == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "pcap_setfilter(): %s",
                pcap_geterr((*fp)->p));
        return (-1);
    }
    return (1);
}

/* EOF */
/*
 *  $Id: packet_inject.c,v 1.2 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  packet_inject.c - Packet injection code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_packet_inject(struct firepack **fp)
{
    int n;

    n = libnet_write((*fp)->l);
    switch (n)
    {
        case -1:
            (*fp)->stats.packets_err++;
            snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                    "libnet_write() %s", libnet_geterror((*fp)->l));
            return (-1);
        default:
            (*fp)->stats.packets_sent++;
            return (1);
    }
}

/* EOF */
/*
 *  $Id: packet_update.c,v 1.2 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  packet_update.c - Packet updating code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_packet_update_probe(struct firepack **fp, u_short cport)
{
    if (!((*fp)->flags & FW_BOUND))
    {
        /* phase one: just update IP TTL */
        (*fp)->ttl++;
    }
    else
    {
        /* phase two; update port scanning probe */
        switch ((*fp)->protocol)
        {
            case IPPROTO_TCP:
                (*fp)->dport = cport;

                (*fp)->tcp = libnet_build_tcp(
                    (*fp)->sport,                       /* source TCP port */
                    (*fp)->dport,                       /* dest TCP port */
                    (*fp)->seq,                         /* sequence number */
                    0L,                                 /* ACK number */
                    TH_SYN,                             /* control flags */
                    1024,                               /* window size */
                    0,                                  /* checksum */
                    0,                                  /* urgent */
                    (*fp)->packet_size - LIBNET_IPV4_H, /* packet size */
                    NULL,                               /* payload */
                    0,                                  /* payload size */
                    (*fp)->l,                           /* libnet context */
                    (*fp)->tcp);                        /* TCP ptag */
     
                if ((*fp)->tcp == -1)
                {       
                    snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                        "libnet_build_tcp() %s",
                        libnet_geterror((*fp)->l));
                    return (-1);
                }
                break;
            case IPPROTO_UDP:
                (*fp)->dport = cport;

                (*fp)->udp = libnet_build_udp(
                    (*fp)->sport,                       /* source UDP port */
                    (*fp)->dport,                       /* dest UDP port */
                    (*fp)->packet_size - LIBNET_IPV4_H, /* size */
                    0,                                  /* checksum */   
                    NULL,                               /* payload */
                    0,                                  /* payload size */
                    (*fp)->l,                           /* libnet context */
                    (*fp)->udp);                        /* udp ptag */
     
                if ((*fp)->udp == -1)
                {       
                    snprintf((*fp)->errbuf, FW_ERRBUF_SIZE,
                        "libnet_build_udp() %s",
                        libnet_geterror((*fp)->l));
                    return (-1);
                }
                break;
        }
    }

    (*fp)->ip = libnet_build_ipv4(
            (*fp)->packet_size,                 /* packetlength */
            0,                                  /* IP tos */
            (*fp)->id,                          /* IP id */
            0,                                  /* IP frag bits */
            (*fp)->ttl,                         /* IP time to live */
            (*fp)->protocol,                    /* transport protocol */
            0,                                  /* checksum */
            (*fp)->sin.sin_addr.s_addr,         /* IP source */
            (*fp)->metric,                      /* IP destination */
            NULL,                               /* IP payload */
            0,                                  /* IP payload size */
            (*fp)->l,                           /* libnet context */
            (*fp)->ip);                         /* ip ptag */
 
    if ((*fp)->ip == -1)
    {
        snprintf((*fp)->errbuf, FW_ERRBUF_SIZE, "libnet_build_ipv4() %s",
                libnet_geterror((*fp)->l));
        return (-1);
    }

    return (1);
}

/* EOF */
/*
 *  $Id: packet_verify.c,v 1.3 2002/05/14 20:20:39 route Exp $
 *
 *  Firewalk 5.0
 *  packet_verify.c - Packet verification code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

int
fw_packet_verify_ramp(struct firepack **fp)
{
    struct libnet_ipv4_hdr *ip_hdr;
    struct libnet_icmpv4_hdr *icmp_hdr;
    struct libnet_ipv4_hdr *o_ip_hdr;
    struct libnet_tcp_hdr *tcp_hdr;

    /* point to the IP header */
    ip_hdr = (struct libnet_ipv4_hdr *)
            ((*fp)->packet + (*fp)->packet_offset);

    if (ip_hdr->ip_src.s_addr == (*fp)->sin.sin_addr.s_addr)
    {
        /* packets we send are of no interest to us here. */
        return (FW_PACKET_IS_BORING);
    }

    switch (ip_hdr->ip_p)
    {
        case IPPROTO_ICMP:
            icmp_hdr = (struct libnet_icmpv4_hdr *)
                ((*fp)->packet + (*fp)->packet_offset + LIBNET_IPV4_H);

            switch (icmp_hdr->icmp_type)
            {
                case ICMP_TIMXCEED:
                    if (icmp_hdr->icmp_code != ICMP_TIMXCEED_INTRANS)
                    {
                        /*
                         *  Packet was from an expired IP frag queue
                         *  reassembly timer.  Nothing we want.
                         */
                        break;
                    }
                case ICMP_UNREACH:
                    /*
                     *  Point to the original IPv4 header inside the ICMP
                     *  message's payload.  An IPv4 header is
                     *  LIBNET_IPV4_H bytes long and both ICMP unreachable
                     *  and time exeeed headers are 8 bytes.
                     */
                    o_ip_hdr = (struct libnet_ipv4_hdr *)
                            ((*fp)->packet + (*fp)->packet_offset
                            + LIBNET_IPV4_H + 8);

                    /*
                     *  Check the IP header of the packet that caused the
                     *  unreachable for our markings which include:
                     *  Original IP ID: set to the process id.
                     *  Original IP source address: our source address.
                     */
                    if (!FW_IS_OURS(o_ip_hdr, fp))
                    {
                        break;
                    }
                    if (ip_hdr->ip_src.s_addr == (*fp)->metric)
                    {
                        /*
                         *  ICMP response from our metric.  This ends
                         *  our scan since we've reached the metric
                         *  before the target gateway.
                         */
                        return ((icmp_hdr->icmp_type == ICMP_TIMXCEED) ?
                                FW_PACKET_IS_TERMINAL_TTL_EX :
                                FW_PACKET_IS_TERMINAL_UNREACH);
                    }
                    if (ip_hdr->ip_src.s_addr == (*fp)->gateway)
                    {
                        /*
                         *  Response from our target gateway.
                         */
                        (*fp)->flags |= FW_BOUND;
                    }
                    /*
                     *  If we get to this point, the packet is an
                     *  ICMP response from an intermediate router.
                     */
                    return ((icmp_hdr->icmp_type == ICMP_TIMXCEED) ?
                            FW_PACKET_IS_TTL_EX_EN_ROUTE :
                            FW_PACKET_IS_UNREACH_EN_ROUTE);
                    break;
                default:
                    break;
            }
        case IPPROTO_TCP:
            if ((*fp)->protocol != IPPROTO_TCP)
            {
                /*
                 *  We're only interested in TCP packets if this is a
                 *  TCP-based scan.
                 */
                break;
            }

            tcp_hdr = (struct libnet_tcp_hdr *)
                    ((*fp)->packet +
                    (*fp)->packet_offset + LIBNET_IPV4_H);

            if (!(tcp_hdr->th_flags & TH_SYN) &&
                !(tcp_hdr->th_flags & TH_RST))
            {
                /*
                 *  We only care about SYN|ACK and RST|ACK packets.
                 *  The rest can burn.
                 */
                break;
            }

            if ((*fp)->flags & FW_STRICT_RFC)
            {
                /*
                 *  Strict RFC compliance dictates that an RST or
                 *  an SYN|ACK will have our SEQ + 1 as the ACK number
                 *  also, the RST will have the ACK bit set).  This is of 
                 *  course, assuming the packet is ours.
                 */
                if (ntohl(tcp_hdr->th_ack) != (*fp)->seq + 1)
                {
                    break;
                }
            }

            if (ntohs(tcp_hdr->th_dport) == (*fp)->sport &&
                    ntohs(tcp_hdr->th_sport) == (*fp)->dport)
            {
                /* this is most likely a response to our SYN probe */
                return (((tcp_hdr->th_flags & TH_SYN) ?
                        FW_PACKET_IS_TERMINAL_SYNACK :
                        FW_PACKET_IS_TERMINAL_RST));
            }
            break;
    }
    return (FW_PACKET_IS_BORING);
}

int
fw_packet_verify_scan(struct firepack **fp)
{
    struct libnet_ipv4_hdr *ip_hdr;
    struct libnet_icmpv4_hdr *icmp_hdr;
    struct libnet_ipv4_hdr *o_ip_hdr;
    struct libnet_tcp_hdr *tcp_hdr;

    ip_hdr = (struct libnet_ipv4_hdr *)((*fp)->packet +
            (*fp)->packet_offset);

    if (ip_hdr->ip_src.s_addr == (*fp)->sin.sin_addr.s_addr)
    {
        /* packets we send are of no interest to us here. */
        return (FW_PACKET_IS_BORING);
    }
    switch (ip_hdr->ip_p)
    {
        case IPPROTO_ICMP:
            icmp_hdr = (struct libnet_icmpv4_hdr *)
                    ((*fp)->packet + (*fp)->packet_offset + LIBNET_IPV4_H);

            switch (icmp_hdr->icmp_type)
            {
                case ICMP_TIMXCEED:
                    if (icmp_hdr->icmp_code != ICMP_TIMXCEED_INTRANS)
                    {
                        /*
                         *  Packet was from an expired IP frag queue
                         *  reassembly timer.  Nothing we want.
                         */
                        break;
                    }
                case ICMP_UNREACH:
                    /*
                     *  Point to the original IPv4 header inside the ICMP
                     *  message's payload.  An IPv4 header is
                     *  LIBNET_IPV4_H bytes long and both ICMP unreachable
                     *  and time exeeed headers are 8 bytes.
                     */
                    o_ip_hdr = (struct libnet_ipv4_hdr *)
                            ((*fp)->packet + (*fp)->packet_offset
                            + LIBNET_IPV4_H + 8);

                    /*
                     *  Check the IP header of the packet that caused the
                     *  unreachable for our markings which include:
                     *  Original IP ID: set to the process id.
                     *  Original IP source address: our source address.
                     */
                    if (FW_IS_OURS(o_ip_hdr, fp))
                    {
                        /* the packet made it through the filter */
                        return ((icmp_hdr->icmp_type == ICMP_TIMXCEED) ?
                                FW_PORT_IS_OPEN_TTL_EX :
                                FW_PORT_IS_OPEN_UNREACH);
                    }
                    break;
                default:
                    break;
        }
        case IPPROTO_TCP:
            if ((*fp)->protocol != IPPROTO_TCP)
            {
                /*
                 *  We're only interested in TCP packets if this is a
                 *  TCP-based scan.
                 */
                break;
            }
            tcp_hdr = (struct libnet_tcp_hdr *)
                    ((*fp)->packet +
                    (*fp)->packet_offset + LIBNET_IPV4_H);

            /*
             *  We only care about SYN|ACK and RST|ACK packets.
             *  The rest can burn.
             */
            if (!(tcp_hdr->th_flags & TH_SYN) &&
                !(tcp_hdr->th_flags & TH_RST))
            {
                break;
            }

            if ((*fp)->flags & FW_STRICT_RFC)
            {
                /*
                 *  Strict RFC compliance dictates that an RST or
                 *  an SYN|ACK will have our SEQ + 1 as the ACK number
                 *  also, the RST will have the ACK bit set).  This is of 
                 *  course, assuming the packet is ours.
                 */
                if (ntohl(tcp_hdr->th_ack) != (*fp)->seq + 1)
                {
                    break;
                }
            }

            if (ntohs(tcp_hdr->th_dport) == (*fp)->sport &&
                    ntohs(tcp_hdr->th_sport) == (*fp)->dport)
            {
                /* the packet made it through the filter */
                return (((tcp_hdr->th_flags & TH_SYN) ?
                        FW_PORT_IS_OPEN_SYNACK :
                        FW_PORT_IS_OPEN_RST));
            }
            break;
        default:
            break;
    }
    return (FW_PACKET_IS_BORING);
}

/* EOF */
/*
 *  $Id: report.c,v 1.3 2002/05/14 23:28:37 route Exp $
 *
 *  Firewalk 5.0
 *  report.c - Reporting code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"
#include "../include/unreachables.h"

void
fw_report(int class, struct firepack **fp)
{
    struct libnet_ipv4_hdr *ip_hdr;

    ip_hdr = (struct libnet_ipv4_hdr *)((*fp)->packet + (*fp)->packet_offset);

    if (((*fp)->flags & FW_BOUND) && ip_hdr->ip_src.s_addr == (*fp)->metric)
    {
        /* adjacent target gateway and metric */
        printf("A! ");
    }
    switch (class)
    {
        case FW_PACKET_IS_TTL_EX_EN_ROUTE:
            printf("expired [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr, 
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PACKET_IS_UNREACH_EN_ROUTE:
            printf("unreach %s [%s]\n",
                    fw_get_unreach_code(fp),
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PACKET_IS_TERMINAL_TTL_EX:
            printf("terimnal (expired) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PACKET_IS_TERMINAL_UNREACH:
            printf("terminal (unreach %s) [%s]\n",
                    fw_get_unreach_code(fp),
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PACKET_IS_TERMINAL_SYNACK:
            printf("terminal (synack) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PACKET_IS_TERMINAL_RST:
            printf("terminal (rst) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            break;
        case FW_PORT_IS_OPEN_SYNACK:
            printf("open (port listen) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            (*fp)->stats.ports_open++;
            break;
        case FW_PORT_IS_OPEN_RST:
            printf("open (port not listen) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            (*fp)->stats.ports_open++;
            break;
        case FW_PORT_IS_OPEN_UNREACH:
            printf("unknown (unreach %s) [%s]\n",
                    fw_get_unreach_code(fp),
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            (*fp)->stats.ports_unknown++;
            break;
        case FW_PORT_IS_OPEN_TTL_EX:
            printf("open (expired) [%s]\n",
                    libnet_addr2name4(ip_hdr->ip_src.s_addr,
                    ((*fp)->flags) & FW_RESOLVE));
            (*fp)->stats.ports_open++;
            break;
        default:
            break;
    }
}


void
fw_report_stats(struct firepack **fp)
{
    printf("\nTotal packets sent:                %ld\n"
             "Total packet errors:               %ld\n"
             "Total packets caught               %ld\n"
             "Total packets caught of interest   %d\n"
             "Total ports scanned                %d\n"
             "Total ports open:                  %d\n"
             "Total ports unknown:               %d\n",
        (*fp)->stats.packets_sent, (*fp)->stats.packets_err,
        (*fp)->stats.packets_caught, (*fp)->stats.packets_caught_interesting,
        (*fp)->stats.ports_total, (*fp)->stats.ports_open,
        (*fp)->stats.ports_unknown);

}


char *
fw_get_unreach_code(struct firepack **fp)
{
    struct libnet_icmpv4_hdr *icmp_hdr;

    icmp_hdr = (struct libnet_icmpv4_hdr *)
            ((*fp)->packet + (*fp)->packet_offset + LIBNET_IPV4_H);
    if (icmp_hdr->icmp_code > 15)
    {
        return ("Unknown unreachable code");
    }
    return (unreachables[icmp_hdr->icmp_code]);
}

/* EOF */
/*
 *  $Id: signal.c,v 1.3 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  signal.c - Signal handling code
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include "../include/firewalk.h"

extern int loop;

int
catch_sig(int signo, void (*handler)())
{
    struct sigaction action;

    /* install the new handler */
    action.sa_handler = handler;

    /* unblock all other signals */
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    if (sigaction(signo, &action, NULL) == -1)
    {
        return (-1);
    }
    else
    {
        return (1);
    }
}


void
catch_sigint(int nil)
{
    loop = 0;
}

/* EOF */
/*
 *  $Id: util.c,v 1.2 2002/05/14 00:17:52 route Exp $
 *
 *  Firewalk 5.0
 *  util.c - Misc routines
 *
 *  Copyright (c) 1998 - 2002 Mike D. Schiffman  <mike@infonexus.com>
 *  Copyright (c) 1998, 1999 David E. Goldsmith <dave@infonexus.com>
 *  http://www.packetfactory.net/firewalk
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if (HAVE_CONFIG_H)
#include "../include/config.h"
#endif
#include <stdarg.h>
#include "../include/firewalk.h"

int
fw_str2int(register const char *str, register const char *what,
    register int min, register int max)
{
    register const char *cp;
    register int val;
    char *ep;

    if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
    {
        cp = str + 2;
        val = (int)strtol(cp, &ep, 16);
    }
    else
    {
        val = (int)strtol(str, &ep, 10);
    }

    if (*ep != '\0')
    {
        fprintf(stderr, "\"%s\" bad value for %s \n", str, what);
        exit(EXIT_FAILURE);
    }
    if (val < min && min >= 0)
    {
        if (min == 0)
        {
            fprintf(stderr, "%s must be >= %d\n", what, min);
            return (-1);
        }
        else
        {
            fprintf(stderr, "%s must be > %d\n", what, min - 1);
            exit(EXIT_FAILURE);
        }
    }
    if (val > max && max >= 0)
    {
        fprintf(stderr, "%s must be <= %d\n", what, max);
        exit(EXIT_FAILURE);
    }
    return (val);
}

int
fw_prot_select(char *protocol)
{
    char *supp_protocols[] = {"UDP", "TCP", 0};
    int i;

    for (i = 0; supp_protocols[i]; i++)
    {
        if ((!strcasecmp(supp_protocols[i], protocol)))
        {
            switch (i)
            {
                case 0:
                    /* UDP */
                    return (IPPROTO_UDP);
                case 1:
                    /* TCP */
                    return (IPPROTO_TCP);
                default:
                    fprintf(stderr, "unsupported protocol: %s\n",
                            protocol);
                    exit(EXIT_FAILURE);
            }
        }
    }
    fprintf(stderr, "unsupported protocol: %s\n", protocol);
    return (-1);
}

/* EOF */