Skip to content

Commit 4083d56

Browse files
committed
Clean-up comments and doc-comments
1 parent 3c49245 commit 4083d56

17 files changed

+865
-1120
lines changed

docs/examples.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,17 @@ let () =
9494
## Simple binary message parser
9595

9696
```ocaml
97-
(*
98-
+---------------+---------------+--------------------------+
99-
| type | subtype | parameter |
100-
+---------------+---------------+--------------------------+
101-
<-- 16 bits --> <-- 16 bits --> <------- 32 bits -------->
97+
(* +---------------+---------------+--------------------------+
98+
| type | subtype | parameter |
99+
+---------------+---------------+--------------------------+
100+
<-- 16 bits --> <-- 16 bits --> <------- 32 bits -------->
102101
103-
All fields are in network byte order.
104-
*)
102+
All fields are in network byte order. *)
105103
106104
let%bitstring make_message typ subtype param = {|
107-
typ : 16;
108-
subtype : 16;
109-
param : 32
105+
typ : 16;
106+
subtype : 16;
107+
param : 32
110108
|};;
111109
```
112110

examples/elf.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
(* Read an ELF (Linux binary) header.
2-
* $Id$
3-
*)
1+
(* Read an ELF (Linux binary) header. *)
42

53
open Printf
64

75
let () =
86
let filename = "/bin/ls" in
97
let bits = Bitstring.bitstring_of_file filename in
108
match%bitstring bits with
11-
| {| 0x7f : 8; "ELF" : 24 : string; (* ELF magic number *)
12-
_ : 12*8 : bitstring; (* ELF identifier *)
13-
e_type : 16 : littleendian; (* object file type *)
14-
e_machine : 16 : littleendian (* architecture *)
15-
|}
9+
| {| 0x7f : 8
10+
; "ELF" : 24 : string (* ELF magic number *)
11+
; _ : 12*8 : bitstring (* ELF identifier *)
12+
; e_type : 16 : littleendian (* object file type *)
13+
; e_machine : 16 : littleendian (* architecture *)
14+
|}
1615
-> printf "%s: ELF binary, type %d, arch %d\n" filename e_type e_machine
1716
| {| _ |} -> eprintf "%s: Not an ELF binary\n" filename
1817
;;

examples/ext3_superblock.ml

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
(* Parse an ext3 superblock.
2-
* $Id$
3-
*)
1+
(* Parse an ext3 superblock. *)
42

53
open Printf
64

@@ -12,58 +10,58 @@ let bits = Bitstring.bitstring_of_file "ext3_sb"
1210

