# Path: conpot/utils/networking.py
# def get_interface_ip(destination_ip: str):
#     # returns interface ip from socket in case direct udp socket access not possible
#     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     s.connect((destination_ip, 80))
#     socket_ip = s.getsockname()[0]
#     s.close()
#     return socket_ip


import sys
import logging
import random
import pysnmp.smi.error
import gevent
import conpot.core as conpot_core
from pysnmp.entity.rfc3413 import cmdrsp
from pysnmp.proto import error
from pysnmp.proto.api import v2c
from pysnmp import debug
from conpot.utils.networking import get_interface_ip


logger = logging.getLogger(__name__)


class conpot_extension(object):
    def _getStateInfo(self, snmpEngine, stateReference):
        for _, v in list(snmpEngine.messageProcessingSubsystems.items()):
            if stateReference in v._cache.__dict__["_Cache__stateReferenceIndex"]:
                state_dict = v._cache.__dict__["_Cache__stateReferenceIndex"][
                    stateReference
                ][0]

        addr = state_dict["transportAddress"]

        # msgVersion 0/1 to SNMPv1/2, msgversion 3 corresponds to SNMPv3
        if state_dict["msgVersion"] < 3:
            snmp_version = state_dict["msgVersion"] + 1
        else:
            snmp_version = state_dict["msgVersion"]

        return addr, snmp_version

    def log(self, version, msg_type, addr, req_varBinds, res_varBinds=None, sock=None):
        session = conpot_core.get_session(

# Path: conpot/utils/networking.py
# def get_interface_ip(destination_ip: str):
#     # returns interface ip from socket in case direct udp socket access not possible
#     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     s.connect((destination_ip, 80))
#     socket_ip = s.getsockname()[0]
#     s.close()
#     return socket_ip


# Path: conpot/protocols/snmp/conpot_cmdrsp.py
import sys
import logging
import random
import pysnmp.smi.error
import gevent
import conpot.core as conpot_core
from pysnmp.entity.rfc3413 import cmdrsp
from pysnmp.proto import error
from pysnmp.proto.api import v2c
from pysnmp import debug
from conpot.utils.networking import get_interface_ip



logger = logging.getLogger(__name__)


class conpot_extension(object):
    def _getStateInfo(self, snmpEngine, stateReference):
        for _, v in list(snmpEngine.messageProcessingSubsystems.items()):
            if stateReference in v._cache.__dict__["_Cache__stateReferenceIndex"]:
                state_dict = v._cache.__dict__["_Cache__stateReferenceIndex"][
                    stateReference
                ][0]

        addr = state_dict["transportAddress"]

        # msgVersion 0/1 to SNMPv1/2, msgversion 3 corresponds to SNMPv3
        if state_dict["msgVersion"] < 3:
            snmp_version = state_dict["msgVersion"] + 1
        else:
            snmp_version = state_dict["msgVersion"]

        return addr, snmp_version

    def log(self, version, msg_type, addr, req_varBinds, res_varBinds=None, sock=None):
        session = conpot_core.get_session(

            "snmp", addr[0], addr[1], get_interface_ip(addr[0]), sock.getsockname()[1]