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