Code

Moved core/error to utils/error.
[sysdb.git] / src / frontend / connection.c
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)
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)
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)
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_LIST:
188                         status = sdb_fe_list(conn);
189                         break;
191                 default:
192                 {
193                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command");
194                         sdb_strbuf_sprintf(conn->errbuf, "Invalid command %#x", conn->cmd);
195                         status = -1;
196                         break;
197                 }
198         }
200         if (status)
201                 sdb_connection_send(conn, CONNECTION_ERROR,
202                                 (uint32_t)sdb_strbuf_len(conn->errbuf),
203                                 sdb_strbuf_string(conn->errbuf));
205         /* remove the command from the buffer */
206         if (conn->cmd_len)
207                 sdb_strbuf_skip(conn->buf, 0, conn->cmd_len);
208         conn->cmd = CONNECTION_IDLE;
209         conn->cmd_len = 0;
210         return status;
211 } /* command_handle */
213 /* initialize the connection state information */
214 static int
215 command_init(sdb_conn_t *conn)
217         size_t len;
219         assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
221         conn->cmd = connection_get_int32(conn, 0);
222         conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
224         len = 2 * sizeof(uint32_t);
225         if (conn->cmd == CONNECTION_IDLE)
226                 len += conn->cmd_len;
227         sdb_strbuf_skip(conn->buf, 0, len);
228         return 0;
229 } /* command_init */
231 /* returns negative value on error, 0 on EOF, number of octets else */
232 static ssize_t
233 connection_read(sdb_conn_t *conn)
235         ssize_t n = 0;
237         while (42) {
238                 ssize_t status;
240                 errno = 0;
241                 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
242                 if (status < 0) {
243                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
244                                 break;
245                         return (int)status;
246                 }
247                 else if (! status) /* EOF */
248                         break;
250                 n += status;
251         }
253         return n;
254 } /* connection_read */
256 /*
257  * public API
258  */
260 sdb_conn_t *
261 sdb_connection_accept(int fd)
263         if (fd < 0)
264                 return NULL;
266         /* the placeholder will be replaced with the accepted file
267          * descriptor when initializing the object */
268         return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
269                                 connection_type, fd));
270 } /* sdb_connection_create */
272 void
273 sdb_connection_close(sdb_conn_t *conn)
275         sdb_object_deref(SDB_OBJ(conn));
276 } /* sdb_connection_close */
278 ssize_t
279 sdb_connection_read(sdb_conn_t *conn)
281         ssize_t n = 0;
283         while (42) {
284                 ssize_t status = connection_read(conn);
286                 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
287                                 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
288                         command_init(conn);
289                 if ((conn->cmd != CONNECTION_IDLE)
290                                 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
291                         command_handle(conn);
293                 if (status <= 0)
294                         break;
296                 n += status;
297         }
298         return n;
299 } /* sdb_connection_read */
301 ssize_t
302 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
303                 uint32_t msg_len, const char *msg)
305         ssize_t status;
307         if ((! conn) || (conn->fd < 0))
308                 return -1;
310         status = sdb_proto_send_msg(conn->fd, code, msg_len, msg);
311         if (status < 0) {
312                 char errbuf[1024];
314                 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
315                                 "(code: %u, len: %u) to client: %s", code, msg_len,
316                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
317         }
318         return status;
319 } /* sdb_connection_send */
321 int
322 sdb_connection_ping(sdb_conn_t *conn)
324         if ((! conn) || (conn->cmd != CONNECTION_PING))
325                 return -1;
327         /* we're alive */
328         sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
329         return 0;
330 } /* sdb_connection_ping */
332 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */