Debian Codegraph @

src/findif_linux.c

raw symbols SCIP
1 /* arping/src/findif_linux.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 #error "This code should never be chosen. If you just want to use it then uncomment this error line"
37
38 /**
39 * WARNING: non-reentrant
40 */
41 const char *
42 arping_lookupdev(uint32_t srcip,
43 uint32_t dstip,
44 char *ebuf)
45 {
46 FILE *f = NULL;
47 static char buf[1024];
48 char buf1[1024];
49 char buf2[1024];
50 char *p,*p2;
51 size_t n;
52
53 *ebuf = 0;
54
55 do_libnet_init(NULL, 0);
56 libnet_addr2name4_r(dstip,0,buf2,1024);
57 libnet_addr2name4_r(srcip,0,buf1,1024);
58
59 /*
60 * Construct and run command
61 */
62 xsnprintf(buf, 1023, "/sbin/ip route get %s from %s 2>&1",
63 buf2, buf1);
64 if (!(f = popen(buf, "r"))) {
65 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
66 "popen(/sbin/ip): %s", strerror(errno));
67 goto failed;
68 }
69 n = fread(buf, 1, sizeof(buf)-1, f);
70 if (n == 0) {
71 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
72 "fread(/sbin/route) empty: %s", strerror(errno));
73 goto failed;
74 }
75 if (ferror(f)) {
76 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
77 "fread(/sbin/ip): %s", strerror(errno));
78 goto failed;
79 }
80 buf[n] = 0;
81 if (-1 == pclose(f)) {
82 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
83 "pclose(/sbin/ip): %s", strerror(errno));
84 goto failed;
85 }
86 f = NULL;
87
88 /*
89 * Parse interface name
90 */
91 const char* head = "dev ";
92 p = strstr(buf, head);
93 if (!p) {
94 if (verbose) {
95 printf("arping: /sbin/ip output: %s\n", buf);
96 }
97 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
98 "\"dev \" not found in /sbin/ip output.");
99 goto failed;
100 }
101
102 p += strlen(head);
103
104 p2 = strchr(p, ' ');
105 if (!p2) {
106 if (verbose) {
107 printf("arping: /sbin/ip output: %s\n", buf);
108 }
109 xsnprintf(ebuf, LIBNET_ERRBUF_SIZE,
110 "interface not found in /sbin/ip output.");
111 goto failed;
112 }
113 *p2 = 0;
114 return p;
115 failed:
116 if (f) {
117 pclose(f);
118 }
119 return NULL;
120 }
121 /* ---- Emacs Variables ----
122 * Local Variables:
123 * c-basic-offset: 8
124 * indent-tabs-mode: nil
125 * End:
126 */