bbfdm/scripts/download
2022-10-12 11:04:10 +00:00

190 lines
6.3 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/libubox/jshn.sh
ROOT="$(dirname $0)"
. ${ROOT}/bbf_api
CAPTURE_FILE="/tmp/download_dump"
download_error() {
json_init
json_add_string "Status" "$1"
json_dump
# Store data in dmmap_diagnostics for both protocols (cwmp/usp)
[ "$2" == "both_proto" ] && {
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState="$1"
}
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.Status="complete"
$UCI_COMMIT_BBF_DMMAP
}
download_launch() {
input="$1"
json_load "${input}"
json_get_var url url
json_get_var iface iface
json_get_var dscp dscp
json_get_var eth_prio eth_prio
json_get_var ip_proto ip_proto
json_get_var num_of_con num_of_con
json_get_var enable_per_con enable_per_con
json_get_var proto proto
logger -t "usp" "download" "url=${url} && iface=${iface} && dscp=${dscp} && eth_prio=${eth_prio} && ip_proto=${ip_proto} && num_of_con=${num_of_con} && enable_per_con=${enable_per_con} && proto=${proto}"
# Check if a download process is already running
download_s=$(uci_get_bbf_dmmap dmmap_diagnostics.download)
logger -t "usp" "download" "download_s=$download_s"
if [ -z "${download_s}" ]; then
logger -t "usp" "download" "Create a download diagnostics section"
[ ! -f /etc/bbfdm/dmmap/dmmap_diagnostics ] && touch /etc/bbfdm/dmmap/dmmap_diagnostics
$UCI_ADD_BBF_DMMAP dmmap_diagnostics download
$UCI_RENAME_BBF_DMMAP dmmap_diagnostics.@download[0]='download'
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.Status="running"
$UCI_COMMIT_BBF_DMMAP
else
logger -t "usp" "download" "there is a download diagnostics section"
Status=$(uci_get_bbf_dmmap dmmap_diagnostics.download.Status)
logger -t "usp" "download" "Status=$Status"
[ "${Status}" == "running" ] && {
return
}
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.Status="running"
$UCI_COMMIT_BBF_DMMAP
fi
# Assign default value
[ -n "${iface}" ] && device=$(ifstatus "${iface}" | jsonfilter -e @.device) || device=$(route -n | grep 'UG[ \t]' | awk '{print $8}')
ip_addr_used=$(get_ip_addr_used "${ip_proto}" "${iface}")
if [ "$ip_proto" = "IPv4" ]; then ip_proto="--ipv4"; elif [ "$ip_proto" = "IPv6" ]; then ip_proto="--ipv6"; else ip_proto=""; fi
# Fail if url is empty
[ -z "${url}" ] && {
download_error "Error_InitConnectionFailed" "${proto}"
return
}
# 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; }
FREE_SIZE=`free | grep Mem | awk '{print $4}'`
FREE_SIZE_BYTE=$((${FREE_SIZE}/1000))
logger -t "usp" "download" "tcpdump -i $device -C ${FREE_SIZE_BYTE} tcp -w ${CAPTURE_FILE}"
tcpdump -i "$device" -C ${FREE_SIZE_BYTE} 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)
logger -t "usp" "download" "curl $ip_proto --fail --silent -o /dev/null $url"
curl $ip_proto --fail --silent -o /dev/null "$url"
error_code="$?"
time2=$(date +%s)
logger -t "usp" "download" "error_code=$error_code"
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" ] && {
download_error "Error_CannotResolveHostName" "${proto}"
kill $PID 2> /dev/null
return
}
[ "$error_code" = "7" ] && {
download_error "Error_InitConnectionFailed" "${proto}"
kill $PID 2> /dev/null
return
}
[ "$error_code" = "22" ] && {
download_error "Error_NoResponse" "${proto}"
kill $PID 2> /dev/null
return
}
[ "$error_code" = "27" ] && {
download_error "Error_IncorrectSize" "${proto}"
kill $PID 2> /dev/null
return
}
[ "$error_code" = "28" ] && {
download_error "Error_Timeout" "${proto}"
kill $PID 2> /dev/null
return
}
[ "$error_code" != "0" ] && {
download_error "Error_Other" "${proto}"
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))
json_init
json_add_string "Status" "Complete"
json_add_string "IPAddressUsed" "${ip_addr_used}"
json_add_int "TotalBytesReceived" "${rx_bytes}"
json_add_int "TotalBytesSent" "${tx_bytes}"
json_add_int "PeriodOfFullLoading" "${periodtime}"
if [ "$enable_per_con" = "true" ] || [ "$enable_per_con" = "1" ]; then
json_add_array "DownloadPerConnection"
json_add_object ""
json_add_int "TotalBytesReceived" "${rx_bytes}"
json_add_int "TotalBytesSent" "${tx_bytes}"
json_close_object
fi
json_dump
# Store data in dmmap_diagnostics for both protocols (cwmp/usp)
[ "${proto}" == "both_proto" ] && {
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.DiagnosticState="Complete"
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.IPAddressUsed="${ip_addr_used}"
$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 [ "$enable_per_con" = "true" ] || [ "$enable_per_con" = "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
}
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; }
$UCI_SET_BBF_DMMAP dmmap_diagnostics.download.Status="complete"
$UCI_COMMIT_BBF_DMMAP
}
if [ -n "$1" ]; then
download_launch "$1"
else
download_error "Error_Internal"
fi