package cstruct

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

Install

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

Conflicts (1)

  1. js_of_ocaml < "3.5.0"
OCaml

Innovation. Community. Security.