1 /*
2     massip-parse
3 
4     This module parses IPv4 and IPv6 addresses.
5 
6     It's not a typical parser. It's optimized around parsing large
7     files containing millions of addresses and ranges using a
8     "state-machine parser".
9 */
10 #ifndef MASSIP_PARSE_H
11 #define MASSIP_PARSE_H
12 #include "massip-addr.h"
13 
14 struct MassIP;
15 struct Range;
16 struct Range6;
17 
18 /**
19  * Parse a file, extracting all the IPv4 and IPv6 addresses and ranges.
20  * This is optimized for speed, handling millions of entries in under
21  * a second. This is especially tuned for IPv6 addresses, as while IPv4
22  * scanning is mostly done with target rnages, IPv6 scanning is mostly
23  * done with huge lists of target addresses.
24  * @param filename
25  *      The name of the file that we'll open, parse, and close.
26  * @param targets_ipv4
27  *      The list of IPv4 targets that we append any IPv4 addresses to.
28  * @param targets_ipv6
29  *      The list of IPv6 targets that we append any IPv6 addresses/ranges to.
30  * @return
31         0 on success, any other number on failure.
32  */
33 int
34 massip_parse_file(struct MassIP *massip, const char *filename);
35 
36 
37 enum RangeParseResult {
38     Bad_Address,
39     Ipv4_Address=4,
40     Ipv6_Address=6,
41 };
42 
43 /**
44  * Parse the next IPv4/IPv6 range from a string. This is called
45  * when parsing strings from the command-line.
46  */
47 enum RangeParseResult
48 massip_parse_range(const char *line, size_t *inout_offset, size_t max, struct Range *ipv4, struct Range6 *ipv6);
49 
50 
51 
52 /**
53  * Parse a single IPv6 address. This is called when working with
54  * the operating system stack, when querying addresses from
55  * the local network adapters.
56  */
57 ipv6address_t
58 massip_parse_ipv6(const char *buf);
59 
60 ipv4address_t
61 massip_parse_ipv4(const char *buf);
62 
63 
64 /**
65  * Do a simplistic unit test of the parser.
66  * @return 0 on success, 1 on failure
67  */
68 int
69 massip_parse_selftest(void);
70 
71 #endif
72 
73