def get_ack_response(con): msg = tcpr_pb2.tcpr() connection = get_connection_peer(con.local_ip, con.remote_ip, con.local_port, con.remote_port) if connection is None: msg.get_ack_response.status = tcpr_pb2.FAILED_NOT_FOUND return msg.SerializeToString() msg.get_ack_response.current_ack = connection.local_seq msg.get_ack_response.status = tcpr_pb2.SUCCESS return msg.SerializeToString()
def create_list_response(): resp = tcpr_pb2.tcpr() for connection in connections: entry = tcpr_pb2.tcpr_set() entry.connection.local_ip = connection.local_addr entry.connection.remote_ip = connection.remote_addr entry.connection.local_port = connection.local_port entry.connection.remote_port = connection.remote_port entry.sack_enabled = connection.sack entry.max_segment_size = connection.mss entry.window_scaling = connection.ws resp.get_list_response.connections.extend([entry]) return resp.SerializeToString()
def get_init_response(con): msg = tcpr_pb2.tcpr() connection = get_connection_peer(con.local_ip, con.remote_ip, con.local_port, con.remote_port) if connection is None: msg.get_init_response.status = tcpr_pb2.FAILED_NOT_FOUND return msg.SerializeToString() msg.get_init_response.initial_seq = connection.initial_seq msg.get_init_response.sack_enabled = connection.sack msg.get_init_response.max_segment_size = connection.mss msg.get_init_response.window_scaling = connection.ws msg.get_init_response.status = tcpr_pb2.SUCCESS return msg.SerializeToString()
def menu(): global cons s = connect() p = tcpr_pb2.tcpr() print_menu() while True: x = input() if x == '1': for c in get_from_server(s): print_entry( c.connection, "mss: {}, ws: {}, sack: {}".format(c.max_segment_size, c.window_scaling, c.sack_enabled)) continue if x == '2': cons = get_from_server(s) continue if x == '3': for c in cons: print_entry( c.connection, "mss: {}, ws: {}, sack: {}".format(c.max_segment_size, c.window_scaling, c.sack_enabled)) continue if x == '4': for c in cons: p.set.CopyFrom(c) s.send(p.SerializeToString()) resp = tcpr_pb2.tcpr.FromString(s.recv()) msg = "SUCCESS" if resp.set_response.status == tcpr_pb2.SUCCESS else "FAILED" print_entry(c.connection, msg) continue print_menu()
def set_connections(set_msg): resp = tcpr_pb2.tcpr() con = set_msg.connection if get_old_connection(con.local_ip, con.remote_ip, con.remote_port) is not None: resp.set_response.status = tcpr_pb2.FAILED_EXISTS return resp.SerializeToString() connection = Connection(local_ip=con.local_ip, remote_ip=con.remote_ip, local_port=con.local_port, remote_port=con.remote_port, is_bind=False, mss=set_msg.max_segment_size, ws=set_msg.window_scaling, sack=set_msg.sack_enabled) connection.register() connection.recover() resp.set_response.status = tcpr_pb2.SUCCESS return resp.SerializeToString()
def get_from_server(s): p = tcpr_pb2.tcpr() p.get_list.SetInParent() s.send(p.SerializeToString()) resp = tcpr_pb2.tcpr.FromString(s.recv()) return resp.get_list_response.connections