writepcap.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * PCAP capture file writer
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include <pcap.h>
  10. #include <pcap-bpf.h>
  11. #include "utils/common.h"
  12. #include "wlantest.h"
  13. int write_pcap_init(struct wlantest *wt, const char *fname)
  14. {
  15. wt->write_pcap = pcap_open_dead(DLT_IEEE802_11_RADIO, 4000);
  16. if (wt->write_pcap == NULL)
  17. return -1;
  18. wt->write_pcap_dumper = pcap_dump_open(wt->write_pcap, fname);
  19. if (wt->write_pcap_dumper == NULL) {
  20. pcap_close(wt->write_pcap);
  21. wt->write_pcap = NULL;
  22. return -1;
  23. }
  24. wpa_printf(MSG_DEBUG, "Writing PCAP dump to '%s'", fname);
  25. return 0;
  26. }
  27. void write_pcap_deinit(struct wlantest *wt)
  28. {
  29. if (wt->write_pcap_dumper) {
  30. pcap_dump_close(wt->write_pcap_dumper);
  31. wt->write_pcap_dumper = NULL;
  32. }
  33. if (wt->write_pcap) {
  34. pcap_close(wt->write_pcap);
  35. wt->write_pcap = NULL;
  36. }
  37. }
  38. void write_pcap_captured(struct wlantest *wt, const u8 *buf, size_t len)
  39. {
  40. struct pcap_pkthdr h;
  41. if (!wt->write_pcap_dumper)
  42. return;
  43. os_memset(&h, 0, sizeof(h));
  44. gettimeofday(&wt->write_pcap_time, NULL);
  45. h.ts = wt->write_pcap_time;
  46. h.caplen = len;
  47. h.len = len;
  48. pcap_dump(wt->write_pcap_dumper, &h, buf);
  49. }
  50. void write_pcap_decrypted(struct wlantest *wt, const u8 *buf1, size_t len1,
  51. const u8 *buf2, size_t len2)
  52. {
  53. struct pcap_pkthdr h;
  54. u8 rtap[] = {
  55. 0x00 /* rev */,
  56. 0x00 /* pad */,
  57. 0x08, 0x00, /* header len */
  58. 0x00, 0x00, 0x00, 0x00 /* present flags */
  59. };
  60. u8 *buf;
  61. size_t len;
  62. if (!wt->write_pcap_dumper)
  63. return;
  64. os_memset(&h, 0, sizeof(h));
  65. h.ts = wt->write_pcap_time;
  66. len = sizeof(rtap) + len1 + len2;
  67. buf = os_malloc(len);
  68. if (buf == NULL)
  69. return;
  70. os_memcpy(buf, rtap, sizeof(rtap));
  71. if (buf1) {
  72. os_memcpy(buf + sizeof(rtap), buf1, len1);
  73. buf[sizeof(rtap) + 1] &= ~0x40; /* Clear Protected flag */
  74. }
  75. if (buf2)
  76. os_memcpy(buf + sizeof(rtap) + len1, buf2, len2);
  77. h.caplen = len;
  78. h.len = len;
  79. pcap_dump(wt->write_pcap_dumper, &h, buf);
  80. os_free(buf);
  81. }