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