From patchwork Sat Nov 7 15:15:11 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 5192 Return-Path: Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by open-mesh.net (Postfix) with SMTP id 71CB0154415 for ; Sat, 7 Nov 2009 15:55:19 +0000 (UTC) Received: (qmail invoked by alias); 07 Nov 2009 15:15:06 -0000 Received: from unknown (EHLO localhost) [89.246.200.129] by mail.gmx.net (mp069) with SMTP; 07 Nov 2009 16:15:06 +0100 X-Authenticated: #15668376 X-Provags-ID: V01U2FsdGVkX1+zK6M3odazy26+dTIo8Z90HiK2Rha1FZHfYIod5T s+ZeCqXsxhRmDZ From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.net Date: Sat, 7 Nov 2009 16:15:11 +0100 Message-Id: <1257606911-24933-1-git-send-email-sven.eckelmann@gmx.de> X-Mailer: git-send-email 1.6.5.2 X-Y-GMX-Trusted: 0 X-FuHaFi: 0.54 Subject: [B.A.T.M.A.N.] [PATCH] [batctl] Prevent division by zero when no packets were send X-BeenThere: b.a.t.m.a.n@lists.open-mesh.net X-Mailman-Version: 2.1.11 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 07 Nov 2009 15:55:19 -0000 We will try to print a statistic even when we receive a sigterm or sigint before we send any data or started to count the send data. This means that the we try to calculate the data loss by dividing through zero as it is the number of send packets. This is not possible and we must check for that corner case. Signed-off-by: Sven Eckelmann --- batctl/ping.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/batctl/ping.c b/batctl/ping.c index 684a84d..ca1fbf0 100644 --- a/batctl/ping.c +++ b/batctl/ping.c @@ -72,7 +72,7 @@ int ping(int argc, char **argv) fd_set read_socket; int ret = EXIT_FAILURE, ping_fd = 0, res, optchar, found_args = 1; int loop_count = -1, loop_interval = 1, timeout = 1; - unsigned int seq_counter = 0, packets_out = 0, packets_in = 0; + unsigned int seq_counter = 0, packets_out = 0, packets_in = 0, packets_loss; char *dst_string, *mac_string; double time_delta; float min = 0.0, max = 0.0, avg = 0.0; @@ -237,9 +237,14 @@ sleep: } + if (packets_out == 0) + packets_loss = 0; + else + packets_loss = ((packets_out - packets_in) * 100) / packets_out; + printf("--- %s ping statistics ---\n", dst_string); printf("%d packets transmitted, %d received, %d%% packet loss\n", - packets_out, packets_in, (((packets_out - packets_in) * 100) / packets_out)); + packets_out, packets_in, packets_loss); printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n", min, (packets_in ? (avg / packets_in) : 0.000), max, (max - min));