readfile.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <stdio.h>
  2. #include <pcap.h>
  3. #include "misc.h"
  4. #define LINE_LEN 16
  5. void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
  6. int main(int argc, char **argv)
  7. {
  8. pcap_t *fp;
  9. char errbuf[PCAP_ERRBUF_SIZE];
  10. char source[PCAP_BUF_SIZE];
  11. /* Load Npcap and its functions. */
  12. if (!LoadNpcapDlls())
  13. {
  14. fprintf(stderr, "Couldn't load Npcap\n");
  15. exit(1);
  16. }
  17. if(argc != 2){
  18. printf("usage: %s filename", argv[0]);
  19. return -1;
  20. }
  21. /* Create the source string according to the new WinPcap syntax */
  22. if ( pcap_createsrcstr( source, // variable that will keep the source string
  23. PCAP_SRC_FILE, // we want to open a file
  24. NULL, // remote host
  25. NULL, // port on the remote host
  26. argv[1], // name of the file we want to open
  27. errbuf // error buffer
  28. ) != 0)
  29. {
  30. fprintf(stderr,"\nError creating a source string\n");
  31. return -1;
  32. }
  33. /* Open the capture file */
  34. if ( (fp= pcap_open(source, // name of the device
  35. 65536, // portion of the packet to capture
  36. // 65536 guarantees that the whole packet will be captured on all the link layers
  37. PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
  38. 1000, // read timeout
  39. NULL, // authentication on the remote machine
  40. errbuf // error buffer
  41. ) ) == NULL)
  42. {
  43. fprintf(stderr,"\nUnable to open the file %s.\n", source);
  44. return -1;
  45. }
  46. // read and dispatch packets until EOF is reached
  47. pcap_loop(fp, 0, dispatcher_handler, NULL);
  48. return 0;
  49. }
  50. void dispatcher_handler(u_char *temp1,
  51. const struct pcap_pkthdr *header, const u_char *pkt_data)
  52. {
  53. u_int i=0;
  54. /*
  55. * Unused variable
  56. */
  57. (VOID)temp1;
  58. /* print pkt timestamp and pkt len */
  59. printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
  60. /* Print the packet */
  61. for (i=1; (i < header->caplen + 1 ) ; i++)
  62. {
  63. printf("%.2x ", pkt_data[i-1]);
  64. if ( (i % LINE_LEN) == 0) printf("\n");
  65. }
  66. printf("\n\n");
  67. }