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