1 /*
2 * SysDB - src/include/utils/proto.h
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 #ifndef SDB_UTILS_PROTO_H
29 #define SDB_UTILS_PROTO_H 1
31 #include "core/data.h"
33 #include <stdint.h>
34 #include <unistd.h>
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /*
41 * sdb_proto_host, sdb_proto_service, sdb_proto_metric:
42 * Protocol-specific representations of the basic information of stored
43 * objects.
44 */
45 typedef struct {
46 sdb_time_t last_update;
47 const char *name;
48 } sdb_proto_host_t;
49 #define SDB_PROTO_HOST_INIT { 0, NULL }
51 typedef struct {
52 sdb_time_t last_update;
53 const char *hostname;
54 const char *name;
55 } sdb_proto_service_t;
56 #define SDB_PROTO_SERVICE_INIT { 0, NULL, NULL }
58 typedef struct {
59 sdb_time_t last_update;
60 const char *hostname;
61 const char *name;
62 const char *store_type; /* optional */
63 const char *store_id; /* optional */
64 } sdb_proto_metric_t;
65 #define SDB_PROTO_METRIC_INIT { 0, NULL, NULL, NULL, NULL }
67 typedef struct {
68 sdb_time_t last_update;
69 int parent_type;
70 const char *hostname; /* optional */
71 const char *parent;
72 const char *key;
73 sdb_data_t value;
74 } sdb_proto_attribute_t;
75 #define SDB_PROTO_ATTRIBUTE_INIT { 0, 0, NULL, NULL, NULL, SDB_DATA_INIT }
77 /*
78 * sdb_proto_marshal:
79 * Encode the message into the wire format by adding an appropriate header.
80 * The encoded message is written to buf which has to be large enough to store
81 * the header (64 bits) and the entire message.
82 *
83 * Returns:
84 * - The number of bytes of the full encoded message on success. The function
85 * does not write more than 'buf_len' bytes. If the output was truncated
86 * then the return value is the number of bytes which would have been
87 * written if enough space had been available.
88 * - a negative value on error
89 */
90 ssize_t
91 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
92 uint32_t msg_len, const char *msg);
94 /*
95 * sdb_proto_marshal_int32:
96 * Encode the 32-bit integer into the wire format and write it to buf.
97 *
98 * Returns:
99 * - The number of bytes of the encoded value on success. The function does
100 * not write more than 'buf_len' bytes. If the output was truncated then
101 * the return value is the number of bytes which would have been written if
102 * enough space had been available.
103 * - a negative value else
104 */
105 ssize_t
106 sdb_proto_marshal_int32(char *buf, size_t buf_len, uint32_t v);
108 /*
109 * sdb_proto_marshal_data:
110 * Encode a datum into the wire format and write it to buf.
111 *
112 * Returns:
113 * - The number of bytes of the full encoded datum on success. The function
114 * does not write more than 'buf_len' bytes. If the output was truncated
115 * then the return value is the number of bytes which would have been
116 * written if enough space had been available.
117 * - a negative value else
118 */
119 ssize_t
120 sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum);
122 /*
123 * sdb_proto_marshal_host, sdb_proto_marshal_service,
124 * sdb_proto_marshal_metric, sdb_proto_marshal_attribute:
125 * Encode the basic information of a stored object into the wire format and
126 * write it to buf. These functions are similar to the sdb_store_<type>
127 * functions. See their documentation for details about the arguments.
128 *
129 * Returns:
130 * - The number of bytes of the full encoded datum on success. The function
131 * does not write more than 'buf_len' bytes. If the output was truncated
132 * then the return value is the number of bytes which would have been
133 * written if enough space had been available.
134 * - a negative value else
135 */
136 ssize_t
137 sdb_proto_marshal_host(char *buf, size_t buf_len,
138 const sdb_proto_host_t *host);
139 ssize_t
140 sdb_proto_marshal_service(char *buf, size_t buf_len,
141 const sdb_proto_service_t *svc);
142 ssize_t
143 sdb_proto_marshal_metric(char *buf, size_t buf_len,
144 const sdb_proto_metric_t *metric);
145 ssize_t
146 sdb_proto_marshal_attribute(char *buf, size_t buf_len,
147 const sdb_proto_attribute_t *attr);
149 /*
150 * sdb_proto_unmarshal_header:
151 * Read and decode a message header from the specified string.
152 *
153 * Returns:
154 * - the number of bytes read on success
155 * - a negative value else
156 */
157 ssize_t
158 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
159 uint32_t *code, uint32_t *msg_len);
161 /*
162 * sdb_proto_unmarshal_int32:
163 * Read and decode a 32-bit integer from the specified string.
164 *
165 * Returns:
166 * - the number of bytes read on success
167 * - a negative value else
168 */
169 ssize_t
170 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len, uint32_t *v);
172 /*
173 * sdb_proto_unmarshal_data:
174 * Read and decode a datum from the specified string. The datum's data will be
175 * allocated dynamically if necessary and will have to be free'd using
176 * sdb_data_free_datum.
177 *
178 * Returns:
179 * - the number of bytes read on success
180 * - a negative value else
181 */
182 ssize_t
183 sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum);
185 /*
186 * sdb_proto_unmarshal_host, sdb_proto_unmarshal_service,
187 * sdb_proto_unmarshal_metric, sdb_proto_unmarshal_attribute:
188 * Read and decode a host, service, metric, or attribute object from the
189 * specified string.
190 *
191 * Returns:
192 * - the number of bytes read on success
193 * - a negative value else
194 */
195 ssize_t
196 sdb_proto_unmarshal_host(const char *buf, size_t len,
197 sdb_proto_host_t *host);
198 ssize_t
199 sdb_proto_unmarshal_service(const char *buf, size_t len,
200 sdb_proto_service_t *svc);
201 ssize_t
202 sdb_proto_unmarshal_metric(const char *buf, size_t len,
203 sdb_proto_metric_t *metric);
204 ssize_t
205 sdb_proto_unmarshal_attribute(const char *buf, size_t len,
206 sdb_proto_attribute_t *attr);
208 #ifdef __cplusplus
209 } /* extern "C" */
210 #endif
212 #endif /* ! SDB_UTILS_PROTO_H */
214 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */