core: fix memory leak if url change fails
[project/uclient.git] / uclient.c
1 /*
2  * uclient - ustream based protocol client library
3  *
4  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include <arpa/inet.h>
19 #include <libubox/ustream-ssl.h>
20 #include "uclient.h"
21 #include "uclient-utils.h"
22 #include "uclient-backend.h"
23
24 char *uclient_get_addr(char *dest, int *port, union uclient_addr *a)
25 {
26         int portval;
27         void *ptr;
28
29         switch(a->sa.sa_family) {
30         case AF_INET:
31                 ptr = &a->sin.sin_addr;
32                 portval = a->sin.sin_port;
33                 break;
34         case AF_INET6:
35                 ptr = &a->sin6.sin6_addr;
36                 portval = a->sin6.sin6_port;
37                 break;
38         default:
39                 return strcpy(dest, "Unknown");
40         }
41
42         inet_ntop(a->sa.sa_family, ptr, dest, INET6_ADDRSTRLEN);
43         if (port)
44                 *port = ntohs(portval);
45
46         return dest;
47 }
48
49
50 struct uclient_url __hidden *
51 uclient_get_url(const char *url_str, const char *auth_str)
52 {
53         static const struct uclient_backend *backends[] = {
54                 &uclient_backend_http,
55         };
56
57         const struct uclient_backend *backend;
58         const char * const *prefix = NULL;
59         struct uclient_url *url;
60         const char *location;
61         char *host_buf, *uri_buf, *auth_buf, *next;
62         int i, host_len;
63
64         for (i = 0; i < ARRAY_SIZE(backends); i++) {
65                 int prefix_len = 0;
66
67                 for (prefix = backends[i]->prefix; *prefix; prefix++) {
68                         prefix_len = strlen(*prefix);
69
70                         if (!strncmp(url_str, *prefix, prefix_len))
71                                 break;
72                 }
73
74                 if (!*prefix)
75                         continue;
76
77                 url_str += prefix_len;
78                 backend = backends[i];
79                 break;
80         }
81
82         if (!*prefix)
83                 return NULL;
84
85         next = strchr(url_str, '/');
86         if (next) {
87                 location = next;
88                 host_len = next - url_str;
89         } else {
90                 location = "/";
91                 host_len = strlen(url_str);
92         }
93
94         url = calloc_a(sizeof(*url),
95                 &host_buf, host_len + 1,
96                 &uri_buf, strlen(location) + 1,
97                 &auth_buf, auth_str ? strlen(auth_str) + 1 : 0);
98
99         url->backend = backend;
100         url->location = strcpy(uri_buf, location);
101         url->prefix = prefix - backend->prefix;
102
103         url->host = strncpy(host_buf, url_str, host_len);
104
105         next = strchr(host_buf, '@');
106         if (next) {
107                 *next = 0;
108                 url->host = next + 1;
109
110                 if (uclient_urldecode(host_buf, host_buf, false) < 0)
111                         goto free;
112
113                 url->auth = host_buf;
114         }
115
116         if (!url->auth && auth_str)
117                 url->auth = strcpy(auth_buf, auth_str);
118
119         /* Literal IPv6 address */
120         if (*url->host == '[') {
121                 url->host++;
122                 next = strrchr(url->host, ']');
123                 if (!next)
124                         goto free;
125
126                 *(next++) = 0;
127                 if (*next == ':')
128                         url->port = next + 1;
129         } else {
130                 next = strrchr(url->host, ':');
131                 if (next) {
132                         *next = 0;
133                         url->port = next + 1;
134                 }
135         }
136
137         return url;
138
139 free:
140         free(url);
141         return NULL;
142 }
143
144 static void uclient_connection_timeout(struct uloop_timeout *timeout)
145 {
146         struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
147
148         if (cl->backend->disconnect)
149                 cl->backend->disconnect(cl);
150
151         uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
152 }
153
154 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
155 {
156         struct uclient *cl;
157         struct uclient_url *url;
158
159         url = uclient_get_url(url_str, auth_str);
160         if (!url)
161                 return NULL;
162
163         cl = url->backend->alloc();
164         if (!cl)
165                 return NULL;
166
167         cl->backend = url->backend;
168         cl->cb = cb;
169         cl->url = url;
170         cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
171         cl->connection_timeout.cb = uclient_connection_timeout;
172
173         return cl;
174 }
175
176 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
177 {
178         const struct uclient_backend *backend = cl->backend;
179         struct uclient_url *url = cl->url;
180
181         url = uclient_get_url(url_str, auth_str);
182         if (!url)
183                 return -1;
184
185         if (url->backend != cl->backend) {
186                 free(url);
187                 return -1;
188         }
189
190         free(cl->url);
191         cl->url = url;
192
193         if (backend->update_url)
194                 backend->update_url(cl);
195
196         return 0;
197 }
198
199 int uclient_set_timeout(struct uclient *cl, int msecs)
200 {
201         if (msecs <= 0)
202                 return -EINVAL;
203
204         cl->timeout_msecs = msecs;
205
206         return 0;
207 }
208
209 int uclient_connect(struct uclient *cl)
210 {
211         return cl->backend->connect(cl);
212 }
213
214 void uclient_free(struct uclient *cl)
215 {
216         struct uclient_url *url = cl->url;
217
218         if (cl->backend->free)
219                 cl->backend->free(cl);
220         else
221                 free(cl);
222
223         free(url);
224 }
225
226 int uclient_write(struct uclient *cl, const char *buf, int len)
227 {
228         if (!cl->backend->write)
229                 return -1;
230
231         return cl->backend->write(cl, buf, len);
232 }
233
234 int uclient_request(struct uclient *cl)
235 {
236         int err;
237
238         if (!cl->backend->request)
239                 return -1;
240
241         err = cl->backend->request(cl);
242         if (err)
243                 return err;
244
245         uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
246
247         return 0;
248 }
249
250 int uclient_read(struct uclient *cl, char *buf, int len)
251 {
252         if (!cl->backend->read)
253                 return -1;
254
255         return cl->backend->read(cl, buf, len);
256 }
257
258 void uclient_disconnect(struct uclient *cl)
259 {
260         uloop_timeout_cancel(&cl->connection_timeout);
261
262         if (!cl->backend->disconnect)
263                 return;
264
265         cl->backend->disconnect(cl);
266 }
267
268 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
269 {
270         struct uclient *cl = container_of(timeout, struct uclient, timeout);
271
272         if (cl->error_code && cl->cb->error)
273                 cl->cb->error(cl, cl->error_code);
274         else if (cl->eof && cl->cb->data_eof)
275                 cl->cb->data_eof(cl);
276 }
277
278 static void uclient_backend_change_state(struct uclient *cl)
279 {
280         cl->timeout.cb = __uclient_backend_change_state;
281         uloop_timeout_set(&cl->timeout, 1);
282 }
283
284 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
285 {
286         if (cl->error_code)
287                 return;
288
289         uloop_timeout_cancel(&cl->connection_timeout);
290         cl->error_code = code;
291         uclient_backend_change_state(cl);
292 }
293
294 void __hidden uclient_backend_set_eof(struct uclient *cl)
295 {
296         if (cl->eof || cl->error_code)
297                 return;
298
299         uloop_timeout_cancel(&cl->connection_timeout);
300         cl->eof = true;
301         uclient_backend_change_state(cl);
302 }
303
304 void __hidden uclient_backend_reset_state(struct uclient *cl)
305 {
306         cl->data_eof = false;
307         cl->eof = false;
308         cl->error_code = 0;
309         uloop_timeout_cancel(&cl->timeout);
310 }