pdu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <strings.h>
  16. #include <rpc/xdr.h>
  17. #include <rpc/rpc_msg.h>
  18. #include <ccan/compiler/compiler.h>
  19. #include "dlinklist.h"
  20. #include "nfs.h"
  21. #include "libnfs-raw.h"
  22. #include "libnfs-private.h"
  23. 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)
  24. {
  25. struct rpc_pdu *pdu;
  26. struct rpc_msg msg;
  27. if (rpc == NULL) {
  28. printf("trying to allocate rpc pdu on NULL context\n");
  29. return NULL;
  30. }
  31. pdu = malloc(sizeof(struct rpc_pdu));
  32. if (pdu == NULL) {
  33. printf("Failed to allocate pdu structure\n");
  34. return NULL;
  35. }
  36. bzero(pdu, sizeof(struct rpc_pdu));
  37. pdu->xid = rpc->xid++;
  38. pdu->cb = cb;
  39. pdu->private_data = private_data;
  40. pdu->xdr_decode_fn = xdr_decode_fn;
  41. pdu->xdr_decode_bufsize = xdr_decode_bufsize;
  42. xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE);
  43. xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */
  44. bzero(&msg, sizeof(struct rpc_msg));
  45. msg.rm_xid = pdu->xid;
  46. msg.rm_direction = CALL;
  47. msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
  48. msg.rm_call.cb_prog = program;
  49. msg.rm_call.cb_vers = version;
  50. msg.rm_call.cb_proc = procedure;
  51. msg.rm_call.cb_cred = rpc->auth->ah_cred;
  52. msg.rm_call.cb_verf = rpc->auth->ah_verf;
  53. if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
  54. printf("xdr_callmsg failed\n");
  55. xdr_destroy(&pdu->xdr);
  56. free(pdu);
  57. return NULL;
  58. }
  59. return pdu;
  60. }
  61. void rpc_free_pdu(struct rpc_context *rpc UNUSED, struct rpc_pdu *pdu)
  62. {
  63. if (pdu->outdata.data != NULL) {
  64. free(pdu->outdata.data);
  65. pdu->outdata.data = NULL;
  66. }
  67. if (pdu->xdr_decode_buf != NULL) {
  68. xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf);
  69. free(pdu->xdr_decode_buf);
  70. pdu->xdr_decode_buf = NULL;
  71. }
  72. xdr_destroy(&pdu->xdr);
  73. free(pdu);
  74. }
  75. int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
  76. {
  77. int size, recordmarker;
  78. size = xdr_getpos(&pdu->xdr);
  79. /* write recordmarker */
  80. xdr_setpos(&pdu->xdr, 0);
  81. recordmarker = (size - 4) | 0x80000000;
  82. xdr_int(&pdu->xdr, &recordmarker);
  83. pdu->outdata.size = size;
  84. pdu->outdata.data = malloc(pdu->outdata.size);
  85. if (pdu->outdata.data == NULL) {
  86. rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n");
  87. rpc_free_pdu(rpc, pdu);
  88. return -1;
  89. }
  90. memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
  91. DLIST_ADD_END(rpc->outqueue, pdu, NULL);
  92. return 0;
  93. }
  94. int rpc_get_pdu_size(char *buf)
  95. {
  96. uint32_t size;
  97. size = ntohl(*(uint32_t *)buf);
  98. if ((size & 0x80000000) == 0) {
  99. printf("cant handle oncrpc fragments\n");
  100. return -1;
  101. }
  102. return (size & 0x7fffffff) + 4;
  103. }
  104. static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
  105. {
  106. struct rpc_msg msg;
  107. bzero(&msg, sizeof(struct rpc_msg));
  108. msg.acpted_rply.ar_verf = _null_auth;
  109. if (pdu->xdr_decode_bufsize > 0) {
  110. pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize);
  111. if (pdu->xdr_decode_buf == NULL) {
  112. printf("xdr_replymsg failed in portmap_getport_reply\n");
  113. pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data);
  114. return 0;
  115. }
  116. bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize);
  117. }
  118. msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
  119. msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn;
  120. if (xdr_replymsg(xdr, &msg) == 0) {
  121. printf("xdr_replymsg failed in portmap_getport_reply\n");
  122. pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data);
  123. if (pdu->xdr_decode_buf != NULL) {
  124. free(pdu->xdr_decode_buf);
  125. pdu->xdr_decode_buf = NULL;
  126. }
  127. return 0;
  128. }
  129. if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
  130. pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data);
  131. return 0;
  132. }
  133. switch (msg.rm_reply.rp_acpt.ar_stat) {
  134. case SUCCESS:
  135. pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
  136. break;
  137. case PROG_UNAVAIL:
  138. pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
  139. break;
  140. case PROG_MISMATCH:
  141. pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
  142. break;
  143. case PROC_UNAVAIL:
  144. pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
  145. break;
  146. case GARBAGE_ARGS:
  147. pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
  148. break;
  149. case SYSTEM_ERR:
  150. pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
  151. break;
  152. default:
  153. pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
  154. break;
  155. }
  156. return 0;
  157. }
  158. int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
  159. {
  160. struct rpc_pdu *pdu;
  161. XDR xdr;
  162. int pos, recordmarker;
  163. unsigned int xid;
  164. bzero(&xdr, sizeof(XDR));
  165. xdrmem_create(&xdr, buf, size, XDR_DECODE);
  166. if (xdr_int(&xdr, &recordmarker) == 0) {
  167. printf("xdr_int reading recordmarker failed\n");
  168. xdr_destroy(&xdr);
  169. return -1;
  170. }
  171. pos = xdr_getpos(&xdr);
  172. if (xdr_int(&xdr, (int *)&xid) == 0) {
  173. printf("xdr_int reading xid failed\n");
  174. xdr_destroy(&xdr);
  175. return -1;
  176. }
  177. xdr_setpos(&xdr, pos);
  178. for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
  179. if (pdu->xid != xid) {
  180. continue;
  181. }
  182. DLIST_REMOVE(rpc->waitpdu, pdu);
  183. if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
  184. printf("rpc_procdess_reply failed\n");
  185. }
  186. xdr_destroy(&xdr);
  187. rpc_free_pdu(rpc, pdu);
  188. return 0;
  189. }
  190. printf("No matching pdu found for xid:%d\n", xid);
  191. xdr_destroy(&xdr);
  192. return -1;
  193. }