12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
22 static int ifprint(pcap_if_t
*d
);
23 static char *iptos(bpf_u_int32 in
);
26 #include "portability.h"
29 * Generate a string for a Win32-specific error (i.e. an error generated when
30 * calling a Win32 API).
31 * For errors occurred during standard C calls, we still use pcap_strerror()
33 #define ERRBUF_SIZE 1024
35 win32_strerror(DWORD error
)
37 static char errbuf
[ERRBUF_SIZE
+1];
41 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
45 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
46 * message. Get rid of it.
48 errlen
= strlen(errbuf
);
50 errbuf
[errlen
- 1] = '\0';
51 errbuf
[errlen
- 2] = '\0';
53 p
= strchr(errbuf
, '\0');
54 pcap_snprintf(p
, ERRBUF_SIZE
+1-(p
-errbuf
), " (%lu)", error
);
59 getpass(const char *prompt
)
61 HANDLE console_handle
= GetStdHandle(STD_INPUT_HANDLE
);
62 DWORD console_mode
, save_console_mode
;
63 static char password
[128+1];
66 fprintf(stderr
, "%s", prompt
);
71 if (!GetConsoleMode(console_handle
, &console_mode
)) {
72 fprintf(stderr
, "Can't get console mode: %s\n",
73 win32_strerror(GetLastError()));
76 save_console_mode
= console_mode
;
77 console_mode
&= ~ENABLE_ECHO_INPUT
;
78 if (!SetConsoleMode(console_handle
, console_mode
)) {
79 fprintf(stderr
, "Can't set console mode: %s\n",
80 win32_strerror(GetLastError()));
83 if (fgets(password
, sizeof password
, stdin
) == NULL
) {
84 fprintf(stderr
, "\n");
85 SetConsoleMode(console_handle
, save_console_mode
);
88 fprintf(stderr
, "\n");
89 SetConsoleMode(console_handle
, save_console_mode
);
90 p
= strchr(password
, '\n');
97 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
)
152 if ( (s
= pcap_lookupdev(errbuf
)) == NULL
)
154 fprintf(stderr
,"Error in pcap_lookupdev: %s\n",errbuf
);
159 printf("Preferred device name: %s\n",s
);
162 if (pcap_lookupnet(s
, &net
, &mask
, errbuf
) < 0)
164 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
169 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
175 static int ifprint(pcap_if_t
*d
)
179 char ntop_buf
[INET6_ADDRSTRLEN
];
182 int status
= 1; /* success */
184 printf("%s\n",d
->name
);
186 printf("\tDescription: %s\n",d
->description
);
189 if (d
->flags
& PCAP_IF_UP
) {
193 if (d
->flags
& PCAP_IF_RUNNING
) {
194 printf("%sRUNNING", sep
);
197 if (d
->flags
& PCAP_IF_LOOPBACK
) {
198 printf("%sLOOPBACK", sep
);
203 for(a
=d
->addresses
;a
;a
=a
->next
) {
205 switch(a
->addr
->sa_family
) {
207 printf("\tAddress Family: AF_INET\n");
209 printf("\t\tAddress: %s\n",
210 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
212 printf("\t\tNetmask: %s\n",
213 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
215 printf("\t\tBroadcast Address: %s\n",
216 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
218 printf("\t\tDestination Address: %s\n",
219 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
223 printf("\tAddress Family: AF_INET6\n");
225 printf("\t\tAddress: %s\n",
227 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
228 ntop_buf
, sizeof ntop_buf
));
230 printf("\t\tNetmask: %s\n",
232 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
233 ntop_buf
, sizeof ntop_buf
));
235 printf("\t\tBroadcast Address: %s\n",
237 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
238 ntop_buf
, sizeof ntop_buf
));
240 printf("\t\tDestination Address: %s\n",
242 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
243 ntop_buf
, sizeof ntop_buf
));
247 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
252 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
260 /* From tcptraceroute */
261 #define IPTOSBUFFERS 12
262 static char *iptos(bpf_u_int32 in
)
264 static char output
[IPTOSBUFFERS
][3*4+3+1];
269 which
= (which
+ 1 == IPTOSBUFFERS
? 0 : which
+ 1);
270 sprintf(output
[which
], "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
271 return output
[which
];