pycapng 0.6__tar.gz → 0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. {pycapng-0.6 → pycapng-0.7}/PKG-INFO +1 -1
  2. {pycapng-0.6 → pycapng-0.7}/bindings/python/pycapng.cpp +39 -7
  3. {pycapng-0.6 → pycapng-0.7}/bindings/python/pycapng.hpp +1 -0
  4. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/io.h +1 -0
  5. pycapng-0.7/lib/io.c +333 -0
  6. pycapng-0.6/lib/io.c +0 -128
  7. {pycapng-0.6 → pycapng-0.7}/.github/workflows/build.yml +0 -0
  8. {pycapng-0.6 → pycapng-0.7}/.github/workflows/release.yml +0 -0
  9. {pycapng-0.6 → pycapng-0.7}/.gitignore +0 -0
  10. {pycapng-0.6 → pycapng-0.7}/CMakeLists.txt +0 -0
  11. {pycapng-0.6 → pycapng-0.7}/LICENSE +0 -0
  12. {pycapng-0.6 → pycapng-0.7}/README-pypi.md +0 -0
  13. {pycapng-0.6 → pycapng-0.7}/README.md +0 -0
  14. {pycapng-0.6 → pycapng-0.7}/Tutorial.md +0 -0
  15. {pycapng-0.6 → pycapng-0.7}/Tutorial2.md +0 -0
  16. {pycapng-0.6 → pycapng-0.7}/VERSION +0 -0
  17. {pycapng-0.6 → pycapng-0.7}/bin/CMakeLists.txt +0 -0
  18. {pycapng-0.6 → pycapng-0.7}/bin/linenoise/linenoise.c +0 -0
  19. {pycapng-0.6 → pycapng-0.7}/bin/linenoise/linenoise.h +0 -0
  20. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh.c +0 -0
  21. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh.h +0 -0
  22. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh.md +0 -0
  23. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh_eval.c +0 -0
  24. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh_io.c +0 -0
  25. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh_layer.c +0 -0
  26. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh_main.c +0 -0
  27. {pycapng-0.6 → pycapng-0.7}/bin/pcapsh_posa.c +0 -0
  28. {pycapng-0.6 → pycapng-0.7}/bin/protos/ah.posa +0 -0
  29. {pycapng-0.6 → pycapng-0.7}/bin/protos/amqp.posa +0 -0
  30. {pycapng-0.6 → pycapng-0.7}/bin/protos/bacnet.posa +0 -0
  31. {pycapng-0.6 → pycapng-0.7}/bin/protos/bfd.posa +0 -0
  32. {pycapng-0.6 → pycapng-0.7}/bin/protos/bgp.posa +0 -0
  33. {pycapng-0.6 → pycapng-0.7}/bin/protos/cdp.posa +0 -0
  34. {pycapng-0.6 → pycapng-0.7}/bin/protos/coap.posa +0 -0
  35. {pycapng-0.6 → pycapng-0.7}/bin/protos/dccp.posa +0 -0
  36. {pycapng-0.6 → pycapng-0.7}/bin/protos/dhcpv6.posa +0 -0
  37. {pycapng-0.6 → pycapng-0.7}/bin/protos/dnp3.posa +0 -0
  38. {pycapng-0.6 → pycapng-0.7}/bin/protos/dot1q.posa +0 -0
  39. {pycapng-0.6 → pycapng-0.7}/bin/protos/eap.posa +0 -0
  40. {pycapng-0.6 → pycapng-0.7}/bin/protos/eapol.posa +0 -0
  41. {pycapng-0.6 → pycapng-0.7}/bin/protos/eapol_key.posa +0 -0
  42. {pycapng-0.6 → pycapng-0.7}/bin/protos/eigrp.posa +0 -0
  43. {pycapng-0.6 → pycapng-0.7}/bin/protos/esp.posa +0 -0
  44. {pycapng-0.6 → pycapng-0.7}/bin/protos/hsrp.posa +0 -0
  45. {pycapng-0.6 → pycapng-0.7}/bin/protos/icmpv6.posa +0 -0
  46. {pycapng-0.6 → pycapng-0.7}/bin/protos/igmp.posa +0 -0
  47. {pycapng-0.6 → pycapng-0.7}/bin/protos/ikev2.posa +0 -0
  48. {pycapng-0.6 → pycapng-0.7}/bin/protos/ipv6.posa +0 -0
  49. {pycapng-0.6 → pycapng-0.7}/bin/protos/isakmp.posa +0 -0
  50. {pycapng-0.6 → pycapng-0.7}/bin/protos/kerberos.posa +0 -0
  51. {pycapng-0.6 → pycapng-0.7}/bin/protos/l2tp.posa +0 -0
  52. {pycapng-0.6 → pycapng-0.7}/bin/protos/lacp.posa +0 -0
  53. {pycapng-0.6 → pycapng-0.7}/bin/protos/llc.posa +0 -0
  54. {pycapng-0.6 → pycapng-0.7}/bin/protos/modbus_tcp.posa +0 -0
  55. {pycapng-0.6 → pycapng-0.7}/bin/protos/mpls.posa +0 -0
  56. {pycapng-0.6 → pycapng-0.7}/bin/protos/mqtt.posa +0 -0
  57. {pycapng-0.6 → pycapng-0.7}/bin/protos/nbns.posa +0 -0
  58. {pycapng-0.6 → pycapng-0.7}/bin/protos/netflow_v5.posa +0 -0
  59. {pycapng-0.6 → pycapng-0.7}/bin/protos/openflow.posa +0 -0
  60. {pycapng-0.6 → pycapng-0.7}/bin/protos/ospf.posa +0 -0
  61. {pycapng-0.6 → pycapng-0.7}/bin/protos/pim.posa +0 -0
  62. {pycapng-0.6 → pycapng-0.7}/bin/protos/ppp.posa +0 -0
  63. {pycapng-0.6 → pycapng-0.7}/bin/protos/pppoe.posa +0 -0
  64. {pycapng-0.6 → pycapng-0.7}/bin/protos/ptpv2.posa +0 -0
  65. {pycapng-0.6 → pycapng-0.7}/bin/protos/rdp.posa +0 -0
  66. {pycapng-0.6 → pycapng-0.7}/bin/protos/ripv2.posa +0 -0
  67. {pycapng-0.6 → pycapng-0.7}/bin/protos/rsvp.posa +0 -0
  68. {pycapng-0.6 → pycapng-0.7}/bin/protos/rtp.posa +0 -0
  69. {pycapng-0.6 → pycapng-0.7}/bin/protos/sctp.posa +0 -0
  70. {pycapng-0.6 → pycapng-0.7}/bin/protos/snap.posa +0 -0
  71. {pycapng-0.6 → pycapng-0.7}/bin/protos/socks5.posa +0 -0
  72. {pycapng-0.6 → pycapng-0.7}/bin/protos/stp.posa +0 -0
  73. {pycapng-0.6 → pycapng-0.7}/bin/protos/stun.posa +0 -0
  74. {pycapng-0.6 → pycapng-0.7}/bin/protos/vrrp.posa +0 -0
  75. {pycapng-0.6 → pycapng-0.7}/bin/protos/wol.posa +0 -0
  76. {pycapng-0.6 → pycapng-0.7}/bin/tests/run_tests.sh +0 -0
  77. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_cstring_security.pcapsh +0 -0
  78. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_dns.pcapsh +0 -0
  79. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_forloop.pcapsh +0 -0
  80. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_inline_protocol.pcapsh +0 -0
  81. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_posa_custom.pcapsh +0 -0
  82. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_protocols.pcapsh +0 -0
  83. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_raw_inspect.pcapsh +0 -0
  84. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_tcp_session.pcapsh +0 -0
  85. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_tftp.pcapsh +0 -0
  86. {pycapng-0.6 → pycapng-0.7}/bin/tests/test_wireshark_samples.pcapsh +0 -0
  87. {pycapng-0.6 → pycapng-0.7}/bin/tests/tftp_protos.posa +0 -0
  88. {pycapng-0.6 → pycapng-0.7}/bindings/CMakeLists.txt +0 -0
  89. {pycapng-0.6 → pycapng-0.7}/bindings/python/CMakeLists.txt +0 -0
  90. {pycapng-0.6 → pycapng-0.7}/bindings/python/embed_protos.py +0 -0
  91. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/capture_tui.py +0 -0
  92. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/live_capture.py +0 -0
  93. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/pcapsh_callback.py +0 -0
  94. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/pcapsh_custom_protocol.py +0 -0
  95. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/pcapsh_inline.py +0 -0
  96. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/pcapsh_run_script.py +0 -0
  97. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/pcapsh_tls_https.py +0 -0
  98. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/read.py +0 -0
  99. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/write_rdp.py +0 -0
  100. {pycapng-0.6 → pycapng-0.7}/bindings/python/examples/write_tcp.py +0 -0
  101. {pycapng-0.6 → pycapng-0.7}/bindings/python/pycapture.cpp +0 -0
  102. {pycapng-0.6 → pycapng-0.7}/bindings/python/pypcapsh.cpp +0 -0
  103. {pycapng-0.6 → pycapng-0.7}/bindings/python/tests/__init__.py +0 -0
  104. {pycapng-0.6 → pycapng-0.7}/bindings/python/tests/test_pcapsh.py +0 -0
  105. {pycapng-0.6 → pycapng-0.7}/lib/CMakeLists.txt +0 -0
  106. {pycapng-0.6 → pycapng-0.7}/lib/blocks.c +0 -0
  107. {pycapng-0.6 → pycapng-0.7}/lib/capture.c +0 -0
  108. {pycapng-0.6 → pycapng-0.7}/lib/dissect.c +0 -0
  109. {pycapng-0.6 → pycapng-0.7}/lib/easyapi.c +0 -0
  110. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/blocks.h +0 -0
  111. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/capture.h +0 -0
  112. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/dissect.h +0 -0
  113. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/easyapi.h +0 -0
  114. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/libpcapng.h +0 -0
  115. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/linktypes.h +0 -0
  116. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/objects.h +0 -0
  117. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/posa.h +0 -0
  118. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/asn1.h +0 -0
  119. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/bootp.h +0 -0
  120. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/dhcp.h +0 -0
  121. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/dns.h +0 -0
  122. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/ethernet.h +0 -0
  123. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/flow.h +0 -0
  124. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/http2.h +0 -0
  125. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/http2_hpack.h +0 -0
  126. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/http2_stream.h +0 -0
  127. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/icmp.h +0 -0
  128. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/ipv4.h +0 -0
  129. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/ntp.h +0 -0
  130. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/rdp.h +0 -0
  131. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/ssh.h +0 -0
  132. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/ssl.h +0 -0
  133. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/tcp.h +0 -0
  134. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/tcp_mss.h +0 -0
  135. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/tls_stream.h +0 -0
  136. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/protocols/udp.h +0 -0
  137. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/reassembly.h +0 -0
  138. {pycapng-0.6 → pycapng-0.7}/lib/include/libpcapng/reassembly_tcp.h +0 -0
  139. {pycapng-0.6 → pycapng-0.7}/lib/objects.c +0 -0
  140. {pycapng-0.6 → pycapng-0.7}/lib/posa.c +0 -0
  141. {pycapng-0.6 → pycapng-0.7}/lib/protocols/asn1.c +0 -0
  142. {pycapng-0.6 → pycapng-0.7}/lib/protocols/dhcp.c +0 -0
  143. {pycapng-0.6 → pycapng-0.7}/lib/protocols/dns.c +0 -0
  144. {pycapng-0.6 → pycapng-0.7}/lib/protocols/ethernet.c +0 -0
  145. {pycapng-0.6 → pycapng-0.7}/lib/protocols/flow.c +0 -0
  146. {pycapng-0.6 → pycapng-0.7}/lib/protocols/http2.c +0 -0
  147. {pycapng-0.6 → pycapng-0.7}/lib/protocols/http2_hpack.c +0 -0
  148. {pycapng-0.6 → pycapng-0.7}/lib/protocols/http2_stream.c +0 -0
  149. {pycapng-0.6 → pycapng-0.7}/lib/protocols/icmp.c +0 -0
  150. {pycapng-0.6 → pycapng-0.7}/lib/protocols/ipv4.c +0 -0
  151. {pycapng-0.6 → pycapng-0.7}/lib/protocols/ntp.c +0 -0
  152. {pycapng-0.6 → pycapng-0.7}/lib/protocols/rdp.c +0 -0
  153. {pycapng-0.6 → pycapng-0.7}/lib/protocols/ssh.c +0 -0
  154. {pycapng-0.6 → pycapng-0.7}/lib/protocols/ssl.c +0 -0
  155. {pycapng-0.6 → pycapng-0.7}/lib/protocols/tcp.c +0 -0
  156. {pycapng-0.6 → pycapng-0.7}/lib/protocols/tcp_mss.c +0 -0
  157. {pycapng-0.6 → pycapng-0.7}/lib/protocols/tls_stream.c +0 -0
  158. {pycapng-0.6 → pycapng-0.7}/lib/protocols/udp.c +0 -0
  159. {pycapng-0.6 → pycapng-0.7}/lib/reassembly.c +0 -0
  160. {pycapng-0.6 → pycapng-0.7}/lib/reassembly_tcp.c +0 -0
  161. {pycapng-0.6 → pycapng-0.7}/lib/tests/CMakeLists.txt +0 -0
  162. {pycapng-0.6 → pycapng-0.7}/lib/tests/dhcp-write.c +0 -0
  163. {pycapng-0.6 → pycapng-0.7}/lib/tests/dns-write.c +0 -0
  164. {pycapng-0.6 → pycapng-0.7}/lib/tests/ntp-write.c +0 -0
  165. {pycapng-0.6 → pycapng-0.7}/lib/tests/pcapng-spec.c +0 -0
  166. {pycapng-0.6 → pycapng-0.7}/lib/tests/rdp-write.c +0 -0
  167. {pycapng-0.6 → pycapng-0.7}/lib/tests/read.c +0 -0
  168. {pycapng-0.6 → pycapng-0.7}/lib/tests/reasm.c +0 -0
  169. {pycapng-0.6 → pycapng-0.7}/lib/tests/ssl-http2-write.c +0 -0
  170. {pycapng-0.6 → pycapng-0.7}/lib/tests/tcp-sequence.c +0 -0
  171. {pycapng-0.6 → pycapng-0.7}/lib/tests/writeall.c +0 -0
  172. {pycapng-0.6 → pycapng-0.7}/libpcapng.pc.cmake +0 -0
  173. {pycapng-0.6 → pycapng-0.7}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: pycapng
