updated logic for creating tunnel keys to use a consistent hashing method, since the builtin hash is not guaranteed in python3 as it was before in python2

This commit is contained in:
Blake Harnden 2019-06-20 13:22:20 -07:00
parent 338c3a1fa1
commit a5f26e664a
2 changed files with 20 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import select
import socket import socket
import threading import threading
from core import utils
from core.api.tlv import coreapi from core.api.tlv import coreapi
from core.nodes.base import CoreNodeBase, CoreNetworkBase from core.nodes.base import CoreNodeBase, CoreNetworkBase
from core.emulator.enumerations import ConfigDataTypes from core.emulator.enumerations import ConfigDataTypes
@ -387,12 +388,13 @@ class CoreBroker(object):
:return: tunnel key for the node pair :return: tunnel key for the node pair
:rtype: int :rtype: int
""" """
logging.debug("creating tunnel key for: %s, %s", n1num, n2num)
sid = self.session_id_master sid = self.session_id_master
if sid is None: if sid is None:
# this is the master session # this is the master session
sid = self.session.id sid = self.session.id
key = (sid << 16) ^ hash(n1num) ^ (hash(n2num) << 8) key = (sid << 16) ^ utils.hash(n1num) ^ (utils.hash(n2num) << 8)
return key & 0xFFFFFFFF return key & 0xFFFFFFFF
def addtunnel(self, remoteip, n1num, n2num, localnum): def addtunnel(self, remoteip, n1num, n2num, localnum):

View file

@ -3,6 +3,7 @@ Miscellaneous utility functions, wrappers around some subprocess procedures.
""" """
import fcntl import fcntl
import hashlib
import importlib import importlib
import inspect import inspect
import logging import logging
@ -17,6 +18,22 @@ from core import CoreCommandError
DEVNULL = open(os.devnull, "wb") DEVNULL = open(os.devnull, "wb")
def hash(value):
"""
Provide a consistent hash that can be used in place
of the builtin hash, that no longer behaves consistently
in python3.
:param str/int value: value to hash
:return: hash value
:rtype: int
"""
if isinstance(value, int):
value = str(value)
value = value.encode("utf-8")
return int(hashlib.sha256(value).hexdigest(), 16)
def _detach_init(): def _detach_init():
""" """
Fork a child process and exit. Fork a child process and exit.