summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: aafdfe8)
raw | patch | inline | side by side (parent: aafdfe8)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 15 Jan 2015 08:05:57 +0000 (09:05 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 15 Jan 2015 08:05:57 +0000 (09:05 +0100) |
… rather than having it split up between the caller and accept().
src/frontend/connection.c | patch | blob | history | |
src/frontend/sock.c | patch | blob | history | |
src/include/frontend/connection.h | patch | blob | history |
index 5f368892c0970f89c14085172eb45e2bdf61fece..6b359d347a9f14e3026ebcd6a6df37708eab7da7 100644 (file)
} /* sdb_connection_enable_logging */
sdb_conn_t *
-sdb_connection_accept(int fd)
+sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data)
{
+ sdb_conn_t *conn;
+
if (fd < 0)
return NULL;
/* the placeholder will be replaced with the accepted file
* descriptor when initializing the object */
- return CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
+ conn = CONN(sdb_object_create(CONN_FD_PREFIX CONN_FD_PLACEHOLDER,
connection_type, fd));
+ if (setup && (setup(conn, user_data) < 0)) {
+ sdb_object_deref(SDB_OBJ(conn));
+ return NULL;
+ }
+ return conn;
} /* sdb_connection_create */
void
diff --git a/src/frontend/sock.c b/src/frontend/sock.c
index 7cb985d9735530c30caba90c26cbe85159ecd027..1827886628ae6236e991251e6b1d8ecd9a5ab34c 100644 (file)
--- a/src/frontend/sock.c
+++ b/src/frontend/sock.c
int type;
int sock_fd;
- int (*accept)(sdb_conn_t *);
- int (*peer)(sdb_conn_t *);
+ int (*setup)(sdb_conn_t *, void *);
} listener_t;
typedef struct {
*/
static int
-unixsock_peer(sdb_conn_t *conn)
+setup_unixsock(sdb_conn_t *conn, void __attribute__((unused)) *user_data)
{
uid_t uid;
return -1;
}
return 0;
-} /* unixsock_peer */
+} /* setup_unixsock */
static int
open_unixsock(listener_t *listener)
return -1;
}
- listener->peer = unixsock_peer;
+ listener->setup = setup_unixsock;
return 0;
} /* open_unixsock */
return NULL;
}
listener->type = type;
- listener->accept = NULL;
- listener->peer = NULL;
+ listener->setup = NULL;
if (listener_impls[type].open(listener)) {
/* prints error */
sdb_object_t *obj;
int status;
- obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd));
+ obj = SDB_OBJ(sdb_connection_accept(listener->sock_fd,
+ listener->setup, NULL));
if (! obj)
return -1;
- if (listener->accept && listener->accept(CONN(obj))) {
- /* accept() is expected to log an error */
- sdb_object_deref(obj);
- return -1;
- }
- if (listener->peer && listener->peer(CONN(obj))) {
- /* peer() is expected to log an error */
- sdb_object_deref(obj);
- return -1;
- }
-
status = sdb_llist_append(sock->open_connections, obj);
if (status)
sdb_log(SDB_LOG_ERR, "frontend: Failed to append "
index 803a195e45c31c2233e4348556368ea8b88538b3..c4f2bc716a75de34b31e5160cecb12fcc6e19a5b 100644 (file)
} sdb_conn_node_t;
#define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj))
+/*
+ * sdb_conn_setup_cb is a callback function used to setup a connection. For
+ * example, it may be used to initialize session information.
+ */
+typedef int (*sdb_conn_setup_cb)(sdb_conn_t *, void *);
+
/*
* sdb_connection_enable_logging:
* Enable logging of connection-related messages to the current client
/*
* sdb_connection_accpet:
* Accept a new connection on the specified file-descriptor 'fd' and return a
- * newly allocated connection object.
+ * newly allocated connection object. If specified, the setup callback is used
+ * to setup the newly created connection. It will receive the connection
+ * object and the specified user data as its arguments.
*
* Returns:
* - 0 on success
* - a negative value else
*/
sdb_conn_t *
-sdb_connection_accept(int fd);
+sdb_connection_accept(int fd, sdb_conn_setup_cb setup, void *user_data);
/*
* sdb_connection_close: