<commit_before># Copyright 2014 ETH Zurich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod:`opaque_field` --- SCION Opaque fields
===========================================
"""
# Stdlib
import logging
import struct

class OpaqueFieldType(object):
    """
    Defines constants for the types of the opaque field (first byte of every
    opaque field, i.e. field).
    TODO describe here layout of Opaque Fields
    """
    # Types for HopOpaqueFields (7 MSB bits).
    NORMAL_OF = 0b0000000
    LAST_OF = 0b0010000  # indicates last hop OF on the half-path (TODO revise)
    PEER_XOVR = 0b0001000
    # Types for Info Opaque Fields (7 MSB bits).
    TDC_XOVR = 0b1000000
    NON_TDC_XOVR = 0b1100000
    INPATH_XOVR = 0b1110000
    INTRATD_PEER = 0b1111000
    INTERTD_PEER = 0b1111100
    TRC_OF = 0b11111111


class OpaqueField(object):
    """
    Base class for the different kinds of opaque fields in SCION.
    """
    LEN = 8

    def __init__(self):
        """
        Initialize an instance of the class OpaqueField.
        """
        self.info = 0  # TODO verify path.PathType in that context
        self.type = 0
        self.parsed = False
        self.raw = None

    def parse(self, raw):
        """
        Populates fields from a raw byte block.
        """
        pass

    def pack(self):
        """
        Returns opaque field as 8 byte binary string.
        """
        pass

    def is_regular(self):
        """
        Returns true if opaque field is regular, false otherwise.
        """
        return (self.info & (1 << 6) == 0)

    def is_continue(self):
        """
        Returns true if continue bit is set, false otherwise.
        """
        return not (self.info & (1 << 5) == 0)

    def is_xovr(self):
        """
        Returns true if crossover point bit is set, false otherwise.
        """
        return not (self.info & (1 << 4) == 0)

    def __str__(self):
        pass

    def __repr__(self):
        return self.__str__()

    # TODO test: one __eq__ breaks router when two SOFs in a path are identical
    def __eq__(self, other):
        if type(other) is type(self):
            return self.raw == other.raw
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)


class HopOpaqueField(OpaqueField):
    """
    Opaque field for a hop in a path of the SCION packet header.

    Each hop opaque field has a info (8 bits), expiration time (8 bits)
    ingress/egress interfaces (2 * 12 bits) and a MAC (24 bits) authenticating
    the opaque field.
    """
    MAC_LEN = 3  # MAC length in bytes.

    def __init__(self, raw=None):
        """
        Initialize an instance of the class HopOpaqueField.

        :param raw:
        :type raw:
        """
        OpaqueField.__init__(self)
        self.exp_time = 0
        self.ingress_if = 0
        self.egress_if = 0
        self.mac = b"\x00" * self.MAC_LEN
        if raw is not None:
            self.parse(raw)

    def parse(self, raw):
        """
        Populates fields from a raw byte block.
        """
        assert isinstance(raw, bytes)
        self.raw = raw
        dlen = len(raw)
        if dlen < self.LEN:
            logging.warning("HOF: Data too short for parsing, len: %u", dlen)
            return
        (self.info, self.exp_time) = struct.unpack("!BB", raw[:2])
        ifs = struct.unpack("!I", b'\0' + raw[2:5])[0]
        self.mac = raw[5:8]
        self.ingress_if = (ifs & 0x00FFF000) >> 12
        self.egress_if = ifs & 0x00000FFF
        self.parsed = True

    @classmethod
    def from_values(cls, exp_time, ingress_if=0, egress_if=0, mac=None):
        """
        Returns HopOpaqueField with fields populated from values.

        @param exp_time: Expiry time. An integer in the range [0,255]
        @param ingress_if: Ingress interface.
        @param egress_if: Egress interface.
        @param mac: MAC of ingress/egress interfaces' ID and timestamp.
        """
        hof = HopOpaqueField()
        hof.exp_time = exp_time
        hof.ingress_if = ingress_if
        hof.egress_if = egress_if
        if mac is None:
            mac = b"\x00" * cls.MAC_LEN
        hof.mac = mac
        return hof

    def pack(self):
        """
        Returns HopOpaqueField as 8 byte binary string.
        """
        ifs = (self.ingress_if << 12) | self.egress_if
        data = struct.pack("!BB", self.info, self.exp_time)
        data += struct.pack("!I", ifs)[1:]
        data += self.mac
        return data

    def __eq__(self, other):
        if type(other) is type(self):
            return (self.exp_time == other.exp_time and<commit_msg>Minor changes<commit_after># Copyright 2014 ETH Zurich
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:mod:`opaque_field` --- SCION Opaque fields
===========================================
"""
# Stdlib
import logging
import struct

