Annotation of embedaddon/arping/src/findif_linux.c, revision 1.1.1.1
1.1 misho 1: /* arping/src/findif_linux.c
2: *
3: * Copyright (C) 2000-2009 Thomas Habets <thomas@habets.pp.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: #if HAVE_CONFIG_H
20: #include "config.h"
21: #endif
22:
23: #include <stdio.h>
24: #include <string.h>
25:
26: #include "arping.h"
27:
28: /**
29: * WARNING: non-reentrant
30: */
31: const char *
32: arping_lookupdev(const char *ifname,
33: uint32_t srcip,
34: uint32_t dstip,
35: char *ebuf)
36: {
37: FILE *f;
38: static char buf[1024];
39: char buf1[1024];
40: char buf2[1024];
41: char *p,*p2;
42: int n;
43:
44: do_libnet_init(ifname);
45: libnet_addr2name4_r(dstip,0,buf2,1024);
46: libnet_addr2name4_r(srcip,0,buf1,1024);
47:
48: /*
49: * Construct and run command
50: */
51: snprintf(buf, 1023, "/sbin/ip route get %s from %s 2>&1",
52: buf2, buf1);
53: if (!(f = popen(buf, "r"))) {
54: goto failed;
55: }
56: if (0>(n = fread(buf, 1, sizeof(buf)-1, f))) {
57: pclose(f);
58: goto failed;
59: }
60: buf[n] = 0;
61: if (-1 == pclose(f)) {
62: perror("arping: pclose()");
63: goto failed;
64: }
65:
66: /*
67: * Parse interface name
68: */
69: p = strstr(buf, "dev ");
70: if (!p) {
71: goto failed;
72: }
73:
74: p+=4;
75:
76: p2 = strchr(p, ' ');
77: if (!p2) {
78: goto failed;
79: }
80: *p2 = 0;
81: return p;
82: failed:
83: return arping_lookupdev_default(ifname,srcip,dstip,ebuf);
84: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>