3
- Version: 0.6
3
+ Version: 0.7
4
4
  Summary: Python bindings for libpcapng — read/write pcapng files and live packet capture
5
5
  Keywords: pcapng,pcap,packet-capture,network,packet,wireshark
6
6
  Author-Email: Seb Tricaud <sebastien.tricaud@proton.me>
@@ -681,16 +681,39 @@ int PcapNG::foreach_packet_cb(uint32_t block_counter, uint32_t block_type, uint3
681
681
  {
682
682
  py::object cb_func = *(py::object *)userdata;
683
683
 
684
+ if (block_type == PCAPNG_ENHANCED_PACKET_BLOCK) {
685
+ /*
686
+ * EPB body (after block_type + block_total_length):
687
+ * interface_id(4) ts_high(4) ts_low(4) cap_len(4) orig_len(4) pkt_data cap_len bytes
688
+ *
689
+ * Pass the full EPB light struct + exact cap_len payload bytes to Python.
690
+ * interface_id is included so callers can dispatch on the correct interface linktype.
691
+ * Using captured_packet_length directly avoids libpcapng_padded_count, which
692
+ * incorrectly strips payload bytes whose values happen to be 0x00.
693
+ */
694
+ pcapng_enhanced_packet_block_light_t *epb = (pcapng_enhanced_packet_block_light_t *)data;
695
+ uint32_t payload_size = (uint32_t)sizeof(*epb) + epb->captured_packet_length;
696
+ cb_func(block_counter, block_type, block_total_length,
697
+ py::bytes((const char *)data, payload_size));
698
+ return 0;
699
+ }
700
+
701
+ if (block_type == PCAPNG_INTERFACE_DESCRIPTION_BLOCK) {
702
+ /*
703
+ * IDB body: linktype(2) reserved(2) snaplen(4) [options] trailing_btl(4)
704
+ * Pass just the fixed 8-byte struct so callers can read linktype at offset 0.
705
+ */
706
+ cb_func(block_counter, block_type, block_total_length,
707
+ py::bytes((const char *)data, sizeof(pcapng_interface_description_block_light_t)));
708
+ return 0;
709
+ }
710
+
711
+ // For other block types (SHB, CDB, …) use the generic CDB-based extraction.
684
712
  uint32_t start_offset = libpcapng_custom_data_block_start_offset();
685
713
  uint32_t data_length = libpcapng_custom_data_block_data_length(block_total_length);
686
-
687
714
  int padded = libpcapng_padded_count(&data[start_offset], data_length);
688
-
689
- // The function sometimes cut the last }
690
-
691
- // FIXME: We just send the data but we will need to make this an object where we can retrieve the pen etc.
692
- cb_func(block_counter, block_type, block_total_length, py::bytes((const char *)&data[start_offset],
693
- data_length - padded));
715
+ cb_func(block_counter, block_type, block_total_length,
716
+ py::bytes((const char *)&data[start_offset], data_length - padded));
694
717
 
695
718
  return 0;
696
719
  }
@@ -700,6 +723,13 @@ int PcapNG::ForeachPacket(const py::object &func)
700
723
  return libpcapng_fp_read(_fp, foreach_packet_cb, (void *)&func);
701
724
  }
702
725
 
726
+ int PcapNG::ForeachMem(py::bytes data, const py::object &func)
727
+ {
728
+ std::string s = data;
729
+ return libpcapng_mem_read((unsigned char *)s.data(), s.size(),
730
+ foreach_packet_cb, (void *)&func);
731
+ }
732
+
703
733
  /* ── RDP helpers ────────────────────────────────────────────────────────── */
704
734
 
705
735
  static void parse_mac_or_throw(const std::string &mac_str, uint8_t out[6])
@@ -1093,6 +1123,8 @@ PYBIND11_MODULE(pycapng, m) {
1093
1123
  .def("BuildTlsApplicationData", &PcapNG::BuildTlsApplicationData)
1094
1124
  .def("WritePacketTime", &PcapNG::WritePacketTime)
1095
1125
  .def("ForeachPacket", &PcapNG::ForeachPacket)
1126
+ .def("ForeachMem", &PcapNG::ForeachMem,
1127
+ "Parse pcap or pcapng bytes from memory and call func(idx, block_type, block_len, data) for each block.")
1096
1128
  /* RDP packet builders */
1097
1129
  .def("BuildRdpConnectionRequest", &PcapNG::BuildRdpConnectionRequest,
1098
1130
  py::arg("src_mac"), py::arg("dst_mac"),
@@ -102,6 +102,7 @@ public:
102
102
  int WritePacketTime(py::bytes data, uint32_t timestamp);
103
103
  int WriteCustom(uint32_t pen, py::bytes data, const std::string &comment);
104
104
  int ForeachPacket(const py::object &func);
105
+ int ForeachMem(py::bytes data, const py::object &func);
105
106
 
106
107
  /* ── RDP ── */
107
108
  py::bytes BuildRdpConnectionRequest(const std::string &src_mac,
@@ -17,6 +17,7 @@ extern "C" {
17
17
 
18
18
  typedef int (*foreach_pcapng_block_cb)(uint32_t block_counter, uint32_t block_type, uint32_t block_total_length, unsigned char *data, void *userdata);
19
19
 
20
+ int libpcapng_mem_read(unsigned char *buf, size_t buf_len, foreach_pcapng_block_cb pcapng_block_cb, void *userdata);
20
21
  int libpcapng_fp_read(FILE *fp, foreach_pcapng_block_cb pcapng_block_cb, void *userdata);
21
22
  int libpcapng_file_read(char *filename, foreach_pcapng_block_cb pcapng_block_cb, void *userdata);
22
23
  int libpcapng_file_read_debug(char *filename);
pycapng-0.7/lib/io.c ADDED
@@ -0,0 +1,333 @@
1
+ /*
2
+ * License MIT
3
+ * Copyright (c) 2021 Devo Inc.
4
+ * Copyright (c) 2022 Sebastien Tricaud
5
+ */
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <stdint.h>
10
+
11
+ #include <libpcapng/blocks.h>
12
+
13
+ #include <libpcapng/io.h>
14
+
15
+ /* ── Classic pcap helpers ────────────────────────────────────────────────── */
16
+
17
+ /*
18
+ * Classic pcap magic values (native-endian after memcpy into uint32_t):
19
+ * 0xa1b2c3d4 microsecond LE file on LE host (or BE file on BE host)
20
+ * 0xd4c3b2a1 microsecond BE file on LE host (or LE file on BE host)
21
+ * 0xa1b23c4d nanosecond LE file on LE host
22
+ * 0x4d3cb2a1 nanosecond BE file on LE host
23
+ */
24
+ #define PCAP_MAGIC_NATIVE 0xa1b2c3d4u
25
+ #define PCAP_MAGIC_SWAPPED 0xd4c3b2a1u
26
+ #define PCAP_MAGIC_NS_NAT 0xa1b23c4du
27
+ #define PCAP_MAGIC_NS_SWP 0x4d3cb2a1u
28
+
29
+ static uint16_t _pcap_swap16(uint16_t v) {
30
+ return (uint16_t)((v >> 8) | (v << 8));
31
+ }
32
+ static uint32_t _pcap_swap32(uint32_t v) {
33
+ return ((v & 0x000000FFu) << 24) | ((v & 0x0000FF00u) << 8) |
34
+ ((v & 0x00FF0000u) >> 8) | ((v & 0xFF000000u) >> 24);
35
+ }
36
+ static uint32_t _pcap_u32(const unsigned char *p, int swap) {
37
+ uint32_t v; memcpy(&v, p, 4); return swap ? _pcap_swap32(v) : v;
38
+ }
39
+ static uint16_t _pcap_u16(const unsigned char *p, int swap) {
40
+ uint16_t v; memcpy(&v, p, 2); return swap ? _pcap_swap16(v) : v;
41
+ }
42
+
43
+ /* Returns 1 if buf starts with a classic pcap magic; sets *need_swap. */
44
+ static int _pcap_classic_magic(const unsigned char *buf, size_t buf_len, int *need_swap)
45
+ {
46
+ uint32_t magic;
47
+ if (buf_len < 4) return 0;
48
+ memcpy(&magic, buf, 4);
49
+ if (magic == PCAP_MAGIC_NATIVE || magic == PCAP_MAGIC_NS_NAT) { *need_swap = 0; return 1; }
50
+ if (magic == PCAP_MAGIC_SWAPPED || magic == PCAP_MAGIC_NS_SWP) { *need_swap = 1; return 1; }
51
+ return 0;
52
+ }
53
+
54
+ /*
55
+ * Emit a synthetic IDB callback and then one EPB callback per packet.
56
+ * Used by both the file and the memory readers.
57
+ */
58
+ static void _pcap_emit_idb(uint32_t linktype, uint32_t snaplen,
59
+ foreach_pcapng_block_cb cb, void *userdata)
60
+ {
61
+ /* IDB body: linktype(2) reserved(2) snaplen(4) trailing_btl(4) */
62
+ unsigned char idb[12];
63
+ uint16_t lt16 = (uint16_t)(linktype & 0xffffu), rsv = 0;
64
+ uint32_t idb_btl = 20; /* 4+4+2+2+4+4 */
65
+ memcpy(idb + 0, &lt16, 2);
66
+ memcpy(idb + 2, &rsv, 2);
67
+ memcpy(idb + 4, &snaplen, 4);
68
+ memcpy(idb + 8, &idb_btl, 4);
69
+ cb(0, PCAPNG_INTERFACE_DESCRIPTION_BLOCK, idb_btl, idb, userdata);
70
+ }
71
+
72
+ static int _pcap_emit_epb(uint32_t ts_sec, uint32_t ts_usec,
73
+ const unsigned char *pkt, uint32_t incl_len, uint32_t orig_len,
74
+ uint64_t counter, foreach_pcapng_block_cb cb, void *userdata)
75
+ {
76
+ /* EPB body: iface_id(4) ts_hi(4) ts_lo(4) cap_len(4) orig_len(4)
77
+ * pkt_data(incl_len) pad trailing_btl(4) */
78
+ uint32_t pad = (4 - (incl_len % 4)) % 4;
79
+ uint32_t epb_btl = 8 + 20 + incl_len + pad + 4;
80
+ uint32_t data_sz = epb_btl - 8;
81
+
82
+ unsigned char *epb = (unsigned char *)malloc(data_sz);
83
+ if (!epb) return -1;
84
+
85
+ uint32_t iface = 0;
86
+ memcpy(epb + 0, &iface, 4);
87
+ memcpy(epb + 4, &ts_sec, 4);
88
+ memcpy(epb + 8, &ts_usec, 4);
89
+ memcpy(epb + 12, &incl_len, 4);
90
+ memcpy(epb + 16, &orig_len, 4);
91
+ memcpy(epb + 20, pkt, incl_len);
92
+ memset(epb + 20 + incl_len, 0, pad);
93
+ memcpy(epb + 20 + incl_len + pad, &epb_btl, 4);
94
+
95
+ cb((uint32_t)counter, PCAPNG_ENHANCED_PACKET_BLOCK, epb_btl, epb, userdata);
96
+ free(epb);
97
+ return 0;
98
+ }
99
+
100
+ /* ── Classic pcap from memory ────────────────────────────────────────────── */
101
+
102
+ static int _mem_read_classic_pcap(const unsigned char *buf, size_t buf_len,
103
+ foreach_pcapng_block_cb cb, void *userdata)
104
+ {
105
+ if (buf_len < 24) return -1;
106
+ int swap;
107
+ if (!_pcap_classic_magic(buf, buf_len, &swap)) return -1;
108
+
109
+ uint32_t snaplen = _pcap_u32(buf + 16, swap);
110
+ uint32_t linktype = _pcap_u32(buf + 20, swap);
111
+ _pcap_emit_idb(linktype, snaplen, cb, userdata);
112
+
113
+ size_t off = 24;
114
+ uint64_t ctr = 1;
115
+ while (off + 16 <= buf_len) {
116
+ uint32_t ts_sec = _pcap_u32(buf + off + 0, swap);
117
+ uint32_t ts_usec = _pcap_u32(buf + off + 4, swap);
118
+ uint32_t incl_len = _pcap_u32(buf + off + 8, swap);
119
+ uint32_t orig_len = _pcap_u32(buf + off + 12, swap);
120
+ off += 16;
121
+ if (incl_len > 65535 || off + incl_len > buf_len) break;
122
+ _pcap_emit_epb(ts_sec, ts_usec, buf + off, incl_len, orig_len, ctr, cb, userdata);
123
+ off += incl_len;
124
+ ctr++;
125
+ }
126
+ return 0;
127
+ }
128
+
129
+ /* ── pcapng from memory ──────────────────────────────────────────────────── */
130
+
131
+ static int _mem_read_pcapng(const unsigned char *buf, size_t buf_len,
132
+ foreach_pcapng_block_cb cb, void *userdata)
133
+ {
134
+ size_t off = 0;
135
+ uint64_t ctr = 1;
136
+ while (off + 8 <= buf_len) {
137
+ uint32_t block_type, block_total_length;
138
+ memcpy(&block_type, buf + off, 4);
139
+ memcpy(&block_total_length, buf + off + 4, 4);
140
+ if (block_total_length < 12 || off + block_total_length > buf_len) break;
141
+ cb((uint32_t)ctr, block_type, block_total_length,
142
+ (unsigned char *)(buf + off + 8), userdata);
143
+ off += block_total_length;
144
+ ctr++;
145
+ }
146
+ return 0;
147
+ }
148
+
149
+ /* ── Classic pcap from FILE* ─────────────────────────────────────────────── */
150
+
151
+ static int _fp_read_classic_pcap(FILE *fp, foreach_pcapng_block_cb cb, void *userdata)
152
+ {
153
+ unsigned char global_hdr[24];
154
+ if (fread(global_hdr, 1, 24, fp) != 24) return -1;
155
+
156
+ int swap;
157
+ if (!_pcap_classic_magic(global_hdr, 24, &swap)) return -1;
158
+
159
+ uint32_t snaplen = _pcap_u32(global_hdr + 16, swap);
160
+ uint32_t linktype = _pcap_u32(global_hdr + 20, swap);
161
+ _pcap_emit_idb(linktype, snaplen, cb, userdata);
162
+
163
+ uint8_t pkt_hdr[16];
164
+ uint64_t ctr = 1;
165
+ unsigned char *pkt_buf = NULL;
166
+ uint32_t pkt_buf_cap = 0;
167
+
168
+ while (fread(pkt_hdr, 1, 16, fp) == 16) {
169
+ uint32_t ts_sec = _pcap_u32(pkt_hdr + 0, swap);
170
+ uint32_t ts_usec = _pcap_u32(pkt_hdr + 4, swap);
171
+ uint32_t incl_len = _pcap_u32(pkt_hdr + 8, swap);
172
+ uint32_t orig_len = _pcap_u32(pkt_hdr + 12, swap);
173
+ if (incl_len > 65535) break;
174
+ if (incl_len > pkt_buf_cap) {
175
+ free(pkt_buf);
176
+ pkt_buf = (unsigned char *)malloc(incl_len);
177
+ if (!pkt_buf) return -1;
178
+ pkt_buf_cap = incl_len;
179
+ }
180
+ if (fread(pkt_buf, 1, incl_len, fp) != incl_len) break;
181
+ _pcap_emit_epb(ts_sec, ts_usec, pkt_buf, incl_len, orig_len, ctr, cb, userdata);
182
+ ctr++;
183
+ }
184
+ free(pkt_buf);
185
+ return 0;
186
+ }
187
+
188
+ int foreach_pcapng_block(uint32_t block_counter, uint32_t block_type, uint32_t block_total_length, unsigned char *data, void *userdata)
189
+ {
190
+
191
+ switch (block_type) {
192
+ case PCAPNG_SECTION_HEADER_BLOCK: {
193
+ /* printf("Section Header Block\n"); */
194
+ pcapng_section_header_block_light_t *shb;
195
+ shb = libpcapng_section_header_block_read(data, block_total_length);
196
+ }
197
+ break;
198
+ case PCAPNG_CUSTOM_DATA_BLOCK: {
199
+ /* printf("Custom Data Block\n"); */
200
+ pcapng_custom_data_block_light_t *cb;
201
+ cb = libpcapng_custom_data_block_read(data, block_total_length);
202
+ }
203
+ break;
204
+ case PCAPNG_ENHANCED_PACKET_BLOCK: {
205
+ /* printf("PCAPNG_ENHANCED_PACKET_BLOCK\n"); */
206
+ pcapng_enhanced_packet_block_light_t *epb;
207
+ epb = libpcapng_enhanced_packet_block_read(data, block_total_length);
208
+ }
209
+ break;
210
+ case PCAPNG_INTERFACE_DESCRIPTION_BLOCK:
211
+ printf("Interface Description Block\n");
212
+ break;
213
+ default:
214
+ fprintf(stderr, "Block type %x not handled yet!\n", block_type);
215
+ break;
216
+ }
217
+
218
+ return 0;
219
+ }
220
+
221
+ int libpcapng_mem_read(unsigned char *buf, size_t buf_len,
222
+ foreach_pcapng_block_cb pcapng_block_cb, void *userdata)
223
+ {
224
+ int swap;
225
+ if (!pcapng_block_cb) { fprintf(stderr, "No Block Callback set!\n"); return -1; }
226
+ if (_pcap_classic_magic(buf, buf_len, &swap))
227
+ return _mem_read_classic_pcap(buf, buf_len, pcapng_block_cb, userdata);
228
+ return _mem_read_pcapng(buf, buf_len, pcapng_block_cb, userdata);
229
+ }
230
+
231
+ int libpcapng_fp_read(FILE *fp, foreach_pcapng_block_cb pcapng_block_cb, void *userdata)
232
+ {
233
+ uint64_t block_counter = 1;
234
+ uint32_t block_info[2]; // [0] = block_type [1] = block_total_length
235
+ size_t read_length;
236
+
237
+ unsigned char data[65535]; // A packet is not greater than snaplen
238
+ uint32_t block_total_length = 0;
239
+
240
+ if (!pcapng_block_cb) {
241
+ fprintf(stderr, "No Block Callback set!\n");
242
+ return -1;
243
+ }
244
+
245
+ /* Detect classic pcap by peeking at the first 4 magic bytes. */
246
+ {
247
+ unsigned char magic_buf[4];
248
+ if (fread(magic_buf, 1, 4, fp) != 4) return -1;
249
+ int swap;
250
+ if (_pcap_classic_magic(magic_buf, 4, &swap)) {
251
+ fseek(fp, 0, SEEK_SET);
252
+ return _fp_read_classic_pcap(fp, pcapng_block_cb, userdata);
253
+ }
254
+ /* Not classic pcap; seek back and continue with pcapng parsing. */
255
+ fseek(fp, 0, SEEK_SET);
256
+ }
257
+
258
+ read_length = fread(&block_info, 1, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
259
+ if (read_length != PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
260
+ fprintf(stderr, "Could not read expected data: got %lu expected %u. Stopping.\n", read_length, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH);
261
+ return -1;
262
+ }
263
+ block_total_length = block_info[1];
264
+ while (read_length > 0) {
265
+ if (block_total_length - PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH > sizeof(data)) {
266
+ fprintf(stderr, "Block size %u exceeds buffer. Stopping.\n", block_total_length);
267
+ return -1;
268
+ }
269
+ read_length = fread(&data, 1, block_total_length - PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
270
+ if (read_length != block_info[1] - PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
271
+ fprintf(stderr, "Could not read expected (%u) block_size; Got %lu. Stopping.\n", block_info[1], read_length);
272
+ return -1;
273
+ }
274
+
275
+ pcapng_block_cb(block_counter, block_info[0], block_info[1], (unsigned char *)data, (void *)userdata);
276
+
277
+ read_length = fread(&block_info, 1, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
278
+ if (read_length == 0) {
279
+ break;
280
+ }
281
+ if (read_length != PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
282
+ fprintf(stderr, "Could not read expected (%d) data; Got %lu. Stopping.\n", PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, read_length);
283
+ return -1;
284
+ }
285
+ block_total_length = block_info[1];
286
+
287
+ block_counter++;
288
+ }
289
+ return 0;
290
+ }
291
+
292
+ int libpcapng_file_read(char *filename, foreach_pcapng_block_cb pcapng_block_cb, void *userdata)
293
+ {
294
+ FILE *fp;
295
+
296
+ fp = fopen(filename, "rb");
297
+ if (!fp) {
298
+ fprintf(stderr, "Cannot read file '%s'\n", filename);
299
+ return -1;
300
+ }
301
+
302
+ libpcapng_fp_read(fp, pcapng_block_cb, userdata);
303
+
304
+ fclose(fp);
305
+
306
+ return 0;
307
+ }
308
+
309
+ int libpcapng_file_read_debug(char *filename)
310
+ {
311
+ return libpcapng_file_read(filename, foreach_pcapng_block, NULL);
312
+ }
313
+
314
+ int libpcapng_padded_count(unsigned char *data, uint32_t data_len)
315
+ {
316
+ int retpad = 0;
317
+ int i;
318
+
319
+ /*
320
+ * Count only trailing *consecutive* zero bytes (pcapng alignment
321
+ * padding is at most 3 bytes). The previous implementation counted
322
+ * every zero byte among the last four positions, which incorrectly
323
+ * stripped payload bytes whose values happened to be 0x00.
324
+ */
325
+ for (i = (int)data_len - 1; i >= 0 && retpad < 3; i--) {
326
+ if (data[i] == 0)
327
+ retpad++;
328
+ else
329
+ break;
330
+ }
331
+
332
+ return retpad;
333
+ }
pycapng-0.6/lib/io.c DELETED
@@ -1,128 +0,0 @@
1
- /*
2
- * License MIT
3
- * Copyright (c) 2021 Devo Inc.
4
- * Copyright (c) 2022 Sebastien Tricaud
5
- */
6
- #include <stdio.h>
7
- #include <stdlib.h>
8
- #include <string.h>
9
-
10
- #include <libpcapng/blocks.h>
11
-
12
- #include <libpcapng/io.h>
13
-
14
- int foreach_pcapng_block(uint32_t block_counter, uint32_t block_type, uint32_t block_total_length, unsigned char *data, void *userdata)
15
- {
16
-
17
- switch (block_type) {
18
- case PCAPNG_SECTION_HEADER_BLOCK: {
19
- /* printf("Section Header Block\n"); */
20
- pcapng_section_header_block_light_t *shb;
21
- shb = libpcapng_section_header_block_read(data, block_total_length);
22
- }
23
- break;
24
- case PCAPNG_CUSTOM_DATA_BLOCK: {
25
- /* printf("Custom Data Block\n"); */
26
- pcapng_custom_data_block_light_t *cb;
27
- cb = libpcapng_custom_data_block_read(data, block_total_length);
28
- }
29
- break;
30
- case PCAPNG_ENHANCED_PACKET_BLOCK: {
31
- /* printf("PCAPNG_ENHANCED_PACKET_BLOCK\n"); */
32
- pcapng_enhanced_packet_block_light_t *epb;
33
- epb = libpcapng_enhanced_packet_block_read(data, block_total_length);
34
- }
35
- break;
36
- case PCAPNG_INTERFACE_DESCRIPTION_BLOCK:
37
- printf("Interface Description Block\n");
38
- break;
39
- default:
40
- fprintf(stderr, "Block type %x not handled yet!\n", block_type);
41
- break;
42
- }
43
-
44
- return 0;
45
- }
46
-
47
- int libpcapng_fp_read(FILE *fp, foreach_pcapng_block_cb pcapng_block_cb, void *userdata)
48
- {
49
- uint64_t block_counter = 1;
50
- uint32_t block_info[2]; // [0] = block_type [1] = block_total_length
51
- size_t read_length;
52
-
53
- unsigned char data[65535]; // A packet is not greater than snaplen
54
- uint32_t block_total_length = 0;
55
-
56
- if (!pcapng_block_cb) {
57
- fprintf(stderr, "No Block Callback set!\n");
58
- return -1;
59
- }
60
-
61
- read_length = fread(&block_info, 1, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
62
- if (read_length != PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
63
- fprintf(stderr, "Could not read expected data: got %lu expected %u. Stopping.\n", read_length, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH);
64
- return -1;
65
- }
66
- block_total_length = block_info[1];
67
- while (read_length > 0) {
68
- read_length = fread(&data, 1, block_total_length - PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
69
- if (read_length != block_info[1] - PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
70
- fprintf(stderr, "Could not read expected (%u) block_size; Got %lu. Stopping.\n", block_info[1], read_length);
71
- return -1;
72
- }
73
-
74
- pcapng_block_cb(block_counter, block_info[0], block_info[1], (unsigned char *)data, (void *)userdata);
75
-
76
- read_length = fread(&block_info, 1, PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, fp);
77
- if (read_length == 0) {
78
- break;
79
- }
80
- if (read_length != PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH) {
81
- fprintf(stderr, "Could not read expected (%d) data; Got %lu. Stopping.\n", PCAPNG_BLOCK_TYPE_AND_SIZE_LENGTH, read_length);
82
- return -1;
83
- }
84
- block_total_length = block_info[1];
85
-
86
- block_counter++;
87
- }
88
- }
89
-
90
- int libpcapng_file_read(char *filename, foreach_pcapng_block_cb pcapng_block_cb, void *userdata)
91
- {
92
- FILE *fp;
93
-
94
- fp = fopen(filename, "rb");
95
- if (!fp) {
96
- fprintf(stderr, "Cannot read file '%s'\n", filename);
97
- return -1;
98
- }
99
-
100
- libpcapng_fp_read(fp, pcapng_block_cb, userdata);
101
-
102
- fclose(fp);
103
-
104
- return 0;
105
- }
106
-
107
- int libpcapng_file_read_debug(char *filename)
108
- {
109
- return libpcapng_file_read(filename, foreach_pcapng_block, NULL);
110
- }
111
-
112
- int libpcapng_padded_count(unsigned char *data, uint32_t data_len)
113
- {
114
- uint32_t i;
115
- int retpad = 0;
116
-
117
- /* printf("vvvvvvvvvvvv\n"); */
118
- /* for (i = 0; i < data_len; i++) { */
119
- /* printf("%x", data[i]); */
120
- /* } */
121
- /* printf("\n"); */
122
-
123
- for (i = 1; i <= 4; i++) {
124
- if (data[data_len-i] == 0) { retpad++; };
125
- }
126
-
127
- return retpad;
128
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes