36b14f5aea450705f6046f5c91a11b7289b6775b
[bluesky.git] / nfs3 / common.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2010  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 /* Common RPC code shared between the BlueSky server and test client code. */
10
11 #include "mount_prot.h"
12 #include "nfs3_prot.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <rpc/pmap_clnt.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <memory.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
22
23 #include "bluesky.h"
24
25 /* Routines for XDR-encoding to a growable string. */
26 static bool_t xdr_string_putlong(XDR *xdrs, const long *lp)
27 {
28     GString *str = (GString *)xdrs->x_private;
29     uint32_t data = htonl(*lp);
30     g_string_set_size(str, str->len + 4);
31     memcpy(str->str + str->len - 4, &data, 4);
32     return TRUE;
33 }
34
35 static bool_t xdr_string_putbytes(XDR *xdrs, const char *addr, u_int len)
36 {
37     GString *str = (GString *)xdrs->x_private;
38     g_string_set_size(str, str->len + len);
39     memcpy(str->str + str->len - len, addr, len);
40     return TRUE;
41 }
42
43 static u_int xdr_string_getpos(const XDR *xdrs)
44 {
45     GString *str = (GString *)xdrs->x_private;
46     return str->len;
47 }
48
49 static bool_t xdr_string_putint32(XDR *xdrs, const int32_t *ip)
50 {
51     GString *str = (GString *)xdrs->x_private;
52     uint32_t data = htonl(*ip);
53     g_string_set_size(str, str->len + 4);
54     memcpy(str->str + str->len - 4, &data, 4);
55     return TRUE;
56 }
57
58 static int32_t *xdr_string_inline(XDR *xdrs, u_int len)
59 {
60     GString *str = (GString *)xdrs->x_private;
61     g_string_set_size(str, str->len + len);
62     return (int32_t *)(str->str + str->len - len);
63 }
64
65 static void xdr_string_destroy(XDR *xdrs)
66 {
67 }
68
69 static struct xdr_ops xdr_string_ops = {
70     .x_putlong = xdr_string_putlong,
71     .x_putbytes = xdr_string_putbytes,
72     .x_getpostn = xdr_string_getpos,
73     .x_putint32 = xdr_string_putint32,
74     .x_inline = xdr_string_inline,
75     .x_destroy = xdr_string_destroy,
76 };
77
78 void xdr_string_create(XDR *xdrs, GString *string, enum xdr_op op)
79 {
80     xdrs->x_op = op;
81     xdrs->x_ops = &xdr_string_ops;
82     xdrs->x_private = (char *)string;
83     xdrs->x_base = NULL;
84     xdrs->x_handy = 0;
85 }