Commit | Line | Data |
---|---|---|
84004dbf RS |
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 | */ | |
a8a1b858 M |
17 | #ifdef WIN32 |
18 | #include "win32_compat.h" | |
19 | #ifndef MSG_DONTWAIT | |
6874f61e | 20 | #define MSG_DONTWAIT 0 |
a8a1b858 | 21 | #endif |
6874f61e | 22 | #else |
84004dbf | 23 | #include <strings.h> |
a8a1b858 | 24 | #endif/*WIN32*/ |
6874f61e RS |
25 | |
26 | #include <stdio.h> | |
98f5fee8 | 27 | #include <stdlib.h> |
4a2b0876 | 28 | #include <assert.h> |
a669857d | 29 | #include <errno.h> |
98f5fee8 | 30 | #include <rpc/rpc.h> |
84004dbf RS |
31 | #include <rpc/xdr.h> |
32 | #include <rpc/rpc_msg.h> | |
33 | #include "slist.h" | |
34 | #include "libnfs.h" | |
35 | #include "libnfs-raw.h" | |
36 | #include "libnfs-private.h" | |
37 | ||
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) | |
39 | { | |
40 | struct rpc_pdu *pdu; | |
41 | struct rpc_msg msg; | |
42 | ||
4a2b0876 | 43 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
84004dbf RS |
44 | |
45 | pdu = malloc(sizeof(struct rpc_pdu)); | |
46 | if (pdu == NULL) { | |
1896d37b | 47 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
84004dbf RS |
48 | return NULL; |
49 | } | |
ea98629a | 50 | memset(pdu, 0, sizeof(struct rpc_pdu)); |
84004dbf RS |
51 | pdu->xid = rpc->xid++; |
52 | pdu->cb = cb; | |
53 | pdu->private_data = private_data; | |
54 | pdu->xdr_decode_fn = xdr_decode_fn; | |
55 | pdu->xdr_decode_bufsize = xdr_decode_bufsize; | |
56 | ||
57 | xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE); | |
a1992412 RS |
58 | if (rpc->is_udp == 0) { |
59 | xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */ | |
60 | } | |
84004dbf | 61 | |
ea98629a | 62 | memset(&msg, 0, sizeof(struct rpc_msg)); |
84004dbf RS |
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; | |
71 | ||
72 | if (xdr_callmsg(&pdu->xdr, &msg) == 0) { | |
1896d37b | 73 | rpc_set_error(rpc, "xdr_callmsg failed"); |
84004dbf RS |
74 | xdr_destroy(&pdu->xdr); |
75 | free(pdu); | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | return pdu; | |
80 | } | |
81 | ||
4a2b0876 | 82 | void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
84004dbf | 83 | { |
4a2b0876 RS |
84 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
85 | ||
84004dbf RS |
86 | if (pdu->outdata.data != NULL) { |
87 | free(pdu->outdata.data); | |
88 | pdu->outdata.data = NULL; | |
89 | } | |
90 | ||
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; | |
95 | } | |
96 | ||
97 | xdr_destroy(&pdu->xdr); | |
df5af25f | 98 | |
84004dbf RS |
99 | free(pdu); |
100 | } | |
101 | ||
102 | ||
103 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) | |
104 | { | |
105 | int size, recordmarker; | |
106 | ||
4a2b0876 RS |
107 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
108 | ||
84004dbf RS |
109 | size = xdr_getpos(&pdu->xdr); |
110 | ||
a669857d RS |
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); | |
116 | return -1; | |
117 | } | |
118 | SLIST_ADD_END(&rpc->waitpdu, pdu); | |
119 | return 0; | |
120 | } | |
121 | ||
84004dbf RS |
122 | /* write recordmarker */ |
123 | xdr_setpos(&pdu->xdr, 0); | |
124 | recordmarker = (size - 4) | 0x80000000; | |
125 | xdr_int(&pdu->xdr, &recordmarker); | |
126 | ||
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); | |
132 | return -1; | |
133 | } | |
134 | ||
135 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); | |
136 | SLIST_ADD_END(&rpc->outqueue, pdu); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | int rpc_get_pdu_size(char *buf) | |
142 | { | |
143 | uint32_t size; | |
144 | ||
145 | size = ntohl(*(uint32_t *)buf); | |
146 | ||
84004dbf RS |
147 | return (size & 0x7fffffff) + 4; |
148 | } | |
149 | ||
150 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr) | |
151 | { | |
152 | struct rpc_msg msg; | |
153 | ||
4a2b0876 RS |
154 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
155 | ||
ea98629a | 156 | memset(&msg, 0, sizeof(struct rpc_msg)); |
84004dbf RS |
157 | msg.acpted_rply.ar_verf = _null_auth; |
158 | if (pdu->xdr_decode_bufsize > 0) { | |
1b9917b8 RS |
159 | if (pdu->xdr_decode_buf != NULL) { |
160 | free(pdu->xdr_decode_buf); | |
161 | } | |
84004dbf RS |
162 | pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); |
163 | if (pdu->xdr_decode_buf == NULL) { | |
1896d37b | 164 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
165 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); |
166 | return 0; | |
167 | } | |
ea98629a | 168 | memset(pdu->xdr_decode_buf, 0, pdu->xdr_decode_bufsize); |
84004dbf RS |
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) { | |
1896d37b | 174 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
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; | |
d678b73e | 217 | int pos, recordmarker = 0; |
84004dbf | 218 | unsigned int xid; |
d678b73e | 219 | char *reasbuf = NULL; |
84004dbf | 220 | |
4a2b0876 RS |
221 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
222 | ||
ea98629a | 223 | memset(&xdr, 0, sizeof(XDR)); |
84004dbf RS |
224 | |
225 | xdrmem_create(&xdr, buf, size, XDR_DECODE); | |
a669857d RS |
226 | if (rpc->is_udp == 0) { |
227 | if (xdr_int(&xdr, &recordmarker) == 0) { | |
228 | rpc_set_error(rpc, "xdr_int reading recordmarker failed"); | |
229 | xdr_destroy(&xdr); | |
230 | return -1; | |
231 | } | |
d678b73e RS |
232 | if (!(recordmarker&0x80000000)) { |
233 | xdr_destroy(&xdr); | |
234 | if (rpc_add_fragment(rpc, buf+4, size-4) != 0) { | |
235 | rpc_set_error(rpc, "Failed to queue fragment for reassembly."); | |
236 | return -1; | |
237 | } | |
238 | return 0; | |
239 | } | |
84004dbf | 240 | } |
d678b73e RS |
241 | |
242 | /* reassembly */ | |
243 | if (recordmarker != 0 && rpc->fragments != NULL) { | |
244 | struct rpc_fragment *fragment; | |
183451cf | 245 | uint64_t total = size - 4; |
d678b73e RS |
246 | char *ptr; |
247 | ||
248 | xdr_destroy(&xdr); | |
249 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { | |
250 | total += fragment->size; | |
251 | } | |
252 | ||
253 | reasbuf = malloc(total); | |
254 | if (reasbuf == NULL) { | |
255 | rpc_set_error(rpc, "Failed to reassemble PDU"); | |
256 | rpc_free_all_fragments(rpc); | |
257 | return -1; | |
258 | } | |
259 | ptr = reasbuf; | |
260 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { | |
261 | memcpy(ptr, fragment->data, fragment->size); | |
262 | ptr += fragment->size; | |
263 | } | |
264 | memcpy(ptr, buf + 4, size - 4); | |
265 | xdrmem_create(&xdr, reasbuf, total, XDR_DECODE); | |
266 | rpc_free_all_fragments(rpc); | |
267 | } | |
268 | ||
84004dbf RS |
269 | pos = xdr_getpos(&xdr); |
270 | if (xdr_int(&xdr, (int *)&xid) == 0) { | |
1896d37b | 271 | rpc_set_error(rpc, "xdr_int reading xid failed"); |
84004dbf | 272 | xdr_destroy(&xdr); |
d678b73e RS |
273 | if (reasbuf != NULL) { |
274 | free(reasbuf); | |
275 | } | |
84004dbf RS |
276 | return -1; |
277 | } | |
278 | xdr_setpos(&xdr, pos); | |
279 | ||
280 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
281 | if (pdu->xid != xid) { | |
282 | continue; | |
283 | } | |
a669857d RS |
284 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
285 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
286 | } | |
84004dbf | 287 | if (rpc_process_reply(rpc, pdu, &xdr) != 0) { |
1896d37b | 288 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
84004dbf RS |
289 | } |
290 | xdr_destroy(&xdr); | |
a669857d RS |
291 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
292 | rpc_free_pdu(rpc, pdu); | |
293 | } | |
d678b73e RS |
294 | if (reasbuf != NULL) { |
295 | free(reasbuf); | |
296 | } | |
84004dbf RS |
297 | return 0; |
298 | } | |
1896d37b | 299 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
84004dbf | 300 | xdr_destroy(&xdr); |
d678b73e RS |
301 | if (reasbuf != NULL) { |
302 | free(reasbuf); | |
303 | } | |
84004dbf RS |
304 | return -1; |
305 | } | |
306 |