bbed183fdf4fc8367d63d4630fdc96da2d2891dd
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;
54 rpc
= malloc(sizeof(struct rpc_context
));
58 memset(rpc
, 0, sizeof(struct rpc_context
));
60 rpc
->magic
= RPC_CONTEXT_MAGIC
;
62 /* Allow 1M of data (for writes) and some */
63 rpc
->encodebuflen
= 1024 * 1024 + 4096;
64 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
65 if (rpc
->encodebuf
== NULL
) {
70 rpc
->auth
= authunix_create_default();
71 if (rpc
->auth
== NULL
) {
76 rpc
->xid
= salt
+ time(NULL
) + getpid() << 16;
79 rpc
->tcp_syncnt
= RPC_PARAM_UNDEFINED
;
80 #if defined(WIN32) || defined(ANDROID)
87 rpc_reset_queue(&rpc
->outqueue
);
88 for (i
= 0; i
< HASHES
; i
++)
89 rpc_reset_queue(&rpc
->waitpdu
[i
]);
95 struct rpc_context
*rpc_init_udp_context(void)
97 struct rpc_context
*rpc
;
99 rpc
= rpc_init_context();
107 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
111 if (rpc
->auth
!= NULL
) {
112 auth_destroy(rpc
->auth
);
117 static void rpc_set_uid_gid(struct rpc_context
*rpc
, int uid
, int gid
) {
118 if (uid
!= rpc
->uid
|| gid
!= rpc
->gid
) {
119 struct AUTH
*auth
= libnfs_authunix_create("libnfs", uid
, gid
, 0, NULL
);
121 rpc_set_auth(rpc
, auth
);
128 void rpc_set_uid(struct rpc_context
*rpc
, int uid
) {
129 rpc_set_uid_gid(rpc
, uid
, rpc
->gid
);
132 void rpc_set_gid(struct rpc_context
*rpc
, int gid
) {
133 rpc_set_uid_gid(rpc
, rpc
->uid
, gid
);
136 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
139 char *old_error_string
= rpc
->error_string
;
141 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
143 va_start(ap
, error_string
);
144 rpc
->error_string
= malloc(1024);
145 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
148 if (old_error_string
!= NULL
) {
149 free(old_error_string
);
153 char *rpc_get_error(struct rpc_context
*rpc
)
155 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
157 return rpc
->error_string
;
160 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
162 struct rpc_pdu
*pdu
, *next
;
165 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
167 while ((pdu
= rpc
->outqueue
.head
) != NULL
) {
168 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
169 rpc
->outqueue
.head
= pdu
->next
;
170 rpc_free_pdu(rpc
, pdu
);
172 rpc
->outqueue
.tail
= NULL
;
174 for (i
= 0; i
< HASHES
; i
++) {
175 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
177 while((pdu
= q
->head
) != NULL
) {
178 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
180 rpc_free_pdu(rpc
, pdu
);
186 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
188 if (fragment
->data
!= NULL
) {
189 free(fragment
->data
);
194 void rpc_free_all_fragments(struct rpc_context
*rpc
)
196 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
198 while (rpc
->fragments
!= NULL
) {
199 struct rpc_fragment
*fragment
= rpc
->fragments
;
201 rpc
->fragments
= fragment
->next
;
202 rpc_free_fragment(fragment
);
206 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
208 struct rpc_fragment
*fragment
;
210 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
212 fragment
= malloc(sizeof(struct rpc_fragment
));
213 if (fragment
== NULL
) {
217 fragment
->size
= size
;
218 fragment
->data
= malloc(fragment
->size
);
219 if(fragment
->data
== NULL
) {
224 memcpy(fragment
->data
, data
, fragment
->size
);
225 LIBNFS_LIST_ADD_END(&rpc
->fragments
, fragment
);
229 void rpc_destroy_context(struct rpc_context
*rpc
)
234 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
236 while((pdu
= rpc
->outqueue
.head
) != NULL
) {
237 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
238 LIBNFS_LIST_REMOVE(&rpc
->outqueue
.head
, pdu
);
239 rpc_free_pdu(rpc
, pdu
);
242 for (i
= 0; i
< HASHES
; i
++) {
243 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
245 while((pdu
= q
->head
) != NULL
) {
246 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
247 LIBNFS_LIST_REMOVE(&q
->head
, pdu
);
248 rpc_free_pdu(rpc
, pdu
);
252 rpc_free_all_fragments(rpc
);
254 auth_destroy(rpc
->auth
);
261 if (rpc
->encodebuf
!= NULL
) {
262 free(rpc
->encodebuf
);
263 rpc
->encodebuf
= NULL
;
266 if (rpc
->error_string
!= NULL
) {
267 free(rpc
->error_string
);
268 rpc
->error_string
= NULL
;
271 if (rpc
->udp_dest
!= NULL
) {
273 rpc
->udp_dest
= NULL
;