package cstruct

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

Install

Dune Dependency

Authors

Maintainers

Sources

cstruct-v5.1.1.tbz
sha256=55d1f42cb85f7872fee499c5ed382aea17b06d55d1709e071d1ba85c7a09fef3
sha512=c3aa9a5a9125a1d022506a76fd7cdf32b21edcdc9df1202d8a9f382d02a28a33fea9a958f79e9302907ade1fce3f166b620c320aed6486e3efcc9a7464379cab

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: 25 Nov 2019

README

Cstruct -- access C-like structures directly from OCaml

v5.1.1

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

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. bigarray-compat
  2. dune
  3. ocaml >= "4.03.0"

Dev Dependencies (1)

  1. alcotest with-test

  1. albatross < "1.4.3"
  2. angstrom < "0.7.0"
  3. asn1-combinators < "0.2.6"
  4. balancer
  5. bip32
  6. buffer-pool
  7. carton < "0.4.3"
  8. certify
  9. chacha < "1.1.0"
  10. colombe < "0.2.0"
  11. conduit-mirage
  12. conex < "0.10.0"
  13. conex-mirage-crypto
  14. cowabloga
  15. crc
  16. cstruct-lwt >= "3.7.0" & < "5.2.0"
  17. cstruct-sexp < "5.2.0"
  18. current_git < "0.6.2"
  19. datakit-server
  20. depyt
  21. dns < "6.3.0"
  22. dns-client < "6.3.0"
  23. dns-forward
  24. dnssd
  25. dream < "1.0.0~alpha2"
  26. duff < "0.3"
  27. eqaf >= "0.8" & < "0.10"
  28. eqaf-cstruct
  29. fat-filesystem >= "0.13.0" & < "0.15.1"
  30. fiat-p256
  31. git < "3.6.0"
  32. github-hooks
  33. gluten-mirage < "0.3.0"
  34. gpt
  35. h2-mirage
  36. hacl-star >= "0.7.0"
  37. hacl_x25519
  38. hex >= "1.4.0"
  39. hkdf
  40. httpun-mirage
  41. httpun-ws-mirage
  42. hvsock < "3.0.0"
  43. io-page
  44. io-page-unix
  45. ipv6-multicast
  46. irmin-git
  47. irmin-indexeddb
  48. key-parsers < "1.2.1"
  49. launchd
  50. learn-ocaml-client
  51. ledgerwallet < "0.2.0"
  52. mirage-block >= "2.0.1"
  53. mirage-block-combinators < "3.0.2"
  54. mirage-block-lwt
  55. mirage-block-unix < "2.14.2"
  56. mirage-btrees
  57. mirage-channel = "4.0.1"
  58. mirage-channel-lwt
  59. mirage-conduit
  60. mirage-console-lwt
  61. mirage-crypto < "0.10.7"
  62. mirage-crypto-rng < "0.10.7"
  63. mirage-crypto-rng-mirage < "1.1.0"
  64. mirage-flow >= "3.0.0"
  65. mirage-flow-lwt
  66. mirage-fs >= "4.0.0"
  67. mirage-fs-lwt
  68. mirage-kv-lwt
  69. mirage-kv-unix
  70. mirage-net >= "4.0.0"
  71. mirage-net-lwt
  72. mirage-net-macosx
  73. mirage-net-solo5
  74. mirage-net-unikraft
  75. mirage-net-unix
  76. mirage-profile
  77. mirage-protocols-lwt
  78. mirage-random
  79. mirage-solo5 < "0.10.0"
  80. mirage-stack-lwt
  81. mirage-tc
  82. mirage-types-lwt
  83. mirage-xen
  84. monorobot
  85. mstruct
  86. nbd = "4.0.3"
  87. nocrypto
  88. noise
  89. pcap-format >= "0.5.2"
  90. pf-qubes
  91. plebeia < "2.0.0"
  92. ppx_cstruct = "5.1.1"
  93. protocol-9p = "2.0.1"
  94. protocol-9p-tool = "0.12.0" | >= "2.0.0" & < "2.0.2"
  95. protocol-9p-unix < "2.0.2"
  96. qcow >= "0.11.0"
  97. qcow-tool
  98. randomconv < "0.2.0"
  99. rawlink < "1.2"
  100. resp-mirage = "0.10.0"
  101. rfc6287
  102. salsa20 < "1.2.0"
  103. salsa20-core >= "0.3.0" & < "1.1.0"
  104. secp256k1-internal < "0.3.0"
  105. sendmail >= "0.4.1" & < "0.5.0"
  106. shared-memory-ring-lwt
  107. slack
  108. ssh-agent
  109. tls < "0.14.1"
  110. tls-liquidsoap
  111. uecc
  112. vchan < "6.0.1"
  113. vchan-unix
  114. vchan-xen
  115. vhd-format >= "0.12.0" & < "0.13.0"
  116. vhd-format-lwt < "0.13.0"
  117. vmnet
  118. x509 < "0.14.1"
  119. xen-gnt

Conflicts (1)

  1. js_of_ocaml < "3.5.0"
OCaml

Innovation. Community. Security.