• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    OCaml
  • License
    ISC License
  • Created over 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Map OCaml arrays onto C-like structs

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.

More Repositories

1

mirage

MirageOS is a library operating system that constructs unikernels
OCaml
2,520
star
2

irmin

Irmin is a distributed database that follows the same design principles as Git
OCaml
1,742
star
3

ocaml-cohttp

An OCaml library for HTTP clients and servers using Lwt or Async
OCaml
644
star
4

alcotest

A lightweight and colourful test framework
OCaml
447
star
5

ocaml-git

Pure OCaml Git format and protocol
OCaml
349
star
6

mirage-tcpip

TCP/IP networking stack in pure OCaml, using the Mirage platform libraries. Includes IPv4/6, ICMP, and UDP/TCP support.
OCaml
321
star
7

jitsu

A DNS server that automatically starts unikernels on demand
OCaml
307
star
8

mirage-skeleton

Examples of simple MirageOS apps
OCaml
224
star
9

qubes-mirage-firewall

A Mirage firewall VM for QubesOS
OCaml
207
star
10

mirage-www

Website infrastructure and content for mirage.io
HTML
162
star
11

decompress

Pure OCaml implementation of Zlib.
OCaml
116
star
12

ocaml-cow

Caml on the Web (COW) is a set of parsers and syntax extensions to let you manipulate HTML, CSS, XML, JSON and Markdown directly from OCaml code.
OCaml
105
star
13

awa-ssh

Purely functional SSH library in ocaml.
OCaml
104
star
14

ocaml-dns

OCaml implementation of the DNS protocol
OCaml
102
star
15

ocaml-solo5

Freestanding OCaml runtime
C
101
star
16

ocaml-github

GitHub APIv3 OCaml bindings
OCaml
100
star
17

capnp-rpc

Cap'n Proto RPC implementation
OCaml
97
star
18

ocaml-rpc

Light library to deal with RPCs in OCaml
OCaml
95
star
19

ocaml-uri

RFC3986 URI parsing library for OCaml
OCaml
93
star
20

digestif

Simple hash algorithms in OCaml
OCaml
88
star
21

ocaml-conduit

Dereference URIs into communication channels for Async or Lwt
OCaml
84
star
22

mirage-crypto

Cryptographic primitives for OCaml, in OCaml (also used in MirageOS)
C
77
star
23

mirage-platform

Archived, see https://github.com/mirage/mirage/issues/1159 for details. Old: Core platform libraries for Mirage (UNIX and Xen). This provides the `OS` library which handles timers, device setup and the main loop, as well as the runtime for the Xen unikernel.
C
77
star
24

xen

Unofficial mirror of xenbits.xen.org/xen.git
C
72
star
25

ocaml-crunch

Convert a filesystem into a static OCaml module
OCaml
70
star
26

ocaml-9p

An OCaml/Mirage-friendly implementation of the 9P protocol
OCaml
63
star
27

functoria

A DSL to invoke otherworldly functors
OCaml
63
star
28

mini-os

Mirror of the Xen MiniOS Git from git://xenbits.xen.org/mini-os.git
C
63
star
29

mirage-qubes

Mirage support for writing QubesOS AppVM unikernels
OCaml
62
star
30

xen-arm-builder

Archived - the Xen and ARM support in MirageOS has been superseeded by our PVH support - Build an SDcard image for Xen/ARM, for a Cubieboard
Shell
57
star
31

charrua

A DHCP library in OCaml
OCaml
55
star
32

orm

Object Relational Mapper extension
OCaml
54
star
33

ke

Fast implementation of queue in OCaml
HTML
52
star
34

eqaf

Constant time equal function to avoid timing attacks in OCaml
OCaml
51
star
35

conan

Like detective conan, find clue about the type of the file
OCaml
49
star
36

prometheus

OCaml library for reporting metrics to a Prometheus server
OCaml
49
star
37

ocaml-matrix

Implementation of a matrix server in OCaml for MirageOS
OCaml
49
star
38

ocaml-tar

Pure OCaml library to read and write tar files
OCaml
49
star
39

ocaml-vchan

Pure OCaml implementation of the "vchan" shared-memory communication protocol
OCaml
46
star
40

metrics

Infrastructure to collect metrics from OCaml applications.
OCaml
46
star
41

bechamel

Agnostic benchmark in OCaml (proof-of-concept)
OCaml
44
star
42

wodan

A Mirage filesystem library
OCaml
44
star
43

ocaml-base64

Base64 encoding and decoding in OCaml
OCaml
43
star
44

colombe

Implementation of SMTP protocols in OCaml
OCaml
42
star
45

ocaml-ipaddr

A library for manipulation of IP (and MAC) address representations
OCaml
40
star
46

mrmime

What do you mean?
OCaml
40
star
47

