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"
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
->encodebuflen
= 65536;
45 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
46 if (rpc
->encodebuf
== NULL
) {
52 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
54 rpc
->auth
= authunix_create_default();
56 if (rpc
->auth
== NULL
) {
68 struct rpc_context
*rpc_init_udp_context(void)
70 struct rpc_context
*rpc
;
72 rpc
= rpc_init_context();
80 void rpc_set_auth(struct rpc_context
*rpc
, AUTH
*auth
)
82 if (rpc
->auth
!= NULL
) {
83 auth_destroy(rpc
->auth
);
89 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
93 if (rpc
->error_string
!= NULL
) {
94 free(rpc
->error_string
);
96 va_start(ap
, error_string
);
97 rpc
->error_string
= malloc(1024);
98 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
102 char *rpc_get_error(struct rpc_context
*rpc
)
104 return rpc
->error_string
;
107 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
111 while((pdu
= rpc
->outqueue
) != NULL
) {
112 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
113 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
114 rpc_free_pdu(rpc
, pdu
);
116 while((pdu
= rpc
->waitpdu
) != NULL
) {
117 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
118 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
119 rpc_free_pdu(rpc
, pdu
);
123 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
125 if (fragment
->data
!= NULL
) {
126 free(fragment
->data
);
131 void rpc_free_all_fragments(struct rpc_context
*rpc
)
133 while (rpc
->fragments
!= NULL
) {
134 struct rpc_fragment
*fragment
= rpc
->fragments
;
136 SLIST_REMOVE(&rpc
->fragments
, fragment
);
137 rpc_free_fragment(fragment
);
141 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
143 struct rpc_fragment
*fragment
;
145 fragment
= malloc(sizeof(struct rpc_fragment
));
146 if (fragment
== NULL
) {
150 fragment
->size
= size
;
151 fragment
->data
= malloc(fragment
->size
);
152 if(fragment
->data
== NULL
) {
157 memcpy(fragment
->data
, data
, fragment
->size
);
158 SLIST_ADD_END(&rpc
->fragments
, fragment
);
162 void rpc_destroy_context(struct rpc_context
*rpc
)
166 while((pdu
= rpc
->outqueue
) != NULL
) {
167 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
168 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
169 rpc_free_pdu(rpc
, pdu
);
171 while((pdu
= rpc
->waitpdu
) != NULL
) {
172 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
173 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
174 rpc_free_pdu(rpc
, pdu
);
177 rpc_free_all_fragments(rpc
);
179 auth_destroy(rpc
->auth
);
184 closesocket(rpc
->fd
);
190 if (rpc
->encodebuf
!= NULL
) {
191 free(rpc
->encodebuf
);
192 rpc
->encodebuf
= NULL
;
195 if (rpc
->error_string
!= NULL
) {
196 free(rpc
->error_string
);
197 rpc
->error_string
= NULL
;
200 if (rpc
->udp_dest
!= NULL
) {
202 rpc
->udp_dest
= NULL
;