Skip to content

Commit

Permalink
feat: UDP client/server example test scripts (Buildd 23.08.08)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechSolomon committed Nov 14, 2023
1 parent 1578d5f commit 64cb0a0
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 34 deletions.
63 changes: 39 additions & 24 deletions sample/FendTcpClientExample.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Passthrough application.
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Client/Server application.
For this example a simple socket is extabilshed with the diode and a test message is continuously sent to the input side
For this example a simple socket is extabilshed with the diode's TCP server and a test message is continuously sent to the input side
of the diode.
For the TCP Passthrough protocol to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
For the TCP Client/Server feature to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
each chunk of data is sent you must wait for the diode to acknowledge (ACK) the send. This ACK will come in the form of
a text string 'OK\r\n'. When this ACK is received new data is free to be sent.
For these scripts to function, the diode will need to be setup in TCP passthrough mode. Please see the user manual
For these scripts to function, the diode will need to be setup in TCP Client/Server mode. Please see the user manual
on instruction for this setup process.
These scripts will continuously run until aborted.
Expand All @@ -17,16 +17,17 @@
1. The script creates a socket object named "client".
2. A connection attempt to the diode is made.
3. If the connection is made, the script enters a sending loop.
4. A test message is sent to the diode. This test message includes a number that increments each send.
4. This loop will check to see if the data is larger than 1460 bytes. If so, it will get a chunk and send that.
5. The script will then wait for the diode to send its ACK before proceeding.
6. The number of sends variable is incremented.
6. The loop will continue until all the data has been chunked and transmitted.
7. The script waits 1 second.
8. The script repeats the send loop.
"""

import socket
from time import sleep
from timeit import default_timer

# Change this to the IP address of your diode's Input Side IP Address.
diodeInputSideIP = "192.168.1.99"
Expand All @@ -36,34 +37,48 @@

# create a socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(1)
client.settimeout(5)

# connect to the server
client.connect((diodeInputSideIP, diodeTcpPassthroughPort))

try:
numberOfSends = 1
# Data to be chunked and sent to the diode's TCP server
data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

try:
while True:
# Send data to diode
sendMessage = f"This is tcp passthrough test message number: {numberOfSends}"
client.send(sendMessage.encode())

# Wait for ACK from diode
while True:
response = client.recv(4096)
# You must wait for the diode to respond with its ACK "OK\r\n"
if response.decode() == "OK\r\n":
break

# Print debug statement
print(f'Successfully sent message to diode: {sendMessage}')

# Update number of sends index
numberOfSends += 1
if len(data) > 1460: # If the data you wish to send is larger than 1460 bytes, you need to chunk.
index = 0
while index < len(data):
# Create chunk of 1460 chars
chunk = data[index : index + 1460]

# Send chunk to the diode's TCP server
client.send(chunk.encode())

#Set timer to wait for ACK from diode
start = default_timer()

# Wait for ACK from diode
while True:
response = client.recv(4096)
# You must wait for the diode to respond with its ACK "OK\r\n"
if response.decode() == "OK\r\n":
break
elif default_timer() - start > 5:
raise TimeoutError

# Print debug statement
print(f'Successfully sent message to diode: {chunk}')

# Adjust index
index += 1460

# This is for testing purposes only. Having a 1 second delay makes testing the cross diode connection easier to see visually.
# For maximum performance, remove this delay.
sleep(1)
except TimeoutError:
print("No response was received from the diode. Please check your settings and try again.")
finally:
client.close()
17 changes: 7 additions & 10 deletions sample/FendTcpServerExample.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
"""
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Passthrough application.
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Client/Server application.
For this example a simple socket is opened and listens for the diode to connect. When data is received it is printed to
For this example a simple socket is opened and listens for the diode's TCP client to connect. When data is received it is printed to
the console window.
For these scripts to function, the diode will need to be setup in TCP passthrough mode. Please see the user manual
For these scripts to function, the diode will need to be setup in TCP client/server mode. Please see the user manual
on instruction for this setup process.
These scripts will continuously run until aborted.
The following is the expected step by step process of what the script does:
1. The script creates a socket object named "server".
2. The socket object begins listening for a connection from the diode
2. The socket object begins listening for a connection from the diode's TCP client
3. If a connection is made, the script enters a receiving loop.
4. Messages sent from the diode will be printed from the screen.
5. The script repeats the receiving loop.
"""

import socket

# Set up a TCP/IP server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Change this to the Target TCP Server IP Address in your diode's Output Side TCP Passthrough Settings.
# Change this to the Target TCP Server IP Address in your diode's Output Side TCP Client Settings.
targetTcpServerIP = "192.168.1.20"

# Change this to the Target TCP Server Port in your diode's Output Side TCP Passthrough Settings.
# Change this to the Target TCP Server Port in your diode's Output Side TCP Client Settings.
targetTcpServerPort = 503

# Set up a TCP/IP server
# Set up a TCP server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((targetTcpServerIP, targetTcpServerPort))

Expand Down
83 changes: 83 additions & 0 deletions sample/FendUdpClientExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
This is an example Python script provided to aid in the integration of a Fend Data Diode in a UDP Client/Server application.
For this example a simple socket is extabilshed with the diode's UDP server and a test message is continuously sent to the input side
of the diode.
For the UDP Client/Server feature to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
each chunk of data is sent you must wait for the diode to acknowledge (ACK) the send. This ACK will come in the form of
a text string 'OK\r\n'. When this ACK is received new data is free to be sent.
For these scripts to function, the diode will need to be setup in UDP Client/Server mode. In addition, the "Reply with an ACK" option
must be selected. Please see the user manualon instruction for this setup process.
These scripts will continuously run until aborted.
The following is the expected step by step process of what the script does:
1. The script creates a socket object named "client".
2. The script enters the sending loop.
3. This loop will check to see if the data is larger than 1460 bytes. If so, it will get a chunk and send that.
4. The script will then wait for the diode to send its ACK before proceeding.
5. The loop will continue until all the data has been chunked and transmitted.
6. The script waits 1 second.
7. The script repeats the send loop.
"""

