| 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 AROS |
| 18 | #include "aros_compat.h" |
| 19 | #endif |
| 20 | |
| 21 | #ifdef WIN32 |
| 22 | #include "win32_compat.h" |
| 23 | #else |
| 24 | #include <strings.h> |
| 25 | #endif/*WIN32*/ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <assert.h> |
| 33 | #include <errno.h> |
| 34 | #include "slist.h" |
| 35 | #include "libnfs-zdr.h" |
| 36 | #include "libnfs.h" |
| 37 | #include "libnfs-raw.h" |
| 38 | #include "libnfs-private.h" |
| 39 | |
| 40 | struct rpc_pdu *rpc_allocate_pdu(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) |
| 41 | { |
| 42 | struct rpc_pdu *pdu; |
| 43 | struct rpc_msg msg; |
| 44 | |
| 45 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 46 | |
| 47 | pdu = malloc(sizeof(struct rpc_pdu)); |
| 48 | if (pdu == NULL) { |
| 49 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
| 50 | return NULL; |
| 51 | } |
| 52 | memset(pdu, 0, sizeof(struct rpc_pdu)); |
| 53 | pdu->xid = rpc->xid++; |
| 54 | pdu->cb = cb; |
| 55 | pdu->private_data = private_data; |
| 56 | pdu->zdr_decode_fn = zdr_decode_fn; |
| 57 | pdu->zdr_decode_bufsize = zdr_decode_bufsize; |
| 58 | |
| 59 | zdrmem_create(&pdu->zdr, rpc->encodebuf, rpc->encodebuflen, ZDR_ENCODE); |
| 60 | if (rpc->is_udp == 0) { |
| 61 | zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */ |
| 62 | } |
| 63 | |
| 64 | memset(&msg, 0, sizeof(struct rpc_msg)); |
| 65 | msg.xid = pdu->xid; |
| 66 | msg.direction = CALL; |
| 67 | msg.body.cbody.rpcvers = RPC_MSG_VERSION; |
| 68 | msg.body.cbody.prog = program; |
| 69 | msg.body.cbody.vers = version; |
| 70 | msg.body.cbody.proc = procedure; |
| 71 | msg.body.cbody.cred = rpc->auth->ah_cred; |
| 72 | msg.body.cbody.verf = rpc->auth->ah_verf; |
| 73 | |
| 74 | if (zdr_callmsg(&pdu->zdr, &msg) == 0) { |
| 75 | rpc_set_error(rpc, "zdr_callmsg failed"); |
| 76 | zdr_destroy(&pdu->zdr); |
| 77 | free(pdu); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | return pdu; |
| 82 | } |
| 83 | |
| 84 | void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
| 85 | { |
| 86 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 87 | |
| 88 | if (pdu->outdata.data != NULL) { |
| 89 | free(pdu->outdata.data); |
| 90 | pdu->outdata.data = NULL; |
| 91 | } |
| 92 | |
| 93 | if (pdu->zdr_decode_buf != NULL) { |
| 94 | zdr_free(pdu->zdr_decode_fn, pdu->zdr_decode_buf); |
| 95 | free(pdu->zdr_decode_buf); |
| 96 | pdu->zdr_decode_buf = NULL; |
| 97 | } |
| 98 | |
| 99 | zdr_destroy(&pdu->zdr); |
| 100 | |
| 101 | free(pdu); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
| 106 | { |
| 107 | int size, recordmarker; |
| 108 | |
| 109 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 110 | |
| 111 | size = zdr_getpos(&pdu->zdr); |
| 112 | |
| 113 | /* for udp we dont queue, we just send it straight away */ |
| 114 | if (rpc->is_udp != 0) { |
| 115 | // XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h |
| 116 | if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) { |
| 117 | rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno)); |
| 118 | rpc_free_pdu(rpc, pdu); |
| 119 | return -1; |
| 120 | } |
| 121 | SLIST_ADD_END(&rpc->waitpdu, pdu); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* write recordmarker */ |
| 126 | zdr_setpos(&pdu->zdr, 0); |
| 127 | recordmarker = (size - 4) | 0x80000000; |
| 128 | zdr_int(&pdu->zdr, &recordmarker); |
| 129 | |
| 130 | pdu->outdata.size = size; |
| 131 | pdu->outdata.data = malloc(pdu->outdata.size); |
| 132 | if (pdu->outdata.data == NULL) { |
| 133 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); |
| 134 | rpc_free_pdu(rpc, pdu); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); |
| 139 | SLIST_ADD_END(&rpc->outqueue, pdu); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int rpc_get_pdu_size(char *buf) |
| 145 | { |
| 146 | uint32_t size; |
| 147 | |
| 148 | size = ntohl(*(uint32_t *)buf); |
| 149 | |
| 150 | return (size & 0x7fffffff) + 4; |
| 151 | } |
| 152 | |
| 153 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *zdr) |
| 154 | { |
| 155 | struct rpc_msg msg; |
| 156 | |
| 157 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 158 | |
| 159 | memset(&msg, 0, sizeof(struct rpc_msg)); |
| 160 | msg.body.rbody.reply.areply.verf = _null_auth; |
| 161 | if (pdu->zdr_decode_bufsize > 0) { |
| 162 | if (pdu->zdr_decode_buf != NULL) { |
| 163 | free(pdu->zdr_decode_buf); |
| 164 | } |
| 165 | pdu->zdr_decode_buf = malloc(pdu->zdr_decode_bufsize); |
| 166 | if (pdu->zdr_decode_buf == NULL) { |
| 167 | rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply"); |
| 168 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of ZDR reply", pdu->private_data); |
| 169 | return 0; |
| 170 | } |
| 171 | memset(pdu->zdr_decode_buf, 0, pdu->zdr_decode_bufsize); |
| 172 | } |
| 173 | msg.body.rbody.reply.areply.reply_data.results.where = pdu->zdr_decode_buf; |
| 174 | msg.body.rbody.reply.areply.reply_data.results.proc = pdu->zdr_decode_fn; |
| 175 | |
| 176 | if (zdr_replymsg(zdr, &msg) == 0) { |
| 177 | rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply"); |
| 178 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
| 179 | if (pdu->zdr_decode_buf != NULL) { |
| 180 | free(pdu->zdr_decode_buf); |
| 181 | pdu->zdr_decode_buf = NULL; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | if (msg.body.rbody.stat != MSG_ACCEPTED) { |
| 186 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); |
| 187 | return 0; |
| 188 | } |
| 189 | switch (msg.body.rbody.reply.areply.stat) { |
| 190 | case SUCCESS: |
| 191 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->zdr_decode_buf, pdu->private_data); |
| 192 | break; |
| 193 | case PROG_UNAVAIL: |
| 194 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); |
| 195 | break; |
| 196 | case PROG_MISMATCH: |
| 197 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); |
| 198 | break; |
| 199 | case PROC_UNAVAIL: |
| 200 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); |
| 201 | break; |
| 202 | case GARBAGE_ARGS: |
| 203 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); |
| 204 | break; |
| 205 | case SYSTEM_ERR: |
| 206 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); |
| 207 | break; |
| 208 | default: |
| 209 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) |
| 217 | { |
| 218 | struct rpc_pdu *pdu; |
| 219 | ZDR zdr; |
| 220 | int pos, recordmarker = 0; |
| 221 | unsigned int xid; |
| 222 | char *reasbuf = NULL; |
| 223 | |
| 224 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
| 225 | |
| 226 | memset(&zdr, 0, sizeof(ZDR)); |
| 227 | |
| 228 | zdrmem_create(&zdr, buf, size, ZDR_DECODE); |
| 229 | if (rpc->is_udp == 0) { |
| 230 | if (zdr_int(&zdr, &recordmarker) == 0) { |
| 231 | rpc_set_error(rpc, "zdr_int reading recordmarker failed"); |
| 232 | zdr_destroy(&zdr); |
| 233 | return -1; |
| 234 | } |
| 235 | if (!(recordmarker&0x80000000)) { |
| 236 | zdr_destroy(&zdr); |
| 237 | if (rpc_add_fragment(rpc, buf+4, size-4) != 0) { |
| 238 | rpc_set_error(rpc, "Failed to queue fragment for reassembly."); |
| 239 | return -1; |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* reassembly */ |
| 246 | if (recordmarker != 0 && rpc->fragments != NULL) { |
| 247 | struct rpc_fragment *fragment; |
| 248 | uint32_t total = size - 4; |
| 249 | char *ptr; |
| 250 | |
| 251 | zdr_destroy(&zdr); |
| 252 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { |
| 253 | total += fragment->size; |
| 254 | } |
| 255 | |
| 256 | reasbuf = malloc(total); |
| 257 | if (reasbuf == NULL) { |
| 258 | rpc_set_error(rpc, "Failed to reassemble PDU"); |
| 259 | rpc_free_all_fragments(rpc); |
| 260 | return -1; |
| 261 | } |
| 262 | ptr = reasbuf; |
| 263 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { |
| 264 | memcpy(ptr, fragment->data, fragment->size); |
| 265 | ptr += fragment->size; |
| 266 | } |
| 267 | memcpy(ptr, buf + 4, size - 4); |
| 268 | zdrmem_create(&zdr, reasbuf, total, ZDR_DECODE); |
| 269 | rpc_free_all_fragments(rpc); |
| 270 | } |
| 271 | |
| 272 | pos = zdr_getpos(&zdr); |
| 273 | if (zdr_int(&zdr, (int *)&xid) == 0) { |
| 274 | rpc_set_error(rpc, "zdr_int reading xid failed"); |
| 275 | zdr_destroy(&zdr); |
| 276 | if (reasbuf != NULL) { |
| 277 | free(reasbuf); |
| 278 | } |
| 279 | return -1; |
| 280 | } |
| 281 | zdr_setpos(&zdr, pos); |
| 282 | |
| 283 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { |
| 284 | if (pdu->xid != xid) { |
| 285 | continue; |
| 286 | } |
| 287 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
| 288 | SLIST_REMOVE(&rpc->waitpdu, pdu); |
| 289 | } |
| 290 | if (rpc_process_reply(rpc, pdu, &zdr) != 0) { |
| 291 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
| 292 | } |
| 293 | zdr_destroy(&zdr); |
| 294 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
| 295 | rpc_free_pdu(rpc, pdu); |
| 296 | } |
| 297 | if (reasbuf != NULL) { |
| 298 | free(reasbuf); |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
| 303 | zdr_destroy(&zdr); |
| 304 | if (reasbuf != NULL) { |
| 305 | free(reasbuf); |
| 306 | } |
| 307 | return -1; |
| 308 | } |
| 309 | |