Thanks Morag, that works and is cleaner.
Original Message:
Sent: Thu January 11, 2024 11:42 PM
From: Morag Hughson
Subject: pymqi and SSLPeerName
Hi Peter,
Looking at the way the PyMQI libraries implemented set_vs
for MQCHARV
structures, I copied that a little and came up with the setPeerName
function shown below to add to my own ssl.py
application. You need to import ctypes
to use it, and I already had the pyStr
function to get around the Python 2 v 3 string debacle - there's probably a better way to do that bit if you know which version of Python you're using, but it works for me so I've kinda stuck with it - keeps the rest of the code a little tidier and I think better in strings than bytes. Hopefully this makes sense but if it helps I can send you the whole sample application.
def pyStr(data):
if sys.version_info.major < 3:
return str(data)
else:
return bytes(data, 'utf-8')
def setPeerName(data):
c_value = ctypes.create_string_buffer(data)
c_value_p = ctypes.cast(c_value, ctypes.c_void_p).value
cd.SSLPeerNamePtr = c_value_p
cd.SSLPeerNameLength = len(data)
then later in my program I called:-
if (args.peername):
setPeerName(pyStr(args.peername))
Cheers,
Morag
------------------------------
Morag Hughson
MQ Technical Education Specialist
MQGem Software Limited
Website: https://www.mqgem.com
Original Message:
Sent: Thu January 11, 2024 08:16 PM
From: Peter Potkay
Subject: pymqi and SSLPeerName
Its been a struggle to get SSLPeerName working in a python app. We got it working doing the below. Seems like a lot of hoops to jump thru Is there a cleaner, simpler way? Yes, I know we could use a Client Channel Definition Table. Is there a better way to do it with code ahead of the MQCONNX call is the question.
# Create a bytes string variable that can be used in C code, since peer name functionality relies on raw C code
cPeerName = ctypes.c_char_p(b'CN=mqcnhx99.thehartford.com')
# Get the pointer (as a string) for the physical location of this C/Python variable in memory (memory address) ie: "c_char_p(1829825702992)"
cPeerNamePointer = str(ctypes.pointer(cPeerName).contents)
# Extract the integer value that the pointer has for the memory address of the C/Python variable. This is stored between "(" and ")"
# ie: c_char_p(1829825702992) extracts to ==> 1829825702992
cPeerNamePointerInt = int(cPeerNamePointer[str(cPeerNamePointer).index('(') + 1 : str(cPeerNamePointer).index(')')])
# Set the pymqi peer name value to the pointer's integer value. The C code will use this pointer to get the value of cPeerName in the MQ libraries from memory
cd.SSLPeerNamePtr = cPeerNamePointerInt
# Set the peer name length to match the value of cPeerName. Since the value of cPeerName is in bytes, decode it back to the string value
cd.SSLPeerNameLength = len(str(cPeerName.value, encoding='utf-8'))
------------------------------
Peter Potkay
------------------------------