libvici: Add callback invoked if connection is closed by daemon

This commit is contained in:
Tobias Brunner 2023-01-26 11:16:11 +01:00
parent 2b206eaf6a
commit 9e5533fef9
2 changed files with 41 additions and 0 deletions

View 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");

View 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.
*/