ia64/xen-unstable

view tools/vnet/vnetd/marshal.c @ 3498:1d24a5b0b338

bitkeeper revision 1.1159.223.25 (41f2cb9aEKMZkZbvqBE0eXhpljlV4Q)

Description: fix path to python
There is no python2 in debian. Instead, use python.

From: Adam Heath <doogie@brainfood.com>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Sat Jan 22 21:54:34 2005 +0000 (2005-01-22)
parents f65b65977b19
children 0a4b76b6b5a0
line source
1 /*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>.
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of the
7 * License, or (at your option) any later version. This library is
8 * distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE.
11 * See the GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
18 #include <errno.h>
19 #include "sys_net.h"
20 #include "allocate.h"
21 #include "marshal.h"
23 #define MODULE_NAME "marshal"
24 #define DEBUG
25 #undef DEBUG
26 #include "debug.h"
28 #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0]))
30 /* Messages are coded as msgid followed by message fields.
31 * Initial message on any channel is hello - so can check version
32 * compatibility.
33 *
34 * char* -> uint16_t:n <n bytes>
35 * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes).
36 * optional fields go as '1' <val> or '0' (the 0/1 is 1 byte).
37 * lists go as ('1' <elt>)* '0'
38 */
40 int marshal_flush(IOStream *io){
41 int err = 0;
42 err = IOStream_flush(io);
43 return err;
44 }
46 int marshal_bytes(IOStream *io, void *s, uint32_t s_n){
47 int err = 0;
48 int n;
49 n = IOStream_write(io, s, s_n);
50 if(n < 0){
51 err = n;
52 } else if (n < s_n){
53 dprintf("> Wanted %d, got %d\n", s_n, n);
54 err = -EIO;
55 }
56 return err;
57 }
59 int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){
60 int err = 0;
61 int n;
62 //dprintf("> s_n=%d\n", s_n);
63 n = IOStream_read(io, s, s_n);
64 //dprintf("> n=%d\n", n);
65 if(n < 0){
66 err = n;
67 } else if(n < s_n){
68 dprintf("> Wanted %d, got %d\n", s_n, n);
69 err = -EIO;
70 }
71 //dprintf("< err=%d\n", err);
72 return err;
73 }
75 int marshal_uint8(IOStream *io, uint8_t x){
76 return marshal_bytes(io, &x, sizeof(x));
77 }
79 int unmarshal_uint8(IOStream *io, uint8_t *x){
80 return unmarshal_bytes(io, x, sizeof(*x));
81 }
83 int marshal_uint16(IOStream *io, uint16_t x){
84 x = htons(x);
85 return marshal_bytes(io, &x, sizeof(x));
86 }
88 int unmarshal_uint16(IOStream *io, uint16_t *x){
89 int err = 0;
90 err = unmarshal_bytes(io, x, sizeof(*x));
91 *x = ntohs(*x);
92 return err;
93 }
95 int marshal_int32(IOStream *io, int32_t x){
96 int err = 0;
97 //dprintf("> x=%d\n", x);
98 x = htonl(x);
99 err = marshal_bytes(io, &x, sizeof(x));
100 //dprintf("< err=%d\n", err);
101 return err;
102 }
104 int unmarshal_int32(IOStream *io, int32_t *x){
105 int err = 0;
106 //dprintf(">\n");
107 err = unmarshal_bytes(io, x, sizeof(*x));
108 *x = ntohl(*x);
109 //dprintf("< err=%d x=%d\n", err, *x);
110 return err;
111 }
113 int marshal_uint32(IOStream *io, uint32_t x){
114 int err = 0;
115 //dprintf("> x=%u\n", x);
116 x = htonl(x);
117 err = marshal_bytes(io, &x, sizeof(x));
118 //dprintf("< err=%d\n", err);
119 return err;
120 }
122 int unmarshal_uint32(IOStream *io, uint32_t *x){
123 int err = 0;
124 //dprintf(">\n");
125 err = unmarshal_bytes(io, x, sizeof(*x));
126 *x = ntohl(*x);
127 //dprintf("< err=%d x=%u\n", err, *x);
128 return err;
129 }
131 int marshal_uint64(IOStream *io, uint64_t x){
132 int err;
133 err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff));
134 if(err) goto exit;
135 err = marshal_uint32(io, (uint32_t) ( x & 0xffffffff));
136 exit:
137 return err;
138 }
140 int unmarshal_uint64(IOStream *io, uint64_t *x){
141 int err = 0;
142 uint32_t hi, lo;
143 err = unmarshal_uint32(io, &hi);
144 if(err) goto exit;
145 err = unmarshal_uint32(io, &lo);
146 *x = (((uint64_t) hi) << 32) | lo;
147 exit:
148 return err;
149 }
151 int marshal_net16(IOStream *io, net16_t x){
152 return marshal_bytes(io, &x, sizeof(x));
153 }
155 int unmarshal_net16(IOStream *io, net16_t *x){
156 int err = 0;
157 err = unmarshal_bytes(io, x, sizeof(*x));
158 return err;
159 }
161 int marshal_net32(IOStream *io, net32_t x){
162 return marshal_bytes(io, &x, sizeof(x));
163 }
165 int unmarshal_net32(IOStream *io, net32_t *x){
166 int err = 0;
167 err = unmarshal_bytes(io, x, sizeof(*x));
168 return err;
169 }
171 int marshal_string(IOStream *io, char *s, uint32_t s_n){
172 int err;
173 //dprintf("> s=%s\n", s);
174 err = marshal_uint32(io, s_n);
175 if(err) goto exit;
176 err = marshal_bytes(io, s, s_n);
177 exit:
178 //dprintf("< err=%d\n", err);
179 return err;
180 }
182 int unmarshal_string(IOStream *io, char *s, uint32_t s_n){
183 int err = 0, val_n = 0;
184 //dprintf(">\n");
185 err = unmarshal_uint32(io, &val_n);
186 if(err) goto exit;
187 if(val_n >= s_n){
188 err = -EINVAL;
189 goto exit;
190 }
191 err = unmarshal_bytes(io, s, val_n);
192 if(err) goto exit;
193 s[val_n] = '\0';
194 exit:
195 //dprintf("< err=%d s=%s\n", err, s);
196 return err;
197 }
199 int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){
200 int err = 0, val_n = 0;
201 char *val = NULL;
202 //dprintf(">\n");
203 err = unmarshal_uint32(io, &val_n);
204 if(err) goto exit;
205 val = allocate(val_n + 1);
206 if(!val){
207 err = -ENOMEM;
208 goto exit;
209 }
210 err = unmarshal_bytes(io, val, val_n);
211 if(err) goto exit;
212 val[val_n] = '\0';
213 exit:
214 if(err){
215 if(val) deallocate(val);
216 val = NULL;
217 val_n = 0;
218 }
219 *s = val;
220 if(s_n) *s_n = val_n;
221 //dprintf("< err=%d s=%s\n", err, *s);
222 return err;
223 }