Debian Codegraph @

src/findif_bsdroute.c

raw symbols SCIP
1 /* arping/src/findif_bsdroute.c
2 *
3 * Copyright (C) 2000-2014 Thomas Habets <[email protected]>
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 size_t 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 xsnprintf(buf, 1023, "/sbin/route -n get %s 2>&1",
59 buf1);
60 if (!(f = popen(buf, "r"))) {
61 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
62 "popen(/sbin/route): %s", strerror(errno));
63 goto failed;
64 }
65 n = fread(buf, 1, sizeof(buf)-1, f);
66 if (n == 0) {
67 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
68 "fread(/sbin/route) empty: %s", strerror(errno));
69 goto failed;
70 }
71 if (ferror(f)) {
72 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
73 "fread(/sbin/route): %s", strerror(errno));
74 goto failed;
75 }
76 buf[n] = 0;
77 if (-1 == pclose(f)) {
78 f = NULL;
79 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
80 "pclose(/sbin/route): %s", strerror(errno));
81 goto failed;
82 }
83 f = NULL;
84
85 /*
86 * Parse interface name
87 */
88 const char* head = "interface: ";
89 p = strstr(buf, head);
90 if (!p) {
91 if (verbose) {
92 printf("arping: /sbin/route output: %s\n", buf);
93 }
94 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
95 "\"interface:\" not found in /sbin/route output.");
96 goto failed;
97 }
98
99 p += strlen(head);
100
101 p2 = strchr(p, '\n');
102 if (!p2) {
103 if (verbose) {
104 printf("arping: /sbin/route output: %s\n", buf);
105 }
106 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
107 "interface not found in /sbin/route output.");
108 goto failed;
109 }
110 *p2 = 0;
111 return p;
112 failed:
113 if (f) {
114 pclose(f);
115 }
116 return NULL;
117 }
118 /* ---- Emacs Variables ----
119 * Local Variables:
120 * c-basic-offset: 8
121 * indent-tabs-mode: nil
122 * End:
123 */