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