06f6e4bbb156d8e021d775ecdf6a234bd23663ce
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
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.
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/>.
16 #include "win32_compat.h"
30 #include "libnfs-zdr.h"
32 #include "libnfs-raw.h"
33 #include "libnfs-private.h"
35 struct rpc_context
*rpc_init_context(void)
37 struct rpc_context
*rpc
;
38 static uint32_t salt
= 0;
40 rpc
= malloc(sizeof(struct rpc_context
));
44 memset(rpc
, 0, sizeof(struct rpc_context
));
46 rpc
->magic
= RPC_CONTEXT_MAGIC
;
47 rpc
->encodebuflen
= 65536;
48 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
49 if (rpc
->encodebuf
== NULL
) {
55 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
57 rpc
->auth
= authunix_create_default();
59 if (rpc
->auth
== NULL
) {
64 rpc
->xid
= salt
+ time(NULL
);
72 struct rpc_context
*rpc_init_udp_context(void)
74 struct rpc_context
*rpc
;
76 rpc
= rpc_init_context();
84 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
86 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
88 if (rpc
->auth
!= NULL
) {
89 auth_destroy(rpc
->auth
);
95 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
99 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
101 if (rpc
->error_string
!= NULL
) {
102 free(rpc
->error_string
);
104 va_start(ap
, error_string
);
105 rpc
->error_string
= malloc(1024);
106 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
110 char *rpc_get_error(struct rpc_context
*rpc
)
112 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
114 return rpc
->error_string
;
117 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
121 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
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
);
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
);
135 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
137 if (fragment
->data
!= NULL
) {
138 free(fragment
->data
);
143 void rpc_free_all_fragments(struct rpc_context
*rpc
)
145 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
147 while (rpc
->fragments
!= NULL
) {
148 struct rpc_fragment
*fragment
= rpc
->fragments
;
150 SLIST_REMOVE(&rpc
->fragments
, fragment
);
151 rpc_free_fragment(fragment
);
155 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
157 struct rpc_fragment
*fragment
;
159 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
161 fragment
= malloc(sizeof(struct rpc_fragment
));
162 if (fragment
== NULL
) {
166 fragment
->size
= size
;
167 fragment
->data
= malloc(fragment
->size
);
168 if(fragment
->data
== NULL
) {
173 memcpy(fragment
->data
, data
, fragment
->size
);
174 SLIST_ADD_END(&rpc
->fragments
, fragment
);
178 void rpc_destroy_context(struct rpc_context
*rpc
)
182 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
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
);
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
);
195 rpc_free_all_fragments(rpc
);
197 auth_destroy(rpc
->auth
);
202 closesocket(rpc
->fd
);
208 if (rpc
->encodebuf
!= NULL
) {
209 free(rpc
->encodebuf
);
210 rpc
->encodebuf
= NULL
;
213 if (rpc
->error_string
!= NULL
) {
214 free(rpc
->error_string
);
215 rpc
->error_string
= NULL
;
218 if (rpc
->udp_dest
!= NULL
) {
220 rpc
->udp_dest
= NULL
;