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