Code

04d12222c89838e8b9ed2a126e3a778773a912bf
[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/error.h"
30 #include "core/object.h"
31 #include "frontend/connection-private.h"
32 #include "utils/strbuf.h"
34 #include <assert.h>
35 #include <errno.h>
37 #include <arpa/inet.h>
38 #include <fcntl.h>
40 #include <string.h>
42 /*
43  * private data types
44  */
46 /* name of connection objects */
47 #define CONN_FD_PREFIX "conn#"
48 #define CONN_FD_PLACEHOLDER "XXXXXXX"
50 static int
51 connection_init(sdb_object_t *obj, va_list ap)
52 {
53         sdb_conn_t *conn;
54         int sock_fd;
55         int sock_fl;
57         assert(obj);
58         conn = CONN(obj);
60         sock_fd = va_arg(ap, int);
62         conn->buf = sdb_strbuf_create(/* size = */ 128);
63         if (! conn->buf) {
64                 sdb_log(SDB_LOG_ERR, "frontend: Failed to allocate a read buffer "
65                                 "for a new connection");
66                 return -1;
67         }
69         conn->client_addr_len = sizeof(conn->client_addr);
70         conn->fd = accept(sock_fd, (struct sockaddr *)&conn->client_addr,
71                         &conn->client_addr_len);
73         if (conn->fd < 0) {
74                 char buf[1024];
75                 sdb_log(SDB_LOG_ERR, "frontend: Failed to accept remote "
76                                 "connection: %s", sdb_strerror(errno,
77                                         buf, sizeof(buf)));
78                 return -1;
79         }
81         if (conn->client_addr.ss_family != AF_UNIX) {
82                 sdb_log(SDB_LOG_ERR, "frontend: Accepted connection using "
83                                 "unexpected family type %d", conn->client_addr.ss_family);
84                 return -1;
85         }
87         sock_fl = fcntl(conn->fd, F_GETFL);
88         if (fcntl(conn->fd, F_SETFL, sock_fl | O_NONBLOCK)) {
89                 char buf[1024];
90                 sdb_log(SDB_LOG_ERR, "frontend: Failed to switch connection conn#%i "
91                                 "to non-blocking mode: %s", conn->fd,
92                                 sdb_strerror(errno, buf, sizeof(buf)));
93                 return -1;
94         }
96         sdb_log(SDB_LOG_DEBUG, "frontend: Accepted connection on fd=%i",
97                         conn->fd);
99         conn->cmd = CONNECTION_IDLE;
100         conn->cmd_len = 0;
102         /* update the object name */
103         snprintf(obj->name + strlen(CONN_FD_PREFIX),
104                         strlen(CONN_FD_PLACEHOLDER), "%i", conn->fd);
105         return 0;
106 } /* connection_init */
108 static void
109 connection_destroy(sdb_object_t *obj)
111         sdb_conn_t *conn;
112         size_t len;
114         assert(obj);
115         conn = CONN(obj);
117         if (conn->buf) {
118                 len = sdb_strbuf_len(conn->buf);
119                 if (len)
120                         sdb_log(SDB_LOG_INFO, "frontend: Discarding incomplete command "
121                                         "(%zu byte%s left in buffer)", len, len == 1 ? "" : "s");
122         }
124         sdb_log(SDB_LOG_DEBUG, "frontend: Closing connection on fd=%i",
125                         conn->fd);
126         close(conn->fd);
127         conn->fd = -1;
129         sdb_strbuf_destroy(conn->buf);
130         conn->buf = NULL;
131 } /* connection_destroy */
133 static sdb_type_t connection_type = {
134         /* size = */ sizeof(sdb_conn_t),
135         /* init = */ connection_init,
136         /* destroy = */ connection_destroy,
137 };
139 /*
140  * connection handler functions
141  */
143 static uint32_t
144 connection_get_int32(sdb_conn_t *conn, size_t offset)
146         const char *data;
147         uint32_t n;
149         assert(conn && (sdb_strbuf_len(conn->buf) >= offset + sizeof(uint32_t)));
151         data = sdb_strbuf_string(conn->buf);
152         memcpy(&n, data + offset, sizeof(n));
153         n = ntohl(n);
154         return n;
155 } /* connection_get_int32 */
157 static int
158 command_handle(sdb_conn_t *conn)
160         int status = -1;
162         assert(conn && (conn->cmd != CONNECTION_IDLE));
164         sdb_log(SDB_LOG_DEBUG, "frontend: Handling command %u (len: %u)",
165                         conn->cmd, conn->cmd_len);
167         switch (conn->cmd) {
168                 case CONNECTION_PING:
169                         status = sdb_connection_ping(conn);
170                         break;
171                 case CONNECTION_STARTUP:
172                         status = sdb_session_start(conn);
173                         break;
175                 case CONNECTION_LIST:
176                         status = sdb_fe_list(conn);
177                         break;
179                 default:
180                 {
181                         char errbuf[1024];
182                         sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command");
183                         snprintf(errbuf, sizeof(errbuf), "Invalid command %#x", conn->cmd);
184                         sdb_connection_send(conn, CONNECTION_ERROR,
185                                         (uint32_t)(strlen(errbuf) + 1), errbuf);
186                         status = -1;
187                         break;
188                 }
189         }
191         /* remove the command from the buffer */
192         if (conn->cmd_len)
193                 sdb_strbuf_skip(conn->buf, conn->cmd_len);
194         conn->cmd = CONNECTION_IDLE;
195         conn->cmd_len = 0;
196         return status;
197 } /* command_handle */
199 /* initialize the connection state information */
200 static int
201 command_init(sdb_conn_t *conn)
203         size_t len;
205         assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
207         conn->cmd = connection_get_int32(conn, 0);
208         conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
210         len = 2 * sizeof(uint32_t);
211         if (conn->cmd == CONNECTION_IDLE)
212                 len += conn->cmd_len;
213         sdb_strbuf_skip(conn->buf, len);
214         return 0;
215 } /* command_init */
217 /* returns negative value on error, 0 on EOF, number of octets else */
218 static ssize_t
219 connection_read(sdb_conn_t *conn)
221         ssize_t n = 0;
223         while (42) {
224                 ssize_t status;
226                 errno = 0;
227                 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
228                 if (status < 0) {
229                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
230                                 break;
231                         return (int)status;
232                 }
233                 else if (! status) /* EOF */
234                         break;
236                 n += status;
237         }
239         return n;
240 } /* connection_read */
242 /*
243  * public API
244  */
246 sdb_conn_t *
247 sdb_connection_accept(int fd)
249         if (fd < 0)
250                 return NULL;
252         /* the placeholder will be replaced with the accepted file
253          * descriptor when initializing the object */
254         return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
255                                 connection_type, fd));
256 } /* sdb_connection_create */
258 void
259 sdb_connection_close(sdb_conn_t *conn)
261         sdb_object_deref(SDB_OBJ(conn));
262 } /* sdb_connection_close */
264 ssize_t
265 sdb_connection_read(sdb_conn_t *conn)
267         ssize_t n = 0;
269         while (42) {
270                 ssize_t status = connection_read(conn);
272                 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
273                                 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
274                         command_init(conn);
275                 if ((conn->cmd != CONNECTION_IDLE)
276                                 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
277                         command_handle(conn);
279                 if (status <= 0)
280                         break;
282                 n += status;
283         }
284         return n;
285 } /* sdb_connection_read */
287 ssize_t
288 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
289                 uint32_t msg_len, const char *msg)
291         size_t len = 2 * sizeof(uint32_t) + msg_len;
292         char buffer[len];
293         char *buf;
295         uint32_t tmp;
297         if ((! conn) || (conn->fd < 0))
298                 return -1;
300         tmp = htonl(code);
301         memcpy(buffer, &tmp, sizeof(uint32_t));
303         tmp = htonl(msg_len);
304         memcpy(buffer + sizeof(uint32_t), &tmp, sizeof(uint32_t));
306         if (msg_len)
307                 memcpy(buffer + 2 * sizeof(uint32_t), msg, msg_len);
309         buf = buffer;
310         while (len > 0) {
311                 ssize_t status;
313                 /* XXX: use select() */
315                 errno = 0;
316                 status = write(conn->fd, buf, len);
317                 if (status < 0) {
318                         char errbuf[1024];
320                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
321                                 continue;
322                         if (errno == EINTR)
323                                 continue;
325                         sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
326                                         "(code: %u, len: %u) to client: %s", code, msg_len,
327                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
328                         return status;
329                 }
331                 len -= (size_t)status;
332                 buf += status;
333         }
334         return (ssize_t)len;
335 } /* sdb_connection_send */
337 int
338 sdb_connection_ping(sdb_conn_t *conn)
340         if ((! conn) || (conn->cmd != CONNECTION_PING))
341                 return -1;
343         /* we're alive */
344         sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
345         return 0;
346 } /* sdb_connection_ping */
348 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */