Code

b626781f9a1f70652bd0de5fa7a51455ff57c346
[sysdb.git] / src / include / utils / proto.h
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_data:
96  * Encode a datum into the wire format and write it to buf.
97  *
98  * Returns:
99  *  - The number of bytes of the full encoded datum on success. The function
100  *    does not write more than 'buf_len' bytes. If the output was truncated
101  *    then the return value is the number of bytes which would have been
102  *    written if enough space had been available.
103  *  - a negative value else
104  */
105 ssize_t
106 sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum);
108 /*
109  * sdb_proto_marshal_host, sdb_proto_marshal_service,
110  * sdb_proto_marshal_metric, sdb_proto_marshal_attribute:
111  * Encode the basic information of a stored object into the wire format and
112  * write it to buf. These functions are similar to the sdb_store_<type>
113  * functions. See their documentation for details about the arguments.
114  *
115  * Returns:
116  *  - The number of bytes of the full encoded datum on success. The function
117  *    does not write more than 'buf_len' bytes. If the output was truncated
118  *    then the return value is the number of bytes which would have been
119  *    written if enough space had been available.
120  *  - a negative value else
121  */
122 ssize_t
123 sdb_proto_marshal_host(char *buf, size_t buf_len,
124                 const sdb_proto_host_t *host);
125 ssize_t
126 sdb_proto_marshal_service(char *buf, size_t buf_len,
127                 const sdb_proto_service_t *svc);
128 ssize_t
129 sdb_proto_marshal_metric(char *buf, size_t buf_len,
130                 const sdb_proto_metric_t *metric);
131 ssize_t
132 sdb_proto_marshal_attribute(char *buf, size_t buf_len,
133                 const sdb_proto_attribute_t *attr);
135 /*
136  * sdb_proto_unmarshal_header:
137  * Read and decode a message header from the specified string.
138  *
139  * Returns:
140  *  - the number of bytes read on success
141  *  - a negative value else
142  */
143 ssize_t
144 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
145                 uint32_t *code, uint32_t *msg_len);
147 /*
148  * sdb_proto_unmarshal_int32:
149  * Read and decode a 32-bit integer from the specified string.
150  *
151  * Returns:
152  *  - the number of bytes read on success
153  *  - a negative value else
154  */
155 ssize_t
156 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len, uint32_t *v);
158 /*
159  * sdb_proto_unmarshal_data:
160  * Read and decode a datum from the specified string. The datum's data will be
161  * allocated dynamically if necessary and will have to be free'd using
162  * sdb_data_free_datum.
163  *
164  * Returns:
165  *  - the number of bytes read on success
166  *  - a negative value else
167  */
168 ssize_t
169 sdb_proto_unmarshal_data(const char *buf, size_t len, sdb_data_t *datum);
171 /*
172  * sdb_proto_unmarshal_host, sdb_proto_unmarshal_service,
173  * sdb_proto_unmarshal_metric, sdb_proto_unmarshal_attribute:
174  * Read and decode a host, service, metric, or attribute object from the
175  * specified string.
176  *
177  * Returns:
178  *  - the number of bytes read on success
179  *  - a negative value else
180  */
181 ssize_t
182 sdb_proto_unmarshal_host(const char *buf, size_t len,
183                 sdb_proto_host_t *host);
184 ssize_t
185 sdb_proto_unmarshal_service(const char *buf, size_t len,
186                 sdb_proto_service_t *svc);
187 ssize_t
188 sdb_proto_unmarshal_metric(const char *buf, size_t len,
189                 sdb_proto_metric_t *metric);
190 ssize_t
191 sdb_proto_unmarshal_attribute(const char *buf, size_t len,
192                 sdb_proto_attribute_t *attr);
194 #ifdef __cplusplus
195 } /* extern "C" */
196 #endif
198 #endif /* ! SDB_UTILS_PROTO_H */
200 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */