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
32 #include <rpc/rpc_msg.h>
35 #include "libnfs-raw.h"
36 #include "libnfs-private.h"
38 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
)
43 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
45 pdu
= malloc(sizeof(struct rpc_pdu
));
47 rpc_set_error(rpc
, "Out of memory: Failed to allocate pdu structure");
50 memset(pdu
, 0, sizeof(struct rpc_pdu
));
51 pdu
->xid
= rpc
->xid
++;
53 pdu
->private_data
= private_data
;
54 pdu
->xdr_decode_fn
= xdr_decode_fn
;
55 pdu
->xdr_decode_bufsize
= xdr_decode_bufsize
;
57 xdrmem_create(&pdu
->xdr
, rpc
->encodebuf
, rpc
->encodebuflen
, XDR_ENCODE
);
58 if (rpc
->is_udp
== 0) {
59 xdr_setpos(&pdu
->xdr
, 4); /* skip past the record marker */
62 memset(&msg
, 0, sizeof(struct rpc_msg
));
63 msg
.rm_xid
= pdu
->xid
;
64 msg
.rm_direction
= CALL
;
65 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
66 msg
.rm_call
.cb_prog
= program
;
67 msg
.rm_call
.cb_vers
= version
;
68 msg
.rm_call
.cb_proc
= procedure
;
69 msg
.rm_call
.cb_cred
= rpc
->auth
->ah_cred
;
70 msg
.rm_call
.cb_verf
= rpc
->auth
->ah_verf
;
72 if (xdr_callmsg(&pdu
->xdr
, &msg
) == 0) {
73 rpc_set_error(rpc
, "xdr_callmsg failed");
74 xdr_destroy(&pdu
->xdr
);
82 void rpc_free_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
84 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
86 if (pdu
->outdata
.data
!= NULL
) {
87 free(pdu
->outdata
.data
);
88 pdu
->outdata
.data
= NULL
;
91 if (pdu
->xdr_decode_buf
!= NULL
) {
92 xdr_free(pdu
->xdr_decode_fn
, pdu
->xdr_decode_buf
);
93 free(pdu
->xdr_decode_buf
);
94 pdu
->xdr_decode_buf
= NULL
;
97 xdr_destroy(&pdu
->xdr
);
103 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
105 int size
, recordmarker
;
107 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
109 size
= xdr_getpos(&pdu
->xdr
);
111 /* for udp we dont queue, we just send it straight away */
112 if (rpc
->is_udp
!= 0) {
113 if (sendto(rpc
->fd
, rpc
->encodebuf
, size
, MSG_DONTWAIT
, rpc
->udp_dest
, sizeof(struct sockaddr_in
)) < 0) {
114 rpc_set_error(rpc
, "Sendto failed with errno %s", strerror(errno
));
115 rpc_free_pdu(rpc
, pdu
);
118 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
122 /* write recordmarker */
123 xdr_setpos(&pdu
->xdr
, 0);
124 recordmarker
= (size
- 4) | 0x80000000;
125 xdr_int(&pdu
->xdr
, &recordmarker
);
127 pdu
->outdata
.size
= size
;
128 pdu
->outdata
.data
= malloc(pdu
->outdata
.size
);
129 if (pdu
->outdata
.data
== NULL
) {
130 rpc_set_error(rpc
, "Out of memory. Failed to allocate buffer for pdu\n");
131 rpc_free_pdu(rpc
, pdu
);
135 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
136 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
141 int rpc_get_pdu_size(char *buf
)
145 size
= ntohl(*(uint32_t *)buf
);
147 return (size
& 0x7fffffff) + 4;
150 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
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
= 0;
219 char *reasbuf
= NULL
;
221 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
223 memset(&xdr
, 0, sizeof(XDR
));
225 xdrmem_create(&xdr
, buf
, size
, XDR_DECODE
);
226 if (rpc
->is_udp
== 0) {
227 if (xdr_int(&xdr
, &recordmarker
) == 0) {
228 rpc_set_error(rpc
, "xdr_int reading recordmarker failed");
232 if (!(recordmarker
&0x80000000)) {
234 if (rpc_add_fragment(rpc
, buf
+4, size
-4) != 0) {
235 rpc_set_error(rpc
, "Failed to queue fragment for reassembly.");
243 if (recordmarker
!= 0 && rpc
->fragments
!= NULL
) {
244 struct rpc_fragment
*fragment
;
245 uint64_t total
= size
- 4;
249 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
250 total
+= fragment
->size
;
253 reasbuf
= malloc(total
);
254 if (reasbuf
== NULL
) {
255 rpc_set_error(rpc
, "Failed to reassemble PDU");
256 rpc_free_all_fragments(rpc
);
260 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
261 memcpy(ptr
, fragment
->data
, fragment
->size
);
262 ptr
+= fragment
->size
;
264 memcpy(ptr
, buf
+ 4, size
- 4);
265 xdrmem_create(&xdr
, reasbuf
, total
, XDR_DECODE
);
266 rpc_free_all_fragments(rpc
);
269 pos
= xdr_getpos(&xdr
);
270 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
271 rpc_set_error(rpc
, "xdr_int reading xid failed");
273 if (reasbuf
!= NULL
) {
278 xdr_setpos(&xdr
, pos
);
280 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
281 if (pdu
->xid
!= xid
) {
284 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
285 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
287 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
288 rpc_set_error(rpc
, "rpc_procdess_reply failed");
291 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
292 rpc_free_pdu(rpc
, pdu
);
294 if (reasbuf
!= NULL
) {
299 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);
301 if (reasbuf
!= NULL
) {