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;
78 rpc
->tcp_syncnt
= RPC_PARAM_UNDEFINED
;
91 struct rpc_context
*rpc_init_udp_context(void)
93 struct rpc_context
*rpc
;
95 rpc
= rpc_init_context();
103 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
105 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
107 if (rpc
->auth
!= NULL
) {
108 auth_destroy(rpc
->auth
);
113 static void rpc_set_uid_gid(struct rpc_context
*rpc
, int uid
, int gid
) {
114 if (uid
!= rpc
->uid
|| gid
!= rpc
->gid
) {
115 struct AUTH
*auth
= libnfs_authunix_create("libnfs", uid
, gid
, 0, NULL
);
117 rpc_set_auth(rpc
, auth
);
124 void rpc_set_uid(struct rpc_context
*rpc
, int uid
) {
125 rpc_set_uid_gid(rpc
, uid
, rpc
->gid
);
128 void rpc_set_gid(struct rpc_context
*rpc
, int gid
) {
129 rpc_set_uid_gid(rpc
, rpc
->uid
, gid
);
132 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
136 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
138 if (rpc
->error_string
!= NULL
) {
139 free(rpc
->error_string
);
141 va_start(ap
, error_string
);
142 rpc
->error_string
= malloc(1024);
143 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
147 char *rpc_get_error(struct rpc_context
*rpc
)
149 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
151 return rpc
->error_string
;
154 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
158 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
160 while((pdu
= rpc
->outqueue
) != NULL
) {
161 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
162 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
163 rpc_free_pdu(rpc
, pdu
);
165 while((pdu
= rpc
->waitpdu
) != NULL
) {
166 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
167 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
168 rpc_free_pdu(rpc
, pdu
);
172 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
174 if (fragment
->data
!= NULL
) {
175 free(fragment
->data
);
180 void rpc_free_all_fragments(struct rpc_context
*rpc
)
182 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
184 while (rpc
->fragments
!= NULL
) {
185 struct rpc_fragment
*fragment
= rpc
->fragments
;
187 SLIST_REMOVE(&rpc
->fragments
, fragment
);
188 rpc_free_fragment(fragment
);
192 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
194 struct rpc_fragment
*fragment
;
196 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
198 fragment
= malloc(sizeof(struct rpc_fragment
));
199 if (fragment
== NULL
) {
203 fragment
->size
= size
;
204 fragment
->data
= malloc(fragment
->size
);
205 if(fragment
->data
== NULL
) {
210 memcpy(fragment
->data
, data
, fragment
->size
);
211 SLIST_ADD_END(&rpc
->fragments
, fragment
);
215 void rpc_destroy_context(struct rpc_context
*rpc
)
219 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
221 while((pdu
= rpc
->outqueue
) != NULL
) {
222 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
223 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
224 rpc_free_pdu(rpc
, pdu
);
226 while((pdu
= rpc
->waitpdu
) != NULL
) {
227 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
228 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
229 rpc_free_pdu(rpc
, pdu
);
232 rpc_free_all_fragments(rpc
);
234 auth_destroy(rpc
->auth
);
241 if (rpc
->encodebuf
!= NULL
) {
242 free(rpc
->encodebuf
);
243 rpc
->encodebuf
= NULL
;
246 if (rpc
->error_string
!= NULL
) {
247 free(rpc
->error_string
);
248 rpc
->error_string
= NULL
;
251 if (rpc
->udp_dest
!= NULL
) {
253 rpc
->udp_dest
= NULL
;