1311
let () =
1412
match%bitstring bits with
15-
| {|s_inodes_count : 32 : littleendian; (* Inodes count *)
16-
s_blocks_count : 32 : littleendian; (* Blocks count *)
17-
s_r_blocks_count : 32 : littleendian; (* Reserved blocks count *)
18-
s_free_blocks_count : 32 : littleendian; (* Free blocks count *)
19-
s_free_inodes_count : 32 : littleendian; (* Free inodes count *)
20-
s_first_data_block : 32 : littleendian; (* First Data Block *)
21-
s_log_block_size : 32 : littleendian; (* Block size *)
22-
s_log_frag_size : 32 : littleendian; (* Fragment size *)
23-
s_blocks_per_group : 32 : littleendian; (* # Blocks per group *)
24-
s_frags_per_group : 32 : littleendian; (* # Fragments per group *)
25-
s_inodes_per_group : 32 : littleendian; (* # Inodes per group *)
26-
s_mtime : 32 : littleendian; (* Mount time *)
27-
s_wtime : 32 : littleendian; (* Write time *)
28-
s_mnt_count : 16 : littleendian; (* Mount count *)
29-
s_max_mnt_count : 16 : littleendian; (* Maximal mount count *)
30-
0xef53 : 16 : littleendian; (* Magic signature *)
31-
s_state : 16 : littleendian; (* File system state *)
32-
s_errors : 16 : littleendian; (* Behaviour when detecting errors *)
33-
s_minor_rev_level : 16 : littleendian; (* minor revision level *)
34-
s_lastcheck : 32 : littleendian; (* time of last check *)
35-
s_checkinterval : 32 : littleendian; (* max. time between checks *)
36-
s_creator_os : 32 : littleendian; (* OS *)
37-
s_rev_level : 32 : littleendian; (* Revision level *)
38-
s_def_resuid : 16 : littleendian; (* Default uid for reserved blocks *)
39-
s_def_resgid : 16 : littleendian; (* Default gid for reserved blocks *)
40-
s_first_ino : 32 : littleendian; (* First non-reserved inode *)
41-
s_inode_size : 16 : littleendian; (* size of inode structure *)
42-
s_block_group_nr : 16 : littleendian; (* block group # of this superblock *)
43-
s_feature_compat : 32 : littleendian; (* compatible feature set *)
44-
s_feature_incompat : 32 : littleendian; (* incompatible feature set *)
45-
s_feature_ro_compat : 32 : littleendian; (* readonly-compatible feature set *)
46-
s_uuid : 128 : string; (* 128-bit uuid for volume *)
47-
s_volume_name : 128 : string; (* volume name *)
48-
s_last_mounted : 512 : string; (* directory where last mounted *)
49-
s_algorithm_usage_bitmap : 32 : littleendian; (* For compression *)
50-
s_prealloc_blocks : 8; (* Nr of blocks to try to preallocate*)
51-
s_prealloc_dir_blocks : 8; (* Nr to preallocate for dirs *)
52-
s_reserved_gdt_blocks : 16 : littleendian;(* Per group desc for online growth *)
53-
s_journal_uuid : 128 : string; (* uuid of journal superblock *)
54-
s_journal_inum : 32 : littleendian; (* inode number of journal file *)
55-
s_journal_dev : 32 : littleendian; (* device number of journal file *)
56-
s_last_orphan : 32 : littleendian; (* start of list of inodes to delete *)
57-
s_hash_seed0 : 32 : littleendian; (* HTREE hash seed *)
58-
s_hash_seed1 : 32 : littleendian;
59-
s_hash_seed2 : 32 : littleendian;
60-
s_hash_seed3 : 32 : littleendian;
61-
s_def_hash_version : 8; (* Default hash version to use *)
62-
s_reserved_char_pad : 8;
63-
s_reserved_word_pad : 16 : littleendian;
64-
s_default_mount_opts : 32 : littleendian;
65-
s_first_meta_bg : 32 : littleendian; (* First metablock block group *)
66-
_ : 6080 : bitstring |}
13+
| {| s_inodes_count : 32 : littleendian (* Inodes count *)
14+
; s_blocks_count : 32 : littleendian (* Blocks count *)
15+
; s_r_blocks_count : 32 : littleendian (* Reserved blocks count *)
16+
; s_free_blocks_count : 32 : littleendian (* Free blocks count *)
17+
; s_free_inodes_count : 32 : littleendian (* Free inodes count *)
18+
; s_first_data_block : 32 : littleendian (* First Data Block *)
19+
; s_log_block_size : 32 : littleendian (* Block size *)
20+
; s_log_frag_size : 32 : littleendian (* Fragment size *)
21+
; s_blocks_per_group : 32 : littleendian (* # Blocks per group *)
22+
; s_frags_per_group : 32 : littleendian (* # Fragments per group *)
23+
; s_inodes_per_group : 32 : littleendian (* # Inodes per group *)
24+
; s_mtime : 32 : littleendian (* Mount time *)
25+
; s_wtime : 32 : littleendian (* Write time *)
26+
; s_mnt_count : 16 : littleendian (* Mount count *)
27+
; s_max_mnt_count : 16 : littleendian (* Maximal mount count *)
28+
; 0xef53 : 16 : littleendian (* Magic signature *)
29+
; s_state : 16 : littleendian (* File system state *)
30+
; s_errors : 16 : littleendian (* Behaviour when detecting errors *)
31+
; s_minor_rev_level : 16 : littleendian (* minor revision level *)
32+
; s_lastcheck : 32 : littleendian (* time of last check *)
33+
; s_checkinterval : 32 : littleendian (* max. time between checks *)
34+
; s_creator_os : 32 : littleendian (* OS *)
35+
; s_rev_level : 32 : littleendian (* Revision level *)
36+
; s_def_resuid : 16 : littleendian (* Default uid for reserved blocks *)
37+
; s_def_resgid : 16 : littleendian (* Default gid for reserved blocks *)
38+
; s_first_ino : 32 : littleendian (* First non-reserved inode *)
39+
; s_inode_size : 16 : littleendian (* size of inode structure *)
40+
; s_block_group_nr : 16 : littleendian (* block group # of this superblock *)
41+
; s_feature_compat : 32 : littleendian (* compatible feature set *)
42+
; s_feature_incompat : 32 : littleendian (* incompatible feature set *)
43+
; s_feature_ro_compat : 32 : littleendian (* readonly-compatible feature set *)
44+
; s_uuid : 128 : string (* 128-bit uuid for volume *)
45+
; s_volume_name : 128 : string (* volume name *)
46+
; s_last_mounted : 512 : string (* directory where last mounted *)
47+
; s_algorithm_usage_bitmap : 32 : littleendian (* For compression *)
48+
; s_prealloc_blocks : 8 (* Nr of blocks to try to preallocate*)
49+
; s_prealloc_dir_blocks : 8 (* Nr to preallocate for dirs *)
50+
; s_reserved_gdt_blocks : 16 : littleendian (* Per group desc for online growth *)
51+
; s_journal_uuid : 128 : string (* uuid of journal superblock *)
52+
; s_journal_inum : 32 : littleendian (* inode number of journal file *)
53+
; s_journal_dev : 32 : littleendian (* device number of journal file *)
54+
; s_last_orphan : 32 : littleendian (* start of list of inodes to delete *)
55+
; s_hash_seed0 : 32 : littleendian (* HTREE hash seed *)
56+
; s_hash_seed1 : 32 : littleendian
57+
; s_hash_seed2 : 32 : littleendian
58+
; s_hash_seed3 : 32 : littleendian
59+
; s_def_hash_version : 8 (* Default hash version to use *)
60+
; s_reserved_char_pad : 8
61+
; s_reserved_word_pad : 16 : littleendian
62+
; s_default_mount_opts : 32 : littleendian
63+
; s_first_meta_bg : 32 : littleendian (* First metablock block group *)
64+
; _ : 6080 : bitstring |}
6765
->
6866
(* Padding to the end of the block *)
6967
printf "ext3 superblock:\n";

examples/gif.ml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
(* GIF header parser.
2-
* $Id$
3-
*)
1+
(* GIF header parser. *)
42

53
open Printf
64

@@ -9,15 +7,16 @@ let () =
97
let filename = Sys.argv.(1) in
108
let bits = Bitstring.bitstring_of_file filename in
119
match%bitstring bits with
12-
| {|("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
13-
width : 16 : littleendian;
14-
height : 16 : littleendian;
15-
colormap : 1; (* Has colormap? *)
16-
colorbits : 3; (* Color res = colorbits+1 *)
17-
sortflag : 1;
18-
bps : 3; (* Bits/pixel = bps+1 *)
19-
bg : 8; (* Background colour. *)
20-
aspectratio : 8|}
10+
| {| ("GIF87a"|"GIF89a") : 6*8 : string (* GIF magic. *)
11+
; width : 16 : littleendian
12+
; height : 16 : littleendian
13+
; colormap : 1 (* Has colormap? *)
14+
; colorbits : 3 (* Color res = colorbits+1 *)
15+
; sortflag : 1
16+
; bps : 3 (* Bits/pixel = bps+1 *)
17+
; bg : 8 (* Background colour. *)
18+
; aspectratio : 8
19+
|}
2120
->
2221
printf "%s: GIF image:\n" filename;
2322
printf " size %d %d\n" width height;

examples/ipv4_header.ml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
(* Parse and display an IPv4 header from a file.
2-
* $Id$
3-
*)
1+
(* Parse and display an IPv4 header from a file. *)
42

53
open Printf
64

75
let header = Bitstring.bitstring_of_file "ipv4_header.dat"
86

97
let () =
108
match%bitstring header with
11-
| {|version : 4; hdrlen : 4; tos : 8; length : 16;
12-
identification : 16; flags : 3; fragoffset : 13;
13-
ttl : 8; protocol : 8; checksum : 16;
14-
source : 32;
15-
dest : 32;
16-
options : (hdrlen-5)*32 : bitstring;
17-
payload : -1 : bitstring|}
18-
when version = 4 ->
19-
printf "IPv%d:\n" version;
9+
| {| 4 : 4
10+
; hdrlen : 4; tos : 8
11+
; length : 16
12+
; identification : 16
13+
; flags : 3
14+
; fragoffset : 13
15+
; ttl : 8
16+
; protocol : 8
17+
; checksum : 16
18+
; source : 32
19+
; dest : 32
20+
; options : (hdrlen-5)*32 : bitstring
21+
; payload : -1 : bitstring
22+
|}
23+
->
24+
printf "IPv4:\n";
2025
printf " header length: %d * 32 bit words\n" hdrlen;
2126
printf " type of service: %d\n" tos;
2227
printf " packet length: %d bytes\n" length;

examples/libpcap.ml

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
(* Print out packets from a tcpdump / libpcap / wireshark capture file.
2-
* $Id$
3-
*
4-
* To test this, capture some data using:
5-
* /usr/sbin/tcpdump -s 1500 -w /tmp/dump
6-
* then analyze it using:
7-
* ./libpcap /tmp/dump
8-
*
9-
* The file format is documented here:
10-
* http://wiki.wireshark.org/Development/LibpcapFileFormat
11-
*
12-
* libpcap endianness is determined at runtime.
13-
*)
2+
3+
To test this, capture some data using:
4+
/usr/sbin/tcpdump -s 1500 -w /tmp/dump
5+
then analyze it using:
6+
./libpcap /tmp/dump
7+
8+
The file format is documented here:
9+
http://wiki.wireshark.org/Development/LibpcapFileFormat
10+
11+
libpcap endianness is determined at runtime. *)
1412

1513
open Printf
1614

@@ -35,66 +33,67 @@ and endian_of = function
3533

3634
and libpcap_header bits =
3735
match%bitstring bits with
38-
| {|((0xa1b2c3d4_l|0xd4c3b2a1_l) as magic) : 32; (* magic number *)
39-
major : 16 : endian (endian_of magic); (* version *)
40-
minor : 16 : endian (endian_of magic);
41-
timezone : 32 : endian (endian_of magic); (* timezone correction (secs)*)
42-
_ : 32 : endian (endian_of magic); (* always 0 apparently *)
43-
snaplen : 32 : endian (endian_of magic); (* max length of capt pckts *)
44-
network : 32 : endian (endian_of magic); (* data link layer type *)
45-
rest : -1 : bitstring
36+
| {| ((0xa1b2c3d4_l|0xd4c3b2a1_l) as magic) : 32 (* magic number *)
37+
; major : 16 : endian (endian_of magic) (* version *)
38+
; minor : 16 : endian (endian_of magic)
39+
; timezone : 32 : endian (endian_of magic) (* timezone correction (secs)*)
40+
; _ : 32 : endian (endian_of magic) (* always 0 apparently *)
41+
; snaplen : 32 : endian (endian_of magic) (* max length of capt pckts *)
42+
; network : 32 : endian (endian_of magic) (* data link layer type *)
43+
; rest : -1 : bitstring
4644
|}
4745
-> endian_of magic, (major, minor, timezone, snaplen, network), rest
4846
| {|_|} -> failwith "not a libpcap/tcpdump packet capture file"
4947

5048
and libpcap_packet e file_header bits =
5149
match%bitstring bits with
52-
| {|ts_sec : 32 : endian (e); (* packet timestamp seconds *)
53-
ts_usec : 32 : endian (e); (* packet timestamp microseconds *)
54-
incl_len : 32 : endian (e); (* packet length saved in this file *)
55-
orig_len : 32 : endian (e); (* packet length originally on wire *)
56-
pkt_data : Int32.to_int incl_len*8 : bitstring;
57-
rest : -1 : bitstring
58-
|}
50+
| {| ts_sec : 32 : endian (e) (* packet timestamp seconds *)
51+
; ts_usec : 32 : endian (e) (* packet timestamp microseconds *)
52+
; incl_len : 32 : endian (e) (* packet length saved in this file *)
53+
; orig_len : 32 : endian (e) (* packet length originally on wire *)
54+
; pkt_data : Int32.to_int incl_len*8 : bitstring
55+
; rest : -1 : bitstring
56+
|}
5957
-> (ts_sec, ts_usec, incl_len, orig_len), pkt_data, rest
6058
| {|_|} -> raise End_of_file
6159

6260
and decode_and_print_packet file_header pkt_header pkt_data =
6361
let ts_sec, ts_usec, _, orig_len = pkt_header in
6462
printf "%ld.%ld %ldB " ts_sec ts_usec orig_len;
65-
(* Assume an ethernet frame containing an IPv4/6 packet. We ignore
66-
* the ethertype field and determine the IP version from the packet
67-
* itself. If it doesn't match our assumptions, hexdump it.
68-
*)
63+
(* Assume an ethernet frame containing an IPv4/6 packet. We ignore the
64+
ethertype field and determine the IP version from the packet itself. If it
65+
doesn't match our assumptions, hexdump it. *)
6966
(match%bitstring pkt_data with
70-
| {|d0 : 8; d1 : 8; d2 : 8; d3 : 8; d4 : 8; d5 : 8; (* ether dest *)
71-
s0 : 8; s1 : 8; s2 : 8; s3 : 8; s4 : 8; s5 : 8; (* ether src *)
72-
_ : 16; (* ethertype *)
73-
packet : -1 : bitstring (* payload *)
67+
| {| d0 : 8; d1 : 8; d2 : 8; d3 : 8; d4 : 8; d5 : 8 (* ether dest *)
68+
; s0 : 8; s1 : 8; s2 : 8; s3 : 8; s4 : 8; s5 : 8 (* ether src *)
69+
; _ : 16 (* ethertype *)
70+
; packet : -1 : bitstring (* payload *)
7471
|}
7572
->
7673
printf "%x:%x:%x:%x:%x:%x < %x:%x:%x:%x:%x:%x " d0 d1 d2 d3 d4 d5 s0 s1 s2 s3 s4 s5;
7774
(match%bitstring packet with
78-
| {|4 : 4; (* IPv4 *)
79-
hdrlen : 4; tos : 8; length : 16;
80-
identification : 16; flags : 3; fragoffset : 13;
81-
ttl : 8; protocol : 8; checksum : 16;
82-
s0 : 8; s1 : 8; s2 : 8; s3 : 8;
83-
d0 : 8; d1 : 8; d2 : 8; d3 : 8;
84-
_(*options*) : (hdrlen-5)*32 : bitstring;
85-
_(*payload*) : -1 : bitstring|}
75+
| {| 4 : 4
76+
; hdrlen : 4 ; tos : 8 ; length : 16
77+
; identification : 16 ; flags : 3 ; fragoffset : 13
78+
; ttl : 8; protocol : 8; checksum : 16
79+
; s0 : 8; s1 : 8; s2 : 8; s3 : 8
80+
; d0 : 8; d1 : 8; d2 : 8; d3 : 8
81+
; _(*options*) : (hdrlen-5)*32 : bitstring
82+
; _(*payload*) : -1 : bitstring
83+
|}
8684
-> printf "IPv4 %d.%d.%d.%d < %d.%d.%d.%d " s0 s1 s2 s3 d0 d1 d2 d3
87-
| {|6 : 4; (* IPv6 *)
88-
tclass : 8; flow : 20;
89-
length : 16; nexthdr : 8; ttl : 8;
90-
_(*source*) : 128 : bitstring;
91-
_(*dest*) : 128 : bitstring;
92-
_(*payload*) : -1 : bitstring|}
85+
| {| 6 : 4
86+
; tclass : 8; flow : 20
87+
; length : 16; nexthdr : 8; ttl : 8
88+
; _(*source*) : 128 : bitstring
89+
; _(*dest*) : 128 : bitstring
90+
; _(*payload*) : -1 : bitstring
91+
|}
9392
-> printf "IPv6 "
94-
| {|_|} ->
93+
| {| _ |} ->
9594
printf "\n";
9695
Bitstring.hexdump_bitstring stdout packet)
97-
| {|_|} ->
96+
| {| _ |} ->
9897
printf "\n";
9998
Bitstring.hexdump_bitstring stdout pkt_data);
10099
printf "\n"

0 commit comments

Comments
 (0)