2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
9 * Copyright (c) 2007, The Storage Networking Industry Association.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of The Storage Networking Industry Association (SNIA)
23 * nor the names of its contributors may be used to endorse or promote
24 * products derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Provides encode/decode routines for all door servers/clients.
50 ndmp_door_decode_start(char *ptr
, int size
)
52 ndmp_door_ctx_t
*ctx
= malloc(sizeof (ndmp_door_ctx_t
));
54 ctx
->start_ptr
= ctx
->ptr
= ptr
;
55 ctx
->end_ptr
= ptr
+ size
;
62 ndmp_door_decode_finish(ndmp_door_ctx_t
*ctx
)
64 int status
= ctx
->status
;
65 if ((status
== 0) && (ctx
->ptr
!= ctx
->end_ptr
)) {
73 ndmp_door_encode_start(char *ptr
, int size
)
75 ndmp_door_ctx_t
*ctx
= malloc(sizeof (ndmp_door_ctx_t
));
77 ctx
->start_ptr
= ctx
->ptr
= ptr
;
78 ctx
->end_ptr
= ptr
+ size
;
85 ndmp_door_encode_finish(ndmp_door_ctx_t
*ctx
, unsigned int *used
)
87 int status
= ctx
->status
;
89 if (ctx
->ptr
< ctx
->end_ptr
) {
90 /*LINTED E_PTRDIFF_OVERFLOW*/
91 *used
= ctx
->ptr
- ctx
->start_ptr
;
101 ndmp_door_get_int32(ndmp_door_ctx_t
*ctx
)
104 if (ctx
->status
== 0) {
105 if (ctx
->ptr
+ sizeof (int32_t) <= ctx
->end_ptr
) {
106 (void) memcpy(&num
, ctx
->ptr
, sizeof (int32_t));
107 ctx
->ptr
+= sizeof (int32_t);
109 ctx
->status
= ENOSPC
;
116 ndmp_door_get_uint32(ndmp_door_ctx_t
*ctx
)
118 return ((uint32_t)ndmp_door_get_int32(ctx
));
122 ndmp_door_get_string(ndmp_door_ctx_t
*ctx
)
125 int len
= ndmp_door_get_int32(ctx
);
127 if (ctx
->status
== 0) {
131 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
132 buf
= malloc(len
+1);
135 (void) strcpy(buf
, "");
137 (void) memcpy(buf
, ctx
->ptr
, len
);
145 ctx
->status
= ENOSPC
;
152 ndmp_door_put_int32(ndmp_door_ctx_t
*ctx
, int32_t num
)
154 if (ctx
->status
== 0) {
155 if (ctx
->ptr
+ sizeof (int32_t) <= ctx
->end_ptr
) {
156 (void) memcpy(ctx
->ptr
, &num
, sizeof (int32_t));
157 ctx
->ptr
+= sizeof (int32_t);
159 ctx
->status
= ENOSPC
;
165 ndmp_door_put_uint32(ndmp_door_ctx_t
*ctx
, uint32_t num
)
167 ndmp_door_put_int32(ctx
, (int32_t)num
);
171 ndmp_door_put_string(ndmp_door_ctx_t
*ctx
, char *buf
)
180 if (ctx
->status
== 0) {
181 ndmp_door_put_int32(ctx
, len
);
185 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
186 (void) memcpy(ctx
->ptr
, buf
, len
);
189 ctx
->status
= ENOSPC
;
195 ndmp_door_free_string(char *buf
)
201 ndmp_door_get_int64(ndmp_door_ctx_t
*ctx
)
204 if (ctx
->status
== 0) {
205 if (ctx
->ptr
+ sizeof (int64_t) <= ctx
->end_ptr
) {
206 (void) memcpy(&num
, ctx
->ptr
, sizeof (int64_t));
207 ctx
->ptr
+= sizeof (int64_t);
209 ctx
->status
= ENOSPC
;
216 ndmp_door_get_uint64(ndmp_door_ctx_t
*ctx
)
218 return ((uint64_t)ndmp_door_get_int64(ctx
));
223 ndmp_door_put_int64(ndmp_door_ctx_t
*ctx
, int64_t num
)
225 if (ctx
->status
== 0) {
226 if (ctx
->ptr
+ sizeof (int64_t) <= ctx
->end_ptr
) {
227 (void) memcpy(ctx
->ptr
, &num
, sizeof (int64_t));
228 ctx
->ptr
+= sizeof (int64_t);
230 ctx
->status
= ENOSPC
;
236 ndmp_door_put_uint64(ndmp_door_ctx_t
*ctx
, uint64_t num
)
238 ndmp_door_put_int64(ctx
, (int64_t)num
);
242 ndmp_door_put_short(ndmp_door_ctx_t
*ctx
, short num
)
244 if (ctx
->status
== 0) {
245 if (ctx
->ptr
+ sizeof (short) <= ctx
->end_ptr
) {
246 (void) memcpy(ctx
->ptr
, &num
, sizeof (short));
247 ctx
->ptr
+= sizeof (short);
249 ctx
->status
= ENOSPC
;
255 ndmp_door_get_short(ndmp_door_ctx_t
*ctx
)
258 if (ctx
->status
== 0) {
259 if (ctx
->ptr
+ sizeof (short) <= ctx
->end_ptr
) {
260 (void) memcpy(&num
, ctx
->ptr
, sizeof (short));
261 ctx
->ptr
+= sizeof (short);
263 ctx
->status
= ENOSPC
;
270 ndmp_door_put_ushort(ndmp_door_ctx_t
*ctx
, unsigned short num
)
272 ndmp_door_put_short(ctx
, (short)num
);
276 ndmp_door_get_ushort(ndmp_door_ctx_t
*ctx
)
278 return ((unsigned short)ndmp_door_get_short(ctx
));
282 ndmp_door_put_buf(ndmp_door_ctx_t
*ctx
, unsigned char *start
, int len
)
284 ndmp_door_put_int32(ctx
, len
);
285 if (ctx
->status
== 0) {
286 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
287 (void) memcpy(ctx
->ptr
, start
, len
);
290 ctx
->status
= ENOSPC
;
296 ndmp_door_get_buf(ndmp_door_ctx_t
*ctx
, unsigned char *buf
, int bufsize
)
303 len
= ndmp_door_get_int32(ctx
);
304 if (ctx
->status
== 0) {
306 ctx
->status
= ENOSPC
;
310 if (ctx
->ptr
+ len
<= ctx
->end_ptr
) {
311 (void) memcpy(buf
, ctx
->ptr
, len
);
314 ctx
->status
= ENOSPC
;