1 /*
2 * SysDB - src/frontend/connection-private.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 /*
29 * private data structures used by frontend modules
30 */
32 #ifndef SDB_FRONTEND_CONNECTION_PRIVATE_H
33 #define SDB_FRONTEND_CONNECTION_PRIVATE_H 1
35 #include "frontend/connection.h"
37 #include "core/object.h"
38 #include "core/store.h"
39 #include "core/timeseries.h"
40 #include "utils/strbuf.h"
42 #include <inttypes.h>
43 #include <arpa/inet.h>
45 #include <stdlib.h>
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
51 struct sdb_conn {
52 sdb_object_t super;
54 /* file-descriptor of the open connection */
55 int fd;
57 /* connection and client information */
58 struct sockaddr_storage client_addr;
59 socklen_t client_addr_len;
61 /* connection handling */
62 ssize_t (*read)(sdb_conn_t *, size_t);
63 ssize_t (*write)(sdb_conn_t *, const void *, size_t);
64 int (*finish)(sdb_conn_t *);
65 void *session;
67 /* read buffer */
68 sdb_strbuf_t *buf;
70 /* connection / protocol state information */
71 uint32_t cmd;
72 uint32_t cmd_len;
74 /* amount of data to skip, e.g., after receiving invalid commands; if this
75 * is non-zero, the 'skip_len' first bytes of 'buf' are invalid */
76 size_t skip_len;
78 sdb_strbuf_t *errbuf;
80 /* user information */
81 char *username; /* NULL if the user has not been authenticated */
82 bool ready; /* indicates that startup finished successfully */
83 };
84 #define CONN(obj) ((sdb_conn_t *)(obj))
86 /*
87 * node types
88 */
90 typedef struct {
91 sdb_conn_node_t super;
92 sdb_store_expr_t *expr;
93 } conn_expr_t;
94 #define CONN_EXPR(obj) ((conn_expr_t *)(obj))
96 typedef struct {
97 sdb_conn_node_t super;
98 sdb_store_matcher_t *matcher;
99 } conn_matcher_t;
100 #define CONN_MATCHER(obj) ((conn_matcher_t *)(obj))
102 typedef struct {
103 sdb_conn_node_t super;
104 int type;
105 conn_matcher_t *filter;
106 } conn_list_t;
107 #define CONN_LIST(obj) ((conn_list_t *)(obj))
109 typedef struct {
110 sdb_conn_node_t super;
111 int type;
112 char *host;
113 char *name; /* NULL for type == SDB_HOST */
114 conn_matcher_t *filter;
115 } conn_fetch_t;
116 #define CONN_FETCH(obj) ((conn_fetch_t *)(obj))
118 typedef struct {
119 sdb_conn_node_t super;
120 int type;
121 conn_matcher_t *matcher;
122 conn_matcher_t *filter;
123 } conn_lookup_t;
124 #define CONN_LOOKUP(obj) ((conn_lookup_t *)(obj))
126 typedef struct {
127 sdb_conn_node_t super;
128 char *name;
129 sdb_time_t last_update;
130 } conn_store_host_t;
131 #define CONN_STORE_HOST(obj) ((conn_store_host_t *)(obj))
133 typedef struct {
134 sdb_conn_node_t super;
135 char *hostname;
136 char *name;
137 sdb_time_t last_update;
138 } conn_store_svc_t;
139 #define CONN_STORE_SVC(obj) ((conn_store_svc_t *)(obj))
141 typedef struct {
142 sdb_conn_node_t super;
143 char *hostname;
144 char *name;
145 char *store_type; /* optional */
146 char *store_id; /* optional */
147 sdb_time_t last_update;
148 } conn_store_metric_t;
149 #define CONN_STORE_METRIC(obj) ((conn_store_metric_t *)(obj))
151 typedef struct {
152 sdb_conn_node_t super;
153 int parent_type;
154 char *hostname; /* optional */
155 char *parent;
156 char *key;
157 sdb_data_t value;
158 sdb_time_t last_update;
159 } conn_store_attr_t;
160 #define CONN_STORE_ATTR(obj) ((conn_store_attr_t *)(obj))
162 typedef struct {
163 sdb_conn_node_t super;
164 char *hostname;
165 char *metric;
166 sdb_timeseries_opts_t opts;
167 } conn_ts_t;
168 #define CONN_TS(obj) ((conn_ts_t *)(obj))
170 /*
171 * type helper functions
172 */
174 static void __attribute__((unused))
175 conn_expr_destroy(sdb_object_t *obj)
176 {
177 sdb_object_deref(SDB_OBJ(CONN_EXPR(obj)->expr));
178 } /* conn_expr_destroy */
180 static void __attribute__((unused))
181 conn_matcher_destroy(sdb_object_t *obj)
182 {
183 sdb_object_deref(SDB_OBJ(CONN_MATCHER(obj)->matcher));
184 } /* conn_matcher_destroy */
186 static void __attribute__((unused))
187 conn_list_destroy(sdb_object_t *obj)
188 {
189 sdb_object_deref(SDB_OBJ(CONN_LIST(obj)->filter));
190 } /* conn_list_destroy */
192 static void __attribute__((unused))
193 conn_fetch_destroy(sdb_object_t *obj)
194 {
195 if (CONN_FETCH(obj)->host)
196 free(CONN_FETCH(obj)->host);
197 if (CONN_FETCH(obj)->name)
198 free(CONN_FETCH(obj)->name);
199 sdb_object_deref(SDB_OBJ(CONN_FETCH(obj)->filter));
200 } /* conn_fetch_destroy */
202 static void __attribute__((unused))
203 conn_lookup_destroy(sdb_object_t *obj)
204 {
205 sdb_object_deref(SDB_OBJ(CONN_LOOKUP(obj)->matcher));
206 sdb_object_deref(SDB_OBJ(CONN_LOOKUP(obj)->filter));
207 } /* conn_lookup_destroy */
209 static void __attribute__((unused))
210 conn_store_host_destroy(sdb_object_t *obj)
211 {
212 conn_store_host_t *host = CONN_STORE_HOST(obj);
213 if (host->name)
214 free((void *)host->name);
215 host->name = NULL;
216 } /* conn_store_host_destroy */
218 static void __attribute__((unused))
219 conn_store_svc_destroy(sdb_object_t *obj)
220 {
221 conn_store_svc_t *svc = CONN_STORE_SVC(obj);
222 if (svc->hostname)
223 free((void *)svc->hostname);
224 svc->hostname = NULL;
225 if (svc->name)
226 free((void *)svc->name);
227 svc->name = NULL;
228 } /* conn_store_svc_destroy */
230 static void __attribute__((unused))
231 conn_store_metric_destroy(sdb_object_t *obj)
232 {
233 conn_store_metric_t *metric = CONN_STORE_METRIC(obj);
234 if (metric->hostname)
235 free((void *)metric->hostname);
236 metric->hostname = NULL;
237 if (metric->name)
238 free((void *)metric->name);
239 metric->name = NULL;
240 if (metric->store_type)
241 free((void *)metric->store_type);
242 metric->store_type = NULL;
243 if (metric->store_id)
244 free((void *)metric->store_id);
245 metric->store_id = NULL;
246 } /* conn_store_metric_destroy */
248 static void __attribute__((unused))
249 conn_store_attr_destroy(sdb_object_t *obj)
250 {
251 conn_store_attr_t *attr = CONN_STORE_ATTR(obj);
252 if (attr->hostname)
253 free((void *)attr->hostname);
254 attr->hostname = NULL;
255 if (attr->parent)
256 free((void *)attr->parent);
257 attr->parent = NULL;
258 if (attr->key)
259 free((void *)attr->key);
260 attr->key = NULL;
261 sdb_data_free_datum(&attr->value);
262 } /* conn_store_attr_destroy */
264 static void __attribute__((unused))
265 conn_ts_destroy(sdb_object_t *obj)
266 {
267 if (CONN_TS(obj)->hostname)
268 free(CONN_TS(obj)->hostname);
269 if (CONN_TS(obj)->metric)
270 free(CONN_TS(obj)->metric);
271 } /* conn_ts_destroy */
273 #ifdef __cplusplus
274 } /* extern "C" */
275 #endif
277 #endif /* ! SDB_FRONTEND_CONNECTION_H */
279 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */