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
;
61 /* Allow 1M of data (for writes) and some */
62 rpc
->encodebuflen
= 1024 * 1024 + 4096;
63 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
64 if (rpc
->encodebuf
== NULL
) {
69 rpc
->auth
= authunix_create_default();
70 if (rpc
->auth
== NULL
) {
75 rpc
->xid
= salt
+ time(NULL
) + getpid() << 16;
83 struct rpc_context
*rpc_init_udp_context(void)
85 struct rpc_context
*rpc
;
87 rpc
= rpc_init_context();
95 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
97 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
99 if (rpc
->auth
!= NULL
) {
100 auth_destroy(rpc
->auth
);
106 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
110 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
112 if (rpc
->error_string
!= NULL
) {
113 free(rpc
->error_string
);
115 va_start(ap
, error_string
);
116 rpc
->error_string
= malloc(1024);
117 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
121 char *rpc_get_error(struct rpc_context
*rpc
)
123 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
125 return rpc
->error_string
;
128 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
132 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
134 while((pdu
= rpc
->outqueue
) != NULL
) {
135 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
136 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
137 rpc_free_pdu(rpc
, pdu
);
139 while((pdu
= rpc
->waitpdu
) != NULL
) {
140 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
141 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
142 rpc_free_pdu(rpc
, pdu
);
146 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
148 if (fragment
->data
!= NULL
) {
149 free(fragment
->data
);
154 void rpc_free_all_fragments(struct rpc_context
*rpc
)
156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
158 while (rpc
->fragments
!= NULL
) {
159 struct rpc_fragment
*fragment
= rpc
->fragments
;
161 SLIST_REMOVE(&rpc
->fragments
, fragment
);
162 rpc_free_fragment(fragment
);
166 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
168 struct rpc_fragment
*fragment
;
170 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
172 fragment
= malloc(sizeof(struct rpc_fragment
));
173 if (fragment
== NULL
) {
177 fragment
->size
= size
;
178 fragment
->data
= malloc(fragment
->size
);
179 if(fragment
->data
== NULL
) {
184 memcpy(fragment
->data
, data
, fragment
->size
);
185 SLIST_ADD_END(&rpc
->fragments
, fragment
);
189 void rpc_destroy_context(struct rpc_context
*rpc
)
193 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
195 while((pdu
= rpc
->outqueue
) != NULL
) {
196 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
197 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
198 rpc_free_pdu(rpc
, pdu
);
200 while((pdu
= rpc
->waitpdu
) != NULL
) {
201 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
202 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
203 rpc_free_pdu(rpc
, pdu
);
206 rpc_free_all_fragments(rpc
);
208 auth_destroy(rpc
->auth
);
215 if (rpc
->encodebuf
!= NULL
) {
216 free(rpc
->encodebuf
);
217 rpc
->encodebuf
= NULL
;
220 if (rpc
->error_string
!= NULL
) {
221 free(rpc
->error_string
);
222 rpc
->error_string
= NULL
;
225 if (rpc
->udp_dest
!= NULL
) {
227 rpc
->udp_dest
= NULL
;