alfred: Avoid large send buffer for fixed size IPC commands
PATCH 1/2 alfred: Avoid large send buffer for fixed size IPC commands
For data related IPC commands, a buffer of 65527 bytes is necessary to send or receive a complete command. But for non-data related IPC commands usually have a fixed size. It is therefore enough to allocate exactly the minimum required amount of bytes on the stack for non-data related IPC commands.
--- client.c | 83 ++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 45 deletions(-)
client.c +38 -45
@@ -23,7 +23,7 @@int alfred_client_request_data (struct globals *globals) {unsigned char buf[MAX_PAYLOAD ], *pos; -struct alfred_request_v0 *request; +struct alfred_request_v0 request;struct alfred_push_data_v0 *push;struct alfred_status_v0 *status;struct alfred_tlv *tlv;
@@ -34,17 +34,16 @@ int alfred_client_request_data(struct globals *globals)if (unix_sock_open_client (globals))return -1 ; - request = (struct alfred_request_v0 *)buf; - len =sizeof (*request); + len =sizeof (request); - request->header.type =ALFRED_REQUEST ; - request->header.version =ALFRED_VERSION ; - request->header.length =sizeof (*request) -sizeof (request->header); - request->header.length =htons (request->header.length); - request->requested_type = globals->clientmode_arg; - request->tx_id =get_random_id (); + request.header.type =ALFRED_REQUEST ; + request.header.version =ALFRED_VERSION ; + request.header.length =sizeof (request) -sizeof (request.header); + request.header.length =htons (request.header.length); + request.requested_type = globals->clientmode_arg; + request.tx_id =get_random_id (); - ret =write (globals->unix_sock, buf, len); + ret =write (globals->unix_sock, &request, len);if (ret != len)fprintf (stderr,"%s: only wrote %d of %d bytes: %s\n" , __func__, ret, len,strerror (errno));
@@ -179,26 +178,24 @@ int alfred_client_set_data(struct globals *globals)int alfred_client_modeswitch (struct globals *globals) { -unsigned char buf[MAX_PAYLOAD ]; -struct alfred_modeswitch_v0 *modeswitch; +struct alfred_modeswitch_v0 modeswitch;int ret, len;if (unix_sock_open_client (globals))return -1 ; - modeswitch = (struct alfred_modeswitch_v0 *)buf; - len =sizeof (*modeswitch); + len =sizeof (modeswitch); - modeswitch->header.type =ALFRED_MODESWITCH ; - modeswitch->header.version =ALFRED_VERSION ; - modeswitch->header.length =htons (len -sizeof (modeswitch->header)); + modeswitch.header.type =ALFRED_MODESWITCH ; + modeswitch.header.version =ALFRED_VERSION ; + modeswitch.header.length =htons (len -sizeof (modeswitch.header));switch (globals->opmode) {case OPMODE_SECONDARY : - modeswitch->mode =ALFRED_MODESWITCH_SECONDARY ; + modeswitch.mode =ALFRED_MODESWITCH_SECONDARY ;break ;case OPMODE_PRIMARY : - modeswitch->mode =ALFRED_MODESWITCH_PRIMARY ; + modeswitch.mode =ALFRED_MODESWITCH_PRIMARY ;break ;default : fprintf(stderr, "%s: unknown opmode %u in modeswitch\n",
@@ -206,7 +203,7 @@ int alfred_client_modeswitch(struct globals *globals)return -1 ; } - ret =write (globals->unix_sock, buf, len); + ret =write (globals->unix_sock, &modeswitch, len);if (ret != len)fprintf (stderr,"%s: only wrote %d of %d bytes: %s\n" , __func__, ret, len,strerror (errno));
@@ -248,8 +245,7 @@ static int check_interface(const char *iface)int alfred_client_change_interface (struct globals *globals) { -unsigned char buf[MAX_PAYLOAD ]; -struct alfred_change_interface_v0 *change_interface; +struct alfred_change_interface_v0 change_interface;int ret, len;char *input, *token, *saveptr;size_t interface_len;
@@ -258,24 +254,23 @@ int alfred_client_change_interface(struct globals *globals)return -1 ; interface_len =strlen (globals->change_interface); -if (interface_len >sizeof (change_interface->ifaces)) { +if (interface_len >sizeof (change_interface.ifaces)) {fprintf (stderr,"%s: interface name list too long, not changing\n" , __func__);return 0 ; } - change_interface = (struct alfred_change_interface_v0 *)buf; - len =sizeof (*change_interface); + len =sizeof (change_interface); - change_interface->header.type =ALFRED_CHANGE_INTERFACE ; - change_interface->header.version =ALFRED_VERSION ; - change_interface->header.length =htons (len -sizeof (change_interface->header)); -strncpy (change_interface->ifaces, globals->change_interface, -sizeof (change_interface->ifaces)); - change_interface->ifaces[sizeof (change_interface->ifaces) -1 ] ='\0' ; + change_interface.header.type =ALFRED_CHANGE_INTERFACE ; + change_interface.header.version =ALFRED_VERSION ; + change_interface.header.length =htons (len -sizeof (change_interface.header)); +strncpy (change_interface.ifaces, globals->change_interface, +sizeof (change_interface.ifaces)); + change_interface.ifaces[sizeof (change_interface.ifaces) -1 ] ='\0' ;/* test it before sending -* globals->change_interface is now saved in change_interface->ifaces +* globals->change_interface is now saved in change_interface.ifaces * and can be modified by strtok_r */ input = globals->change_interface;
@@ -287,7 +282,7 @@ int alfred_client_change_interface(struct globals *globals)return 0 ; } - ret =write (globals->unix_sock, buf, len); + ret =write (globals->unix_sock, &change_interface, len);if (ret != len)fprintf (stderr,"%s: only wrote %d of %d bytes: %s\n" , __func__, ret, len,strerror (errno));
@@ -299,8 +294,7 @@ int alfred_client_change_interface(struct globals *globals)int alfred_client_change_bat_iface (struct globals *globals) { -unsigned char buf[MAX_PAYLOAD ]; -struct alfred_change_bat_iface_v0 *change_bat_iface; +struct alfred_change_bat_iface_v0 change_bat_iface;int ret, len;size_t interface_len;
@@ -308,23 +302,22 @@ int alfred_client_change_bat_iface(struct globals *globals)return -1 ; interface_len =strlen (globals->mesh_iface); -if (interface_len >sizeof (change_bat_iface->bat_iface)) { +if (interface_len >sizeof (change_bat_iface.bat_iface)) {fprintf (stderr,"%s: batman-adv interface name list too long, not changing\n" , __func__);return 0 ; } - change_bat_iface = (struct alfred_change_bat_iface_v0 *)buf; - len =sizeof (*change_bat_iface); + len =sizeof (change_bat_iface); - change_bat_iface->header.type =ALFRED_CHANGE_BAT_IFACE ; - change_bat_iface->header.version =ALFRED_VERSION ; - change_bat_iface->header.length =htons (len -sizeof (change_bat_iface->header)); -strncpy (change_bat_iface->bat_iface, globals->mesh_iface, -sizeof (change_bat_iface->bat_iface)); - change_bat_iface->bat_iface[sizeof (change_bat_iface->bat_iface) -1 ] ='\0' ; + change_bat_iface.header.type =ALFRED_CHANGE_BAT_IFACE ; + change_bat_iface.header.version =ALFRED_VERSION ; + change_bat_iface.header.length =htons (len -sizeof (change_bat_iface.header)); +strncpy (change_bat_iface.bat_iface, globals->mesh_iface, +sizeof (change_bat_iface.bat_iface)); + change_bat_iface.bat_iface[sizeof (change_bat_iface.bat_iface) -1 ] ='\0' ; - ret =write (globals->unix_sock, buf, len); + ret =write (globals->unix_sock, &change_bat_iface, len);if (ret != len)fprintf (stderr,"%s: only wrote %d of %d bytes: %s\n" , __func__, ret, len,strerror (errno));
Signature · 1 line
2.30.2
PATCH 2/2 alfred: Simplify calculation of fixed size IPC TLV length
Instead of copying the same code to calculate the length of fixed size TLVs, just use a common macro.
--- alfred.h | 3 +++ client.c | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-)
alfred.h +3 -0
@@ -30,6 +30,9 @@#define ALFRED_SOCK_PATH_DEFAULT "/var/run/alfred.sock"#define NO_FILTER -1 +#define FIXED_TLV_LEN (__tlv_type) \ + htons(sizeof(__tlv_type) - sizeof(__tlv_type.header)) +enum data_source {SOURCE_LOCAL =0 ,SOURCE_FIRST_HAND =1 ,
client.c +4 -5
@@ -38,8 +38,7 @@ int alfred_client_request_data(struct globals *globals) request.header.type =ALFRED_REQUEST ; request.header.version =ALFRED_VERSION ; - request.header.length =sizeof (request) -sizeof (request.header); - request.header.length =htons (request.header.length); + request.header.length =FIXED_TLV_LEN (request); request.requested_type = globals->clientmode_arg; request.tx_id =get_random_id ();
@@ -188,7 +187,7 @@ int alfred_client_modeswitch(struct globals *globals) modeswitch.header.type =ALFRED_MODESWITCH ; modeswitch.header.version =ALFRED_VERSION ; - modeswitch.header.length =htons (len -sizeof (modeswitch.header)); + modeswitch.header.length =FIXED_TLV_LEN (modeswitch);switch (globals->opmode) {case OPMODE_SECONDARY :
@@ -264,7 +263,7 @@ int alfred_client_change_interface(struct globals *globals) change_interface.header.type =ALFRED_CHANGE_INTERFACE ; change_interface.header.version =ALFRED_VERSION ; - change_interface.header.length =htons (len -sizeof (change_interface.header)); + change_interface.header.length =FIXED_TLV_LEN (change_interface);strncpy (change_interface.ifaces, globals->change_interface,sizeof (change_interface.ifaces)); change_interface.ifaces[sizeof (change_interface.ifaces) -1 ] ='\0' ;
@@ -312,7 +311,7 @@ int alfred_client_change_bat_iface(struct globals *globals) change_bat_iface.header.type =ALFRED_CHANGE_BAT_IFACE ; change_bat_iface.header.version =ALFRED_VERSION ; - change_bat_iface.header.length =htons (len -sizeof (change_bat_iface.header)); + change_bat_iface.header.length =FIXED_TLV_LEN (change_bat_iface);strncpy (change_bat_iface.bat_iface, globals->mesh_iface,sizeof (change_bat_iface.bat_iface)); change_bat_iface.bat_iface[sizeof (change_bat_iface.bat_iface) -1 ] ='\0' ;
Signature · 1 line
2.30.2
Re: [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length
Quoted 4 lines · On Tuesday, 4 January 2022 10:11:03 CET Sven Eckelmann wrote:
Instead of copying the same code to calculate the length of fixed size TLVs, just use a common macro.
Signed-off-by Sven Eckelmann <[email protected]>
Cheers,
Marek
Quoted 7 lines · On Tuesday, 4 January 2022 10:11:02 CET Sven Eckelmann wrote:
For data related IPC commands, a buffer of 65527 bytes is necessary to send or receive a complete command. But for non-data related IPC commands usually have a fixed size. It is therefore enough to allocate exactly the minimum required amount of bytes on the stack for non-data related IPC commands.
Signed-off-by Sven Eckelmann <[email protected]>
Cheers,
Marek