writepcap.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * PCAP capture file writer
  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 <pcap/bpf.h>
  17. #include "utils/common.h"
  18. #include "wlantest.h"
  19. int write_pcap_init(struct wlantest *wt, const char *fname)
  20. {
  21. wt->write_pcap = pcap_open_dead(DLT_IEEE802_11_RADIO, 4000);
  22. if (wt->write_pcap == NULL)
  23. return -1;
  24. wt->write_pcap_dumper = pcap_dump_open(wt->write_pcap, fname);
  25. if (wt->write_pcap_dumper == NULL) {
  26. pcap_close(wt->write_pcap);
  27. wt->write_pcap = NULL;
  28. return -1;
  29. }
  30. wpa_printf(MSG_DEBUG, "Writing PCAP dump to '%s'", fname);
  31. return 0;
  32. }
  33. void write_pcap_deinit(struct wlantest *wt)
  34. {
  35. if (wt->write_pcap_dumper) {
  36. pcap_dump_close(wt->write_pcap_dumper);
  37. wt->write_pcap_dumper = NULL;
  38. }
  39. if (wt->write_pcap) {
  40. pcap_close(wt->write_pcap);
  41. wt->write_pcap = NULL;
  42. }
  43. }
  44. void write_pcap_captured(struct wlantest *wt, const u8 *buf, size_t len)
  45. {
  46. struct pcap_pkthdr h;
  47. if (!wt->write_pcap_dumper)
  48. return;
  49. os_memset(&h, 0, sizeof(h));
  50. gettimeofday(&wt->write_pcap_time, NULL);
  51. h.ts = wt->write_pcap_time;
  52. h.caplen = len;
  53. h.len = len;
  54. pcap_dump(wt->write_pcap_dumper, &h, buf);
  55. }
  56. void write_pcap_decrypted(struct wlantest *wt, const u8 *buf1, size_t len1,
  57. const u8 *buf2, size_t len2)
  58. {
  59. struct pcap_pkthdr h;
  60. u8 rtap[] = {
  61. 0x00 /* rev */,
  62. 0x00 /* pad */,
  63. 0x08, 0x00, /* header len */
  64. 0x00, 0x00, 0x00, 0x00 /* present flags */
  65. };
  66. u8 *buf;
  67. size_t len;
  68. if (!wt->write_pcap_dumper)
  69. return;
  70. os_memset(&h, 0, sizeof(h));
  71. h.ts = wt->write_pcap_time;
  72. len = sizeof(rtap) + len1 + len2;
  73. buf = os_malloc(len);
  74. if (buf == NULL)
  75. return;
  76. os_memcpy(buf, rtap, sizeof(rtap));
  77. if (buf1) {
  78. os_memcpy(buf + sizeof(rtap), buf1, len1);
  79. buf[sizeof(rtap) + 1] &= ~0x40; /* Clear Protected flag */
  80. }
  81. if (buf2)
  82. os_memcpy(buf + sizeof(rtap) + len1, buf2, len2);
  83. h.caplen = len;
  84. h.len = len;
  85. pcap_dump(wt->write_pcap_dumper, &h, buf);
  86. os_free(buf);
  87. }