Fix unused results warnings
[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 static struct uclient_url *
50 __uclient_get_url(const struct uclient_backend *backend,
51                   const char *host, int host_len,
52                   const char *location, const char *auth_str)
53 {
54         struct uclient_url *url;
55         char *host_buf, *uri_buf, *auth_buf, *next;
56
57         url = calloc_a(sizeof(*url),
58                 &host_buf, host_len + 1,
59                 &uri_buf, strlen(location) + 1,
60                 &auth_buf, auth_str ? strlen(auth_str) + 1 : 0);
61
62         url->backend = backend;
63         url->location = strcpy(uri_buf, location);
64         if (host)
65                 url->host = strncpy(host_buf, host, host_len);
66
67         next = strchr(host_buf, '@');
68         if (next) {
69                 *next = 0;
70                 url->host = next + 1;
71
72                 if (uclient_urldecode(host_buf, host_buf, false) < 0)
73                         goto free;
74
75                 url->auth = host_buf;
76         }
77
78         if (!url->auth && auth_str)
79                 url->auth = strcpy(auth_buf, auth_str);
80
81         /* Literal IPv6 address */
82         if (*url->host == '[') {
83                 url->host++;
84                 next = strrchr(url->host, ']');
85                 if (!next)
86                         goto free;
87
88                 *(next++) = 0;
89                 if (*next == ':')
90                         url->port = next + 1;
91         } else {
92                 next = strrchr(url->host, ':');
93                 if (next) {
94                         *next = 0;
95                         url->port = next + 1;
96                 }
97         }
98
99         return url;
100
101 free:
102         free(url);
103         return NULL;
104 }
105
106 static const char *
107 uclient_split_host(const char *base, int *host_len)
108 {
109         char *next, *location;
110
111         next = strchr(base, '/');
112         if (next) {
113                 location = next;
114                 *host_len = next - base;
115         } else {
116                 location = "/";
117                 *host_len = strlen(base);
118         }
119
120         return location;
121 }
122
123 struct uclient_url __hidden *
124 uclient_get_url_location(struct uclient_url *url, const char *location)
125 {
126         struct uclient_url *new_url;
127         char *host_buf, *uri_buf, *auth_buf, *port_buf;
128         int host_len = strlen(url->host) + 1;
129         int auth_len = url->auth ? strlen(url->auth) + 1 : 0;
130         int port_len = url->port ? strlen(url->port) + 1 : 0;
131         int uri_len;
132
133         if (strstr(location, "://"))
134                 return uclient_get_url(location, url->auth);
135
136         if (location[0] == '/')
137                 uri_len = strlen(location) + 1;
138         else
139                 uri_len = strlen(url->location) + strlen(location) + 2;
140
141         new_url = calloc_a(sizeof(*url),
142                 &host_buf, host_len,
143                 &port_buf, port_len,
144                 &uri_buf, uri_len,
145                 &auth_buf, auth_len);
146
147         if (!new_url)
148                 return NULL;
149
150         new_url->backend = url->backend;
151         new_url->prefix = url->prefix;
152         new_url->host = strcpy(host_buf, url->host);
153         if (url->port)
154                 new_url->port = strcpy(port_buf, url->port);
155         if (url->auth)
156                 new_url->auth = strcpy(auth_buf, url->auth);
157
158         new_url->location = uri_buf;
159         if (location[0] == '/')
160                 strcpy(uri_buf, location);
161         else {
162                 int len = strcspn(url->location, "?#");
163                 char *buf = uri_buf;
164
165                 memcpy(buf, url->location, len);
166                 if (buf[len - 1] != '/') {
167                         buf[len] = '/';
168                         len++;
169                 }
170
171                 buf += len;
172                 strcpy(buf, location);
173         }
174
175         return new_url;
176 }
177
178 struct uclient_url __hidden *
179 uclient_get_url(const char *url_str, const char *auth_str)
180 {
181         static const struct uclient_backend *backends[] = {
182                 &uclient_backend_http,
183         };
184
185         const struct uclient_backend *backend;
186         const char * const *prefix = NULL;
187         struct uclient_url *url;
188         const char *location;
189         int host_len;
190         int i;
191
192         for (i = 0; i < ARRAY_SIZE(backends); i++) {
193                 int prefix_len = 0;
194
195                 for (prefix = backends[i]->prefix; *prefix; prefix++) {
196                         prefix_len = strlen(*prefix);
197
198                         if (!strncmp(url_str, *prefix, prefix_len))
199                                 break;
200                 }
201
202                 if (!*prefix)
203                         continue;
204
205                 url_str += prefix_len;
206                 backend = backends[i];
207                 break;
208         }
209
210         if (!*prefix)
211                 return NULL;
212
213         location = uclient_split_host(url_str, &host_len);
214         url = __uclient_get_url(backend, url_str, host_len, location, auth_str);
215         if (!url)
216                 return NULL;
217
218         url->prefix = prefix - backend->prefix;
219         return url;
220 }
221
222 static void uclient_connection_timeout(struct uloop_timeout *timeout)
223 {
224         struct uclient *cl = container_of(timeout, struct uclient, connection_timeout);
225
226         if (cl->backend->disconnect)
227                 cl->backend->disconnect(cl);
228
229         uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT);
230 }
231
232 struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb)
233 {
234         struct uclient *cl;
235         struct uclient_url *url;
236
237         url = uclient_get_url(url_str, auth_str);
238         if (!url)
239                 return NULL;
240
241         cl = url->backend->alloc();
242         if (!cl)
243                 return NULL;
244
245         cl->backend = url->backend;
246         cl->cb = cb;
247         cl->url = url;
248         cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS;
249         cl->connection_timeout.cb = uclient_connection_timeout;
250
251         return cl;
252 }
253
254 int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str)
255 {
256         const struct uclient_backend *backend = cl->backend;
257         struct uclient_url *url;
258         int host_len;
259         char *next, *host;
260
261         if (!backend->update_proxy_url)
262                 return -1;
263
264         next = strstr(url_str, "://");
265         if (!next)
266                 return -1;
267
268         host = next + 3;
269         uclient_split_host(host, &host_len);
270
271         url = __uclient_get_url(NULL, host, host_len, url_str, auth_str);
272         if (!url)
273                 return -1;
274
275         free(cl->proxy_url);
276         cl->proxy_url = url;
277
278         if (backend->update_proxy_url)
279                 backend->update_proxy_url(cl);
280
281         return 0;
282 }
283
284 int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_str)
285 {
286         const struct uclient_backend *backend = cl->backend;
287         struct uclient_url *url = cl->url;
288
289         url = uclient_get_url(url_str, auth_str);
290         if (!url)
291                 return -1;
292
293         if (url->backend != cl->backend) {
294                 free(url);
295                 return -1;
296         }
297
298         free(cl->proxy_url);
299         cl->proxy_url = NULL;
300
301         free(cl->url);
302         cl->url = url;
303
304         if (backend->update_url)
305                 backend->update_url(cl);
306
307         return 0;
308 }
309
310 int uclient_set_timeout(struct uclient *cl, int msecs)
311 {
312         if (msecs <= 0)
313                 return -EINVAL;
314
315         cl->timeout_msecs = msecs;
316
317         return 0;
318 }
319
320 int uclient_connect(struct uclient *cl)
321 {
322         return cl->backend->connect(cl);
323 }
324
325 void uclient_free(struct uclient *cl)
326 {
327         struct uclient_url *url = cl->url;
328
329         if (cl->backend->free)
330                 cl->backend->free(cl);
331         else
332                 free(cl);
333
334         free(url);
335 }
336
337 int uclient_write(struct uclient *cl, const char *buf, int len)
338 {
339         if (!cl->backend->write)
340                 return -1;
341
342         return cl->backend->write(cl, buf, len);
343 }
344
345 int uclient_request(struct uclient *cl)
346 {
347         int err;
348
349         if (!cl->backend->request)
350                 return -1;
351
352         err = cl->backend->request(cl);
353         if (err)
354                 return err;
355
356         uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
357
358         return 0;
359 }
360
361 int uclient_read(struct uclient *cl, char *buf, int len)
362 {
363         if (!cl->backend->read)
364                 return -1;
365
366         return cl->backend->read(cl, buf, len);
367 }
368
369 void uclient_disconnect(struct uclient *cl)
370 {
371         uloop_timeout_cancel(&cl->connection_timeout);
372
373         if (!cl->backend->disconnect)
374                 return;
375
376         cl->backend->disconnect(cl);
377 }
378
379 static void __uclient_backend_change_state(struct uloop_timeout *timeout)
380 {
381         struct uclient *cl = container_of(timeout, struct uclient, timeout);
382
383         if (cl->error_code && cl->cb->error)
384                 cl->cb->error(cl, cl->error_code);
385         else if (cl->eof && cl->cb->data_eof)
386                 cl->cb->data_eof(cl);
387 }
388
389 static void uclient_backend_change_state(struct uclient *cl)
390 {
391         cl->timeout.cb = __uclient_backend_change_state;
392         uloop_timeout_set(&cl->timeout, 1);
393 }
394
395 void __hidden uclient_backend_set_error(struct uclient *cl, int code)
396 {
397         if (cl->error_code)
398                 return;
399
400         uloop_timeout_cancel(&cl->connection_timeout);
401         cl->error_code = code;
402         uclient_backend_change_state(cl);
403 }
404
405 void __hidden uclient_backend_set_eof(struct uclient *cl)
406 {
407         if (cl->eof || cl->error_code)
408                 return;
409
410         uloop_timeout_cancel(&cl->connection_timeout);
411         cl->eof = true;
412         uclient_backend_change_state(cl);
413 }
414
415 void __hidden uclient_backend_reset_state(struct uclient *cl)
416 {
417         cl->data_eof = false;
418         cl->eof = false;
419         cl->error_code = 0;
420         uloop_timeout_cancel(&cl->timeout);
421 }