[win32] - make it compile on win32
[deb_libnfs.git] / lib / pdu.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #ifdef WIN32
18 #include "win32_compat.h"
19 #ifndef MSG_DONTWAIT
20 #define MSG_DONTWAIT 0
21 #endif
22 #else
23 #include <strings.h>
24 #endif/*WIN32*/
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <rpc/rpc.h>
30 #include <rpc/xdr.h>
31 #include <rpc/rpc_msg.h>
32 #include "slist.h"
33 #include "libnfs.h"
34 #include "libnfs-raw.h"
35 #include "libnfs-private.h"
36
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)
38 {
39 struct rpc_pdu *pdu;
40 struct rpc_msg msg;
41
42 if (rpc == NULL) {
43 return NULL;
44 }
45
46 pdu = malloc(sizeof(struct rpc_pdu));
47 if (pdu == NULL) {
48 rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure");
49 return NULL;
50 }
51 memset(pdu, 0, sizeof(struct rpc_pdu));
52 pdu->xid = rpc->xid++;
53 pdu->cb = cb;
54 pdu->private_data = private_data;
55 pdu->xdr_decode_fn = xdr_decode_fn;
56 pdu->xdr_decode_bufsize = xdr_decode_bufsize;
57
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 */
61 }
62
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;
72
73 if (xdr_callmsg(&pdu->xdr, &msg) == 0) {
74 rpc_set_error(rpc, "xdr_callmsg failed");
75 xdr_destroy(&pdu->xdr);
76 free(pdu);
77 return NULL;
78 }
79
80 return pdu;
81 }
82
83 void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu)
84 {
85 if (pdu->outdata.data != NULL) {
86 free(pdu->outdata.data);
87 pdu->outdata.data = NULL;
88 }
89
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;
94 }
95
96 xdr_destroy(&pdu->xdr);
97
98 free(pdu);
99 }
100
101
102 int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
103 {
104 int size, recordmarker;
105
106 size = xdr_getpos(&pdu->xdr);
107
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);
113 return -1;
114 }
115 SLIST_ADD_END(&rpc->waitpdu, pdu);
116 return 0;
117 }
118
119 /* write recordmarker */
120 xdr_setpos(&pdu->xdr, 0);
121 recordmarker = (size - 4) | 0x80000000;
122 xdr_int(&pdu->xdr, &recordmarker);
123
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);
129 return -1;
130 }
131
132 memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size);
133 SLIST_ADD_END(&rpc->outqueue, pdu);
134
135 return 0;
136 }
137
138 int rpc_get_pdu_size(char *buf)
139 {
140 uint32_t size;
141
142 size = ntohl(*(uint32_t *)buf);
143
144 if ((size & 0x80000000) == 0) {
145 /* cant handle oncrpc fragments */
146 return -1;
147 }
148
149 return (size & 0x7fffffff) + 4;
150 }
151
152 static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr)
153 {
154 struct rpc_msg msg;
155
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);
161 }
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);
166 return 0;
167 }
168 memset(pdu->xdr_decode_buf, 0, pdu->xdr_decode_bufsize);
169 }
170 msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf;
171 msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn;
172
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;
179 }
180 return 0;
181 }
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);
184 return 0;
185 }
186 switch (msg.rm_reply.rp_acpt.ar_stat) {
187 case SUCCESS:
188 pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data);
189 break;
190 case PROG_UNAVAIL:
191 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data);
192 break;
193 case PROG_MISMATCH:
194 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data);
195 break;
196 case PROC_UNAVAIL:
197 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data);
198 break;
199 case GARBAGE_ARGS:
200 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data);
201 break;
202 case SYSTEM_ERR:
203 pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data);
204 break;
205 default:
206 pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data);
207 break;
208 }
209
210 return 0;
211 }
212
213 int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size)
214 {
215 struct rpc_pdu *pdu;
216 XDR xdr;
217 int pos, recordmarker;
218 unsigned int xid;
219
220 memset(&xdr, 0, sizeof(XDR));
221
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");
226 xdr_destroy(&xdr);
227 return -1;
228 }
229 }
230 pos = xdr_getpos(&xdr);
231 if (xdr_int(&xdr, (int *)&xid) == 0) {
232 rpc_set_error(rpc, "xdr_int reading xid failed");
233 xdr_destroy(&xdr);
234 return -1;
235 }
236 xdr_setpos(&xdr, pos);
237
238 for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) {
239 if (pdu->xid != xid) {
240 continue;
241 }
242 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
243 SLIST_REMOVE(&rpc->waitpdu, pdu);
244 }
245 if (rpc_process_reply(rpc, pdu, &xdr) != 0) {
246 rpc_set_error(rpc, "rpc_procdess_reply failed");
247 }
248 xdr_destroy(&xdr);
249 if (rpc->is_udp == 0 || rpc->is_broadcast == 0) {
250 rpc_free_pdu(rpc, pdu);
251 }
252 return 0;
253 }
254 rpc_set_error(rpc, "No matching pdu found for xid:%d", xid);
255 xdr_destroy(&xdr);
256 return -1;
257 }
258