Remove some debug residuals from fuse_nfs
[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;
84004dbf
RS
60 rpc->encodebuflen = 65536;
61 rpc->encodebuf = malloc(rpc->encodebuflen);
62 if (rpc->encodebuf == NULL) {
84004dbf
RS
63 free(rpc);
64 return NULL;
65 }
66
eecdc4f3 67 rpc->auth = authunix_create_default();
84004dbf 68 if (rpc->auth == NULL) {
84004dbf
RS
69 free(rpc->encodebuf);
70 free(rpc);
71 return NULL;
72 }
6fda9871 73 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 74 salt += 0x01000000;
84004dbf
RS
75 rpc->fd = -1;
76
77 return rpc;
78}
79
80
9e00b8c6
RS
81struct rpc_context *rpc_init_udp_context(void)
82{
83 struct rpc_context *rpc;
84
85 rpc = rpc_init_context();
86 if (rpc != NULL) {
87 rpc->is_udp = 1;
88 }
89
90 return rpc;
91}
92
67ba2239 93void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 94{
f3a75078
RS
95 assert(rpc->magic == RPC_CONTEXT_MAGIC);
96
84004dbf
RS
97 if (rpc->auth != NULL) {
98 auth_destroy(rpc->auth);
99 }
8704724f 100 rpc->auth = auth;
84004dbf
RS
101}
102
103
104void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
105{
106 va_list ap;
84004dbf 107
f3a75078
RS
108 assert(rpc->magic == RPC_CONTEXT_MAGIC);
109
84004dbf
RS
110 if (rpc->error_string != NULL) {
111 free(rpc->error_string);
112 }
113 va_start(ap, error_string);
b85c7de2
RS
114 rpc->error_string = malloc(1024);
115 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
116 va_end(ap);
117}
118
119char *rpc_get_error(struct rpc_context *rpc)
120{
f3a75078
RS
121 assert(rpc->magic == RPC_CONTEXT_MAGIC);
122
84004dbf
RS
123 return rpc->error_string;
124}
125
126void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
127{
128 struct rpc_pdu *pdu;
129
f3a75078
RS
130 assert(rpc->magic == RPC_CONTEXT_MAGIC);
131
84004dbf
RS
132 while((pdu = rpc->outqueue) != NULL) {
133 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
134 SLIST_REMOVE(&rpc->outqueue, pdu);
135 rpc_free_pdu(rpc, pdu);
136 }
137 while((pdu = rpc->waitpdu) != NULL) {
138 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
139 SLIST_REMOVE(&rpc->waitpdu, pdu);
140 rpc_free_pdu(rpc, pdu);
141 }
142}
143
d678b73e
RS
144static void rpc_free_fragment(struct rpc_fragment *fragment)
145{
146 if (fragment->data != NULL) {
147 free(fragment->data);
148 }
149 free(fragment);
150}
151
152void rpc_free_all_fragments(struct rpc_context *rpc)
153{
f3a75078
RS
154 assert(rpc->magic == RPC_CONTEXT_MAGIC);
155
d678b73e
RS
156 while (rpc->fragments != NULL) {
157 struct rpc_fragment *fragment = rpc->fragments;
158
159 SLIST_REMOVE(&rpc->fragments, fragment);
160 rpc_free_fragment(fragment);
161 }
162}
163
183451cf 164int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
165{
166 struct rpc_fragment *fragment;
167
f3a75078
RS
168 assert(rpc->magic == RPC_CONTEXT_MAGIC);
169
d678b73e
RS
170 fragment = malloc(sizeof(struct rpc_fragment));
171 if (fragment == NULL) {
172 return -1;
173 }
174
175 fragment->size = size;
176 fragment->data = malloc(fragment->size);
177 if(fragment->data == NULL) {
178 free(fragment);
179 return -1;
180 }
181
182 memcpy(fragment->data, data, fragment->size);
183 SLIST_ADD_END(&rpc->fragments, fragment);
184 return 0;
185}
84004dbf
RS
186
187void rpc_destroy_context(struct rpc_context *rpc)
188{
189 struct rpc_pdu *pdu;
190
f3a75078
RS
191 assert(rpc->magic == RPC_CONTEXT_MAGIC);
192
84004dbf
RS
193 while((pdu = rpc->outqueue) != NULL) {
194 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
195 SLIST_REMOVE(&rpc->outqueue, pdu);
196 rpc_free_pdu(rpc, pdu);
197 }
198 while((pdu = rpc->waitpdu) != NULL) {
199 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
200 SLIST_REMOVE(&rpc->waitpdu, pdu);
201 rpc_free_pdu(rpc, pdu);
202 }
203
d678b73e
RS
204 rpc_free_all_fragments(rpc);
205
84004dbf
RS
206 auth_destroy(rpc->auth);
207 rpc->auth =NULL;
208
209 if (rpc->fd != -1) {
eecdc4f3 210 close(rpc->fd);
84004dbf
RS
211 }
212
213 if (rpc->encodebuf != NULL) {
214 free(rpc->encodebuf);
215 rpc->encodebuf = NULL;
216 }
217
218 if (rpc->error_string != NULL) {
219 free(rpc->error_string);
220 rpc->error_string = NULL;
221 }
222
7ff2f3a0
RS
223 if (rpc->udp_dest != NULL) {
224 free(rpc->udp_dest);
225 rpc->udp_dest = NULL;
226 }
227
f3a75078 228 rpc->magic = 0;
84004dbf
RS
229 free(rpc);
230}
231
232