# Copyright: (c) 2020, Jordan Borean (@jborean93) # MIT License (see LICENSE or https://opensource.org/licenses/MIT) import enum import struct import typing def _pack_value(addr_type: typing.Optional["AddressType"], b: typing.Optional[bytes]) -> bytes: """ Packs an type/data entry into the byte structure required. """ if not b: b = b"" return (struct.pack(" typing.Tuple[bytes, int]: """ Unpacks a raw C struct value to a byte string. """ length = struct.unpack("'. Args: initiator_addrtype: The address type of the initiator address. initiator_address: The initiator's address. acceptor_addrtype: The address type of the acceptor address. acceptor_address: The acceptor's address. application_data: Any extra application data to set on the bindings struct. """ def __init__( self, initiator_addrtype: AddressType = AddressType.unspecified, initiator_address: typing.Optional[bytes] = None, acceptor_addrtype: AddressType = AddressType.unspecified, acceptor_address: typing.Optional[bytes] = None, application_data: typing.Optional[bytes] = None, ) -> None: self.initiator_addrtype = AddressType(initiator_addrtype) self.initiator_address = initiator_address self.acceptor_addrtype = AddressType(acceptor_addrtype) self.acceptor_address = acceptor_address self.application_data = application_data def __repr__(self) -> str: return "{0}.{1} initiator_addrtype={2}|initiator_address={3}|acceptor_addrtype={4}|acceptor_address={5}|" \ "application_data={6}".format(type(self).__module__, type(self).__name__, repr(self.initiator_addrtype), repr(self.initiator_address), repr(self.acceptor_addrtype), repr(self.acceptor_address), repr(self.application_data)) def __str__(self) -> str: return "{0} initiator_addr({1}|{2!r}) | acceptor_addr({3}|{4!r}) | application_data({5!r})".format( type(self).__name__, str(self.initiator_addrtype), self.initiator_address, str(self.acceptor_addrtype), self.acceptor_address, self.application_data ) def __eq__(self, other: object) -> bool: if not isinstance(other, (bytes, GssChannelBindings)): return False if isinstance(other, GssChannelBindings): other = other.pack() return self.pack() == other def pack(self) -> bytes: """ Pack struct into a byte string. """ return b"".join([ _pack_value(self.initiator_addrtype, self.initiator_address), _pack_value(self.acceptor_addrtype, self.acceptor_address), _pack_value(None, self.application_data) ]) @staticmethod def unpack(b_data: bytes) -> "GssChannelBindings": b_mem = memoryview(b_data) initiator_addrtype = struct.unpack("