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/>.
22 #include "aros_compat.h"
26 #include "win32_compat.h"
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
47 #include "libnfs-zdr.h"
49 #include "libnfs-raw.h"
50 #include "libnfs-private.h"
52 struct rpc_pdu
*rpc_allocate_pdu(struct rpc_context
*rpc
, int program
, int version
, int procedure
, rpc_cb cb
, void *private_data
, zdrproc_t zdr_decode_fn
, int zdr_decode_bufsize
)
57 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
59 pdu
= malloc(sizeof(struct rpc_pdu
));
61 rpc_set_error(rpc
, "Out of memory: Failed to allocate pdu structure");
64 memset(pdu
, 0, sizeof(struct rpc_pdu
));
65 pdu
->xid
= rpc
->xid
++;
67 pdu
->private_data
= private_data
;
68 pdu
->zdr_decode_fn
= zdr_decode_fn
;
69 pdu
->zdr_decode_bufsize
= zdr_decode_bufsize
;
71 zdrmem_create(&pdu
->zdr
, rpc
->encodebuf
, rpc
->encodebuflen
, ZDR_ENCODE
);
72 if (rpc
->is_udp
== 0) {
73 zdr_setpos(&pdu
->zdr
, 4); /* skip past the record marker */
76 memset(&msg
, 0, sizeof(struct rpc_msg
));
79 msg
.body
.cbody
.rpcvers
= RPC_MSG_VERSION
;
80 msg
.body
.cbody
.prog
= program
;
81 msg
.body
.cbody
.vers
= version
;
82 msg
.body
.cbody
.proc
= procedure
;
83 msg
.body
.cbody
.cred
= rpc
->auth
->ah_cred
;
84 msg
.body
.cbody
.verf
= rpc
->auth
->ah_verf
;
86 if (zdr_callmsg(&pdu
->zdr
, &msg
) == 0) {
87 rpc_set_error(rpc
, "zdr_callmsg failed");
88 zdr_destroy(&pdu
->zdr
);
96 void rpc_free_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
98 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
100 if (pdu
->outdata
.data
!= NULL
) {
101 free(pdu
->outdata
.data
);
102 pdu
->outdata
.data
= NULL
;
105 if (pdu
->zdr_decode_buf
!= NULL
) {
106 zdr_free(pdu
->zdr_decode_fn
, pdu
->zdr_decode_buf
);
107 free(pdu
->zdr_decode_buf
);
108 pdu
->zdr_decode_buf
= NULL
;
111 zdr_destroy(&pdu
->zdr
);
116 void rpc_set_next_xid(struct rpc_context
*rpc
, uint32_t xid
)
121 int rpc_queue_pdu(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
)
123 int size
, recordmarker
;
125 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
127 size
= zdr_getpos(&pdu
->zdr
);
129 /* for udp we dont queue, we just send it straight away */
130 if (rpc
->is_udp
!= 0) {
131 // XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h
132 if (sendto(rpc
->fd
, rpc
->encodebuf
, size
, MSG_DONTWAIT
, rpc
->udp_dest
, sizeof(struct sockaddr_in
)) < 0) {
133 rpc_set_error(rpc
, "Sendto failed with errno %s", strerror(errno
));
134 rpc_free_pdu(rpc
, pdu
);
137 SLIST_ADD_END(&rpc
->waitpdu
, pdu
);
141 /* write recordmarker */
142 zdr_setpos(&pdu
->zdr
, 0);
143 recordmarker
= (size
- 4) | 0x80000000;
144 zdr_int(&pdu
->zdr
, &recordmarker
);
146 pdu
->outdata
.size
= size
;
147 pdu
->outdata
.data
= malloc(pdu
->outdata
.size
);
148 if (pdu
->outdata
.data
== NULL
) {
149 rpc_set_error(rpc
, "Out of memory. Failed to allocate buffer for pdu\n");
150 rpc_free_pdu(rpc
, pdu
);
154 memcpy(pdu
->outdata
.data
, rpc
->encodebuf
, pdu
->outdata
.size
);
155 SLIST_ADD_END(&rpc
->outqueue
, pdu
);
160 int rpc_get_pdu_size(char *buf
)
164 size
= ntohl(*(uint32_t *)buf
);
166 return (size
& 0x7fffffff) + 4;
169 static int rpc_process_reply(struct rpc_context
*rpc
, struct rpc_pdu
*pdu
, ZDR
*zdr
)
173 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
175 memset(&msg
, 0, sizeof(struct rpc_msg
));
176 msg
.body
.rbody
.reply
.areply
.verf
= _null_auth
;
177 if (pdu
->zdr_decode_bufsize
> 0) {
178 if (pdu
->zdr_decode_buf
!= NULL
) {
179 free(pdu
->zdr_decode_buf
);
181 pdu
->zdr_decode_buf
= malloc(pdu
->zdr_decode_bufsize
);
182 if (pdu
->zdr_decode_buf
== NULL
) {
183 rpc_set_error(rpc
, "zdr_replymsg failed in portmap_getport_reply");
184 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Failed to allocate buffer for decoding of ZDR reply", pdu
->private_data
);
187 memset(pdu
->zdr_decode_buf
, 0, pdu
->zdr_decode_bufsize
);
189 msg
.body
.rbody
.reply
.areply
.reply_data
.results
.where
= pdu
->zdr_decode_buf
;
190 msg
.body
.rbody
.reply
.areply
.reply_data
.results
.proc
= pdu
->zdr_decode_fn
;
192 if (zdr_replymsg(zdr
, &msg
) == 0) {
193 rpc_set_error(rpc
, "zdr_replymsg failed in portmap_getport_reply");
194 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Message rejected by server", pdu
->private_data
);
195 if (pdu
->zdr_decode_buf
!= NULL
) {
196 free(pdu
->zdr_decode_buf
);
197 pdu
->zdr_decode_buf
= NULL
;
201 if (msg
.body
.rbody
.stat
!= MSG_ACCEPTED
) {
202 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "RPC Packet not accepted by the server", pdu
->private_data
);
205 switch (msg
.body
.rbody
.reply
.areply
.stat
) {
207 pdu
->cb(rpc
, RPC_STATUS_SUCCESS
, pdu
->zdr_decode_buf
, pdu
->private_data
);
210 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program not available", pdu
->private_data
);
213 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Program version mismatch", pdu
->private_data
);
216 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Procedure not available", pdu
->private_data
);
219 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: Garbage arguments", pdu
->private_data
);
222 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Server responded: System Error", pdu
->private_data
);
225 pdu
->cb(rpc
, RPC_STATUS_ERROR
, "Unknown rpc response from server", pdu
->private_data
);
232 int rpc_process_pdu(struct rpc_context
*rpc
, char *buf
, int size
)
236 int pos
, recordmarker
= 0;
238 char *reasbuf
= NULL
;
240 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
242 memset(&zdr
, 0, sizeof(ZDR
));
244 zdrmem_create(&zdr
, buf
, size
, ZDR_DECODE
);
245 if (rpc
->is_udp
== 0) {
246 if (zdr_int(&zdr
, &recordmarker
) == 0) {
247 rpc_set_error(rpc
, "zdr_int reading recordmarker failed");
251 if (!(recordmarker
&0x80000000)) {
253 if (rpc_add_fragment(rpc
, buf
+4, size
-4) != 0) {
254 rpc_set_error(rpc
, "Failed to queue fragment for reassembly.");
262 if (recordmarker
!= 0 && rpc
->fragments
!= NULL
) {
263 struct rpc_fragment
*fragment
;
264 uint32_t total
= size
- 4;
268 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
269 total
+= fragment
->size
;
272 reasbuf
= malloc(total
);
273 if (reasbuf
== NULL
) {
274 rpc_set_error(rpc
, "Failed to reassemble PDU");
275 rpc_free_all_fragments(rpc
);
279 for (fragment
= rpc
->fragments
; fragment
; fragment
= fragment
->next
) {
280 memcpy(ptr
, fragment
->data
, fragment
->size
);
281 ptr
+= fragment
->size
;
283 memcpy(ptr
, buf
+ 4, size
- 4);
284 zdrmem_create(&zdr
, reasbuf
, total
, ZDR_DECODE
);
285 rpc_free_all_fragments(rpc
);
288 pos
= zdr_getpos(&zdr
);
289 if (zdr_int(&zdr
, (int *)&xid
) == 0) {
290 rpc_set_error(rpc
, "zdr_int reading xid failed");
292 if (reasbuf
!= NULL
) {
297 zdr_setpos(&zdr
, pos
);
299 for (pdu
=rpc
->waitpdu
; pdu
; pdu
=pdu
->next
) {
300 if (pdu
->xid
!= xid
) {
303 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
304 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
306 if (rpc_process_reply(rpc
, pdu
, &zdr
) != 0) {
307 rpc_set_error(rpc
, "rpc_procdess_reply failed");
310 if (rpc
->is_udp
== 0 || rpc
->is_broadcast
== 0) {
311 rpc_free_pdu(rpc
, pdu
);
313 if (reasbuf
!= NULL
) {
318 rpc_set_error(rpc
, "No matching pdu found for xid:%d", xid
);
320 if (reasbuf
!= NULL
) {