Main Page   Modules   Data Structures   File List   Data Fields   Globals  

Pktdump.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999 - 2002
00003  *  Politecnico di Torino.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that: (1) source code distributions
00007  * retain the above copyright notice and this paragraph in its entirety, (2)
00008  * distributions including binary code include the above copyright notice and
00009  * this paragraph in its entirety in the documentation or other materials
00010  * provided with the distribution, and (3) all advertising materials mentioning
00011  * features or use of this software display the following acknowledgement:
00012  * ``This product includes software developed by the Politecnico
00013  * di Torino, and its contributors.'' Neither the name of
00014  * the University nor the names of its contributors may be used to endorse
00015  * or promote products derived from this software without specific prior
00016  * written permission.
00017  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  */
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 
00025 #include <pcap.h>
00026 
00027 #define LINE_LEN 16
00028 
00029 
00030 void dispatcher_handler(u_char *, 
00031     const struct pcap_pkthdr *, const u_char *);
00032 
00033 main(int argc, char **argv) {
00034     
00035     pcap_if_t *alldevs, *d;
00036     pcap_t *fp;
00037     int inum, i=0;
00038     char errbuf[PCAP_ERRBUF_SIZE];
00039 
00040     printf("pktdump: prints the packets of the network using WinPcap.\n");
00041     printf("\t Usage: pktdump [-n adapter] | [-f file_name]\n\n");
00042 
00043     if(argc < 3){
00044 
00045         /* The user didn't provide a packet source: Retrieve the device list */
00046         if (pcap_findalldevs(&alldevs, errbuf) == -1)
00047         {
00048             fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
00049             exit(1);
00050         }
00051         
00052         /* Print the list */
00053         for(d=alldevs; d; d=d->next)
00054         {
00055             printf("%d. %s", ++i, d->name);
00056             if (d->description)
00057                 printf(" (%s)\n", d->description);
00058             else
00059                 printf(" (No description available)\n");
00060         }
00061         
00062         if(i==0)
00063         {
00064             printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
00065             return -1;
00066         }
00067         
00068         printf("Enter the interface number (1-%d):",i);
00069         scanf("%d", &inum);
00070         
00071         if(inum < 1 || inum > i)
00072         {
00073             printf("\nInterface number out of range.\n");
00074             /* Free the device list */
00075             pcap_freealldevs(alldevs);
00076             return -1;
00077         }
00078         
00079         /* Jump to the selected adapter */
00080         for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
00081         
00082         /* Open the device */
00083         if ( (fp= pcap_open_live(d->name, 100, 1, 20, errbuf) ) == NULL)
00084         {
00085             fprintf(stderr,"\nError opening adapter\n");
00086             return -1;
00087         }
00088     }
00089     else{
00090         
00091         /* The user provided a packet source: open it */
00092         switch (argv[1] [1])
00093         {
00094             
00095         case 'n':
00096             {
00097                 /* Open a physical device */
00098                 if ( (fp= pcap_open_live(argv[2], 100, 1, 20, errbuf) ) == NULL)
00099                 {
00100                     fprintf(stderr,"\nError opening adapter\n");
00101                     return -1;
00102                 }
00103             };
00104             break;
00105             
00106         case 'f':
00107             {
00108                 /* Open a capture file */
00109                 if ( (fp = pcap_open_offline(argv[2], NULL) ) == NULL)
00110                 {
00111                     fprintf(stderr,"\nError opening dump file\n");
00112                     return -1;
00113                 }
00114             };
00115             break;
00116         }
00117     }
00118 
00119     // read and dispatch packets until EOF is reached
00120     pcap_loop(fp, 0, dispatcher_handler, NULL);
00121 
00122     return 0;
00123 }
00124 
00125 
00126 
00127 void dispatcher_handler(u_char *temp1, 
00128                         const struct pcap_pkthdr *header, const u_char *pkt_data)
00129 {
00130     u_int i=0;
00131     
00132     /* print pkt timestamp and pkt len */
00133     printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);          
00134     
00135     /* Print the packet */
00136     for (i=1; (i < header->caplen + 1 ) ; i++)
00137     {
00138         printf("%.2x ", pkt_data[i-1]);
00139         if ( (i % LINE_LEN) == 0) printf("\n");
00140     }
00141     
00142     printf("\n\n");     
00143     
00144 }

documentation. Copyright (c) 2002 Politecnico di Torino. All rights reserved.