[win32] - make it compile on win32
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4
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 #ifdef WIN32
16 #include "win32_compat.h"
17 #else
18 #include <unistd.h>
19 #include <strings.h>
20 #endif/*WIN32*/
21 #define _GNU_SOURCE
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <rpc/rpc.h>
28 #include <rpc/xdr.h>
29 #include "slist.h"
30 #include "libnfs.h"
31 #include "libnfs-raw.h"
32 #include "libnfs-private.h"
33
34 struct rpc_context *rpc_init_context(void)
35 {
36 struct rpc_context *rpc;
37
38 rpc = malloc(sizeof(struct rpc_context));
39 if (rpc == NULL) {
40 return NULL;
41 }
42 memset(rpc, 0, sizeof(struct rpc_context));
43
44 rpc->encodebuflen = 65536;
45 rpc->encodebuf = malloc(rpc->encodebuflen);
46 if (rpc->encodebuf == NULL) {
47 free(rpc);
48 return NULL;
49 }
50
51 #if defined(WIN32)
52 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
53 #else
54 rpc->auth = authunix_create_default();
55 #endif
56 if (rpc->auth == NULL) {
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
68 struct 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
80 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
81 {
82 if (rpc->auth != NULL) {
83 auth_destroy(rpc->auth);
84 }
85 rpc->auth = (AUTH *)auth;
86 }
87
88
89 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
90 {
91 va_list ap;
92
93 if (rpc->error_string != NULL) {
94 free(rpc->error_string);
95 }
96 va_start(ap, error_string);
97 rpc->error_string = malloc(1024);
98 vsnprintf(rpc->error_string, 1024, error_string, ap);
99 va_end(ap);
100 }
101
102 char *rpc_get_error(struct rpc_context *rpc)
103 {
104 return rpc->error_string;
105 }
106
107 void 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
124 void 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) {
143 #if defined(WIN32)
144 closesocket(rpc->fd);
145 #else
146 close(rpc->fd);
147 #endif
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
160 if (rpc->udp_dest != NULL) {
161 free(rpc->udp_dest);
162 rpc->udp_dest = NULL;
163 }
164
165 free(rpc);
166 }
167
168