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