From: Tobias Brunner Date: Thu, 26 Jan 2023 10:16:11 +0000 (+0100) Subject: libvici: Add callback invoked if connection is closed by daemon X-Git-Tag: 5.9.10rc1~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9e5533fef9405188954bb7b9be69f88f9a033666;p=thirdparty%2Fstrongswan.git libvici: Add callback invoked if connection is closed by daemon --- diff --git a/src/libcharon/plugins/vici/libvici.c b/src/libcharon/plugins/vici/libvici.c index da934294ac..95ee7c8faa 100644 --- a/src/libcharon/plugins/vici/libvici.c +++ b/src/libcharon/plugins/vici/libvici.c @@ -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"); diff --git a/src/libcharon/plugins/vici/libvici.h b/src/libcharon/plugins/vici/libvici.h index 8193199b84..d3b1ba6149 100644 --- a/src/libcharon/plugins/vici/libvici.h +++ b/src/libcharon/plugins/vici/libvici.h @@ -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. */