<commit_before>#!/usr/bin/env python

# scapy.contrib.description = EIGRP
# scapy.contrib.status = loads

"""
    EIGRP Scapy Extension
    ~~~~~~~~~~~~~~~~~~~~~

    :version:   2009-08-13
    :copyright: 2009 by <NAME>
    :e-mail:    <EMAIL> / <EMAIL>
    :license:   GPL v2

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

    :TODO

    - Replace TLV code with a more generic solution
        * http://trac.secdev.org/scapy/ticket/90
    - Write function for calculating authentication data

    :Known bugs:

        -

    :Thanks:

    - TLV code derived from the CDP implementation of scapy. (Thanks to <NAME> and <NAME>)
        http://trac.secdev.org/scapy/ticket/18
    - IOS / EIGRP Version Representation FIX by <NAME>
"""

from scapy.packet import *
from scapy.fields import *
from scapy.layers.inet import IP
from scapy.layers.inet6 import *

class EigrpIPField(StrField, IPField):
    """
    This is a special field type for handling ip addresses of destination networks in internal and
    external route updates.

    EIGRP removes zeros from the host portion of the ip address if the netmask is 8, 16 or 24 bits.
    """

    def __init__(self, name, default, length=None, length_from=None):
        StrField.__init__(self, name, default)
        self.length_from  = length_from
        if length is not None:
            self.length_from = lambda pkt,length=length: length

    def h2i(self, pkt, x):
        return IPField.h2i(self, pkt, x)

    def i2m(self, pkt, x):
        x = inet_aton(x)
        l = self.length_from(pkt)

        if l <= 8:
            return x[:1]
        elif l <= 16:
            return x[:2]
        elif l <= 24:
            return x[:3]
        else:
            return x

    def m2i(self, pkt, x):
        l = self.length_from(pkt)

        if l <= 8:
            x += "\x00\x00\x00"
        elif l <= 16:
            x += "\x00\x00"
        elif l <= 24:
            x += "\x00"

        return inet_ntoa(x)

    def prefixlen_to_bytelen(self, l):
        if l <= 8:
            l = 1
        elif l <= 16:
            l = 2
        elif l <= 24:
            l = 3
        else:
            l = 4

        return l

    def i2len(self, pkt, x):
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return l

    def getfield(self, pkt, s):
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return s[l:], self.m2i(pkt, s[:l])

    def randval(self):
        return IPField.randval(self)

class EigrpIP6Field(StrField, IP6Field, EigrpIPField):
    """
    This is a special field type for handling ip addresses of destination networks in internal and
    external route updates.

    """

    def __init__(self, name, default, length=None, length_from=None):
        StrField.__init__(self, name, default)
        self.length_from  = length_from
        if length is not None:
            self.length_from = lambda pkt,length=length: length

    def any2i(self, pkt, x):
        return IP6Field.any2i(self, pkt, x)

    def i2repr(self, pkt, x):
        return IP6Field.i2repr(self, pkt, x)

    def h2i(self, pkt, x):
        return IP6Field.h2i(self, pkt, x)

    def i2m(self, pkt, x):
        x = inet_pton(socket.AF_INET6, x)
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return x[:l]

    def m2i(self, pkt, x):
        l = self.length_from(pkt)

        prefixlen = self.prefixlen_to_bytelen(l)
        if l > 128:
            warning("EigrpIP6Field: Prefix length is > 128. Dissection of this packet will fail")
        else:
            pad = "\x00" * (16 - prefixlen)
            x += pad

        return inet_ntop(socket.AF_INET6, x)

    def prefixlen_to_bytelen(self, l):
        l = l / 8

        if l < 16:
            l += 1

        return l

    def i2len(self, pkt, x):
        return EigrpIPField.i2len(self, pkt, x)

    def getfield(self, pkt, s):
        return EigrpIPField.getfield(self, pkt, s)


class EIGRPGeneric(Packet):
    name = "EIGRP Generic TLV"
    fields_desc = [ XShortField("type", 0x0000),
            FieldLenField("len", None, "value", "!H", adjust=lambda pkt,x: x + 4),
            StrLenField("value", "\x00", length_from=lambda pkt: pkt.len - 4)]

    def guess_payload_class(self, p):
        return conf.padding_layer

class EIGRPParam(EIGRPGeneric):
    name = "EIGRP Parameters"
    fields_desc = [ XShortField("type", 0x0001),
            ShortField("len", 12),
            # Bandwidth
            ByteField("k1", 1),
            # Load
            ByteField("k2", 0),
            # Delay
            ByteField("k3", 1),
            # Reliability
            ByteField("k4", 0),
            # MTU
            ByteField("k5", 0),
            ByteField("reserved", 0),
            ShortField("holdtime", 15)
            ]

class EIGRPAuthData(EIGRPGeneric):<commit_msg>EIGRP (contrib): fix multiple inheritance issue<commit_after>#!/usr/bin/env python

# scapy.contrib.description = EIGRP
# scapy.contrib.status = loads

