Skip to content
Browse files

sync build with latest skeleton

  • Loading branch information...
1 parent ecc6994 commit d31394726a0158e35370b375268d870a192a9570 @avsm committed Jun 17, 2012
View
18 repos/cstruct/Makefile
@@ -1,9 +1,15 @@
-VERSION=0.2
+.PHONY: all clean
-LIBS = cstruct
-SYNTAX = cstruct-syntax
-RUNTIME_C = libcstruct
+all:
+ ./cmd configure
+ ./cmd build
-NAME = cstruct
+install:
+ ./cmd install
-include Makefile.inc
+clean:
+ ./cmd clean
+
+test: all
+ ./cmd test
+
View
1 repos/cstruct/Makefile.inc
View
6 repos/cstruct/_tags
@@ -1,2 +1,4 @@
-<runtime/*.c>: include_syscaml
-
+<lib_test/*>: use_lib, custom
+<syntax/*>: build_syntax
+not <syntax/*>: use_syntax
+true: camlp4o
View
7 repos/cstruct/_vars
@@ -0,0 +1,7 @@
+NAME=cstruct
+VERSION=0.1dev
+SYNTAX="cstruct-syntax"
+DEPS="bigarray"
+RUNTIME="cstruct"
+LIB=cstruct
+TESTS="basic enum pcap"
View
1 repos/cstruct/configure
View
93 repos/cstruct/lib_test/basic.ml
@@ -0,0 +1,93 @@
+(*
+ * Copyright (c) 2012 Anil Madhavapeddy <anil@recoil.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *)
+
+cstruct foo {
+ uint8_t a;
+ uint16_t b;
+ uint32_t c;
+ uint8_t d[8]
+} as big_endian
+
+cstruct bar {
+ uint8_t a;
+ uint16_t b;
+ uint32_t c;
+ uint8_t d[8]
+} as big_endian
+
+let _ =
+ (* Test basic set/get functions *)
+ let be = Bigarray.(Array1.create char c_layout sizeof_foo) in
+ for i = 0 to 255 do
+ set_bar_a be i;
+ assert(get_bar_a be = i)
+ done;
+ let le = Bigarray.(Array1.create char c_layout sizeof_bar) in
+ for i = 0 to 255 do
+ set_foo_a le i;
+ assert(get_foo_a le = i)
+ done;
+ let be = Bigarray.(Array1.create char c_layout sizeof_foo) in
+ for i = 0 to 65535 do
+ set_bar_b be i;
+ assert(get_bar_b be = i)
+ done;
+ let le = Bigarray.(Array1.create char c_layout sizeof_bar) in
+ for i = 0 to 65535 do
+ set_foo_b le i;
+ assert(get_foo_b le = i)
+ done;
+ let be = Bigarray.(Array1.create char c_layout sizeof_foo) in
+ let rec fn = function
+ |i when i < 0l -> ()
+ |i ->
+ set_bar_c be i;
+ assert(get_bar_c be = i);
+ fn (Int32.sub i 0x10l)
+ in fn 0xffffffff_l;
+ (* Get/set buffers and blits *)
+ let le = Bigarray.(Array1.create char c_layout sizeof_bar) in
+ let rec fn = function
+ |i when i < 0l -> ()
+ |i ->
+ set_foo_c le i;
+ assert(get_foo_c le = i);
+ fn (Int32.sub i 0x10l)
+ in fn 0xffffffff_l;
+ let s1 = "deadbeef" in
+ set_foo_d s1 0 be;
+ assert(copy_foo_d be = s1);
+ let sb1 = get_foo_d be in
+ blit_bar_d sb1 0 le;
+ assert(copy_bar_d le = s1);
+ Printf.printf "%s %s\n" (copy_foo_d be) (copy_bar_d le);
+ (* Create sub-view and shift it back *)
+ let be = Bigarray.(Array1.create char c_layout sizeof_foo) in
+ set_foo_a be 7;
+ set_foo_b be 44;
+ set_foo_c be 0xbeef_l;
+ set_foo_d "abcdefgh" 0 be;
+ (* shifting the base array should fail *)
+ assert(not (Cstruct.shift_left be 1));
+ (* get a subview *)
+ let be2 = Cstruct.shift be 3 in
+ assert(Cstruct.BE.get_uint32 be2 0 = 0xbeef_l);
+ (* shift it back *)
+ assert(Cstruct.shift_left be2 3);
+ assert(Cstruct.BE.get_uint32 be2 3 = 0xbeef_l);
+ assert(not (Cstruct.shift_left be2 1));
+ assert(get_foo_b be2 = 44);
+ assert(get_foo_a be2 = 7)
View
54 repos/cstruct/lib_test/enum.ml
@@ -0,0 +1,54 @@
+(*
+ * Copyright (c) 2012 Anil Madhavapeddy <anil@recoil.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *)
+
+cenum foo64 {
+ ONE64;
+ TWO64;
+ THREE64
+} as uint64_t
+
+cenum foo32 {
+ ONE32;
+ TWO32 = 3;
+ THREE32
+} as uint32_t
+
+cenum foo16 {
+ ONE16;
+ TWO16;
+ THREE16
+} as uint16_t
+
+cenum foo8 {
+ ONE8;
+ TWO8;
+ THREE8
+} as uint8_t
+
+let _ =
+ ignore(int_to_foo64 2L);
+ ignore(int_to_foo32 1l);
+ ignore(int_to_foo16 1);
+ ignore(int_to_foo8 1);
+ ignore(foo64_to_int ONE64);
+ ignore(foo32_to_int ONE32);
+ ignore(foo16_to_int ONE16);
+ ignore(foo8_to_int ONE8);
+ assert(foo32_to_int TWO32 = 3l);
+ assert(foo32_to_int THREE32 = 1l);
+ assert(int_to_foo32 3l = Some (TWO32));
+ assert(int_to_foo32 1l = Some (THREE32));
+ print_endline (foo8_to_string ONE8)
View
BIN repos/cstruct/lib_test/http.cap
Binary file not shown.
View
154 repos/cstruct/lib_test/pcap.ml
@@ -0,0 +1,154 @@
+(*
+ * Copyright (c) 2012 Anil Madhavapeddy <anil@recoil.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *)
+
+cstruct pcap_header {
+ uint32_t magic_number; (* magic number *)
+ uint16_t version_major; (* major version number *)
+ uint16_t version_minor; (* minor version number *)
+ uint32_t thiszone; (* GMT to local correction *)
+ uint32_t sigfigs; (* accuracy of timestamps *)
+ uint32_t snaplen; (* max length of captured packets, in octets *)
+ uint32_t network (* data link type *)
+} as little_endian
+
+cstruct pcap_packet {
+ uint32_t ts_sec; (* timestamp seconds *)
+ uint32_t ts_usec; (* timestamp microseconds *)
+ uint32_t incl_len; (* number of octets of packet saved in file *)
+ uint32_t orig_len (* actual length of packet *)
+} as little_endian
+
+cstruct ethernet {
+ uint8_t dst[6];
+ uint8_t src[6];
+ uint16_t ethertype
+} as big_endian
+
+cstruct ipv4 {
+ uint8_t hlen_version;
+ uint8_t tos;
+ uint16_t len;
+ uint16_t id;
+ uint16_t off;
+ uint8_t ttl;
+ uint8_t proto;
+ uint16_t csum;
+ uint8_t src[4];
+ uint8_t dst[4]
+} as big_endian
+
+cstruct tcpv4 {
+ uint16_t src_port;
+ uint16_t dst_port;
+ uint32_t seqnum;
+ uint32_t acknum;
+ uint16_t offset_flags;
+ uint16_t window;
+ uint16_t checksum;
+ uint16_t urg
+} as big_endian
+
+open Printf
+
+let num_packets = ref 0
+
+let mac_to_string buf =
+ let i n = Cstruct.get_uint8 buf n in
+ sprintf "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
+ (i 0) (i 1) (i 2) (i 3) (i 4) (i 5)
+
+let print_packet p =
+ let dst_mac = mac_to_string (get_ethernet_dst p) in
+ let src_mac = mac_to_string (get_ethernet_src p) in
+ let ethertype = get_ethernet_ethertype p in
+ printf "ether %s -> %s etype %x\n" src_mac dst_mac ethertype;
+ match ethertype with
+ |0x0800 -> begin
+ let ip = Cstruct.shift p sizeof_ethernet in
+ let version = get_ipv4_hlen_version ip lsr 4 in
+ let hlen = (get_ipv4_hlen_version ip land 0xf) * 4 in
+ let ttl = get_ipv4_ttl ip in
+ let proto = get_ipv4_proto ip in
+ printf "ipv%d hlen %d ttl %d proto %d\n" version hlen ttl proto;
+ match proto with
+ |6 -> begin (* tcp *)
+ let tcp = Cstruct.shift ip sizeof_ipv4 in
+ let off = 0 in
+ let x = get_tcpv4_offset_flags tcp in
+ let data_offset = (x lsr 12) * 4 in
+ let options =
+ match data_offset - sizeof_tcpv4 with
+ |0 -> 0
+ |n -> n (* TODO parse *)
+ in
+ let payload = Cstruct.shift tcp data_offset in
+ let fin = (x land 1) = 1 in
+ let syn = (x land 2) = 2 in
+ let flags = "?" in
+ printf "tcpv4 port %d->%d seq %lu ack %lu win %d off %d flags %s opt %d fin %b syn %b\n"
+ (get_tcpv4_src_port tcp) (get_tcpv4_dst_port tcp) (get_tcpv4_seqnum tcp)
+ (get_tcpv4_acknum tcp) (get_tcpv4_window tcp) off flags options fin syn;
+ printf "%S\n" (Cstruct.to_string payload)
+ end
+ |_ -> printf "unknown ip proto %d\n" proto
+ end
+ |_ -> printf "unknown body\n"
+
+let rec print_pcap_packet (hdr,pkt) =
+ printf "\n** %lu.%lu bytes %lu (of %lu)\n"
+ (get_pcap_packet_ts_sec hdr)
+ (get_pcap_packet_ts_usec hdr)
+ (get_pcap_packet_incl_len hdr)
+ (get_pcap_packet_orig_len hdr);
+ print_packet pkt
+
+let print_pcap_header buf =
+ let magic = get_pcap_header_magic_number buf in
+ let endian =
+ match magic with
+ |0xa1b2c3d4l -> "bigendian"
+ |0xd4c3b2a1l -> "littlendian"
+ |_ -> "not a pcap file"
+ in
+ printf "pcap_header (len %d)\n" sizeof_pcap_header;
+ printf "magic_number %lx (%s)\n%!" magic endian;
+ printf "version %d %d\n"
+ (get_pcap_header_version_major buf) (get_pcap_header_version_minor buf);
+ printf "timezone shift %lu\n" (get_pcap_header_thiszone buf);
+ printf "timestamp accuracy %lu\n" (get_pcap_header_sigfigs buf);
+ printf "snaplen %lu\n" (get_pcap_header_snaplen buf);
+ printf "lltype %lx\n" (get_pcap_header_network buf)
+
+let parse () =
+ let fd = Unix.(openfile "http.cap" [O_RDONLY] 0) in
+ let buf = Bigarray.(Array1.map_file fd Bigarray.char c_layout false (-1)) in
+ printf "total pcap file length %d\n" (Cstruct.len buf);
+
+ let header, body = Cstruct.split buf sizeof_pcap_header in
+ print_pcap_header header;
+
+ let packets = Cstruct.iter
+ (fun buf -> Some (sizeof_pcap_packet + (Int32.to_int (get_pcap_packet_incl_len buf))))
+ (fun buf -> buf, (Cstruct.shift buf sizeof_pcap_packet))
+ body
+ in
+ let num_packets = Cstruct.fold
+ (fun a packet -> print_pcap_packet packet; (a+1))
+ packets 0
+ in
+ printf "num_packets %d\n" num_packets
+
+let _ = parse ()
View
2 repos/cstruct/syntax/_tags
@@ -1,2 +0,0 @@
-true: pp(camlp4o -I +camlp4 -parser Camlp4QuotationCommon -parser Camlp4OCamlOriginalQuotationExpander -parser Camlp4GrammarParser)
-true: include_camlp4

0 comments on commit d313947

Please sign in to comment.
Something went wrong with that request. Please try again.