e1d4980913f62a0dcc2129f1b792266a98fb27fb
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"
36 #include "aros_compat.h"
39 struct rpc_context
*rpc_init_context(void)
41 struct rpc_context
*rpc
;
42 static uint32_t salt
= 0;
44 rpc
= malloc(sizeof(struct rpc_context
));
48 memset(rpc
, 0, sizeof(struct rpc_context
));
50 rpc
->magic
= RPC_CONTEXT_MAGIC
;
51 rpc
->encodebuflen
= 65536;
52 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
53 if (rpc
->encodebuf
== NULL
) {
59 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
61 rpc
->auth
= authunix_create_default();
63 if (rpc
->auth
== NULL
) {
68 rpc
->xid
= salt
+ time(NULL
);
76 struct rpc_context
*rpc_init_udp_context(void)
78 struct rpc_context
*rpc
;
80 rpc
= rpc_init_context();
88 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
90 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
92 if (rpc
->auth
!= NULL
) {
93 auth_destroy(rpc
->auth
);
99 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
103 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
105 if (rpc
->error_string
!= NULL
) {
106 free(rpc
->error_string
);
108 va_start(ap
, error_string
);
109 rpc
->error_string
= malloc(1024);
110 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
114 char *rpc_get_error(struct rpc_context
*rpc
)
116 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
118 return rpc
->error_string
;
121 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
125 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
127 while((pdu
= rpc
->outqueue
) != NULL
) {
128 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
129 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
130 rpc_free_pdu(rpc
, pdu
);
132 while((pdu
= rpc
->waitpdu
) != NULL
) {
133 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
134 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
135 rpc_free_pdu(rpc
, pdu
);
139 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
141 if (fragment
->data
!= NULL
) {
142 free(fragment
->data
);
147 void rpc_free_all_fragments(struct rpc_context
*rpc
)
149 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
151 while (rpc
->fragments
!= NULL
) {
152 struct rpc_fragment
*fragment
= rpc
->fragments
;
154 SLIST_REMOVE(&rpc
->fragments
, fragment
);
155 rpc_free_fragment(fragment
);
159 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
161 struct rpc_fragment
*fragment
;
163 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
165 fragment
= malloc(sizeof(struct rpc_fragment
));
166 if (fragment
== NULL
) {
170 fragment
->size
= size
;
171 fragment
->data
= malloc(fragment
->size
);
172 if(fragment
->data
== NULL
) {
177 memcpy(fragment
->data
, data
, fragment
->size
);
178 SLIST_ADD_END(&rpc
->fragments
, fragment
);
182 void rpc_destroy_context(struct rpc_context
*rpc
)
186 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
188 while((pdu
= rpc
->outqueue
) != NULL
) {
189 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
190 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
191 rpc_free_pdu(rpc
, pdu
);
193 while((pdu
= rpc
->waitpdu
) != NULL
) {
194 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
195 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
196 rpc_free_pdu(rpc
, pdu
);
199 rpc_free_all_fragments(rpc
);
201 auth_destroy(rpc
->auth
);
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
;