* [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands
@ 2022-01-04 9:11 Sven Eckelmann
2022-01-04 9:11 ` [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length Sven Eckelmann
2022-01-12 16:07 ` [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Marek Lindner
0 siblings, 2 replies; 4+ messages in thread
From: Sven Eckelmann @ 2022-01-04 9:11 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: Sven Eckelmann
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 <sven@narfation.org>
---
client.c | 83 ++++++++++++++++++++++++++------------------------------
1 file changed, 38 insertions(+), 45 deletions(-)
diff --git a/client.c b/client.c
index e1107bf..b5faf3b 100644
--- a/client.c
+++ b/client.c
@@ -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));
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length
2022-01-04 9:11 [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Sven Eckelmann
@ 2022-01-04 9:11 ` Sven Eckelmann
2022-01-12 16:11 ` Marek Lindner
2022-01-12 16:07 ` [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Marek Lindner
1 sibling, 1 reply; 4+ messages in thread
From: Sven Eckelmann @ 2022-01-04 9:11 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: Sven Eckelmann
Instead of copying the same code to calculate the length of fixed size
TLVs, just use a common macro.
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
alfred.h | 3 +++
client.c | 9 ++++-----
2 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/alfred.h b/alfred.h
index 26eb157..0e4dd26 100644
--- a/alfred.h
+++ b/alfred.h
@@ -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,
diff --git a/client.c b/client.c
index b5faf3b..d0d19fb 100644
--- a/client.c
+++ b/client.c
@@ -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';
--
2.30.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands
2022-01-04 9:11 [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Sven Eckelmann
2022-01-04 9:11 ` [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length Sven Eckelmann
@ 2022-01-12 16:07 ` Marek Lindner
1 sibling, 0 replies; 4+ messages in thread
From: Marek Lindner @ 2022-01-12 16:07 UTC (permalink / raw)
To: The list for a Better Approach To Mobile Ad-hoc Networking
[-- Attachment #1: Type: text/plain, Size: 498 bytes --]
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 <sven@narfation.org>
Acked-by: Marek Lindner <mareklindner@neomailbox.ch>
Cheers,
Marek
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length
2022-01-04 9:11 ` [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length Sven Eckelmann
@ 2022-01-12 16:11 ` Marek Lindner
0 siblings, 0 replies; 4+ messages in thread
From: Marek Lindner @ 2022-01-12 16:11 UTC (permalink / raw)
To: The list for a Better Approach To Mobile Ad-hoc Networking
[-- Attachment #1: Type: text/plain, Size: 293 bytes --]
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 <sven@narfation.org>
Acked-by: Marek Lindner <mareklindner@neomailbox.ch>
Cheers,
Marek
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-01-12 16:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-04 9:11 [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Sven Eckelmann
2022-01-04 9:11 ` [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length Sven Eckelmann
2022-01-12 16:11 ` Marek Lindner
2022-01-12 16:07 ` [PATCH 1/2] alfred: Avoid large send buffer for fixed size IPC commands Marek Lindner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).