9 #include <sys/socket.h>
10 #include <netinet/in.h>
12 #include <arpa/inet.h>
23 #include "pcap/funcattrs.h"
25 static int ifprint(pcap_if_t
*d
);
26 static char *iptos(bpf_u_int32 in
);
29 #include "portability.h"
32 * Generate a string for a Win32-specific error (i.e. an error generated when
33 * calling a Win32 API).
34 * For errors occurred during standard C calls, we still use pcap_strerror()
36 #define ERRBUF_SIZE 1024
38 win32_strerror(DWORD error
)
40 static char errbuf
[ERRBUF_SIZE
+1];
44 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
48 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
49 * message. Get rid of it.
51 errlen
= strlen(errbuf
);
53 errbuf
[errlen
- 1] = '\0';
54 errbuf
[errlen
- 2] = '\0';
56 p
= strchr(errbuf
, '\0');
57 pcap_snprintf(p
, ERRBUF_SIZE
+1-(p
-errbuf
), " (%lu)", error
);
62 getpass(const char *prompt
)
64 HANDLE console_handle
= GetStdHandle(STD_INPUT_HANDLE
);
65 DWORD console_mode
, save_console_mode
;
66 static char password
[128+1];
69 fprintf(stderr
, "%s", prompt
);
74 if (!GetConsoleMode(console_handle
, &console_mode
)) {
75 fprintf(stderr
, "Can't get console mode: %s\n",
76 win32_strerror(GetLastError()));
79 save_console_mode
= console_mode
;
80 console_mode
&= ~ENABLE_ECHO_INPUT
;
81 if (!SetConsoleMode(console_handle
, console_mode
)) {
82 fprintf(stderr
, "Can't set console mode: %s\n",
83 win32_strerror(GetLastError()));
86 if (fgets(password
, sizeof password
, stdin
) == NULL
) {
87 fprintf(stderr
, "\n");
88 SetConsoleMode(console_handle
, save_console_mode
);
91 fprintf(stderr
, "\n");
92 SetConsoleMode(console_handle
, save_console_mode
);
93 p
= strchr(password
, '\n');
100 int main(int argc
, char **argv
)
104 bpf_u_int32 net
, mask
;
106 char errbuf
[PCAP_ERRBUF_SIZE
+1];
108 struct pcap_rmtauth auth
;
109 char username
[128+1];
117 if (pcap_findalldevs_ex(argv
[1], NULL
, &alldevs
, errbuf
) == -1)
120 * OK, try it with a user name and password.
122 fprintf(stderr
, "User name: ");
123 if (fgets(username
, sizeof username
, stdin
) == NULL
)
125 p
= strchr(username
, '\n');
128 password
= getpass("Password: ");
129 auth
.type
= RPCAP_RMTAUTH_PWD
;
130 auth
.username
= username
;
131 auth
.password
= password
;
132 if (pcap_findalldevs_ex(argv
[1], &auth
, &alldevs
, errbuf
) == -1)
134 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
142 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
144 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
148 for(d
=alldevs
;d
;d
=d
->next
)
156 if (pcap_lookupnet(alldevs
->name
, &net
, &mask
, errbuf
) < 0)
158 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
163 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
167 pcap_freealldevs(alldevs
);
171 static int ifprint(pcap_if_t
*d
)
175 char ntop_buf
[INET6_ADDRSTRLEN
];
178 int status
= 1; /* success */
180 printf("%s\n",d
->name
);
182 printf("\tDescription: %s\n",d
->description
);
185 if (d
->flags
& PCAP_IF_UP
) {
189 if (d
->flags
& PCAP_IF_RUNNING
) {
190 printf("%sRUNNING", sep
);
193 if (d
->flags
& PCAP_IF_LOOPBACK
) {
194 printf("%sLOOPBACK", sep
);
199 for(a
=d
->addresses
;a
;a
=a
->next
) {
201 switch(a
->addr
->sa_family
) {
203 printf("\tAddress Family: AF_INET\n");
205 printf("\t\tAddress: %s\n",
206 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
208 printf("\t\tNetmask: %s\n",
209 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
211 printf("\t\tBroadcast Address: %s\n",
212 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
214 printf("\t\tDestination Address: %s\n",
215 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
219 printf("\tAddress Family: AF_INET6\n");
221 printf("\t\tAddress: %s\n",
223 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
224 ntop_buf
, sizeof ntop_buf
));
226 printf("\t\tNetmask: %s\n",
228 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
229 ntop_buf
, sizeof ntop_buf
));
231 printf("\t\tBroadcast Address: %s\n",
233 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
234 ntop_buf
, sizeof ntop_buf
));
236 printf("\t\tDestination Address: %s\n",
238 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
239 ntop_buf
, sizeof ntop_buf
));
243 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
248 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
256 /* From tcptraceroute */
257 #define IPTOSBUFFERS 12
258 static char *iptos(bpf_u_int32 in
)
260 static char output
[IPTOSBUFFERS
][3*4+3+1];
265 which
= (which
+ 1 == IPTOSBUFFERS
? 0 : which
+ 1);
266 sprintf(output
[which
], "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
267 return output
[which
];