# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


from struct import pack, unpack
from conpot.protocols.s7comm.exceptions import ParseException
from conpot.utils.networking import str_to_bytes
import struct


class TPKT:
    # References: rfc2126 section-4.3, rfc1006# section-6
    # Packet format:
    # +--------+--------+----------------+-----------....---------------+
    # |version |reserved| packet length  |             TPDU             |
    # +----------------------------------------------....---------------+
    # <8 bits> <8 bits> <   16 bits    > <       variable length       >

    def __init__(self, version=3, payload=""):
        self.payload = payload
        self.version = version
        self.reserved = 0
        self.packet_length = len(payload) + 4

    def pack(self):
        return pack(
            "!BBH", self.version, self.reserved, self.packet_length

# Path: conpot/utils/networking.py
# def str_to_bytes(x):
#     return x if isinstance(x, bytes) else str(x).encode("ascii")


# Path: conpot/protocols/s7comm/tpkt.py
from struct import pack, unpack
from conpot.protocols.s7comm.exceptions import ParseException
from conpot.utils.networking import str_to_bytes
import struct



class TPKT:
    # References: rfc2126 section-4.3, rfc1006# section-6
    # Packet format:
    # +--------+--------+----------------+-----------....---------------+
    # |version |reserved| packet length  |             TPDU             |
    # +----------------------------------------------....---------------+
    # <8 bits> <8 bits> <   16 bits    > <       variable length       >

    def __init__(self, version=3, payload=""):
        self.payload = payload
        self.version = version
        self.reserved = 0
        self.packet_length = len(payload) + 4

    def pack(self):
        return pack(
            "!BBH", self.version, self.reserved, self.packet_length

        ) + str_to_bytes(self.payload)