1 /* 2 * ARP Scan is Copyright (C) 2005-2016 Roy Hills, NTA Monitor Ltd. 3 * 4 * This file is part of arp-scan. 5 * 6 * arp-scan is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * arp-scan is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with arp-scan. If not, see <http://www.gnu.org/licenses/>. 18 * 19 * arp-scan.h -- Header file for ARP scanner 20 * 21 * Author: Roy Hills 22 * Date: 11 October 2005 23 * 24 */ 25 26 /* Includes */ 27 #ifdef HAVE_CONFIG_H 28 #include "config.h" 29 #endif 30 31 /* C89 standard headers */ 32 #include <stdio.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <ctype.h> 36 #include <stdarg.h> 37 #include <errno.h> 38 39 #include <sys/types.h> 40 41 /* Integer types */ 42 #ifdef HAVE_INTTYPES_H 43 #include <inttypes.h> 44 #else 45 #ifdef HAVE_STDINT_H 46 #include <stdint.h> 47 #endif 48 #endif 49 50 #ifdef __CYGWIN__ 51 #include <windows.h> /* Include windows.h if compiling under Cygwin */ 52 #endif 53 54 #ifdef HAVE_UNISTD_H 55 #include <unistd.h> 56 #endif 57 58 #ifdef HAVE_NETDB_H 59 #include <netdb.h> 60 #endif 61 62 #ifdef HAVE_GETOPT_H 63 #include <getopt.h> 64 #else 65 /* Include getopt.h for the sake of getopt_long. 66 We don't need the declaration of getopt, and it could conflict 67 with something from a system header file, so effectively nullify that. */ 68 #define getopt getopt_loser 69 #include "getopt.h" 70 #undef getopt 71 #endif 72 73 #ifdef HAVE_NETINET_IN_H 74 #include <netinet/in.h> 75 #endif 76 77 #ifdef HAVE_SYS_TIME_H 78 #include <sys/time.h> 79 #endif 80 81 #ifdef HAVE_SYS_SOCKET_H 82 #include <sys/socket.h> 83 #endif 84 85 #ifdef HAVE_ARPA_INET_H 86 #include <arpa/inet.h> 87 #endif 88 89 #ifdef HAVE_REGEX_H 90 #include <regex.h> /* Posix regular expression functions */ 91 #endif 92 93 #ifdef HAVE_SYS_STAT_H 94 #include <sys/stat.h> 95 #endif 96 97 #ifdef HAVE_FCNTL_H 98 #include <fcntl.h> 99 #endif 100 101 #ifdef HAVE_PCAP_H 102 #include <pcap.h> 103 #endif 104 105 #ifdef HAVE_SYS_IOCTL_H 106 #include <sys/ioctl.h> 107 #endif 108 109 #ifdef ARP_PCAP_DLPI 110 #ifdef HAVE_SYS_BUFMOD_H 111 #include <sys/bufmod.h> 112 #endif 113 #endif 114 115 #ifdef HAVE_SEARCH_H 116 #include <search.h> 117 #endif 118 119 /* Defines */ 120 121 #define MAXLINE 255 /* Max line length for input files */ 122 #define MAX_FRAME 2048 /* Maximum allowed frame size */ 123 #define REALLOC_COUNT 1000 /* Entries to realloc at once */ 124 #define DEFAULT_BANDWIDTH 256000 /* Default bandwidth in bits/sec */ 125 #define PACKET_OVERHEAD 18 /* layer 2 overhead (6+6+2 + 4) */ 126 #define MINIMUM_FRAME_SIZE 46 /* Minimum layer 2 date size */ 127 #define DEFAULT_BACKOFF_FACTOR 1.5 /* Default timeout backoff factor */ 128 #define DEFAULT_RETRY 2 /* Default number of retries */ 129 #define DEFAULT_TIMEOUT 500 /* Default per-host timeout in ms */ 130 #define SNAPLEN 64 /* 14 (ether) + 28 (ARP) + extra */ 131 #define PROMISC 1 /* Enable promiscuous mode */ 132 #define TO_MS 1000 /* Timeout for pcap_set_timeout() */ 133 #define OPTIMISE 1 /* Optimise pcap filter */ 134 #define ARPHRD_ETHER 1 /* Ethernet ARP type */ 135 #define ARPOP_REQUEST 1 /* ARP Request */ 136 #define ARPOP_REPLY 2 /* ARP Reply */ 137 #define ETHER_HDR_SIZE 14 /* Size of Ethernet frame header in bytes */ 138 #define ARP_PKT_SIZE 28 /* Size of ARP Packet in bytes */ 139 #define ETH_ALEN 6 /* Octets in one ethernet addr */ 140 #define ETH_P_IP 0x0800 /* Internet Protocol packet */ 141 #define ETH_P_ARP 0x0806 /* Address Resolution packet */ 142 #define OUIFILENAME "ieee-oui.txt" /* Default IEEE OUI filename */ 143 #define IABFILENAME "ieee-iab.txt" /* Default IEEE IAB filename */ 144 #define MACFILENAME "mac-vendor.txt" /* Default MAC/Vendor filename */ 145 #define DEFAULT_ARP_OP ARPOP_REQUEST /* Default ARP operation */ 146 #define DEFAULT_ARP_HRD ARPHRD_ETHER /* Default ARP hardware type */ 147 #define DEFAULT_ARP_PRO ETH_P_IP /* Default ARP protocol */ 148 #define DEFAULT_ARP_HLN 6 /* Default hardware length */ 149 #define DEFAULT_ARP_PLN 4 /* Default protocol length */ 150 #define DEFAULT_ETH_PRO ETH_P_ARP /* Default Ethernet protocol */ 151 #define FRAMING_ETHERNET_II 0 /* Standard Ethernet-II Framing */ 152 #define FRAMING_LLC_SNAP 1 /* 802.3 with LLC/SNAP */ 153 #define OPT_WRITEPKTTOFILE 256 /* --writepkttofile option */ 154 #define OPT_READPKTFROMFILE 257 /* --readpktfromfile option */ 155 #define OPT_RANDOMSEED 258 /* --randomseed option */ 156 #define HASH_TABLE_SIZE 50000 /* Max size of OUI/Vendor hash table */ 157 158 /* Structures */ 159 160 typedef struct { 161 unsigned timeout; /* Timeout for this host in us */ 162 struct in_addr addr; /* Host IP address */ 163 struct timeval last_send_time; /* Time when last packet sent to this addr */ 164 unsigned short num_sent; /* Number of packets sent */ 165 unsigned short num_recv; /* Number of packets received */ 166 unsigned char live; /* Set when awaiting response */ 167 } host_entry; 168 169 /* Ethernet frame header */ 170 typedef struct { 171 uint8_t dest_addr[ETH_ALEN]; /* Destination hardware address */ 172 uint8_t src_addr[ETH_ALEN]; /* Source hardware address */ 173 uint16_t frame_type; /* Ethernet frame type */ 174 } ether_hdr; 175 176 /* Ethernet ARP packet from RFC 826 */ 177 typedef struct { 178 uint16_t ar_hrd; /* Format of hardware address */ 179 uint16_t ar_pro; /* Format of protocol address */ 180 uint8_t ar_hln; /* Length of hardware address */ 181 uint8_t ar_pln; /* Length of protocol address */ 182 uint16_t ar_op; /* ARP opcode (command) */ 183 uint8_t ar_sha[ETH_ALEN]; /* Sender hardware address */ 184 uint32_t ar_sip; /* Sender IP address */ 185 uint8_t ar_tha[ETH_ALEN]; /* Target hardware address */ 186 uint32_t ar_tip; /* Target IP address */ 187 } arp_ether_ipv4; 188 189 /* Functions */ 190 191 #ifndef HAVE_STRLCAT 192 size_t strlcat(char *dst, const char *src, size_t siz); 193 #endif 194 #ifndef HAVE_STRLCPY 195 size_t strlcpy(char *dst, const char *src, size_t siz); 196 #endif 197 198 void err_sys(const char *, ...); 199 void warn_sys(const char *, ...); 200 void err_msg(const char *, ...); 201 void warn_msg(const char *, ...); 202 void err_print(int, const char *, va_list); 203 void usage(int, int); 204 void add_host_pattern(const char *, unsigned); 205 void add_host(const char *, unsigned, int); 206 int send_packet(pcap_t *, host_entry *, struct timeval *); 207 void recvfrom_wto(int, int, pcap_t *); 208 void remove_host(host_entry **); 209 void timeval_diff(const struct timeval *, const struct timeval *, 210 struct timeval *); 211 host_entry *find_host(host_entry **, struct in_addr *); 212 void display_packet(host_entry *, arp_ether_ipv4 *, const unsigned char *, 213 size_t, int, int, ether_hdr *, const struct pcap_pkthdr *); 214 void advance_cursor(void); 215 void dump_list(void); 216 void clean_up(pcap_t *); 217 void arp_scan_version(void); 218 char *make_message(const char *, ...); 219 void callback(u_char *, const struct pcap_pkthdr *, const u_char *); 220 void process_options(int, char *[]); 221 struct in_addr *get_host_address(const char *, int, struct in_addr *, char **); 222 const char *my_ntoa(struct in_addr); 223 int get_source_ip(const char *, struct in_addr *); 224 void get_hardware_address(const char *, unsigned char []); 225 void marshal_arp_pkt(unsigned char *, ether_hdr *, arp_ether_ipv4 *, size_t *, 226 const unsigned char *, size_t); 227 int unmarshal_arp_pkt(const unsigned char *, size_t, ether_hdr *, 228 arp_ether_ipv4 *, unsigned char *, size_t *, int *); 229 unsigned char *hex2data(const char *, size_t *); 230 unsigned int hstr_i(const char *); 231 char *hexstring(const unsigned char *, size_t); 232 int get_ether_addr(const char *, unsigned char *); 233 int add_mac_vendor(const char *); 234 char *get_mac_vendor_filename(const char *, const char *, const char *); 235 /* Wrappers */ 236 int Gettimeofday(struct timeval *); 237 void *Malloc(size_t); 238 void *Realloc(void *, size_t); 239 unsigned long int Strtoul(const char *, int); 240 long int Strtol(const char *, int); 241 char *my_lookupdev(char *); 242 unsigned str_to_bandwidth(const char *); 243 unsigned str_to_interval(const char *); 244 char *dupstr(const char *); 245 /* MT19937 prototypes */ 246 void init_genrand(unsigned long); 247 void init_by_array(unsigned long[], int); 248 unsigned long genrand_int32(void); 249 long genrand_int31(void); 250 double genrand_real1(void); 251 double genrand_real2(void); 252 double genrand_real3(void); 253 double genrand_res53(void); 254