1: /* arping/src/findif_bsdroute.c
2: *
3: * Copyright (C) 2000-2014 Thomas Habets <thomas@habets.se>
4: *
5: * This program is free software; you can redistribute it and/or modify
6: * it under the terms of the GNU General Public License as published by
7: * the Free Software Foundation; either version 2 of the License, or
8: * (at your option) any later version.
9: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * You should have received a copy of the GNU General Public License along
16: * with this program; if not, write to the Free Software Foundation, Inc.,
17: * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18: */
19: /**
20: * Fallback to ugly solution. This should not actually be used, as
21: * modern systems have getifaddrs().
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: int n;
49:
50: *ebuf = 0;
51:
52: do_libnet_init(NULL, 0);
53: libnet_addr2name4_r(dstip,0,buf1, 1024);
54:
55: /*
56: * Construct and run command
57: */
58: snprintf(buf, 1023, "/sbin/route -n get %s 2>&1",
59: buf1);
60: if (!(f = popen(buf, "r"))) {
61: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
62: "popen(/sbin/route): %s", strerror(errno));
63: goto failed;
64: }
65: if (0 > (n = fread(buf, 1, sizeof(buf)-1, f))) {
66: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
67: "fread(/sbin/route): %s", strerror(errno));
68: goto failed;
69: }
70: buf[n] = 0;
71: if (-1 == pclose(f)) {
72: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
73: "pclose(/sbin/route): %s", strerror(errno));
74: goto failed;
75: }
76: f = NULL;
77:
78: /*
79: * Parse interface name
80: */
81: const char* head = "interface: ";
82: p = strstr(buf, head);
83: if (!p) {
84: if (verbose) {
85: printf("arping: /sbin/route output: %s\n", buf);
86: }
87: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
88: "\"interface:\" not found in /sbin/route output.");
89: goto failed;
90: }
91:
92: p += strlen(head);
93:
94: p2 = strchr(p, '\n');
95: if (!p2) {
96: if (verbose) {
97: printf("arping: /sbin/route output: %s\n", buf);
98: }
99: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
100: "interface not found in /sbin/route output.");
101: goto failed;
102: }
103: *p2 = 0;
104: return p;
105: failed:
106: if (f) {
107: pclose(f);
108: }
109: return NULL;
110: }
111: /* ---- Emacs Variables ----
112: * Local Variables:
113: * c-basic-offset: 8
114: * indent-tabs-mode: nil
115: * End:
116: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>