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
, ...)
135 char *old_error_string
= rpc
->error_string
;
137 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
139 va_start(ap
, error_string
);
140 rpc
->error_string
= malloc(1024);
141 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
144 if (old_error_string
!= NULL
) {
145 free(old_error_string
);
149 char *rpc_get_error(struct rpc_context
*rpc
)
151 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
153 return rpc
->error_string
;
156 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
160 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
162 while((pdu
= rpc
->outqueue
) != NULL
) {
163 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
164 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
165 rpc_free_pdu(rpc
, pdu
);
167 while((pdu
= rpc
->waitpdu
) != NULL
) {
168 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
169 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
170 rpc_free_pdu(rpc
, pdu
);
174 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
176 if (fragment
->data
!= NULL
) {
177 free(fragment
->data
);
182 void rpc_free_all_fragments(struct rpc_context
*rpc
)
184 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
186 while (rpc
->fragments
!= NULL
) {
187 struct rpc_fragment
*fragment
= rpc
->fragments
;
189 SLIST_REMOVE(&rpc
->fragments
, fragment
);
190 rpc_free_fragment(fragment
);
194 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
196 struct rpc_fragment
*fragment
;
198 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
200 fragment
= malloc(sizeof(struct rpc_fragment
));
201 if (fragment
== NULL
) {
205 fragment
->size
= size
;
206 fragment
->data
= malloc(fragment
->size
);
207 if(fragment
->data
== NULL
) {
212 memcpy(fragment
->data
, data
, fragment
->size
);
213 SLIST_ADD_END(&rpc
->fragments
, fragment
);
217 void rpc_destroy_context(struct rpc_context
*rpc
)
221 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
223 while((pdu
= rpc
->outqueue
) != NULL
) {
224 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
225 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
226 rpc_free_pdu(rpc
, pdu
);
228 while((pdu
= rpc
->waitpdu
) != NULL
) {
229 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
230 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
231 rpc_free_pdu(rpc
, pdu
);
234 rpc_free_all_fragments(rpc
);
236 auth_destroy(rpc
->auth
);
243 if (rpc
->encodebuf
!= NULL
) {
244 free(rpc
->encodebuf
);
245 rpc
->encodebuf
= NULL
;
248 if (rpc
->error_string
!= NULL
) {
249 free(rpc
->error_string
);
250 rpc
->error_string
= NULL
;
253 if (rpc
->udp_dest
!= NULL
) {
255 rpc
->udp_dest
= NULL
;