Exemplo n.º 1
0
 def run(self):
     while True:
         try:
             receive(self.conn, PACKAGE_LEN)
         except ConnectionClosedException:
             break  # Error is expected as client closes connection wh done
         send(self.conn, ACK_MSG)
Exemplo n.º 2
0
    def run_proxy_ping(self):
        """
        Executes proxy ping count times and prints output
        """

        # handshaking
        send(self.sock, PROXY_PING)

        format_count = '0' + str(COUNT_LEN) + 'd'
        send(self.sock, format(self.count, format_count))

        format_dest_addr = '0' + str(DEST_ADDR_LEN) + 'd'
        send(self.sock, format(len(self.proxy_address), format_dest_addr))
        send(self.sock, self.proxy_address)

        sig = receive(self.sock, ACK_LEN)
        if sig != ACK_MSG:
            print("Unable to connect to proxy server", file=sys.stderr)
            return

        self.out_mgr.print_file_version()
        self.out_mgr.print_operation(False, False, True)
        self.out_mgr.print_server(self.dest_address)
        self.out_mgr.print_client(self.address)

        packet_loss = 0
        rtt_list = []
        for i in range(self.count):
            _sig = receive(self.sock, ACK_LEN)
            if _sig != ACK_MSG:
                print("Connection between server and proxy ended " +
                      "unexpectedly",
                      file=sys.stderr)
                return

            rtt = float(receive(self.sock, RTT_LEN))

            if rtt == DISCARDED_PCK_RTT:
                packet_loss += 1
                continue

            rtt_list.append(rtt)
            if self.verbose:
                self.out_mgr.print_latest_message(PACKAGE_LEN,
                                                  self.dest_address, i + 1,
                                                  rtt)

        self.out_mgr.print_statistics(self.dest_address, self.count,
                                      self.count - packet_loss, rtt_list)
Exemplo n.º 3
0
    def run(self):
        while True:
            conn, addr = self.sock.accept()
            if not conn:
                print("Server socket closed", file=sys.stderr)
                break

            try:
                ping_type = receive(conn, PING_TYPE_LEN)
                if ping_type == DIRECT_PING:
                    print("Running Direct Ping")
                    DirectPing(conn).run()
                elif ping_type == REVERSE_PING:
                    print("Running Reverse Ping")
                    ReversePing(conn).run()
                elif ping_type == PROXY_PING:
                    print("Running Proxy Ping")
                    ProxyPing(conn).run()
                print("DONE!")
            except ConnectionClosedException as e:
                print(f"ERROR: {str(e)}")
            finally:
                conn.close()

        self.sock.close()
Exemplo n.º 4
0
    def run(self):
        for i in range(self.count):
            initial_dt = dt.now()
            send(self.conn, get_random_string(PACKAGE_LEN))
            receive(self.conn, ACK_LEN)
            final_dt = dt.now()

            delta = final_dt - initial_dt
            if delta.seconds >= TIMEOUT_SECONDS:
                send(self.conn, str(DISCARDED_PCK_RTT))
                continue

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)
            delta = "{:07.1f}".format(delta)
            send(self.conn, delta)
        self.conn.close()
Exemplo n.º 5
0
    def run_direct_ping(self):
        """
        Executes direct ping count times and prints output
        """

        # handshaking
        send(self.sock, DIRECT_PING)
        rcv = receive(self.sock, ACK_LEN)
        if rcv != ACK_MSG:
            raise ValueError(
                str.format("Expected %s, received %s", (ACK_MSG, rcv)))

        self.out_mgr.print_file_version()
        self.out_mgr.print_operation(True, False, False)
        self.out_mgr.print_server(self.dest_address)
        self.out_mgr.print_client(self.address)

        # direct ping
        packet_loss = 0
        rtt_list = []
        for i in range(self.count):
            before = dt.now()
            send(self.sock, get_random_string(PACKAGE_LEN))
            rcv = receive(self.sock, ACK_LEN)
            delta = dt.now() - before

            if rcv != ACK_MSG:
                raise ValueError(f"Expected {ACK_MSG}, received {rcv}")

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)

            if delta >= (TIMEOUT_SECONDS * 1000):
                packet_loss += 1
                continue

            rtt_list.append(delta)
            if self.verbose:
                self.out_mgr.print_latest_message(PACKAGE_LEN,
                                                  self.dest_address, i + 1,
                                                  delta)

        self.out_mgr.print_statistics(self.dest_address, self.count,
                                      self.count - packet_loss, rtt_list)
Exemplo n.º 6
0
    def run_reverse_ping(self):
        """
        Executes reverse ping count times and prints output
        """

        # handshaking
        send(self.sock, REVERSE_PING)
        sig = receive(self.sock, ACK_LEN)
        if sig != ACK_MSG:
            raise ValueError(
                str.format("Expected %s, received %s", (ACK_MSG, sig)))

        _format = '0' + str(COUNT_LEN) + 'd'
        send(self.sock, format(self.count, _format))

        self.out_mgr.print_file_version()
        self.out_mgr.print_operation(False, True, False)
        self.out_mgr.print_server(self.dest_address)
        self.out_mgr.print_client(self.address)

        packet_loss = 0
        rtt_list = []

        for i in range(self.count):
            try:
                receive(self.sock, PACKAGE_LEN)
                send(self.sock, ACK_MSG)
                rtt = float(receive(self.sock, RTT_LEN))

                if rtt == DISCARDED_PCK_RTT:
                    packet_loss += 1
                    continue

                rtt_list.append(rtt)
                if self.verbose:
                    self.out_mgr.print_latest_message(PACKAGE_LEN,
                                                      self.dest_address, i + 1,
                                                      rtt)
            except ConnectionClosedException:
                break

        self.out_mgr.print_statistics(self.dest_address, self.count,
                                      self.count - packet_loss, rtt_list)
Exemplo n.º 7
0
    def __init__(self, conn):
        self.client_conn = conn

        self.count = int(receive(conn, COUNT_LEN))

        addr_len = int(receive(conn, DEST_ADDR_LEN))
        dest_addr_split = receive(conn, addr_len).split(':')
        dest_addr = (dest_addr_split[0], int(dest_addr_split[1]))

        # create socket and connect to proxy
        self.proxy_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # handshaking with proxy sv to begin direct ping
        try:
            self.proxy_conn.connect(dest_addr)
            send(self.proxy_conn, DIRECT_PING)
            receive(self.proxy_conn, ACK_LEN)
        except (gaierror, ConnectionRefusedError, ConnectionClosedException) \
                as e:
            send(self.client_conn, ERR_MSG)
            raise ConnectionClosedException(str(e))

        send(self.client_conn, ACK_MSG)
Exemplo n.º 8
0
    def run(self):
        for i in range(self.count):

            initial_dt = dt.now()

            try:
                send(self.proxy_conn, get_random_string(PACKAGE_LEN))
                receive(self.proxy_conn, ACK_LEN)
            except ConnectionClosedException as e:
                send(self.client_conn, ERR_MSG)
                raise ConnectionClosedException(str(e))

            final_dt = dt.now()
            send(self.client_conn, ACK_MSG)

            delta = final_dt - initial_dt
            if delta.seconds >= TIMEOUT_SECONDS:
                send(self.client_conn, str(DISCARDED_PCK_RTT))
                continue

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)
            delta = "{:07.1f}".format(delta)
            send(self.client_conn, delta)
Exemplo n.º 9
0
 def __init__(self, conn):
     send(conn, ACK_MSG)
     self.count = int(receive(conn, COUNT_LEN))
     self.conn = conn