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/>.
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 bzero(rpc
, sizeof(struct rpc_context
));
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 if (rpc
->auth
!= NULL
) {
84 auth_destroy(rpc
->auth
);
90 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
95 if (rpc
->error_string
!= NULL
) {
96 free(rpc
->error_string
);
98 va_start(ap
, error_string
);
101 vsnprintf(str
, 1024, error_string
, ap
);
103 vasprintf(&str
, error_string
, ap
);
105 rpc
->error_string
= str
;
109 char *rpc_get_error(struct rpc_context
*rpc
)
111 return rpc
->error_string
;
114 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
118 while((pdu
= rpc
->outqueue
) != NULL
) {
119 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
120 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
121 rpc_free_pdu(rpc
, pdu
);
123 while((pdu
= rpc
->waitpdu
) != NULL
) {
124 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
125 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
126 rpc_free_pdu(rpc
, pdu
);
131 void rpc_destroy_context(struct rpc_context
*rpc
)
135 while((pdu
= rpc
->outqueue
) != NULL
) {
136 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
137 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
138 rpc_free_pdu(rpc
, pdu
);
140 while((pdu
= rpc
->waitpdu
) != NULL
) {
141 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
142 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
143 rpc_free_pdu(rpc
, pdu
);
146 auth_destroy(rpc
->auth
);
151 closesocket(rpc
->fd
);
157 if (rpc
->encodebuf
!= NULL
) {
158 free(rpc
->encodebuf
);
159 rpc
->encodebuf
= NULL
;
162 if (rpc
->error_string
!= NULL
) {
163 free(rpc
->error_string
);
164 rpc
->error_string
= NULL
;
167 if (rpc
->udp_dest
!= NULL
) {
169 rpc
->udp_dest
= NULL
;