def command(self, command, data):
        """Sends the C side a command.

        The flags can be found in SEASIDE.

        Args:
            command (int): The flag to send to the C side.
            data (str): Any additional data to send with the flag.

        TODO: Clean up the types / documentation.
        TODO: Remove the eval.
        """
        SEASIDE.send_SEASIDE(c_socket, c_socket_lock, int(command), eval(data))
    def upload_pcap_file(self, file_data):
        """Takes a pcap file, and sends it to the C side for sending.

        Args:
            file_data (File): A CherryPy representation of a file.

        TODO: Find a way to remove that NamedTemporaryFile middleman.
        """
        pcap_file = tempfile.NamedTemporaryFile()
        pcap_file.write(bytearray(file_data.file.read()))
        pcap_file.seek(0)
        packet = rdpcap(pcap_file.name)[0]
        packet = conversions.convert_packet_int_array(packet)
        SEASIDE.send_SEASIDE(c_socket, c_socket_lock, 0, packet)
    def load_pcap_file(self, filename):
        """Takes a filename, and attempts to load it from pcap_files/.

        Reads the files, takes the first packet that is specified in that
        file, and then sends it over to the C side for sending.

        Args:
            filename (str): Name of the file located in the pcap_files dir.
        """
        if filename == '':
            return
        try:
            packet = rdpcap('pcap_files/' + filename)[0]
        except IOError:
            print filename, "doesn't exist."
            return
        packet = conversions.convert_packet_int_array(packet)
        SEASIDE.send_SEASIDE(c_socket, c_socket_lock, 0, packet)
    def packet_config(self, packet_layers):
        """Takes the configured layers and sends it to the C side.

        Takes the packet_layers (in a JSON format, see configure_packet_layers
        above), turns it into a legitimate Scapy packet, and then sends it to
        the C side.

        Args:
            packet_layers (JSON): The packet layers in a string of JSON. See
                configure_packet_layers above for an example of what this would
                look like.
        """
        # If the user didn't even configure a packet, but just clicked the
        # "Done configuring" button like the silly user they are.
        if packet_layers == '[]':
            return
        # Turn it into a Scapy packet.
        packet = configure_packet_layers(packet_layers)
        # Turn it into the corresponding int array.
        packet = conversions.convert_packet_int_array(packet)
        # Send it to the C side, using the SEASIDE format.
        SEASIDE.send_SEASIDE(c_socket, c_socket_lock, 0, packet)
def user_interaction(lcd, lcd_lock, c_socket, c_socket_lock):
    """Uses the LCD screen to interact with the user.

    Waits for the user to press a button and then performs the corresponding
    action. While it is waiting, update_display_loop has control of the screen
    and is displaying the statistics screen. When the user enters a
    configuration option, that function takes control of the screen and 
    update_display_loop waits until it has released the lock to resume control.

    The SEASIDE protocol is used for communication between the C and Python
    programs. For more information about SEASIDE, see shared_files/SEASIDE.py.

    Args:
        lcd (LCD_Input_Wrapper object): the lcd screen to use.
        lcd_lock (RLock object): the lock associated with the screen.
        c_socket (socket object): the socket used for SEASIDE communication
        c_socket_lock (RLock object): the lock associated with the socket.

    """
    led_state = (0, 1, 0)  # (r, g, b) for the screen LED
    is_sending = False

    global packet

    delay_seconds, delay_useconds = 1, 0
    delay_bytes = conversions.convert_delay_bytes(delay_seconds,
                                                  delay_useconds)
    while True:
        if lcd.is_pressed(LCD.SELECT):  # Configure packet
            packet_temp = pgen.configure_packet(lcd, lcd_lock)
            if packet_temp is None:
                time.sleep(0.3)
                continue
            packet = conversions.convert_packet_int_array(packet_temp)

            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.DELAY.value, delay_bytes)
            time.sleep(1)  # TODO: This separates sends. Make it unnecessary.
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.PACKET.value, packet)
            led_state = (0, 1, 0)
        elif lcd.is_pressed(LCD.UP):  # Begin sending
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.START.value)
            is_sending = True
        elif lcd.is_pressed(LCD.DOWN):  # Stop sending
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.STOP.value)
            is_sending = False
        elif lcd.is_pressed(LCD.LEFT):  # Send single packet
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.SINGLE_PACKET.value)
            threaded_lcd.flash_led(lcd, lcd_lock, *led_state)
        elif lcd.is_pressed(LCD.RIGHT):  # Configure delay
            delay_seconds, delay_useconds = pgen.configure_delay(lcd, lcd_lock)
            delay_bytes = conversions.convert_delay_bytes(delay_seconds,
                                                          delay_useconds)
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.DELAY.value, delay_bytes)
            threaded_lcd.flash_led(lcd, lcd_lock, 0, 0, 1)
        if is_sending:  # Ensures the LED always stays the right color.
            threaded_lcd.lock_and_set_led_color(lcd, lcd_lock, *led_state)
        else:  # Turns it off if we've stopped sending
            threaded_lcd.lock_and_set_led_color(lcd, lcd_lock, 0, 0, 0)
        time.sleep(0.3)  # Prevents duplicate presses
def user_interaction(lcd, lcd_lock, c_socket, c_socket_lock):
    led_state = (0, 1, 0)
    is_sending = False

    global packet

    delay_seconds, delay_useconds = 1, 0

    delay_bytes = conversions.convert_delay_bytes(delay_seconds,
                                                  delay_useconds)
    while True:
        if lcd.is_pressed(LCD.SELECT):  # Configure packet
            packet_temp = pgen.configure_packet(lcd, lcd_lock)
            if packet_temp is None:
                time.sleep(0.3)
                continue
            packet = conversions.convert_packet_int_array(packet_temp)

            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.DELAY.value, delay_bytes)
            time.sleep(1)  # TODO: Fix needing this sleep function
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.PACKET.value, packet)
            led_state = (0, 1, 0)
        elif lcd.is_pressed(LCD.UP):  # Begin sending
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.START.value)
            is_sending = True
        elif lcd.is_pressed(LCD.RIGHT):  # Configure delay
            delay_seconds, delay_useconds = pgen.configure_delay(lcd, lcd_lock)
            delay_bytes = conversions.convert_delay_bytes(
                delay_seconds, delay_useconds)
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.DELAY.value, delay_bytes)
            threaded_lcd.flash_led(lcd, lcd_lock, 0, 0, 1)
        elif lcd.is_pressed(LCD.LEFT):  # Send single packet
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.SINGLE_PACKET.value)
            threaded_lcd.flash_led(lcd, lcd_lock, *led_state)
        elif lcd.is_pressed(LCD.DOWN):  # Stop sending
            SEASIDE.send_SEASIDE(c_socket, c_socket_lock,
                                 SEASIDE_FLAGS.STOP.value)
            is_sending = False

        if is_sending:
            threaded_lcd.lock_and_set_led_color(lcd, lcd_lock, *led_state)
        else:
            threaded_lcd.lock_and_set_led_color(lcd, lcd_lock, 0, 0, 0)
        time.sleep(0.3)
示例#7
0
 def load_pcap_file(self, filename):
     if filename == '':
         return
     packet = rdpcap('pcap_files/' + filename)[0]
     packet = conversions.convert_packet_int_array(packet)
     SEASIDE.send_SEASIDE(c_socket, c_socket_lock, 0, packet)