dump_frame.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @file dump_frame.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  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 the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Function for dumping an Ethernet frame to a file in pcap format, used
  32. * for debugging (e.g. for analyzing with Wireshark).
  33. */
  34. #ifndef BADVPN_MISC_DUMP_FRAME_H
  35. #define BADVPN_MISC_DUMP_FRAME_H
  36. #include <stdint.h>
  37. #include <stddef.h>
  38. #include <stdio.h>
  39. struct pcap_hdr {
  40. uint32_t magic_number;
  41. uint16_t version_major;
  42. uint16_t version_minor;
  43. int32_t thiszone;
  44. uint32_t sigfigs;
  45. uint32_t snaplen;
  46. uint32_t network;
  47. } __attribute__((packed));
  48. struct pcaprec_hdr {
  49. uint32_t ts_sec;
  50. uint32_t ts_usec;
  51. uint32_t incl_len;
  52. uint32_t orig_len;
  53. } __attribute__((packed));
  54. static int write_to_file (uint8_t *data, size_t data_len, FILE *f)
  55. {
  56. while (data_len > 0) {
  57. size_t bytes = fwrite(data, 1, data_len, f);
  58. if (bytes == 0) {
  59. return 0;
  60. }
  61. data += bytes;
  62. data_len -= bytes;
  63. }
  64. return 1;
  65. }
  66. static int dump_frame (uint8_t *data, size_t data_len, const char *file)
  67. {
  68. FILE *f = fopen(file, "w");
  69. if (!f) {
  70. goto fail0;
  71. }
  72. struct pcap_hdr gh;
  73. gh.magic_number = 0xa1b2c3d4;
  74. gh.version_major = 2;
  75. gh.version_minor = 4;
  76. gh.thiszone = 0;
  77. gh.sigfigs = 0;
  78. gh.snaplen = 65535;
  79. gh.network = 1;
  80. if (!write_to_file((uint8_t *)&gh, sizeof(gh), f)) {
  81. goto fail1;
  82. }
  83. struct pcaprec_hdr ph;
  84. ph.ts_sec = 0;
  85. ph.ts_usec = 0;
  86. ph.incl_len = data_len;
  87. ph.orig_len = data_len;
  88. if (!write_to_file((uint8_t *)&ph, sizeof(ph), f)) {
  89. goto fail1;
  90. }
  91. if (!write_to_file(data, data_len, f)) {
  92. goto fail1;
  93. }
  94. if (fclose(f) != 0) {
  95. return 0;
  96. }
  97. return 1;
  98. fail1:
  99. fclose(f);
  100. fail0:
  101. return 0;
  102. }
  103. #endif