Skip to content

Commit

Permalink
chore(network): Formatting issues
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Bode <eric.bode@foundries.io>
  • Loading branch information
StealthyCoder committed Oct 9, 2023
1 parent 2e07254 commit eca4872
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 114 deletions.
222 changes: 111 additions & 111 deletions src/bash/io/network/tcp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,123 +11,123 @@ require logging/logging
require arrays/arrays

function network.tcp.send {
__verify_nr_args "$#" 2 network.tcp.send
local fd data
fd="$1"
data="$2"
echo -e "$data" >&$fd
__verify_nr_args "$#" 2 network.tcp.send
local fd data
fd="$1"
data="$2"
echo -e "$data" >&"$fd"
}

function network.tcp.receive {
__verify_nr_args "$#" 1 network.tcp.receive
local fd response
fd="$1"
response=$(cat <&$fd)
echo "$response"
__verify_nr_args "$#" 1 network.tcp.receive
local fd response
fd="$1"
response=$(cat <&"$fd")
echo "$response"
}

function network.tcp {
# switch on number of arguments
# Minimum is two:
# on 2 - host and port we only do setup
# on 3 - we do immediate host, port and send data and retrieve
# on 4+ - we log warning that surplus is ignored
case "$#" in
0 | 1)
logging.error "network.tcp: Wrong number of arguments, need at least 2 or 3."
logging.error "network.tcp: usage is network.tcp <HOST> <PORT>"
logging.error "to setup a file descriptor to send messages"
logging.error "network.tcp: usage is network.tcp <HOST> <PORT> <DATA>"
logging.error "to send one message and immediately get response"
exit 1
;;
2)
# 4. exec 3<>/dev/tcp/www.google.com/80
# echo -e "GET / HTTP/1.1\r\nhost: http://www.google.com\r\nConnection: close\r\n\r\n" >&3
# cat <&3
local host port
host="$1"
port="$2"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
export CRUCIBLE_NETWORK_FD="$available_fd"
logging.debug "Exported CRUCIBLE_NETWORK_FD to value: $available_fd"
;;
3)
local host port data
host="$1"
port="$2"
data="$3"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
network.tcp_send "$available_fd" "$data"
echo "$(network.tcp_receive "$available_fd")"
;;
*)
arrays.transform_into_array args
arrays.add_all args "$@"
local surplus i
surplus=""
for ((i = 3; i < ${#args[@]}; i++)); do
surplus+=" ${args[$i]}"
done
logging.warning "Ignoring surplus:$surplus"
unset args
local host port data
host="$1"
port="$2"
data="$3"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
network.tcp_send "$available_fd" "$data"
echo "$(network.tcp_receive "$available_fd")"
;;
esac
# switch on number of arguments
# Minimum is two:
# on 2 - host and port we only do setup
# on 3 - we do immediate host, port and send data and retrieve
# on 4+ - we log warning that surplus is ignored
case "$#" in
0 | 1)
logging.error "network.tcp: Wrong number of arguments, need at least 2 or 3."
logging.error "network.tcp: usage is network.tcp <HOST> <PORT>"
logging.error "to setup a file descriptor to send messages"
logging.error "network.tcp: usage is network.tcp <HOST> <PORT> <DATA>"
logging.error "to send one message and immediately get response"
exit 1
;;
2)
# 4. exec 3<>/dev/tcp/www.google.com/80
# echo -e "GET / HTTP/1.1\r\nhost: http://www.google.com\r\nConnection: close\r\n\r\n" >&3
# cat <&3
local host port
host="$1"
port="$2"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
export CRUCIBLE_NETWORK_FD="$available_fd"
logging.debug "Exported CRUCIBLE_NETWORK_FD to value: $available_fd"
;;
3)
local host port data
host="$1"
port="$2"
data="$3"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
network.tcp_send "$available_fd" "$data"
echo "$(network.tcp_receive "$available_fd")"
;;
*)
arrays.transform_into_array args
arrays.add_all args "$@"
local surplus i
surplus=""
for ((i = 3; i < ${#args[@]}; i++)); do
surplus+=" ${args[$i]}"
done
logging.warning "Ignoring surplus:$surplus"
unset args
local host port data
host="$1"
port="$2"
data="$3"
if ! network.verify_host "$host"; then
logging.error "Invalid hostname: $host"
exit 1
fi
if ! network.verify_port "$port"; then
logging.error "Invalid port: $port"
exit 1
fi
local available_fd
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
available_fd="$(($available_fd + 1))"
fi
done
eval "exec $available_fd<>/dev/tcp/$host/$port"
network.tcp_send "$available_fd" "$data"
echo "$(network.tcp_receive "$available_fd")"
;;
esac
}
function network.tcp.close {
__verify_nr_args "$#" 1 network.close
Expand Down
6 changes: 3 additions & 3 deletions src/bash/io/network/udp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function network.udp {
exit 1
fi
local available_fd
for fd in "$(ls /proc/$$/fd)"; do
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
Expand All @@ -263,7 +263,7 @@ function network.udp {
exit 1
fi
local available_fd
for fd in "$(ls /proc/$$/fd)"; do
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
Expand Down Expand Up @@ -297,7 +297,7 @@ function network.udp {
exit 1
fi
local available_fd
for fd in "$(ls /proc/$$/fd)"; do
for fd in $(ls /proc/$$/fd); do
if test "$fd" -lt 255; then
available_fd="$fd"
elif test "$fd" -eq 255; then
Expand Down

0 comments on commit eca4872

Please sign in to comment.