]> The Tcpdump Group git mirrors - libpcap/blob - tests/findalldevstest.c
This might be what we want.
[libpcap] / tests / findalldevstest.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
11 #include <netdb.h>
12 #ifdef _WIN32
13 #include <windows.h>
14 #else
15 #include <pwd.h>
16 #include <unistd.h>
17 #endif
18
19 #include <pcap.h>
20
21 #include "pcap/funcattrs.h"
22
23 static int ifprint(pcap_if_t *d);
24 static char *iptos(bpf_u_int32 in);
25
26 #ifdef _WIN32
27 #include "portability.h"
28
29 /*
30 * Generate a string for a Win32-specific error (i.e. an error generated when
31 * calling a Win32 API).
32 * For errors occurred during standard C calls, we still use pcap_strerror()
33 */
34 #define ERRBUF_SIZE 1024
35 static const char *
36 win32_strerror(DWORD error)
37 {
38 static char errbuf[ERRBUF_SIZE+1];
39 size_t errlen;
40 char *p;
41
42 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
43 ERRBUF_SIZE, NULL);
44
45 /*
46 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
47 * message. Get rid of it.
48 */
49 errlen = strlen(errbuf);
50 if (errlen >= 2) {
51 errbuf[errlen - 1] = '\0';
52 errbuf[errlen - 2] = '\0';
53 }
54 p = strchr(errbuf, '\0');
55 pcap_snprintf(p, ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
56 return errbuf;
57 }
58
59 static char *
60 getpass(const char *prompt)
61 {
62 HANDLE console_handle = GetStdHandle(STD_INPUT_HANDLE);
63 DWORD console_mode, save_console_mode;
64 static char password[128+1];
65 char *p;
66
67 fprintf(stderr, "%s", prompt);
68
69 /*
70 * Turn off echoing.
71 */
72 if (!GetConsoleMode(console_handle, &console_mode)) {
73 fprintf(stderr, "Can't get console mode: %s\n",
74 win32_strerror(GetLastError()));
75 exit(1);
76 }
77 save_console_mode = console_mode;
78 console_mode &= ~ENABLE_ECHO_INPUT;
79 if (!SetConsoleMode(console_handle, console_mode)) {
80 fprintf(stderr, "Can't set console mode: %s\n",
81 win32_strerror(GetLastError()));
82 exit(1);
83 }
84 if (fgets(password, sizeof password, stdin) == NULL) {
85 fprintf(stderr, "\n");
86 SetConsoleMode(console_handle, save_console_mode);
87 exit(1);
88 }
89 fprintf(stderr, "\n");
90 SetConsoleMode(console_handle, save_console_mode);
91 p = strchr(password, '\n');
92 if (p != NULL)
93 *p = '\0';
94 return password;
95 }
96 #endif
97
98 int main(int argc, char **argv)
99 {
100 pcap_if_t *alldevs;
101 pcap_if_t *d;
102 bpf_u_int32 net, mask;
103 int exit_status = 0;
104 char errbuf[PCAP_ERRBUF_SIZE+1];
105 #ifdef ENABLE_REMOTE
106 struct pcap_rmtauth auth;
107 char username[128+1];
108 char *p;
109 char *password;
110 #endif
111
112 #ifdef ENABLE_REMOTE
113 if (argc >= 2)
114 {
115 if (pcap_findalldevs_ex(argv[1], NULL, &alldevs, errbuf) == -1)
116 {
117 /*
118 * OK, try it with a user name and password.
119 */
120 fprintf(stderr, "User name: ");
121 if (fgets(username, sizeof username, stdin) == NULL)
122 exit(1);
123 p = strchr(username, '\n');
124 if (p != NULL)
125 *p = '\0';
126 password = getpass("Password: ");
127 auth.type = RPCAP_RMTAUTH_PWD;
128 auth.username = username;
129 auth.password = password;
130 if (pcap_findalldevs_ex(argv[1], &auth, &alldevs, errbuf) == -1)
131 {
132 fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
133 exit(1);
134 }
135 }
136 }
137 else
138 #endif
139 {
140 if (pcap_findalldevs(&alldevs, errbuf) == -1)
141 {
142 fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
143 exit(1);
144 }
145 }
146 for(d=alldevs;d;d=d->next)
147 {
148 if (!ifprint(d))
149 exit_status = 2;
150 }
151
152 if (alldevs != NULL)
153 {
154 if (pcap_lookupnet(alldevs->name, &net, &mask, errbuf) < 0)
155 {
156 fprintf(stderr,"Error in pcap_lookupnet: %s\n",errbuf);
157 exit_status = 2;
158 }
159 else
160 {
161 printf("Preferred device is on network: %s/%s\n",iptos(net), iptos(mask));
162 }
163 }
164
165 pcap_freealldevs(alldevs);
166 exit(exit_status);
167 }
168
169 static int ifprint(pcap_if_t *d)
170 {
171 pcap_addr_t *a;
172 #ifdef INET6
173 char ntop_buf[INET6_ADDRSTRLEN];
174 #endif
175 const char *sep;
176 int status = 1; /* success */
177
178 printf("%s\n",d->name);
179 if (d->description)
180 printf("\tDescription: %s\n",d->description);
181 printf("\tFlags: ");
182 sep = "";
183 if (d->flags & PCAP_IF_UP) {
184 printf("%sUP", sep);
185 sep = ", ";
186 }
187 if (d->flags & PCAP_IF_RUNNING) {
188 printf("%sRUNNING", sep);
189 sep = ", ";
190 }
191 if (d->flags & PCAP_IF_LOOPBACK) {
192 printf("%sLOOPBACK", sep);
193 sep = ", ";
194 }
195 printf("\n");
196
197 for(a=d->addresses;a;a=a->next) {
198 if (a->addr != NULL)
199 switch(a->addr->sa_family) {
200 case AF_INET:
201 printf("\tAddress Family: AF_INET\n");
202 if (a->addr)
203 printf("\t\tAddress: %s\n",
204 inet_ntoa(((struct sockaddr_in *)(a->addr))->sin_addr));
205 if (a->netmask)
206 printf("\t\tNetmask: %s\n",
207 inet_ntoa(((struct sockaddr_in *)(a->netmask))->sin_addr));
208 if (a->broadaddr)
209 printf("\t\tBroadcast Address: %s\n",
210 inet_ntoa(((struct sockaddr_in *)(a->broadaddr))->sin_addr));
211 if (a->dstaddr)
212 printf("\t\tDestination Address: %s\n",
213 inet_ntoa(((struct sockaddr_in *)(a->dstaddr))->sin_addr));
214 break;
215 #ifdef INET6
216 case AF_INET6:
217 printf("\tAddress Family: AF_INET6\n");
218 if (a->addr)
219 printf("\t\tAddress: %s\n",
220 inet_ntop(AF_INET6,
221 ((struct sockaddr_in6 *)(a->addr))->sin6_addr.s6_addr,
222 ntop_buf, sizeof ntop_buf));
223 if (a->netmask)
224 printf("\t\tNetmask: %s\n",
225 inet_ntop(AF_INET6,
226 ((struct sockaddr_in6 *)(a->netmask))->sin6_addr.s6_addr,
227 ntop_buf, sizeof ntop_buf));
228 if (a->broadaddr)
229 printf("\t\tBroadcast Address: %s\n",
230 inet_ntop(AF_INET6,
231 ((struct sockaddr_in6 *)(a->broadaddr))->sin6_addr.s6_addr,
232 ntop_buf, sizeof ntop_buf));
233 if (a->dstaddr)
234 printf("\t\tDestination Address: %s\n",
235 inet_ntop(AF_INET6,
236 ((struct sockaddr_in6 *)(a->dstaddr))->sin6_addr.s6_addr,
237 ntop_buf, sizeof ntop_buf));
238 break;
239 #endif
240 default:
241 printf("\tAddress Family: Unknown (%d)\n", a->addr->sa_family);
242 break;
243 }
244 else
245 {
246 fprintf(stderr, "\tWarning: a->addr is NULL, skipping this address.\n");
247 status = 0;
248 }
249 }
250 printf("\n");
251 return status;
252 }
253
254 /* From tcptraceroute */
255 #define IPTOSBUFFERS 12
256 static char *iptos(bpf_u_int32 in)
257 {
258 static char output[IPTOSBUFFERS][3*4+3+1];
259 static short which;
260 u_char *p;
261
262 p = (u_char *)&in;
263 which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
264 sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
265 return output[which];
266 }