bbfdm/scripts/download_launch
Amin Ben Ramdhane 080f7b4f85 Ticket refs #7333: fix warnings, code analysis and clean up the code
- Fix all errors catched by cppcheck threadsafety (cppcheck --error-exitcode=0 --addon=threadsafety bbf)
 - Fix some errors catched by cppcheck cert (cppcheck --error-exitcode=0 --addon=cert bbf)
 - Add new str-protected macros instead of using str functions directly to avoid crashes
2022-02-16 10:33:59 +00:00

91 lines
4.5 KiB
Bash
Executable file

#!/bin/sh
# Copyright (C) 2022 iopsys Software Solutions AB
# Author: IMEN Bhiri <imen.bhiri@pivasoftware.com>
# Author: AMIN Ben Ramdhane <amin.benramdhane@pivasoftware.com>
. /usr/share/bbfdm/bbf_uci_api
CAPTURE_FILE="/tmp/download_dump"
DOWNLOAD_DIAGNOSTIC_FILE="/tmp/bbfdm_download_diagnostic"
CONNECTION_TIMEOUT=60
download_launch() {
url=$1
device=$2
[ "$url" = "" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_InitConnectionFailed; $UCI_COMMIT_BBF_DMMAP; return; }
protocol=$(uci_get_bbf_dmmap dmmap_diagnostics.download.ProtocolVersion Any)
if [ "$protocol" = "IPv4" ]; then proto="--ipv4"; elif [ "$protocol" = "IPv6" ]; then proto="--ipv6"; else proto=""; fi
# Disable acceleration on Broadcom devices to capture all packets with tcpdump
[ -e /usr/sbin/fcctl ] && { fcctl disable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
tcpdump -i "$device" tcp -w ${CAPTURE_FILE} > /dev/null 2>&1 &
PID=$!
sleep 1
if [ ${url:0:7} = http:// -o ${url:0:6} = ftp:// ]; then
tx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
rx_bytes_before=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes)
time1=$(date +%s)
curl $proto --fail --silent --connect-timeout ${CONNECTION_TIMEOUT} -o ${DOWNLOAD_DIAGNOSTIC_FILE} "$url"
error_code="$?"
time2=$(date +%s)
tx_bytes_after=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes)
rx_bytes_after=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes)
[ "$error_code" = "6" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_CannotResolveHostName; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "7" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_InitConnectionFailed; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "22" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_NoResponse; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "27" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_IncorrectSize; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" = "28" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_Timeout; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
[ "$error_code" != "0" ] && { $UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Error_Other; $UCI_COMMIT_BBF_DMMAP; kill $PID 2> /dev/null; return; }
fi
tx_bytes=$((tx_bytes_after-tx_bytes_before))
rx_bytes=$((rx_bytes_after-rx_bytes_before))
periodtime=$(($((time2-time1))*1000000))
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=Complete
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.TotalBytesReceived=$rx_bytes
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.TotalBytesSent=$tx_bytes
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.PeriodOfFullLoading=$periodtime
perconnection=$($UCI_GET_BBF_DMMAP dmmap_diagnostics.download.EnablePerConnection)
if [ "$perconnection" = "true" ] || [ "$perconnection" = "1" ]; then
$UCI_ADD_BBF_DMMAP dmmap_diagnostics DownloadPerConnection
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@DownloadPerConnection[0].TotalBytesReceived=$rx_bytes
$UCI_SET_BBF_DMMAP dmmap_diagnostics.@DownloadPerConnection[0].TotalBytesSent=$tx_bytes
else
$UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@DownloadPerConnection[0]
fi
$UCI_COMMIT_BBF_DMMAP
rm ${DOWNLOAD_DIAGNOSTIC_FILE} 2>/dev/null
sleep 1
kill $PID >/dev/null 2>&1
# Enable acceleration on Broadcom devices after killing the tcpdump pid
[ -e /usr/sbin/fcctl ] && { fcctl enable >/dev/null 2>&1; fcctl flush >/dev/null 2>&1; }
}
download_stop_diagnostic() {
$UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@DownloadPerConnection[0]
pids=$(pgrep -f download_launch.*run)
if [ -n "$pids" ]; then
kill -9 "$pids" >/dev/null 2>&1
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=None
fi
pids=$(pgrep -f download_launch.*run)
if [ -n "$pids" ]; then
kids=$(grep -l "PPid.*$pids" /proc/*/task/*/status | grep -o "[0-9]*")
for kid in $kids; do
kill -9 "$kid" >/dev/null 2>&1
done
kill -9 "$pids" >/dev/null 2>&1
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState=None
fi
$UCI_COMMIT_BBF_DMMAP
}
if [ "$1" = "run" ] ; then
download_launch "$2" "$3"
elif [ "$1" = "stop" ]; then
download_stop_diagnostic
else
return
fi