dump_frame.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * @file dump_frame.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Function for dumping an Ethernet frame to a file in pcap format, used
  25. * for debugging (e.g. for analyzing with Wireshark).
  26. */
  27. #ifndef BADVPN_MISC_DUMP_FRAME_H
  28. #define BADVPN_MISC_DUMP_FRAME_H
  29. #include <stdint.h>
  30. #include <stddef.h>
  31. #include <stdio.h>
  32. struct pcap_hdr {
  33. uint32_t magic_number;
  34. uint16_t version_major;
  35. uint16_t version_minor;
  36. int32_t thiszone;
  37. uint32_t sigfigs;
  38. uint32_t snaplen;
  39. uint32_t network;
  40. } __attribute__((packed));
  41. struct pcaprec_hdr {
  42. uint32_t ts_sec;
  43. uint32_t ts_usec;
  44. uint32_t incl_len;
  45. uint32_t orig_len;
  46. } __attribute__((packed));
  47. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  48. {
  49. while (data_len > 0) {
  50. size_t bytes = fwrite(data, 1, data_len, f);
  51. if (bytes == 0) {
  52. return 0;
  53. }
  54. data += bytes;
  55. data_len -= bytes;
  56. }
  57. return 1;
  58. }
  59. static int dump_frame (uint8_t *data, size_t data_len, const char *file)
  60. {
  61. FILE *f = fopen(file, "w");
  62. if (!f) {
  63. goto fail0;
  64. }
  65. struct pcap_hdr gh;
  66. gh.magic_number = 0xa1b2c3d4;
  67. gh.version_major = 2;
  68. gh.version_minor = 4;
  69. gh.thiszone = 0;
  70. gh.sigfigs = 0;
  71. gh.snaplen = 65535;
  72. gh.network = 1;
  73. if (!write_to_file((uint8_t *)&gh, sizeof(gh), f)) {
  74. goto fail1;
  75. }
  76. struct pcaprec_hdr ph;
  77. ph.ts_sec = 0;
  78. ph.ts_usec = 0;
  79. ph.incl_len = data_len;
  80. ph.orig_len = data_len;
  81. if (!write_to_file((uint8_t *)&ph, sizeof(ph), f)) {
  82. goto fail1;
  83. }
  84. if (!write_to_file(data, data_len, f)) {
  85. goto fail1;
  86. }
  87. if (fclose(f) != 0) {
  88. return 0;
  89. }
  90. return 1;
  91. fail1:
  92. fclose(f);
  93. fail0:
  94. return 0;
  95. }
  96. #endif