Commit | Line | Data |
---|---|---|
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 | 26 | #include <stdlib.h> |
f3a75078 | 27 | #include <assert.h> |
84004dbf | 28 | #include "slist.h" |
763cd6e3 | 29 | #include "libnfs-zdr.h" |
84004dbf RS |
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) { | |
84004dbf RS |
40 | return NULL; |
41 | } | |
ea98629a | 42 | memset(rpc, 0, sizeof(struct rpc_context)); |
84004dbf | 43 | |
f3a75078 | 44 | rpc->magic = RPC_CONTEXT_MAGIC; |
84004dbf RS |
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 |
69 | struct 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 | ||
67ba2239 | 81 | void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) |
84004dbf | 82 | { |
f3a75078 RS |
83 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
84 | ||
84004dbf RS |
85 | if (rpc->auth != NULL) { |
86 | auth_destroy(rpc->auth); | |
87 | } | |
8704724f | 88 | rpc->auth = auth; |
84004dbf RS |
89 | } |
90 | ||
91 | ||
92 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) | |
93 | { | |
94 | va_list ap; | |
84004dbf | 95 | |
f3a75078 RS |
96 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
97 | ||
84004dbf RS |
98 | if (rpc->error_string != NULL) { |
99 | free(rpc->error_string); | |
100 | } | |
101 | va_start(ap, error_string); | |
b85c7de2 RS |
102 | rpc->error_string = malloc(1024); |
103 | vsnprintf(rpc->error_string, 1024, error_string, ap); | |
84004dbf RS |
104 | va_end(ap); |
105 | } | |
106 | ||
107 | char *rpc_get_error(struct rpc_context *rpc) | |
108 | { | |
f3a75078 RS |
109 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
110 | ||
84004dbf RS |
111 | return rpc->error_string; |
112 | } | |
113 | ||
114 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error) | |
115 | { | |
116 | struct rpc_pdu *pdu; | |
117 | ||
f3a75078 RS |
118 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
119 | ||
84004dbf RS |
120 | while((pdu = rpc->outqueue) != NULL) { |
121 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
122 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
123 | rpc_free_pdu(rpc, pdu); | |
124 | } | |
125 | while((pdu = rpc->waitpdu) != NULL) { | |
126 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
127 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
128 | rpc_free_pdu(rpc, pdu); | |
129 | } | |
130 | } | |
131 | ||
d678b73e RS |
132 | static void rpc_free_fragment(struct rpc_fragment *fragment) |
133 | { | |
134 | if (fragment->data != NULL) { | |
135 | free(fragment->data); | |
136 | } | |
137 | free(fragment); | |
138 | } | |
139 | ||
140 | void rpc_free_all_fragments(struct rpc_context *rpc) | |
141 | { | |
f3a75078 RS |
142 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
143 | ||
d678b73e RS |
144 | while (rpc->fragments != NULL) { |
145 | struct rpc_fragment *fragment = rpc->fragments; | |
146 | ||
147 | SLIST_REMOVE(&rpc->fragments, fragment); | |
148 | rpc_free_fragment(fragment); | |
149 | } | |
150 | } | |
151 | ||
183451cf | 152 | int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size) |
d678b73e RS |
153 | { |
154 | struct rpc_fragment *fragment; | |
155 | ||
f3a75078 RS |
156 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
157 | ||
d678b73e RS |
158 | fragment = malloc(sizeof(struct rpc_fragment)); |
159 | if (fragment == NULL) { | |
160 | return -1; | |
161 | } | |
162 | ||
163 | fragment->size = size; | |
164 | fragment->data = malloc(fragment->size); | |
165 | if(fragment->data == NULL) { | |
166 | free(fragment); | |
167 | return -1; | |
168 | } | |
169 | ||
170 | memcpy(fragment->data, data, fragment->size); | |
171 | SLIST_ADD_END(&rpc->fragments, fragment); | |
172 | return 0; | |
173 | } | |
84004dbf RS |
174 | |
175 | void rpc_destroy_context(struct rpc_context *rpc) | |
176 | { | |
177 | struct rpc_pdu *pdu; | |
178 | ||
f3a75078 RS |
179 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
180 | ||
84004dbf RS |
181 | while((pdu = rpc->outqueue) != NULL) { |
182 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
183 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
184 | rpc_free_pdu(rpc, pdu); | |
185 | } | |
186 | while((pdu = rpc->waitpdu) != NULL) { | |
187 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
188 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
189 | rpc_free_pdu(rpc, pdu); | |
190 | } | |
191 | ||
d678b73e RS |
192 | rpc_free_all_fragments(rpc); |
193 | ||
84004dbf RS |
194 | auth_destroy(rpc->auth); |
195 | rpc->auth =NULL; | |
196 | ||
197 | if (rpc->fd != -1) { | |
eecdc4f3 RS |
198 | #if defined(WIN32) |
199 | closesocket(rpc->fd); | |
200 | #else | |
201 | close(rpc->fd); | |
202 | #endif | |
84004dbf RS |
203 | } |
204 | ||
205 | if (rpc->encodebuf != NULL) { | |
206 | free(rpc->encodebuf); | |
207 | rpc->encodebuf = NULL; | |
208 | } | |
209 | ||
210 | if (rpc->error_string != NULL) { | |
211 | free(rpc->error_string); | |
212 | rpc->error_string = NULL; | |
213 | } | |
214 | ||
7ff2f3a0 RS |
215 | if (rpc->udp_dest != NULL) { |
216 | free(rpc->udp_dest); | |
217 | rpc->udp_dest = NULL; | |
218 | } | |
219 | ||
f3a75078 | 220 | rpc->magic = 0; |
84004dbf RS |
221 | free(rpc); |
222 | } | |
223 | ||
224 |