readpcap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * PCAP capture file reader
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include <pcap/pcap.h>
  16. #include "utils/common.h"
  17. #include "wlantest.h"
  18. int read_cap_file(struct wlantest *wt, const char *fname)
  19. {
  20. char errbuf[PCAP_ERRBUF_SIZE];
  21. pcap_t *pcap;
  22. unsigned int count = 0;
  23. struct pcap_pkthdr *hdr;
  24. const u_char *data;
  25. int res;
  26. pcap = pcap_open_offline(fname, errbuf);
  27. if (pcap == NULL) {
  28. wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
  29. fname, errbuf);
  30. return -1;
  31. }
  32. for (;;) {
  33. res = pcap_next_ex(pcap, &hdr, &data);
  34. if (res == -2)
  35. break; /* No more packets */
  36. if (res == -1) {
  37. wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
  38. pcap_geterr(pcap));
  39. break;
  40. }
  41. if (res != 1) {
  42. wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
  43. "value %d", res);
  44. break;
  45. }
  46. /* Packet was read without problems */
  47. wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
  48. "len=%u/%u",
  49. (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
  50. hdr->caplen, hdr->len);
  51. if (wt->write_pcap_dumper) {
  52. wt->write_pcap_time = hdr->ts;
  53. pcap_dump(wt->write_pcap_dumper, hdr, data);
  54. }
  55. if (hdr->caplen < hdr->len) {
  56. wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
  57. "(%u/%u captured)",
  58. hdr->caplen, hdr->len);
  59. continue;
  60. }
  61. count++;
  62. wlantest_process(wt, data, hdr->caplen);
  63. }
  64. pcap_close(pcap);
  65. wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
  66. return 0;
  67. }
  68. int read_wired_cap_file(struct wlantest *wt, const char *fname)
  69. {
  70. char errbuf[PCAP_ERRBUF_SIZE];
  71. pcap_t *pcap;
  72. unsigned int count = 0;
  73. struct pcap_pkthdr *hdr;
  74. const u_char *data;
  75. int res;
  76. pcap = pcap_open_offline(fname, errbuf);
  77. if (pcap == NULL) {
  78. wpa_printf(MSG_ERROR, "Failed to read pcap file '%s': %s",
  79. fname, errbuf);
  80. return -1;
  81. }
  82. for (;;) {
  83. res = pcap_next_ex(pcap, &hdr, &data);
  84. if (res == -2)
  85. break; /* No more packets */
  86. if (res == -1) {
  87. wpa_printf(MSG_INFO, "pcap_next_ex failure: %s",
  88. pcap_geterr(pcap));
  89. break;
  90. }
  91. if (res != 1) {
  92. wpa_printf(MSG_INFO, "Unexpected pcap_next_ex return "
  93. "value %d", res);
  94. break;
  95. }
  96. /* Packet was read without problems */
  97. wpa_printf(MSG_EXCESSIVE, "pcap hdr: ts=%d.%06d "
  98. "len=%u/%u",
  99. (int) hdr->ts.tv_sec, (int) hdr->ts.tv_usec,
  100. hdr->caplen, hdr->len);
  101. if (hdr->caplen < hdr->len) {
  102. wpa_printf(MSG_DEBUG, "pcap: Dropped incomplete frame "
  103. "(%u/%u captured)",
  104. hdr->caplen, hdr->len);
  105. continue;
  106. }
  107. count++;
  108. wlantest_process_wired(wt, data, hdr->caplen);
  109. }
  110. pcap_close(pcap);
  111. wpa_printf(MSG_DEBUG, "Read %s: %u packets", fname, count);
  112. return 0;
  113. }