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/>.
16 #include "win32_compat.h"
28 #include "libnfs-zdr.h"
30 #include "libnfs-raw.h"
31 #include "libnfs-private.h"
33 struct rpc_context
*rpc_init_context(void)
35 struct rpc_context
*rpc
;
37 rpc
= malloc(sizeof(struct rpc_context
));
41 memset(rpc
, 0, sizeof(struct rpc_context
));
43 rpc
->encodebuflen
= 65536;
44 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
45 if (rpc
->encodebuf
== NULL
) {
51 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
53 rpc
->auth
= authunix_create_default();
55 if (rpc
->auth
== NULL
) {
67 struct rpc_context
*rpc_init_udp_context(void)
69 struct rpc_context
*rpc
;
71 rpc
= rpc_init_context();
79 void rpc_set_auth(struct rpc_context
*rpc
, AUTH
*auth
)
81 if (rpc
->auth
!= NULL
) {
82 auth_destroy(rpc
->auth
);
88 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
92 if (rpc
->error_string
!= NULL
) {
93 free(rpc
->error_string
);
95 va_start(ap
, error_string
);
96 rpc
->error_string
= malloc(1024);
97 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
101 char *rpc_get_error(struct rpc_context
*rpc
)
103 return rpc
->error_string
;
106 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
110 while((pdu
= rpc
->outqueue
) != NULL
) {
111 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
112 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
113 rpc_free_pdu(rpc
, pdu
);
115 while((pdu
= rpc
->waitpdu
) != NULL
) {
116 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
117 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
118 rpc_free_pdu(rpc
, pdu
);
122 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
124 if (fragment
->data
!= NULL
) {
125 free(fragment
->data
);
130 void rpc_free_all_fragments(struct rpc_context
*rpc
)
132 while (rpc
->fragments
!= NULL
) {
133 struct rpc_fragment
*fragment
= rpc
->fragments
;
135 SLIST_REMOVE(&rpc
->fragments
, fragment
);
136 rpc_free_fragment(fragment
);
140 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
142 struct rpc_fragment
*fragment
;
144 fragment
= malloc(sizeof(struct rpc_fragment
));
145 if (fragment
== NULL
) {
149 fragment
->size
= size
;
150 fragment
->data
= malloc(fragment
->size
);
151 if(fragment
->data
== NULL
) {
156 memcpy(fragment
->data
, data
, fragment
->size
);
157 SLIST_ADD_END(&rpc
->fragments
, fragment
);
161 void rpc_destroy_context(struct rpc_context
*rpc
)
165 while((pdu
= rpc
->outqueue
) != NULL
) {
166 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
167 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
168 rpc_free_pdu(rpc
, pdu
);
170 while((pdu
= rpc
->waitpdu
) != NULL
) {
171 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
172 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
173 rpc_free_pdu(rpc
, pdu
);
176 rpc_free_all_fragments(rpc
);
178 auth_destroy(rpc
->auth
);
183 closesocket(rpc
->fd
);
189 if (rpc
->encodebuf
!= NULL
) {
190 free(rpc
->encodebuf
);
191 rpc
->encodebuf
= NULL
;
194 if (rpc
->error_string
!= NULL
) {
195 free(rpc
->error_string
);
196 rpc
->error_string
= NULL
;
199 if (rpc
->udp_dest
!= NULL
) {
201 rpc
->udp_dest
= NULL
;