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" | |
eecdc4f3 | 24 | #else |
eecdc4f3 | 25 | #include <strings.h> |
a8a1b858 M |
26 | #endif/*WIN32*/ |
27 | #define _GNU_SOURCE | |
eecdc4f3 | 28 | |
00748f36 RS |
29 | #ifdef HAVE_UNISTD_H |
30 | #include <unistd.h> | |
31 | #endif | |
32 | ||
84004dbf RS |
33 | #include <stdio.h> |
34 | #include <stdarg.h> | |
84004dbf | 35 | #include <string.h> |
98f5fee8 | 36 | #include <stdlib.h> |
f3a75078 | 37 | #include <assert.h> |
a2756193 | 38 | #include <time.h> |
84004dbf | 39 | #include "slist.h" |
763cd6e3 | 40 | #include "libnfs-zdr.h" |
84004dbf RS |
41 | #include "libnfs.h" |
42 | #include "libnfs-raw.h" | |
43 | #include "libnfs-private.h" | |
44 | ||
45 | struct rpc_context *rpc_init_context(void) | |
46 | { | |
47 | struct rpc_context *rpc; | |
a2756193 | 48 | static uint32_t salt = 0; |
84004dbf RS |
49 | |
50 | rpc = malloc(sizeof(struct rpc_context)); | |
51 | if (rpc == NULL) { | |
84004dbf RS |
52 | return NULL; |
53 | } | |
ea98629a | 54 | memset(rpc, 0, sizeof(struct rpc_context)); |
84004dbf | 55 | |
f3a75078 | 56 | rpc->magic = RPC_CONTEXT_MAGIC; |
84004dbf RS |
57 | rpc->encodebuflen = 65536; |
58 | rpc->encodebuf = malloc(rpc->encodebuflen); | |
59 | if (rpc->encodebuf == NULL) { | |
84004dbf RS |
60 | free(rpc); |
61 | return NULL; | |
62 | } | |
63 | ||
eecdc4f3 | 64 | rpc->auth = authunix_create_default(); |
84004dbf | 65 | if (rpc->auth == NULL) { |
84004dbf RS |
66 | free(rpc->encodebuf); |
67 | free(rpc); | |
68 | return NULL; | |
69 | } | |
a2756193 SC |
70 | rpc->xid = salt + time(NULL); |
71 | salt += 0x01000000; | |
84004dbf RS |
72 | rpc->fd = -1; |
73 | ||
74 | return rpc; | |
75 | } | |
76 | ||
77 | ||
9e00b8c6 RS |
78 | struct rpc_context *rpc_init_udp_context(void) |
79 | { | |
80 | struct rpc_context *rpc; | |
81 | ||
82 | rpc = rpc_init_context(); | |
83 | if (rpc != NULL) { | |
84 | rpc->is_udp = 1; | |
85 | } | |
86 | ||
87 | return rpc; | |
88 | } | |
89 | ||
67ba2239 | 90 | void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) |
84004dbf | 91 | { |
f3a75078 RS |
92 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
93 | ||
84004dbf RS |
94 | if (rpc->auth != NULL) { |
95 | auth_destroy(rpc->auth); | |
96 | } | |
8704724f | 97 | rpc->auth = auth; |
84004dbf RS |
98 | } |
99 | ||
100 | ||
101 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) | |
102 | { | |
103 | va_list ap; | |
84004dbf | 104 | |
f3a75078 RS |
105 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
106 | ||
84004dbf RS |
107 | if (rpc->error_string != NULL) { |
108 | free(rpc->error_string); | |
109 | } | |
110 | va_start(ap, error_string); | |
b85c7de2 RS |
111 | rpc->error_string = malloc(1024); |
112 | vsnprintf(rpc->error_string, 1024, error_string, ap); | |
84004dbf RS |
113 | va_end(ap); |
114 | } | |
115 | ||
116 | char *rpc_get_error(struct rpc_context *rpc) | |
117 | { | |
f3a75078 RS |
118 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
119 | ||
84004dbf RS |
120 | return rpc->error_string; |
121 | } | |
122 | ||
123 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error) | |
124 | { | |
125 | struct rpc_pdu *pdu; | |
126 | ||
f3a75078 RS |
127 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
128 | ||
84004dbf RS |
129 | while((pdu = rpc->outqueue) != NULL) { |
130 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
131 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
132 | rpc_free_pdu(rpc, pdu); | |
133 | } | |
134 | while((pdu = rpc->waitpdu) != NULL) { | |
135 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
136 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
137 | rpc_free_pdu(rpc, pdu); | |
138 | } | |
139 | } | |
140 | ||
d678b73e RS |
141 | static void rpc_free_fragment(struct rpc_fragment *fragment) |
142 | { | |
143 | if (fragment->data != NULL) { | |
144 | free(fragment->data); | |
145 | } | |
146 | free(fragment); | |
147 | } | |
148 | ||
149 | void rpc_free_all_fragments(struct rpc_context *rpc) | |
150 | { | |
f3a75078 RS |
151 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
152 | ||
d678b73e RS |
153 | while (rpc->fragments != NULL) { |
154 | struct rpc_fragment *fragment = rpc->fragments; | |
155 | ||
156 | SLIST_REMOVE(&rpc->fragments, fragment); | |
157 | rpc_free_fragment(fragment); | |
158 | } | |
159 | } | |
160 | ||
183451cf | 161 | int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size) |
d678b73e RS |
162 | { |
163 | struct rpc_fragment *fragment; | |
164 | ||
f3a75078 RS |
165 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
166 | ||
d678b73e RS |
167 | fragment = malloc(sizeof(struct rpc_fragment)); |
168 | if (fragment == NULL) { | |
169 | return -1; | |
170 | } | |
171 | ||
172 | fragment->size = size; | |
173 | fragment->data = malloc(fragment->size); | |
174 | if(fragment->data == NULL) { | |
175 | free(fragment); | |
176 | return -1; | |
177 | } | |
178 | ||
179 | memcpy(fragment->data, data, fragment->size); | |
180 | SLIST_ADD_END(&rpc->fragments, fragment); | |
181 | return 0; | |
182 | } | |
84004dbf RS |
183 | |
184 | void rpc_destroy_context(struct rpc_context *rpc) | |
185 | { | |
186 | struct rpc_pdu *pdu; | |
187 | ||
f3a75078 RS |
188 | assert(rpc->magic == RPC_CONTEXT_MAGIC); |
189 | ||
84004dbf RS |
190 | while((pdu = rpc->outqueue) != NULL) { |
191 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
192 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
193 | rpc_free_pdu(rpc, pdu); | |
194 | } | |
195 | while((pdu = rpc->waitpdu) != NULL) { | |
196 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
197 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
198 | rpc_free_pdu(rpc, pdu); | |
199 | } | |
200 | ||
d678b73e RS |
201 | rpc_free_all_fragments(rpc); |
202 | ||
84004dbf RS |
203 | auth_destroy(rpc->auth); |
204 | rpc->auth =NULL; | |
205 | ||
206 | if (rpc->fd != -1) { | |
eecdc4f3 | 207 | close(rpc->fd); |
84004dbf RS |
208 | } |
209 | ||
210 | if (rpc->encodebuf != NULL) { | |
211 | free(rpc->encodebuf); | |
212 | rpc->encodebuf = NULL; | |
213 | } | |
214 | ||
215 | if (rpc->error_string != NULL) { | |
216 | free(rpc->error_string); | |
217 | rpc->error_string = NULL; | |
218 | } | |
219 | ||
7ff2f3a0 RS |
220 | if (rpc->udp_dest != NULL) { |
221 | free(rpc->udp_dest); | |
222 | rpc->udp_dest = NULL; | |
223 | } | |
224 | ||
f3a75078 | 225 | rpc->magic = 0; |
84004dbf RS |
226 | free(rpc); |
227 | } | |
228 | ||
229 |