Annotation of embedaddon/arping/src/findif_bsdroute.c, revision 1.1.1.1
1.1 misho 1: /* arping/src/findif_bsdroute.c
2: *
3: * Copyright (C) 2000-2009 Thomas Habets <thomas@habets.se>
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU General Public
7: * License as published by the Free Software Foundation; either
8: * version 2 of the License, or (at your option) any later version.
9: *
10: * This library 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 GNU
13: * 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: #include "arping.h"
31:
32: /**
33: *
34: */
35: const char *
36: arping_lookupdev(uint32_t srcip,
37: uint32_t dstip,
38: char *ebuf)
39: {
40: FILE *f;
41: static char buf[10240];
42: char buf1[1024];
43: char *p,*p2;
44: int n;
45:
46: do_libnet_init(NULL);
47: libnet_addr2name4_r(dstip,0,buf1, 1024);
48:
49: /*
50: * Construct and run command
51: */
52: snprintf(buf, 1023, "/sbin/route -n get %s 2>&1",
53: buf1);
54: if (!(f = popen(buf, "r"))) {
55: goto failed;
56: }
57: if (0 > (n = fread(buf, 1, sizeof(buf)-1, f))) {
58: pclose(f);
59: goto failed;
60: }
61: buf[n] = 0;
62: if (-1 == pclose(f)) {
63: perror("arping: pclose()");
64: goto failed;
65: }
66:
67: /*
68: * Parse interface name
69: */
70: p = strstr(buf, "interface: ");
71: if (!p) {
72: goto failed;
73: }
74:
75: p+=11;
76:
77: p2 = strchr(p, '\n');
78: if (!p2) {
79: goto failed;
80: }
81: *p2 = 0;
82: return p;
83: failed:
84: return NULL;
85: }
86: /* ---- Emacs Variables ----
87: * Local Variables:
88: * c-basic-offset: 8
89: * indent-tabs-mode: nil
90: * End:
91: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>