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 | ||
18 | #include <stdio.h> | |
19 | #include <strings.h> | |
98f5fee8 M |
20 | #include <stdlib.h> |
21 | #include <rpc/rpc.h> | |
84004dbf RS |
22 | #include <rpc/xdr.h> |
23 | #include <rpc/rpc_msg.h> | |
24 | #include "slist.h" | |
25 | #include "libnfs.h" | |
26 | #include "libnfs-raw.h" | |
27 | #include "libnfs-private.h" | |
28 | ||
29 | 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) | |
30 | { | |
31 | struct rpc_pdu *pdu; | |
32 | struct rpc_msg msg; | |
33 | ||
34 | if (rpc == NULL) { | |
84004dbf RS |
35 | return NULL; |
36 | } | |
37 | ||
38 | pdu = malloc(sizeof(struct rpc_pdu)); | |
39 | if (pdu == NULL) { | |
1896d37b | 40 | rpc_set_error(rpc, "Out of memory: Failed to allocate pdu structure"); |
84004dbf RS |
41 | return NULL; |
42 | } | |
43 | bzero(pdu, sizeof(struct rpc_pdu)); | |
44 | pdu->xid = rpc->xid++; | |
45 | pdu->cb = cb; | |
46 | pdu->private_data = private_data; | |
47 | pdu->xdr_decode_fn = xdr_decode_fn; | |
48 | pdu->xdr_decode_bufsize = xdr_decode_bufsize; | |
49 | ||
50 | xdrmem_create(&pdu->xdr, rpc->encodebuf, rpc->encodebuflen, XDR_ENCODE); | |
a1992412 RS |
51 | if (rpc->is_udp == 0) { |
52 | xdr_setpos(&pdu->xdr, 4); /* skip past the record marker */ | |
53 | } | |
84004dbf RS |
54 | |
55 | bzero(&msg, sizeof(struct rpc_msg)); | |
56 | msg.rm_xid = pdu->xid; | |
57 | msg.rm_direction = CALL; | |
58 | msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; | |
59 | msg.rm_call.cb_prog = program; | |
60 | msg.rm_call.cb_vers = version; | |
61 | msg.rm_call.cb_proc = procedure; | |
62 | msg.rm_call.cb_cred = rpc->auth->ah_cred; | |
63 | msg.rm_call.cb_verf = rpc->auth->ah_verf; | |
64 | ||
65 | if (xdr_callmsg(&pdu->xdr, &msg) == 0) { | |
1896d37b | 66 | rpc_set_error(rpc, "xdr_callmsg failed"); |
84004dbf RS |
67 | xdr_destroy(&pdu->xdr); |
68 | free(pdu); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | return pdu; | |
73 | } | |
74 | ||
75 | void rpc_free_pdu(struct rpc_context *rpc _U_, struct rpc_pdu *pdu) | |
76 | { | |
77 | if (pdu->outdata.data != NULL) { | |
78 | free(pdu->outdata.data); | |
79 | pdu->outdata.data = NULL; | |
80 | } | |
81 | ||
82 | if (pdu->xdr_decode_buf != NULL) { | |
83 | xdr_free(pdu->xdr_decode_fn, pdu->xdr_decode_buf); | |
84 | free(pdu->xdr_decode_buf); | |
85 | pdu->xdr_decode_buf = NULL; | |
86 | } | |
87 | ||
88 | xdr_destroy(&pdu->xdr); | |
df5af25f | 89 | |
84004dbf RS |
90 | free(pdu); |
91 | } | |
92 | ||
93 | ||
94 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu) | |
95 | { | |
96 | int size, recordmarker; | |
97 | ||
98 | size = xdr_getpos(&pdu->xdr); | |
99 | ||
100 | /* write recordmarker */ | |
101 | xdr_setpos(&pdu->xdr, 0); | |
102 | recordmarker = (size - 4) | 0x80000000; | |
103 | xdr_int(&pdu->xdr, &recordmarker); | |
104 | ||
105 | pdu->outdata.size = size; | |
106 | pdu->outdata.data = malloc(pdu->outdata.size); | |
107 | if (pdu->outdata.data == NULL) { | |
108 | rpc_set_error(rpc, "Out of memory. Failed to allocate buffer for pdu\n"); | |
109 | rpc_free_pdu(rpc, pdu); | |
110 | return -1; | |
111 | } | |
112 | ||
113 | memcpy(pdu->outdata.data, rpc->encodebuf, pdu->outdata.size); | |
114 | SLIST_ADD_END(&rpc->outqueue, pdu); | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
119 | int rpc_get_pdu_size(char *buf) | |
120 | { | |
121 | uint32_t size; | |
122 | ||
123 | size = ntohl(*(uint32_t *)buf); | |
124 | ||
125 | if ((size & 0x80000000) == 0) { | |
1896d37b | 126 | /* cant handle oncrpc fragments */ |
84004dbf RS |
127 | return -1; |
128 | } | |
129 | ||
130 | return (size & 0x7fffffff) + 4; | |
131 | } | |
132 | ||
133 | static int rpc_process_reply(struct rpc_context *rpc, struct rpc_pdu *pdu, XDR *xdr) | |
134 | { | |
135 | struct rpc_msg msg; | |
136 | ||
137 | bzero(&msg, sizeof(struct rpc_msg)); | |
138 | msg.acpted_rply.ar_verf = _null_auth; | |
139 | if (pdu->xdr_decode_bufsize > 0) { | |
140 | pdu->xdr_decode_buf = malloc(pdu->xdr_decode_bufsize); | |
141 | if (pdu->xdr_decode_buf == NULL) { | |
1896d37b | 142 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
143 | pdu->cb(rpc, RPC_STATUS_ERROR, "Failed to allocate buffer for decoding of XDR reply", pdu->private_data); |
144 | return 0; | |
145 | } | |
146 | bzero(pdu->xdr_decode_buf, pdu->xdr_decode_bufsize); | |
147 | } | |
148 | msg.acpted_rply.ar_results.where = pdu->xdr_decode_buf; | |
149 | msg.acpted_rply.ar_results.proc = pdu->xdr_decode_fn; | |
150 | ||
151 | if (xdr_replymsg(xdr, &msg) == 0) { | |
1896d37b | 152 | rpc_set_error(rpc, "xdr_replymsg failed in portmap_getport_reply"); |
84004dbf RS |
153 | pdu->cb(rpc, RPC_STATUS_ERROR, "Message rejected by server", pdu->private_data); |
154 | if (pdu->xdr_decode_buf != NULL) { | |
155 | free(pdu->xdr_decode_buf); | |
156 | pdu->xdr_decode_buf = NULL; | |
157 | } | |
158 | return 0; | |
159 | } | |
160 | if (msg.rm_reply.rp_stat != MSG_ACCEPTED) { | |
161 | pdu->cb(rpc, RPC_STATUS_ERROR, "RPC Packet not accepted by the server", pdu->private_data); | |
162 | return 0; | |
163 | } | |
164 | switch (msg.rm_reply.rp_acpt.ar_stat) { | |
165 | case SUCCESS: | |
166 | pdu->cb(rpc, RPC_STATUS_SUCCESS, pdu->xdr_decode_buf, pdu->private_data); | |
167 | break; | |
168 | case PROG_UNAVAIL: | |
169 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program not available", pdu->private_data); | |
170 | break; | |
171 | case PROG_MISMATCH: | |
172 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Program version mismatch", pdu->private_data); | |
173 | break; | |
174 | case PROC_UNAVAIL: | |
175 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Procedure not available", pdu->private_data); | |
176 | break; | |
177 | case GARBAGE_ARGS: | |
178 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: Garbage arguments", pdu->private_data); | |
179 | break; | |
180 | case SYSTEM_ERR: | |
181 | pdu->cb(rpc, RPC_STATUS_ERROR, "Server responded: System Error", pdu->private_data); | |
182 | break; | |
183 | default: | |
184 | pdu->cb(rpc, RPC_STATUS_ERROR, "Unknown rpc response from server", pdu->private_data); | |
185 | break; | |
186 | } | |
187 | ||
188 | return 0; | |
189 | } | |
190 | ||
191 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) | |
192 | { | |
193 | struct rpc_pdu *pdu; | |
194 | XDR xdr; | |
195 | int pos, recordmarker; | |
196 | unsigned int xid; | |
197 | ||
198 | bzero(&xdr, sizeof(XDR)); | |
199 | ||
200 | xdrmem_create(&xdr, buf, size, XDR_DECODE); | |
201 | if (xdr_int(&xdr, &recordmarker) == 0) { | |
1896d37b | 202 | rpc_set_error(rpc, "xdr_int reading recordmarker failed"); |
84004dbf RS |
203 | xdr_destroy(&xdr); |
204 | return -1; | |
205 | } | |
206 | pos = xdr_getpos(&xdr); | |
207 | if (xdr_int(&xdr, (int *)&xid) == 0) { | |
1896d37b | 208 | rpc_set_error(rpc, "xdr_int reading xid failed"); |
84004dbf RS |
209 | xdr_destroy(&xdr); |
210 | return -1; | |
211 | } | |
212 | xdr_setpos(&xdr, pos); | |
213 | ||
214 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { | |
215 | if (pdu->xid != xid) { | |
216 | continue; | |
217 | } | |
218 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
219 | if (rpc_process_reply(rpc, pdu, &xdr) != 0) { | |
1896d37b | 220 | rpc_set_error(rpc, "rpc_procdess_reply failed"); |
84004dbf RS |
221 | } |
222 | xdr_destroy(&xdr); | |
223 | rpc_free_pdu(rpc, pdu); | |
224 | return 0; | |
225 | } | |
1896d37b | 226 | rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); |
84004dbf RS |
227 | xdr_destroy(&xdr); |
228 | return -1; | |
229 | } | |
230 |