]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
libvici: Add callback invoked if connection is closed by daemon
authorTobias Brunner <tobias@strongswan.org>
Thu, 26 Jan 2023 10:16:11 +0000 (11:16 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 22 Feb 2023 10:44:14 +0000 (11:44 +0100)
src/libcharon/plugins/vici/libvici.c
src/libcharon/plugins/vici/libvici.h

index da934294ac42da522708049a63bfe9237bc4499f..95ee7c8faacb357c852ee9e796451d602a3deb4f 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2023 Tobias Brunner
  * Copyright (C) 2014 Martin Willi
  *
  * Copyright (C) secunet Security Networks AG
@@ -66,6 +67,10 @@ struct vici_conn_t {
        int error;
        /** wait state */
        wait_state_t wait;
+       /** callback if connection closed */
+       vici_close_cb_t on_close;
+       /** user data for above callback */
+       void *on_close_user;
 };
 
 /**
@@ -118,6 +123,10 @@ static bool wait_result(vici_conn_t *conn, wait_state_t wait)
 static bool read_error(vici_conn_t *conn, int err)
 {
        conn->error = err;
+       if (err == ECONNRESET && conn->on_close)
+       {
+               conn->on_close(conn->on_close_user);
+       }
        return wait_result(conn, WAIT_READ_ERROR);
 }
 
@@ -210,6 +219,10 @@ CALLBACK(on_read, bool,
                {
                        return TRUE;
                }
+               if (!hlen)
+               {
+                       errno = ECONNRESET;
+               }
                return read_error(conn, errno);
        }
        if (hlen < sizeof(len))
@@ -744,6 +757,12 @@ int vici_register(vici_conn_t *conn, char *name, vici_event_cb_t cb, void *user)
        return ret;
 }
 
+void vici_on_close(vici_conn_t *conn, vici_close_cb_t cb, void *user)
+{
+       conn->on_close = cb;
+       conn->on_close_user = user;
+}
+
 void vici_init()
 {
        library_init(NULL, "vici");
index 8193199b844a8f600fd6cb69448114f153f26958..d3b1ba61490d226d979caf19721d86bd4e23b7c9 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2023 Tobias Brunner
  * Copyright (C) 2014 Martin Willi
  *
  * libvici.h is MIT-licensed to simplify reuse, but please note that libvici.c
@@ -78,6 +79,8 @@
  * To register or unregister for asynchronous event messages vici_register() is
  * used. The registered callback gets invoked by an asynchronous thread. To
  * parse the event message, the vici_parse*() functions can be used.
+ * To get notified if the connection is closed by the vici service while waiting
+ * for event messages, vici_on_close() may be used.
  */
 
 #ifndef LIBVICI_H_
@@ -160,6 +163,13 @@ typedef int        (*vici_parse_value_cb_t)(void *user, vici_res_t *res, char *name,
  */
 typedef int (*vici_parse_section_cb_t)(void *user, vici_res_t *res, char *name);
 
+/**
+ * Callback function invoked if the connection is closed by the vici service.
+ *
+ * @param user         user data, as passed to vici_on_close()
+ */
+typedef void (*vici_close_cb_t)(void *user);
+
 /**
  * Open a new vici connection.
  *
@@ -458,6 +468,18 @@ void vici_free_res(vici_res_t *res);
  */
 int vici_register(vici_conn_t *conn, char *name, vici_event_cb_t cb, void *user);
 
+/**
+ * (Un-)Register a callback that's invoked if the connection is closed by the
+ * vici service.
+ *
+ * Primarily useful when listening for events via vici_register(). The callback
+ * gets invoked by a different thread from the libstrongswan thread pool.
+ *
+ * @param cb           callback function to register, NULL to unregister
+ * @param user         user data passed to callback invocation
+ */
+void vici_on_close(vici_conn_t *conn, vici_close_cb_t cb, void *user);
+
 /**
  * Initialize libvici before first time use.
  */