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 *)
31
32let of_string s =
33	let tid, rid, opint, dlen = header_of_string_internal s in
34	(* A packet which is bigger than xenstore_payload_max is illegal.
35	   This will leave the guest connection is a bad state and will
36	   be hard to recover from without restarting the connection
37	   (ie rebooting the guest) *)
38	let dlen = min xenstore_payload_max dlen in
39	{
40		tid = tid;
41		rid = rid;
42		ty = (Op.of_cval opint);
43		len = dlen;
44		buf = Buffer.create dlen;
45	}
46
47let append pkt s sz =
48	if pkt.len > 4096 then failwith "Buffer.add: cannot grow buffer";
49	Buffer.add_string pkt.buf (String.sub s 0 sz)
50
51let to_complete pkt =
52	pkt.len - (Buffer.length pkt.buf)
53