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: 27 Feb 2019

README

Cstruct -- access C-like structures directly from OCaml

v3.5.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.7.0"
  3. arp >= "0.2.1" & < "3.0.0"
  4. arp-mirage
  5. asn1-combinators != "0.2.0-1" & < "0.2.6"
  6. awa < "0.0.4"
  7. awa-lwt < "0.0.4"
  8. awa-mirage < "0.0.4"
  9. balancer
  10. bip32
  11. buffer-pool
  12. capnp-rpc-net >= "1.1" & < "1.2.1"
  13. certify
  14. chacha < "1.1.0"
  15. charrua < "1.4.1"
  16. charrua-client < "1.4.1"
  17. charrua-client-lwt
  18. charrua-client-mirage < "0.12.0"
  19. charrua-core
  20. charrua-server < "1.4.1"
  21. colombe < "0.2.0"
  22. conduit-mirage
  23. conex < "0.10.0"
  24. conex-mirage-crypto
  25. conex-nocrypto
  26. cowabloga
  27. crc
  28. crunch >= "2.0.0" & < "3.0.0"
  29. cstruct-async >= "3.4.0" & < "4.0.0"
  30. cstruct-lwt >= "3.4.0" & < "4.0.0"
  31. datakit-server
  32. depyt
  33. dns < "6.0.0"
  34. dns-forward
  35. dnssd
  36. dream < "1.0.0~alpha2"
  37. duff < "0.3"
  38. eqaf >= "0.8" & < "0.10"
  39. eqaf-cstruct
  40. ethernet < "2.2.1"
  41. fat-filesystem < "0.15.1"
  42. fiat-p256
  43. git >= "2.0.0" & < "3.0.0"
  44. github-hooks
  45. gluten-mirage < "0.3.0"
  46. gpt
  47. h2-mirage
  48. hacl-star >= "0.7.0"
  49. hacl_x25519
  50. hex
  51. hkdf < "2.0.0"
  52. httpun-mirage
  53. httpun-ws-mirage
  54. hvsock < "3.0.0"
  55. io-page
  56. io-page-unix
  57. io-page-xen
  58. ipaddr-cstruct < "5.2.0"
  59. ipv6-multicast
  60. irmin-git >= "2.3.0"
  61. irmin-indexeddb
  62. key-parsers < "1.2.1"
  63. launchd
  64. learn-ocaml-client
  65. letsencrypt = "0.2.5"
  66. letsencrypt-app < "0.4.0"
  67. macaddr-cstruct < "5.2.0"
  68. metrics-mirage
  69. mirage-block-combinators < "3.0.0"
  70. mirage-block-lwt
  71. mirage-block-ramdisk < "0.6"
  72. mirage-block-solo5 < "0.6.2"
  73. mirage-block-unix >= "2.11.1" & < "2.13.0"
  74. mirage-block-xen < "2.1.1"
  75. mirage-btrees
  76. mirage-channel-lwt
  77. mirage-clock-unix < "1.0.0"
  78. mirage-clock-xen < "1.0.0"
  79. mirage-conduit
  80. mirage-console-lwt
  81. mirage-console-solo5
  82. mirage-console-unix < "3.0.2"
  83. mirage-console-xen = "4.0.0"
  84. mirage-console-xen-backend < "2.3.4" | = "4.0.0"
  85. mirage-crypto < "0.10.4"
  86. mirage-crypto-ec < "0.10.4"
  87. mirage-crypto-pk < "0.10.4"
  88. mirage-crypto-rng < "0.8.0"
  89. mirage-entropy < "0.5.0"
  90. mirage-flow-lwt
  91. mirage-flow-rawlink
  92. mirage-flow-unix < "2.0.1"
  93. mirage-fs >= "0.4.0" & < "1.0.0"
  94. mirage-fs-lwt
  95. mirage-fs-mem
  96. mirage-fs-unix != "1.5.0"
  97. mirage-kv-lwt
  98. mirage-kv-unix
  99. mirage-nat < "2.2.4"
  100. mirage-net-fd
  101. mirage-net-lwt
  102. mirage-net-macosx
  103. mirage-net-solo5
  104. mirage-net-unix
  105. mirage-net-xen < "2.1.0"
  106. mirage-profile >= "0.8.2"
  107. mirage-protocols-lwt
  108. mirage-qubes >= "0.7.0" & < "0.9.2"
  109. mirage-qubes-ipv4 < "0.9.3"
  110. mirage-random < "2.0.0"
  111. mirage-random-stdlib
  112. mirage-random-test
  113. mirage-solo5 < "0.9.4"
  114. mirage-stack-lwt
  115. mirage-tc
  116. mirage-types-lwt < "3.7.1"
  117. mirage-unix < "3.0.8"
  118. mirage-vnetif < "0.6.0"
  119. mirage-xen
  120. monorobot
  121. mstruct
  122. nbd = "4.0.3"
  123. netchannel < "2.1.0"
  124. nocrypto
  125. noise
  126. otr < "0.3.9"
  127. pbkdf < "0.3.0" | = "1.1.0"
  128. pcap-format >= "0.5.2"
  129. pf-qubes
  130. ppx_cstruct = "3.5.0"
  131. protocol-9p < "2.0.1"
  132. protocol-9p-tool = "0.12.0" | >= "2.0.0" & < "2.0.2"
  133. protocol-9p-unix < "2.0.2"
  134. qcow
  135. qcow-tool
  136. randomconv < "0.2.0"
  137. rawlink < "1.2"
  138. resp-mirage = "0.10.0"
  139. rfc6287
  140. salsa20 < "1.2.0"
  141. salsa20-core >= "0.3.0" & < "1.1.0"
  142. scrypt-kdf >= "0.2.0" & < "1.2.0"
  143. secp256k1-internal < "0.3.0"
  144. sendmail >= "0.4.1" & < "0.5.0"
  145. shared-block-ring < "3.0.1"
  146. shared-memory-ring >= "3.0.1" & < "3.1.1"
  147. shared-memory-ring-lwt
  148. slack
  149. ssh-agent
  150. tar >= "1.0.0" & < "2.0.0"
  151. tar-mirage < "3.0.0"
  152. tar-unix != "1.0.0" & < "2.0.0"
  153. tcpip < "6.3.0"
  154. tls-liquidsoap
  155. u2f < "0.1.2"
  156. uecc
  157. vchan < "6.0.1"
  158. vchan-unix
  159. vchan-xen
  160. vhd-format >= "0.12.0" & < "0.13.0"
  161. vhd-format-lwt < "0.13.0"
  162. vmnet
  163. xen-gnt
  164. xenstore >= "2.1.0" & < "2.3.0"

Conflicts

None

OCaml

Innovation. Community. Security.