222bee92405009fc3294fb58600ca8627ee82dc3
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define _GNU_SOURCE
19
20 #if defined(WIN32)
21 #include <winsock2.h>
22 #else
23 #include <unistd.h>
24 #include <strings.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <rpc/rpc.h>
32 #include <rpc/xdr.h>
33 #include "slist.h"
34 #include "libnfs.h"
35 #include "libnfs-raw.h"
36 #include "libnfs-private.h"
37
38 struct rpc_context *rpc_init_context(void)
39 {
40 struct rpc_context *rpc;
41
42 rpc = malloc(sizeof(struct rpc_context));
43 if (rpc == NULL) {
44 return NULL;
45 }
46 bzero(rpc, sizeof(struct rpc_context));
47
48 rpc->encodebuflen = 65536;
49 rpc->encodebuf = malloc(rpc->encodebuflen);
50 if (rpc->encodebuf == NULL) {
51 free(rpc);
52 return NULL;
53 }
54
55 #if defined(WIN32)
56 rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL);
57 #else
58 rpc->auth = authunix_create_default();
59 #endif
60 if (rpc->auth == NULL) {
61 free(rpc->encodebuf);
62 free(rpc);
63 return NULL;
64 }
65 rpc->xid = 1;
66 rpc->fd = -1;
67
68 return rpc;
69 }
70
71
72 struct rpc_context *rpc_init_udp_context(void)
73 {
74 struct rpc_context *rpc;
75
76 rpc = rpc_init_context();
77 if (rpc != NULL) {
78 rpc->is_udp = 1;
79 }
80
81 return rpc;
82 }
83
84 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
85 {
86 if (rpc->auth != NULL) {
87 auth_destroy(rpc->auth);
88 }
89 rpc->auth = auth;
90 }
91
92
93 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
94 {
95 va_list ap;
96 char *str;
97
98 if (rpc->error_string != NULL) {
99 free(rpc->error_string);
100 }
101 va_start(ap, error_string);
102 // vasprintf(&str, error_string, ap);
103 // rpc->error_string = str;
104 va_end(ap);
105 }
106
107 char *rpc_get_error(struct rpc_context *rpc)
108 {
109 return rpc->error_string;
110 }
111
112 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
113 {
114 struct rpc_pdu *pdu;
115
116 while((pdu = rpc->outqueue) != NULL) {
117 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
118 SLIST_REMOVE(&rpc->outqueue, pdu);
119 rpc_free_pdu(rpc, pdu);
120 }
121 while((pdu = rpc->waitpdu) != NULL) {
122 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
123 SLIST_REMOVE(&rpc->waitpdu, pdu);
124 rpc_free_pdu(rpc, pdu);
125 }
126 }
127
128
129 void rpc_destroy_context(struct rpc_context *rpc)
130 {
131 struct rpc_pdu *pdu;
132
133 while((pdu = rpc->outqueue) != NULL) {
134 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
135 SLIST_REMOVE(&rpc->outqueue, pdu);
136 rpc_free_pdu(rpc, pdu);
137 }
138 while((pdu = rpc->waitpdu) != NULL) {
139 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
140 SLIST_REMOVE(&rpc->waitpdu, pdu);
141 rpc_free_pdu(rpc, pdu);
142 }
143
144 auth_destroy(rpc->auth);
145 rpc->auth =NULL;
146
147 if (rpc->fd != -1) {
148 #if defined(WIN32)
149 closesocket(rpc->fd);
150 #else
151 close(rpc->fd);
152 #endif
153 }
154
155 if (rpc->encodebuf != NULL) {
156 free(rpc->encodebuf);
157 rpc->encodebuf = NULL;
158 }
159
160 if (rpc->error_string != NULL) {
161 free(rpc->error_string);
162 rpc->error_string = NULL;
163 }
164
165 if (rpc->udp_dest != NULL) {
166 free(rpc->udp_dest);
167 rpc->udp_dest = NULL;
168 }
169
170 free(rpc);
171 }
172
173