예제 #1
0
def set_self_id(mptn_id):
    global self_id_net_endian_string
    try:
        self_id_net_endian_string = struct.pack("!L", socket.htonl(mptn_id))
    except Exception as e:
        logger.error("set_self_id unknown error: %s\n%s" %
                     (str(e), traceback.format_exc()))
예제 #2
0
def socket_send(context, dest_id, message, expect_reply=False):
    if dest_id is None:
        logger.error("socket_send dest ID %s is not valid" %
                     ID_TO_STRING(dest_id))
        return None

    if context is not None and context.direction == ONLY_FROM_TCP_SERVER and context.id == dest_id:
        # logger.debug("socket_send reuses socket since context ID is the same as dest_id %s" % str(dest_id))
        next_hop_id = dest_id
        address = context.address
        sock = context.socket
        nonce = context.nonce

    else:
        next_hop = find_nexthop_for_id(dest_id)
        # logger.debug("socket_send next_hop is %s" % str(next_hop))
        if next_hop is None:
            logger.error(
                "socket_send next hop for dest ID %s 0x%X cannot be found" %
                (ID_TO_STRING(dest_id), dest_id))
            return None

        next_hop_id = next_hop.id
        address = next_hop.tcp_address

        sock = ConnectionManager.init().get_peer_by_id(next_hop_id)
        nonce = os.urandom(MPTN_TCP_NONCE_SIZE)

    if sock is None:
        # logger.debug("socket_send no socket found for ID %s"%ID_TO_STRING(next_hop_id))
        sock = reconnect(address)
        if sock is None:
            # logger.error("socket_send cannot re-setup socket for next_hop_id=%s addr=%s msg is\n%s" % (ID_TO_STRING(next_hop_id), str(address), formatted_print(split_packet_to_list(message))))
            return
        try:
            sock.send(self_id_net_endian_string)
        except Exception as e:
            logger.error(
                "socket_send self_id_net_endian_string error=%s. addr=%s, self_id_net_endian_string=%s, nonce=%s, message is\n%s\nerror=%s\n%s"
                % (str(address), ID_TO_STRING(self_id_net_endian_string),
                   str(map(ord, nonce)),
                   str(formatted_print(split_packet_to_list(message))), str(e),
                   traceback.format_exc()))
            return
        gevent.spawn(socket_recv, sock, address, next_hop_id)
        ConnectionManager.init().add_peer(
            address, Peer(socket=sock, id=next_hop_id, address=address))
        gevent.sleep(0)

    # logger.debug("socket_send message %s to ID %s" % (str(message), ID_TO_STRING(next_hop_id)))
    size = 0
    try:
        sock.send(nonce)
        size = struct.pack("!L", socket.htonl(len(message)))
        sock.send(size)
        sock.sendall(message)
    except Exception as e:
        logger.error(
            "socket_send nonce addr=%s, self_id_net_endian_string=%s, nonce=%s, message is\n%s\nerror=%s\n%s"
            % (str(address), ID_TO_STRING(self_id_net_endian_string),
               str(map(ord, nonce)),
               str(formatted_print(split_packet_to_list(message))), str(e),
               traceback.format_exc()))
        ConnectionManager.init().remove_peer(address)
        return None

    if not expect_reply: return None

    callback = AsyncResult()
    ConnectionManager.init().add_nonce(nonce, NonceCallback(dest_id, callback))

    if context is None:
        while not callback.ready():
            gevent.sleep(0)

    return callback.get()
예제 #3
0
 def int_to_ip(self, int_ip):
     return socket.inet_ntoa(struct.pack('I', socket.htonl(int_ip)))