<commit_before># Copyright (C) 2010-2013 Cuckoo Sandbox Developers.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.

import select
import socket
import threading

try:
    import pycares
    HAVE_CARES = True
except:
    HAVE_CARES = False

#try:
#    import gevent, gevent.socket
#    HAVE_GEVENT = True
#except:
HAVE_GEVENT = False


# these are used by all resolvers
DNS_TIMEOUT = 5
DNS_TIMEOUT_VALUE = ""

def set_timeout(value):
    global DNS_TIMEOUT
    DNS_TIMEOUT = value

def set_timeout_value(value):
    global DNS_TIMEOUT_VALUE
    DNS_TIMEOUT_VALUE = value


# standard gethostbyname in thread
# http://code.activestate.com/recipes/473878/
def with_timeout(func, args=(), kwargs={}):
    """This function will spawn a thread and run the given function
    using the args, kwargs and return the given default value if the
    timeout_duration is exceeded.
    """
    class ResultThread(threading.Thread):
        daemon = True
        def __init__(self):
            threading.Thread.__init__(self)
            self.result, self.error = None, None
        def run(self):
            try: self.result = func(*args, **kwargs)
            except Exception, e:
                self.error = e

    it = ResultThread()
    it.start()
    it.join(DNS_TIMEOUT)
    if it.isAlive():
        return DNS_TIMEOUT_VALUE
    else:
        if it.error: raise e
        return it.result

def resolve_thread(name):
    return with_timeout(gethostbyname, (name,))

def gethostbyname(name):
    try: ip = socket.gethostbyname(name)
    except socket.gaierror:
        ip = ""
    return ip


# C-ARES (http://c-ares.haxx.se/)
def resolve_cares(name):
    # create new c-ares channel
    careschan = pycares.Channel(timeout=DNS_TIMEOUT, tries=1)

    # if we don't get a response we return the default value
    result = Resultholder()
    result.value = DNS_TIMEOUT_VALUE

    def setresult_cb(res, error):
        # ignore error and just take first result ip (randomized anyway)
        if res and res.addresses: result.value = res.addresses[0]

    # resolve with cb<commit_msg>bugfix raise in dns<commit_after># Copyright (C) 2010-2013 Cuckoo Sandbox Developers.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.

import select
import socket
import threading

try:
    import pycares
    HAVE_CARES = True
except:
    HAVE_CARES = False

#try:
#    import gevent, gevent.socket
#    HAVE_GEVENT = True
#except:
HAVE_GEVENT = False


# these are used by all resolvers
DNS_TIMEOUT = 5
DNS_TIMEOUT_VALUE = ""

def set_timeout(value):
    global DNS_TIMEOUT
    DNS_TIMEOUT = value

def set_timeout_value(value):
    global DNS_TIMEOUT_VALUE
    DNS_TIMEOUT_VALUE = value


# standard gethostbyname in thread
# http://code.activestate.com/recipes/473878/
def with_timeout(func, args=(), kwargs={}):
    """This function will spawn a thread and run the given function
    using the args, kwargs and return the given default value if the
    timeout_duration is exceeded.
    """
    class ResultThread(threading.Thread):
        daemon = True
        def __init__(self):
            threading.Thread.__init__(self)
            self.result, self.error = None, None
        def run(self):
            try: self.result = func(*args, **kwargs)
            except Exception, e:
                self.error = e

    it = ResultThread()
    it.start()
    it.join(DNS_TIMEOUT)
    if it.isAlive():
        return DNS_TIMEOUT_VALUE
    else:
        if it.error: raise
        return it.result

def resolve_thread(name):
    return with_timeout(gethostbyname, (name,))

def gethostbyname(name):
    try: ip = socket.gethostbyname(name)
    except socket.gaierror:
        ip = ""
    return ip


# C-ARES (http://c-ares.haxx.se/)
def resolve_cares(name):
    # create new c-ares channel
    careschan = pycares.Channel(timeout=DNS_TIMEOUT, tries=1)

    # if we don't get a response we return the default value
    result = Resultholder()
    result.value = DNS_TIMEOUT_VALUE

    def setresult_cb(res, error):
        # ignore error and just take first result ip (randomized anyway)
        if res and res.addresses: result.value = res.addresses[0]

    # resolve with cb
{'fraction_non_alphanumeric': 0.08968401486988847, 'fraction_numerical': 0.007899628252788104, 'mean_word_length': 2.8959276018099547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1040093', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1374, 'n_words': 403}