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 return (size
& 0x7fffffff) + 4;
147 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
151 memset(&msg
, 0, sizeof(struct rpc_msg
));
152 msg
.acpted_rply
.ar_verf
= _null_auth
;
153 if (pdu
->xdr_decode_bufsize
> 0) {
154 if (pdu
->xdr_decode_buf
!= NULL
) {
155 free(pdu
->xdr_decode_buf
);
157 pdu
->xdr_decode_buf
= malloc(pdu
->xdr_decode_bufsize
);
158 if (pdu
->xdr_decode_buf
== NULL
) {
159 rpc_set_error(rpc
, "xdr_replymsg failed in portmap_getport_reply");
160 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Failed to allocate buffer for decoding of XDR reply", pdu
->private_data
);
163 memset(pdu
->xdr_decode_buf
, 0, pdu
->xdr_decode_bufsize
);
165 msg
.acpted_rply
.ar_results
.where
= pdu
->xdr_decode_buf
;
166 msg
.acpted_rply
.ar_results
.proc
= pdu
->xdr_decode_fn
;
168 if (xdr_replymsg(xdr
, &msg
) == 0) {
169 rpc_set_error(rpc
, "xdr_replymsg failed in portmap_getport_reply");
170 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Message rejected by server", pdu
->private_data
);
171 if (pdu
->xdr_decode_buf
!= NULL
) {
172 free(pdu
->xdr_decode_buf
);
173 pdu
->xdr_decode_buf
= NULL
;
177 if (msg
.rm_reply
.rp_stat
!= MSG_ACCEPTED
) {
178 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "RPC Packet not accepted by the server", pdu
->private_data
);
181 switch (msg
.rm_reply
.rp_acpt
.ar_stat
) {
183 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->xdr_decode_buf
, pdu
->private_data
);
186 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
189 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
192 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
195 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
198 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
201 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
208 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
212 int pos
, recordmarker
= 0;
214 char *reasbuf
= NULL
;
216 memset(&xdr
, 0, sizeof(XDR
));
218 xdrmem_create(&xdr
, buf
, size
, XDR_DECODE
);
219 if (rpc
->is_udp
== 0) {
220 if (xdr_int(&xdr
, &recordmarker
) == 0) {
221 rpc_set_error(rpc
, "xdr_int reading recordmarker failed");
225 if (!(recordmarker
&0x80000000)) {
227 if (rpc_add_fragment(rpc
, buf
+4, size
-4) != 0) {
228 rpc_set_error(rpc
, "Failed to queue fragment for reassembly.");
236 if (recordmarker
!= 0 && rpc
->fragments
!= NULL
) {
237 struct rpc_fragment
*fragment
;
238 uint64_t total
= size
- 4;
242 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
243 total
+= fragment
->size
;
246 reasbuf
= malloc(total
);
247 if (reasbuf
== NULL
) {
248 rpc_set_error(rpc
, "Failed to reassemble PDU");
249 rpc_free_all_fragments(rpc
);
253 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
254 memcpy(ptr
, fragment
->data
, fragment
->size
);
255 ptr
+= fragment
->size
;
257 memcpy(ptr
, buf
+ 4, size
- 4);
258 xdrmem_create(&xdr
, reasbuf
, total
, XDR_DECODE
);
259 rpc_free_all_fragments(rpc
);
262 pos
= xdr_getpos(&xdr
);
263 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
264 rpc_set_error(rpc
, "xdr_int reading xid failed");
266 if (reasbuf
!= NULL
) {
271 xdr_setpos(&xdr
, pos
);
273 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
274 if (pdu
->xid
!= xid
) {
277 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
278 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
280 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
281 rpc_set_error(rpc
, "rpc_procdess_reply failed");
284 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
285 rpc_free_pdu(rpc
, pdu
);
287 if (reasbuf
!= NULL
) {
292 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);
294 if (reasbuf
!= NULL
) {