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/>.
19 #include "aros_compat.h"
23 #include "win32_compat.h"
40 #include "libnfs-zdr.h"
42 #include "libnfs-raw.h"
43 #include "libnfs-private.h"
45 struct rpc_context
*rpc_init_context(void)
47 struct rpc_context
*rpc
;
48 static uint32_t salt
= 0;
50 rpc
= malloc(sizeof(struct rpc_context
));
54 memset(rpc
, 0, sizeof(struct rpc_context
));
56 rpc
->magic
= RPC_CONTEXT_MAGIC
;
57 rpc
->encodebuflen
= 65536;
58 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
59 if (rpc
->encodebuf
== NULL
) {
64 rpc
->auth
= authunix_create_default();
65 if (rpc
->auth
== NULL
) {
70 rpc
->xid
= salt
+ time(NULL
);
78 struct rpc_context
*rpc_init_udp_context(void)
80 struct rpc_context
*rpc
;
82 rpc
= rpc_init_context();
90 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
92 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
94 if (rpc
->auth
!= NULL
) {
95 auth_destroy(rpc
->auth
);
101 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
105 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
107 if (rpc
->error_string
!= NULL
) {
108 free(rpc
->error_string
);
110 va_start(ap
, error_string
);
111 rpc
->error_string
= malloc(1024);
112 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
116 char *rpc_get_error(struct rpc_context
*rpc
)
118 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
120 return rpc
->error_string
;
123 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
127 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
129 while((pdu
= rpc
->outqueue
) != NULL
) {
130 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
131 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
132 rpc_free_pdu(rpc
, pdu
);
134 while((pdu
= rpc
->waitpdu
) != NULL
) {
135 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
136 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
137 rpc_free_pdu(rpc
, pdu
);
141 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
143 if (fragment
->data
!= NULL
) {
144 free(fragment
->data
);
149 void rpc_free_all_fragments(struct rpc_context
*rpc
)
151 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
153 while (rpc
->fragments
!= NULL
) {
154 struct rpc_fragment
*fragment
= rpc
->fragments
;
156 SLIST_REMOVE(&rpc
->fragments
, fragment
);
157 rpc_free_fragment(fragment
);
161 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
163 struct rpc_fragment
*fragment
;
165 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
167 fragment
= malloc(sizeof(struct rpc_fragment
));
168 if (fragment
== NULL
) {
172 fragment
->size
= size
;
173 fragment
->data
= malloc(fragment
->size
);
174 if(fragment
->data
== NULL
) {
179 memcpy(fragment
->data
, data
, fragment
->size
);
180 SLIST_ADD_END(&rpc
->fragments
, fragment
);
184 void rpc_destroy_context(struct rpc_context
*rpc
)
188 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
190 while((pdu
= rpc
->outqueue
) != NULL
) {
191 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
192 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
193 rpc_free_pdu(rpc
, pdu
);
195 while((pdu
= rpc
->waitpdu
) != NULL
) {
196 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
197 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
198 rpc_free_pdu(rpc
, pdu
);
201 rpc_free_all_fragments(rpc
);
203 auth_destroy(rpc
->auth
);
210 if (rpc
->encodebuf
!= NULL
) {
211 free(rpc
->encodebuf
);
212 rpc
->encodebuf
= NULL
;
215 if (rpc
->error_string
!= NULL
) {
216 free(rpc
->error_string
);
217 rpc
->error_string
= NULL
;
220 if (rpc
->udp_dest
!= NULL
) {
222 rpc
->udp_dest
= NULL
;