Grow the marshalling buffer from 64k to 1M so we can marshall really large WRITE3s
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
2606f9bb 4
84004dbf
RS
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU Lesser General Public License for more details.
10
11 You should have received a copy of the GNU Lesser General Public License
12 along with this program; if not, see <http://www.gnu.org/licenses/>.
13*/
00748f36
RS
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
84004dbf 17
108c622a
RS
18#ifdef AROS
19#include "aros_compat.h"
20#endif
21
a8a1b858
M
22#ifdef WIN32
23#include "win32_compat.h"
bff8fe46
RS
24#endif
25
a8a1b858 26#define _GNU_SOURCE
eecdc4f3 27
00748f36
RS
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
bff8fe46
RS
32#ifdef HAVE_STRINGS_H
33#include <strings.h>
34#endif
35
84004dbf
RS
36#include <stdio.h>
37#include <stdarg.h>
84004dbf 38#include <string.h>
98f5fee8 39#include <stdlib.h>
f3a75078 40#include <assert.h>
a2756193 41#include <time.h>
84004dbf 42#include "slist.h"
763cd6e3 43#include "libnfs-zdr.h"
84004dbf
RS
44#include "libnfs.h"
45#include "libnfs-raw.h"
46#include "libnfs-private.h"
47
48struct rpc_context *rpc_init_context(void)
49{
50 struct rpc_context *rpc;
a2756193 51 static uint32_t salt = 0;
84004dbf
RS
52
53 rpc = malloc(sizeof(struct rpc_context));
54 if (rpc == NULL) {
84004dbf
RS
55 return NULL;
56 }
ea98629a 57 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 58
f3a75078 59 rpc->magic = RPC_CONTEXT_MAGIC;
35280fd7
RS
60
61 /* Allow 1M of data (for writes) and some */
62 rpc->encodebuflen = 1024 * 1024 + 4096;
84004dbf
RS
63 rpc->encodebuf = malloc(rpc->encodebuflen);
64 if (rpc->encodebuf == NULL) {
84004dbf
RS
65 free(rpc);
66 return NULL;
67 }
68
eecdc4f3 69 rpc->auth = authunix_create_default();
84004dbf 70 if (rpc->auth == NULL) {
84004dbf
RS
71 free(rpc->encodebuf);
72 free(rpc);
73 return NULL;
74 }
6fda9871 75 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 76 salt += 0x01000000;
84004dbf
RS
77 rpc->fd = -1;
78
79 return rpc;
80}
81
82
9e00b8c6
RS
83struct rpc_context *rpc_init_udp_context(void)
84{
85 struct rpc_context *rpc;
86
87 rpc = rpc_init_context();
88 if (rpc != NULL) {
89 rpc->is_udp = 1;
90 }
91
92 return rpc;
93}
94
67ba2239 95void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 96{
f3a75078
RS
97 assert(rpc->magic == RPC_CONTEXT_MAGIC);
98
84004dbf
RS
99 if (rpc->auth != NULL) {
100 auth_destroy(rpc->auth);
101 }
8704724f 102 rpc->auth = auth;
84004dbf
RS
103}
104
105
106void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
107{
108 va_list ap;
84004dbf 109
f3a75078
RS
110 assert(rpc->magic == RPC_CONTEXT_MAGIC);
111
84004dbf
RS
112 if (rpc->error_string != NULL) {
113 free(rpc->error_string);
114 }
115 va_start(ap, error_string);
b85c7de2
RS
116 rpc->error_string = malloc(1024);
117 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
118 va_end(ap);
119}
120
121char *rpc_get_error(struct rpc_context *rpc)
122{
f3a75078
RS
123 assert(rpc->magic == RPC_CONTEXT_MAGIC);
124
84004dbf
RS
125 return rpc->error_string;
126}
127
128void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
129{
130 struct rpc_pdu *pdu;
131
f3a75078
RS
132 assert(rpc->magic == RPC_CONTEXT_MAGIC);
133
84004dbf
RS
134 while((pdu = rpc->outqueue) != NULL) {
135 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
136 SLIST_REMOVE(&rpc->outqueue, pdu);
137 rpc_free_pdu(rpc, pdu);
138 }
139 while((pdu = rpc->waitpdu) != NULL) {
140 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
141 SLIST_REMOVE(&rpc->waitpdu, pdu);
142 rpc_free_pdu(rpc, pdu);
143 }
144}
145
d678b73e
RS
146static void rpc_free_fragment(struct rpc_fragment *fragment)
147{
148 if (fragment->data != NULL) {
149 free(fragment->data);
150 }
151 free(fragment);
152}
153
154void rpc_free_all_fragments(struct rpc_context *rpc)
155{
f3a75078
RS
156 assert(rpc->magic == RPC_CONTEXT_MAGIC);
157
d678b73e
RS
158 while (rpc->fragments != NULL) {
159 struct rpc_fragment *fragment = rpc->fragments;
160
161 SLIST_REMOVE(&rpc->fragments, fragment);
162 rpc_free_fragment(fragment);
163 }
164}
165
183451cf 166int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
167{
168 struct rpc_fragment *fragment;
169
f3a75078
RS
170 assert(rpc->magic == RPC_CONTEXT_MAGIC);
171
d678b73e
RS
172 fragment = malloc(sizeof(struct rpc_fragment));
173 if (fragment == NULL) {
174 return -1;
175 }
176
177 fragment->size = size;
178 fragment->data = malloc(fragment->size);
179 if(fragment->data == NULL) {
180 free(fragment);
181 return -1;
182 }
183
184 memcpy(fragment->data, data, fragment->size);
185 SLIST_ADD_END(&rpc->fragments, fragment);
186 return 0;
187}
84004dbf
RS
188
189void rpc_destroy_context(struct rpc_context *rpc)
190{
191 struct rpc_pdu *pdu;
192
f3a75078
RS
193 assert(rpc->magic == RPC_CONTEXT_MAGIC);
194
84004dbf
RS
195 while((pdu = rpc->outqueue) != NULL) {
196 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
197 SLIST_REMOVE(&rpc->outqueue, pdu);
198 rpc_free_pdu(rpc, pdu);
199 }
200 while((pdu = rpc->waitpdu) != NULL) {
201 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
202 SLIST_REMOVE(&rpc->waitpdu, pdu);
203 rpc_free_pdu(rpc, pdu);
204 }
205
d678b73e
RS
206 rpc_free_all_fragments(rpc);
207
84004dbf
RS
208 auth_destroy(rpc->auth);
209 rpc->auth =NULL;
210
211 if (rpc->fd != -1) {
eecdc4f3 212 close(rpc->fd);
84004dbf
RS
213 }
214
215 if (rpc->encodebuf != NULL) {
216 free(rpc->encodebuf);
217 rpc->encodebuf = NULL;
218 }
219
220 if (rpc->error_string != NULL) {
221 free(rpc->error_string);
222 rpc->error_string = NULL;
223 }
224
7ff2f3a0
RS
225 if (rpc->udp_dest != NULL) {
226 free(rpc->udp_dest);
227 rpc->udp_dest = NULL;
228 }
229
f3a75078 230 rpc->magic = 0;
84004dbf
RS
231 free(rpc);
232}
233
234