pcap_fopen.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2008 CACE Technologies, Davis (California)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of CACE Technologies nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * This sample was contributed by
  31. * Marcin Okraszewski (Marcin.OkraszewskiATpl.compuware.com)
  32. *
  33. */
  34. #include <tchar.h>
  35. #include <pcap.h>
  36. #include <stdio.h>
  37. BOOL LoadNpcapDlls()
  38. {
  39. _TCHAR npcap_dir[512];
  40. UINT len;
  41. len = GetSystemDirectory(npcap_dir, 480);
  42. if (!len) {
  43. fprintf(stderr, "Error in GetSystemDirectory: %x", GetLastError());
  44. return FALSE;
  45. }
  46. _tcscat_s(npcap_dir, 512, _T("\\Npcap"));
  47. if (SetDllDirectory(npcap_dir) == 0) {
  48. fprintf(stderr, "Error in SetDllDirectory: %x", GetLastError());
  49. return FALSE;
  50. }
  51. return TRUE;
  52. }
  53. /** Prints packet timestaps regardless of format*/
  54. int _tmain(int argc, _TCHAR* argv[])
  55. {
  56. char errbuf[PCAP_ERRBUF_SIZE];
  57. _TCHAR cmd[1024];
  58. _TCHAR tshark_path[MAX_PATH];
  59. _TCHAR file_path[MAX_PATH];
  60. /* Load Npcap and its functions. */
  61. if (!LoadNpcapDlls())
  62. {
  63. fprintf(stderr, "Couldn't load Npcap\n");
  64. exit(1);
  65. }
  66. if ( argc != 3 ) {
  67. _tprintf(_T("Prints packet timestaps regardless of format.\n"));
  68. _tprintf(_T("Usage:\n\t%s <tshark path> <trace file>\n"), argv[0]);
  69. return 1;
  70. }
  71. // conversion to short path name in case there are spaces
  72. if ( ! GetShortPathName(argv[1], tshark_path, MAX_PATH) ||
  73. ! GetShortPathName(argv[2], file_path, MAX_PATH) )
  74. {
  75. _tprintf(_T("Failed to convert paths to short form."));
  76. return 1;
  77. }
  78. // create tshark command, which will make the trace conversion and print in libpcap format to stdout
  79. if ( _stprintf_s(cmd, 1024, _T("%s -r %s -w - -F libpcap"), tshark_path, file_path) < 0 ) {
  80. _tprintf(_T("Failed to create command\n"));
  81. return 1;
  82. }
  83. // start tshark
  84. FILE *tshark_out = _tpopen(cmd, _T("rb"));
  85. if ( tshark_out == NULL ) {
  86. strerror_s(errbuf, PCAP_ERRBUF_SIZE, errno);
  87. printf("Failed run tshark: %s\n", errbuf);
  88. _tprintf(_T("Command: %s"), cmd);
  89. return 1;
  90. }
  91. // open stdout from tshark
  92. pcap_t *pcap = pcap_fopen_offline(tshark_out, errbuf);
  93. if ( pcap == NULL ) {
  94. printf("Error opening stream from tshark: %s\n", errbuf);
  95. return 1;
  96. }
  97. // print information about every packet int trace
  98. struct pcap_pkthdr hdr;
  99. while ( pcap_next(pcap, &hdr) ) {
  100. printf("packet: ts: %u.%06u, len: %4u, caplen: %4u\n", hdr.ts.tv_sec, hdr.ts.tv_usec, hdr.len, hdr.caplen);
  101. }
  102. // clean up
  103. pcap_close(pcap);
  104. _pclose(tshark_out);
  105. return 0;
  106. }