"""
    EIGRP Scapy Extension
    ~~~~~~~~~~~~~~~~~~~~~

    :version:   2009-08-13
    :copyright: 2009 by <NAME>
    :e-mail:    <EMAIL> / <EMAIL>
    :license:   GPL v2

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

    :TODO

    - Replace TLV code with a more generic solution
        * http://trac.secdev.org/scapy/ticket/90
    - Write function for calculating authentication data

    :Known bugs:

        -

    :Thanks:

    - TLV code derived from the CDP implementation of scapy. (Thanks to <NAME> and <NAME>)
        http://trac.secdev.org/scapy/ticket/18
    - IOS / EIGRP Version Representation FIX by Dirk Loss
"""

from scapy.packet import *
from scapy.fields import *
from scapy.layers.inet import IP
from scapy.layers.inet6 import *

class EigrpIPField(StrField, IPField):
    """
    This is a special field type for handling ip addresses of destination networks in internal and
    external route updates.

    EIGRP removes zeros from the host portion of the ip address if the netmask is 8, 16 or 24 bits.
    """

    def __init__(self, name, default, length=None, length_from=None):
        StrField.__init__(self, name, default)
        self.length_from  = length_from
        if length is not None:
            self.length_from = lambda pkt,length=length: length

    def h2i(self, pkt, x):
        return IPField.h2i(self, pkt, x)

    def i2m(self, pkt, x):
        x = inet_aton(x)
        l = self.length_from(pkt)

        if l <= 8:
            return x[:1]
        elif l <= 16:
            return x[:2]
        elif l <= 24:
            return x[:3]
        else:
            return x

    def m2i(self, pkt, x):
        l = self.length_from(pkt)

        if l <= 8:
            x += "\x00\x00\x00"
        elif l <= 16:
            x += "\x00\x00"
        elif l <= 24:
            x += "\x00"

        return inet_ntoa(x)

    def prefixlen_to_bytelen(self, l):
        if l <= 8:
            l = 1
        elif l <= 16:
            l = 2
        elif l <= 24:
            l = 3
        else:
            l = 4

        return l

    def i2len(self, pkt, x):
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return l

    def getfield(self, pkt, s):
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return s[l:], self.m2i(pkt, s[:l])

    def randval(self):
        return IPField.randval(self)

class EigrpIP6Field(StrField, IP6Field):
    """
    This is a special field type for handling ip addresses of destination networks in internal and
    external route updates.

    """

    def __init__(self, name, default, length=None, length_from=None):
        StrField.__init__(self, name, default)
        self.length_from  = length_from
        if length is not None:
            self.length_from = lambda pkt,length=length: length

    def any2i(self, pkt, x):
        return IP6Field.any2i(self, pkt, x)

    def i2repr(self, pkt, x):
        return IP6Field.i2repr(self, pkt, x)

    def h2i(self, pkt, x):
        return IP6Field.h2i(self, pkt, x)

    def i2m(self, pkt, x):
        x = inet_pton(socket.AF_INET6, x)
        l = self.length_from(pkt)
        l = self.prefixlen_to_bytelen(l)

        return x[:l]

    def m2i(self, pkt, x):
        l = self.length_from(pkt)

        prefixlen = self.prefixlen_to_bytelen(l)
        if l > 128:
            warning("EigrpIP6Field: Prefix length is > 128. Dissection of this packet will fail")
        else:
            pad = "\x00" * (16 - prefixlen)
            x += pad

        return inet_ntop(socket.AF_INET6, x)

    def prefixlen_to_bytelen(self, l):
        l = l / 8

        if l < 16:
            l += 1

        return l

    def i2len(self, pkt, x):
        return EigrpIPField.i2len(self, pkt, x)

    def getfield(self, pkt, s):
        return EigrpIPField.getfield(self, pkt, s)

    def randval(self):
        return IP6Field.randval(self)

class EIGRPGeneric(Packet):
    name = "EIGRP Generic TLV"
    fields_desc = [ XShortField("type", 0x0000),
            FieldLenField("len", None, "value", "!H", adjust=lambda pkt,x: x + 4),
            StrLenField("value", "\x00", length_from=lambda pkt: pkt.len - 4)]

    def guess_payload_class(self, p):
        return conf.padding_layer

class EIGRPParam(EIGRPGeneric):
    name = "EIGRP Parameters"
    fields_desc = [ XShortField("type", 0x0001),
            ShortField("len", 12),
            # Bandwidth
            ByteField("k1", 1),
            # Load
            ByteField("k2", 0),
            # Delay
            ByteField("k3", 1),
            # Reliability
            ByteField("k4", 0),
            # MTU
            ByteField("k5", 0),
            ByteField("reserved", 0),
            ShortField("holdtime", 15)
            ]

class EIGRPAuthData(EIGRPGeneric):
{'fraction_non_alphanumeric': 0.10517560073937153, 'fraction_numerical': 0.023197781885397412, 'mean_word_length': 2.0083402835696416, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '7520707', 'n_tokens_mistral': 3653, 'n_tokens_neox': 3483, 'n_words': 1052}