src/net/kad/rpc.c
/**
 * Processes the incoming message in `buf` and places the response, if any,
 * into the provided `rsp` buffer.
 */
Processes the incoming message in `buf` and places the response, if any,
into the provided `rsp` buffer.
Processes the incoming message in ` buf ` and places the response if any into the provided ` rsp ` buffer .
bool kad_rpc_handle(struct kad_ctx *ctx, const struct sockaddr_storage *addr,
                    const char buf[], const size_t slen, struct iobuf *rsp)
{
    struct kad_rpc_msg msg = {0};
    if (!benc_decode_rpc_msg(&msg, buf, slen)) {
        log_error("Invalid message received.");
        struct kad_rpc_msg rspmsg = {0};
        kad_rpc_error(&rspmsg, KAD_RPC_ERR_PROTOCOL, &msg, &ctx->routes->self_id);
        if (!benc_encode_rpc_msg(rsp, &rspmsg))
            log_error("Error while encoding error response.");
        return false;
    }
    kad_rpc_msg_log(&msg); 
    time_t now = 0;
    if (!now_sec(&now))
        return false;
    struct kad_node_info info = {.id=msg.node_id, .addr=*addr};
    sockaddr_storage_fmt(info.addr_str, addr);
    if (msg.node_id.is_set && !routes_upsert(ctx->routes, &info, now))
        log_warning("Routes update failed.");
    switch (msg.type) {
    case KAD_RPC_TYPE_NONE: {
        log_error("Got msg of type none.");
        return false;
    }
    case KAD_RPC_TYPE_ERROR: {
        return kad_rpc_handle_error(&msg);
    }
    case KAD_RPC_TYPE_QUERY: {  
        return kad_rpc_handle_query(ctx, &msg, rsp);
    }
    case KAD_RPC_TYPE_RESPONSE: {
        return kad_rpc_handle_response(ctx, &msg);
    }
    default:
        log_error("Unknown msg type.");
        return false;
    }
}
bool kad_rpc_handle ( struct kad_ctx * ctx , const struct sockaddr_storage * addr , const char buf [ ] , const size_t slen , struct iobuf * rsp ) { struct kad_rpc_msg msg = { 0 } ; if ( ! benc_decode_rpc_msg ( & msg , buf , slen ) ) { log_error ( " " ) ; struct kad_rpc_msg rspmsg = { 0 } ; kad_rpc_error ( & rspmsg , KAD_RPC_ERR_PROTOCOL , & msg , & ctx -> routes -> self_id ) ; if ( ! benc_encode_rpc_msg ( rsp , & rspmsg ) ) log_error ( " " ) ; return false ; } kad_rpc_msg_log ( & msg ) ; time_t now = 0 ; if ( ! now_sec ( & now ) ) return false ; struct kad_node_info info = { . id = msg . node_id , . addr = * addr } ; sockaddr_storage_fmt ( info . addr_str , addr ) ; if ( msg . node_id . is_set && ! routes_upsert ( ctx -> routes , & info , now ) ) log_warning ( " " ) ; switch ( msg . type ) { case KAD_RPC_TYPE_NONE : { log_error ( " " ) ; return false ; } case KAD_RPC_TYPE_ERROR : { return kad_rpc_handle_error ( & msg ) ; } case KAD_RPC_TYPE_QUERY : { return kad_rpc_handle_query ( ctx , & msg , rsp ) ; } case KAD_RPC_TYPE_RESPONSE : { return kad_rpc_handle_response ( ctx , & msg ) ; } default : log_error ( " " ) ; return false ; } }
Processes the incoming message in `buf` and places the response, if any,
 into the provided `rsp` buffer.
Processes the incoming message in ` buf ` and places the response if any into the provided ` rsp ` buffer .
// TESTING /* We'll respond immediately */