Skip to contentSkip to conversation

alfred: introduce 'server status' IPC call

PATCH Superseded Marek Lindner 2 participants 2 messages Feb 9, 2022 – Feb 20, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 alfred: introduce 'server status' IPC call

The 'server status' call exports the 'mode' as well as interface status via IPC. Both parameters can be modified at runtime via the IPC and as such, the current configuration is dynamic and not necessarily obvious.

The server status 'request' and 'reply' packet types are added to allow the IPC client to initiate the status retrieval. The server will respond with the 'reply' message.

The information is encapsulated in TLV data chunks allowing for future backward compatible extension of the server status call.

---
v3:
- close client socket at the end of unix_sock_server_status()

 alfred.h     |   2 +
 client.c     | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++
 main.c       |   9 +++-
 man/alfred.8 |   3 ++
 packet.h     |  65 +++++++++++++++++++++++++++++
 unix_sock.c  |  90 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 284 insertions(+), 1 deletion(-)
alfred.h +2 -0
index 9ab92a2..3d1d72a 100644
@@ -93,6 +93,7 @@ enum clientmode {
 	CLIENT_MODESWITCH,
 	CLIENT_CHANGE_INTERFACE,
 	CLIENT_CHANGE_BAT_IFACE,
+	CLIENT_SERVER_STATUS,
 };
 
 struct interface {
@@ -155,6 +156,7 @@ int alfred_client_set_data(struct globals *globals);
 int alfred_client_modeswitch(struct globals *globals);
 int alfred_client_change_interface(struct globals *globals);
 int alfred_client_change_bat_iface(struct globals *globals);
+int alfred_client_server_status(struct globals *globals);
 /* recv.c */
 int recv_alfred_packet(struct globals *globals, struct interface *interface,
 		       int recv_sock);
client.c +116 -0

119 lines not on this page · raw message

main.c +8 -1
index d40a0cc..68d6efd 100644
@@ -38,6 +38,7 @@ static void alfred_usage(void)
 	printf("                   secondary          switch daemon to mode secondary\n");
 	printf("  -I, --change-interface [interface]  change to the specified interface(s)\n");
 	printf("  -B, --change-bat-iface [interface]  change to the specified batman-adv interface\n");
+	printf("  -S, --server-status                 request server status info such as mode & interfaces\n");
 	printf("\n");
 	printf("server mode options:\n");
 	printf("  -i, --interface                     specify the interface (or comma separated list of interfaces) to listen on\n");
@@ -162,6 +163,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 		{"modeswitch",		required_argument,	NULL,	'M'},
 		{"change-interface",	required_argument,	NULL,	'I'},
 		{"change-bat-iface",	required_argument,	NULL,	'B'},
+		{"server-status",	required_argument,	NULL,	'S'},
 		{"unix-path",		required_argument,	NULL,	'u'},
 		{"update-command",	required_argument,	NULL,	'c'},
 		{"version",		no_argument,		NULL,	'v'},
@@ -196,7 +198,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 
 	time_random_seed();
 
-	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:B:u:dc:p:4:f", long_options,
+	while ((opt = getopt_long(argc, argv, "ms:r:hi:b:vV:M:I:B:Su:dc:p:4:f", long_options,
 				  &opt_ind)) != -1) {
 		switch (opt) {
 		case 'r':
@@ -258,6 +260,9 @@ static struct globals *alfred_init(int argc, char *argv[])
 			globals->clientmode = CLIENT_CHANGE_BAT_IFACE;
 			globals->mesh_iface = strdup(optarg);
 			break;
+		case 'S':
+			globals->clientmode = CLIENT_SERVER_STATUS;
+			break;
 		case 'u':
 			globals->unix_path = optarg;
 			break;
@@ -321,6 +326,8 @@ int main(int argc, char *argv[])
 		return alfred_client_change_interface(globals);
 	case CLIENT_CHANGE_BAT_IFACE:
 		return alfred_client_change_bat_iface(globals);
+	case CLIENT_SERVER_STATUS:
+		return alfred_client_server_status(globals);
 	}
 
 	return 0;
man/alfred.8 +3 -0
index 74814e0..cf0eafc 100644
@@ -94,6 +94,9 @@ Change the alfred server to use the new \fBinterface\fP(s)
 .TP
 \fB\-B\fP, \fB\-\-change\-bat\-iface\fP \fIinterface\fP
 Change the alfred server to use the new \fBbatman-adv interface\fP
+.TP
+\fB\-S\fP, \fB\-\-server\-status\fP
+Request server status information such as mode & interfaces\fP
 .
 .SH SERVER OPTIONS
 .TP
packet.h +65 -0

77 lines not on this page · raw message

unix_sock.c +90 -0

102 lines not on this page · raw message

Signature · 1 line
2.32.0.rc0
#1 lore raw
Quoted 9 lines · On Wednesday, 9 February 2022 17:43:54 CET Marek Lindner wrote:

The 'server status' call exports the 'mode' as well as interface status via IPC. Both parameters can be modified at runtime via the IPC and as such, the current configuration is dynamic and not necessarily obvious.

The server status 'request' and 'reply' packet types are added to allow the IPC client to initiate the status retrieval. The server will respond with the 'reply' message.

Quoted 7 lines

The information is encapsulated in TLV data chunks allowing for future backward compatible extension of the server status call.

---
v3:
- close client socket at the end of unix_sock_server_status()

Things I've noticed are:

* there are no safety checks for the buffer size in the server code
* the packet/tlv type definitions don't include the header (unlike the rest of the code)
* version checks for the "sub tlv" types is missing

Attached is a rough sketch of potential changes.

Kind regards,
Sven

client.c +11 -9
index 41ee640..6a58196 100644
@@ -328,9 +328,9 @@ int alfred_client_change_bat_iface(struct globals *globals)
 
 int alfred_client_server_status(struct globals *globals)
 {
-	struct alfred_server_status_net_iface *status_net_iface;
-	struct alfred_server_status_bat_iface *status_bat_iface;
-	struct alfred_server_status_op_mode *status_op_mode;
+	struct alfred_server_status_net_iface_v0 *status_net_iface;
+	struct alfred_server_status_bat_iface_v0 *status_bat_iface;
+	struct alfred_server_status_op_mode_v0 *status_op_mode;
 	struct alfred_server_status_req_v0 status_req;
 	struct alfred_server_status_rep_v0 *status_rep;
 	int ret, tlvsize, headsize, len, consumed;
@@ -390,9 +390,11 @@ int alfred_client_server_status(struct globals *globals)
 			break;
 
 		status_tlv = (struct alfred_tlv *)(buf + consumed);
-		consumed += sizeof(*status_tlv);
+		if (status_tlv->version != ALFRED_VERSION)
+			break;
 
 		tlvsize = ntohs(status_tlv->length);
+		tlvsize += sizeof(*status_tlv);
 		if (len - consumed < tlvsize)
 			break;
 
@@ -401,7 +403,7 @@ int alfred_client_server_status(struct globals *globals)
 			if (tlvsize != sizeof(*status_op_mode))
 				break;
 
-			status_op_mode = (struct alfred_server_status_op_mode *)(buf + consumed);
+			status_op_mode = (struct alfred_server_status_op_mode_v0 *)(buf + consumed);
 
 			switch (status_op_mode->mode) {
 			case ALFRED_MODESWITCH_SECONDARY:
@@ -417,19 +419,19 @@ int alfred_client_server_status(struct globals *globals)
 
 			break;
 		case ALFRED_SERVER_NET_IFACE:
-			if (tlvsize != sizeof(struct alfred_server_status_net_iface))
+			if (tlvsize != sizeof(*status_net_iface))
 				break;
 
-			status_net_iface = (struct alfred_server_status_net_iface *)(buf + consumed);
+			status_net_iface = (struct alfred_server_status_net_iface_v0 *)(buf + consumed);
 			fprintf(stderr, "- interface: %s\n", status_net_iface->net_iface);
 			fprintf(stderr, "\t- status: %s\n",
 				status_net_iface->active == 1 ? "active" : "inactive");
 			break;
 		case ALFRED_SERVER_BAT_IFACE:
-			if (tlvsize != sizeof(struct alfred_server_status_bat_iface))
+			if (tlvsize != sizeof(*status_bat_iface))
 				break;
 
-			status_bat_iface = (struct alfred_server_status_bat_iface *)(buf + consumed);
+			status_bat_iface = (struct alfred_server_status_bat_iface_v0 *)(buf + consumed);
 			fprintf(stdout, "- batman-adv interface: %s\n", status_bat_iface->bat_iface);
 			break;
 		}
packet.h +9 -6
index 4b6bde5..0c1a2eb 100644
@@ -185,34 +185,37 @@ struct alfred_server_status_req_v0 {
 } __packed;
 
 /**
- * struct alfred_server_status_op_mode - server op mode status information
+ * struct alfred_server_status_op_mode_v0 - server op mode status information
  * @op_mode: active op mode
  *
  * Sent to the client by daemon in response to status request
  */
-struct alfred_server_status_op_mode {
+struct alfred_server_status_op_mode_v0 {
+	struct alfred_tlv header;
 	uint8_t mode;
 } __packed;
 
 /**
- * struct alfred_server_status_net_iface - server net iface status information
+ * struct alfred_server_status_net_iface_v0 - server net iface status information
  * @net_iface: configured network interface
  * @active: network interface active/inactive status info
  *
  * Sent to the client by daemon in response to status request
  */
-struct alfred_server_status_net_iface {
+struct alfred_server_status_net_iface_v0 {
+	struct alfred_tlv header;
 	char net_iface[IFNAMSIZ];
 	uint8_t active;
 } __packed;
 
 /**
- * struct alfred_server_status_bat_iface - server bat iface status information
+ * struct alfred_server_status_bat_iface_v0 - server bat iface status information
  * @op_mode: configured batman interface
  *
  * Sent to the client by daemon in response to status request
  */
-struct alfred_server_status_bat_iface {
+struct alfred_server_status_bat_iface_v0 {
+	struct alfred_tlv header;
 	char bat_iface[IFNAMSIZ];
 } __packed;
 
unix_sock.c +40 -22

98 lines not on this page · raw message