WIN32: More ifdef cleanups
[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*/
14
a8a1b858
M
15#ifdef WIN32
16#include "win32_compat.h"
eecdc4f3
RS
17#else
18#include <unistd.h>
19#include <strings.h>
a8a1b858
M
20#endif/*WIN32*/
21#define _GNU_SOURCE
eecdc4f3 22
84004dbf
RS
23#include <stdio.h>
24#include <stdarg.h>
84004dbf 25#include <string.h>
98f5fee8 26#include <stdlib.h>
f3a75078 27#include <assert.h>
a2756193 28#include <time.h>
84004dbf 29#include "slist.h"
763cd6e3 30#include "libnfs-zdr.h"
84004dbf
RS
31#include "libnfs.h"
32#include "libnfs-raw.h"
33#include "libnfs-private.h"
34
2cdf4fcb
RS
35#ifdef AROS
36#include "aros_compat.h"
37#endif
38
84004dbf
RS
39struct rpc_context *rpc_init_context(void)
40{
41 struct rpc_context *rpc;
a2756193 42 static uint32_t salt = 0;
84004dbf
RS
43
44 rpc = malloc(sizeof(struct rpc_context));
45 if (rpc == NULL) {
84004dbf
RS
46 return NULL;
47 }
ea98629a 48 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 49
f3a75078 50 rpc->magic = RPC_CONTEXT_MAGIC;
84004dbf
RS
51 rpc->encodebuflen = 65536;
52 rpc->encodebuf = malloc(rpc->encodebuflen);
53 if (rpc->encodebuf == NULL) {
84004dbf
RS
54 free(rpc);
55 return NULL;
56 }
57
eecdc4f3
RS
58#if defined(WIN32)
59 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
60#else
61 rpc->auth = authunix_create_default();
62#endif
84004dbf 63 if (rpc->auth == NULL) {
84004dbf
RS
64 free(rpc->encodebuf);
65 free(rpc);
66 return NULL;
67 }
a2756193
SC
68 rpc->xid = salt + time(NULL);
69 salt += 0x01000000;
84004dbf
RS
70 rpc->fd = -1;
71
72 return rpc;
73}
74
75
9e00b8c6
RS
76struct rpc_context *rpc_init_udp_context(void)
77{
78 struct rpc_context *rpc;
79
80 rpc = rpc_init_context();
81 if (rpc != NULL) {
82 rpc->is_udp = 1;
83 }
84
85 return rpc;
86}
87
67ba2239 88void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 89{
f3a75078
RS
90 assert(rpc->magic == RPC_CONTEXT_MAGIC);
91
84004dbf
RS
92 if (rpc->auth != NULL) {
93 auth_destroy(rpc->auth);
94 }
8704724f 95 rpc->auth = auth;
84004dbf
RS
96}
97
98
99void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
100{
101 va_list ap;
84004dbf 102
f3a75078
RS
103 assert(rpc->magic == RPC_CONTEXT_MAGIC);
104
84004dbf
RS
105 if (rpc->error_string != NULL) {
106 free(rpc->error_string);
107 }
108 va_start(ap, error_string);
b85c7de2
RS
109 rpc->error_string = malloc(1024);
110 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
111 va_end(ap);
112}
113
114char *rpc_get_error(struct rpc_context *rpc)
115{
f3a75078
RS
116 assert(rpc->magic == RPC_CONTEXT_MAGIC);
117
84004dbf
RS
118 return rpc->error_string;
119}
120
121void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
122{
123 struct rpc_pdu *pdu;
124
f3a75078
RS
125 assert(rpc->magic == RPC_CONTEXT_MAGIC);
126
84004dbf
RS
127 while((pdu = rpc->outqueue) != NULL) {
128 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
129 SLIST_REMOVE(&rpc->outqueue, pdu);
130 rpc_free_pdu(rpc, pdu);
131 }
132 while((pdu = rpc->waitpdu) != NULL) {
133 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
134 SLIST_REMOVE(&rpc->waitpdu, pdu);
135 rpc_free_pdu(rpc, pdu);
136 }
137}
138
d678b73e
RS
139static void rpc_free_fragment(struct rpc_fragment *fragment)
140{
141 if (fragment->data != NULL) {
142 free(fragment->data);
143 }
144 free(fragment);
145}
146
147void rpc_free_all_fragments(struct rpc_context *rpc)
148{
f3a75078
RS
149 assert(rpc->magic == RPC_CONTEXT_MAGIC);
150
d678b73e
RS
151 while (rpc->fragments != NULL) {
152 struct rpc_fragment *fragment = rpc->fragments;
153
154 SLIST_REMOVE(&rpc->fragments, fragment);
155 rpc_free_fragment(fragment);
156 }
157}
158
183451cf 159int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
160{
161 struct rpc_fragment *fragment;
162
f3a75078
RS
163 assert(rpc->magic == RPC_CONTEXT_MAGIC);
164
d678b73e
RS
165 fragment = malloc(sizeof(struct rpc_fragment));
166 if (fragment == NULL) {
167 return -1;
168 }
169
170 fragment->size = size;
171 fragment->data = malloc(fragment->size);
172 if(fragment->data == NULL) {
173 free(fragment);
174 return -1;
175 }
176
177 memcpy(fragment->data, data, fragment->size);
178 SLIST_ADD_END(&rpc->fragments, fragment);
179 return 0;
180}
84004dbf
RS
181
182void rpc_destroy_context(struct rpc_context *rpc)
183{
184 struct rpc_pdu *pdu;
185
f3a75078
RS
186 assert(rpc->magic == RPC_CONTEXT_MAGIC);
187
84004dbf
RS
188 while((pdu = rpc->outqueue) != NULL) {
189 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
190 SLIST_REMOVE(&rpc->outqueue, pdu);
191 rpc_free_pdu(rpc, pdu);
192 }
193 while((pdu = rpc->waitpdu) != NULL) {
194 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
195 SLIST_REMOVE(&rpc->waitpdu, pdu);
196 rpc_free_pdu(rpc, pdu);
197 }
198
d678b73e
RS
199 rpc_free_all_fragments(rpc);
200
84004dbf
RS
201 auth_destroy(rpc->auth);
202 rpc->auth =NULL;
203
204 if (rpc->fd != -1) {
eecdc4f3 205 close(rpc->fd);
84004dbf
RS
206 }
207
208 if (rpc->encodebuf != NULL) {
209 free(rpc->encodebuf);
210 rpc->encodebuf = NULL;
211 }
212
213 if (rpc->error_string != NULL) {
214 free(rpc->error_string);
215 rpc->error_string = NULL;
216 }
217
7ff2f3a0
RS
218 if (rpc->udp_dest != NULL) {
219 free(rpc->udp_dest);
220 rpc->udp_dest = NULL;
221 }
222
f3a75078 223 rpc->magic = 0;
84004dbf
RS
224 free(rpc);
225}
226
227