package cstruct

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

Install

Dune Dependency

Authors

Maintainers

Sources

cstruct-3.1.1.tbz
sha256=621bc539c4495aa59cb96a712b694684690c0f7533cb26ed8a2ed3d0b6ee92f4
md5=9834ebf3e161ae62577962332b544c24

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

README

Cstruct -- access C-like structures directly from OCaml

v3.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-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 jbuilder, using make or jbuilder build directly. Since everything is built via jbuilder, you can also place this repository within a wider jbuilder 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 (5)

  1. base-bytes
  2. sexplib
  3. ocplib-endian
  4. jbuilder >= "1.0+beta10"
  5. ocaml >= "4.03.0"

Dev Dependencies (1)

  1. ounit with-test

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

Conflicts

None

OCaml

Innovation. Community. Security.