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 | */ | |
72897005 RS |
17 | #ifdef HAVE_CONFIG_H |
18 | #include "config.h" | |
19 | #endif | |
20 | ||
108c622a RS |
21 | #ifdef AROS |
22 | #include "aros_compat.h" | |
23 | #endif | |
24 | ||
a8a1b858 M |
25 | #ifdef WIN32 |
26 | #include "win32_compat.h" | |
6874f61e | 27 | #else |
84004dbf | 28 | #include <strings.h> |
67a9f57e | 29 | #include <sys/socket.h> |
a8a1b858 | 30 | #endif/*WIN32*/ |
6874f61e | 31 | |
72897005 RS |
32 | #ifdef HAVE_NETINET_IN_H |
33 | #include <netinet/in.h> | |
34 | #endif | |
35 | ||
6874f61e | 36 | #include <stdio.h> |
98f5fee8 | 37 | #include <stdlib.h> |
763cd6e3 | 38 | #include <string.h> |
f3a75078 | 39 | #include <assert.h> |
a669857d | 40 | #include <errno.h> |
84004dbf | 41 | #include "slist.h" |
763cd6e3 | 42 | #include "libnfs-zdr.h" |
84004dbf RS |
43 | #include "libnfs.h" |
44 | #include "libnfs-raw.h" | |
45 | #include "libnfs-private.h" | |
46 | ||
763cd6e3 | 47 | 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) |
84004dbf RS |
48 | { |
49 | struct rpc_pdu *pdu; | |
50 | struct rpc_msg msg; | |
51 | ||
f3a75078 | 52 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
84004dbf RS |
53 | |
54 | pdu = malloc(sizeof(struct rpc_pdu)); | |
55 | if (pdu == NULL) { | |
1896d37b | 56 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
84004dbf RS |
57 | return NULL; |
58 | } | |
ea98629a | 59 | memset(pdu, 0, sizeof(struct rpc_pdu)); |
84004dbf RS |
60 | pdu->xid = rpc->xid++; |
61 | pdu->cb = cb; | |
62 | pdu->private_data = private_data; | |
763cd6e3 RS |
63 | pdu->zdr_decode_fn = zdr_decode_fn; |
64 | pdu->zdr_decode_bufsize = zdr_decode_bufsize; | |
84004dbf | 65 | |
763cd6e3 | 66 | zdrmem_create(&pdu->zdr, rpc->encodebuf, rpc->encodebuflen, ZDR_ENCODE); |
a1992412 | 67 | if (rpc->is_udp == 0) { |
763cd6e3 | 68 | zdr_setpos(&pdu->zdr, 4); /* skip past the record marker */ |
a1992412 | 69 | } |
84004dbf | 70 | |
ea98629a | 71 | memset(&msg, 0, sizeof(struct rpc_msg)); |
aab6538b RS |
72 | msg.xid = pdu->xid; |
73 | msg.direction = CALL; | |
74 | msg.body.cbody.rpcvers = RPC_MSG_VERSION; | |
75 | msg.body.cbody.prog = program; | |
76 | msg.body.cbody.vers = version; | |
77 | msg.body.cbody.proc = procedure; | |
78 | msg.body.cbody.cred = rpc->auth->ah_cred; | |
79 | msg.body.cbody.verf = rpc->auth->ah_verf; | |
84004dbf | 80 | |
763cd6e3 RS |
81 | if (zdr_callmsg(&pdu->zdr, &msg) == 0) { |
82 | rpc_set_error(rpc, "zdr_callmsg failed"); | |
83 | zdr_destroy(&pdu->zdr); | |
84004dbf RS |
84 | free(pdu); |
85 | return NULL; | |
86 | } | |
87 | ||
88 | return pdu; | |
89 | } | |
90 | ||
f3a75078 | 91 | void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) |
84004dbf | 92 | { |
f3a75078 RS |
93 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
94 | ||
84004dbf RS |
95 | if (pdu->outdata.data != NULL) { |
96 | free(pdu->outdata.data); | |
97 | pdu->outdata.data = NULL; | |
98 | } | |
99 | ||
763cd6e3 RS |
100 | if (pdu->zdr_decode_buf != NULL) { |
101 | zdr_free(pdu->zdr_decode_fn, pdu->zdr_decode_buf); | |
102 | free(pdu->zdr_decode_buf); | |
103 | pdu->zdr_decode_buf = NULL; | |
84004dbf RS |
104 | } |
105 | ||
763cd6e3 | 106 | zdr_destroy(&pdu->zdr); |
df5af25f | 107 | |
84004dbf RS |
108 | free(pdu); |
109 | } | |
110 | ||
111 | ||
112 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) | |
113 | { | |
114 | int size, recordmarker; | |
115 | ||
f3a75078 RS |
116 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
117 | ||
763cd6e3 | 118 | size = zdr_getpos(&pdu->zdr); |
84004dbf | 119 | |
a669857d RS |
120 | /* for udp we dont queue, we just send it straight away */ |
121 | if (rpc->is_udp != 0) { | |
763cd6e3 | 122 | // XXX add a rpc->udp_dest_sock_size and get rid of sys/socket.h and netinet/in.h |
a669857d RS |
123 | if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) { |
124 | rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno)); | |
125 | rpc_free_pdu(rpc, pdu); | |
126 | return -1; | |
127 | } | |
128 | SLIST_ADD_END(&rpc->waitpdu, pdu); | |
129 | return 0; | |
130 | } | |
131 | ||
84004dbf | 132 | /* write recordmarker */ |
763cd6e3 | 133 | zdr_setpos(&pdu->zdr, 0); |
84004dbf | 134 | recordmarker = (size - 4) | 0x80000000; |
763cd6e3 | 135 | zdr_int(&pdu->zdr, &recordmarker); |
84004dbf RS |
136 | |
137 | pdu->outdata.size = size; | |
138 | pdu->outdata.data = malloc(pdu->outdata.size); | |
139 | if (pdu->outdata.data == NULL) { | |
140 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); | |
141 | rpc_free_pdu(rpc, pdu); | |
142 | return -1; | |
143 | } | |
144 | ||
145 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); | |
146 | SLIST_ADD_END(&rpc->outqueue, pdu); | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | int rpc_get_pdu_size(char *buf) | |
152 | { | |
153 | uint32_t size; | |
154 | ||
155 | size = ntohl(*(uint32_t *)buf); | |
156 | ||
84004dbf RS |
157 | return (size & 0x7fffffff) + 4; |
158 | } | |
159 | ||
763cd6e3 | 160 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, ZDR *zdr) |
84004dbf RS |
161 | { |
162 | struct rpc_msg msg; | |
163 | ||
f3a75078 RS |
164 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
165 | ||
ea98629a | 166 | memset(&msg, 0, sizeof(struct rpc_msg)); |
aab6538b | 167 | msg.body.rbody.reply.areply.verf = _null_auth; |
763cd6e3 RS |
168 | if (pdu->zdr_decode_bufsize > 0) { |
169 | if (pdu->zdr_decode_buf != NULL) { | |
170 | free(pdu->zdr_decode_buf); | |
1b9917b8 | 171 | } |
763cd6e3 RS |
172 | pdu->zdr_decode_buf = malloc(pdu->zdr_decode_bufsize); |
173 | if (pdu->zdr_decode_buf == NULL) { | |
174 | rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply"); | |
175 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of ZDR reply", pdu->private_data); | |
84004dbf RS |
176 | return 0; |
177 | } | |
763cd6e3 | 178 | memset(pdu->zdr_decode_buf, 0, pdu->zdr_decode_bufsize); |
84004dbf | 179 | } |
aab6538b RS |
180 | msg.body.rbody.reply.areply.reply_data.results.where = pdu->zdr_decode_buf; |
181 | msg.body.rbody.reply.areply.reply_data.results.proc = pdu->zdr_decode_fn; | |
84004dbf | 182 | |
763cd6e3 RS |
183 | if (zdr_replymsg(zdr, &msg) == 0) { |
184 | rpc_set_error(rpc, "zdr_replymsg failed in portmap_getport_reply"); | |
84004dbf | 185 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
763cd6e3 RS |
186 | if (pdu->zdr_decode_buf != NULL) { |
187 | free(pdu->zdr_decode_buf); | |
188 | pdu->zdr_decode_buf = NULL; | |
84004dbf RS |
189 | } |
190 | return 0; | |
191 | } | |
aab6538b | 192 | if (msg.body.rbody.stat != MSG_ACCEPTED) { |
84004dbf RS |
193 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); |
194 | return 0; | |
195 | } | |
aab6538b | 196 | switch (msg.body.rbody.reply.areply.stat) { |
84004dbf | 197 | case SUCCESS: |
763cd6e3 | 198 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->zdr_decode_buf, pdu->private_data); |
84004dbf RS |
199 | break; |
200 | case PROG_UNAVAIL: | |
201 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); | |
202 | break; | |
203 | case PROG_MISMATCH: | |
204 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); | |
205 | break; | |
206 | case PROC_UNAVAIL: | |
207 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); | |
208 | break; | |
209 | case GARBAGE_ARGS: | |
210 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); | |
211 | break; | |
212 | case SYSTEM_ERR: | |
213 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); | |
214 | break; | |
215 | default: | |
216 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); | |
217 | break; | |
218 | } | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
223 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) | |
224 | { | |
225 | struct rpc_pdu *pdu; | |
763cd6e3 | 226 | ZDR zdr; |
d678b73e | 227 | int pos, recordmarker = 0; |
84004dbf | 228 | unsigned int xid; |
d678b73e | 229 | char *reasbuf = NULL; |
84004dbf | 230 | |
f3a75078 RS |
231 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
232 | ||
763cd6e3 | 233 | memset(&zdr, 0, sizeof(ZDR)); |
84004dbf | 234 | |
763cd6e3 | 235 | zdrmem_create(&zdr, buf, size, ZDR_DECODE); |
a669857d | 236 | if (rpc->is_udp == 0) { |
763cd6e3 RS |
237 | if (zdr_int(&zdr, &recordmarker) == 0) { |
238 | rpc_set_error(rpc, "zdr_int reading recordmarker failed"); | |
239 | zdr_destroy(&zdr); | |
a669857d RS |
240 | return -1; |
241 | } | |
d678b73e | 242 | if (!(recordmarker&0x80000000)) { |
763cd6e3 | 243 | zdr_destroy(&zdr); |
d678b73e RS |
244 | if (rpc_add_fragment(rpc, buf+4, size-4) != 0) { |
245 | rpc_set_error(rpc, "Failed to queue fragment for reassembly."); | |
246 | return -1; | |
247 | } | |
248 | return 0; | |
249 | } | |
84004dbf | 250 | } |
d678b73e RS |
251 | |
252 | /* reassembly */ | |
253 | if (recordmarker != 0 && rpc->fragments != NULL) { | |
254 | struct rpc_fragment *fragment; | |
574095b9 | 255 | uint32_t total = size - 4; |
d678b73e RS |
256 | char *ptr; |
257 | ||
763cd6e3 | 258 | zdr_destroy(&zdr); |
d678b73e RS |
259 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { |
260 | total += fragment->size; | |
261 | } | |
262 | ||
263 | reasbuf = malloc(total); | |
264 | if (reasbuf == NULL) { | |
265 | rpc_set_error(rpc, "Failed to reassemble PDU"); | |
266 | rpc_free_all_fragments(rpc); | |
267 | return -1; | |
268 | } | |
269 | ptr = reasbuf; | |
270 | for (fragment = rpc->fragments; fragment; fragment = fragment->next) { | |
271 | memcpy(ptr, fragment->data, fragment->size); | |
272 | ptr += fragment->size; | |
273 | } | |
274 | memcpy(ptr, buf + 4, size - 4); | |
763cd6e3 | 275 | zdrmem_create(&zdr, reasbuf, total, ZDR_DECODE); |
d678b73e RS |
276 | rpc_free_all_fragments(rpc); |
277 | } | |
278 | ||
763cd6e3 RS |
279 | pos = zdr_getpos(&zdr); |
280 | if (zdr_int(&zdr, (int *)&xid) == 0) { | |
281 | rpc_set_error(rpc, "zdr_int reading xid failed"); | |
282 | zdr_destroy(&zdr); | |
d678b73e RS |
283 | if (reasbuf != NULL) { |
284 | free(reasbuf); | |
285 | } | |
84004dbf RS |
286 | return -1; |
287 | } | |
763cd6e3 | 288 | zdr_setpos(&zdr, pos); |
84004dbf RS |
289 | |
290 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
291 | if (pdu->xid != xid) { | |
292 | continue; | |
293 | } | |
a669857d RS |
294 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
295 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
296 | } | |
763cd6e3 | 297 | if (rpc_process_reply(rpc, pdu, &zdr) != 0) { |
1896d37b | 298 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
84004dbf | 299 | } |
763cd6e3 | 300 | zdr_destroy(&zdr); |
a669857d RS |
301 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
302 | rpc_free_pdu(rpc, pdu); | |
303 | } | |
d678b73e RS |
304 | if (reasbuf != NULL) { |
305 | free(reasbuf); | |
306 | } | |
84004dbf RS |
307 | return 0; |
308 | } | |
1896d37b | 309 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
763cd6e3 | 310 | zdr_destroy(&zdr); |
d678b73e RS |
311 | if (reasbuf != NULL) { |
312 | free(reasbuf); | |
313 | } | |
84004dbf RS |
314 | return -1; |
315 | } | |
316 |