import socket
from time import sleep
from timeit import default_timer

# Change this to the IP address of your diode's Input Side IP Address.
diodeInputSideIP = "192.168.1.99"

# Change this to the Diode UDP Server Port in your diode's Input Side UDP Passthrough Settings.
diodeUDPPassthroughPort = 50000

# create a socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.settimeout(5)

# connect to the server
client.connect((diodeInputSideIP, diodeUDPPassthroughPort))

# Data to be chunked and sent to the diode's UDP server
data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

try:
while True:
# Send data to diode
if len(data) > 1460: # If the data you wish to send is larger than 1460 bytes, you need to chunk.
index = 0
while index < len(data):
# Create chunk of 1460 chars
chunk = data[index : index + 1460]

# Send chunk to the diode's UDP server
client.send(chunk.encode())

#Set timer to wait for ACK from diode
start = default_timer()

# Wait for ACK from diode
while True:
response = client.recv(4096)
# You must wait for the diode to respond with its ACK "OK\r\n"
if response.decode() == "OK\r\n":
break
elif default_timer() - start > 5:
raise TimeoutError

# Print debug statement
print(f'Successfully sent message to diode: {chunk}')

# Adjust index
index += 1460

# This is for testing purposes only. Having a 1 second delay makes testing the cross diode connection easier to see visually.
# For maximum performance, remove this delay.
sleep(1)
except TimeoutError:
print("No response was received from the diode. Please check your settings and try again.")
finally:
client.close()
40 changes: 40 additions & 0 deletions sample/FendUdpServerExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
This is an example Python script provided to aid in the integration of a Fend Data Diode in a UDP Client/Server application.
For this example a simple socket is opened and listens for the diode's UDP client to connect. When data is received it is printed to
the console window.
For these scripts to function, the diode will need to be setup in UDP client/server mode. Please see the user manual
on instruction for this setup process.
These scripts will continuously run until aborted.
The following is the expected step by step process of what the script does:
1. The script creates a socket object named "server".
2. The socket object begins listening for datagrams from the diode's UDP client
3. Messages sent from the diode's UDP client will be printed from the screen.
4. The script repeats the receiving loop.
"""

import socket

# Change this to the Target UDP Server IP Address in your diode's Output Side UDP Client Settings.
targetUDPServerIP = "192.168.1.20"

# Change this to the Target UDP Server Port in your diode's Output Side UDP Client Settings.
targetUDPServerPort = 503

# Set up a UDP server
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind((targetUDPServerIP, targetUDPServerPort))

# Receive and print data 10 Kbytes at a time, as long as the client is sending something
try:
while True:
data, addr = server.recvfrom(10240)
print(f"Received data from {addr}: {data}")

if not data:
break
finally:
server.close()

0 comments on commit 64cb0a0

Please sign in to comment.