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"
32 #include "libnfs-raw.h"
33 #include "libnfs-private.h"
35 struct rpc_context
*rpc_init_context(void)
37 struct rpc_context
*rpc
;
39 rpc
= malloc(sizeof(struct rpc_context
));
43 memset(rpc
, 0, sizeof(struct rpc_context
));
45 rpc
->magic
= RPC_CONTEXT_MAGIC
;
46 rpc
->encodebuflen
= 65536;
47 rpc
->encodebuf
= malloc(rpc
->encodebuflen
);
48 if (rpc
->encodebuf
== NULL
) {
54 rpc
->auth
= authunix_create("LibNFS", 65535, 65535, 0, NULL
);
56 rpc
->auth
= authunix_create_default();
58 if (rpc
->auth
== NULL
) {
70 struct rpc_context
*rpc_init_udp_context(void)
72 struct rpc_context
*rpc
;
74 rpc
= rpc_init_context();
82 void rpc_set_auth(struct rpc_context
*rpc
, AUTH
*auth
)
84 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
86 if (rpc
->auth
!= NULL
) {
87 auth_destroy(rpc
->auth
);
93 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
97 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
99 if (rpc
->error_string
!= NULL
) {
100 free(rpc
->error_string
);
102 va_start(ap
, error_string
);
103 rpc
->error_string
= malloc(1024);
104 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
108 char *rpc_get_error(struct rpc_context
*rpc
)
110 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
112 return rpc
->error_string
;
115 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
119 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
121 while((pdu
= rpc
->outqueue
) != NULL
) {
122 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
123 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
124 rpc_free_pdu(rpc
, pdu
);
126 while((pdu
= rpc
->waitpdu
) != NULL
) {
127 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
128 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
129 rpc_free_pdu(rpc
, pdu
);
133 static void rpc_free_fragment(struct rpc_fragment
*fragment
)
135 if (fragment
->data
!= NULL
) {
136 free(fragment
->data
);
141 void rpc_free_all_fragments(struct rpc_context
*rpc
)
143 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
145 while (rpc
->fragments
!= NULL
) {
146 struct rpc_fragment
*fragment
= rpc
->fragments
;
148 SLIST_REMOVE(&rpc
->fragments
, fragment
);
149 rpc_free_fragment(fragment
);
153 int rpc_add_fragment(struct rpc_context
*rpc
, char *data
, uint64_t size
)
155 struct rpc_fragment
*fragment
;
157 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
159 fragment
= malloc(sizeof(struct rpc_fragment
));
160 if (fragment
== NULL
) {
164 fragment
->size
= size
;
165 fragment
->data
= malloc(fragment
->size
);
166 if(fragment
->data
== NULL
) {
171 memcpy(fragment
->data
, data
, fragment
->size
);
172 SLIST_ADD_END(&rpc
->fragments
, fragment
);
176 void rpc_destroy_context(struct rpc_context
*rpc
)
180 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
182 while((pdu
= rpc
->outqueue
) != NULL
) {
183 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
184 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
185 rpc_free_pdu(rpc
, pdu
);
187 while((pdu
= rpc
->waitpdu
) != NULL
) {
188 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
189 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
190 rpc_free_pdu(rpc
, pdu
);
193 rpc_free_all_fragments(rpc
);
195 auth_destroy(rpc
->auth
);
200 closesocket(rpc
->fd
);
206 if (rpc
->encodebuf
!= NULL
) {
207 free(rpc
->encodebuf
);
208 rpc
->encodebuf
= NULL
;
211 if (rpc
->error_string
!= NULL
) {
212 free(rpc
->error_string
);
213 rpc
->error_string
= NULL
;
216 if (rpc
->udp_dest
!= NULL
) {
218 rpc
->udp_dest
= NULL
;