Code

f767fa21a887fdd929a32ea45f16fec656ff6b5f
[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 #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_int64(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_int64 */
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_int64(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 >= sizeof(tmp) + 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)
104         /* The actual string including the terminating null byte. */
105         size_t len = strlen(v) + 1;
106         if (buf_len >= len)
107                 memcpy(buf, v, len);
108         return len;
109 } /* marshal_string */
111 /*
112  * public API
113  */
115 ssize_t
116 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
117                 uint32_t msg_len, const char *msg)
119         size_t len = 2 * sizeof(uint32_t) + msg_len;
120         uint32_t tmp;
122         if (buf_len < 2 * sizeof(uint32_t))
123                 return -1;
124         if (buf_len < len) /* crop message */
125                 msg_len -= (uint32_t)(len - buf_len);
127         tmp = htonl(code);
128         memcpy(buf, &tmp, sizeof(tmp));
129         tmp = htonl(msg_len);
130         memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
132         if (msg_len)
133                 memcpy(buf + 2 * sizeof(tmp), msg, msg_len);
134         return len;
135 } /* sdb_proto_marshal */
137 ssize_t
138 sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
140         ssize_t len = 0, n = 0;
141         uint32_t tmp;
142         size_t i;
143         int type;
145         if (buf_len >= sizeof(tmp)) {
146                 tmp = htonl((uint32_t)datum->type);
147                 memcpy(buf, &tmp, sizeof(tmp));
148                 buf += sizeof(tmp);
149                 buf_len -= sizeof(tmp);
150         }
151         else
152                 buf_len = 0;
153         len += sizeof(tmp);
155         if (datum->type == SDB_TYPE_NULL)
156                 return len;
158         if (datum->type == SDB_TYPE_INTEGER)
159                 n = marshal_int64(buf, buf_len, datum->data.integer);
160         else if (datum->type == SDB_TYPE_DECIMAL)
161                 n = marshal_double(buf, buf_len, datum->data.decimal);
162         else if (datum->type == SDB_TYPE_STRING)
163                 n = marshal_string(buf, buf_len, datum->data.string);
164         else if (datum->type == SDB_TYPE_DATETIME)
165                 n = marshal_datetime(buf, buf_len, datum->data.datetime);
166         else if (datum->type == SDB_TYPE_BINARY)
167                 n = marshal_binary(buf, buf_len,
168                                 datum->data.binary.length, datum->data.binary.datum);
169         else if (datum->type == SDB_TYPE_REGEX)
170                 n = marshal_string(buf, buf_len, datum->data.re.raw);
172         if (n < 0)
173                 return n;
174         else if (n > 0)
175                 return len + n;
177         if (! (datum->type & SDB_TYPE_ARRAY)) {
178                 errno = EINVAL;
179                 return -1;
180         }
182         /* arrays */
183         if (buf_len >= sizeof(tmp)) {
184                 tmp = htonl((uint32_t)datum->data.array.length);
185                 memcpy(buf, &tmp, sizeof(tmp));
186                 buf += sizeof(tmp);
187                 buf_len -= sizeof(tmp);
188         }
189         else
190                 buf_len = 0;
191         len += sizeof(tmp);
193         type = datum->type & 0xff;
194         for (i = 0; i < datum->data.array.length; ++i) {
195                 if (type == SDB_TYPE_INTEGER) {
196                         int64_t *v = datum->data.array.values;
197                         n = marshal_int64(buf, buf_len, v[i]);
198                 }
199                 else if (type == SDB_TYPE_DECIMAL) {
200                         double *v = datum->data.array.values;
201                         n = marshal_double(buf, buf_len, v[i]);
202                 }
203                 else if (type == SDB_TYPE_STRING) {
204                         char **v = datum->data.array.values;
205                         n = marshal_string(buf, buf_len, v[i]);
206                 }
207                 else if (type == SDB_TYPE_DATETIME) {
208                         sdb_time_t *v = datum->data.array.values;
209                         n = marshal_datetime(buf, buf_len, v[i]);
210                 }
211                 else if (type == SDB_TYPE_BINARY) {
212                         struct {
213                                 size_t length;
214                                 unsigned char *datum;
215                         } *v = datum->data.array.values;
216                         n = marshal_binary(buf, buf_len, v[i].length, v[i].datum);
217                 }
218                 else if (type == SDB_TYPE_REGEX) {
219                         struct {
220                                 char *raw;
221                                 regex_t regex;
222                         } *v = datum->data.array.values;
223                         n = marshal_string(buf, buf_len, v[i].raw);
224                 }
225                 else {
226                         errno = EINVAL;
227                         return -1;
228                 }
230                 if (n < 0)
231                         return -1;
232                 if (buf_len >= (size_t)n) {
233                         buf += n;
234                         buf_len -= n;
235                 }
236                 else
237                         buf_len = 0;
238                 len += n;
239         }
240         return len;
241 } /* sdb_proto_marshal_data */
243 int
244 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
245                 uint32_t *code, uint32_t *msg_len)
247         uint32_t tmp;
249         if (buf_len < 2 * sizeof(uint32_t))
250                 return -1;
252         tmp = sdb_proto_unmarshal_int32(buf, buf_len);
253         if (code)
254                 *code = tmp;
255         tmp = sdb_proto_unmarshal_int32(buf + sizeof(uint32_t),
256                         buf_len - sizeof(uint32_t));
257         if (msg_len)
258                 *msg_len = tmp;
259         return 0;
260 } /* sdb_proto_unmarshal_header */
262 uint32_t
263 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len)
265         uint32_t n;
267         /* not enough data to read */
268         if (buf_len < sizeof(n))
269                 return UINT32_MAX;
271         memcpy(&n, buf, sizeof(n));
272         return ntohl(n);
273 } /* sdb_proto_unmarshal_int32 */
275 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */