package cstruct

  1. Overview
  2. Docs

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: 13 Mar 2019

README

Cstruct -- access C-like structures directly from OCaml

v3.7.0

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-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.02.3 and later, since it provides a ppx extension point. The old camlp4 syntax extension is nolonger available; the last version 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.

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
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

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.

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

Dependencies (3)

  1. sexplib
  2. dune >= "1.0"
  3. ocaml >= "4.03.0"

Dev Dependencies (1)

  1. alcotest with-test

  1. albatross < "1.3.0"
  2. angstrom >= "0.2.0" & < "0.7.0"
  3. arakoon >= "1.8.6" & < "1.8.12"
  4. arp >= "0.2.1" & < "3.0.0"
  5. arp-mirage
  6. asn1-combinators >= "0.1.2" & != "0.2.0-1" & < "0.2.6"
  7. awa < "0.0.4"
  8. awa-lwt < "0.0.4"
  9. awa-mirage < "0.0.4"
  10. balancer
  11. bip32
  12. buffer-pool
  13. capnp-rpc-net >= "1.1" & < "1.2.1"
  14. certify >= "0.2"
  15. chacha < "1.1.0"
  16. channel
  17. charrua < "1.4.1"
  18. charrua-client < "1.4.1"
  19. charrua-client-lwt
  20. charrua-client-mirage < "0.12.0"
  21. charrua-core >= "0.8"
  22. charrua-server < "1.4.1"
  23. charrua-unix = "0.6"
  24. cohttp >= "0.9.7" & < "0.10.0"
  25. colombe < "0.2.0"
  26. conduit >= "0.6.0" & < "0.15.2"
  27. conduit-async = "3.0.0"
  28. conduit-lwt = "3.0.0"
  29. conduit-mirage != "3.0.0"
  30. conex < "0.10.0"
  31. conex-mirage-crypto
  32. conex-nocrypto
  33. cowabloga >= "0.0.5"
  34. crc
  35. crunch >= "2.0.0" & < "3.0.0"
  36. cstruct-async >= "3.4.0" & < "4.0.0"
  37. cstruct-lwt >= "3.4.0" & < "4.0.0"
  38. cstruct-sexp = "5.0.0"
  39. cstruct-unix >= "3.1.0" & < "3.2.0" | >= "3.4.0" & < "4.0.0"
  40. datakit
  41. datakit-ci >= "0.10.0" & < "0.12.4"
  42. datakit-client
  43. datakit-client-9p
  44. datakit-server
  45. depyt
  46. dns >= "1.0.0" & < "6.0.0"
  47. dns-forward >= "0.9.0"
  48. dnssd
  49. dream < "1.0.0~alpha2"
  50. duff < "0.3"
  51. eqaf >= "0.8"
  52. ethernet < "2.2.1"
  53. fat-filesystem >= "0.13.0" & < "0.15.1"
  54. fiat-p256
  55. frenetic < "2.0.0" | >= "3.2.0" & < "5.0.0"
  56. git >= "2.0.0" & < "3.0.0"
  57. git-http < "1.11.0"
  58. github-hooks >= "0.2.0"
  59. gluten-mirage < "0.3.0"
  60. gpt
  61. h2-mirage
  62. hacl-star >= "0.7.0"
  63. hacl_x25519
  64. hex >= "0.2.0"
  65. hkdf
  66. httpun-mirage
  67. hvsock < "3.0.0"
  68. io-page
  69. io-page-unix
  70. io-page-xen
  71. ipaddr-cstruct < "5.2.0"
  72. ipv6-multicast >= "0.9"
  73. irmin >= "0.9.0" & < "1.0.0" | >= "1.1.0" & < "2.0.0"
  74. irmin-git >= "2.3.0"
  75. irmin-indexeddb >= "0.3"
  76. key-parsers >= "0.5.0" & < "1.2.1"
  77. launchd
  78. learn-ocaml-client
  79. letsencrypt = "0.2.5"
  80. letsencrypt-app < "0.4.0"
  81. macaddr-cstruct < "5.2.0"
  82. metrics-mirage
  83. mirage >= "0.7.2" & < "0.9.0" | >= "0.10.0" & < "2.4.0"
  84. mirage-block < "1.0.0"
  85. mirage-block-ccm < "1.1.0"
  86. mirage-block-combinators < "3.0.0"
  87. mirage-block-lwt
  88. mirage-block-ramdisk
  89. mirage-block-solo5 < "0.6.2"
  90. mirage-block-unix = "2.0.0" | = "2.7.0" | >= "2.11.1" & < "2.13.0"
  91. mirage-block-xen >= "1.4.0" & < "1.5.2" | >= "1.6.0" & < "2.1.1"
  92. mirage-btrees
  93. mirage-channel-lwt
  94. mirage-clock-unix < "1.0.0"
  95. mirage-clock-xen < "1.0.0"
  96. mirage-conduit < "2.0.0" | >= "2.3.1"
  97. mirage-console-lwt
  98. mirage-console-solo5 >= "0.2.0"
  99. mirage-console-unix >= "2.2.1" & < "3.0.0"
  100. mirage-console-xen = "4.0.0"
  101. mirage-console-xen-backend >= "2.3.2" & < "2.3.4" | = "4.0.0"
  102. mirage-crypto < "0.10.4"
  103. mirage-crypto-ec < "0.10.4"
  104. mirage-crypto-pk < "0.10.4"
  105. mirage-crypto-rng < "0.7.0"
  106. mirage-dns != "2.6.0" & < "2.7.0"
  107. mirage-entropy < "0.5.0"
  108. mirage-entropy-xen < "0.3.0"
  109. mirage-flow < "1.2.0"
  110. mirage-flow-lwt
  111. mirage-flow-rawlink
  112. mirage-flow-unix < "2.0.0"
  113. mirage-fs >= "0.4.0" & < "1.0.0"
  114. mirage-fs-lwt
  115. mirage-fs-mem
  116. mirage-fs-unix < "1.1.0" | >= "1.4.0" & != "1.5.0"
  117. mirage-kv-lwt
  118. mirage-kv-unix
  119. mirage-nat < "2.2.4"
  120. mirage-net = "0.5.2"
  121. mirage-net-fd
  122. mirage-net-lwt
  123. mirage-net-macosx
  124. mirage-net-solo5
  125. mirage-net-unix < "2.1.0" | >= "2.2.1"
  126. mirage-net-xen != "1.4.2" & != "1.7.0" & < "2.1.0"
  127. mirage-profile >= "0.8.2"
  128. mirage-protocols-lwt
  129. mirage-qubes != "0.2" & < "0.5" | >= "0.7.0" & < "0.9.2"
  130. mirage-qubes-ipv4 < "0.9.3"
  131. mirage-random < "2.0.0"
  132. mirage-random-stdlib
  133. mirage-random-test
  134. mirage-solo5
  135. mirage-stack-lwt
  136. mirage-tc
  137. mirage-tcpip-unix
  138. mirage-tcpip-xen
  139. mirage-types-lwt < "3.7.1"
  140. mirage-unix < "0.9.4" | >= "2.5.0" & < "3.0.8"
  141. mirage-vnetif < "0.6.0"
  142. mirage-www < "0.4.0" | >= "1.1.0"
  143. mirage-xen < "2.0.0" | >= "2.6.0"
  144. monorobot
  145. mrt-format
  146. mstruct
  147. nbd = "4.0.3"
  148. netchannel < "2.1.0"
  149. nocrypto < "0.4.0" | >= "0.5.4-1"
  150. noise
  151. openflow < "0.2.0"
  152. otr = "0.3.1" | >= "0.3.4" & < "0.3.9"
  153. ox < "1.1.1"
  154. pbkdf < "0.3.0" | = "1.1.0"
  155. pcap-format >= "0.5.1"
  156. pf-qubes
  157. ppx_cstruct = "3.7.0"
  158. protocol-9p < "0.5.1" | >= "0.10.0" & < "2.0.1"
  159. protocol-9p-tool = "0.12.0" | >= "2.0.0" & < "2.0.2"
  160. protocol-9p-unix < "2.0.2"
  161. qcow
  162. qcow-tool
  163. randomconv < "0.2.0"
  164. rawlink >= "0.6" & < "1.2"
  165. resp-mirage = "0.10.0"
  166. rfc6287 >= "1.0.2"
  167. salsa20 < "1.2.0"
  168. salsa20-core >= "0.3.0" & < "1.1.0"
  169. scrypt-kdf >= "0.2.0" & < "1.2.0"
  170. secp256k1-internal < "0.3.0"
  171. sendmail >= "0.4.1" & < "0.5.0"
  172. shared-block-ring < "3.0.1"
  173. shared-memory-ring >= "3.0.1" & < "3.1.1"
  174. shared-memory-ring-lwt
  175. slack
  176. ssh-agent
  177. tar >= "1.0.0" & < "2.0.0"
  178. tar-mirage
  179. tar-unix != "1.0.0" & < "2.0.0"
  180. tcpip >= "3.3.0" & < "6.3.0"
  181. tezos-lmdb
  182. tls >= "0.9.0" & < "0.10.2"
  183. u2f = "0.1.1"
  184. uecc
  185. vchan >= "3.0.0" & < "6.0.1"
  186. vchan-unix
  187. vchan-xen
  188. vhd-format >= "0.12.0"
  189. vhd-format-lwt
  190. vhd-tool < "0.12.0"
  191. vmnet >= "1.1.0"
  192. x509 < "0.6.3"
  193. xe
  194. xen-api-client >= "0.9.6" & < "0.9.14"
  195. xen-block-driver >= "0.2.5"
  196. xen-gnt
  197. xenstore >= "2.1.0" & < "2.3.0"

Conflicts

None

OCaml

Innovation. Community. Security.