Skip to content

Commit 64cb0a0

Browse files
committed
feat: UDP client/server example test scripts (Buildd 23.08.08)
1 parent 1578d5f commit 64cb0a0

File tree

4 files changed

+169
-34
lines changed

4 files changed

+169
-34
lines changed

sample/FendTcpClientExample.py

+39-24
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
2-
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Passthrough application.
2+
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Client/Server application.
33
4-
For this example a simple socket is extabilshed with the diode and a test message is continuously sent to the input side
4+
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
55
of the diode.
66
7-
For the TCP Passthrough protocol to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
7+
For the TCP Client/Server feature to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
88
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
99
a text string 'OK\r\n'. When this ACK is received new data is free to be sent.
1010
11-
For these scripts to function, the diode will need to be setup in TCP passthrough mode. Please see the user manual
11+
For these scripts to function, the diode will need to be setup in TCP Client/Server mode. Please see the user manual
1212
on instruction for this setup process.
1313
1414
These scripts will continuously run until aborted.
@@ -17,16 +17,17 @@
1717
1. The script creates a socket object named "client".
1818
2. A connection attempt to the diode is made.
1919
3. If the connection is made, the script enters a sending loop.
20-
4. A test message is sent to the diode. This test message includes a number that increments each send.
20+
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.
2121
5. The script will then wait for the diode to send its ACK before proceeding.
22-
6. The number of sends variable is incremented.
22+
6. The loop will continue until all the data has been chunked and transmitted.
2323
7. The script waits 1 second.
2424
8. The script repeats the send loop.
2525
2626
"""
2727

2828
import socket
2929
from time import sleep
30+
from timeit import default_timer
3031

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

3738
# create a socket
3839
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
39-
client.settimeout(1)
40+
client.settimeout(5)
4041

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

44-
try:
45-
numberOfSends = 1
45+
# Data to be chunked and sent to the diode's TCP server
46+
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."
4647

48+
try:
4749
while True:
4850
# Send data to diode
49-
sendMessage = f"This is tcp passthrough test message number: {numberOfSends}"
50-
client.send(sendMessage.encode())
51-
52-
# Wait for ACK from diode
53-
while True:
54-
response = client.recv(4096)
55-
# You must wait for the diode to respond with its ACK "OK\r\n"
56-
if response.decode() == "OK\r\n":
57-
break
58-
59-
# Print debug statement
60-
print(f'Successfully sent message to diode: {sendMessage}')
61-
62-
# Update number of sends index
63-
numberOfSends += 1
51+
if len(data) > 1460: # If the data you wish to send is larger than 1460 bytes, you need to chunk.
52+
index = 0
53+
while index < len(data):
54+
# Create chunk of 1460 chars
55+
chunk = data[index : index + 1460]
56+
57+
# Send chunk to the diode's TCP server
58+
client.send(chunk.encode())
59+
60+
#Set timer to wait for ACK from diode
61+
start = default_timer()
62+
63+
# Wait for ACK from diode
64+
while True:
65+
response = client.recv(4096)
66+
# You must wait for the diode to respond with its ACK "OK\r\n"
67+
if response.decode() == "OK\r\n":
68+
break
69+
elif default_timer() - start > 5:
70+
raise TimeoutError
71+
72+
# Print debug statement
73+
print(f'Successfully sent message to diode: {chunk}')
74+
75+
# Adjust index
76+
index += 1460
6477

6578
# This is for testing purposes only. Having a 1 second delay makes testing the cross diode connection easier to see visually.
6679
# For maximum performance, remove this delay.
6780
sleep(1)
81+
except TimeoutError:
82+
print("No response was received from the diode. Please check your settings and try again.")
6883
finally:
6984
client.close()

sample/FendTcpServerExample.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
"""
2-
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Passthrough application.
2+
This is an example Python script provided to aid in the integration of a Fend Data Diode in a TCP Client/Server application.
33
4-
For this example a simple socket is opened and listens for the diode to connect. When data is received it is printed to
4+
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
55
the console window.
66
7-
For these scripts to function, the diode will need to be setup in TCP passthrough mode. Please see the user manual
7+
For these scripts to function, the diode will need to be setup in TCP client/server mode. Please see the user manual
88
on instruction for this setup process.
99
1010
These scripts will continuously run until aborted.
1111
1212
The following is the expected step by step process of what the script does:
1313
1. The script creates a socket object named "server".
14-
2. The socket object begins listening for a connection from the diode
14+
2. The socket object begins listening for a connection from the diode's TCP client
1515
3. If a connection is made, the script enters a receiving loop.
1616
4. Messages sent from the diode will be printed from the screen.
1717
5. The script repeats the receiving loop.
1818
"""
1919

2020
import socket
2121

22-
# Set up a TCP/IP server
23-
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24-
25-
# Change this to the Target TCP Server IP Address in your diode's Output Side TCP Passthrough Settings.
22+
# Change this to the Target TCP Server IP Address in your diode's Output Side TCP Client Settings.
2623
targetTcpServerIP = "192.168.1.20"
2724

28-
# Change this to the Target TCP Server Port in your diode's Output Side TCP Passthrough Settings.
25+
# Change this to the Target TCP Server Port in your diode's Output Side TCP Client Settings.
2926
targetTcpServerPort = 503
3027

31-
# Set up a TCP/IP server
28+
# Set up a TCP server
3229
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3330
server.bind((targetTcpServerIP, targetTcpServerPort))
3431

sample/FendUdpClientExample.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
This is an example Python script provided to aid in the integration of a Fend Data Diode in a UDP Client/Server application.
3+
4+
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
5+
of the diode.
6+
7+
For the UDP Client/Server feature to function properly, data must be sent in data chunks no larger than 1460 Bytes. After
8+
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
9+
a text string 'OK\r\n'. When this ACK is received new data is free to be sent.
10+
11+
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
12+
must be selected. Please see the user manualon instruction for this setup process.
13+
14+
These scripts will continuously run until aborted.
15+
16+
The following is the expected step by step process of what the script does:
17+
1. The script creates a socket object named "client".
18+
2. The script enters the sending loop.
19+
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.
20+
4. The script will then wait for the diode to send its ACK before proceeding.
21+
5. The loop will continue until all the data has been chunked and transmitted.
22+
6. The script waits 1 second.
23+
7. The script repeats the send loop.
24+
25+
"""
26+
27+
import socket
28+
from time import sleep
29+
from timeit import default_timer
30+
31+
# Change this to the IP address of your diode's Input Side IP Address.
32+
diodeInputSideIP = "192.168.1.99"
33+
34+
# Change this to the Diode UDP Server Port in your diode's Input Side UDP Passthrough Settings.
35+
diodeUDPPassthroughPort = 50000
36+
37+
# create a socket
38+
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
39+
client.settimeout(5)
40+
41+
# connect to the server
42+
client.connect((diodeInputSideIP, diodeUDPPassthroughPort))
43+
44+
# Data to be chunked and sent to the diode's UDP server
45+
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."
46+
47+
try:
48+
while True:
49+
# Send data to diode
50+
if len(data) > 1460: # If the data you wish to send is larger than 1460 bytes, you need to chunk.
51+
index = 0
52+
while index < len(data):
53+
# Create chunk of 1460 chars
54+
chunk = data[index : index + 1460]
55+
56+
# Send chunk to the diode's UDP server
57+
client.send(chunk.encode())
58+
59+
#Set timer to wait for ACK from diode
60+
start = default_timer()
61+
62+
# Wait for ACK from diode
63+
while True:
64+
response = client.recv(4096)
65+
# You must wait for the diode to respond with its ACK "OK\r\n"
66+
if response.decode() == "OK\r\n":
67+
break
68+
elif default_timer() - start > 5:
69+
raise TimeoutError
70+
71+
# Print debug statement
72+
print(f'Successfully sent message to diode: {chunk}')
73+
74+
# Adjust index
75+
index += 1460
76+
77+
# This is for testing purposes only. Having a 1 second delay makes testing the cross diode connection easier to see visually.
78+
# For maximum performance, remove this delay.
79+
sleep(1)
80+
except TimeoutError:
81+
print("No response was received from the diode. Please check your settings and try again.")
82+
finally:
83+
client.close()

