I want to set up a fpolicy server, which I can connect to from ontap.
I have tried setting up one using python as shown below. But I am getting below error when ontap tries to connect to it.
Error -
Reason for FPolicy Server Disconnection: FPolicy server sent message in which didn't had correct length format marker.
FPolicy Server using Python :-
import socket
import struct
import xml.etree.ElementTree as ET
def start_fpolicy_server(host, port):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen(5)
print(f"FPolicy listen-only server started on {host}:{port}")
while True:
client_socket, addr = server_socket.accept()
print(f"Connection from {addr}")
handle_client(client_socket)
def handle_client(client_socket):
try:
while True:
data = client_socket.recv(4096)
if not data:
break
print(f"Received data: {data}")
# Process the handshake request
response = process_handshake_request(data)
if response:
print(f"Sending response: {response}")
client_socket.sendall(response)
finally:
client_socket.close()
def process_handshake_request(data):
try:
# Print the raw data for debugging
print("Raw data:", data)
# Split the data to separate the header and the body
parts = data.split(b'\n\n', 1)
if len(parts) != 2:
raise ValueError("Invalid data format: missing header or body")
header, body = parts
# Print the header and body for debugging
print("Header:", header)
print("Body:", body)
# Decode the XML body and remove the null character
body_str = body.decode('utf-8').rstrip('\x00')
# Print the decoded body for debugging
print("Decoded Body:", body_str)
# Parse the XML body
root = ET.fromstring(body_str)
if root.tag == 'Handshake':
session_id = root.find('SessionId').text
vs_uuid = root.find('VsUUID').text
policy_name = root.find('PolicyName').text
# Construct the handshake response
response_body = f'''<?xml version="1.0"?>
<Header>
<NotfType>NEGO_RESP</NotfType>
<ContentLen>0</ContentLen>
<DataFormat>XML</DataFormat>
</Header>
<?xml version="1.0"?>
<HandshakeResp>
<SessionId>{session_id}</SessionId>
<VsUUID>{vs_uuid}</VsUUID>
<PolicyName>{policy_name}</PolicyName>
<ProtVersion>3.0</ProtVersion>
</HandshakeResp>'''
# Encode the response body
response_body_bytes = response_body.encode('utf-8')
# Calculate the length of the response body
response_length = len(response_body_bytes) #+ 4 # Include the length of the length prefix itself
# Construct the length prefix (4 bytes, network byte order)
length_prefix = struct.pack('!I', response_length)
# Combine the length prefix and the response body
response = length_prefix + response_body_bytes
return response
except Exception as e:
print(f"Error processing handshake request: {e}")
return None
if __name__ == "__main__":
start_fpolicy_server("0.0.0.0", 9000)