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