def delta_test(self, mappings):
        """
        This function is designed to find the most commonly occurring
        difference between a set of numbers given a predefined margin
        of error. Its complexity is due to the fact that it allows
        for port collisions which may occur during NAT mapping.
        Its therefore more fault tolerant than simply considering
        the difference between two numbers and hence more accurate
        at determining a delta type NAT.
        """

        # Calculate differences.
        mapping_no = len(mappings)
        differences = []
        for i in range(0, mapping_no):
            # Overflow.
            if i + 1 >= mapping_no:
                break
            differences.append(mappings[i + 1]["remote"] -
                               mappings[i]["remote"])
        differences = list(set(differences))

        # Record delta pattern results.
        delta = 0
        for difference in differences:
            """
            Calculate matches relative to each number for each difference.
            The matches are relative to mappings[i]
            """
            masked = []
            for i in range(0, mapping_no):
                matches = 0
                for j in range(0, mapping_no):
                    # This is ourself.
                    if i == j:
                        continue

                    # Use value of mappings[i] to derive test value
                    # for mappings[j].
                    if i > j:
                        # How many bellow it?
                        test_val = mappings[i]["remote"] -\
                                   (difference * (i - j))
                    else:
                        # How many above it?
                        test_val = mappings[i]["remote"] +\
                                   (difference * (j - i))

                    # Pattern was predicted for relative comparison so
                    # increment matches.
                    if test_val == mappings[j]["remote"]:
                        matches += 1

                # Matches parses the minimum threshold so these don't count
                # as collisions.
                if matches + 1 > self.port_collisions:
                    masked.append(mappings[i]["remote"])

            # Check number of collisions satisfies delta requirement.
            collision_no = mapping_no - len(masked)
            if collision_no > self.port_collisions:
                continue
            if collision_no == int(mapping_no) / 2 and not mapping_no % 2:
                """
                This means there's no way to be sure. The number of
                collisions can be just as high as the number of
                "successes", in which case it's a stalemate.
                """
                continue
            delta = difference
            break

        if delta:
            nat_type = "delta"
        else:
            nat_type = "random"

        ret = {
            "nat_type": nat_type,
            "delta": delta
        }

        return ret
def delta_test ( self , mappings ) : mapping_no = len ( mappings ) differences = [ ] for i in range ( 0 , mapping_no ) : if i + 1 >= mapping_no : break differences . append ( mappings [ i + 1 ] [ "remote" ] - mappings [ i ] [ "remote" ] ) differences = list ( set ( differences ) ) delta = 0 for difference in differences : masked = [ ] for i in range ( 0 , mapping_no ) : matches = 0 for j in range ( 0 , mapping_no ) : if i == j : continue if i > j : test_val = mappings [ i ] [ "remote" ] - ( difference * ( i - j ) ) else : test_val = mappings [ i ] [ "remote" ] + ( difference * ( j - i ) ) if test_val == mappings [ j ] [ "remote" ] : matches += 1 if matches + 1 > self . port_collisions : masked . append ( mappings [ i ] [ "remote" ] ) collision_no = mapping_no - len ( masked ) if collision_no > self . port_collisions : continue if collision_no == int ( mapping_no ) / 2 and not mapping_no % 2 : continue delta = difference break if delta : nat_type = "delta" else : nat_type = "random" ret = { "nat_type" : nat_type , "delta" : delta } return ret
This function is designed to find the most commonly occurring
        difference between a set of numbers given a predefined margin
        of error. Its complexity is due to the fact that it allows
        for port collisions which may occur during NAT mapping.
        Its therefore more fault tolerant than simply considering
        the difference between two numbers and hence more accurate
        at determining a delta type NAT.