12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
22 #include "pcap/funcattrs.h"
24 static int ifprint(pcap_if_t
*d
);
25 static char *iptos(bpf_u_int32 in
);
28 #include "portability.h"
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()
35 #define ERRBUF_SIZE 1024
37 win32_strerror(DWORD error
)
39 static char errbuf
[ERRBUF_SIZE
+1];
43 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error
, 0, errbuf
,
47 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
48 * message. Get rid of it.
50 errlen
= strlen(errbuf
);
52 errbuf
[errlen
- 1] = '\0';
53 errbuf
[errlen
- 2] = '\0';
55 p
= strchr(errbuf
, '\0');
56 pcap_snprintf(p
, ERRBUF_SIZE
+1-(p
-errbuf
), " (%lu)", error
);
61 getpass(const char *prompt
)
63 HANDLE console_handle
= GetStdHandle(STD_INPUT_HANDLE
);
64 DWORD console_mode
, save_console_mode
;
65 static char password
[128+1];
68 fprintf(stderr
, "%s", prompt
);
73 if (!GetConsoleMode(console_handle
, &console_mode
)) {
74 fprintf(stderr
, "Can't get console mode: %s\n",
75 win32_strerror(GetLastError()));
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()));
85 if (fgets(password
, sizeof password
, stdin
) == NULL
) {
86 fprintf(stderr
, "\n");
87 SetConsoleMode(console_handle
, save_console_mode
);
90 fprintf(stderr
, "\n");
91 SetConsoleMode(console_handle
, save_console_mode
);
92 p
= strchr(password
, '\n');
99 int main(int argc
, char **argv
)
103 bpf_u_int32 net
, mask
;
105 char errbuf
[PCAP_ERRBUF_SIZE
+1];
107 struct pcap_rmtauth auth
;
108 char username
[128+1];
116 if (pcap_findalldevs_ex(argv
[1], NULL
, &alldevs
, errbuf
) == -1)
119 * OK, try it with a user name and password.
121 fprintf(stderr
, "User name: ");
122 if (fgets(username
, sizeof username
, stdin
) == NULL
)
124 p
= strchr(username
, '\n');
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)
133 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
141 if (pcap_findalldevs(&alldevs
, errbuf
) == -1)
143 fprintf(stderr
,"Error in pcap_findalldevs: %s\n",errbuf
);
147 for(d
=alldevs
;d
;d
=d
->next
)
155 if (pcap_lookupnet(alldevs
->name
, &net
, &mask
, errbuf
) < 0)
157 fprintf(stderr
,"Error in pcap_lookupnet: %s\n",errbuf
);
162 printf("Preferred device is on network: %s/%s\n",iptos(net
), iptos(mask
));
166 pcap_freealldevs(alldevs
);
170 static int ifprint(pcap_if_t
*d
)
174 char ntop_buf
[INET6_ADDRSTRLEN
];
177 int status
= 1; /* success */
179 printf("%s\n",d
->name
);
181 printf("\tDescription: %s\n",d
->description
);
184 if (d
->flags
& PCAP_IF_UP
) {
188 if (d
->flags
& PCAP_IF_RUNNING
) {
189 printf("%sRUNNING", sep
);
192 if (d
->flags
& PCAP_IF_LOOPBACK
) {
193 printf("%sLOOPBACK", sep
);
198 for(a
=d
->addresses
;a
;a
=a
->next
) {
200 switch(a
->addr
->sa_family
) {
202 printf("\tAddress Family: AF_INET\n");
204 printf("\t\tAddress: %s\n",
205 inet_ntoa(((struct sockaddr_in
*)(a
->addr
))->sin_addr
));
207 printf("\t\tNetmask: %s\n",
208 inet_ntoa(((struct sockaddr_in
*)(a
->netmask
))->sin_addr
));
210 printf("\t\tBroadcast Address: %s\n",
211 inet_ntoa(((struct sockaddr_in
*)(a
->broadaddr
))->sin_addr
));
213 printf("\t\tDestination Address: %s\n",
214 inet_ntoa(((struct sockaddr_in
*)(a
->dstaddr
))->sin_addr
));
218 printf("\tAddress Family: AF_INET6\n");
220 printf("\t\tAddress: %s\n",
222 ((struct sockaddr_in6
*)(a
->addr
))->sin6_addr
.s6_addr
,
223 ntop_buf
, sizeof ntop_buf
));
225 printf("\t\tNetmask: %s\n",
227 ((struct sockaddr_in6
*)(a
->netmask
))->sin6_addr
.s6_addr
,
228 ntop_buf
, sizeof ntop_buf
));
230 printf("\t\tBroadcast Address: %s\n",
232 ((struct sockaddr_in6
*)(a
->broadaddr
))->sin6_addr
.s6_addr
,
233 ntop_buf
, sizeof ntop_buf
));
235 printf("\t\tDestination Address: %s\n",
237 ((struct sockaddr_in6
*)(a
->dstaddr
))->sin6_addr
.s6_addr
,
238 ntop_buf
, sizeof ntop_buf
));
242 printf("\tAddress Family: Unknown (%d)\n", a
->addr
->sa_family
);
247 fprintf(stderr
, "\tWarning: a->addr is NULL, skipping this address.\n");
255 /* From tcptraceroute */
256 #define IPTOSBUFFERS 12
257 static char *iptos(bpf_u_int32 in
)
259 static char output
[IPTOSBUFFERS
][3*4+3+1];
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
];