diff options
| author | t3serakt <t3ss@posteo.de> | 2024-02-15 13:53:40 +0100 |
|---|---|---|
| committer | t3serakt <t3ss@posteo.de> | 2024-02-15 14:04:16 +0100 |
| commit | 4b2303a35baa05a966543f036b43bb0cfaf22ab5 (patch) | |
| tree | 2a8c8bb8547db7106a5598b2374db9c46623e46e | |
| parent | 04c59fc58fbfd279306aaa22451754caa9b5a150 (diff) | |
| download | gnunet-4b2303a35baa05a966543f036b43bb0cfaf22ab5.tar.gz gnunet-4b2303a35baa05a966543f036b43bb0cfaf22ab5.zip | |
Transport: Added test case to simulate router NAT mapping behavior.dev/t3ss/probnat
| -rw-r--r-- | scripts/Makefile.am | 4 | ||||
| -rwxr-xr-x | scripts/netjail/getmsg.sh | 15 | ||||
| -rwxr-xr-x | scripts/netjail/nat_node_test.sh | 25 | ||||
| -rwxr-xr-x | scripts/netjail/netjail_start.sh | 10 | ||||
| -rwxr-xr-x | scripts/netjail/netjail_stop.sh | 2 | ||||
| -rw-r--r-- | src/service/testing/testing_api_cmd_exec_bash_script.c | 50 | ||||
| -rw-r--r-- | src/service/transport/Makefile.am | 17 | ||||
| -rwxr-xr-x | src/service/transport/nat_router.sh | 63 | ||||
| -rwxr-xr-x | src/service/transport/test_transport_nat_mapping.sh | 12 | ||||
| -rw-r--r-- | src/service/transport/test_transport_nat_mapping_topo.conf | 8 | ||||
| -rw-r--r-- | src/service/transport/test_transport_plugin_cmd_nat_mapping.c | 419 | ||||
| -rwxr-xr-x | src/service/transport/timeout.sh | 59 |
12 files changed, 653 insertions, 31 deletions
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 994d90952..f714cdc84 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include -I$(top_builddir)/src/include | 2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include -I$(top_builddir)/src/include |
| 3 | 3 | ||
| 4 | dist_pkgdata_DATA = \ | 4 | dist_pkgdata_DATA = \ |
| 5 | netjail/nat_node_test.sh \ | ||
| 6 | netjail/getmsg.sh \ | ||
| 5 | netjail/netjail_core.sh \ | 7 | netjail/netjail_core.sh \ |
| 6 | netjail/netjail_exec.sh \ | 8 | netjail/netjail_exec.sh \ |
| 7 | netjail/netjail_start.sh \ | 9 | netjail/netjail_start.sh \ |
| @@ -20,6 +22,8 @@ EXTRA_DIST = \ | |||
| 20 | sphinx_update.sh | 22 | sphinx_update.sh |
| 21 | 23 | ||
| 22 | install-data-hook: | 24 | install-data-hook: |
| 25 | chmod o+x '$(DESTDIR)$(pkgdatadir)/nat_node_test.sh' | ||
| 26 | chmod o+x '$(DESTDIR)$(pkgdatadir)/getmsg.sh' | ||
| 23 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_core.sh' | 27 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_core.sh' |
| 24 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_start.sh' | 28 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_start.sh' |
| 25 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_stop.sh' | 29 | chmod o+x '$(DESTDIR)$(pkgdatadir)/netjail_stop.sh' |
diff --git a/scripts/netjail/getmsg.sh b/scripts/netjail/getmsg.sh new file mode 100755 index 000000000..84d500c88 --- /dev/null +++ b/scripts/netjail/getmsg.sh | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | dirname=$(dirname "$0") | ||
| 3 | if [ $3 -gt 3 ] | ||
| 4 | then | ||
| 5 | exit 1 | ||
| 6 | fi | ||
| 7 | it=$(($3+1)) | ||
| 8 | read MESSAGE | ||
| 9 | echo START "$MESSAGE" END | ||
| 10 | if [ "START second END" != "$MESSAGE" ] || [ "START END" != "$MESSAGE" ] | ||
| 11 | then | ||
| 12 | (nc -N -l $1 | $dirname/getmsg.sh $1 $2 $it >> getmsg_$2.out) & | ||
| 13 | else | ||
| 14 | echo failure | ||
| 15 | fi | ||
diff --git a/scripts/netjail/nat_node_test.sh b/scripts/netjail/nat_node_test.sh new file mode 100755 index 000000000..f10e581ee --- /dev/null +++ b/scripts/netjail/nat_node_test.sh | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | dirname=$(dirname "$0") | ||
| 3 | PORT=$1 | ||
| 4 | PREFIX=$2 | ||
| 5 | DST=$3 | ||
| 6 | echo nat_node_test args $PORT $PREFIX $DST > gaga_$PREFIX.out | ||
| 7 | (nc -N -l $PORT | $dirname/getmsg.sh $PORT $PREFIX 1 > getmsg_$PREFIX.out) & | ||
| 8 | printf "first" |nc -N $DST $PORT | ||
| 9 | sleep 5 | ||
| 10 | printf "second" |nc -N $DST $PORT | ||
| 11 | sleep 10 | ||
| 12 | printf "third" |nc -N $DST $PORT | ||
| 13 | sleep 5 | ||
| 14 | if [ "" != "$(grep failure getmsg_$PREFIX.out)" ] | ||
| 15 | then | ||
| 16 | echo FAILURE: We received third message. >> gaga_$PREFIX.out | ||
| 17 | exit 1 | ||
| 18 | elif [ "" != "$(grep second getmsg_$PREFIX.out)" ] | ||
| 19 | then | ||
| 20 | echo SUCCESS >> gaga_$PREFIX.out | ||
| 21 | exit 0 | ||
| 22 | else | ||
| 23 | echo FAILURE: Something unexpected happened. >> gaga_$PREFIX.out | ||
| 24 | exit 1 | ||
| 25 | fi | ||
diff --git a/scripts/netjail/netjail_start.sh b/scripts/netjail/netjail_start.sh index 5e1dd2b1f..11a3f3a58 100755 --- a/scripts/netjail/netjail_start.sh +++ b/scripts/netjail/netjail_start.sh | |||
| @@ -99,18 +99,18 @@ for N in $(seq $GLOBAL_N); do | |||
| 99 | ip netns exec ${ROUTERS[$N]} ip route add "$KNOWN_GROUP.$X" dev ${ROUTER_EXT_IF[$N]} | 99 | ip netns exec ${ROUTERS[$N]} ip route add "$KNOWN_GROUP.$X" dev ${ROUTER_EXT_IF[$N]} |
| 100 | ip netns exec ${ROUTERS[$N]} ip route add default via "$KNOWN_GROUP.$X" | 100 | ip netns exec ${ROUTERS[$N]} ip route add default via "$KNOWN_GROUP.$X" |
| 101 | 101 | ||
| 102 | 102 | ||
| 103 | netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24 | 103 | netjail_node_link_bridge ${ROUTERS[$N]} ${ROUTER_NETS[$N]} $ROUTER_ADDR 24 |
| 104 | ROUTER_LINKS[$N]=$RESULT | 104 | ROUTER_LINKS[$N]=$RESULT |
| 105 | 105 | ||
| 106 | netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24 | 106 | netjail_node_add_nat ${ROUTERS[$N]} $ROUTER_ADDR 24 |
| 107 | 107 | ||
| 108 | for M in $(seq $LOCAL_M); do | 108 | for M in $(seq $LOCAL_M); do |
| 109 | netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR | 109 | netjail_node_add_default ${NODES[$N,$M]} $ROUTER_ADDR |
| 110 | done | 110 | done |
| 111 | 111 | ||
| 112 | # TODO Topology configuration must be enhanced to configure forwarding to more than one subnet node via different ports. | 112 | # TODO Topology configuration must be enhanced to configure forwarding to more than one subnet node via different ports. |
| 113 | 113 | ||
| 114 | if [ "1" == "${R_TCP[$N]}" ] | 114 | if [ "1" == "${R_TCP[$N]}" ] |
| 115 | then | 115 | then |
| 116 | #ip netns exec ${ROUTERS[$N]} nft add rule ip nat prerouting ip daddr $GLOBAL_GROUP.$N tcp dport 60002 counter dnat to $LOCAL_GROUP.1 | 116 | #ip netns exec ${ROUTERS[$N]} nft add rule ip nat prerouting ip daddr $GLOBAL_GROUP.$N tcp dport 60002 counter dnat to $LOCAL_GROUP.1 |
| @@ -159,7 +159,7 @@ for N in $(seq $GLOBAL_N); do | |||
| 159 | fi | 159 | fi |
| 160 | if [ "" != "${R_SCRIPT[$N]}" ] | 160 | if [ "" != "${R_SCRIPT[$N]}" ] |
| 161 | then | 161 | then |
| 162 | ip netns exec ${ROUTERS[$N]} ./${R_SCRIPT[$N]} ${ROUTER_NETS[$N]} 1 | 162 | ip netns exec ${ROUTERS[$N]} ./${R_SCRIPT[$N]} ${ROUTER_NETS[$N]} 1 $N & |
| 163 | fi | 163 | fi |
| 164 | done | 164 | done |
| 165 | 165 | ||
diff --git a/scripts/netjail/netjail_stop.sh b/scripts/netjail/netjail_stop.sh index 8f6c07f50..420a14b62 100755 --- a/scripts/netjail/netjail_stop.sh +++ b/scripts/netjail/netjail_stop.sh | |||
| @@ -47,7 +47,7 @@ for N in $(seq $GLOBAL_N); do | |||
| 47 | then | 47 | then |
| 48 | if [ "" != "${R_SCRIPT[$N]}" ] | 48 | if [ "" != "${R_SCRIPT[$N]}" ] |
| 49 | then | 49 | then |
| 50 | ip netns exec ${ROUTERS[$N]} ./${R_SCRIPT[$N]} ${ROUTERS[$N]} 0 $PREFIX | 50 | ip netns exec ${ROUTERS[$N]} ./${R_SCRIPT[$N]} ${ROUTERS[$N]} 0 $N |
| 51 | fi | 51 | fi |
| 52 | rm -rf /tmp/netjail_scripts | 52 | rm -rf /tmp/netjail_scripts |
| 53 | fi | 53 | fi |
diff --git a/src/service/testing/testing_api_cmd_exec_bash_script.c b/src/service/testing/testing_api_cmd_exec_bash_script.c index 171a2baac..54850ccb9 100644 --- a/src/service/testing/testing_api_cmd_exec_bash_script.c +++ b/src/service/testing/testing_api_cmd_exec_bash_script.c | |||
| @@ -59,7 +59,7 @@ struct BashScriptState | |||
| 59 | /** | 59 | /** |
| 60 | * Arguments for the script | 60 | * Arguments for the script |
| 61 | */ | 61 | */ |
| 62 | char *const*script_argv; | 62 | char **script_argv; |
| 63 | 63 | ||
| 64 | /** | 64 | /** |
| 65 | * Size of script_argv. | 65 | * Size of script_argv. |
| @@ -139,28 +139,21 @@ exec_bash_script_run (void *cls, | |||
| 139 | { | 139 | { |
| 140 | struct BashScriptState *bss = cls; | 140 | struct BashScriptState *bss = cls; |
| 141 | enum GNUNET_GenericReturnValue helper_check; | 141 | enum GNUNET_GenericReturnValue helper_check; |
| 142 | char *argv[bss->argc + 2]; | ||
| 143 | |||
| 144 | char *data_dir; | ||
| 145 | char *script_name; | ||
| 146 | |||
| 147 | data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); | ||
| 148 | GNUNET_asprintf (&script_name, "%s%s", data_dir, bss->script); | ||
| 149 | 142 | ||
| 150 | helper_check = GNUNET_OS_check_helper_binary ( | 143 | helper_check = GNUNET_OS_check_helper_binary ( |
| 151 | script_name, | 144 | bss->script_argv[0], |
| 152 | GNUNET_YES, | 145 | GNUNET_YES, |
| 153 | NULL); | 146 | NULL); |
| 154 | 147 | ||
| 155 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 148 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
| 156 | "script_name %s\n", | 149 | "script_name %s\n", |
| 157 | script_name); | 150 | bss->script_argv[0]); |
| 158 | 151 | ||
| 159 | if (GNUNET_NO == helper_check) | 152 | if (GNUNET_NO == helper_check) |
| 160 | { | 153 | { |
| 161 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 154 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
| 162 | "No SUID for %s!\n", | 155 | "No SUID for %s!\n", |
| 163 | script_name); | 156 | bss->script_argv[0]); |
| 164 | GNUNET_TESTING_interpreter_fail (is); | 157 | GNUNET_TESTING_interpreter_fail (is); |
| 165 | return; | 158 | return; |
| 166 | } | 159 | } |
| @@ -168,24 +161,17 @@ exec_bash_script_run (void *cls, | |||
| 168 | { | 161 | { |
| 169 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 162 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
| 170 | "%s not found!\n", | 163 | "%s not found!\n", |
| 171 | script_name); | 164 | bss->script_argv[0]); |
| 172 | GNUNET_TESTING_interpreter_fail (is); | 165 | GNUNET_TESTING_interpreter_fail (is); |
| 173 | return; | 166 | return; |
| 174 | } | 167 | } |
| 175 | argv[0] = script_name; | ||
| 176 | if (NULL != bss->script_argv) | ||
| 177 | { | ||
| 178 | for (int i = 0; i < bss->argc;i++) | ||
| 179 | argv[i + 1] = bss->script_argv[i]; | ||
| 180 | } | ||
| 181 | argv[bss->argc] = NULL; | ||
| 182 | 168 | ||
| 183 | bss->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, | 169 | bss->start_proc = GNUNET_OS_start_process_vap (GNUNET_OS_INHERIT_STD_ERR, |
| 184 | NULL, | 170 | NULL, |
| 185 | NULL, | 171 | NULL, |
| 186 | NULL, | 172 | NULL, |
| 187 | script_name, | 173 | bss->script_argv[0], |
| 188 | argv); | 174 | bss->script_argv); |
| 189 | bss->cwh = GNUNET_wait_child (bss->start_proc, | 175 | bss->cwh = GNUNET_wait_child (bss->start_proc, |
| 190 | &child_completed_callback, | 176 | &child_completed_callback, |
| 191 | bss); | 177 | bss); |
| @@ -200,13 +186,27 @@ GNUNET_TESTING_cmd_exec_bash_script (const char *label, | |||
| 200 | GNUNET_ChildCompletedCallback cb) | 186 | GNUNET_ChildCompletedCallback cb) |
| 201 | { | 187 | { |
| 202 | struct BashScriptState *bss; | 188 | struct BashScriptState *bss; |
| 189 | char *data_dir; | ||
| 190 | char *script_name; | ||
| 191 | unsigned int c; | ||
| 192 | |||
| 193 | data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR); | ||
| 194 | GNUNET_asprintf (&script_name, "%s%s", data_dir, script); | ||
| 203 | 195 | ||
| 204 | bss = GNUNET_new (struct BashScriptState); | 196 | bss = GNUNET_new (struct BashScriptState); |
| 205 | bss->script = script; | ||
| 206 | bss->script_argv = script_argv; // FIXME this is not just a cast to fix | ||
| 207 | bss->argc = argc; | ||
| 208 | bss->cb = cb; | 197 | bss->cb = cb; |
| 198 | bss->script_argv = GNUNET_malloc (sizeof(char *) * (argc + 2)); | ||
| 209 | 199 | ||
| 200 | bss->script_argv[0] = GNUNET_strdup (script_name); | ||
| 201 | for (c = 0; c < argc; c++) | ||
| 202 | { | ||
| 203 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 204 | "script_argv %u: %s\n", | ||
| 205 | c, | ||
| 206 | script_argv[c]); | ||
| 207 | bss->script_argv[c + 1] = GNUNET_strdup (script_argv[c]); | ||
| 208 | } | ||
| 209 | bss->script_argv[c + 1] = NULL; | ||
| 210 | return GNUNET_TESTING_command_new (bss, | 210 | return GNUNET_TESTING_command_new (bss, |
| 211 | label, | 211 | label, |
| 212 | &exec_bash_script_run, | 212 | &exec_bash_script_run, |
diff --git a/src/service/transport/Makefile.am b/src/service/transport/Makefile.am index e15a9b029..03c5448f4 100644 --- a/src/service/transport/Makefile.am +++ b/src/service/transport/Makefile.am | |||
| @@ -189,6 +189,7 @@ gnunet_service_transport_LDADD = \ | |||
| 189 | $(GN_LIBINTL) | 189 | $(GN_LIBINTL) |
| 190 | 190 | ||
| 191 | plugin_LTLIBRARIES = \ | 191 | plugin_LTLIBRARIES = \ |
| 192 | libgnunet_test_transport_plugin_cmd_nat_mapping.la \ | ||
| 192 | libgnunet_test_transport_plugin_cmd_simple_send_performance.la \ | 193 | libgnunet_test_transport_plugin_cmd_simple_send_performance.la \ |
| 193 | libgnunet_test_transport_plugin_cmd_nat_upnp.la \ | 194 | libgnunet_test_transport_plugin_cmd_nat_upnp.la \ |
| 194 | libgnunet_test_transport_plugin_cmd_simple_send.la \ | 195 | libgnunet_test_transport_plugin_cmd_simple_send.la \ |
| @@ -196,6 +197,22 @@ plugin_LTLIBRARIES = \ | |||
| 196 | libgnunet_test_transport_plugin_cmd_simple_send_dv.la \ | 197 | libgnunet_test_transport_plugin_cmd_simple_send_dv.la \ |
| 197 | libgnunet_test_transport_plugin_cmd_udp_backchannel.la | 198 | libgnunet_test_transport_plugin_cmd_udp_backchannel.la |
| 198 | 199 | ||
| 200 | libgnunet_test_transport_plugin_cmd_nat_mapping_la_SOURCES = \ | ||
| 201 | test_transport_plugin_cmd_nat_mapping.c | ||
| 202 | libgnunet_test_transport_plugin_cmd_nat_mapping_la_LIBADD = \ | ||
| 203 | libgnunettransporttesting2.la \ | ||
| 204 | libgnunettransportapplication.la \ | ||
| 205 | libgnunettransportcore.la \ | ||
| 206 | $(top_builddir)/src/service/testing/libgnunettesting.la \ | ||
| 207 | $(top_builddir)/src/service/peerstore/libgnunetpeerstore.la \ | ||
| 208 | $(top_builddir)/src/service/statistics/libgnunetstatistics.la \ | ||
| 209 | $(top_builddir)/src/lib/hello/libgnunethello.la \ | ||
| 210 | $(top_builddir)/src/service/arm/libgnunetarm.la \ | ||
| 211 | $(top_builddir)/src/lib/util/libgnunetutil.la \ | ||
| 212 | $(LTLIBINTL) | ||
| 213 | libgnunet_test_transport_plugin_cmd_nat_mapping_la_LDFLAGS = \ | ||
| 214 | $(GN_PLUGIN_LDFLAGS) | ||
| 215 | |||
| 199 | libgnunet_test_transport_plugin_cmd_nat_upnp_la_SOURCES = \ | 216 | libgnunet_test_transport_plugin_cmd_nat_upnp_la_SOURCES = \ |
| 200 | test_transport_plugin_cmd_nat_upnp.c | 217 | test_transport_plugin_cmd_nat_upnp.c |
| 201 | libgnunet_test_transport_plugin_cmd_nat_upnp_la_LIBADD = \ | 218 | libgnunet_test_transport_plugin_cmd_nat_upnp_la_LIBADD = \ |
diff --git a/src/service/transport/nat_router.sh b/src/service/transport/nat_router.sh new file mode 100755 index 000000000..37fa5db3c --- /dev/null +++ b/src/service/transport/nat_router.sh | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | dirname=$(dirname "$0") | ||
| 3 | PREFIX=$3 | ||
| 4 | echo start $2 >> timeout_$PREFIX.log | ||
| 5 | if [ $2 -eq 1 ] | ||
| 6 | then | ||
| 7 | if [ ! -d /tmp/netjail_scripts ] | ||
| 8 | then | ||
| 9 | mkdir /tmp/netjail_scripts | ||
| 10 | fi | ||
| 11 | if [ -f timeout_$PREFIX.out ] | ||
| 12 | then | ||
| 13 | rm timeout_$PREFIX.out | ||
| 14 | fi | ||
| 15 | touch timeout_$PREFIX.out | ||
| 16 | if [ -f timeout_$PREFIX.log ] | ||
| 17 | then | ||
| 18 | rm timeout_$PREFIX.log | ||
| 19 | fi | ||
| 20 | touch timeout_$PREFIX.log | ||
| 21 | timeout=6000000000 | ||
| 22 | $dirname/timeout.sh $timeout $PREFIX & | ||
| 23 | echo gaga >> timeout_$PREFIX.log | ||
| 24 | timeout_pid=$! | ||
| 25 | conntrack -E -e NEW -s 192.168.15.1 -d 92.68.150.1/24 | while read line | ||
| 26 | do | ||
| 27 | protocol=$(echo $line|awk '{printf $2"\n"}'|awk '{printf $1"\n"}') | ||
| 28 | dst=$(echo $line|awk -Fdst= '{printf $2"\n"}'|awk '{printf $1"\n"}') | ||
| 29 | src=$(echo $line|awk -Fdst= '{printf $1"\n"}'|awk -Fsrc= '{printf $2"\n"}') | ||
| 30 | port=$(echo $line|awk -Fdport= '{printf $2"\n"}'|awk '{printf $1"\n"}') | ||
| 31 | echo dnat >> timeout_$PREFIX.log | ||
| 32 | now=$(date +%s%N) | ||
| 33 | kill -TSTP $timeout_pid | ||
| 34 | if [ $(wc -l < timeout_$PREFIX.out) -eq 0 ] | ||
| 35 | then | ||
| 36 | iptables-nft -t nat -A PREROUTING -p $protocol -s $dst -d 92.68.150.$PREFIX -j DNAT --to $src | ||
| 37 | # echo iptables-nft -t nat -A PREROUTING -p $protocol -s $dst -d 92.68.150.1 -j DNAT --to $src >> timeout_$PREFIX.out | ||
| 38 | echo forwarding >> timeout_$PREFIX.log | ||
| 39 | fi | ||
| 40 | # echo $line >> timeout_$PREFIX.out | ||
| 41 | echo $now $protocol $dst $src $port >> timeout_$PREFIX.out | ||
| 42 | kill -CONT $timeout_pid | ||
| 43 | done | ||
| 44 | echo gigi >> timeout_$PREFIX.log | ||
| 45 | rm timeout_$PREFIX.out | ||
| 46 | else | ||
| 47 | #echo "find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$1|while read x" >> timeout_$PREFIX.log | ||
| 48 | #find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$1|while read x | ||
| 49 | #do | ||
| 50 | #if [ "" != "$(ps aux|grep $x|grep conntrack)" ] | ||
| 51 | #then | ||
| 52 | echo kill conntrack >> timeout_$PREFIX.log | ||
| 53 | killall conntrack #$x | ||
| 54 | #fi | ||
| 55 | #if [ "" != "$(ps aux|grep $x|grep timeout)" ] | ||
| 56 | #then | ||
| 57 | echo kill timeout >> timeout_$PREFIX.log | ||
| 58 | killall timeout.sh #$x | ||
| 59 | echo kill getmsg >> timeout_$PREFIX.log | ||
| 60 | killall getmsg.sh | ||
| 61 | #fi | ||
| 62 | #done | ||
| 63 | fi | ||
diff --git a/src/service/transport/test_transport_nat_mapping.sh b/src/service/transport/test_transport_nat_mapping.sh new file mode 100755 index 000000000..4e7d6d738 --- /dev/null +++ b/src/service/transport/test_transport_nat_mapping.sh | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | if ! [ -d "/run/netns" ]; then | ||
| 3 | echo You have to create the directory /run/netns. | ||
| 4 | fi | ||
| 5 | if [ -f /proc/sys/kernel/unprivileged_userns_clone ]; then | ||
| 6 | if [ "$(cat /proc/sys/kernel/unprivileged_userns_clone)" != 1 ]; then | ||
| 7 | echo -e "Error during test setup: The kernel parameter kernel.unprivileged_userns_clone has to be set to 1! One has to execute\n\n sysctl kernel.unprivileged_userns_clone=1\n" | ||
| 8 | exit 78 | ||
| 9 | fi | ||
| 10 | fi | ||
| 11 | # exec unshare -r -nmU bash -c "mount -t tmpfs --make-rshared tmpfs /run/netns; valgrind --leak-check=full --track-origins=yes --trace-children=yes --trace-children-skip=/usr/bin/awk,/usr/bin/cut,/usr/bin/seq,/sbin/ip/sed/bash ./test_transport_start_with_config test_transport_distance_vector_inverse_topo.conf" | ||
| 12 | exec unshare -r -nmU bash -c "mount -t tmpfs --make-rshared tmpfs /run/netns; ./test_transport_start_with_config test_transport_nat_mapping_topo.conf" | ||
diff --git a/src/service/transport/test_transport_nat_mapping_topo.conf b/src/service/transport/test_transport_nat_mapping_topo.conf new file mode 100644 index 000000000..4993d77f4 --- /dev/null +++ b/src/service/transport/test_transport_nat_mapping_topo.conf | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | M:1 | ||
| 2 | N:2 | ||
| 3 | X:0 | ||
| 4 | T:libgnunet_test_transport_plugin_cmd_nat_mapping | ||
| 5 | R:1|{tcp_port:0}|{udp_port:0}|{script:nat_router.sh} | ||
| 6 | R:2|{tcp_port:0}|{udp_port:0}|{script:nat_router.sh} | ||
| 7 | P:1:1|{connect:{P:2:1:tcp}} | ||
| 8 | P:2:1|{connect:{P:1:1:tcp}} \ No newline at end of file | ||
diff --git a/src/service/transport/test_transport_plugin_cmd_nat_mapping.c b/src/service/transport/test_transport_plugin_cmd_nat_mapping.c new file mode 100644 index 000000000..cb16da284 --- /dev/null +++ b/src/service/transport/test_transport_plugin_cmd_nat_mapping.c | |||
| @@ -0,0 +1,419 @@ | |||
| 1 | /* | ||
| 2 | This file is part of GNUnet | ||
| 3 | Copyright (C) 2021 GNUnet e.V. | ||
| 4 | |||
| 5 | GNUnet is free software: you can redistribute it and/or modify it | ||
| 6 | under the terms of the GNU Affero General Public License as published | ||
| 7 | by the Free Software Foundation, either version 3 of the License, | ||
| 8 | or (at your option) any later version. | ||
| 9 | |||
| 10 | GNUnet is distributed in the hope that it will be useful, but | ||
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | Affero General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU Affero General Public License | ||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file testbed/plugin_cmd_nat_mapping.c | ||
| 23 | * @brief a plugin to provide the API for running test cases. | ||
| 24 | * @author t3sserakt | ||
| 25 | */ | ||
| 26 | #include "platform.h" | ||
| 27 | #include "gnunet_testing_barrier.h" | ||
| 28 | #include "gnunet_testing_netjail_lib.h" | ||
| 29 | #include "gnunet_util_lib.h" | ||
| 30 | #include "gnunet_transport_application_service.h" | ||
| 31 | #include "transport-testing2.h" | ||
| 32 | #include "transport-testing-cmds.h" | ||
| 33 | #include "gnunet_testing_barrier.h" | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Generic logging shortcut | ||
| 37 | */ | ||
| 38 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
| 39 | |||
| 40 | #define BASE_DIR "testdir" | ||
| 41 | |||
| 42 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600) | ||
| 43 | |||
| 44 | #define ROUTER_BASE_IP "92.68.150." | ||
| 45 | |||
| 46 | static struct GNUNET_TESTING_Command block_send; | ||
| 47 | |||
| 48 | static struct GNUNET_TESTING_Command block_receive; | ||
| 49 | |||
| 50 | static struct GNUNET_TESTING_Command connect_peers; | ||
| 51 | |||
| 52 | static struct GNUNET_TESTING_Command local_prepared; | ||
| 53 | |||
| 54 | static struct GNUNET_TESTING_Interpreter *is; | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Function called to check a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE being | ||
| 58 | * received. | ||
| 59 | * | ||
| 60 | */ | ||
| 61 | static int | ||
| 62 | check_test (void *cls, | ||
| 63 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) | ||
| 64 | { | ||
| 65 | return GNUNET_OK; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | /** | ||
| 70 | * Function called to handle a message of type GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE | ||
| 71 | * being received. | ||
| 72 | * | ||
| 73 | */ | ||
| 74 | static void | ||
| 75 | handle_test (void *cls, | ||
| 76 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message) | ||
| 77 | { | ||
| 78 | struct GNUNET_TESTING_AsyncContext *ac; | ||
| 79 | |||
| 80 | GNUNET_TESTING_get_trait_async_context (&block_receive, | ||
| 81 | &ac); | ||
| 82 | GNUNET_assert (NULL != ac); | ||
| 83 | if (NULL == ac->cont) | ||
| 84 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
| 85 | else | ||
| 86 | GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | struct GNUNET_TESTING_BarrierList * | ||
| 91 | get_waiting_for_barriers () | ||
| 92 | { | ||
| 93 | //No Barrier | ||
| 94 | return GNUNET_new (struct GNUNET_TESTING_BarrierList); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | /** | ||
| 99 | * Callback to set the flag indicating all peers started. Will be called via the plugin api. | ||
| 100 | * | ||
| 101 | */ | ||
| 102 | static void | ||
| 103 | all_peers_started () | ||
| 104 | { | ||
| 105 | struct GNUNET_TESTING_AsyncContext *ac; | ||
| 106 | |||
| 107 | GNUNET_TESTING_get_trait_async_context (&block_send, | ||
| 108 | &ac); | ||
| 109 | GNUNET_assert (NULL != ac); | ||
| 110 | if (NULL == ac->cont) | ||
| 111 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
| 112 | else | ||
| 113 | GNUNET_TESTING_async_finish ((struct GNUNET_TESTING_AsyncContext *) ac); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | /** | ||
| 118 | * Function called with the final result of the test. | ||
| 119 | * | ||
| 120 | * @param cls the `struct MainParams` | ||
| 121 | * @param rv #GNUNET_OK if the test passed | ||
| 122 | */ | ||
| 123 | static void | ||
| 124 | handle_result (void *cls, | ||
| 125 | enum GNUNET_GenericReturnValue rv) | ||
| 126 | { | ||
| 127 | struct TestState *ts = cls; | ||
| 128 | |||
| 129 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
| 130 | "Local test exits with status %d\n", | ||
| 131 | rv); | ||
| 132 | |||
| 133 | ts->finished_cb (rv); | ||
| 134 | GNUNET_free (ts->testdir); | ||
| 135 | GNUNET_free (ts->cfgname); | ||
| 136 | GNUNET_TESTING_free_topology (ts->topology); | ||
| 137 | GNUNET_free (ts); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | /** | ||
| 142 | * Callback from start peer cmd for signaling a peer got connected. | ||
| 143 | * | ||
| 144 | */ | ||
| 145 | static void * | ||
| 146 | notify_connect (struct GNUNET_TESTING_Interpreter *is, | ||
| 147 | const struct GNUNET_PeerIdentity *peer) | ||
| 148 | { | ||
| 149 | const struct ConnectPeersState *cps; | ||
| 150 | const struct GNUNET_TESTING_Command *cmd; | ||
| 151 | |||
| 152 | cmd = GNUNET_TESTING_interpreter_lookup_command (is, | ||
| 153 | "connect-peers"); | ||
| 154 | GNUNET_TRANSPORT_TESTING_get_trait_connect_peer_state (cmd, | ||
| 155 | &cps); | ||
| 156 | void *ret = NULL; | ||
| 157 | |||
| 158 | cps->notify_connect (is, | ||
| 159 | peer); | ||
| 160 | return ret; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | /** | ||
| 165 | * Callback to set the flag indicating all peers are prepared to finish. Will be called via the plugin api. | ||
| 166 | */ | ||
| 167 | static void | ||
| 168 | all_local_tests_prepared () | ||
| 169 | { | ||
| 170 | const struct GNUNET_TESTING_LocalPreparedState *lfs; | ||
| 171 | |||
| 172 | GNUNET_TESTING_get_trait_local_prepared_state (&local_prepared, | ||
| 173 | &lfs); | ||
| 174 | GNUNET_assert (NULL != &lfs->ac); | ||
| 175 | if (NULL == lfs->ac.cont) | ||
| 176 | GNUNET_TESTING_async_fail ((struct GNUNET_TESTING_AsyncContext *) &lfs->ac); | ||
| 177 | else | ||
| 178 | GNUNET_TESTING_async_finish ((struct | ||
| 179 | GNUNET_TESTING_AsyncContext *) &lfs->ac); | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | static void | ||
| 184 | child_completed_callback (void *cls, | ||
| 185 | enum GNUNET_OS_ProcessStatusType type, | ||
| 186 | long unsigned int exit_code) | ||
| 187 | { | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | /** | ||
| 193 | * Function to start a local test case. | ||
| 194 | * | ||
| 195 | * @param write_message Callback to send a message to the master loop. | ||
| 196 | * @param router_ip Global address of the network namespace. | ||
| 197 | * @param node_ip The IP address of the node. | ||
| 198 | * @param m The number of the node in a network namespace. | ||
| 199 | * @param n The number of the network namespace. | ||
| 200 | * @param local_m The number of nodes in a network namespace. | ||
| 201 | */ | ||
| 202 | static struct GNUNET_TESTING_Interpreter * | ||
| 203 | start_testcase (GNUNET_TESTING_cmd_helper_write_cb write_message, | ||
| 204 | const char *router_ip, | ||
| 205 | const char *node_ip, | ||
| 206 | const char *m, | ||
| 207 | const char *n, | ||
| 208 | const char *local_m, | ||
| 209 | const char *topology_data, | ||
| 210 | unsigned int *read_file, | ||
| 211 | GNUNET_TESTING_cmd_helper_finish_cb finished_cb) | ||
| 212 | { | ||
| 213 | |||
| 214 | unsigned int n_int; | ||
| 215 | unsigned int m_int; | ||
| 216 | unsigned int local_m_int; | ||
| 217 | unsigned int num; | ||
| 218 | struct TestState *ts = GNUNET_new (struct TestState); | ||
| 219 | struct GNUNET_TESTING_NetjailTopology *topology; | ||
| 220 | unsigned int sscanf_ret = 0; | ||
| 221 | char **argv = NULL; | ||
| 222 | unsigned int argc = 0; | ||
| 223 | char *dst_ip; | ||
| 224 | char *num_string; | ||
| 225 | |||
| 226 | |||
| 227 | |||
| 228 | ts->finished_cb = finished_cb; | ||
| 229 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
| 230 | "n %s m %s\n", | ||
| 231 | n, | ||
| 232 | m); | ||
| 233 | |||
| 234 | if (GNUNET_YES == *read_file) | ||
| 235 | { | ||
| 236 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 237 | "read from file\n"); | ||
| 238 | topology = GNUNET_TESTING_get_topo_from_file (topology_data); | ||
| 239 | } | ||
| 240 | else | ||
| 241 | topology = GNUNET_TESTING_get_topo_from_string (topology_data); | ||
| 242 | |||
| 243 | ts->topology = topology; | ||
| 244 | |||
| 245 | errno = 0; | ||
| 246 | sscanf_ret = sscanf (m, "%u", &m_int); | ||
| 247 | if (errno != 0) | ||
| 248 | { | ||
| 249 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
| 250 | } | ||
| 251 | GNUNET_assert (0 < sscanf_ret); | ||
| 252 | errno = 0; | ||
| 253 | sscanf_ret = sscanf (n, "%u", &n_int); | ||
| 254 | if (errno != 0) | ||
| 255 | { | ||
| 256 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
| 257 | } | ||
| 258 | GNUNET_assert (0 < sscanf_ret); | ||
| 259 | errno = 0; | ||
| 260 | sscanf_ret = sscanf (local_m, "%u", &local_m_int); | ||
| 261 | if (errno != 0) | ||
| 262 | { | ||
| 263 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sscanf"); | ||
| 264 | } | ||
| 265 | GNUNET_assert (0 < sscanf_ret); | ||
| 266 | |||
| 267 | if (0 == n_int) | ||
| 268 | num = m_int; | ||
| 269 | else | ||
| 270 | num = (n_int - 1) * local_m_int + m_int + topology->nodes_x; | ||
| 271 | |||
| 272 | block_send = GNUNET_TESTING_cmd_block_until_external_trigger ( | ||
| 273 | "block"); | ||
| 274 | block_receive = GNUNET_TESTING_cmd_block_until_external_trigger ( | ||
| 275 | "block-receive"); | ||
| 276 | connect_peers = GNUNET_TRANSPORT_cmd_connect_peers ("connect-peers", | ||
| 277 | "start-peer", | ||
| 278 | "system-create", | ||
| 279 | num, | ||
| 280 | topology, | ||
| 281 | 0, | ||
| 282 | GNUNET_YES); | ||
| 283 | local_prepared = GNUNET_TESTING_cmd_local_test_prepared ( | ||
| 284 | "local-test-prepared", | ||
| 285 | write_message); | ||
| 286 | |||
| 287 | |||
| 288 | GNUNET_asprintf (&ts->cfgname, | ||
| 289 | "test_transport_api2_tcp_node1.conf"); | ||
| 290 | |||
| 291 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 292 | "plugin cfgname: %s\n", | ||
| 293 | ts->cfgname); | ||
| 294 | |||
| 295 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 296 | "node ip: %s\n", | ||
| 297 | node_ip); | ||
| 298 | |||
| 299 | GNUNET_asprintf (&ts->testdir, | ||
| 300 | "%s%s%s", | ||
| 301 | BASE_DIR, | ||
| 302 | m, | ||
| 303 | n); | ||
| 304 | |||
| 305 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
| 306 | GNUNET_MQ_hd_var_size (test, | ||
| 307 | GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE, | ||
| 308 | struct GNUNET_TRANSPORT_TESTING_TestMessage, | ||
| 309 | ts), | ||
| 310 | GNUNET_MQ_handler_end () | ||
| 311 | }; | ||
| 312 | |||
| 313 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 314 | "num: %u\n", | ||
| 315 | num); | ||
| 316 | GNUNET_asprintf (&num_string, | ||
| 317 | "%u", | ||
| 318 | num); | ||
| 319 | GNUNET_array_append (argv, argc, "7777"); | ||
| 320 | GNUNET_array_append (argv, argc, num_string); | ||
| 321 | if (1 == num ) | ||
| 322 | { | ||
| 323 | GNUNET_asprintf (&dst_ip, | ||
| 324 | ROUTER_BASE_IP "%u", | ||
| 325 | num + 1); | ||
| 326 | GNUNET_array_append (argv, argc, dst_ip); | ||
| 327 | } | ||
| 328 | else | ||
| 329 | { | ||
| 330 | GNUNET_asprintf (&dst_ip, | ||
| 331 | ROUTER_BASE_IP "%u", | ||
| 332 | num - 1); | ||
| 333 | GNUNET_array_append (argv, argc, dst_ip); | ||
| 334 | } | ||
| 335 | |||
| 336 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
| 337 | "dst_ip %s\n", | ||
| 338 | dst_ip); | ||
| 339 | struct GNUNET_TESTING_Command commands[] = { | ||
| 340 | GNUNET_TESTING_cmd_system_create ("system-create", | ||
| 341 | ts->testdir), | ||
| 342 | GNUNET_TRANSPORT_cmd_start_peer ("start-peer", | ||
| 343 | "system-create", | ||
| 344 | num, | ||
| 345 | node_ip, | ||
| 346 | handlers, | ||
| 347 | ts->cfgname, | ||
| 348 | notify_connect, | ||
| 349 | GNUNET_NO), | ||
| 350 | GNUNET_TESTING_cmd_exec_bash_script ("nat_node_test", | ||
| 351 | "nat_node_test.sh", | ||
| 352 | argv, | ||
| 353 | argc, | ||
| 354 | &child_completed_callback), | ||
| 355 | GNUNET_TESTING_cmd_send_peer_ready ("send-peer-ready", | ||
| 356 | write_message), | ||
| 357 | block_send, | ||
| 358 | connect_peers, | ||
| 359 | local_prepared, | ||
| 360 | GNUNET_TRANSPORT_cmd_stop_peer ("stop-peer", | ||
| 361 | "start-peer"), | ||
| 362 | GNUNET_TESTING_cmd_system_destroy ("system-destroy", | ||
| 363 | "system-create"), | ||
| 364 | GNUNET_TESTING_cmd_end () | ||
| 365 | }; | ||
| 366 | |||
| 367 | ts->write_message = write_message; | ||
| 368 | |||
| 369 | is = GNUNET_TESTING_run (commands, | ||
| 370 | TIMEOUT, | ||
| 371 | &handle_result, | ||
| 372 | ts); | ||
| 373 | GNUNET_free (num_string); | ||
| 374 | GNUNET_free (dst_ip); | ||
| 375 | return is; | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | /** | ||
| 380 | * Entry point for the plugin. | ||
| 381 | * | ||
| 382 | * @param cls NULL | ||
| 383 | * @return the exported block API | ||
| 384 | */ | ||
| 385 | void * | ||
| 386 | libgnunet_test_transport_plugin_cmd_nat_mapping_init (void *cls) | ||
| 387 | { | ||
| 388 | struct GNUNET_TESTING_PluginFunctions *api; | ||
| 389 | |||
| 390 | GNUNET_log_setup ("simple-send", | ||
| 391 | "DEBUG", | ||
| 392 | NULL); | ||
| 393 | |||
| 394 | api = GNUNET_new (struct GNUNET_TESTING_PluginFunctions); | ||
| 395 | api->start_testcase = &start_testcase; | ||
| 396 | api->all_peers_started = &all_peers_started; | ||
| 397 | api->all_local_tests_prepared = all_local_tests_prepared; | ||
| 398 | api->get_waiting_for_barriers = get_waiting_for_barriers; | ||
| 399 | return api; | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | /** | ||
| 404 | * Exit point from the plugin. | ||
| 405 | * | ||
| 406 | * @param cls the return value from #libgnunet_test_transport_plugin_cmd_nat_mapping_done | ||
| 407 | * @return NULL | ||
| 408 | */ | ||
| 409 | void * | ||
| 410 | libgnunet_test_transport_plugin_cmd_nat_mapping_done (void *cls) | ||
| 411 | { | ||
| 412 | struct GNUNET_TESTING_PluginFunctions *api = cls; | ||
| 413 | |||
| 414 | GNUNET_free (api); | ||
| 415 | return NULL; | ||
| 416 | } | ||
| 417 | |||
| 418 | |||
| 419 | /* end of plugin_cmd_nat_mapping.c */ | ||
diff --git a/src/service/transport/timeout.sh b/src/service/transport/timeout.sh new file mode 100755 index 000000000..b5692eb7c --- /dev/null +++ b/src/service/transport/timeout.sh | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | timeout=$1 | ||
| 3 | PREFIX=$2 | ||
| 4 | line=$(head -n 1 timeout_$PREFIX.out) | ||
| 5 | date=$(echo $line|awk '{printf $1"\n"}') | ||
| 6 | now=$(date +%s%N) | ||
| 7 | sleeptime=$(bc -l <<< "0.000000001*$1") | ||
| 8 | if [ "" != "$line" ] && [ $timeout -le $(($now - $date)) ] | ||
| 9 | then | ||
| 10 | sleeptime=0 | ||
| 11 | elif [ "" != "$line" ] | ||
| 12 | then | ||
| 13 | sleeptime=$(bc -l <<< "(${now}-${date})/1000000000") | ||
| 14 | fi | ||
| 15 | echo $sleeptime >> timeout_$PREFIX.log | ||
| 16 | while sleep $sleeptime | ||
| 17 | do | ||
| 18 | line_num=$(wc -l < timeout_$PREFIX.out) | ||
| 19 | if [ 0 -lt $line_num ];then | ||
| 20 | for i in $(seq 1 $line_num) | ||
| 21 | do | ||
| 22 | line=$(head -n 1 timeout_$PREFIX.out) | ||
| 23 | date=$(echo $line|awk '{printf $1"\n"}') | ||
| 24 | port=$(echo $line|awk '{printf $5"\n"}') | ||
| 25 | dst=$(echo $line|awk '{printf $3"\n"}') | ||
| 26 | src=$(echo $line|awk '{printf $4"\n"}') | ||
| 27 | protocol=$(echo $line|awk '{printf $2"\n"}') | ||
| 28 | ports[$i]=-1 | ||
| 29 | now=$(date +%s%N) | ||
| 30 | echo $timeout $now $date >> timeout_$PREFIX.log | ||
| 31 | if [ $timeout -le $(($now - $date)) ] | ||
| 32 | then | ||
| 33 | ports[$i]=$port | ||
| 34 | echo delete dnat $port ${ports[$i]} >> timeout_$PREFIX.log | ||
| 35 | sed -i -n -e '2,$p' timeout_$PREFIX.out | ||
| 36 | sleeptime=$(bc -l <<< "0.000000001*$1") | ||
| 37 | else | ||
| 38 | for j in $(seq 1 $i) | ||
| 39 | do | ||
| 40 | if [ ${ports[$j]} -eq $port ] | ||
| 41 | then | ||
| 42 | ports[$j]=-1 | ||
| 43 | fi | ||
| 44 | done | ||
| 45 | sleeptime=$(bc -l <<< "(${now}-${date})/1000000000") | ||
| 46 | echo $sleeptime >> timeout_$PREFIX.log | ||
| 47 | fi | ||
| 48 | done | ||
| 49 | for i in $(seq 1 $line_num) | ||
| 50 | do | ||
| 51 | echo $i ${ports[$i]} >> timeout_$PREFIX.log | ||
| 52 | if [ ${ports[$i]} -ne -1 ] | ||
| 53 | then | ||
| 54 | echo iptables-nft -t nat -D PREROUTING -p $protocol -s $dst -d 92.68.150.$PREFIX -j DNAT --to $src >> timeout_$PREFIX.log | ||
| 55 | iptables-nft -t nat -D PREROUTING -p $protocol -s $dst -d 92.68.150.$PREFIX -j DNAT --to $src | ||
| 56 | fi | ||
| 57 | done | ||
| 58 | fi | ||
| 59 | done | ||