Annotation of embedaddon/arping/src/findif_linux.c, revision 1.1.1.2
1.1 misho 1: /* arping/src/findif_linux.c
2: *
1.1.1.2 ! misho 3: * Copyright (C) 2000-2009 Thomas Habets <thomas@habets.se>
1.1 misho 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: */
1.1.1.2 ! misho 19: /**
! 20: * Fallback to ugly solution. This should not actually be used, as
! 21: * modern systems have getifaddrs().
! 22: */
1.1 misho 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: * WARNING: non-reentrant
34: */
35: const char *
1.1.1.2 ! misho 36: arping_lookupdev(uint32_t srcip,
1.1 misho 37: uint32_t dstip,
38: char *ebuf)
39: {
40: FILE *f;
41: static char buf[1024];
42: char buf1[1024];
43: char buf2[1024];
44: char *p,*p2;
45: int n;
46:
1.1.1.2 ! misho 47: do_libnet_init(NULL);
1.1 misho 48: libnet_addr2name4_r(dstip,0,buf2,1024);
49: libnet_addr2name4_r(srcip,0,buf1,1024);
50:
51: /*
52: * Construct and run command
53: */
54: snprintf(buf, 1023, "/sbin/ip route get %s from %s 2>&1",
55: buf2, buf1);
56: if (!(f = popen(buf, "r"))) {
57: goto failed;
58: }
59: if (0>(n = fread(buf, 1, sizeof(buf)-1, f))) {
60: pclose(f);
61: goto failed;
62: }
63: buf[n] = 0;
64: if (-1 == pclose(f)) {
65: perror("arping: pclose()");
66: goto failed;
67: }
68:
69: /*
70: * Parse interface name
71: */
72: p = strstr(buf, "dev ");
73: if (!p) {
74: goto failed;
75: }
76:
77: p+=4;
78:
79: p2 = strchr(p, ' ');
80: if (!p2) {
81: goto failed;
82: }
83: *p2 = 0;
84: return p;
85: failed:
1.1.1.2 ! misho 86: return NULL;
1.1 misho 87: }
1.1.1.2 ! misho 88: /* ---- Emacs Variables ----
! 89: * Local Variables:
! 90: * c-basic-offset: 8
! 91: * indent-tabs-mode: nil
! 92: * End:
! 93: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>