1 /*
2  * The ARP Scanner (arp-scan) is Copyright (C) 2005-2019 Roy Hills,
3  * NTA Monitor Ltd.
4  *
5  * This file is part of arp-scan.
6  *
7  * arp-scan is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * arp-scan is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with arp-scan.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Roy Hills
21  * Date: 8 November 2003
22  *
23  * This file contains wrapper functions for system and library calls that
24  * are not expected to fail.  If they do fail, then it calls err_sys to
25  * print a diagnostic and terminate the program.  This removed the tedious
26  * "if ((function()) == NULL) err_sys("function");" logic thus making the
27  * code easier to read.
28  *
29  * The wrapper functions have the same name as the system or library function
30  * but with an initial capital letter.  E.g. Gethostbyname().  This convention
31  * if from Richard Steven's UNIX Network Programming book.
32  *
33  */
34 
35 #include "arp-scan.h"
36 
37 /*
38  * We omit the timezone arg from this wrapper since it's obsolete and we never
39  * use it.
40  */
Gettimeofday(struct timeval * tv)41 int Gettimeofday(struct timeval *tv) {
42    int result;
43 
44    result = gettimeofday(tv, NULL);
45 
46    if (result != 0)
47       err_sys("gettimeofday");
48 
49    return result;
50 }
51 
Malloc(size_t size)52 void *Malloc(size_t size) {
53    void *result;
54 
55    result = malloc(size);
56 
57    if (result == NULL)
58       err_sys("malloc");
59 
60    return result;
61 }
62 
Realloc(void * ptr,size_t size)63 void *Realloc(void *ptr, size_t size) {
64    void *result;
65 
66    result=realloc(ptr, size);
67 
68    if (result == NULL)
69       err_sys("realloc");
70 
71    return result;
72 }
73 
Strtoul(const char * nptr,int base)74 unsigned long int Strtoul(const char *nptr, int base) {
75    char *endptr;
76    unsigned long int result;
77 
78    result=strtoul(nptr, &endptr, base);
79    if (endptr == nptr)	/* No digits converted */
80       err_msg("ERROR: \"%s\" is not a valid numeric value", nptr);
81    if (*endptr != '\0' && !isspace((unsigned char)*endptr))
82       err_msg("ERROR: \"%s\" is not a valid numeric value", nptr);
83 
84    return result;
85 }
86 
Strtol(const char * nptr,int base)87 long int Strtol(const char *nptr, int base) {
88    char *endptr;
89    long int result;
90 
91    result=strtol(nptr, &endptr, base);
92    if (endptr == nptr)	/* No digits converted */
93       err_msg("ERROR: \"%s\" is not a valid numeric value", nptr);
94    if (*endptr != '\0' && !isspace((unsigned char)*endptr))
95       err_msg("ERROR: \"%s\" is not a valid numeric value", nptr);
96 
97    return result;
98 }
99 
100 /*
101  * my_lookupdev() is adapted from pcap_lookupdev() which is depreciated
102  * in libpcap 1.9.0 and later.
103  */
104 char *
my_lookupdev(char * errbuf)105 my_lookupdev(char *errbuf)
106 {
107    pcap_if_t *alldevs;
108 
109 #ifndef IF_NAMESIZE
110 #define IF_NAMESIZE 8192
111 #endif
112 
113    static char device[IF_NAMESIZE + 1];
114    char *ret;
115 
116    if (pcap_findalldevs(&alldevs, errbuf) == -1)
117       return (NULL);
118 
119    if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
120    /*
121    * There are no devices on the list, or the first device
122    * on the list is a loopback device, which means there
123    * are no non-loopback devices on the list.  This means
124    * we can't return any device.
125    */
126       (void)strlcpy(errbuf, "no suitable device found", PCAP_ERRBUF_SIZE);
127       ret = NULL;
128    } else {
129    /*
130    * Return the name of the first device on the list.
131    */
132       (void)strlcpy(device, alldevs->name, sizeof(device));
133       ret = device;
134    }
135 
136    pcap_freealldevs(alldevs);
137    return (ret);
138 }
139