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