99ec1596e5927be44eaf358b17abf0d2176644fc
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
]);
94 void rpc_set_readahead(struct rpc_context
*rpc
, uint32_t v
)
96 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
101 struct rpc_context
*rpc_init_udp_context(void)
103 struct rpc_context
*rpc
;
105 rpc
= rpc_init_context();
113 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
115 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
117 if (rpc
->auth
!= NULL
) {
118 auth_destroy(rpc
->auth
);
123 static void rpc_set_uid_gid(struct rpc_context
*rpc
, int uid
, int gid
) {
124 if (uid
!= rpc
->uid
|| gid
!= rpc
->gid
) {
125 struct AUTH
*auth
= libnfs_authunix_create("libnfs", uid
, gid
, 0, NULL
);
127 rpc_set_auth(rpc
, auth
);
134 void rpc_set_uid(struct rpc_context
*rpc
, int uid
) {
135 rpc_set_uid_gid(rpc
, uid
, rpc
->gid
);
138 void rpc_set_gid(struct rpc_context
*rpc
, int gid
) {
139 rpc_set_uid_gid(rpc
, rpc
->uid
, gid
);
142 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
145 char *old_error_string
= rpc
->error_string
;
147 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
149 va_start(ap
, error_string
);
150 rpc
->error_string
= malloc(1024);
151 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
154 if (old_error_string
!= NULL
) {
155 free(old_error_string
);
159 char *rpc_get_error(struct rpc_context
*rpc
)
161 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
163 return rpc
->error_string
;
166 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
168 struct rpc_pdu
*pdu
, *next
;
171 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
173 while ((pdu
= rpc
->outqueue
.head
) != NULL
) {
174 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
175 rpc
->outqueue
.head
= pdu
->next
;
176 rpc_free_pdu(rpc
, pdu
);
178 rpc
->outqueue
.tail
= NULL
;
180 for (i
= 0; i
< HASHES
; i
++) {
181 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
183 while((pdu
= q
->head
) != NULL
) {
184 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
186 rpc_free_pdu(rpc
, pdu
);
192 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
194 if (fragment
->data
!= NULL
) {
195 free(fragment
->data
);
200 void rpc_free_all_fragments(struct rpc_context
*rpc
)
202 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
204 while (rpc
->fragments
!= NULL
) {
205 struct rpc_fragment
*fragment
= rpc
->fragments
;
207 rpc
->fragments
= fragment
->next
;
208 rpc_free_fragment(fragment
);
212 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
214 struct rpc_fragment
*fragment
;
216 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
218 fragment
= malloc(sizeof(struct rpc_fragment
));
219 if (fragment
== NULL
) {
223 fragment
->size
= size
;
224 fragment
->data
= malloc(fragment
->size
);
225 if(fragment
->data
== NULL
) {
230 memcpy(fragment
->data
, data
, fragment
->size
);
231 LIBNFS_LIST_ADD_END(&rpc
->fragments
, fragment
);
235 void rpc_destroy_context(struct rpc_context
*rpc
)
240 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
242 while((pdu
= rpc
->outqueue
.head
) != NULL
) {
243 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
244 rpc
->outqueue
.head
= pdu
->next
;
245 rpc_free_pdu(rpc
, pdu
);
248 for (i
= 0; i
< HASHES
; i
++) {
249 struct rpc_queue
*q
= &rpc
->waitpdu
[i
];
251 while((pdu
= q
->head
) != NULL
) {
252 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
253 rpc
->outqueue
.head
= pdu
->next
;
254 rpc_free_pdu(rpc
, pdu
);
258 rpc_free_all_fragments(rpc
);
260 auth_destroy(rpc
->auth
);
267 if (rpc
->encodebuf
!= NULL
) {
268 free(rpc
->encodebuf
);
269 rpc
->encodebuf
= NULL
;
272 if (rpc
->error_string
!= NULL
) {
273 free(rpc
->error_string
);
274 rpc
->error_string
= NULL
;
277 if (rpc
->udp_dest
!= NULL
) {
279 rpc
->udp_dest
= NULL
;