]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Use a helper macro for SQL trunk notify callback to avoid boilerplate
authorNick Porter <nick@portercomputing.co.uk>
Tue, 16 Jul 2024 11:21:38 +0000 (12:21 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Mon, 29 Jul 2024 18:14:49 +0000 (19:14 +0100)
src/lib/server/trunk.h
src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c

index 6c6db3e669811ced7b0519f100d878b317764681..7f6ce09baa7763070f43ae1c2f5f18d1de4748ef 100644 (file)
@@ -937,6 +937,54 @@ bool trunk_request_search(trunk_request_t *treq, void *ptr);
 
 #undef _CONST
 
+/** Helper macro for building generic trunk notify callback
+ *
+ * @param _name        of the callback function to build
+ * @param _type of the conn->h handle.  Needs to contain an fd element.
+ */
+#define TRUNK_NOTIFY_FUNC(_name, _type) \
+static void _conn_writeable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) \
+{ \
+       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \
+       trunk_connection_signal_writable(tconn); \
+} \
+static void _conn_readable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx) \
+{ \
+       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \
+       trunk_connection_signal_readable(tconn); \
+} \
+static void _conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx) \
+{ \
+       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t); \
+       ERROR("%s - Connection failed: %s", tconn->conn->name, fr_syserror(fd_errno)); \
+       connection_signal_reconnect(tconn->conn, CONNECTION_FAILED); \
+} \
+static void _name(trunk_connection_t *tconn, connection_t *conn, \
+                 fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx) \
+{ \
+       _type                   *c = talloc_get_type_abort(conn->h, _type); \
+       fr_event_fd_cb_t        read_fn = NULL, write_fn = NULL; \
+       switch (notify_on) { \
+       case TRUNK_CONN_EVENT_NONE: \
+               fr_event_fd_delete(el, c->fd, FR_EVENT_FILTER_IO); \
+               return; \
+       case TRUNK_CONN_EVENT_READ: \
+               read_fn = _conn_readable; \
+               break; \
+       case TRUNK_CONN_EVENT_WRITE: \
+               write_fn = _conn_writeable; \
+               break; \
+       case TRUNK_CONN_EVENT_BOTH: \
+               read_fn = _conn_readable; \
+               write_fn = _conn_writeable; \
+               break; \
+       } \
+       if (fr_event_fd_insert(c, NULL, el, c->fd, read_fn, write_fn, _conn_error, tconn) <0) { \
+               PERROR("Failed inserting FD event"); \
+               trunk_connection_signal_reconnect(tconn, CONNECTION_FAILED); \
+       } \
+}
+
 #ifdef __cplusplus
 }
 #endif
index 4043b4070f030956dbd9e6236ae01db34b3d3ba2..76213d7b461bc23b4c778c0e5bae0372d2f01418 100644 (file)
@@ -827,25 +827,6 @@ static size_t sql_escape_func(UNUSED request_t *request, char *out, size_t outle
        return mysql_real_escape_string(&conn->db, out, in, inlen);
 }
 
-static void sql_conn_writable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
-{
-       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
-       trunk_connection_signal_writable(tconn);
-}
-
-static void sql_conn_readable(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
-{
-       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
-       trunk_connection_signal_readable(tconn);
-}
-
-static void sql_conn_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
-{
-       trunk_connection_t      *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
-       ERROR("%s - Connection failed: %s", tconn->conn->name, fr_syserror(fd_errno));
-       connection_signal_reconnect(tconn->conn, CONNECTION_FAILED);
-}
-
 /** Allocate an SQL trunk connection
  *
  * @param[in] tconn            Trunk handle.
@@ -875,37 +856,7 @@ static connection_t *sql_trunk_connection_alloc(trunk_connection_t *tconn, fr_ev
        return conn;
 }
 
-static void sql_trunk_connection_notify(trunk_connection_t *tconn, connection_t *conn,
-                                       fr_event_list_t *el,
-                                       trunk_connection_event_t notify_on, UNUSED void *uctx)
-{
-       rlm_sql_mysql_conn_t    *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_mysql_conn_t);
-       fr_event_fd_cb_t        read_fn = NULL, write_fn = NULL;
-
-       switch (notify_on) {
-       case TRUNK_CONN_EVENT_NONE:
-               fr_event_fd_delete(el, sql_conn->fd, FR_EVENT_FILTER_IO);
-               return;
-
-       case TRUNK_CONN_EVENT_READ:
-               read_fn = sql_conn_readable;
-               break;
-
-       case TRUNK_CONN_EVENT_WRITE:
-               write_fn = sql_conn_writable;
-               break;
-
-       case TRUNK_CONN_EVENT_BOTH:
-               read_fn = sql_conn_readable;
-               write_fn = sql_conn_writable;
-               break;
-       }
-
-       if (fr_event_fd_insert(sql_conn, NULL, el, sql_conn->fd, read_fn, write_fn, sql_conn_error, tconn) < 0) {
-               PERROR("Failed inserting FD event");
-               trunk_connection_signal_reconnect(tconn, CONNECTION_FAILED);
-       }
-}
+TRUNK_NOTIFY_FUNC(sql_trunk_connection_notify, rlm_sql_mysql_conn_t)
 
 static void sql_trunk_request_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn,
                                  connection_t *conn, UNUSED void *uctx)