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

"""
Start of TCP Fuzzer. Still very rough.
"""

import socket
import select
import signal
import time
import sys
import os
import constants as fuzz_constants

BUFSIZ = 1024

# Things to do:
# - Some way to take fuzz inputs rather than hard coding things
# - Save place when something causes a client to disconnect, subsequent
#   reconnects will try next item in fuzz_strings
# - Option to disable select(), only allow 1 connection at a time
# - expect abilities. example: client sends PASS*, server sends +OK
# - Docstrings

class FuzzTCPServer(object):
    """TCP Server object for thefuzz suite"""

    def __init__(self, bindaddr="0.0.0.0", port=4444, backlog=5):
        self.clients = 0
        self.clientmap = {}
        self.inputs = []
        self.outputs = []
        self.banner = None
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        # Validate IP address
        try:
            socket.inet_aton(bindaddr)
        except socket.error:
            print "[-] Invalid bindaddr: %s" % bindaddr
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        # Validate port number
        if port > 65535 or port < 1:
            print "[-] Invalid port number: %d" % port
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        # bind to port
        try:
            self.server.bind((bindaddr, port))
        except socket.error, exp:
            print "[-] Unable to bind to %s:%d: %s" % (bindaddr, port, exp)
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        print "[+] Listening on %s:%d" % (bindaddr, port)
        self.server.listen(backlog)

        signal.signal(signal.SIGINT, self.siginthandler)


    def add_connection(self, sock, address):
        """Add connection"""
        self.clients += 1
        self.inputs.append(sock)
        self.outputs.append(sock)
        self.clientmap[sock] = (address, str(sock.fileno()))


    def remove_connection(self, sock):
        """Cleanup"""
        self.clients -= 1
        sock.close()
        self.inputs.remove(sock)
        self.outputs.remove(sock)
        del self.clientmap[sock]


    def siginthandler(self, signum, frame):
        """Handle SIGINT"""
        print "[-] Caught Interrupt"

        for output in self.outputs:
            output.close()

        self.server.close()


    def getname(self, client):
        """Get name of client"""
        info = self.clientmap[client]
        host, name = info[0][0], info[1]
        return '@'.join((name, host))


    def send(self, sock, buf):
        """send data"""
        try:
            sock.send(buf)
        except socket.error, exc:<commit_msg>made getname() a bit more clear in what it is returning<commit_after>#!/usr/bin/env python

"""
Start of TCP Fuzzer. Still very rough.
"""

import socket
import select
import signal
import time
import sys
import os
import constants as fuzz_constants

BUFSIZ = 1024

# Things to do:
# - Some way to take fuzz inputs rather than hard coding things
# - Save place when something causes a client to disconnect, subsequent
#   reconnects will try next item in fuzz_strings
# - Option to disable select(), only allow 1 connection at a time
# - expect abilities. example: client sends PASS*, server sends +OK
# - Docstrings

class FuzzTCPServer(object):
    """TCP Server object for thefuzz suite"""

    def __init__(self, bindaddr="0.0.0.0", port=4444, backlog=5):
        self.clients = 0
        self.clientmap = {}
        self.inputs = []
        self.outputs = []
        self.banner = None
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        # Validate IP address
        try:
            socket.inet_aton(bindaddr)
        except socket.error:
            print "[-] Invalid bindaddr: %s" % bindaddr
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        # Validate port number
        if port > 65535 or port < 1:
            print "[-] Invalid port number: %d" % port
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        # bind to port
        try:
            self.server.bind((bindaddr, port))
        except socket.error, exp:
            print "[-] Unable to bind to %s:%d: %s" % (bindaddr, port, exp)
            print "[-] Exiting."
            sys.exit(os.EX_USAGE)

        print "[+] Listening on %s:%d" % (bindaddr, port)
        self.server.listen(backlog)

        signal.signal(signal.SIGINT, self.siginthandler)


    def add_connection(self, sock, address):
        """Add connection"""
        self.clients += 1
        self.inputs.append(sock)
        self.outputs.append(sock)
        self.clientmap[sock] = (address, str(sock.fileno()))


    def remove_connection(self, sock):
        """Cleanup"""
        self.clients -= 1
        sock.close()
        self.inputs.remove(sock)
        self.outputs.remove(sock)
        del self.clientmap[sock]


    def siginthandler(self, signum, frame):
        """Handle SIGINT"""
        print "[-] Caught Interrupt"

        for output in self.outputs:
            output.close()

        self.server.close()


    def getname(self, client):
        """Get name of client"""
        info = self.clientmap[client]
        host, socketno = info[0][0], info[1]
        return '@'.join((host, socketno))


    def send(self, sock, buf):
        """send data"""
        try:
            sock.send(buf)
        except socket.error, exc:
{'fraction_non_alphanumeric': 0.11963081292154774, 'fraction_numerical': 0.009584664536741214, 'mean_word_length': 2.163952835485682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4634001', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1668, 'n_words': 479}