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