| 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 <string.h> |
| 29 |
|
| 30 |
#if HAVE_LIBNET_H |
| 31 |
#include <libnet.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "arping.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
const char * |
| 40 |
arping_lookupdev(uint32_t srcip, |
| 41 |
uint32_t dstip, |
| 42 |
char *ebuf) |
| 43 |
{ |
| 44 |
FILE *f = NULL; |
| 45 |
static char buf[10240]; |
| 46 |
char buf1[1024]; |
| 47 |
char *p,*p2; |
| 48 |
size_t n; |
| 49 |
|
| 50 |
*ebuf = 0; |
| 51 |
|
| 52 |
do_libnet_init(NULL, 0); |
| 53 |
libnet_addr2name4_r(dstip,0,buf1, 1024); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
xsnprintf(buf, 1023, "/sbin/route -n get %s 2>&1", |
| 59 |
buf1); |
| 60 |
if (!(f = popen(buf, "r"))) { |
| 61 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 62 |
"popen(/sbin/route): %s", strerror(errno)); |
| 63 |
goto failed; |
| 64 |
} |
| 65 |
n = fread(buf, 1, sizeof(buf)-1, f); |
| 66 |
if (n == 0) { |
| 67 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 68 |
"fread(/sbin/route) empty: %s", strerror(errno)); |
| 69 |
goto failed; |
| 70 |
} |
| 71 |
if (ferror(f)) { |
| 72 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 73 |
"fread(/sbin/route): %s", strerror(errno)); |
| 74 |
goto failed; |
| 75 |
} |
| 76 |
buf[n] = 0; |
| 77 |
if (-1 == pclose(f)) { |
| 78 |
f = NULL; |
| 79 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 80 |
"pclose(/sbin/route): %s", strerror(errno)); |
| 81 |
goto failed; |
| 82 |
} |
| 83 |
f = NULL; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
const char* head = "interface: "; |
| 89 |
p = strstr(buf, head); |
| 90 |
if (!p) { |
| 91 |
if (verbose) { |
| 92 |
printf("arping: /sbin/route output: %s\n", buf); |
| 93 |
} |
| 94 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 95 |
"\"interface:\" not found in /sbin/route output."); |
| 96 |
goto failed; |
| 97 |
} |
| 98 |
|
| 99 |
p += strlen(head); |
| 100 |
|
| 101 |
p2 = strchr(p, '\n'); |
| 102 |
if (!p2) { |
| 103 |
if (verbose) { |
| 104 |
printf("arping: /sbin/route output: %s\n", buf); |
| 105 |
} |
| 106 |
xsnprintf(ebuf, LIBNET_ERRBUF_SIZE, |
| 107 |
"interface not found in /sbin/route output."); |
| 108 |
goto failed; |
| 109 |
} |
| 110 |
*p2 = 0; |
| 111 |
return p; |
| 112 |
failed: |
| 113 |
if (f) { |
| 114 |
pclose(f); |
| 115 |
} |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|