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 | */ | |
17 | ||
6874f61e RS |
18 | #if defined(WIN32) |
19 | #include <winsock2.h> | |
20 | #define MSG_DONTWAIT 0 | |
21 | #else | |
84004dbf | 22 | #include <strings.h> |
6874f61e RS |
23 | #endif |
24 | ||
25 | #include <stdio.h> | |
98f5fee8 | 26 | #include <stdlib.h> |
a669857d | 27 | #include <errno.h> |
98f5fee8 | 28 | #include <rpc/rpc.h> |
84004dbf RS |
29 | #include <rpc/xdr.h> |
30 | #include <rpc/rpc_msg.h> | |
31 | #include "slist.h" | |
32 | #include "libnfs.h" | |
33 | #include "libnfs-raw.h" | |
34 | #include "libnfs-private.h" | |
35 | ||
36 | 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) | |
37 | { | |
38 | struct rpc_pdu *pdu; | |
39 | struct rpc_msg msg; | |
40 | ||
41 | if (rpc == NULL) { | |
84004dbf RS |
42 | return NULL; |
43 | } | |
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 | } | |
42f43e48 | 50 | bzero((char *)pdu, 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 | |
42f43e48 | 62 | bzero((char *)&msg, 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 | ||
82 | void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu) | |
83 | { | |
84 | if (pdu->outdata.data != NULL) { | |
85 | free(pdu->outdata.data); | |
86 | pdu->outdata.data = NULL; | |
87 | } | |
88 | ||
89 | if (pdu->xdr_decode_buf != NULL) { | |
90 | xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf); | |
91 | free(pdu->xdr_decode_buf); | |
92 | pdu->xdr_decode_buf = NULL; | |
93 | } | |
94 | ||
95 | xdr_destroy(&pdu->xdr); | |
df5af25f | 96 | |
84004dbf RS |
97 | free(pdu); |
98 | } | |
99 | ||
100 | ||
101 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) | |
102 | { | |
103 | int size, recordmarker; | |
104 | ||
105 | size = xdr_getpos(&pdu->xdr); | |
106 | ||
a669857d RS |
107 | /* for udp we dont queue, we just send it straight away */ |
108 | if (rpc->is_udp != 0) { | |
109 | if (sendto(rpc->fd, rpc->encodebuf, size, MSG_DONTWAIT, rpc->udp_dest, sizeof(struct sockaddr_in)) < 0) { | |
110 | rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno)); | |
111 | rpc_free_pdu(rpc, pdu); | |
112 | return -1; | |
113 | } | |
114 | SLIST_ADD_END(&rpc->waitpdu, pdu); | |
115 | return 0; | |
116 | } | |
117 | ||
84004dbf RS |
118 | /* write recordmarker */ |
119 | xdr_setpos(&pdu->xdr, 0); | |
120 | recordmarker = (size - 4) | 0x80000000; | |
121 | xdr_int(&pdu->xdr, &recordmarker); | |
122 | ||
123 | pdu->outdata.size = size; | |
124 | pdu->outdata.data = malloc(pdu->outdata.size); | |
125 | if (pdu->outdata.data == NULL) { | |
126 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); | |
127 | rpc_free_pdu(rpc, pdu); | |
128 | return -1; | |
129 | } | |
130 | ||
131 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); | |
132 | SLIST_ADD_END(&rpc->outqueue, pdu); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | int rpc_get_pdu_size(char *buf) | |
138 | { | |
139 | uint32_t size; | |
140 | ||
141 | size = ntohl(*(uint32_t *)buf); | |
142 | ||
143 | if ((size & 0x80000000) == 0) { | |
1896d37b | 144 | /* cant handle oncrpc fragments */ |
84004dbf RS |
145 | return -1; |
146 | } | |
147 | ||
148 | return (size & 0x7fffffff) + 4; | |
149 | } | |
150 | ||
151 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr) | |
152 | { | |
153 | struct rpc_msg msg; | |
154 | ||
42f43e48 | 155 | bzero((char *)&msg, sizeof(struct rpc_msg)); |
84004dbf RS |
156 | msg.acpted_rply.ar_verf = _null_auth; |
157 | if (pdu->xdr_decode_bufsize > 0) { | |
1b9917b8 RS |
158 | if (pdu->xdr_decode_buf != NULL) { |
159 | free(pdu->xdr_decode_buf); | |
160 | } | |
84004dbf RS |
161 | pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); |
162 | if (pdu->xdr_decode_buf == NULL) { | |
1896d37b | 163 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
164 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); |
165 | return 0; | |
166 | } | |
42f43e48 | 167 | bzero((char *)pdu->xdr_decode_buf, pdu->xdr_decode_bufsize); |
84004dbf RS |
168 | } |
169 | msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf; | |
170 | msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn; | |
171 | ||
172 | if (xdr_replymsg(xdr, &msg) == 0) { | |
1896d37b | 173 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
174 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
175 | if (pdu->xdr_decode_buf != NULL) { | |
176 | free(pdu->xdr_decode_buf); | |
177 | pdu->xdr_decode_buf = NULL; | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | if (msg.rm_reply.rp_stat != MSG_ACCEPTED) { | |
182 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); | |
183 | return 0; | |
184 | } | |
185 | switch (msg.rm_reply.rp_acpt.ar_stat) { | |
186 | case SUCCESS: | |
187 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data); | |
188 | break; | |
189 | case PROG_UNAVAIL: | |
190 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); | |
191 | break; | |
192 | case PROG_MISMATCH: | |
193 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); | |
194 | break; | |
195 | case PROC_UNAVAIL: | |
196 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); | |
197 | break; | |
198 | case GARBAGE_ARGS: | |
199 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); | |
200 | break; | |
201 | case SYSTEM_ERR: | |
202 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); | |
203 | break; | |
204 | default: | |
205 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); | |
206 | break; | |
207 | } | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) | |
213 | { | |
214 | struct rpc_pdu *pdu; | |
215 | XDR xdr; | |
216 | int pos, recordmarker; | |
217 | unsigned int xid; | |
218 | ||
42f43e48 | 219 | bzero((char *)&xdr, sizeof(XDR)); |
84004dbf RS |
220 | |
221 | xdrmem_create(&xdr, buf, size, XDR_DECODE); | |
a669857d RS |
222 | if (rpc->is_udp == 0) { |
223 | if (xdr_int(&xdr, &recordmarker) == 0) { | |
224 | rpc_set_error(rpc, "xdr_int reading recordmarker failed"); | |
225 | xdr_destroy(&xdr); | |
226 | return -1; | |
227 | } | |
84004dbf RS |
228 | } |
229 | pos = xdr_getpos(&xdr); | |
230 | if (xdr_int(&xdr, (int *)&xid) == 0) { | |
1896d37b | 231 | rpc_set_error(rpc, "xdr_int reading xid failed"); |
84004dbf RS |
232 | xdr_destroy(&xdr); |
233 | return -1; | |
234 | } | |
235 | xdr_setpos(&xdr, pos); | |
236 | ||
237 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
238 | if (pdu->xid != xid) { | |
239 | continue; | |
240 | } | |
a669857d RS |
241 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
242 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
243 | } | |
84004dbf | 244 | if (rpc_process_reply(rpc, pdu, &xdr) != 0) { |
1896d37b | 245 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
84004dbf RS |
246 | } |
247 | xdr_destroy(&xdr); | |
a669857d RS |
248 | if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { |
249 | rpc_free_pdu(rpc, pdu); | |
250 | } | |
84004dbf RS |
251 | return 0; |
252 | } | |
1896d37b | 253 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
84004dbf RS |
254 | xdr_destroy(&xdr); |
255 | return -1; | |
256 | } | |
257 |