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