class OpaqueFieldType(object):
    """
    Defines constants for the types of the opaque field (first byte of every
    opaque field, i.e. field).
    TODO describe here layout of Opaque Fields
    """
    # Types for HopOpaqueFields (7 MSB bits).
    NORMAL_OF = 0b0000000
    LAST_OF = 0b0010000  # indicates last hop OF on the half-path (TODO revise)
    PEER_XOVR = 0b0001000
    # Types for Info Opaque Fields (7 MSB bits).
    TDC_XOVR = 0b1000000
    NON_TDC_XOVR = 0b1100000
    INPATH_XOVR = 0b1110000
    INTRATD_PEER = 0b1111000
    INTERTD_PEER = 0b1111100
    TRC_OF = 0b11111111


class OpaqueField(object):
    """
    Base class for the different kinds of opaque fields in SCION.
    """
    LEN = 8

    def __init__(self):
        """
        Initialize an instance of the class OpaqueField.
        """
        self.info = 0  # TODO verify path.PathType in that context
        self.type = 0
        self.parsed = False
        self.raw = None

    def parse(self, raw):
        """
        Populates fields from a raw byte block.
        """
        pass

    def pack(self):
        """
        Returns opaque field as 8 byte binary string.
        """
        pass

    def is_regular(self):
        """
        Returns true if opaque field is regular, false otherwise.
        """
        return (self.info & (1 << 6) == 0)

    def is_continue(self):
        """
        Returns true if continue bit is set, false otherwise.
        """
        return not (self.info & (1 << 5) == 0)

    def is_xovr(self):
        """
        Returns true if crossover point bit is set, false otherwise.
        """
        return not (self.info & (1 << 4) == 0)

    def __str__(self):
        pass

    def __repr__(self):
        return self.__str__()

    # TODO test: one __eq__ breaks router when two SOFs in a path are identical
    def __eq__(self, other):
        if type(other) is type(self):
            return self.raw == other.raw
        else:
            return False

    def __ne__(self, other):
        return not self.__eq__(other)


class HopOpaqueField(OpaqueField):
    """
    Opaque field for a hop in a path of the SCION packet header.

    Each hop opaque field has a info (8 bits), expiration time (8 bits)
    ingress/egress interfaces (2 * 12 bits) and a MAC (24 bits) authenticating
    the opaque field.
    """
    MAC_LEN = 3  # MAC length in bytes.

    def __init__(self, raw=None):
        """
        Initialize an instance of the class HopOpaqueField.

        :param raw:
        :type raw:
        """
        OpaqueField.__init__(self)
        self.exp_time = 0
        self.ingress_if = 0
        self.egress_if = 0
        self.mac = b"\x00" * self.MAC_LEN
        if raw is not None:
            self.parse(raw)

    def parse(self, raw):
        """
        Populates fields from a raw byte block.
        """
        assert isinstance(raw, bytes)
        self.raw = raw
        dlen = len(raw)
        if dlen < self.LEN:
            logging.warning("HOF: Data too short for parsing, len: %u", dlen)
            return
        (self.info, self.exp_time) = struct.unpack("!BB", raw[:2])
        # A byte added as length of three bytes can't be unpacked
        (ifs,) = struct.unpack("!I", b'\0' + raw[2:5])
        self.mac = raw[5:8]
        self.ingress_if = (ifs & 0xFFF000) >> 12
        self.egress_if = ifs & 0x000FFF
        self.parsed = True

    @classmethod
    def from_values(cls, exp_time, ingress_if=0, egress_if=0, mac=None):
        """
        Returns HopOpaqueField with fields populated from values.

        @param exp_time: Expiry time. An integer in the range [0,255]
        @param ingress_if: Ingress interface.
        @param egress_if: Egress interface.
        @param mac: MAC of ingress/egress interfaces' ID and timestamp.
        """
        hof = HopOpaqueField()
        hof.exp_time = exp_time
        hof.ingress_if = ingress_if
        hof.egress_if = egress_if
        if mac is None:
            mac = b"\x00" * cls.MAC_LEN
        hof.mac = mac
        return hof

    def pack(self):
        """
        Returns HopOpaqueField as 8 byte binary string.
        """
        ifs = (self.ingress_if << 12) | self.egress_if
        data = struct.pack("!BB", self.info, self.exp_time)
        # Ingress and egress interface info is packed into three bytes 
        data += struct.pack("!I", ifs)[1:]
        data += self.mac
        return data

    def __eq__(self, other):
        if type(other) is type(self):
            return (self.exp_time == other.exp_time and
{'fraction_non_alphanumeric': 0.10260336906584992, 'fraction_numerical': 0.026703675344563554, 'mean_word_length': 2.1340731853629276, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2613811', 'n_tokens_mistral': 3432, 'n_tokens_neox': 3139, 'n_words': 1085}