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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "core/data.h"
33 #include "core/time.h"
34 #include "utils/error.h"
35 #include "utils/proto.h"
37 #include <assert.h>
38 #include <errno.h>
40 #include <arpa/inet.h>
41 #include <limits.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include <sys/select.h>
48 /*
49 * private helper functions
50 */
52 /* In case there's not enough buffer space, the marshal functions have to
53 * return the number of bytes that would have been written if enough space had
54 * been available. */
56 static ssize_t
57 marshal_int(char *buf, size_t buf_len, int64_t v)
58 {
59 if (buf_len >= sizeof(v)) {
60 #if __BYTE_ORDER != __BIG_ENDIAN
61 v = (((int64_t)ntohl((int32_t)v)) << 32)
62 + ((int64_t)ntohl((int32_t)(v >> 32)));
63 #endif
64 memcpy(buf, &v, sizeof(v));
65 }
66 return sizeof(v);
67 } /* marshal_int */
69 static ssize_t
70 marshal_double(char *buf, size_t buf_len, double v)
71 {
72 uint64_t t = 0;
73 assert(sizeof(v) == sizeof(t));
74 memcpy(&t, &v, sizeof(v));
75 #if IEEE754_DOUBLE_BYTE_ORDER != IEEE754_DOUBLE_BIG_ENDIAN
76 t = (((int64_t)ntohl((int32_t)t)) << 32)
77 + ((int64_t)ntohl((int32_t)(t >> 32)));
78 #endif
79 if (buf_len >= sizeof(t))
80 memcpy(buf, &t, sizeof(t));
81 return sizeof(t);
82 } /* marshal_double */
84 static ssize_t
85 marshal_datetime(char *buf, size_t buf_len, sdb_time_t v)
86 {
87 return marshal_int(buf, buf_len, (int64_t)v);
88 } /* marshal_datetime */
90 static ssize_t
91 marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
92 {
93 uint32_t tmp = htonl((uint32_t)len);
94 if (buf_len >= len) {
95 memcpy(buf, &tmp, sizeof(tmp));
96 memcpy(buf + sizeof(tmp), v, len);
97 }
98 return sizeof(tmp) + len;
99 } /* marshal_binary */
101 static ssize_t
102 marshal_string(char *buf, size_t buf_len, const char *v)
103 {
104 /* The actual string including the terminating null byte. */
105 return marshal_binary(buf, buf_len,
106 strlen(v) + 1, (const unsigned char *)v);
107 } /* marshal_string */
109 /*
110 * public API
111 */
113 ssize_t
114 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
115 uint32_t msg_len, const char *msg)
116 {
117 size_t len = 2 * sizeof(uint32_t) + msg_len;
118 uint32_t tmp;
120 if (buf_len < 2 * sizeof(uint32_t))
121 return -1;
122 if (buf_len < len) /* crop message */
123 msg_len -= (uint32_t)(len - buf_len);
125 tmp = htonl(code);
126 memcpy(buf, &tmp, sizeof(tmp));
127 tmp = htonl(msg_len);
128 memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
130 if (msg_len)
131 memcpy(buf + 2 * sizeof(tmp), msg, msg_len);
132 return len;
133 } /* sdb_proto_marshal */
135 ssize_t
136 sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
137 {
138 ssize_t len = 0, n = 0;
139 uint32_t tmp;
140 size_t i;
141 int type;
143 if (buf_len >= sizeof(tmp)) {
144 tmp = htonl((uint32_t)datum->type);
145 memcpy(buf, &tmp, sizeof(tmp));
146 buf += sizeof(tmp);
147 buf_len -= sizeof(tmp);
148 }
149 else
150 buf_len = 0;
151 len += sizeof(tmp);
153 if (datum->type == SDB_TYPE_NULL)
154 return len;
156 if (datum->type == SDB_TYPE_INTEGER)
157 n = marshal_int(buf, buf_len, datum->data.integer);
158 else if (datum->type == SDB_TYPE_DECIMAL)
159 n = marshal_double(buf, buf_len, datum->data.decimal);
160 else if (datum->type == SDB_TYPE_STRING)
161 n = marshal_string(buf, buf_len, datum->data.string);
162 else if (datum->type == SDB_TYPE_DATETIME)
163 n = marshal_datetime(buf, buf_len, datum->data.datetime);
164 else if (datum->type == SDB_TYPE_BINARY)
165 n = marshal_binary(buf, buf_len,
166 datum->data.binary.length, datum->data.binary.datum);
167 else if (datum->type == SDB_TYPE_REGEX)
168 n = marshal_string(buf, buf_len, datum->data.re.raw);
170 if (n < 0)
171 return n;
172 else if (n > 0)
173 return len + n;
175 if (! (datum->type & SDB_TYPE_ARRAY)) {
176 errno = EINVAL;
177 return -1;
178 }
180 /* arrays */
181 if (buf_len >= sizeof(tmp)) {
182 tmp = htonl((uint32_t)datum->data.array.length);
183 memcpy(buf, &tmp, sizeof(tmp));
184 buf += sizeof(tmp);
185 buf_len -= sizeof(tmp);
186 }
187 else
188 buf_len = 0;
189 len += sizeof(tmp);
191 type = datum->type & 0xff;
192 for (i = 0; i < datum->data.array.length; ++i) {
193 if (type == SDB_TYPE_INTEGER) {
194 int64_t *v = datum->data.array.values;
195 n = marshal_int(buf, buf_len, v[i]);
196 }
197 else if (type == SDB_TYPE_DECIMAL) {
198 double *v = datum->data.array.values;
199 n = marshal_double(buf, buf_len, v[i]);
200 }
201 else if (type == SDB_TYPE_STRING) {
202 char **v = datum->data.array.values;
203 n = marshal_string(buf, buf_len, v[i]);
204 }
205 else if (type == SDB_TYPE_DATETIME) {
206 sdb_time_t *v = datum->data.array.values;
207 n = marshal_datetime(buf, buf_len, v[i]);
208 }
209 else if (type == SDB_TYPE_BINARY) {
210 struct {
211 size_t length;
212 unsigned char *datum;
213 } *v = datum->data.array.values;
214 n = marshal_binary(buf, buf_len, v[i].length, v[i].datum);
215 }
216 else if (type == SDB_TYPE_REGEX) {
217 struct {
218 char *raw;
219 regex_t regex;
220 } *v = datum->data.array.values;
221 n = marshal_string(buf, buf_len, v[i].raw);
222 }
223 else {
224 errno = EINVAL;
225 return -1;
226 }
228 if (n < 0)
229 return -1;
230 if (buf_len >= (size_t)n) {
231 buf += n;
232 buf_len -= n;
233 }
234 else
235 buf_len = 0;
236 len += n;
237 }
238 return len;
239 } /* sdb_proto_marshal_data */
241 int
242 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
243 uint32_t *code, uint32_t *msg_len)
244 {
245 uint32_t tmp;
247 if (buf_len < 2 * sizeof(uint32_t))
248 return -1;
250 tmp = sdb_proto_unmarshal_int(buf, buf_len);
251 if (code)
252 *code = tmp;
253 tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
254 buf_len - sizeof(uint32_t));
255 if (msg_len)
256 *msg_len = tmp;
257 return 0;
258 } /* sdb_proto_unmarshal_header */
260 uint32_t
261 sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
262 {
263 uint32_t n;
265 /* not enough data to read */
266 if (buf_len < sizeof(n))
267 return UINT32_MAX;
269 memcpy(&n, buf, sizeof(n));
270 return ntohl(n);
271 } /* sdb_proto_unmarshal_int */
273 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */