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