| 1 | /* |
| 2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #ifdef WIN32 |
| 18 | #include "win32_compat.h" |
| 19 | #ifndef MSG_DONTWAIT |
| 20 | #define MSG_DONTWAIT 0 |
| 21 | #endif |
| 22 | #else |
| 23 | #include <strings.h> |
| 24 | #endif/*WIN32*/ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <assert.h> |
| 29 | #include <errno.h> |
| 30 | #include <rpc/rpc.h> |
| 31 | #include <rpc/xdr.h> |
| 32 | #include <rpc/rpc_msg.h> |
| 33 | #include "slist.h" |
| 34 | #include "libnfs.h" |
| 35 | #include "libnfs-raw.h" |
| 36 | #include "libnfs-private.h" |
| 37 | |
| 38 | struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, xdrproc_t xdr_decode_fn, int xdr_decode_bufsize) |
| 39 | { |
| 40 | struct rpc_pdu *pdu; |
| 41 | struct rpc_msg msg; |
| 42 | |
| 43 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 44 | |
| 45 | pdu = malloc(sizeof(struct rpc_pdu)); |
| 46 | if (pdu == NULL) { |
| 47 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
| 48 | return NULL; |
| 49 | } |
| 50 | memset(pdu, 0, sizeof(struct rpc_pdu)); |
| 51 | pdu->xid = rpc->xid++; |
| 52 | pdu->cb = cb; |
| 53 | pdu->private_data = private_data; |
| 54 | pdu->xdr_decode_fn = xdr_decode_fn; |
| 55 | pdu->xdr_decode_bufsize = xdr_decode_bufsize; |
| 56 | |
| 57 | xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE); |
| 58 | if (rpc->is_udp == 0) { |
| 59 | xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */ |
| 60 | } |
| 61 | |
| 62 | memset(&msg, 0, sizeof(struct rpc_msg)); |
| 63 | msg.rm_xid = pdu->xid; |
| 64 | msg.rm_direction = CALL; |
| 65 | msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; |
| 66 | msg.rm_call.cb_prog = program; |
| 67 | msg.rm_call.cb_vers = version; |
| 68 | msg.rm_call.cb_proc = procedure; |
| 69 | msg.rm_call.cb_cred = rpc->auth->ah_cred; |
| 70 | msg.rm_call.cb_verf = rpc->auth->ah_verf; |
| 71 | |
| 72 | if (xdr_callmsg(&pdu->xdr, &msg) == 0) { |
| 73 | rpc_set_error(rpc, "xdr_callmsg failed"); |
| 74 | xdr_destroy(&pdu->xdr); |
| 75 | free(pdu); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | return pdu; |
| 80 | } |
| 81 | |
| 82 | void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
| 83 | { |
| 84 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 85 | |
| 86 | if (pdu->outdata.data != NULL) { |
| 87 | free(pdu->outdata.data); |
| 88 | pdu->outdata.data = NULL; |
| 89 | } |
| 90 | |
| 91 | if (pdu->xdr_decode_buf != NULL) { |
| 92 | xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf); |
| 93 | free(pdu->xdr_decode_buf); |
| 94 | pdu->xdr_decode_buf = NULL; |
| 95 | } |
| 96 | |
| 97 | xdr_destroy(&pdu->xdr); |
| 98 | |
| 99 | free(pdu); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
| 104 | { |
| 105 | int size, recordmarker; |
| 106 | |
| 107 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 108 | |
| 109 | size = xdr_getpos(&pdu->xdr); |
| 110 | |
| 111 | /* for udp we dont queue, we just send it straight away */ |
| 112 | if (rpc->is_udp != 0) { |
| 113 | if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) { |
| 114 | rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno)); |
| 115 | rpc_free_pdu(rpc, pdu); |
| 116 | return -1; |
| 117 | } |
| 118 | SLIST_ADD_END(&rpc->waitpdu, pdu); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* write recordmarker */ |
| 123 | xdr_setpos(&pdu->xdr, 0); |
| 124 | recordmarker = (size - 4) | 0x80000000; |
| 125 | xdr_int(&pdu->xdr, &recordmarker); |
| 126 | |
| 127 | pdu->outdata.size = size; |
| 128 | pdu->outdata.data = malloc(pdu->outdata.size); |
| 129 | if (pdu->outdata.data == NULL) { |
| 130 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); |
| 131 | rpc_free_pdu(rpc, pdu); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); |
| 136 | SLIST_ADD_END(&rpc->outqueue, pdu); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int rpc_get_pdu_size(char *buf) |
| 142 | { |
| 143 | uint32_t size; |
| 144 | |
| 145 | size = ntohl(*(uint32_t *)buf); |
| 146 | |
| 147 | return (size & 0x7fffffff) + 4; |
| 148 | } |
| 149 | |
| 150 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr) |
| 151 | { |
| 152 | struct rpc_msg msg; |
| 153 | |
| 154 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 155 | |
| 156 | memset(&msg, 0, sizeof(struct rpc_msg)); |
| 157 | msg.acpted_rply.ar_verf = _null_auth; |
| 158 | if (pdu->xdr_decode_bufsize > 0) { |
| 159 | if (pdu->xdr_decode_buf != NULL) { |
| 160 | free(pdu->xdr_decode_buf); |
| 161 | } |
| 162 | pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); |
| 163 | if (pdu->xdr_decode_buf == NULL) { |
| 164 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
| 165 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); |
| 166 | return 0; |
| 167 | } |
| 168 | memset(pdu->xdr_decode_buf, 0, pdu->xdr_decode_bufsize); |
| 169 | } |
| 170 | msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf; |
| 171 | msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn; |
| 172 | |
| 173 | if (xdr_replymsg(xdr, &msg) == 0) { |
| 174 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
| 175 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
| 176 | if (pdu->xdr_decode_buf != NULL) { |
| 177 | free(pdu->xdr_decode_buf); |
| 178 | pdu->xdr_decode_buf = NULL; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | if (msg.rm_reply.rp_stat != MSG_ACCEPTED) { |
| 183 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); |
| 184 | return 0; |
| 185 | } |
| 186 | switch (msg.rm_reply.rp_acpt.ar_stat) { |
| 187 | case SUCCESS: |
| 188 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data); |
| 189 | break; |
| 190 | case PROG_UNAVAIL: |
| 191 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); |
| 192 | break; |
| 193 | case PROG_MISMATCH: |
| 194 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); |
| 195 | break; |
| 196 | case PROC_UNAVAIL: |
| 197 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); |
| 198 | break; |
| 199 | case GARBAGE_ARGS: |
| 200 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); |
| 201 | break; |
| 202 | case SYSTEM_ERR: |
| 203 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); |
| 204 | break; |
| 205 | default: |
| 206 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) |
| 214 | { |
| 215 | struct rpc_pdu *pdu; |
| 216 | XDR xdr; |
| 217 | int pos, recordmarker = 0; |
| 218 | unsigned int xid; |
| 219 | char *reasbuf = NULL; |
| 220 | |
| 221 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 222 | |
| 223 | memset(&xdr, 0, sizeof(XDR)); |
| 224 | |
| 225 | xdrmem_create(&xdr, buf, size, XDR_DECODE); |
| 226 | if (rpc->is_udp == 0) { |
| 227 | if (xdr_int(&xdr, &recordmarker) == 0) { |
| 228 | rpc_set_error(rpc, "xdr_int reading recordmarker failed"); |
| 229 | xdr_destroy(&xdr); |
| 230 | return -1; |
| 231 | } |
| 232 | if (!(recordmarker&0x80000000)) { |
| 233 | xdr_destroy(&xdr); |
| 234 | if (rpc_add_fragment(rpc, buf+4, size-4) != 0) { |
| 235 | rpc_set_error(rpc, "Failed to queue fragment for reassembly."); |
| 236 | return -1; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* reassembly */ |
| 243 | if (recordmarker != 0 && rpc->fragments != NULL) { |
| 244 | struct rpc_fragment *fragment; |
| 245 | uint64_t total = size - 4; |
| 246 | char *ptr; |
| 247 | |
| 248 | xdr_destroy(&xdr); |
| 249 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { |
| 250 | total += fragment->size; |
| 251 | } |
| 252 | |
| 253 | reasbuf = malloc(total); |
| 254 | if (reasbuf == NULL) { |
| 255 | rpc_set_error(rpc, "Failed to reassemble PDU"); |
| 256 | rpc_free_all_fragments(rpc); |
| 257 | return -1; |
| 258 | } |
| 259 | ptr = reasbuf; |
| 260 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { |
| 261 | memcpy(ptr, fragment->data, fragment->size); |
| 262 | ptr += fragment->size; |
| 263 | } |
| 264 | memcpy(ptr, buf + 4, size - 4); |
| 265 | xdrmem_create(&xdr, reasbuf, total, XDR_DECODE); |
| 266 | rpc_free_all_fragments(rpc); |
| 267 | } |
| 268 | |
| 269 | pos = xdr_getpos(&xdr); |
| 270 | if (xdr_int(&xdr, (int *)&xid) == 0) { |
| 271 | rpc_set_error(rpc, "xdr_int reading xid failed"); |
| 272 | xdr_destroy(&xdr); |
| 273 | if (reasbuf != NULL) { |
| 274 | free(reasbuf); |
| 275 | } |
| 276 | return -1; |
| 277 | } |
| 278 | xdr_setpos(&xdr, pos); |
| 279 | |
| 280 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { |
| 281 | if (pdu->xid != xid) { |
| 282 | continue; |
| 283 | } |
| 284 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
| 285 | SLIST_REMOVE(&rpc->waitpdu, pdu); |
| 286 | } |
| 287 | if (rpc_process_reply(rpc, pdu, &xdr) != 0) { |
| 288 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
| 289 | } |
| 290 | xdr_destroy(&xdr); |
| 291 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
| 292 | rpc_free_pdu(rpc, pdu); |
| 293 | } |
| 294 | if (reasbuf != NULL) { |
| 295 | free(reasbuf); |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
| 300 | xdr_destroy(&xdr); |
| 301 | if (reasbuf != NULL) { |
| 302 | free(reasbuf); |
| 303 | } |
| 304 | return -1; |
| 305 | } |
| 306 | |