#!/bin/sh # Copyright (C) 2022 iopsys Software Solutions AB # Author: IMEN Bhiri # Author: AMIN Ben Ramdhane . /usr/share/libubox/jshn.sh ROOT="$(dirname $0)" . ${ROOT}/bbf_api CAPTURE_FILE="/tmp/upload_dump" upload_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.upload.DiagnosticState="$1" } $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.Status="complete" $UCI_COMMIT_BBF_DMMAP } upload_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 file_length file_length 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 # Check if an upload process is already running upload_s=$(uci_get_bbf_dmmap dmmap_diagnostics.upload) if [ -z "${upload_s}" ]; then [ ! -f /etc/bbfdm/dmmap/dmmap_diagnostics ] && touch /etc/bbfdm/dmmap/dmmap_diagnostics $UCI_ADD_BBF_DMMAP dmmap_diagnostics upload $UCI_RENAME_BBF_DMMAP dmmap_diagnostics.@upload[0]='upload' $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.Status="running" $UCI_COMMIT_BBF_DMMAP else Status=$(uci_get_bbf_dmmap dmmap_diagnostics.upload.Status) [ "${Status}" == "running" ] && { return } $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.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}" ] && { upload_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)) 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) dd if=/dev/zero bs="$file_length" count=1 2>/dev/null | curl $ip_proto --fail --silent -T - "$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" ] && { upload_error "Error_CannotResolveHostName" "${proto}" kill $PID 2> /dev/null return } [ "$error_code" = "7" ] && { upload_error "Error_InitConnectionFailed" "${proto}" kill $PID 2> /dev/null return } [ "$error_code" = "22" ] && { upload_error "Error_NoResponse" "${proto}" kill $PID 2> /dev/null return } [ "$error_code" = "27" ] && { upload_error "Error_IncorrectSize" "${proto}" kill $PID 2> /dev/null return } [ "$error_code" = "28" ] && { upload_error "Error_Timeout" "${proto}" kill $PID 2> /dev/null return } [ "$error_code" != "0" ] && { upload_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_string "TestBytesSent" "${file_length}" 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 "UploadPerConnection" json_add_object "" json_add_int "TestBytesSent" "${file_length}" 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.upload.DiagnosticState="Complete" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TestBytesSent="${file_length}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TotalBytesReceived="${rx_bytes}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TotalBytesSent="${tx_bytes}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.PeriodOfFullLoading="${periodtime}" perconnection=$($UCI_GET_BBF_DMMAP dmmap_diagnostics.upload.EnablePerConnection) if [ "$enable_per_con" = "true" ] || [ "$enable_per_con" = "1" ]; then $UCI_ADD_BBF_DMMAP dmmap_diagnostics UploadPerConnection $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TestBytesSent="${file_length}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TotalBytesReceived="${rx_bytes}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TotalBytesSent="${tx_bytes}" else $UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0] fi $UCI_COMMIT_BBF_DMMAP } sleep 3 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.upload.Status="complete" $UCI_COMMIT_BBF_DMMAP } if [ -n "$1" ]; then upload_launch "$1" else upload_error "Error_Internal" fi