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 
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <sys/mman.h>
20 #include <string.h>
21 #include <errno.h>
22 
23 #include <caml/mlvalues.h>
24 #include <caml/memory.h>
25 #include <caml/alloc.h>
26 #include <caml/custom.h>
27 #include <caml/fail.h>
28 #include <caml/callback.h>
29 
30 #include <xenctrl.h>
31 #include <xen/io/xs_wire.h>
32 
stub_header_size(void)33 CAMLprim value stub_header_size(void)
34 {
35 	CAMLparam0();
36 	CAMLreturn(Val_int(sizeof(struct xsd_sockmsg)));
37 }
38 
stub_header_of_string(value s)39 CAMLprim value stub_header_of_string(value s)
40 {
41 	CAMLparam1(s);
42 	CAMLlocal1(ret);
43 	const struct xsd_sockmsg *hdr;
44 
45 	if (caml_string_length(s) != sizeof(struct xsd_sockmsg))
46 		caml_failwith("xb header incomplete");
47 	ret = caml_alloc_tuple(4);
48 	hdr = (const struct xsd_sockmsg *) String_val(s);
49 	Store_field(ret, 0, Val_int(hdr->tx_id));
50 	Store_field(ret, 1, Val_int(hdr->req_id));
51 	Store_field(ret, 2, Val_int(hdr->type));
52 	Store_field(ret, 3, Val_int(hdr->len));
53 	CAMLreturn(ret);
54 }
55 
stub_string_of_header(value tid,value rid,value ty,value len)56 CAMLprim value stub_string_of_header(value tid, value rid, value ty, value len)
57 {
58 	CAMLparam4(tid, rid, ty, len);
59 	CAMLlocal1(ret);
60 	struct xsd_sockmsg xsd = {
61 		.type = Int_val(ty),
62 		.tx_id = Int_val(tid),
63 		.req_id = Int_val(rid),
64 		.len = Int_val(len),
65 	};
66 
67 	ret = caml_alloc_string(sizeof(struct xsd_sockmsg));
68 	memcpy((char *) String_val(ret), &xsd, sizeof(struct xsd_sockmsg));
69 
70 	CAMLreturn(ret);
71 }
72