ezjsonm

An easy interface on top of the Jsonm library.
OCaml
40
star
48

index

A platform-agnostic multi-level index
OCaml
34
star
49

bloomf

Efficient Bloom filters for OCaml
OCaml
34
star
50

emile

& images
OCaml
31
star
51

mirage-nat

library for network address translation intended for use with mirage unikernels
OCaml
31
star
52

repr

OCaml
31
star
53

ocaml-hex

Hexadecimal converter
OCaml
29
star
54

ocaml-diet

A simple implementation of Discrete Interval Encoding Trees
OCaml
28
star
55

ptt

Postes, Télégraphes et Téléphones
OCaml
26
star
56

ocaml-fat

Read and write FAT format filesystems from OCaml
OCaml
26
star
57

encore

Synonym of angkor
OCaml
25
star
58

ocaml-magic-mime

Convert file extensions to MIME types
OCaml
24
star
59

irmin-server

A high-performance server for Irmin
OCaml
24
star
60

optint

Library to provide a fast integer (x64 arch) or allocated int32 (x84 arch)
OCaml
24
star
61

ocaml-lazy-trie

Lazy prefix trees in OCaml
OCaml
23
star
62

qubes-mirage-skeleton

An example Mirage unikernel that runs as a Qubes AppVM
OCaml
23
star
63

ocaml-pcap

OCaml code for generating and analysing pcap (packet capture) files
OCaml
22
star
64

duff

Pure OCaml implementation of libXdiff (Rabin's fingerprint)
OCaml
21
star
65

hacl

Archived. Curve25519 support has been integrated into mirage-crypto-ec (via fiat-crypto). Hacl bindings are available from the hacl-star opam package. OCaml bindings for HACL* elliptic curves
C
21
star
66

arp

Address resolution protocol (ARP) implementation in OCaml targeting MirageOS
OCaml
21
star
67

shared-memory-ring

Xen-style shared memory rings
OCaml
20
star
68

irmin-rpc

RPC client/server for Irmin
OCaml
20
star
69

typebeat

Parsing of the Content-Type header in pure OCaml
OCaml
20
star
70

mirage-solo5

Solo5 core platform libraries for MirageOS
OCaml
20
star
71

ocaml-tuntap

Bindings to UNIX tuntap facilities
OCaml
20
star
72

mirage-lambda

An eDSL for MirageOS apps
OCaml
19
star
73

merge-queues

Mergeable queues
OCaml
19
star
74

ocaml-qcow

Pure OCaml code for parsing, printing, modifying .qcow format data
OCaml
19
star
75

ocaml-vmnet

NATed networking on MacOS X using the vmnet framework
OCaml
18
star
76

mirage-xen

Xen core platform libraries for MirageOS
C
18
star
77

mirage-profile

Collect profiling information
OCaml
18
star
78

cactus

A Btree library in OCaml
OCaml
18
star
79

mirage-clock

Portable clock implementation for Unix and Xen
OCaml
18
star
80

ocaml-mbr

A simple library for manipulating Master Boot Records
OCaml
18
star
81

spamtacus

Ocaml modular spam filter
OCaml
16
star
82

mirage-dev

Development OPAM repository for work-in-progress packages
16
star
83

ocaml-fsevents

macOS bindings to the FSEvents API
OCaml
16
star
84

mirage-fs-unix

Unix Filesystem passthrough for MirageOS
OCaml
16
star
85

mirage-vnetif

Virtual network interface and software bridge for Mirage
OCaml
16
star
86

irmin-rs

Rust
15
star
87

ocaml-hvsock

Bindings for hypervisor sockets, for Linux, Windows and macOS (via Hyperkit)
OCaml
15
star
88

ca-certs

Detect root CA certificates from the operating system
OCaml
15
star
89

irmin-watcher

Portable implementation of the Irmin Watch API
OCaml
15
star
90

checkseum

C
15
star
91

mirage-handbook

WIP Handbook for MirageOS
14
star
92

retreat.mirage.io

Microsite for the MirageOS hack retreats
OCaml
14
star
93

mmap

File mapping
OCaml
13
star
94

mirage-decks

These are the MirageOS slide decks, written as a self-hosting unikernel
HTML
13
star
95

ezxmlm

Like the tax form, this is an easier interface for quick n dirty XMLM scripts
OCaml
13
star
96

mirage-unix

Unix core platform libraries for MirageOS
OCaml
13
star
97

ocaml-gpt

A simple library for manipulating GUID partition tables
OCaml
12
star
98

irmin.org

Irmin website
CSS
12
star
99

mirage-console

Portable console handling for Mirage applications
OCaml
12
star
100

mirage-net-xen

Xen Netfront and Netback ethernet device drivers for Mirage
OCaml
12
star