1(*
2 * Copyright (C) 2006-2007 XenSource Ltd.
3 * Copyright (C) 2008      Citrix Ltd.
4 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *)
16
17type pkt =
18  {
19    tid: int;
20    rid: int;
21    ty: Op.operation;
22    len: int;
23    buf: Buffer.t;
24  }
25
26external header_size: unit -> int = "stub_header_size"
27external header_of_string_internal: string -> int * int * int * int
28  = "stub_header_of_string"
29
30let xenstore_payload_max = 4096 (* xen/include/public/io/xs_wire.h *)
31let xenstore_rel_path_max = 2048 (* xen/include/public/io/xs_wire.h *)
32
33let of_string s =
34  let tid, rid, opint, dlen = header_of_string_internal s in
35  (* A packet which is bigger than xenstore_payload_max is illegal.
36     	   This will leave the guest connection is a bad state and will
37     	   be hard to recover from without restarting the connection
38     	   (ie rebooting the guest) *)
39  let dlen = max 0 (min xenstore_payload_max dlen) in
40  {
41    tid = tid;
42    rid = rid;
43    ty = (Op.of_cval opint);
44    len = dlen;
45    buf = Buffer.create dlen;
46  }
47
48let append pkt s sz =
49  if Buffer.length pkt.buf + sz > xenstore_payload_max then failwith "Buffer.add: cannot grow buffer";
50  Buffer.add_substring pkt.buf s 0 sz
51
52let to_complete pkt =
53  pkt.len - (Buffer.length pkt.buf)
54