1 #ifndef MASSIP_H 2 #define MASSIP_H 3 #include <stddef.h> 4 #include "massip-rangesv4.h" 5 #include "massip-rangesv6.h" 6 7 struct MassIP { 8 struct RangeList ipv4; 9 struct Range6List ipv6; 10 11 /** 12 * The ports we are scanning for. The user can specify repeated ports 13 * and overlapping ranges, but we'll deduplicate them, scanning ports 14 * only once. 15 * NOTE: TCP ports are stored 0-64k, but UDP ports are stored in the 16 * range 64k-128k, thus, allowing us to scan both at the same time. 17 */ 18 struct RangeList ports; 19 20 /** 21 * Used internally to differentiate between indexes selecting an 22 * IPv4 address and higher ones selecting an IPv6 address. 23 */ 24 uint64_t ipv4_index_threshold; 25 26 uint64_t count_ports; 27 uint64_t count_ipv4s; 28 uint64_t count_ipv6s; 29 }; 30 31 /** 32 * Count the total number of targets in a scan. This is calculated 33 * the (IPv6 addresses * IPv4 addresses * ports). This can produce 34 * a 128-bit number (larger, actually). 35 */ 36 massint128_t massip_range(struct MassIP *massip); 37 38 /** 39 * Remove everything in "targets" that's listed in the "exclude" 40 * list. The reason for this is that we'll have a single policy 41 * file of those address ranges which we are forbidden to scan. 42 * Then, each time we run a scan with different targets, we 43 * apply this policy file. 44 */ 45 void massip_apply_excludes(struct MassIP *targets, struct MassIP *exclude); 46 47 /** 48 * The last step after processing the configuration, setting up the 49 * state to be used for scanning. This sorts the address, removes 50 * duplicates, and creates an optimized 'picker' system to easily 51 * find an address given an index, or find an index given an address. 52 */ 53 void massip_optimize(struct MassIP *targets); 54 55 /** 56 * This selects an IP+port combination given an index whose value 57 * is [0..range], where 'range' is the value returned by the function 58 * `massip_range()`. Since the optimization step (`massip_optimized()`) 59 * sorted all addresses/ports, a monotonically increeasing index will 60 * list everything in sorted order. The intent, however, is to use the 61 * "blackrock" algorithm to randomize the index before calling this function. 62 * 63 * It is this function, plus the 'blackrock' randomization algorith, that 64 * is at the heart of Masscan. 65 */ 66 int massip_pick(const struct MassIP *massip, uint64_t index, ipaddress *addr, unsigned *port); 67 68 69 int massip_has_ip(const struct MassIP *massip, ipaddress ip); 70 71 int massip_has_port(const struct MassIP *massip, unsigned port); 72 73 int massip_add_target_string(struct MassIP *massip, const char *string); 74 75 /** 76 * Parse the string contain port specifier. 77 */ 78 int massip_add_port_string(struct MassIP *massip, const char *string, unsigned proto); 79 80 81 /** 82 * Indicates whether there are IPv4 targets. If so, we'll have to 83 * initialize the IPv4 portion of the stack. 84 * @return true if there are IPv4 targets to be scanned, false 85 * otherwise 86 */ 87 int massip_has_ipv4_targets(const struct MassIP *massip); 88 int massip_has_target_ports(const struct MassIP *massip); 89 90 /** 91 * Indicates whether there are IPv6 targets. If so, we'll have to 92 * initialize the IPv6 portion of the stack. 93 * @return true if there are IPv6 targets to be scanned, false 94 * otherwise 95 */ 96 int massip_has_ipv6_targets(const struct MassIP *massip); 97 98 99 int massip_selftest(void); 100 101 #endif 102