Code

proto: Added sdb_proto_marshal_data.
[sysdb.git] / src / utils / proto.c
1 /*
2  * SysDB - src/utils/proto.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "core/data.h"
29 #include "core/time.h"
30 #include "utils/error.h"
31 #include "utils/proto.h"
33 #include <arpa/inet.h>
34 #include <errno.h>
36 #include <limits.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include <sys/select.h>
43 /*
44  * private helper functions
45  */
47 /* In case there's not enough buffer space, the marshal functions have to
48  * return the number of bytes that would have been written if enough space had
49  * been available. */
51 static ssize_t
52 marshal_int(char *buf, size_t buf_len, int64_t v)
53 {
54         if (buf_len >= sizeof(v)) {
55 #if __BYTE_ORDER != __BIG_ENDIAN
56                 v = (((int64_t)ntohl((int32_t)v)) << 32)
57                         + ((int64_t)ntohl((int32_t)(v >> 32)));
58 #endif
59                 memcpy(buf, &v, sizeof(v));
60         }
61         return sizeof(v);
62 } /* marshal_int */
64 static ssize_t
65 marshal_double(char __attribute__((unused)) *buf,
66                 size_t __attribute__((unused)) buf_len,
67                 double __attribute__((unused)) v)
68 {
69         /* XXX: find a good network representation */
70         errno = ENOTSUP;
71         return -1;
72 } /* marshal_double */
74 static ssize_t
75 marshal_datetime(char *buf, size_t buf_len, sdb_time_t v)
76 {
77         return marshal_int(buf, buf_len, (int64_t)v);
78 } /* marshal_datetime */
80 static ssize_t
81 marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
82 {
83         uint32_t tmp = htonl((uint32_t)len);
84         if (buf_len >= len) {
85                 memcpy(buf, &tmp, sizeof(tmp));
86                 memcpy(buf + sizeof(tmp), v, len);
87         }
88         return sizeof(tmp) + len;
89 } /* marshal_binary */
91 static ssize_t
92 marshal_string(char *buf, size_t buf_len, const char *v)
93 {
94         /* The actual string including the terminating null byte. */
95         return marshal_binary(buf, buf_len,
96                         strlen(v) + 1, (const unsigned char *)v);
97 } /* marshal_string */
99 /*
100  * public API
101  */
103 ssize_t
104 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
105                 uint32_t msg_len, const char *msg)
107         size_t len = 2 * sizeof(uint32_t) + msg_len;
108         uint32_t tmp;
110         if (buf_len < 2 * sizeof(uint32_t))
111                 return -1;
112         if (buf_len < len) /* crop message */
113                 msg_len -= (uint32_t)(len - buf_len);
115         tmp = htonl(code);
116         memcpy(buf, &tmp, sizeof(tmp));
117         tmp = htonl(msg_len);
118         memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
120         if (msg_len)
121                 memcpy(buf + 2 * sizeof(tmp), msg, msg_len);
122         return len;
123 } /* sdb_proto_marshal */
125 ssize_t
126 sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
128         ssize_t len = 0, n = 0;
129         uint32_t tmp;
130         size_t i;
131         int type;
133         if (buf_len >= sizeof(tmp)) {
134                 tmp = htonl((uint32_t)datum->type);
135                 memcpy(buf, &tmp, sizeof(tmp));
136                 buf += sizeof(tmp);
137                 buf_len -= sizeof(tmp);
138         }
139         else
140                 buf_len = 0;
141         len += sizeof(tmp);
143         if (datum->type == SDB_TYPE_NULL)
144                 return len;
146         if (datum->type == SDB_TYPE_INTEGER)
147                 n = marshal_int(buf, buf_len, datum->data.integer);
148         else if (datum->type == SDB_TYPE_DECIMAL)
149                 n = marshal_double(buf, buf_len, datum->data.decimal);
150         else if (datum->type == SDB_TYPE_STRING)
151                 n = marshal_string(buf, buf_len, datum->data.string);
152         else if (datum->type == SDB_TYPE_DATETIME)
153                 n = marshal_datetime(buf, buf_len, datum->data.datetime);
154         else if (datum->type == SDB_TYPE_BINARY)
155                 n = marshal_binary(buf, buf_len,
156                                 datum->data.binary.length, datum->data.binary.datum);
158         if (n < 0)
159                 return n;
160         else if (n > 0)
161                 return len + n;
163         if (! (datum->type & SDB_TYPE_ARRAY)) {
164                 errno = EINVAL;
165                 return -1;
166         }
168         /* arrays */
169         if (buf_len >= sizeof(tmp)) {
170                 tmp = htonl((uint32_t)datum->data.array.length);
171                 memcpy(buf, &tmp, sizeof(tmp));
172                 buf += sizeof(tmp);
173                 buf_len -= sizeof(tmp);
174         }
175         else
176                 buf_len = 0;
177         len += sizeof(tmp);
179         type = datum->type & 0xff;
180         for (i = 0; i < datum->data.array.length; ++i) {
181                 if (type == SDB_TYPE_INTEGER) {
182                         int64_t *v = datum->data.array.values;
183                         n = marshal_int(buf, buf_len, v[i]);
184                 }
185                 else if (type == SDB_TYPE_DECIMAL) {
186                         double *v = datum->data.array.values;
187                         n = marshal_double(buf, buf_len, v[i]);
188                 }
189                 else if (type == SDB_TYPE_STRING) {
190                         char **v = datum->data.array.values;
191                         n = marshal_string(buf, buf_len, v[i]);
192                 }
193                 else {
194                         errno = ENOTSUP;
195                         return -1;
196                 }
198                 if (n < 0)
199                         return -1;
200                 if (buf_len >= (size_t)n) {
201                         buf += n;
202                         buf_len -= n;
203                 }
204                 else
205                         buf_len = 0;
206                 len += n;
207         }
208         return len;
209 } /* sdb_proto_marshal_data */
211 int
212 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
213                 uint32_t *code, uint32_t *msg_len)
215         uint32_t tmp;
217         if (buf_len < 2 * sizeof(uint32_t))
218                 return -1;
220         tmp = sdb_proto_unmarshal_int(buf, buf_len);
221         if (code)
222                 *code = tmp;
223         tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
224                         buf_len - sizeof(uint32_t));
225         if (msg_len)
226                 *msg_len = tmp;
227         return 0;
228 } /* sdb_proto_unmarshal_header */
230 uint32_t
231 sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
233         uint32_t n;
235         /* not enough data to read */
236         if (buf_len < sizeof(n))
237                 return UINT32_MAX;
239         memcpy(&n, buf, sizeof(n));
240         return ntohl(n);
241 } /* sdb_proto_unmarshal_int */
243 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */