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"
29 #include "libnfs-zdr.h"
31 #include "libnfs-raw.h"
32 #include "libnfs-private.h"
34 struct rpc_context
*rpc_init_context(void)
36 struct rpc_context
*rpc
;
38 rpc
= malloc(sizeof(struct rpc_context
));
42 memset(rpc
, 0, sizeof(struct rpc_context
));
44 rpc
->magic
= RPC_CONTEXT_MAGIC
;
45 rpc
->encodebuflen
= 65536;
46 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
47 if (rpc
->encodebuf
== NULL
) {
53 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
55 rpc
->auth
= authunix_create_default();
57 if (rpc
->auth
== NULL
) {
69 struct rpc_context
*rpc_init_udp_context(void)
71 struct rpc_context
*rpc
;
73 rpc
= rpc_init_context();
81 void rpc_set_auth(struct rpc_context
*rpc
, struct AUTH
*auth
)
83 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
85 if (rpc
->auth
!= NULL
) {
86 auth_destroy(rpc
->auth
);
92 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
96 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
98 if (rpc
->error_string
!= NULL
) {
99 free(rpc
->error_string
);
101 va_start(ap
, error_string
);
102 rpc
->error_string
= malloc(1024);
103 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
107 char *rpc_get_error(struct rpc_context
*rpc
)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
111 return rpc
->error_string
;
114 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
118 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
120 while((pdu
= rpc
->outqueue
) != NULL
) {
121 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
122 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
123 rpc_free_pdu(rpc
, pdu
);
125 while((pdu
= rpc
->waitpdu
) != NULL
) {
126 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
127 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
128 rpc_free_pdu(rpc
, pdu
);
132 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
134 if (fragment
->data
!= NULL
) {
135 free(fragment
->data
);
140 void rpc_free_all_fragments(struct rpc_context
*rpc
)
142 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
144 while (rpc
->fragments
!= NULL
) {
145 struct rpc_fragment
*fragment
= rpc
->fragments
;
147 SLIST_REMOVE(&rpc
->fragments
, fragment
);
148 rpc_free_fragment(fragment
);
152 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
154 struct rpc_fragment
*fragment
;
156 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
158 fragment
= malloc(sizeof(struct rpc_fragment
));
159 if (fragment
== NULL
) {
163 fragment
->size
= size
;
164 fragment
->data
= malloc(fragment
->size
);
165 if(fragment
->data
== NULL
) {
170 memcpy(fragment
->data
, data
, fragment
->size
);
171 SLIST_ADD_END(&rpc
->fragments
, fragment
);
175 void rpc_destroy_context(struct rpc_context
*rpc
)
179 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
181 while((pdu
= rpc
->outqueue
) != NULL
) {
182 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
183 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
184 rpc_free_pdu(rpc
, pdu
);
186 while((pdu
= rpc
->waitpdu
) != NULL
) {
187 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
188 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
189 rpc_free_pdu(rpc
, pdu
);
192 rpc_free_all_fragments(rpc
);
194 auth_destroy(rpc
->auth
);
199 closesocket(rpc
->fd
);
205 if (rpc
->encodebuf
!= NULL
) {
206 free(rpc
->encodebuf
);
207 rpc
->encodebuf
= NULL
;
210 if (rpc
->error_string
!= NULL
) {
211 free(rpc
->error_string
);
212 rpc
->error_string
= NULL
;
215 if (rpc
->udp_dest
!= NULL
) {
217 rpc
->udp_dest
= NULL
;