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