sample/FendUdpServerExample.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
This is an example Python script provided to aid in the integration of a Fend Data Diode in a UDP Client/Server application.
3+
4+
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
5+
the console window.
6+
7+
For these scripts to function, the diode will need to be setup in UDP client/server mode. Please see the user manual
8+
on instruction for this setup process.
9+
10+
These scripts will continuously run until aborted.
11+
12+
The following is the expected step by step process of what the script does:
13+
1. The script creates a socket object named "server".
14+
2. The socket object begins listening for datagrams from the diode's UDP client
15+
3. Messages sent from the diode's UDP client will be printed from the screen.
16+
4. The script repeats the receiving loop.
17+
"""
18+
19+
import socket
20+
21+
# Change this to the Target UDP Server IP Address in your diode's Output Side UDP Client Settings.
22+
targetUDPServerIP = "192.168.1.20"
23+
24+
# Change this to the Target UDP Server Port in your diode's Output Side UDP Client Settings.
25+
targetUDPServerPort = 503
26+
27+
# Set up a UDP server
28+
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
29+
server.bind((targetUDPServerIP, targetUDPServerPort))
30+
31+
# Receive and print data 10 Kbytes at a time, as long as the client is sending something
32+
try:
33+
while True:
34+
data, addr = server.recvfrom(10240)
35+
print(f"Received data from {addr}: {data}")
36+
37+
if not data:
38+
break
39+
finally:
40+
server.close()

0 commit comments

Comments
 (0)