]> git.corax.cc Git - toolbox/commitdiff
utils: Make ipc-tap and ipc-inject use the same endpoint
authorMatthias Kruk <m@m10k.eu>
Thu, 9 Mar 2023 11:11:38 +0000 (20:11 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 9 Mar 2023 11:11:38 +0000 (20:11 +0900)
When ipc-sshtunnel is used to tunnel a pubsub topic in both directions,
ipc-tap will receive messages sent from the ipc-inject instance of the
same tunnel. This makes it impossible to use ipc-sshtunnel to relay a
topic in both directions.

This commit modifies ipc-tap and ipc-inject so that the caller may pass
an endpoint name that the scripts will use for IPC communication. This
further modifies ipc-sshtunnel so that it passes the same endpoint name
to ipc-tap and ipc-inject, causing the same endpoint to be used for
sending and receiving of messages. This causes messages from ipc-inject
not to be sent to ipc-tap.

utils/ipc-inject.sh
utils/ipc-sshtunnel.sh
utils/ipc-tap.sh

index 004e8293d8e34d3d102133dad5aef1b1925d32ea..ff7beab1e5b86efe0bdd43bf935219c1bfb22944 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 # ipc-inject.sh - Injector for toolbox IPC PubSub messages
-# Copyright (C) 2022 Matthias Kruk
+# Copyright (C) 2022-2023 Matthias Kruk
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -123,6 +123,7 @@ handle_message() {
 main() {
        local proto
        local endpoint
+       local endpoint_name
        local message
        declare -gA hooks
        declare -gi signal_received
@@ -133,13 +134,15 @@ main() {
 
        signal_received=0
 
-       opt_add_arg "k" "hook"  "v" ""               \
+       opt_add_arg "k" "hook"     "v" ""            \
                    "Command for handling hook data" \
                    '^[^:]+:.*$'                     \
                    add_hook
-       opt_add_arg "p" "proto" "v" "ipc"            \
+       opt_add_arg "p" "proto"    "v" "ipc"         \
                    "The IPC protocol to inject"     \
                    '^u?ipc$'
+       opt_add_arg "e" "endpoint" "v" ""            \
+                   "The IPC endpoint to use"
 
        if ! opt_parse "$@"; then
                return 1
@@ -150,7 +153,8 @@ main() {
                return 1
        fi
 
-       if ! endpoint=$(ipc_endpoint_open); then
+       endpoint_name=$(opt_get "endpoint")
+       if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then
                return 1
        fi
 
index 091f8442baad37743d8e45566d4a250b999d4b11..91c69477f669bd8b234d8dca4f909dc718e2c368 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 # ipc-sshtunnel.sh - Tunnel toolbox PubSub IPC messages over SSH
-# Copyright (C) 2022 Matthias Kruk
+# Copyright (C) 2022-2023 Matthias Kruk
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -45,9 +45,10 @@ _array_add() {
 establish_ipc_tunnel() {
        local direction="$1"
        local remote="$2"
-       local -n ref_topics="$3"
-       local -n ref_tap_hooks="$4"
-       local -n ref_inject_hooks="$5"
+       local endpoint="$3"
+       local -n ref_topics="$4"
+       local -n ref_tap_hooks="$5"
+       local -n ref_inject_hooks="$6"
 
        local proto
        local topic
@@ -57,10 +58,12 @@ establish_ipc_tunnel() {
 
        proto=$(opt_get "proto")
        tap_args=(
-               --proto "$proto"
+               --proto    "$proto"
+               --endpoint "$endpoint"
        )
        inject_args=(
-               --proto "$proto"
+               --proto    "$proto"
+               --endpoint "$endpoint"
        )
 
        for topic in "${ref_topics[@]}"; do
@@ -102,13 +105,14 @@ process_is_running() {
 spawn_tunnel() {
        local direction="$1"
        local remote="$2"
-       local ref_topics="$3"
-       local ref_tap_hooks="$4"
-       local ref_inject_hooks="$5"
+       local endpoint="$3"
+       local ref_topics="$4"
+       local ref_tap_hooks="$5"
+       local ref_inject_hooks="$6"
 
        local -i tunnel
 
-       if tunnel=$(establish_ipc_tunnel "$direction" "$remote" "$ref_topics" "$ref_tap_hooks" "$ref_inject_hooks"); then
+       if tunnel=$(establish_ipc_tunnel "$direction" "$remote" "$endpoint" "$ref_topics" "$ref_tap_hooks" "$ref_inject_hooks"); then
                while inst_running && process_is_running "$tunnel"; do
                        sleep 5
                done
@@ -127,6 +131,7 @@ main() {
        declare -gxa tap_hooks
        declare -gxa inject_hooks
        local remote
+       local endpoint
 
        input_topics=()
        output_topics=()
@@ -149,10 +154,11 @@ main() {
                return 1
        fi
 
+       endpoint="ipc-sshtunnel-$HOSTNAME-$$"
        remote=$(opt_get "remote")
 
-       if ! inst_start spawn_tunnel "in"  "$remote" input_topics  tap_hooks inject_hooks ||
-          ! inst_start spawn_tunnel "out" "$remote" output_topics tap_hooks inject_hooks; then
+       if ! inst_start spawn_tunnel "in"  "$remote" "$endpoint" input_topics  tap_hooks inject_hooks ||
+          ! inst_start spawn_tunnel "out" "$remote" "$endpoint" output_topics tap_hooks inject_hooks; then
                return 1
        fi
 
index 452f0c6f35ebaca5821a6f3d3bf3876698bdc295..c72b3138acf2b04ed1afeff8381093155bbb7592 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 # ipc-tap.sh - Interceptor for toolbox IPC PubSub messages
-# Copyright (C) 2022 Matthias Kruk
+# Copyright (C) 2022-2023 Matthias Kruk
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -81,7 +81,8 @@ invoke_hooks() {
 }
 
 tap_topics() {
-       local topics=("$@")
+       local endpoint_name="$1"
+       local topics=("${@:2}")
 
        local endpoint
        local topic
@@ -91,7 +92,7 @@ tap_topics() {
        err=0
        signal_received=0
 
-       if ! endpoint=$(ipc_endpoint_open); then
+       if ! endpoint=$(ipc_endpoint_open "$endpoint_name"); then
                return 1
        fi
 
@@ -136,23 +137,26 @@ tap_topics() {
 }
 
 main() {
+       local endpoint
        local proto
        local topics
        declare -gA hooks
 
        topics=()
 
-       opt_add_arg "t" "topic" "rv" ""            \
+       opt_add_arg "t" "topic"    "rv" ""         \
                    "A topic to tap into"          \
                    ''                             \
                    add_topic
-       opt_add_arg "k" "hook"  "v"  ""            \
+       opt_add_arg "k" "hook"     "v"  ""         \
                    "Hook to execute upon receipt" \
                    '^[^:]+:.+$'                   \
                    add_hook
-       opt_add_arg "p" "proto" "v"  "ipc"         \
+       opt_add_arg "p" "proto"    "v"  "ipc"      \
                    "The IPC protocol to tap"      \
                    '^u?ipc$'
+       opt_add_arg "e" "endpoint" "v"  ""         \
+                   "The IPC endpoint to use"
 
        if ! opt_parse "$@"; then
                return 1
@@ -163,7 +167,8 @@ main() {
                return 1
        fi
 
-       if ! tap_topics "${topics[@]}"; then
+       endpoint=$(opt_get "endpoint")
+       if ! tap_topics "$endpoint" "${topics[@]}"; then
                return 1
        fi