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((char *)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
);
86 rpc
->auth
= (AUTH
*)auth
;
90 void rpc_set_error(struct rpc_context
*rpc
, char *error_string
, ...)
94 if (rpc
->error_string
!= NULL
) {
95 free(rpc
->error_string
);
97 va_start(ap
, error_string
);
98 rpc
->error_string
= malloc(1024);
99 vsnprintf(rpc
->error_string
, 1024, error_string
, ap
);
103 char *rpc_get_error(struct rpc_context
*rpc
)
105 return rpc
->error_string
;
108 void rpc_error_all_pdus(struct rpc_context
*rpc
, char *error
)
112 while((pdu
= rpc
->outqueue
) != NULL
) {
113 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
114 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
115 rpc_free_pdu(rpc
, pdu
);
117 while((pdu
= rpc
->waitpdu
) != NULL
) {
118 pdu
->cb(rpc
, RPC_STATUS_ERROR
, error
, pdu
->private_data
);
119 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
120 rpc_free_pdu(rpc
, pdu
);
125 void rpc_destroy_context(struct rpc_context
*rpc
)
129 while((pdu
= rpc
->outqueue
) != NULL
) {
130 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
131 SLIST_REMOVE(&rpc
->outqueue
, pdu
);
132 rpc_free_pdu(rpc
, pdu
);
134 while((pdu
= rpc
->waitpdu
) != NULL
) {
135 pdu
->cb(rpc
, RPC_STATUS_CANCEL
, NULL
, pdu
->private_data
);
136 SLIST_REMOVE(&rpc
->waitpdu
, pdu
);
137 rpc_free_pdu(rpc
, pdu
);
140 auth_destroy(rpc
->auth
);
145 closesocket(rpc
->fd
);
151 if (rpc
->encodebuf
!= NULL
) {
152 free(rpc
->encodebuf
);
153 rpc
->encodebuf
= NULL
;
156 if (rpc
->error_string
!= NULL
) {
157 free(rpc
->error_string
);
158 rpc
->error_string
= NULL
;
161 if (rpc
->udp_dest
!= NULL
) {
163 rpc
->udp_dest
= NULL
;