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"
43 #include "libnfs-zdr.h"
45 #include "libnfs-raw.h"
46 #include "libnfs-private.h"
48 struct rpc_context
*rpc_init_context(void)
50 struct rpc_context
*rpc
;
51 static uint32_t salt
= 0;
53 rpc
= malloc(sizeof(struct rpc_context
));
57 memset(rpc
, 0, sizeof(struct rpc_context
));
59 rpc
->magic
= RPC_CONTEXT_MAGIC
;
60 rpc
->encodebuflen
= 65536;
61 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
62 if (rpc
->encodebuf
== NULL
) {
67 rpc
->auth
= authunix_create_default();
68 if (rpc
->auth
== NULL
) {
73 rpc
->xid
= salt
+ time(NULL
);
81 struct rpc_context
*rpc_init_udp_context(void)
83 struct rpc_context
*rpc
;
85 rpc
= rpc_init_context();
93 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
95 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
97 if (rpc
->auth
!= NULL
) {
98 auth_destroy(rpc
->auth
);
104 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
108 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
110 if (rpc
->error_string
!= NULL
) {
111 free(rpc
->error_string
);
113 va_start(ap
, error_string
);
114 rpc
->error_string
= malloc(1024);
115 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
119 char *rpc_get_error(struct rpc_context
*rpc
)
121 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
123 return rpc
->error_string
;
126 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
130 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
132 while((pdu
= rpc
->outqueue
) != NULL
) {
133 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
134 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
135 rpc_free_pdu(rpc
, pdu
);
137 while((pdu
= rpc
->waitpdu
) != NULL
) {
138 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
139 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
140 rpc_free_pdu(rpc
, pdu
);
144 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
146 if (fragment
->data
!= NULL
) {
147 free(fragment
->data
);
152 void rpc_free_all_fragments(struct rpc_context
*rpc
)
154 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
156 while (rpc
->fragments
!= NULL
) {
157 struct rpc_fragment
*fragment
= rpc
->fragments
;
159 SLIST_REMOVE(&rpc
->fragments
, fragment
);
160 rpc_free_fragment(fragment
);
164 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
166 struct rpc_fragment
*fragment
;
168 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
170 fragment
= malloc(sizeof(struct rpc_fragment
));
171 if (fragment
== NULL
) {
175 fragment
->size
= size
;
176 fragment
->data
= malloc(fragment
->size
);
177 if(fragment
->data
== NULL
) {
182 memcpy(fragment
->data
, data
, fragment
->size
);
183 SLIST_ADD_END(&rpc
->fragments
, fragment
);
187 void rpc_destroy_context(struct rpc_context
*rpc
)
191 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
193 while((pdu
= rpc
->outqueue
) != NULL
) {
194 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
195 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
196 rpc_free_pdu(rpc
, pdu
);
198 while((pdu
= rpc
->waitpdu
) != NULL
) {
199 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
200 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
201 rpc_free_pdu(rpc
, pdu
);
204 rpc_free_all_fragments(rpc
);
206 auth_destroy(rpc
->auth
);
213 if (rpc
->encodebuf
!= NULL
) {
214 free(rpc
->encodebuf
);
215 rpc
->encodebuf
= NULL
;
218 if (rpc
->error_string
!= NULL
) {
219 free(rpc
->error_string
);
220 rpc
->error_string
= NULL
;
223 if (rpc
->udp_dest
!= NULL
) {
225 rpc
->udp_dest
= NULL
;