OpenVPN: track interface disconnect

Setup disconnection callback and use it to properly track management
interface availability.

Change-Id: I11b8bc71c1e9cbbf992ee3b7c1c9aae6e6f697e0
Requires: libosmo-netif I905adb2d6191216551a3bcdcd1aec1f96f01612a
Related: SYS#2655
diff --git a/src/osysmon_openvpn.c b/src/osysmon_openvpn.c
index 135a532..148e3b8 100644
--- a/src/osysmon_openvpn.c
+++ b/src/osysmon_openvpn.c
@@ -113,12 +113,26 @@
 	return NULL;
 }
 
+static int disconnect_cb(struct osmo_stream_cli *conn)
+{
+	struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
+
+	update_name(vpn->rem_cfg, "management interface unavailable");
+	vpn->connected = false;
+	talloc_free(vpn->tun_ip);
+	talloc_free(vpn->rem_cfg->remote_host);
+	vpn->tun_ip = NULL;
+	vpn->rem_cfg->remote_host = NULL;
+
+	return 0;
+}
+
 static int connect_cb(struct osmo_stream_cli *conn)
 {
 	struct openvpn_client *vpn = osmo_stream_cli_get_data(conn);
 
 	update_name(vpn->rem_cfg, "management interface incompatible");
-	vpn->connected = true; /* FIXME: there's no callback for lost connection to drop this flag */
+	vpn->connected = true;
 
 	return 0;
 }
@@ -174,6 +188,7 @@
 	osmo_stream_cli_set_reconnect_timeout(vpn->mgmt, 60);
 	osmo_stream_cli_set_read_cb(vpn->mgmt, read_cb);
 	osmo_stream_cli_set_connect_cb(vpn->mgmt, connect_cb);
+	osmo_stream_cli_set_disconnect_cb(vpn->mgmt, disconnect_cb);
 
 	if (osmo_stream_cli_open(vpn->mgmt) < 0) {
 		OVPN_LOG(vpn->rem_cfg, vpn, "failed to connect to management interface\n");
@@ -261,7 +276,6 @@
 	if (remote)
 		value_node_add(vn_host, "remote", remote);
 
-	/* FIXME: there's no way to check client state so this might be triggered even while it's reconnecting */
 	if (vpn->connected) { /* re-trigger state command */
 		msgb_printf(msg, "state\n");
 		osmo_stream_cli_send(vpn->mgmt, msg);