Code

94c3b4a576ef443602b14ebf4e0b969cd0a22cfd
[sysdb.git] / src / frontend / connection-private.h
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/ssl.h"
41 #include "utils/strbuf.h"
43 #include <inttypes.h>
44 #include <arpa/inet.h>
46 #include <stdlib.h>
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
52 struct sdb_conn {
53         sdb_object_t super;
55         /* file-descriptor of the open connection */
56         int fd;
58         /* connection and client information */
59         struct sockaddr_storage client_addr;
60         socklen_t client_addr_len;
62         /* connection handling */
63         ssize_t (*read)(sdb_conn_t *, size_t);
64         ssize_t (*write)(sdb_conn_t *, const void *, size_t);
65         int (*finish)(sdb_conn_t *);
66         sdb_ssl_session_t *ssl_session;
68         /* read buffer */
69         sdb_strbuf_t *buf;
71         /* connection / protocol state information */
72         uint32_t cmd;
73         uint32_t cmd_len;
75         /* amount of data to skip, e.g., after receiving invalid commands; if this
76          * is non-zero, the 'skip_len' first bytes of 'buf' are invalid */
77         size_t skip_len;
79         sdb_strbuf_t *errbuf;
81         /* user information */
82         char *username; /* NULL if the user has not been authenticated */
83         bool  ready; /* indicates that startup finished successfully */
84 };
85 #define CONN(obj) ((sdb_conn_t *)(obj))
87 /*
88  * node types
89  */
91 typedef struct {
92         sdb_conn_node_t super;
93         sdb_store_expr_t *expr;
94 } conn_expr_t;
95 #define CONN_EXPR(obj) ((conn_expr_t *)(obj))
97 typedef struct {
98         sdb_conn_node_t super;
99         sdb_store_matcher_t *matcher;
100 } conn_matcher_t;
101 #define CONN_MATCHER(obj) ((conn_matcher_t *)(obj))
103 typedef struct {
104         sdb_conn_node_t super;
105         int type;
106         conn_matcher_t *filter;
107 } conn_list_t;
108 #define CONN_LIST(obj) ((conn_list_t *)(obj))
110 typedef struct {
111         sdb_conn_node_t super;
112         int type;
113         char *host;
114         char *name; /* NULL for type == SDB_HOST */
115         conn_matcher_t *filter;
116 } conn_fetch_t;
117 #define CONN_FETCH(obj) ((conn_fetch_t *)(obj))
119 typedef struct {
120         sdb_conn_node_t super;
121         int type;
122         conn_matcher_t *matcher;
123         conn_matcher_t *filter;
124 } conn_lookup_t;
125 #define CONN_LOOKUP(obj) ((conn_lookup_t *)(obj))
127 typedef struct {
128         sdb_conn_node_t super;
129         char *name;
130         sdb_time_t last_update;
131 } conn_store_host_t;
132 #define CONN_STORE_HOST(obj) ((conn_store_host_t *)(obj))
134 typedef struct {
135         sdb_conn_node_t super;
136         char *hostname;
137         char *name;
138         sdb_time_t last_update;
139 } conn_store_svc_t;
140 #define CONN_STORE_SVC(obj) ((conn_store_svc_t *)(obj))
142 typedef struct {
143         sdb_conn_node_t super;
144         char *hostname;
145         char *name;
146         char *store_type; /* optional */
147         char *store_id;   /* optional */
148         sdb_time_t last_update;
149 } conn_store_metric_t;
150 #define CONN_STORE_METRIC(obj) ((conn_store_metric_t *)(obj))
152 typedef struct {
153         sdb_conn_node_t super;
154         int parent_type;
155         char *hostname; /* optional */
156         char *parent;
157         char *key;
158         sdb_data_t value;
159         sdb_time_t last_update;
160 } conn_store_attr_t;
161 #define CONN_STORE_ATTR(obj) ((conn_store_attr_t *)(obj))
163 typedef struct {
164         sdb_conn_node_t super;
165         char *hostname;
166         char *metric;
167         sdb_timeseries_opts_t opts;
168 } conn_ts_t;
169 #define CONN_TS(obj) ((conn_ts_t *)(obj))
171 /*
172  * type helper functions
173  */
175 static void __attribute__((unused))
176 conn_expr_destroy(sdb_object_t *obj)
178         sdb_object_deref(SDB_OBJ(CONN_EXPR(obj)->expr));
179 } /* conn_expr_destroy */
181 static void __attribute__((unused))
182 conn_matcher_destroy(sdb_object_t *obj)
184         sdb_object_deref(SDB_OBJ(CONN_MATCHER(obj)->matcher));
185 } /* conn_matcher_destroy */
187 static void __attribute__((unused))
188 conn_list_destroy(sdb_object_t *obj)
190         sdb_object_deref(SDB_OBJ(CONN_LIST(obj)->filter));
191 } /* conn_list_destroy */
193 static void __attribute__((unused))
194 conn_fetch_destroy(sdb_object_t *obj)
196         if (CONN_FETCH(obj)->host)
197                 free(CONN_FETCH(obj)->host);
198         if (CONN_FETCH(obj)->name)
199                 free(CONN_FETCH(obj)->name);
200         sdb_object_deref(SDB_OBJ(CONN_FETCH(obj)->filter));
201 } /* conn_fetch_destroy */
203 static void __attribute__((unused))
204 conn_lookup_destroy(sdb_object_t *obj)
206         sdb_object_deref(SDB_OBJ(CONN_LOOKUP(obj)->matcher));
207         sdb_object_deref(SDB_OBJ(CONN_LOOKUP(obj)->filter));
208 } /* conn_lookup_destroy */
210 static void __attribute__((unused))
211 conn_store_host_destroy(sdb_object_t *obj)
213         conn_store_host_t *host = CONN_STORE_HOST(obj);
214         if (host->name)
215                 free((void *)host->name);
216         host->name = NULL;
217 } /* conn_store_host_destroy */
219 static void __attribute__((unused))
220 conn_store_svc_destroy(sdb_object_t *obj)
222         conn_store_svc_t *svc = CONN_STORE_SVC(obj);
223         if (svc->hostname)
224                 free((void *)svc->hostname);
225         svc->hostname = NULL;
226         if (svc->name)
227                 free((void *)svc->name);
228         svc->name = NULL;
229 } /* conn_store_svc_destroy */
231 static void __attribute__((unused))
232 conn_store_metric_destroy(sdb_object_t *obj)
234         conn_store_metric_t *metric = CONN_STORE_METRIC(obj);
235         if (metric->hostname)
236                 free((void *)metric->hostname);
237         metric->hostname = NULL;
238         if (metric->name)
239                 free((void *)metric->name);
240         metric->name = NULL;
241         if (metric->store_type)
242                 free((void *)metric->store_type);
243         metric->store_type = NULL;
244         if (metric->store_id)
245                 free((void *)metric->store_id);
246         metric->store_id = NULL;
247 } /* conn_store_metric_destroy */
249 static void __attribute__((unused))
250 conn_store_attr_destroy(sdb_object_t *obj)
252         conn_store_attr_t *attr = CONN_STORE_ATTR(obj);
253         if (attr->hostname)
254                 free((void *)attr->hostname);
255         attr->hostname = NULL;
256         if (attr->parent)
257                 free((void *)attr->parent);
258         attr->parent = NULL;
259         if (attr->key)
260                 free((void *)attr->key);
261         attr->key = NULL;
262         sdb_data_free_datum(&attr->value);
263 } /* conn_store_attr_destroy */
265 static void __attribute__((unused))
266 conn_ts_destroy(sdb_object_t *obj)
268         if (CONN_TS(obj)->hostname)
269                 free(CONN_TS(obj)->hostname);
270         if (CONN_TS(obj)->metric)
271                 free(CONN_TS(obj)->metric);
272 } /* conn_ts_destroy */
274 #ifdef __cplusplus
275 } /* extern "C" */
276 #endif
278 #endif /* ! SDB_FRONTEND_CONNECTION_H */
280 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */