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)
110 {
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)
145 {
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)
159 {
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;
174 default:
175 {
176 char errbuf[1024];
177 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring invalid command");
178 snprintf(errbuf, sizeof(errbuf), "Invalid command %#x", conn->cmd);
179 sdb_connection_send(conn, CONNECTION_ERROR,
180 (uint32_t)(strlen(errbuf) + 1), errbuf);
181 status = -1;
182 break;
183 }
184 }
186 /* remove the command from the buffer */
187 if (conn->cmd_len)
188 sdb_strbuf_skip(conn->buf, conn->cmd_len);
189 conn->cmd = CONNECTION_IDLE;
190 conn->cmd_len = 0;
191 return status;
192 } /* command_handle */
194 /* initialize the connection state information */
195 static int
196 command_init(sdb_conn_t *conn)
197 {
198 size_t len;
200 assert(conn && (conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len));
202 conn->cmd = connection_get_int32(conn, 0);
203 conn->cmd_len = connection_get_int32(conn, sizeof(uint32_t));
205 len = 2 * sizeof(uint32_t);
206 if (conn->cmd == CONNECTION_IDLE)
207 len += conn->cmd_len;
208 sdb_strbuf_skip(conn->buf, len);
209 return 0;
210 } /* command_init */
212 /* returns negative value on error, 0 on EOF, number of octets else */
213 static ssize_t
214 connection_read(sdb_conn_t *conn)
215 {
216 ssize_t n = 0;
218 while (42) {
219 ssize_t status;
221 errno = 0;
222 status = sdb_strbuf_read(conn->buf, conn->fd, 1024);
223 if (status < 0) {
224 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
225 break;
226 return (int)status;
227 }
228 else if (! status) /* EOF */
229 break;
231 n += status;
232 }
234 return n;
235 } /* connection_read */
237 /*
238 * public API
239 */
241 sdb_conn_t *
242 sdb_connection_accept(int fd)
243 {
244 if (fd < 0)
245 return NULL;
247 /* the placeholder will be replaced with the accepted file
248 * descriptor when initializing the object */
249 return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
250 connection_type, fd));
251 } /* sdb_connection_create */
253 void
254 sdb_connection_close(sdb_conn_t *conn)
255 {
256 sdb_object_deref(SDB_OBJ(conn));
257 } /* sdb_connection_close */
259 ssize_t
260 sdb_connection_read(sdb_conn_t *conn)
261 {
262 ssize_t n = 0;
264 while (42) {
265 ssize_t status = connection_read(conn);
267 if ((conn->cmd == CONNECTION_IDLE) && (! conn->cmd_len)
268 && (sdb_strbuf_len(conn->buf) >= 2 * sizeof(int32_t)))
269 command_init(conn);
270 if ((conn->cmd != CONNECTION_IDLE)
271 && (sdb_strbuf_len(conn->buf) >= conn->cmd_len))
272 command_handle(conn);
274 if (status <= 0)
275 break;
277 n += status;
278 }
279 return n;
280 } /* sdb_connection_read */
282 ssize_t
283 sdb_connection_send(sdb_conn_t *conn, uint32_t code,
284 uint32_t msg_len, char *msg)
285 {
286 size_t len = 2 * sizeof(uint32_t) + msg_len;
287 char buffer[len];
288 char *buf;
290 uint32_t tmp;
292 if ((! conn) || (conn->fd < 0))
293 return -1;
295 tmp = htonl(code);
296 memcpy(buffer, &tmp, sizeof(uint32_t));
298 tmp = htonl(msg_len);
299 memcpy(buffer + sizeof(uint32_t), &tmp, sizeof(uint32_t));
301 if (msg_len)
302 memcpy(buffer + 2 * sizeof(uint32_t), msg, msg_len);
304 buf = buffer;
305 while (len > 0) {
306 ssize_t status;
308 /* XXX: use select() */
310 errno = 0;
311 status = write(conn->fd, buf, len);
312 if (status < 0) {
313 char errbuf[1024];
315 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
316 continue;
317 if (errno == EINTR)
318 continue;
320 sdb_log(SDB_LOG_ERR, "frontend: Failed to send msg "
321 "(code: %u, len: %u) to client: %s", code, msg_len,
322 sdb_strerror(errno, errbuf, sizeof(errbuf)));
323 return status;
324 }
326 len -= (size_t)status;
327 buf += status;
328 }
329 return (ssize_t)len;
330 } /* sdb_connection_send */
332 int
333 sdb_connection_ping(sdb_conn_t *conn)
334 {
335 if ((! conn) || (conn->cmd != CONNECTION_PING))
336 return -1;
338 /* we're alive */
339 sdb_connection_send(conn, CONNECTION_OK, 0, NULL);
340 return 0;
341 } /* sdb_connection_ping */
343 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */