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