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/>.
24 #include <rpc/rpc_msg.h>
27 #include "libnfs-raw.h"
28 #include "libnfs-private.h"
30 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
)
39 pdu
= malloc(sizeof(struct rpc_pdu
));
41 rpc_set_error(rpc
, "Out of memory: Failed to allocate pdu structure");
44 bzero(pdu
, sizeof(struct rpc_pdu
));
45 pdu
->xid
= rpc
->xid
++;
47 pdu
->private_data
= private_data
;
48 pdu
->xdr_decode_fn
= xdr_decode_fn
;
49 pdu
->xdr_decode_bufsize
= xdr_decode_bufsize
;
51 xdrmem_create(&pdu
->xdr
, rpc
->encodebuf
, rpc
->encodebuflen
, XDR_ENCODE
);
52 if (rpc
->is_udp
== 0) {
53 xdr_setpos(&pdu
->xdr
, 4); /* skip past the record marker */
56 bzero(&msg
, sizeof(struct rpc_msg
));
57 msg
.rm_xid
= pdu
->xid
;
58 msg
.rm_direction
= CALL
;
59 msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
60 msg
.rm_call
.cb_prog
= program
;
61 msg
.rm_call
.cb_vers
= version
;
62 msg
.rm_call
.cb_proc
= procedure
;
63 msg
.rm_call
.cb_cred
= rpc
->auth
->ah_cred
;
64 msg
.rm_call
.cb_verf
= rpc
->auth
->ah_verf
;
66 if (xdr_callmsg(&pdu
->xdr
, &msg
) == 0) {
67 rpc_set_error(rpc
, "xdr_callmsg failed");
68 xdr_destroy(&pdu
->xdr
);
76 void rpc_free_pdu(struct rpc_context
*rpc _U_
, struct rpc_pdu
*pdu
)
78 if (pdu
->outdata
.data
!= NULL
) {
79 free(pdu
->outdata
.data
);
80 pdu
->outdata
.data
= NULL
;
83 if (pdu
->xdr_decode_buf
!= NULL
) {
84 xdr_free(pdu
->xdr_decode_fn
, pdu
->xdr_decode_buf
);
85 free(pdu
->xdr_decode_buf
);
86 pdu
->xdr_decode_buf
= NULL
;
89 xdr_destroy(&pdu
->xdr
);
95 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
97 int size
, recordmarker
;
99 size
= xdr_getpos(&pdu
->xdr
);
101 /* for udp we dont queue, we just send it straight away */
102 if (rpc
->is_udp
!= 0) {
103 if (sendto(rpc
->fd
, rpc
->encodebuf
, size
, MSG_DONTWAIT
, rpc
->udp_dest
, sizeof(struct sockaddr_in
)) < 0) {
104 rpc_set_error(rpc
, "Sendto failed with errno %s", strerror(errno
));
105 rpc_free_pdu(rpc
, pdu
);
108 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
112 /* write recordmarker */
113 xdr_setpos(&pdu
->xdr
, 0);
114 recordmarker
= (size
- 4) | 0x80000000;
115 xdr_int(&pdu
->xdr
, &recordmarker
);
117 pdu
->outdata
.size
= size
;
118 pdu
->outdata
.data
= malloc(pdu
->outdata
.size
);
119 if (pdu
->outdata
.data
== NULL
) {
120 rpc_set_error(rpc
, "Out of memory. Failed to allocate buffer for pdu\n");
121 rpc_free_pdu(rpc
, pdu
);
125 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
126 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
131 int rpc_get_pdu_size(char *buf
)
135 size
= ntohl(*(uint32_t *)buf
);
137 if ((size
& 0x80000000) == 0) {
138 /* cant handle oncrpc fragments */
142 return (size
& 0x7fffffff) + 4;
145 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, XDR
*xdr
)
149 bzero(&msg
, sizeof(struct rpc_msg
));
150 msg
.acpted_rply
.ar_verf
= _null_auth
;
151 if (pdu
->xdr_decode_bufsize
> 0) {
152 pdu
->xdr_decode_buf
= malloc(pdu
->xdr_decode_bufsize
);
153 if (pdu
->xdr_decode_buf
== NULL
) {
154 rpc_set_error(rpc
, "xdr_replymsg failed in portmap_getport_reply");
155 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Failed to allocate buffer for decoding of XDR reply", pdu
->private_data
);
158 bzero(pdu
->xdr_decode_buf
, pdu
->xdr_decode_bufsize
);
160 msg
.acpted_rply
.ar_results
.where
= pdu
->xdr_decode_buf
;
161 msg
.acpted_rply
.ar_results
.proc
= pdu
->xdr_decode_fn
;
163 if (xdr_replymsg(xdr
, &msg
) == 0) {
164 rpc_set_error(rpc
, "xdr_replymsg failed in portmap_getport_reply");
165 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Message rejected by server", pdu
->private_data
);
166 if (pdu
->xdr_decode_buf
!= NULL
) {
167 free(pdu
->xdr_decode_buf
);
168 pdu
->xdr_decode_buf
= NULL
;
172 if (msg
.rm_reply
.rp_stat
!= MSG_ACCEPTED
) {
173 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "RPC Packet not accepted by the server", pdu
->private_data
);
176 switch (msg
.rm_reply
.rp_acpt
.ar_stat
) {
178 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->xdr_decode_buf
, pdu
->private_data
);
181 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
184 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
187 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
190 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
193 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
196 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
203 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
207 int pos
, recordmarker
;
210 bzero(&xdr
, sizeof(XDR
));
212 xdrmem_create(&xdr
, buf
, size
, XDR_DECODE
);
213 if (rpc
->is_udp
== 0) {
214 if (xdr_int(&xdr
, &recordmarker
) == 0) {
215 rpc_set_error(rpc
, "xdr_int reading recordmarker failed");
220 pos
= xdr_getpos(&xdr
);
221 if (xdr_int(&xdr
, (int *)&xid
) == 0) {
222 rpc_set_error(rpc
, "xdr_int reading xid failed");
226 xdr_setpos(&xdr
, pos
);
228 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
229 if (pdu
->xid
!= xid
) {
232 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
233 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
235 if (rpc_process_reply(rpc
, pdu
, &xdr
) != 0) {
236 rpc_set_error(rpc
, "rpc_procdess_reply failed");
239 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
240 rpc_free_pdu(rpc
, pdu
);
244 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);