#!/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 UPLOAD_TIMEOUT=1800 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 @.l3_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 } [ "${url:0:7}" != "http://" ] && [ "${url:0:6}" != "ftp://" ] && { upload_error "Error_Other" "${proto}" return } format='{ "size_upload":"%{size_upload}", "time_appconnect":"%{time_appconnect}", "time_connect":"%{time_connect}", "time_pretransfer":"%{time_pretransfer}", "time_starttransfer":"%{time_starttransfer}", "time_total":"%{time_total}", "exitcode":"%{exitcode}" }' len_mb=$((file_length/(1000*1000))) if [ $len_mb -gt 0 ]; then bs="1MB" count="$len_mb" else bs="$file_length" count="1" fi tx_bytes_start=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes) rx_bytes_start=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes) time_start=$(date +"%s.282646") # It should be like that time_start=$(date +"%s.%6N") but since OpenWrt busybox has limitations and doesn't support nonoseconds so keep it hardcoded res=$(dd if=/dev/zero bs="${bs}" count=${count} 2>/dev/null | curl ${ip_proto} --fail --silent --max-time ${UPLOAD_TIMEOUT} -T - "${url}" -w "${format}"|grep -wo '{.*}') time_end=$(date +"%s.282646") # It should be like that time_end=$(date +"%s.%6N") but since OpenWrt busybox has limitations and doesn't support nonoseconds so keep it hardcoded tx_bytes_end=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.tx_bytes) rx_bytes_end=$(ubus call network.device status "{'name':'$device'}" | jsonfilter -e @.statistics.rx_bytes) logger -t "bbf_upload" "# ${url} => ${res} #" json_load "${res}" json_get_var size_upload size_upload json_get_var time_appconnect time_appconnect json_get_var time_connect time_connect json_get_var time_pretransfer time_pretransfer json_get_var time_starttransfer time_starttransfer json_get_var time_total time_total json_get_var exitcode exitcode [ "$exitcode" = "6" ] && { upload_error "Error_CannotResolveHostName" "${proto}" return } [ "$exitcode" = "7" ] && { upload_error "Error_InitConnectionFailed" "${proto}" return } [ "$exitcode" = "22" ] && { upload_error "Error_NoResponse" "${proto}" return } [ "$exitcode" = "27" ] && { upload_error "Error_IncorrectSize" "${proto}" return } [ "$exitcode" = "28" ] && { upload_error "Error_Timeout" "${proto}" return } [ "$exitcode" != "0" ] && { upload_error "Error_Other" "${proto}" return } tcp_open_request_time=$(echo "${time_start}" "${time_appconnect}" | awk '{printf "%.6f", $1 + $2}') tcp_open_response_time=$(echo "${time_start}" "${time_connect}" | awk '{printf "%.6f", $1 + $2}') rom_time=$(echo "${time_start}" "${time_pretransfer}" | awk '{printf "%.6f", $1 + $2}') bom_time=$(echo "${time_start}" "${time_starttransfer}" | awk '{printf "%.6f", $1 + $2}') eom_time=$(echo "${time_start}" "${time_total}" | awk '{printf "%.6f", $1 + $2}') separator_idx=$(expr index "${tcp_open_request_time}" .) TCPOpenRequestTime_MicroSec=${tcp_open_request_time:$separator_idx} TCPOpenRequestTime_Sec=${tcp_open_request_time:0:$((separator_idx-1))} separator_idx=$(expr index "${tcp_open_response_time}" .) TCPOpenResponseTime_MicroSec=${tcp_open_response_time:$separator_idx} TCPOpenResponseTime_Sec=${tcp_open_response_time:0:$((separator_idx-1))} separator_idx=$(expr index "${rom_time}" .) ROMTime_MicroSec=${rom_time:$separator_idx} ROMTime_Sec=${rom_time:0:$((separator_idx-1))} separator_idx=$(expr index "${bom_time}" .) BOMTime_MicroSec=${bom_time:$separator_idx} BOMTime_Sec=${bom_time:0:$((separator_idx-1))} separator_idx=$(expr index "${eom_time}" .) EOMTime_MicroSec=${eom_time:$separator_idx} EOMTime_Sec=${eom_time:0:$((separator_idx-1))} TCPOpenRequestTime=$(date -u +"%Y-%m-%dT%H:%M:%S.${TCPOpenRequestTime_MicroSec}Z" -d @"${TCPOpenRequestTime_Sec}") TCPOpenResponseTime=$(date -u +"%Y-%m-%dT%H:%M:%S.${TCPOpenResponseTime_MicroSec}Z" -d @"${TCPOpenResponseTime_Sec}") ROMTime=$(date -u +"%Y-%m-%dT%H:%M:%S.${ROMTime_MicroSec}Z" -d @"${ROMTime_Sec}") BOMTime=$(date -u +"%Y-%m-%dT%H:%M:%S.${BOMTime_MicroSec}Z" -d @"${BOMTime_Sec}") EOMTime=$(date -u +"%Y-%m-%dT%H:%M:%S.${EOMTime_MicroSec}Z" -d @"${EOMTime_Sec}") tx_bytes=$((tx_bytes_end-tx_bytes_start)) rx_bytes=$((rx_bytes_end-rx_bytes_start)) period_time=$(echo "${time_end}" "${time_start}" | awk '{printf ($1 - $2) * 1000000}') json_init json_add_string "Status" "Complete" json_add_string "IPAddressUsed" "${ip_addr_used}" json_add_string "ROMTime" "${ROMTime}" json_add_string "BOMTime" "${BOMTime}" json_add_string "EOMTime" "${EOMTime}" json_add_int "TestBytesSent" "${size_upload}" json_add_int "TotalBytesReceived" "${rx_bytes}" json_add_int "TotalBytesSent" "${tx_bytes}" json_add_int "PeriodOfFullLoading" "${period_time}" json_add_string "TCPOpenRequestTime" "${TCPOpenRequestTime}" json_add_string "TCPOpenResponseTime" "${TCPOpenResponseTime}" if [ "$enable_per_con" = "true" ] || [ "$enable_per_con" = "1" ]; then json_add_array "UploadPerConnection" json_add_object "" json_add_string "ROMTime" "${ROMTime}" json_add_string "BOMTime" "${BOMTime}" json_add_string "EOMTime" "${EOMTime}" json_add_int "TestBytesSent" "${size_upload}" json_add_int "TotalBytesReceived" "${rx_bytes}" json_add_int "TotalBytesSent" "${tx_bytes}" json_add_string "TCPOpenRequestTime" "${TCPOpenRequestTime}" json_add_string "TCPOpenResponseTime" "${TCPOpenResponseTime}" 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.IPAddressUsed="${ip_addr_used}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.ROMTime="${ROMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.BOMTime="${BOMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.EOMTime="${EOMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TestBytesSent="${size_upload}" $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="${period_time}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TCPOpenRequestTime="${TCPOpenRequestTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.upload.TCPOpenResponseTime="${TCPOpenResponseTime}" 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].ROMTime="${ROMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].BOMTime="${BOMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].EOMTime="${EOMTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TestBytesSent="${size_upload}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TotalBytesReceived="${rx_bytes}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TotalBytesSent="${tx_bytes}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TCPOpenRequestTime="${TCPOpenRequestTime}" $UCI_SET_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0].TCPOpenResponseTime="${TCPOpenResponseTime}" else $UCI_DELETE_BBF_DMMAP dmmap_diagnostics.@UploadPerConnection[0] fi } $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