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