2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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.
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/>.
18 #include "win32_compat.h"
20 #define MSG_DONTWAIT 0
31 #include <rpc/rpc_msg.h>
34 #include "libnfs-raw.h"
35 #include "libnfs-private.h"
37 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
)
46 pdu
= malloc(sizeof(struct rpc_pdu
));
48 rpc_set_error(rpc
, "Out of memory: Failed to allocate pdu structure");
51 memset(pdu
, 0, sizeof(struct rpc_pdu
));
52 pdu
->xid
= rpc
->xid
++;
54 pdu
->private_data
= private_data
;
55 pdu
->xdr_decode_fn
= xdr_decode_fn
;
56 pdu
->xdr_decode_bufsize
= xdr_decode_bufsize
;
58 xdrmem_create(&pdu
->xdr
, rpc
->encodebuf
, rpc
->encodebuflen
, XDR_ENCODE
);
59 if (rpc
->is_udp
== 0) {
60 xdr_setpos(&pdu
->xdr
, 4); /* skip past the record marker */
63 memset(&msg
, 0, sizeof(struct rpc_msg
));
64 msg
.rm_xid
= pdu
->xid
;
65 msg
.rm_direction
= CALL
;
66 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
67 msg
.rm_call
.cb_prog
= program
;
68 msg
.rm_call
.cb_vers
= version
;
69 msg
.rm_call
.cb_proc
= procedure
;
70 msg
.rm_call
.cb_cred
= rpc
->auth
->ah_cred
;
71 msg
.rm_call
.cb_verf
= rpc
->auth
->ah_verf
;
73 if (xdr_callmsg(&pdu
->xdr
, &msg
) == 0) {
74 rpc_set_error(rpc
, "xdr_callmsg failed");
75 xdr_destroy(&pdu
->xdr
);
83 void rpc_free_pdu(struct rpc_context
*rpc _U_
, struct rpc_pdu
*pdu
)
85 if (pdu
->outdata
.data
!= NULL
) {
86 free(pdu
->outdata
.data
);
87 pdu
->outdata
.data
= NULL
;
90 if (pdu
->xdr_decode_buf
!= NULL
) {
91 xdr_free(pdu
->xdr_decode_fn
, pdu
->xdr_decode_buf
);
92 free(pdu
->xdr_decode_buf
);
93 pdu
->xdr_decode_buf
= NULL
;
96 xdr_destroy(&pdu
->xdr
);
102 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
104 int size
, recordmarker
;
106 size
= xdr_getpos(&pdu
->xdr
);
108 /* for udp we dont queue, we just send it straight away */
109 if (rpc
->is_udp
!= 0) {
110 if (sendto(rpc
->fd
, rpc
->encodebuf
, size
, MSG_DONTWAIT
, rpc
->udp_dest
, sizeof(struct sockaddr_in
)) < 0) {
111 rpc_set_error(rpc
, "Sendto failed with errno %s", strerror(errno
));
112 rpc_free_pdu(rpc
, pdu
);
115 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
119 /* write recordmarker */
120 xdr_setpos(&pdu
->xdr
, 0);
121 recordmarker
= (size
- 4) | 0x80000000;
122 xdr_int(&pdu
->xdr
, &recordmarker
);
124 pdu
->outdata
.size
= size
;
125 pdu
->outdata
.data
= malloc(pdu
->outdata
.size
);
126 if (pdu
->outdata
.data
== NULL
) {
127 rpc_set_error(rpc
, "Out of memory. Failed to allocate buffer for pdu\n");
128 rpc_free_pdu(rpc
, pdu
);
132 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
133 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
138 int rpc_get_pdu_size(char *buf
)
142 size
= ntohl(*(uint32_t *)buf
);
144 if ((size
& 0x80000000) == 0) {
145 /* cant handle oncrpc fragments */
149 return (size
& 0x7fffffff) + 4;
152 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
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
);
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
);
168 memset(pdu
->xdr_decode_buf
, 0, pdu
->xdr_decode_bufsize
);
170 msg
.acpted_rply
.ar_results
.where
= pdu
->xdr_decode_buf
;
171 msg
.acpted_rply
.ar_results
.proc
= pdu
->xdr_decode_fn
;
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
;
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
);
186 switch (msg
.rm_reply
.rp_acpt
.ar_stat
) {
188 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->xdr_decode_buf
, pdu
->private_data
);
191 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
194 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
197 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
200 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
203 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
206 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
213 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
217 int pos
, recordmarker
;
220 memset(&xdr
, 0, sizeof(XDR
));
222 xdrmem_create(&xdr
, buf
, size
, XDR_DECODE
);
223 if (rpc
->is_udp
== 0) {
224 if (xdr_int(&xdr
, &recordmarker
) == 0) {
225 rpc_set_error(rpc
, "xdr_int reading recordmarker failed");
230 pos
= xdr_getpos(&xdr
);
231 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
232 rpc_set_error(rpc
, "xdr_int reading xid failed");
236 xdr_setpos(&xdr
, pos
);
238 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
239 if (pdu
->xid
!= xid
) {
242 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
243 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
245 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
246 rpc_set_error(rpc
, "rpc_procdess_reply failed");
249 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
250 rpc_free_pdu(rpc
, pdu
);
254 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);