1 /*
2 * SysDB - src/frontend/connection.c
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 #include "sysdb.h"
29 #include "core/object.h"
30 #include "frontend/connection-private.h"
31 #include "utils/error.h"
32 #include "utils/strbuf.h"
33 #include "utils/proto.h"
35 #include <assert.h>
36 #include <errno.h>
38 #include <arpa/inet.h>
39 #include <fcntl.h>
41 #include <string.h>
43 /*
44 * private data types
45 */
47 /* name of connection objects */
48 #define CONN_FD_PREFIX "conn#"
49 #define CONN_FD_PLACEHOLDER "XXXXXXX"
51 static int
52 connection_init(sdb_object_t *obj, va_list ap)
53 {
54 sdb_conn_t *conn;
55 int sock_fd;
56 int sock_fl;
58 assert(obj);
59 conn = CONN(obj);
61 sock_fd = va_arg(ap, int);
63 conn->buf = sdb_strbuf_create(/* size = */ 128);
64 if (! conn->buf) {
65 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
66 "for a new connection");
67 return -1;
68 }
69 conn->errbuf = sdb_strbuf_create(0);
70 if (! conn->errbuf) {
71 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate an error buffer "
72 "for a new connection");
73 return -1;
74 }
76 conn->client_addr_len = sizeof(conn->client_addr);
77 conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
78 &conn->client_addr_len);
80 if (conn->fd < 0) {
81 char buf[1024];
82 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
83 "connection: %s", sdb_strerror(errno,
84 buf, sizeof(buf)));
85 return -1;
86 }
88 if (conn->client_addr.ss_family != AF_UNIX) {
89 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
90 "unexpected family type %d", conn->client_addr.ss_family);
91 return -1;
92 }
94 sock_fl = fcntl(conn->fd, F_GETFL);
95 if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
96 char buf[1024];
97 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
98 "to non-blocking mode: %s", conn->fd,
99 sdb_strerror(errno, buf, sizeof(buf)));
100 return -1;
101 }
103 sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
104 conn->fd);
106 conn->cmd = CONNECTION_IDLE;
107 conn->cmd_len = 0;
109 /* update the object name */
110 snprintf(obj->name + strlen(CONN_FD_PREFIX),
111 strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
112 return 0;
113 } /* connection_init */
115 static void
116 connection_destroy(sdb_object_t *obj)
117 {
118 sdb_conn_t *conn;
119 size_t len;
121 assert(obj);
122 conn = CONN(obj);
124 if (conn->buf) {
125 len = sdb_strbuf_len(conn->buf);
126 if (len)
127 sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
128 "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
129 }
131 sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i",
132 conn->fd);
133 close(conn->fd);
134 conn->fd = -1;
136 sdb_strbuf_destroy(conn->buf);
137 conn->buf = NULL;
138 sdb_strbuf_destroy(conn->errbuf);
139 conn->errbuf = NULL;
140 } /* connection_destroy */
142 static sdb_type_t connection_type = {
143 /* size = */ sizeof(sdb_conn_t),
144 /* init = */ connection_init,
145 /* destroy = */ connection_destroy,
146 };
148 /*
149 * connection handler functions
150 */
152 static uint32_t
153 connection_get_int32(sdb_conn_t *conn, size_t offset)
154 {
155 const char *data;
156 uint32_t n;
158 assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
160 data = sdb_strbuf_string(conn->buf);
161 memcpy(&n, data + offset, sizeof(n));
162 n = ntohl(n);
163 return n;
164 } /* connection_get_int32 */
166 static int
167 command_handle(sdb_conn_t *conn)
168 {
169 int status = -1;
171 assert(conn && (conn->cmd != CONNECTION_IDLE));
173 sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
174 conn->cmd, conn->cmd_len);
176 /* reset */
177 sdb_strbuf_sprintf(conn->errbuf, "");
179 switch (conn->cmd) {
180 case CONNECTION_PING:
181 status = sdb_connection_ping(conn);
182 break;
183 case CONNECTION_STARTUP:
184 status = sdb_fe_session_start(conn);
185 break;
187 case CONNECTION_QUERY:
188 {
189 sdb_llist_t *parsetree;
190 sdb_conn_node_t *node = NULL;
192 parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
193 (int)conn->cmd_len);
194 if (! parsetree) {
195 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
196 sdb_strbuf_string(conn->buf));
197 status = -1;
198 break;
199 }
201 switch (sdb_llist_len(parsetree)) {
202 case 0:
203 /* skipping empty command */
204 break;
205 case 1:
206 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
207 break;
209 default:
210 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %d command%s "
211 "in multi-statement query '%s'",
212 sdb_llist_len(parsetree) - 1,
213 sdb_llist_len(parsetree) == 2 ? "" : "s",
214 sdb_strbuf_string(conn->buf));
215 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
216 }
218 if (node)
219 status = sdb_fe_exec(conn, node);
221 sdb_llist_destroy(parsetree);
222 break;
223 }
225 case CONNECTION_LIST:
226 status = sdb_fe_list(conn);
227 break;
229 default:
230 {
231 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command %#x",
232 conn->cmd);
233 sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
234 status = -1;
235 break;
236 }
237 }
239 if (status)
240 sdb_connection_send(conn, CONNECTION_ERROR,
241 (uint32_t)sdb_strbuf_len(conn->errbuf),
242 sdb_strbuf_string(conn->errbuf));
244 /* remove the command from the buffer */
245 if (conn->cmd_len)
246 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
247 conn->cmd = CONNECTION_IDLE;
248 conn->cmd_len = 0;
249 return status;
250 } /* command_handle */
252 /* initialize the connection state information */
253 static int
254 command_init(sdb_conn_t *conn)
255 {
256 size_t len;
258 assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
260 conn->cmd = connection_get_int32(conn, 0);
261 conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
263 len = 2 * sizeof(uint32_t);
264 if (conn->cmd == CONNECTION_IDLE)
265 len += conn->cmd_len;
266 sdb_strbuf_skip(conn->buf, 0, len);
267 return 0;
268 } /* command_init */
270 /* returns negative value on error, 0 on EOF, number of octets else */
271 static ssize_t
272 connection_read(sdb_conn_t *conn)
273 {
274 ssize_t n = 0;
276 while (42) {
277 ssize_t status;
279 errno = 0;
280 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
281 if (status < 0) {
282 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
283 break;
284 return (int)status;
285 }
286 else if (! status) /* EOF */
287 break;
289 n += status;
290 }
292 return n;
293 } /* connection_read */
295 /*
296 * public API
297 */
299 sdb_conn_t *
300 sdb_connection_accept(int fd)
301 {
302 if (fd < 0)
303 return NULL;
305 /* the placeholder will be replaced with the accepted file
306 * descriptor when initializing the object */
307 return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
308 connection_type, fd));
309 } /* sdb_connection_create */
311 void
312 sdb_connection_close(sdb_conn_t *conn)
313 {
314 sdb_object_deref(SDB_OBJ(conn));
315 } /* sdb_connection_close */
317 ssize_t
318 sdb_connection_read(sdb_conn_t *conn)
319 {
320 ssize_t n = 0;
322 while (42) {
323 ssize_t status = connection_read(conn);
325 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
326 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
327 command_init(conn);
328 if ((conn->cmd != CONNECTION_IDLE)
329 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
330 command_handle(conn);
332 if (status <= 0)
333 break;
335 n += status;
336 }
337 return n;
338 } /* sdb_connection_read */
340 ssize_t
341 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
342 uint32_t msg_len, const char *msg)
343 {
344 ssize_t status;
346 if ((! conn) || (conn->fd < 0))
347 return -1;
349 status = sdb_proto_send_msg(conn->fd, code, msg_len, msg);
350 if (status < 0) {
351 char errbuf[1024];
353 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
354 "(code: %u, len: %u) to client: %s", code, msg_len,
355 sdb_strerror(errno, errbuf, sizeof(errbuf)));
356 }
357 return status;
358 } /* sdb_connection_send */
360 int
361 sdb_connection_ping(sdb_conn_t *conn)
362 {
363 if ((! conn) || (conn->cmd != CONNECTION_PING))
364 return -1;
366 /* we're alive */
367 sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
368 return 0;
369 } /* sdb_connection_ping */
371 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */