Annotation of embedaddon/arping/src/findif_bsd.c, revision 1.1.1.1
1.1 misho 1: /* arping/src/findif_bsd.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: *
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[10240];
39: char buf1[1024];
40: char *p,*p2;
41: int n;
42:
43: do_libnet_init(ifname);
44: libnet_addr2name4_r(dstip,0,buf1, 1024);
45:
46: /*
47: * Construct and run command
48: */
49: snprintf(buf, 1023, "/sbin/route -n get %s 2>&1",
50: buf1);
51: if (!(f = popen(buf, "r"))) {
52: goto failed;
53: }
54: if (0 > (n = fread(buf, 1, sizeof(buf)-1, f))) {
55: pclose(f);
56: goto failed;
57: }
58: buf[n] = 0;
59: if (-1 == pclose(f)) {
60: perror("arping: pclose()");
61: goto failed;
62: }
63:
64: /*
65: * Parse interface name
66: */
67: p = strstr(buf, "interface: ");
68: if (!p) {
69: goto failed;
70: }
71:
72: p+=11;
73:
74: p2 = strchr(p, '\n');
75: if (!p2) {
76: goto failed;
77: }
78: *p2 = 0;
79: return p;
80: failed:
81: return arping_lookupdev_default(ifname,srcip,dstip,ebuf);
82: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>