8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <arpa/inet.h>
21 #include "pcap/funcattrs.h"
23 static int ifprint(pcap_if_t
*d
);
24 static char *iptos(bpf_u_int32 in
);
27 #include "portability.h"
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()
34 #define ERRBUF_SIZE 1024
36 win32_strerror(DWORD error
)
38 static char errbuf
[ERRBUF_SIZE
+1];
42 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
46 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
47 * message. Get rid of it.
49 errlen
= strlen(errbuf
);
51 errbuf
[errlen
- 1] = '\0';
52 errbuf
[errlen
- 2] = '\0';
54 p
= strchr(errbuf
, '\0');
55 pcap_snprintf(p
, ERRBUF_SIZE
+1-(p
-errbuf
), " (%lu)", error
);
60 getpass(const char *prompt
)
62 HANDLE console_handle
= GetStdHandle(STD_INPUT_HANDLE
);
63 DWORD console_mode
, save_console_mode
;
64 static char password
[128+1];
67 fprintf(stderr
, "%s", prompt
);
72 if (!GetConsoleMode(console_handle
, &console_mode
)) {
73 fprintf(stderr
, "Can't get console mode: %s\n",
74 win32_strerror(GetLastError()));
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()));
84 if (fgets(password
, sizeof password
, stdin
) == NULL
) {
85 fprintf(stderr
, "\n");
86 SetConsoleMode(console_handle
, save_console_mode
);
89 fprintf(stderr
, "\n");
90 SetConsoleMode(console_handle
, save_console_mode
);
91 p
= strchr(password
, '\n');
98 int main(int argc
, char **argv
)
102 bpf_u_int32 net
, mask
;
104 char errbuf
[PCAP_ERRBUF_SIZE
+1];
106 struct pcap_rmtauth auth
;
107 char username
[128+1];
115 if (pcap_findalldevs_ex(argv
[1], NULL
, &alldevs
, errbuf
) == -1)
118 * OK, try it with a user name and password.
120 fprintf(stderr
, "User name: ");
121 if (fgets(username
, sizeof username
, stdin
) == NULL
)
123 p
= strchr(username
, '\n');
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)
132 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
140 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
142 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
146 for(d
=alldevs
;d
;d
=d
->next
)
154 if (pcap_lookupnet(alldevs
->name
, &net
, &mask
, errbuf
) < 0)
156 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
161 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
165 pcap_freealldevs(alldevs
);
169 static int ifprint(pcap_if_t
*d
)
173 char ntop_buf
[INET6_ADDRSTRLEN
];
176 int status
= 1; /* success */
178 printf("%s\n",d
->name
);
180 printf("\tDescription: %s\n",d
->description
);
183 if (d
->flags
& PCAP_IF_UP
) {
187 if (d
->flags
& PCAP_IF_RUNNING
) {
188 printf("%sRUNNING", sep
);
191 if (d
->flags
& PCAP_IF_LOOPBACK
) {
192 printf("%sLOOPBACK", sep
);
197 for(a
=d
->addresses
;a
;a
=a
->next
) {
199 switch(a
->addr
->sa_family
) {
201 printf("\tAddress Family: AF_INET\n");
203 printf("\t\tAddress: %s\n",
204 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
206 printf("\t\tNetmask: %s\n",
207 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
209 printf("\t\tBroadcast Address: %s\n",
210 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
212 printf("\t\tDestination Address: %s\n",
213 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
217 printf("\tAddress Family: AF_INET6\n");
219 printf("\t\tAddress: %s\n",
221 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
222 ntop_buf
, sizeof ntop_buf
));
224 printf("\t\tNetmask: %s\n",
226 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
227 ntop_buf
, sizeof ntop_buf
));
229 printf("\t\tBroadcast Address: %s\n",
231 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
232 ntop_buf
, sizeof ntop_buf
));
234 printf("\t\tDestination Address: %s\n",
236 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
237 ntop_buf
, sizeof ntop_buf
));
241 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
246 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
254 /* From tcptraceroute */
255 #define IPTOSBUFFERS 12
256 static char *iptos(bpf_u_int32 in
)
258 static char output
[IPTOSBUFFERS
][3*4+3+1];
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
];