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