2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 // NOTE: remember to include WPCAP and HAVE_REMOTE among your
40 // preprocessor definitions.
47 main(int argc
, char **argv
)
49 pcap_if_t
*alldevs
, *d
;
52 char errbuf
[PCAP_ERRBUF_SIZE
];
54 struct pcap_pkthdr
*header
;
55 const u_char
*pkt_data
;
57 printf("pktdump_ex: prints the packets of the network using WinPcap.\n");
58 printf(" Usage: pktdump_ex [-s source]\n\n"
60 " pktdump_ex -s file://c:/temp/file.acp\n"
61 " pktdump_ex -s rpcap://\\Device\\NPF_{C8736017-F3C3-4373-94AC-9A34B7DAD998}\n\n");
66 printf("\nNo adapter selected: printing the device list:\n");
67 /* The user didn't provide a packet source: Retrieve the local device list */
68 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING
, NULL
, &alldevs
, errbuf
) == -1)
70 fprintf(stderr
,"Error in pcap_findalldevs_ex: %s\n", errbuf
);
75 for(d
=alldevs
; d
; d
=d
->next
)
77 printf("%d. %s\n ", ++i
, d
->name
);
80 printf(" (%s)\n", d
->description
);
82 printf(" (No description available)\n");
87 fprintf(stderr
,"No interfaces found! Exiting.\n");
91 printf("Enter the interface number (1-%d):",i
);
94 if (inum
< 1 || inum
> i
)
96 printf("\nInterface number out of range.\n");
98 /* Free the device list */
99 pcap_freealldevs(alldevs
);
103 /* Jump to the selected adapter */
104 for (d
=alldevs
, i
=0; i
< inum
-1 ;d
=d
->next
, i
++);
106 /* Open the device */
107 if ( (fp
= pcap_open(d
->name
,
109 PCAP_OPENFLAG_PROMISCUOUS
/*flags*/,
111 NULL
/* remote authentication */,
115 fprintf(stderr
,"\nError opening adapter\n");
121 // Do not check for the switch type ('-s')
122 if ( (fp
= pcap_open(argv
[2],
124 PCAP_OPENFLAG_PROMISCUOUS
/*flags*/,
126 NULL
/* remote authentication */,
130 fprintf(stderr
,"\nError opening source: %s\n", errbuf
);
135 /* Read the packets */
136 while((res
= pcap_next_ex( fp
, &header
, &pkt_data
)) >= 0)
140 /* Timeout elapsed */
143 /* print pkt timestamp and pkt len */
144 printf("%ld:%ld (%ld)\n", header
->ts
.tv_sec
, header
->ts
.tv_usec
, header
->len
);
146 /* Print the packet */
147 for (i
=1; (i
< header
->caplen
+ 1 ) ; i
++)
149 printf("%.2x ", pkt_data
[i
-1]);
150 if ( (i
% LINE_LEN
) == 0) printf("\n");
158 fprintf(stderr
, "Error reading the packets: %s\n", pcap_geterr(fp
));