Win32: add workaround for lack of vasprintf() and use vsnprintf() on windows on a...
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
2606f9bb 4
84004dbf
RS
5
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.
10
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/>.
13*/
14
15#define _GNU_SOURCE
eecdc4f3
RS
16
17#if defined(WIN32)
18#include <winsock2.h>
19#else
20#include <unistd.h>
21#include <strings.h>
22#endif
23
84004dbf
RS
24#include <stdio.h>
25#include <stdarg.h>
84004dbf 26#include <string.h>
98f5fee8
M
27#include <stdlib.h>
28#include <rpc/rpc.h>
84004dbf
RS
29#include <rpc/xdr.h>
30#include "slist.h"
31#include "libnfs.h"
32#include "libnfs-raw.h"
33#include "libnfs-private.h"
34
35struct rpc_context *rpc_init_context(void)
36{
37 struct rpc_context *rpc;
38
39 rpc = malloc(sizeof(struct rpc_context));
40 if (rpc == NULL) {
84004dbf
RS
41 return NULL;
42 }
43 bzero(rpc, sizeof(struct rpc_context));
44
45 rpc->encodebuflen = 65536;
46 rpc->encodebuf = malloc(rpc->encodebuflen);
47 if (rpc->encodebuf == NULL) {
84004dbf
RS
48 free(rpc);
49 return NULL;
50 }
51
eecdc4f3
RS
52#if defined(WIN32)
53 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
54#else
55 rpc->auth = authunix_create_default();
56#endif
84004dbf 57 if (rpc->auth == NULL) {
84004dbf
RS
58 free(rpc->encodebuf);
59 free(rpc);
60 return NULL;
61 }
62 rpc->xid = 1;
63 rpc->fd = -1;
64
65 return rpc;
66}
67
68
9e00b8c6
RS
69struct rpc_context *rpc_init_udp_context(void)
70{
71 struct rpc_context *rpc;
72
73 rpc = rpc_init_context();
74 if (rpc != NULL) {
75 rpc->is_udp = 1;
76 }
77
78 return rpc;
79}
80
84004dbf
RS
81void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
82{
83 if (rpc->auth != NULL) {
84 auth_destroy(rpc->auth);
85 }
86 rpc->auth = auth;
87}
88
89
90void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
91{
92 va_list ap;
93 char *str;
94
95 if (rpc->error_string != NULL) {
96 free(rpc->error_string);
97 }
98 va_start(ap, error_string);
2606f9bb
RS
99#if defined (WIN32)
100 str = malloc(1024);
101 vsnprintf(str, 1024, error_string, ap);
102#else
103 vasprintf(&str, error_string, ap);
104#endif
105 rpc->error_string = str;
84004dbf
RS
106 va_end(ap);
107}
108
109char *rpc_get_error(struct rpc_context *rpc)
110{
111 return rpc->error_string;
112}
113
114void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
115{
116 struct rpc_pdu *pdu;
117
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);
122 }
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);
127 }
128}
129
130
131void rpc_destroy_context(struct rpc_context *rpc)
132{
133 struct rpc_pdu *pdu;
134
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);
139 }
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);
144 }
145
146 auth_destroy(rpc->auth);
147 rpc->auth =NULL;
148
149 if (rpc->fd != -1) {
eecdc4f3
RS
150#if defined(WIN32)
151 closesocket(rpc->fd);
152#else
153 close(rpc->fd);
154#endif
84004dbf
RS
155 }
156
157 if (rpc->encodebuf != NULL) {
158 free(rpc->encodebuf);
159 rpc->encodebuf = NULL;
160 }
161
162 if (rpc->error_string != NULL) {
163 free(rpc->error_string);
164 rpc->error_string = NULL;
165 }
166
7ff2f3a0
RS
167 if (rpc->udp_dest != NULL) {
168 free(rpc->udp_dest);
169 rpc->udp_dest = NULL;
170 }
171
84004dbf
RS
172 free(rpc);
173}
174
175