Commit | Line | Data |
---|---|---|
84004dbf RS |
1 | /* |
2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> | |
3 | ||
f0888d12 RS |
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. | |
84004dbf RS |
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 | */ | |
00748f36 RS |
17 | #ifdef HAVE_CONFIG_H |
18 | #include "config.h" | |
19 | #endif | |
84004dbf | 20 | |
108c622a RS |
21 | #ifdef AROS |
22 | #include "aros_compat.h" | |
23 | #endif | |
24 | ||
a8a1b858 M |
25 | #ifdef WIN32 |
26 | #include "win32_compat.h" | |
bff8fe46 RS |
27 | #endif |
28 | ||
a8a1b858 | 29 | #define _GNU_SOURCE |
eecdc4f3 | 30 | |
00748f36 RS |
31 | #ifdef HAVE_UNISTD_H |
32 | #include <unistd.h> | |
33 | #endif | |
34 | ||
bff8fe46 RS |
35 | #ifdef HAVE_STRINGS_H |
36 | #include <strings.h> | |
37 | #endif | |
38 | ||
84004dbf RS |
39 | #include <stdio.h> |
40 | #include <stdarg.h> | |
84004dbf | 41 | #include <string.h> |
98f5fee8 | 42 | #include <stdlib.h> |
f3a75078 | 43 | #include <assert.h> |
a2756193 | 44 | #include <time.h> |
84004dbf | 45 | #include "slist.h" |
763cd6e3 | 46 | #include "libnfs-zdr.h" |
84004dbf RS |
47 | #include "libnfs.h" |
48 | #include "libnfs-raw.h" | |
49 | #include "libnfs-private.h" | |
50 | ||
51 | struct rpc_context *rpc_init_context(void) | |
52 | { | |
53 | struct rpc_context *rpc; | |
a2756193 | 54 | static uint32_t salt = 0; |
63f36a09 | 55 | unsigned int i; |
84004dbf RS |
56 | |
57 | rpc = malloc(sizeof(struct rpc_context)); | |
58 | if (rpc == NULL) { | |
84004dbf RS |
59 | return NULL; |
60 | } | |
ea98629a | 61 | memset(rpc, 0, sizeof(struct rpc_context)); |
84004dbf | 62 | |
f3a75078 | 63 | rpc->magic = RPC_CONTEXT_MAGIC; |
35280fd7 RS |
64 | |
65 | /* Allow 1M of data (for writes) and some */ | |
66 | rpc->encodebuflen = 1024 * 1024 + 4096; | |
84004dbf RS |
67 | rpc->encodebuf = malloc(rpc->encodebuflen); |
68 | if (rpc->encodebuf == NULL) { | |
84004dbf RS |
69 | free(rpc); |
70 | return NULL; | |
71 | } | |
72 | ||
eecdc4f3 | 73 | rpc->auth = authunix_create_default(); |
84004dbf | 74 | if (rpc->auth == NULL) { |
84004dbf RS |
75 | free(rpc->encodebuf); |
76 | free(rpc); | |
77 | return NULL; | |
78 | } | |
6fda9871 | 79 | rpc->xid = salt + time(NULL) + getpid() << 16; |
a2756193 | 80 | salt += 0x01000000; |
84004dbf | 81 | rpc->fd = -1; |
1c8b4547 | 82 | rpc->tcp_syncnt = RPC_PARAM_UNDEFINED; |
3af0c022 | 83 | #if defined(WIN32) || defined(ANDROID) |
9126c9c0 PL |
84 | rpc->uid = 65534; |
85 | rpc->gid = 65534; | |
86 | #else | |
87 | rpc->uid = getuid(); | |
88 | rpc->gid = getgid(); | |
89 | #endif | |
aec45c62 | 90 | rpc_reset_queue(&rpc->outqueue); |
63f36a09 MH |
91 | for (i = 0; i < HASHES; i++) |
92 | rpc_reset_queue(&rpc->waitpdu[i]); | |
84004dbf RS |
93 | |
94 | return rpc; | |
95 | } | |
96 | ||
97 | ||
9e00b8c6 RS |
98 | struct rpc_context *rpc_init_udp_context(void) |
99 | { | |
100 | struct rpc_context *rpc; | |
101 | ||
102 | rpc = rpc_init_context(); | |
103 | if (rpc != NULL) { | |
104 | rpc->is_udp = 1; | |
105 | } | |
106 | ||
107 | return rpc; | |
108 | } | |
109 | ||
67ba2239 | 110 | void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) |
84004dbf | 111 | { |
f3a75078 RS |
112 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
113 | ||
84004dbf RS |
114 | if (rpc->auth != NULL) { |
115 | auth_destroy(rpc->auth); | |
116 | } | |
8704724f | 117 | rpc->auth = auth; |
84004dbf RS |
118 | } |
119 | ||
9126c9c0 PL |
120 | static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) { |
121 | if (uid != rpc->uid || gid != rpc->gid) { | |
122 | struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL); | |
123 | if (auth != NULL) { | |
124 | rpc_set_auth(rpc, auth); | |
125 | rpc->uid = uid; | |
126 | rpc->gid = gid; | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | void rpc_set_uid(struct rpc_context *rpc, int uid) { | |
132 | rpc_set_uid_gid(rpc, uid, rpc->gid); | |
133 | } | |
134 | ||
135 | void rpc_set_gid(struct rpc_context *rpc, int gid) { | |
136 | rpc_set_uid_gid(rpc, rpc->uid, gid); | |
137 | } | |
84004dbf RS |
138 | |
139 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) | |
140 | { | |
141 | va_list ap; | |
f0cb8042 | 142 | char *old_error_string = rpc->error_string; |
84004dbf | 143 | |
f3a75078 RS |
144 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
145 | ||
84004dbf | 146 | va_start(ap, error_string); |
b85c7de2 RS |
147 | rpc->error_string = malloc(1024); |
148 | vsnprintf(rpc->error_string, 1024, error_string, ap); | |
84004dbf | 149 | va_end(ap); |
f0cb8042 RS |
150 | |
151 | if (old_error_string != NULL) { | |
152 | free(old_error_string); | |
153 | } | |
84004dbf RS |
154 | } |
155 | ||
156 | char *rpc_get_error(struct rpc_context *rpc) | |
157 | { | |
f3a75078 RS |
158 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
159 | ||
84004dbf RS |
160 | return rpc->error_string; |
161 | } | |
162 | ||
163 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error) | |
164 | { | |
aec45c62 | 165 | struct rpc_pdu *pdu, *next; |
63f36a09 | 166 | unsigned int i; |
84004dbf | 167 | |
f3a75078 RS |
168 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
169 | ||
aec45c62 | 170 | while ((pdu = rpc->outqueue.head) != NULL) { |
84004dbf | 171 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); |
aec45c62 | 172 | rpc->outqueue.head = pdu->next; |
84004dbf RS |
173 | rpc_free_pdu(rpc, pdu); |
174 | } | |
aec45c62 MH |
175 | rpc->outqueue.tail = NULL; |
176 | ||
63f36a09 MH |
177 | for (i = 0; i < HASHES; i++) { |
178 | struct rpc_queue *q = &rpc->waitpdu[i]; | |
179 | ||
180 | while((pdu = q->head) != NULL) { | |
181 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
182 | q->head = pdu->next; | |
183 | rpc_free_pdu(rpc, pdu); | |
184 | } | |
185 | q->tail = NULL; | |
84004dbf RS |
186 | } |
187 | } | |
188 | ||
d678b73e RS |
189 | static void rpc_free_fragment(struct rpc_fragment *fragment) |
190 | { | |
191 | if (fragment->data != NULL) { | |
192 | free(fragment->data); | |
193 | } | |
194 | free(fragment); | |
195 | } | |
196 | ||
197 | void rpc_free_all_fragments(struct rpc_context *rpc) | |
198 | { | |
f3a75078 RS |
199 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
200 | ||
d678b73e RS |
201 | while (rpc->fragments != NULL) { |
202 | struct rpc_fragment *fragment = rpc->fragments; | |
203 | ||
aec45c62 | 204 | rpc->fragments = fragment->next; |
d678b73e RS |
205 | rpc_free_fragment(fragment); |
206 | } | |
207 | } | |
208 | ||
183451cf | 209 | int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size) |
d678b73e RS |
210 | { |
211 | struct rpc_fragment *fragment; | |
212 | ||
f3a75078 RS |
213 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
214 | ||
d678b73e RS |
215 | fragment = malloc(sizeof(struct rpc_fragment)); |
216 | if (fragment == NULL) { | |
217 | return -1; | |
218 | } | |
219 | ||
220 | fragment->size = size; | |
221 | fragment->data = malloc(fragment->size); | |
222 | if(fragment->data == NULL) { | |
223 | free(fragment); | |
224 | return -1; | |
225 | } | |
226 | ||
227 | memcpy(fragment->data, data, fragment->size); | |
8a52596b | 228 | LIBNFS_LIST_ADD_END(&rpc->fragments, fragment); |
d678b73e RS |
229 | return 0; |
230 | } | |
84004dbf RS |
231 | |
232 | void rpc_destroy_context(struct rpc_context *rpc) | |
233 | { | |
234 | struct rpc_pdu *pdu; | |
63f36a09 | 235 | unsigned int i; |
84004dbf | 236 | |
f3a75078 RS |
237 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
238 | ||
aec45c62 | 239 | while((pdu = rpc->outqueue.head) != NULL) { |
84004dbf | 240 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); |
1a6ec3ee | 241 | LIBNFS_LIST_REMOVE(&rpc->outqueue.head, pdu); |
84004dbf RS |
242 | rpc_free_pdu(rpc, pdu); |
243 | } | |
63f36a09 MH |
244 | |
245 | for (i = 0; i < HASHES; i++) { | |
246 | struct rpc_queue *q = &rpc->waitpdu[i]; | |
247 | ||
248 | while((pdu = q->head) != NULL) { | |
249 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
1a6ec3ee | 250 | LIBNFS_LIST_REMOVE(&q->head, pdu); |
63f36a09 MH |
251 | rpc_free_pdu(rpc, pdu); |
252 | } | |
84004dbf RS |
253 | } |
254 | ||
d678b73e RS |
255 | rpc_free_all_fragments(rpc); |
256 | ||
84004dbf RS |
257 | auth_destroy(rpc->auth); |
258 | rpc->auth =NULL; | |
259 | ||
260 | if (rpc->fd != -1) { | |
eecdc4f3 | 261 | close(rpc->fd); |
84004dbf RS |
262 | } |
263 | ||
264 | if (rpc->encodebuf != NULL) { | |
265 | free(rpc->encodebuf); | |
266 | rpc->encodebuf = NULL; | |
267 | } | |
268 | ||
269 | if (rpc->error_string != NULL) { | |
270 | free(rpc->error_string); | |
271 | rpc->error_string = NULL; | |
272 | } | |
273 | ||
7ff2f3a0 RS |
274 | if (rpc->udp_dest != NULL) { |
275 | free(rpc->udp_dest); | |
276 | rpc->udp_dest = NULL; | |
277 | } | |
278 | ||
f3a75078 | 279 | rpc->magic = 0; |
84004dbf RS |
280 | free(rpc); |
281 | } | |
282 | ||
283 |