From patchwork Tue Jan 4 09:11:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 18447 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 76C0E84139; Tue, 4 Jan 2022 10:11:40 +0100 (CET) Received: from dvalin.narfation.org (dvalin.narfation.org [213.160.73.56]) by diktynna.open-mesh.org (Postfix) with ESMTPS id B5EF183F18 for ; Tue, 4 Jan 2022 10:11:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1641287497; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/h9aYrIDWmWehSxkrwZ9T2TzZC+/hjyR8an/CxYNq6c=; b=Y0vdVeG0sb3EijyfwiuSn9ZDl9e4fHVuGimdwh1SDNOSoN149WG6EFnobK3A98oerdEt2r 0ldiYu8170frJqzy7z1RTOuMzzC5XA/NiQrk571POn8r1LCheNx///AwScU0b0FWzHzFEf 0DK6dE+sCCN3IHpgdwLDdVKszoSDoWU= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Cc: Sven Eckelmann Subject: [PATCH 2/2] alfred: Simplify calculation of fixed size IPC TLV length Date: Tue, 4 Jan 2022 10:11:03 +0100 Message-Id: <20220104091103.162365-2-sven@narfation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220104091103.162365-1-sven@narfation.org> References: <20220104091103.162365-1-sven@narfation.org> MIME-Version: 1.0 ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=Y0vdVeG0; dmarc=pass (policy=none) header.from=narfation.org; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 213.160.73.56 as permitted sender) smtp.mailfrom=sven@narfation.org ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1641287497; a=rsa-sha256; cv=none; b=aKgws+Dq3frwRJiJmA6WQDJWtnNFUcXJT5Z7Ol3tvebD7BPV/sdpCFni6Z1CUIn4I7mwkt 8qBrapWy+7q3IKSMVamUHenDE69cJvu2+KL2JB5jS05RCmJc7FBaJ+d5V5J+AeG6Va9IZ8 4X3KGEGy2EQk2EQryGfL2wrUy+hgN1g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1641287497; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/h9aYrIDWmWehSxkrwZ9T2TzZC+/hjyR8an/CxYNq6c=; b=Dveme+Z3TBiCxapQt6D7e7dcUEnWAHS1MCl9t+UVmKbMC/fvf6GvJ5P+6RlMT/wMuE5Cye UtIiRIdAFgeYiSYvQW54no7SlQGJ/WCmNoocZnRxd9T35aLrbcDI/RNcKuTrpr6xcXLz1e ddkJd485TlA2AWE3OT4dW9QlztY58uM= Message-ID-Hash: MB4QLXBB3AJGUYJQOZRSX66GRFT7V2KQ X-Message-ID-Hash: MB4QLXBB3AJGUYJQOZRSX66GRFT7V2KQ X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Instead of copying the same code to calculate the length of fixed size TLVs, just use a common macro. Signed-off-by: Sven Eckelmann Acked-by: Marek Lindner --- 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';