WIN32: More ifdef cleanups
[deb_libnfs.git] / lib / init.c
... / ...
CommitLineData
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4
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
15#ifdef WIN32
16#include "win32_compat.h"
17#else
18#include <unistd.h>
19#include <strings.h>
20#endif/*WIN32*/
21#define _GNU_SOURCE
22
23#include <stdio.h>
24#include <stdarg.h>
25#include <string.h>
26#include <stdlib.h>
27#include <assert.h>
28#include <time.h>
29#include "slist.h"
30#include "libnfs-zdr.h"
31#include "libnfs.h"
32#include "libnfs-raw.h"
33#include "libnfs-private.h"
34
35#ifdef AROS
36#include "aros_compat.h"
37#endif
38
39struct rpc_context *rpc_init_context(void)
40{
41 struct rpc_context *rpc;
42 static uint32_t salt = 0;
43
44 rpc = malloc(sizeof(struct rpc_context));
45 if (rpc == NULL) {
46 return NULL;
47 }
48 memset(rpc, 0, sizeof(struct rpc_context));
49
50 rpc->magic = RPC_CONTEXT_MAGIC;
51 rpc->encodebuflen = 65536;
52 rpc->encodebuf = malloc(rpc->encodebuflen);
53 if (rpc->encodebuf == NULL) {
54 free(rpc);
55 return NULL;
56 }
57
58#if defined(WIN32)
59 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
60#else
61 rpc->auth = authunix_create_default();
62#endif
63 if (rpc->auth == NULL) {
64 free(rpc->encodebuf);
65 free(rpc);
66 return NULL;
67 }
68 rpc->xid = salt + time(NULL);
69 salt += 0x01000000;
70 rpc->fd = -1;
71
72 return rpc;
73}
74
75
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
88void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
89{
90 assert(rpc->magic == RPC_CONTEXT_MAGIC);
91
92 if (rpc->auth != NULL) {
93 auth_destroy(rpc->auth);
94 }
95 rpc->auth = auth;
96}
97
98
99void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
100{
101 va_list ap;
102
103 assert(rpc->magic == RPC_CONTEXT_MAGIC);
104
105 if (rpc->error_string != NULL) {
106 free(rpc->error_string);
107 }
108 va_start(ap, error_string);
109 rpc->error_string = malloc(1024);
110 vsnprintf(rpc->error_string, 1024, error_string, ap);
111 va_end(ap);
112}
113
114char *rpc_get_error(struct rpc_context *rpc)
115{
116 assert(rpc->magic == RPC_CONTEXT_MAGIC);
117
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
125 assert(rpc->magic == RPC_CONTEXT_MAGIC);
126
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
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{
149 assert(rpc->magic == RPC_CONTEXT_MAGIC);
150
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
159int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
160{
161 struct rpc_fragment *fragment;
162
163 assert(rpc->magic == RPC_CONTEXT_MAGIC);
164
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}
181
182void rpc_destroy_context(struct rpc_context *rpc)
183{
184 struct rpc_pdu *pdu;
185
186 assert(rpc->magic == RPC_CONTEXT_MAGIC);
187
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
199 rpc_free_all_fragments(rpc);
200
201 auth_destroy(rpc->auth);
202 rpc->auth =NULL;
203
204 if (rpc->fd != -1) {
205 close(rpc->fd);
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
218 if (rpc->udp_dest != NULL) {
219 free(rpc->udp_dest);
220 rpc->udp_dest = NULL;
221 }
222
223 rpc->magic = 0;
224 free(rpc);
225}
226
227