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
;
89 struct rpc_context
*rpc_init_udp_context(void)
91 struct rpc_context
*rpc
;
93 rpc
= rpc_init_context();
101 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
103 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
105 if (rpc
->auth
!= NULL
) {
106 auth_destroy(rpc
->auth
);
111 static void rpc_set_uid_gid(struct rpc_context
*rpc
, int uid
, int gid
) {
112 if (uid
!= rpc
->uid
|| gid
!= rpc
->gid
) {
113 struct AUTH
*auth
= libnfs_authunix_create("libnfs", uid
, gid
, 0, NULL
);
115 rpc_set_auth(rpc
, auth
);
122 void rpc_set_uid(struct rpc_context
*rpc
, int uid
) {
123 rpc_set_uid_gid(rpc
, uid
, rpc
->gid
);
126 void rpc_set_gid(struct rpc_context
*rpc
, int gid
) {
127 rpc_set_uid_gid(rpc
, rpc
->uid
, gid
);
130 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
134 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
136 if (rpc
->error_string
!= NULL
) {
137 free(rpc
->error_string
);
139 va_start(ap
, error_string
);
140 rpc
->error_string
= malloc(1024);
141 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
145 char *rpc_get_error(struct rpc_context
*rpc
)
147 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
149 return rpc
->error_string
;
152 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
158 while((pdu
= rpc
->outqueue
) != NULL
) {
159 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
160 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
161 rpc_free_pdu(rpc
, pdu
);
163 while((pdu
= rpc
->waitpdu
) != NULL
) {
164 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
165 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
166 rpc_free_pdu(rpc
, pdu
);
170 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
172 if (fragment
->data
!= NULL
) {
173 free(fragment
->data
);
178 void rpc_free_all_fragments(struct rpc_context
*rpc
)
180 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
182 while (rpc
->fragments
!= NULL
) {
183 struct rpc_fragment
*fragment
= rpc
->fragments
;
185 SLIST_REMOVE(&rpc
->fragments
, fragment
);
186 rpc_free_fragment(fragment
);
190 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
192 struct rpc_fragment
*fragment
;
194 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
196 fragment
= malloc(sizeof(struct rpc_fragment
));
197 if (fragment
== NULL
) {
201 fragment
->size
= size
;
202 fragment
->data
= malloc(fragment
->size
);
203 if(fragment
->data
== NULL
) {
208 memcpy(fragment
->data
, data
, fragment
->size
);
209 SLIST_ADD_END(&rpc
->fragments
, fragment
);
213 void rpc_destroy_context(struct rpc_context
*rpc
)
217 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
219 while((pdu
= rpc
->outqueue
) != NULL
) {
220 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
221 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
222 rpc_free_pdu(rpc
, pdu
);
224 while((pdu
= rpc
->waitpdu
) != NULL
) {
225 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
226 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
227 rpc_free_pdu(rpc
, pdu
);
230 rpc_free_all_fragments(rpc
);
232 auth_destroy(rpc
->auth
);
239 if (rpc
->encodebuf
!= NULL
) {
240 free(rpc
->encodebuf
);
241 rpc
->encodebuf
= NULL
;
244 if (rpc
->error_string
!= NULL
) {
245 free(rpc
->error_string
);
246 rpc
->error_string
= NULL
;
249 if (rpc
->udp_dest
!= NULL
) {
251 rpc
->udp_dest
= NULL
;