static bool_t libnfs_rpc_msg(struct rpc_context *rpc, ZDR *zdrs, struct rpc_msg *msg) { int ret; if (!libnfs_zdr_u_int(zdrs, &msg->xid)) { rpc_set_error(rpc, "libnfs_rpc_msg failed to decode XID"); return FALSE; } if (!libnfs_zdr_u_int(zdrs, &msg->direction)) { rpc_set_error(rpc, "libnfs_rpc_msg failed to decode DIRECTION"); return FALSE; } switch (msg->direction) { case CALL: ret = libnfs_rpc_call_body(rpc, zdrs, &msg->body.cbody); if (!ret) { rpc_set_error(rpc, "libnfs_rpc_msg failed to encode " "CALL, ret=%d: %s", ret, rpc_get_error(rpc)); } return ret; case REPLY: ret = libnfs_rpc_reply_body(rpc, zdrs, &msg->body.rbody); if (!ret) { rpc_set_error(rpc, "libnfs_rpc_msg failed to decode " "REPLY, ret=%d: %s", ret, rpc_get_error(rpc)); } return ret; default: rpc_set_error(rpc, "libnfs_rpc_msg failed to decode. " "Neither CALL not REPLY"); return FALSE; } }
static int rpc_send_error_reply(struct rpc_context *rpc, struct rpc_msg *call, enum accept_stat err, int min_vers, int max_vers) { struct rpc_pdu *pdu; struct rpc_msg res; assert(rpc->magic == RPC_CONTEXT_MAGIC); memset(&res, 0, sizeof(struct rpc_msg)); res.xid = call->xid; res.direction = REPLY; res.body.rbody.stat = MSG_ACCEPTED; res.body.rbody.reply.areply.reply_data.mismatch_info.low = min_vers; res.body.rbody.reply.areply.reply_data.mismatch_info.high = max_vers; res.body.rbody.reply.areply.verf = _null_auth; res.body.rbody.reply.areply.stat = err; if (rpc->is_udp) { /* send the reply back to the client */ memcpy(&rpc->udp_dest, &rpc->udp_src, sizeof(rpc->udp_dest)); } pdu = rpc_allocate_reply_pdu(rpc, &res, 0); if (pdu == NULL) { rpc_set_error(rpc, "Failed to send error_reply: %s", rpc_get_error(rpc)); return -1; } rpc_queue_pdu(rpc, pdu); return 0; }
int rpc_pmap_callit_async(struct rpc_context *rpc, int program, int version, int procedure, const char *data, int datalen, rpc_cb cb, void *private_data) { struct rpc_pdu *pdu; struct pmap_call_args ca; pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP_CALLIT, cb, private_data, (xdrproc_t)xdr_pmap_call_result, sizeof(pmap_call_result)); if (pdu == NULL) { rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for portmap/callit call"); return -1; } ca.prog = program; ca.vers = version; ca.proc = procedure; ca.args.args_len = datalen; ca.args.args_val = data; if (xdr_pmap_call_args(&pdu->xdr, &ca) == 0) { rpc_set_error(rpc, "XDR error: Failed to encode data for portmap/callit call"); rpc_free_pdu(rpc, pdu); return -1; } if (rpc_queue_pdu(rpc, pdu) != 0) { rpc_set_error(rpc, "Failed to queue portmap/callit pdu: %s", rpc_get_error(rpc)); return -1; } return 0; }
struct rpc_pdu *rpc_allocate_pdu2(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_decode_bufsize, size_t alloc_hint) { struct rpc_pdu *pdu; struct rpc_msg msg; int pdu_size; assert(rpc->magic == RPC_CONTEXT_MAGIC); /* Since we already know how much buffer we need for the decoding * we can just piggyback in the same alloc as for the pdu. */ pdu_size = PAD_TO_8_BYTES(sizeof(struct rpc_pdu)); pdu_size += PAD_TO_8_BYTES(zdr_decode_bufsize); pdu = malloc(pdu_size); if (pdu == NULL) { rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); return NULL; } memset(pdu, 0, pdu_size); pdu->xid = rpc->xid++; pdu->cb = cb; pdu->private_data = private_data; pdu->zdr_decode_fn = zdr_decode_fn; pdu->zdr_decode_bufsize = zdr_decode_bufsize; pdu->outdata.data = malloc(ZDR_ENCODEBUF_MINSIZE + alloc_hint); if (pdu->outdata.data == NULL) { rpc_set_error(rpc, "Out of memory: Failed to allocate encode buffer"); free(pdu); return NULL; } zdrmem_create(&pdu->zdr, pdu->outdata.data, ZDR_ENCODEBUF_MINSIZE + alloc_hint, ZDR_ENCODE); if (rpc->is_udp == 0) { zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */ } memset(&msg, 0, sizeof(struct rpc_msg)); msg.xid = pdu->xid; msg.direction = CALL; msg.body.cbody.rpcvers = RPC_MSG_VERSION; msg.body.cbody.prog = program; msg.body.cbody.vers = version; msg.body.cbody.proc = procedure; msg.body.cbody.cred = rpc->auth->ah_cred; msg.body.cbody.verf = rpc->auth->ah_verf; if (zdr_callmsg(rpc, &pdu->zdr, &msg) == 0) { rpc_set_error(rpc, "zdr_callmsg failed with %s", rpc_get_error(rpc)); zdr_destroy(&pdu->zdr); free(pdu->outdata.data); free(pdu); return NULL; } return pdu; }
static int rpc_process_call(struct rpc_context *rpc, ZDR *zdr) { struct rpc_msg call; struct rpc_endpoint *endpoint; int i, min_version = 0, max_version = 0, found_program = 0; assert(rpc->magic == RPC_CONTEXT_MAGIC); memset(&call, 0, sizeof(struct rpc_msg)); if (zdr_callmsg(rpc, zdr, &call) == 0) { rpc_set_error(rpc, "Failed to decode CALL message. %s", rpc_get_error(rpc)); return rpc_send_error_reply(rpc, &call, GARBAGE_ARGS, 0, 0); } for (endpoint = rpc->endpoints; endpoint; endpoint = endpoint->next) { if (call.body.cbody.prog == endpoint->program) { if (!found_program) { min_version = max_version = endpoint->version; } if (endpoint->version < min_version) { min_version = endpoint->version; } if (endpoint->version > max_version) { max_version = endpoint->version; } found_program = 1; if (call.body.cbody.vers == endpoint->version) { break; } } } if (endpoint == NULL) { rpc_set_error(rpc, "No endpoint found for CALL " "program:0x%08x version:%d\n", call.body.cbody.prog, call.body.cbody.vers); if (!found_program) { return rpc_send_error_reply(rpc, &call, PROG_UNAVAIL, 0, 0); } return rpc_send_error_reply(rpc, &call, PROG_MISMATCH, min_version, max_version); } for (i = 0; i < endpoint->num_procs; i++) { if (endpoint->procs[i].proc == call.body.cbody.proc) { if (endpoint->procs[i].decode_buf_size) { call.body.cbody.args = zdr_malloc(zdr, endpoint->procs[i].decode_buf_size); } if (!endpoint->procs[i].decode_fn(zdr, call.body.cbody.args)) { rpc_set_error(rpc, "Failed to unmarshall " "call payload"); return rpc_send_error_reply(rpc, &call, GARBAGE_ARGS, 0 ,0); } return endpoint->procs[i].func(rpc, &call); } } return rpc_send_error_reply(rpc, &call, PROC_UNAVAIL, 0 ,0); }
static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *zdr) { struct rpc_msg msg; assert(rpc->magic == RPC_CONTEXT_MAGIC); memset(&msg, 0, sizeof(struct rpc_msg)); msg.body.rbody.reply.areply.verf = _null_auth; if (pdu->zdr_decode_bufsize > 0) { pdu->zdr_decode_buf = (char *)pdu + PAD_TO_8_BYTES(sizeof(struct rpc_pdu)); } msg.body.rbody.reply.areply.reply_data.results.where = pdu->zdr_decode_buf; msg.body.rbody.reply.areply.reply_data.results.proc = pdu->zdr_decode_fn; if (zdr_replymsg(rpc, zdr, &msg) == 0) { rpc_set_error(rpc, "zdr_replymsg failed in rpc_process_reply: " "%s", rpc_get_error(rpc)); pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); if (pdu->zdr_decode_buf != NULL) { pdu->zdr_decode_buf = NULL; } return 0; } if (msg.body.rbody.stat != MSG_ACCEPTED) { pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); return 0; } switch (msg.body.rbody.reply.areply.stat) { case SUCCESS: pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->zdr_decode_buf, pdu->private_data); break; case PROG_UNAVAIL: pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); break; case PROG_MISMATCH: pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); break; case PROC_UNAVAIL: pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); break; case GARBAGE_ARGS: pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); break; case SYSTEM_ERR: pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); break; default: pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); break; } return 0; }
void mount_export_cb(struct rpc_context *mount_context, int status, void *data, void *private_data) { struct client *client = private_data; exports export = *(exports *)data; if (status < 0) { printf("MOUNT/EXPORT failed with \"%s\"\n", rpc_get_error(mount_context)); exit(10); } printf("Got exports list from server %s\n", client->server); while (export != NULL) { printf("Export: %s\n", export->ex_dir); export = export->ex_next; }
static struct rpc_pdu *rpc_allocate_reply_pdu(struct rpc_context *rpc, struct rpc_msg *res, size_t alloc_hint) { struct rpc_pdu *pdu; assert(rpc->magic == RPC_CONTEXT_MAGIC); pdu = malloc(sizeof(struct rpc_pdu)); if (pdu == NULL) { rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); return NULL; } memset(pdu, 0, sizeof(struct rpc_pdu)); pdu->flags = PDU_DISCARD_AFTER_SENDING; pdu->xid = 0; pdu->cb = NULL; pdu->private_data = NULL; pdu->zdr_decode_fn = NULL; pdu->zdr_decode_bufsize = 0; pdu->outdata.data = malloc(ZDR_ENCODEBUF_MINSIZE + alloc_hint); if (pdu->outdata.data == NULL) { rpc_set_error(rpc, "Out of memory: Failed to allocate encode buffer"); free(pdu); return NULL; } zdrmem_create(&pdu->zdr, pdu->outdata.data, ZDR_ENCODEBUF_MINSIZE + alloc_hint, ZDR_ENCODE); if (rpc->is_udp == 0) { zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */ } if (zdr_replymsg(rpc, &pdu->zdr, res) == 0) { rpc_set_error(rpc, "zdr_replymsg failed with %s", rpc_get_error(rpc)); zdr_destroy(&pdu->zdr); free(pdu->outdata.data); free(pdu); return NULL; } return pdu; }
int rpc_send_reply(struct rpc_context *rpc, struct rpc_msg *call, void *reply, zdrproc_t encode_fn, int alloc_hint) { struct rpc_pdu *pdu; struct rpc_msg res; assert(rpc->magic == RPC_CONTEXT_MAGIC); memset(&res, 0, sizeof(struct rpc_msg)); res.xid = call->xid; res.direction = REPLY; res.body.rbody.stat = MSG_ACCEPTED; res.body.rbody.reply.areply.verf = _null_auth; res.body.rbody.reply.areply.stat = SUCCESS; res.body.rbody.reply.areply.reply_data.results.where = reply; res.body.rbody.reply.areply.reply_data.results.proc = encode_fn; if (rpc->is_udp) { /* send the reply back to the client */ memcpy(&rpc->udp_dest, &rpc->udp_src, sizeof(rpc->udp_dest)); } pdu = rpc_allocate_reply_pdu(rpc, &res, alloc_hint); if (pdu == NULL) { rpc_set_error(rpc, "Failed to send error_reply: %s", rpc_get_error(rpc)); return -1; } rpc_queue_pdu(rpc, pdu); return 0; }