b5cfe04700c52793388f4d3d62bc40dec1674b83
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 "utils/error.h"
29 #include "utils/proto.h"
31 #include <arpa/inet.h>
32 #include <errno.h>
34 #include <limits.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <sys/select.h>
41 /*
42 * public API
43 */
45 ssize_t
46 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
47 uint32_t msg_len, const char *msg)
48 {
49 size_t len = 2 * sizeof(uint32_t) + msg_len;
50 uint32_t tmp;
52 if (buf_len < 2 * sizeof(uint32_t))
53 return -1;
54 if (buf_len < len) /* crop message */
55 msg_len -= (uint32_t)(len - buf_len);
57 tmp = htonl(code);
58 memcpy(buf, &tmp, sizeof(tmp));
59 tmp = htonl(msg_len);
60 memcpy(buf + sizeof(tmp), &tmp, sizeof(tmp));
62 if (msg_len)
63 memcpy(buf + 2 * sizeof(tmp), msg, msg_len);
64 return len;
65 } /* sdb_proto_marshal */
67 int
68 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
69 uint32_t *code, uint32_t *msg_len)
70 {
71 uint32_t tmp;
73 if (buf_len < 2 * sizeof(uint32_t))
74 return -1;
76 tmp = sdb_proto_unmarshal_int(buf, buf_len);
77 if (code)
78 *code = tmp;
79 tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
80 buf_len - sizeof(uint32_t));
81 if (msg_len)
82 *msg_len = tmp;
83 return 0;
84 } /* sdb_proto_unmarshal_header */
86 uint32_t
87 sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
88 {
89 uint32_t n;
91 /* not enough data to read */
92 if (buf_len < sizeof(n))
93 return UINT32_MAX;
95 memcpy(&n, buf, sizeof(n));
96 return ntohl(n);
97 } /* sdb_proto_unmarshal_int */
99 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */