package cstruct

  1. Overview
  2. Docs
Access C-like structures directly from OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

cstruct-6.2.0.tbz
sha256=9a78073392580e8349148fa3ab4b1b2e989dc9d30d07401b04c96b7c60f03e62
sha512=8d33fe6b3707a3994d0225cd33cadde0bb2ca834ef01096e3df33a08e4a8c6d02ebccddf558a73988b8a5595b65fdc10de61efbf872c6c9e55c719c7e19c463d

Description

Cstruct is a library and syntax extension to make it easier to access C-like structures directly from OCaml. It supports both reading and writing to these structures, and they are accessed via the Bigarray module.

Tags

org:mirage org:ocamllabs

Published: 14 Mar 2023

README

Cstruct - access C-like structures directly from OCaml

Cstruct is a library and syntax extension to make it easier to access C-like structures directly from OCaml. It supports both reading and writing to these structures, and they are accessed via the Bigarray module.

Installation

This repository provides several packages that can be installed via the opam package manager:

  • cstruct: the core Cstruct library
  • cstruct-sexp: serialisers into s-expression format of Cstructs
  • cstruct-unix: provide Unix variations of the read/write functions using file descriptors
  • cstruct-async: provide Async Pipe and Bigstring support
  • cstruct-lwt: provide Lwt variants of read/write functions
  • ppx_cstruct: a PPX syntax extension (see below)

The libraries depend on OCaml version 4.08.0 and later, since it provides a ppx extension point. The old camlp4 syntax extension is nolonger available; the last cstruct release which contained it was v1.9.0.

Local development

You can build the library via dune, using make or dune build directly. Since everything is built via dune, you can also place this repository within a wider dune workspace in order to make local modifications across repositories.

Documentation

A documentation of the last version of cstruct is available here.

Usage

PPX

The PPX processor is used by passing the OCaml source code through the ppx_cstruct binary. An example pcap description is:

[%%cstruct
type pcap_header = {
  magic_number: uint32_t;   (* magic number *)
  version_major: uint16_t;  (* major version number *)
  version_minor: uint16_t;  (* minor version number *)
  thiszone: uint32_t;       (* GMT to local correction *)
  sigfigs: uint32_t;        (* accuracy of timestamps *)
  snaplen: uint32_t;        (* max length of captured packets, in octets *)
  network: uint32_t;        (* data link type *)
} [@@little_endian]]

[%%cstruct
type pcap_packet = {
  ts_sec: uint32_t;         (* timestamp seconds *)
  ts_usec: uint32_t;        (* timestamp microseconds *)
  incl_len: uint32_t;       (* number of octets of packet saved in file *)
  orig_len: uint32_t;       (* actual length of packet *)
} [@@little_endian]]

[%%cstruct
type ethernet = {
  dst: uint8_t [@len 6];
  src: uint8_t [@len 6];
  ethertype: uint16_t;
} [@@big_endian]]

[%%cstruct
type ipv4 = {
  hlen_version: uint8_t;
  tos: uint8_t;
  len: uint16_t;
  id: uint16_t;
  off: uint16_t;
  ttl: uint8_t;
  proto: uint8_t;
  csum: uint16_t;
  src: uint8_t [@len 4];
  dst: uint8_t [@len 4];
} [@@big_endian]]

This auto-generates generates functions of the form below in the ml file:

let sizeof_pcap_packet = 16
let get_pcap_packet_ts_sec v = Cstruct.LE.get_uint32 v 0
let set_pcap_packet_ts_sec v x = Cstruct.LE.set_uint32 v 0 x
let get_pcap_packet_ts_usec v = Cstruct.LE.get_uint32 v 4
let set_pcap_packet_ts_usec v x = Cstruct.LE.set_uint32 v 4 x
let get_pcap_packet_incl_len v = Cstruct.LE.get_uint32 v 8
let set_pcap_packet_incl_len v x = Cstruct.LE.set_uint32 v 8 x
let get_pcap_packet_orig_len v = Cstruct.LE.get_uint32 v 12
let set_pcap_packet_orig_len v x = Cstruct.LE.set_uint32 v 12 x

let sizeof_ethernet = 14
let get_ethernet_dst src = Cstruct.sub src 0 6
let copy_ethernet_dst src = Cstruct.copy src 0 6
let set_ethernet_dst src srcoff dst =
  Cstruct.blit_from_string src srcoff dst 0 6
let blit_ethernet_dst src srcoff dst = Cstruct.blit src srcoff dst 0 6
let get_ethernet_src src = Cstruct.sub src 6 6
let copy_ethernet_src src = Cstruct.copy src 6 6
let set_ethernet_src src srcoff dst =
  Cstruct.blit_from_string src srcoff dst 6 6
let blit_ethernet_src src srcoff dst = Cstruct.blit src srcoff dst 6 6
let get_ethernet_ethertype v = Cstruct.BE.get_uint16 v 12
let set_ethernet_ethertype v x = Cstruct.BE.set_uint16 v 12 x

The mli file will have signatures of this form:

val sizeof_pcap_packet : int
val get_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_ts_sec : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_ts_usec : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_incl_len : Cstruct.t -> Cstruct.uint32 -> unit
val get_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32
val set_pcap_packet_orig_len : Cstruct.t -> Cstruct.uint32 -> unit
val hexdump_pcap_packet_to_buffer : Buffer.t -> pcap_packet -> unit
val hexdump_pcap_packet : Cstruct.t -> unit

val sizeof_ethernet : int
val get_ethernet_dst : Cstruct.t -> Cstruct.t
val copy_ethernet_dst : Cstruct.t -> string
val set_ethernet_dst : string -> int -> Cstruct.t -> unit
val blit_ethernet_dst : Cstruct.t -> int -> Cstruct.t -> unit
val get_ethernet_src : Cstruct.t -> Cstruct.t
val copy_ethernet_src : Cstruct.t -> string
val set_ethernet_src : string -> int -> Cstruct.t -> unit
val blit_ethernet_src : Cstruct.t -> int -> Cstruct.t -> unit
val get_ethernet_ethertype : Cstruct.t -> Cstruct.uint16
val set_ethernet_ethertype : Cstruct.t -> Cstruct.uint16 -> unit
val hexdump_ethernet_to_buffer : Buffer.t -> Cstruct.t -> unit
val hexdump_ethernet : Cstruct.t -> unit

The hexdump functions above are convenient pretty-printing functions to help you debug, and aren't intended to be high performance.

You can also declare C-like enums:

[%%cenum
type foo32 =
  | ONE32
  | TWO32 [@id 0xfffffffel]
  | THREE32
  [@@uint32_t]
]

[%%cenum
type bar16 =
  | ONE [@id 1]
  | TWO
  | FOUR [@id 4]
  | FIVE
  [@@uint16_t]
]

This generates signatures of the form:

type foo32 = | ONE32 | TWO32 | THREE32
val int_to_foo32 : int32 -> foo32 option
val foo32_to_int : foo32 -> int32
val foo32_to_string : foo32 -> string
val string_to_foo32 : string -> foo32 option
val compare_foo32 : foo32 -> foo32 -> int
type bar16 = | ONE | TWO | FOUR | FIVE
val int_to_bar16 : int -> bar16 option
val bar16_to_int : bar16 -> int
val bar16_to_string : bar16 -> string
val string_to_bar16 : string -> bar16 option
val compare_bar16 : bar16 -> bar16 -> int

Comparisons will be done relatively to the constructor ids.

You can also add a (sexp) decorator to output s-expression convertors for use with the sexplib library.

[%%cenum
type foo64 =
  | ONE64
  | TWO64
  | THREE64
  [@@uint64_t] [@@sexp]
]

And sexp_of_foo64 and foo64_of_sexp functions will also be available. The representation of the Sexp is the string representation of the enum.

If you do use the sexp decorator, then you will also need to add sexplib to the dependency list for your package (both in the dune file and the opam file).

Please see the ppx_test/ directory for more in-depth examples.

Dependencies (3)

  1. fmt >= "0.8.9"
  2. dune >= "2.0.0"
  3. ocaml >= "4.08.0"

