| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#if HAVE_CONFIG_H |
| 24 |
#include "config.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <arpa/inet.h> |
| 33 |
|
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/socket.h> |
| 36 |
|
| 37 |
#include <sys/param.h> |
| 38 |
#include <sys/sysctl.h> |
| 39 |
#include <net/if.h> |
| 40 |
#include <net/if_dl.h> |
| 41 |
#include <net/route.h> |
| 42 |
|
| 43 |
#if HAVE_LIBNET_H |
| 44 |
#include <libnet.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "arping.h" |
| 48 |
|
| 49 |
#ifndef SALIGN |
| 50 |
#define SALIGN (sizeof(int32_t) - 1) |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifndef SA_SIZE |
| 54 |
#define SA_SIZE(sa) ((sa)->sa_len \ |
| 55 |
? (((sa)->sa_len + SALIGN) & ~SALIGN)\ |
| 56 |
: (SALIGN + 1)) |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
const char * |
| 64 |
arping_lookupdev(uint32_t srcip, |
| 65 |
uint32_t dstip, |
| 66 |
char *ebuf) |
| 67 |
{ |
| 68 |
int mib[6] = { |
| 69 |
CTL_NET, |
| 70 |
PF_ROUTE, |
| 71 |
0, |
| 72 |
AF_INET, |
| 73 |
NET_RT_IFLIST, |
| 74 |
0 |
| 75 |
}; |
| 76 |
int c; |
| 77 |
|
| 78 |
|
| 79 |
char *buf_memory = NULL; |
| 80 |
char *lim; |
| 81 |
size_t bufsize; |
| 82 |
|
| 83 |
|
| 84 |
int match_count = 0; |
| 85 |
|
| 86 |
|
| 87 |
in_addr_t best_mask = 0; |
| 88 |
struct in_addr best_addr; |
| 89 |
|
| 90 |
|
| 91 |
static char ifName[IFNAMSIZ]; |
| 92 |
*ebuf = 0; |
| 93 |
|
| 94 |
|
| 95 |
for (c = 0;;) { |
| 96 |
if (sysctl(mib, 6, NULL, &bufsize, NULL, 0) < 0) { |
| 97 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 98 |
"sysctl: get buffer size error: %s", |
| 99 |
strerror(errno)); |
| 100 |
goto failed; |
| 101 |
} |
| 102 |
if ((buf_memory = malloc(bufsize)) == NULL) { |
| 103 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 104 |
"malloc: error: %s", strerror(errno)); |
| 105 |
goto failed; |
| 106 |
} |
| 107 |
if (sysctl(mib, 6, buf_memory, &bufsize, NULL, 0) == 0) { |
| 108 |
break; |
| 109 |
} |
| 110 |
if (errno != ENOMEM || ++c >= 10 ) { |
| 111 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 112 |
"sysctl: get ifaces error: %s", |
| 113 |
strerror(errno)); |
| 114 |
goto failed; |
| 115 |
} |
| 116 |
if (verbose > 2) { |
| 117 |
printf("sysctl: buffer size changed."); |
| 118 |
} |
| 119 |
free(buf_memory); |
| 120 |
buf_memory = NULL; |
| 121 |
} |
| 122 |
|
| 123 |
const char* buf = buf_memory; |
| 124 |
lim = buf + bufsize; |
| 125 |
|
| 126 |
|
| 127 |
while (buf < lim) { |
| 128 |
struct sockaddr_dl *sdl; |
| 129 |
char tmpIfName[IFNAMSIZ]; |
| 130 |
int i; |
| 131 |
|
| 132 |
|
| 133 |
if (buf + sizeof(struct if_msghdr) > lim) { |
| 134 |
if (verbose > 1) { |
| 135 |
fprintf(stderr, "arping: More buffer available, but not enough.\n"); |
| 136 |
} |
| 137 |
break; |
| 138 |
} |
| 139 |
|
| 140 |
struct if_msghdr *ifh = (struct if_msghdr *)buf; |
| 141 |
if (ifh->ifm_type != RTM_IFINFO) { |
| 142 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 143 |
"Wrong data in NET_RT_IFLIST."); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
sdl = (struct sockaddr_dl *)(buf + |
| 147 |
sizeof(struct if_msghdr) - |
| 148 |
sizeof(struct if_data) + |
| 149 |
sizeof(struct if_data)); |
| 150 |
|
| 151 |
i = sdl->sdl_nlen < sizeof(ifName) |
| 152 |
? sdl->sdl_nlen |
| 153 |
: (sizeof(tmpIfName)-1); |
| 154 |
memcpy(tmpIfName, sdl->sdl_data, i); |
| 155 |
tmpIfName[i] = 0; |
| 156 |
|
| 157 |
buf += ifh->ifm_msglen; |
| 158 |
|
| 159 |
|
| 160 |
while (buf < lim) { |
| 161 |
|
| 162 |
if (buf + sizeof(struct ifa_msghdr) > lim) { |
| 163 |
if (verbose > 1) { |
| 164 |
fprintf(stderr, "arping: More buffer available, but not enough.\n"); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
struct ifa_msghdr *ifht = (struct ifa_msghdr *)buf; |
| 170 |
char* addrptr; |
| 171 |
struct sockaddr_in *if_addr = NULL; |
| 172 |
struct sockaddr_in *if_nmsk = NULL; |
| 173 |
struct sockaddr_in *if_bcst = NULL; |
| 174 |
in_addr_t mask; |
| 175 |
|
| 176 |
if (ifht->ifam_type != RTM_NEWADDR) { |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
addrptr = buf + sizeof(struct ifa_msghdr); |
| 181 |
buf += ifht->ifam_msglen; |
| 182 |
|
| 183 |
if (ifh->ifm_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
for (c=1; c < (1<<RTAX_MAX); c<<=1) { |
| 189 |
struct sockaddr_in *sa; |
| 190 |
if (addrptr + sizeof(struct sockaddr_in) > lim) { |
| 191 |
if (verbose > 1) { |
| 192 |
fprintf(stderr, "arping: More buffer available, but not enough.\n"); |
| 193 |
} |
| 194 |
break; |
| 195 |
} |
| 196 |
size_t len; |
| 197 |
sa = addrptr; |
| 198 |
switch (c & ifht->ifam_addrs) { |
| 199 |
case 0: |
| 200 |
continue; |
| 201 |
case RTA_NETMASK: |
| 202 |
if_nmsk = sa; |
| 203 |
break; |
| 204 |
case RTA_IFA: |
| 205 |
if_addr = sa; |
| 206 |
break; |
| 207 |
case RTA_BRD: |
| 208 |
if_bcst = sa; |
| 209 |
break; |
| 210 |
} |
| 211 |
addrptr += SA_SIZE((struct sockaddr*)sa); |
| 212 |
} |
| 213 |
|
| 214 |
if (!if_addr || !if_nmsk || !if_bcst) { |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
if (if_addr->sin_family != AF_INET) { |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
if ((dstip & if_nmsk->sin_addr.s_addr) |
| 223 |
!= (if_addr->sin_addr.s_addr |
| 224 |
& if_nmsk->sin_addr.s_addr)) { |
| 225 |
continue; |
| 226 |
} |
| 227 |
|
| 228 |
match_count++; |
| 229 |
|
| 230 |
if (verbose > 1) { |
| 231 |
printf("Specified addr matches " |
| 232 |
"interface '%s':\n", tmpIfName); |
| 233 |
printf(" IP addr %s, ", |
| 234 |
inet_ntoa(if_addr->sin_addr)); |
| 235 |
printf("mask %s, ", |
| 236 |
inet_ntoa(if_nmsk->sin_addr)); |
| 237 |
printf("bcast %s\n", |
| 238 |
inet_ntoa(if_bcst->sin_addr)); |
| 239 |
} |
| 240 |
|
| 241 |
mask = ntohl(if_nmsk->sin_addr.s_addr); |
| 242 |
if (mask > best_mask) { |
| 243 |
memcpy(ifName, |
| 244 |
tmpIfName, |
| 245 |
sizeof(ifName)); |
| 246 |
best_mask = mask; |
| 247 |
best_addr = if_addr->sin_addr; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (match_count == 0 ) { |
| 253 |
if (verbose) { |
| 254 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 255 |
"No interface found that matches" |
| 256 |
" specified IP."); |
| 257 |
} |
| 258 |
goto failed; |
| 259 |
} |
| 260 |
|
| 261 |
if (verbose && match_count > 1) { |
| 262 |
printf("arping: Using interface '%s' with src IP %s due " |
| 263 |
"to longer mask.\n", ifName, inet_ntoa(best_addr)); |
| 264 |
} |
| 265 |
#if 0 |
| 266 |
if (ifce_ip != 0) { |
| 267 |
*ifce_ip = best_addr.s_addr; |
| 268 |
} |
| 269 |
#endif |
| 270 |
free(buf_memory); |
| 271 |
return ifName; |
| 272 |
|
| 273 |
failed: |
| 274 |
free(buf_memory); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|