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);
168 if (n < 0)
169 return n;
170 else if (n > 0)
171 return len + n;
173 if (! (datum->type & SDB_TYPE_ARRAY)) {
174 errno = EINVAL;
175 return -1;
176 }
178 /* arrays */
179 if (buf_len >= sizeof(tmp)) {
180 tmp = htonl((uint32_t)datum->data.array.length);
181 memcpy(buf, &tmp, sizeof(tmp));
182 buf += sizeof(tmp);
183 buf_len -= sizeof(tmp);
184 }
185 else
186 buf_len = 0;
187 len += sizeof(tmp);
189 type = datum->type & 0xff;
190 for (i = 0; i < datum->data.array.length; ++i) {
191 if (type == SDB_TYPE_INTEGER) {
192 int64_t *v = datum->data.array.values;
193 n = marshal_int(buf, buf_len, v[i]);
194 }
195 else if (type == SDB_TYPE_DECIMAL) {
196 double *v = datum->data.array.values;
197 n = marshal_double(buf, buf_len, v[i]);
198 }
199 else if (type == SDB_TYPE_STRING) {
200 char **v = datum->data.array.values;
201 n = marshal_string(buf, buf_len, v[i]);
202 }
203 else {
204 errno = ENOTSUP;
205 return -1;
206 }
208 if (n < 0)
209 return -1;
210 if (buf_len >= (size_t)n) {
211 buf += n;
212 buf_len -= n;
213 }
214 else
215 buf_len = 0;
216 len += n;
217 }
218 return len;
219 } /* sdb_proto_marshal_data */
221 int
222 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
223 uint32_t *code, uint32_t *msg_len)
224 {
225 uint32_t tmp;
227 if (buf_len < 2 * sizeof(uint32_t))
228 return -1;
230 tmp = sdb_proto_unmarshal_int(buf, buf_len);
231 if (code)
232 *code = tmp;
233 tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
234 buf_len - sizeof(uint32_t));
235 if (msg_len)
236 *msg_len = tmp;
237 return 0;
238 } /* sdb_proto_unmarshal_header */
240 uint32_t
241 sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
242 {
243 uint32_t n;
245 /* not enough data to read */
246 if (buf_len < sizeof(n))
247 return UINT32_MAX;
249 memcpy(&n, buf, sizeof(n));
250 return ntohl(n);
251 } /* sdb_proto_unmarshal_int */
253 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */