Code

proto: Let unmarshal functions return the number of bytes processed.
[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;
50 typedef struct {
51         sdb_time_t last_update;
52         const char *hostname;
53         const char *name;
54 } sdb_proto_service_t;
56 typedef struct {
57         sdb_time_t last_update;
58         const char *hostname;
59         const char *name;
60         const char *store_type; /* optional */
61         const char *store_id;   /* optional */
62 } sdb_proto_metric_t;
64 typedef struct {
65         sdb_time_t last_update;
66         int parent_type;
67         const char *hostname; /* optional */
68         const char *parent;
69         const char *key;
70         const sdb_data_t *value;
71 } sdb_proto_attribute_t;
73 /*
74  * sdb_proto_marshal:
75  * Encode the message into the wire format by adding an appropriate header.
76  * The encoded message is written to buf which has to be large enough to store
77  * the header (64 bits) and the entire message.
78  *
79  * Returns:
80  *  - The number of bytes of the full encoded message on success. The function
81  *    does not write more than 'buf_len' bytes. If the output was truncated
82  *    then the return value is the number of bytes which would have been
83  *    written if enough space had been available.
84  *  - a negative value on error
85  */
86 ssize_t
87 sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
88                 uint32_t msg_len, const char *msg);
90 /*
91  * sdb_proto_marshal_data:
92  * Encode a datum into the wire format and write it to buf.
93  *
94  * Returns:
95  *  - The number of bytes of the full encoded datum on success. The function
96  *    does not write more than 'buf_len' bytes. If the output was truncated
97  *    then the return value is the number of bytes which would have been
98  *    written if enough space had been available.
99  *  - a negative value else
100  */
101 ssize_t
102 sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum);
104 /*
105  * sdb_proto_marshal_host, sdb_proto_marshal_service,
106  * sdb_proto_marshal_metric, sdb_proto_marshal_attribute:
107  * Encode the basic information of a stored object into the wire format and
108  * write it to buf. These functions are similar to the sdb_store_<type>
109  * functions. See their documentation for details about the arguments.
110  *
111  * Returns:
112  *  - The number of bytes of the full encoded datum on success. The function
113  *    does not write more than 'buf_len' bytes. If the output was truncated
114  *    then the return value is the number of bytes which would have been
115  *    written if enough space had been available.
116  *  - a negative value else
117  */
118 ssize_t
119 sdb_proto_marshal_host(char *buf, size_t buf_len,
120                 const sdb_proto_host_t *host);
121 ssize_t
122 sdb_proto_marshal_service(char *buf, size_t buf_len,
123                 const sdb_proto_service_t *svc);
124 ssize_t
125 sdb_proto_marshal_metric(char *buf, size_t buf_len,
126                 const sdb_proto_metric_t *metric);
127 ssize_t
128 sdb_proto_marshal_attribute(char *buf, size_t buf_len,
129                 const sdb_proto_attribute_t *attr);
131 /*
132  * sdb_proto_unmarshal_header:
133  * Read and decode a message header from the specified string.
134  *
135  * Returns:
136  *  - the number of bytes read on success
137  *  - a negative value else
138  */
139 ssize_t
140 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
141                 uint32_t *code, uint32_t *msg_len);
143 /*
144  * sdb_proto_unmarshal_int32:
145  * Read and decode a 32-bit integer from the specified string.
146  *
147  * Returns:
148  *  - the number of bytes read on success
149  *  - a negative value else
150  */
151 ssize_t
152 sdb_proto_unmarshal_int32(const char *buf, size_t buf_len, uint32_t *v);
154 #ifdef __cplusplus
155 } /* extern "C" */
156 #endif
158 #endif /* ! SDB_UTILS_PROTO_H */
160 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */