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
) + getpid() << 16;
76 rpc
->tcp_syncnt
= RPC_PARAM_UNDEFINED
;
82 struct rpc_context
*rpc_init_udp_context(void)
84 struct rpc_context
*rpc
;
86 rpc
= rpc_init_context();
94 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
96 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
98 if (rpc
->auth
!= NULL
) {
99 auth_destroy(rpc
->auth
);
105 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
111 if (rpc
->error_string
!= NULL
) {
112 free(rpc
->error_string
);
114 va_start(ap
, error_string
);
115 rpc
->error_string
= malloc(1024);
116 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
120 char *rpc_get_error(struct rpc_context
*rpc
)
122 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
124 return rpc
->error_string
;
127 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
131 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
133 while((pdu
= rpc
->outqueue
) != NULL
) {
134 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
135 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
136 rpc_free_pdu(rpc
, pdu
);
138 while((pdu
= rpc
->waitpdu
) != NULL
) {
139 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
140 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
141 rpc_free_pdu(rpc
, pdu
);
145 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
147 if (fragment
->data
!= NULL
) {
148 free(fragment
->data
);
153 void rpc_free_all_fragments(struct rpc_context
*rpc
)
155 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
157 while (rpc
->fragments
!= NULL
) {
158 struct rpc_fragment
*fragment
= rpc
->fragments
;
160 SLIST_REMOVE(&rpc
->fragments
, fragment
);
161 rpc_free_fragment(fragment
);
165 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
167 struct rpc_fragment
*fragment
;
169 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
171 fragment
= malloc(sizeof(struct rpc_fragment
));
172 if (fragment
== NULL
) {
176 fragment
->size
= size
;
177 fragment
->data
= malloc(fragment
->size
);
178 if(fragment
->data
== NULL
) {
183 memcpy(fragment
->data
, data
, fragment
->size
);
184 SLIST_ADD_END(&rpc
->fragments
, fragment
);
188 void rpc_destroy_context(struct rpc_context
*rpc
)
192 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
194 while((pdu
= rpc
->outqueue
) != NULL
) {
195 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
196 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
197 rpc_free_pdu(rpc
, pdu
);
199 while((pdu
= rpc
->waitpdu
) != NULL
) {
200 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
201 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
202 rpc_free_pdu(rpc
, pdu
);
205 rpc_free_all_fragments(rpc
);
207 auth_destroy(rpc
->auth
);
214 if (rpc
->encodebuf
!= NULL
) {
215 free(rpc
->encodebuf
);
216 rpc
->encodebuf
= NULL
;
219 if (rpc
->error_string
!= NULL
) {
220 free(rpc
->error_string
);
221 rpc
->error_string
= NULL
;
224 if (rpc
->udp_dest
!= NULL
) {
226 rpc
->udp_dest
= NULL
;