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 | ||
15 | #define _GNU_SOURCE | |
eecdc4f3 RS |
16 | |
17 | #if defined(WIN32) | |
18 | #include <winsock2.h> | |
19 | #else | |
20 | #include <unistd.h> | |
21 | #include <strings.h> | |
22 | #endif | |
23 | ||
84004dbf RS |
24 | #include <stdio.h> |
25 | #include <stdarg.h> | |
84004dbf | 26 | #include <string.h> |
98f5fee8 M |
27 | #include <stdlib.h> |
28 | #include <rpc/rpc.h> | |
84004dbf RS |
29 | #include <rpc/xdr.h> |
30 | #include "slist.h" | |
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; | |
38 | ||
39 | rpc = malloc(sizeof(struct rpc_context)); | |
40 | if (rpc == NULL) { | |
84004dbf RS |
41 | return NULL; |
42 | } | |
43 | bzero(rpc, sizeof(struct rpc_context)); | |
44 | ||
45 | rpc->encodebuflen = 65536; | |
46 | rpc->encodebuf = malloc(rpc->encodebuflen); | |
47 | if (rpc->encodebuf == NULL) { | |
84004dbf RS |
48 | free(rpc); |
49 | return NULL; | |
50 | } | |
51 | ||
eecdc4f3 RS |
52 | #if defined(WIN32) |
53 | rpc->auth = authunix_create("LibNFS", 65535, 65535, 0, NULL); | |
54 | #else | |
55 | rpc->auth = authunix_create_default(); | |
56 | #endif | |
84004dbf | 57 | if (rpc->auth == NULL) { |
84004dbf RS |
58 | free(rpc->encodebuf); |
59 | free(rpc); | |
60 | return NULL; | |
61 | } | |
62 | rpc->xid = 1; | |
63 | rpc->fd = -1; | |
64 | ||
65 | return rpc; | |
66 | } | |
67 | ||
68 | ||
9e00b8c6 RS |
69 | struct rpc_context *rpc_init_udp_context(void) |
70 | { | |
71 | struct rpc_context *rpc; | |
72 | ||
73 | rpc = rpc_init_context(); | |
74 | if (rpc != NULL) { | |
75 | rpc->is_udp = 1; | |
76 | } | |
77 | ||
78 | return rpc; | |
79 | } | |
80 | ||
84004dbf RS |
81 | void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) |
82 | { | |
83 | if (rpc->auth != NULL) { | |
84 | auth_destroy(rpc->auth); | |
85 | } | |
86 | rpc->auth = auth; | |
87 | } | |
88 | ||
89 | ||
90 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) | |
91 | { | |
92 | va_list ap; | |
84004dbf RS |
93 | |
94 | if (rpc->error_string != NULL) { | |
95 | free(rpc->error_string); | |
96 | } | |
97 | va_start(ap, error_string); | |
b85c7de2 RS |
98 | rpc->error_string = malloc(1024); |
99 | vsnprintf(rpc->error_string, 1024, error_string, ap); | |
84004dbf RS |
100 | va_end(ap); |
101 | } | |
102 | ||
103 | char *rpc_get_error(struct rpc_context *rpc) | |
104 | { | |
105 | return rpc->error_string; | |
106 | } | |
107 | ||
108 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error) | |
109 | { | |
110 | struct rpc_pdu *pdu; | |
111 | ||
112 | while((pdu = rpc->outqueue) != NULL) { | |
113 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
114 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
115 | rpc_free_pdu(rpc, pdu); | |
116 | } | |
117 | while((pdu = rpc->waitpdu) != NULL) { | |
118 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
119 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
120 | rpc_free_pdu(rpc, pdu); | |
121 | } | |
122 | } | |
123 | ||
124 | ||
125 | void rpc_destroy_context(struct rpc_context *rpc) | |
126 | { | |
127 | struct rpc_pdu *pdu; | |
128 | ||
129 | while((pdu = rpc->outqueue) != NULL) { | |
130 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, 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_CANCEL, NULL, pdu->private_data); | |
136 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
137 | rpc_free_pdu(rpc, pdu); | |
138 | } | |
139 | ||
140 | auth_destroy(rpc->auth); | |
141 | rpc->auth =NULL; | |
142 | ||
143 | if (rpc->fd != -1) { | |
eecdc4f3 RS |
144 | #if defined(WIN32) |
145 | closesocket(rpc->fd); | |
146 | #else | |
147 | close(rpc->fd); | |
148 | #endif | |
84004dbf RS |
149 | } |
150 | ||
151 | if (rpc->encodebuf != NULL) { | |
152 | free(rpc->encodebuf); | |
153 | rpc->encodebuf = NULL; | |
154 | } | |
155 | ||
156 | if (rpc->error_string != NULL) { | |
157 | free(rpc->error_string); | |
158 | rpc->error_string = NULL; | |
159 | } | |
160 | ||
7ff2f3a0 RS |
161 | if (rpc->udp_dest != NULL) { |
162 | free(rpc->udp_dest); | |
163 | rpc->udp_dest = NULL; | |
164 | } | |
165 | ||
84004dbf RS |
166 | free(rpc); |
167 | } | |
168 | ||
169 |