[win32] - make it compile on win32
[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
a8a1b858
M
15#ifdef WIN32
16#include "win32_compat.h"
eecdc4f3
RS
17#else
18#include <unistd.h>
19#include <strings.h>
a8a1b858
M
20#endif/*WIN32*/
21#define _GNU_SOURCE
eecdc4f3 22
84004dbf
RS
23#include <stdio.h>
24#include <stdarg.h>
84004dbf 25#include <string.h>
98f5fee8
M
26#include <stdlib.h>
27#include <rpc/rpc.h>
84004dbf
RS
28#include <rpc/xdr.h>
29#include "slist.h"
30#include "libnfs.h"
31#include "libnfs-raw.h"
32#include "libnfs-private.h"
33
34struct rpc_context *rpc_init_context(void)
35{
36 struct rpc_context *rpc;
37
38 rpc = malloc(sizeof(struct rpc_context));
39 if (rpc == NULL) {
84004dbf
RS
40 return NULL;
41 }
ea98629a 42 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf
RS
43
44 rpc->encodebuflen = 65536;
45 rpc->encodebuf = malloc(rpc->encodebuflen);
46 if (rpc->encodebuf == NULL) {
84004dbf
RS
47 free(rpc);
48 return NULL;
49 }
50
eecdc4f3
RS
51#if defined(WIN32)
52 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
53#else
54 rpc->auth = authunix_create_default();
55#endif
84004dbf 56 if (rpc->auth == NULL) {
84004dbf
RS
57 free(rpc->encodebuf);
58 free(rpc);
59 return NULL;
60 }
61 rpc->xid = 1;
62 rpc->fd = -1;
63
64 return rpc;
65}
66
67
9e00b8c6
RS
68struct rpc_context *rpc_init_udp_context(void)
69{
70 struct rpc_context *rpc;
71
72 rpc = rpc_init_context();
73 if (rpc != NULL) {
74 rpc->is_udp = 1;
75 }
76
77 return rpc;
78}
79
84004dbf
RS
80void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
81{
82 if (rpc->auth != NULL) {
83 auth_destroy(rpc->auth);
84 }
bb4e9ed6 85 rpc->auth = (AUTH *)auth;
84004dbf
RS
86}
87
88
89void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
90{
91 va_list ap;
84004dbf
RS
92
93 if (rpc->error_string != NULL) {
94 free(rpc->error_string);
95 }
96 va_start(ap, error_string);
b85c7de2
RS
97 rpc->error_string = malloc(1024);
98 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
99 va_end(ap);
100}
101
102char *rpc_get_error(struct rpc_context *rpc)
103{
104 return rpc->error_string;
105}
106
107void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
108{
109 struct rpc_pdu *pdu;
110
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);
115 }
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);
120 }
121}
122
123
124void rpc_destroy_context(struct rpc_context *rpc)
125{
126 struct rpc_pdu *pdu;
127
128 while((pdu = rpc->outqueue) != NULL) {
129 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
130 SLIST_REMOVE(&rpc->outqueue, pdu);
131 rpc_free_pdu(rpc, pdu);
132 }
133 while((pdu = rpc->waitpdu) != NULL) {
134 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
135 SLIST_REMOVE(&rpc->waitpdu, pdu);
136 rpc_free_pdu(rpc, pdu);
137 }
138
139 auth_destroy(rpc->auth);
140 rpc->auth =NULL;
141
142 if (rpc->fd != -1) {
eecdc4f3
RS
143#if defined(WIN32)
144 closesocket(rpc->fd);
145#else
146 close(rpc->fd);
147#endif
84004dbf
RS
148 }
149
150 if (rpc->encodebuf != NULL) {
151 free(rpc->encodebuf);
152 rpc->encodebuf = NULL;
153 }
154
155 if (rpc->error_string != NULL) {
156 free(rpc->error_string);
157 rpc->error_string = NULL;
158 }
159
7ff2f3a0
RS
160 if (rpc->udp_dest != NULL) {
161 free(rpc->udp_dest);
162 rpc->udp_dest = NULL;
163 }
164
84004dbf
RS
165 free(rpc);
166}
167
168