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