Dev Dependencies (2)

  1. crowbar with-test
  2. alcotest with-test

  1. albatross >= "1.4.3" & < "2.3.0"
  2. arp
  3. asn1-combinators = "0.2.6"
  4. async_unix >= "v0.17.0"
  5. awa
  6. awa-mirage
  7. bip32
  8. builder < "0.4.0"
  9. builder-web < "0.2.0"
  10. capnp-rpc-net
  11. capnp-rpc-unix >= "2.1"
  12. carton >= "0.4.3" & < "1.0.0"
  13. carton-git
  14. carton-lwt < "1.0.0"
  15. certify < "0.3.3"
  16. chamelon
  17. charrua
  18. charrua-client
  19. charrua-server
  20. coap
  21. coap-core
  22. coap-server-lwt
  23. cohttp-mirage
  24. colombe < "0.2.0"
  25. conduit-mirage
  26. conex < "0.10.0"
  27. conex-mirage-crypto < "1.0.0"
  28. cowabloga
  29. crc
  30. cstruct-async
  31. cstruct-lwt >= "6.2.0"
  32. cstruct-sexp >= "6.2.0"
  33. cstruct-unix >= "6.2.0"
  34. current-albatross-deployer
  35. current_git >= "0.6.2"
  36. current_github
  37. current_web
  38. dbase4 < "0.2.0"
  39. depyt
  40. dirsp-proscript
  41. dns >= "6.3.0" & < "9.1.0"
  42. dns-cli < "9.1.0"
  43. dns-client < "7.0.3"
  44. dns-forward
  45. dns-mirage
  46. dns-server
  47. dns-stub
  48. dns-tsig < "9.1.0"
  49. dnssd
  50. dnssec < "9.1.0"
  51. dream
  52. eio
  53. eqaf >= "0.8" & < "0.10"
  54. eqaf-cstruct
  55. ethernet
  56. fat-filesystem = "0.13.0" | >= "0.15.0"
  57. frenetic
  58. geojsone >= "0.2.0"
  59. git < "3.6.0" | >= "3.15.0"
  60. git-cohttp
  61. git-cohttp-unix
  62. git-kv >= "0.2.0"
  63. git-mirage
  64. git-unix >= "3.2.0"
  65. github-hooks
  66. gluten-mirage < "0.5.2"
  67. gpt
  68. h2-mirage
  69. hacl-star >= "0.7.0"
  70. hex >= "1.4.0"
  71. hkdf
  72. http-multipart-formdata >= "3.1.0"
  73. httpun-mirage
  74. httpun-ws-mirage
  75. hvsock
  76. intel_hex
  77. io-page
  78. io-page-unix
  79. ipaddr-cstruct
  80. irmin-git
  81. irmin-indexeddb
  82. jose < "0.10.0"
  83. key-parsers >= "1.2.1"
  84. launchd
  85. learn-ocaml-client
  86. ledgerwallet >= "0.2.1"
  87. letsencrypt < "1.0.0"
  88. letsencrypt-app < "1.0.0"
  89. lt-code
  90. macaddr-cstruct
  91. mbr-format
  92. memtrace_viewer < "v0.15.0"
  93. mimic >= "0.0.4"
  94. mirage-block >= "2.0.1"
  95. mirage-block-ccm
  96. mirage-block-combinators >= "3.0.2"
  97. mirage-block-ramdisk
  98. mirage-block-solo5
  99. mirage-block-unikraft
  100. mirage-block-unix != "2.12.1"
  101. mirage-block-xen
  102. mirage-btrees
  103. mirage-channel >= "4.1.0"
  104. mirage-channel-lwt
  105. mirage-conduit
  106. mirage-console-lwt
  107. mirage-crypto >= "0.10.7" & < "1.1.0"
  108. mirage-crypto-ec < "1.1.0"
  109. mirage-crypto-pk < "1.1.0"
  110. mirage-crypto-rng < "1.1.0"
  111. mirage-crypto-rng-eio
  112. mirage-crypto-rng-mirage < "1.1.0"
  113. mirage-flow >= "3.0.0"
  114. mirage-flow-combinators
  115. mirage-flow-unix
  116. mirage-fs >= "4.0.0"
  117. mirage-fs-lwt
  118. mirage-kv-lwt
  119. mirage-kv-unix
  120. mirage-nat
  121. mirage-net >= "4.0.0"
  122. mirage-net-lwt
  123. mirage-net-macosx
  124. mirage-net-solo5
  125. mirage-net-unikraft
  126. mirage-net-unix
  127. mirage-net-xen
  128. mirage-profile
  129. mirage-protocols >= "7.0.0"
  130. mirage-protocols-lwt
  131. mirage-qubes
  132. mirage-random
  133. mirage-solo5 < "0.10.0"
  134. mirage-stack-lwt
  135. mirage-tc
  136. mirage-types-lwt
  137. mirage-vnetif
  138. mirage-xen
  139. monorobot
  140. multihash
  141. nbd >= "4.0.3"
  142. noise
  143. oneffs
  144. otr < "1.0.0"
  145. paf
  146. pbkdf
  147. pcap-format >= "0.5.2"
  148. pf-qubes
  149. plebeia
  150. plist-xml >= "0.4.0" & < "0.5.1"
  151. ppx_cstruct >= "6.2.0"
  152. protocol-9p >= "2.0.2"
  153. protocol-9p-tool = "2.0.2"
  154. protocol-9p-unix = "0.11.3" | >= "2.0.2"
  155. proton
  156. qcow >= "0.11.0"
  157. qcow-tool
  158. qcow-types
  159. randomconv < "0.2.0"
  160. rawlink >= "1.2"
  161. reparse >= "3.0.0"
  162. reparse-lwt
  163. reparse-lwt-unix
  164. rfc6287 < "1.0.4"
  165. rio
  166. riot = "0.0.7"
  167. salsa20 != "1.1.0"
  168. salsa20-core = "1.1.0"
  169. scrypt-kdf
  170. secp256k1-internal >= "0.3.1"
  171. sendmail >= "0.5.0"
  172. shared-block-ring
  173. shared-memory-ring
  174. shared-memory-ring-lwt
  175. sihl >= "3.0.0"
  176. slack
  177. solo5-elftool < "0.4.0"
  178. spoke >= "0.0.4"
  179. ssh-agent != "0.3.0"
  180. swapfs
  181. tar < "3.3.0"
  182. tar-mirage
  183. tar-unix < "3.3.0"
  184. tcpip >= "7.1.2"
  185. tls >= "0.12.8" & < "1.0.4"
  186. tls-liquidsoap
  187. uecc
  188. uring
  189. vchan
  190. vchan-unix
  191. vchan-xen
  192. vhd-format >= "0.12.0" & < "0.12.2" | >= "0.13.0"
  193. vhd-format-lwt >= "0.13.0"
  194. vmnet
  195. wayland >= "1.0"
  196. webauthn < "0.2.0"
  197. x509 >= "0.14.1" & < "1.0.6"
  198. xen-gnt
  199. yocaml_git >= "2.0.0"

Conflicts (1)

  1. js_of_ocaml < "3.5.0"