2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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.
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.
18 #include <arpa/inet.h>
19 #include <libubox/ustream-ssl.h>
21 #include "uclient-utils.h"
22 #include "uclient-backend.h"
24 char *uclient_get_addr(char *dest
, int *port
, union uclient_addr
*a
)
29 switch(a
->sa
.sa_family
) {
31 ptr
= &a
->sin
.sin_addr
;
32 portval
= a
->sin
.sin_port
;
35 ptr
= &a
->sin6
.sin6_addr
;
36 portval
= a
->sin6
.sin6_port
;
39 return strcpy(dest
, "Unknown");
42 inet_ntop(a
->sa
.sa_family
, ptr
, dest
, INET6_ADDRSTRLEN
);
44 *port
= ntohs(portval
);
50 struct uclient_url __hidden
*
51 uclient_get_url(const char *url_str
, const char *auth_str
)
53 static const struct uclient_backend
*backends
[] = {
54 &uclient_backend_http
,
57 const struct uclient_backend
*backend
;
58 const char * const *prefix
= NULL
;
59 struct uclient_url
*url
;
61 char *host_buf
, *uri_buf
, *auth_buf
, *next
;
64 for (i
= 0; i
< ARRAY_SIZE(backends
); i
++) {
67 for (prefix
= backends
[i
]->prefix
; *prefix
; prefix
++) {
68 prefix_len
= strlen(*prefix
);
70 if (!strncmp(url_str
, *prefix
, prefix_len
))
77 url_str
+= prefix_len
;
78 backend
= backends
[i
];
85 next
= strchr(url_str
, '/');
88 host_len
= next
- url_str
;
91 host_len
= strlen(url_str
);
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);
99 url
->backend
= backend
;
100 url
->location
= strcpy(uri_buf
, location
);
101 url
->prefix
= prefix
- backend
->prefix
;
103 url
->host
= strncpy(host_buf
, url_str
, host_len
);
105 next
= strchr(host_buf
, '@');
108 url
->host
= next
+ 1;
110 if (uclient_urldecode(host_buf
, host_buf
, false) < 0)
113 url
->auth
= host_buf
;
116 if (!url
->auth
&& auth_str
)
117 url
->auth
= strcpy(auth_buf
, auth_str
);
119 /* Literal IPv6 address */
120 if (*url
->host
== '[') {
122 next
= strrchr(url
->host
, ']');
128 url
->port
= next
+ 1;
130 next
= strrchr(url
->host
, ':');
133 url
->port
= next
+ 1;
144 static void uclient_connection_timeout(struct uloop_timeout
*timeout
)
146 struct uclient
*cl
= container_of(timeout
, struct uclient
, connection_timeout
);
148 if (cl
->backend
->disconnect
)
149 cl
->backend
->disconnect(cl
);
151 uclient_backend_set_error(cl
, UCLIENT_ERROR_TIMEDOUT
);
154 struct uclient
*uclient_new(const char *url_str
, const char *auth_str
, const struct uclient_cb
*cb
)
157 struct uclient_url
*url
;
159 url
= uclient_get_url(url_str
, auth_str
);
163 cl
= url
->backend
->alloc();
167 cl
->backend
= url
->backend
;
170 cl
->timeout_msecs
= UCLIENT_DEFAULT_TIMEOUT_MS
;
171 cl
->connection_timeout
.cb
= uclient_connection_timeout
;
176 int uclient_set_url(struct uclient
*cl
, const char *url_str
, const char *auth_str
)
178 const struct uclient_backend
*backend
= cl
->backend
;
179 struct uclient_url
*url
= cl
->url
;
181 url
= uclient_get_url(url_str
, auth_str
);
185 if (url
->backend
!= cl
->backend
)
191 if (backend
->update_url
)
192 backend
->update_url(cl
);
197 int uclient_set_timeout(struct uclient
*cl
, int msecs
)
202 cl
->timeout_msecs
= msecs
;
207 int uclient_connect(struct uclient
*cl
)
209 return cl
->backend
->connect(cl
);
212 void uclient_free(struct uclient
*cl
)
214 struct uclient_url
*url
= cl
->url
;
216 if (cl
->backend
->free
)
217 cl
->backend
->free(cl
);
224 int uclient_write(struct uclient
*cl
, const char *buf
, int len
)
226 if (!cl
->backend
->write
)
229 return cl
->backend
->write(cl
, buf
, len
);
232 int uclient_request(struct uclient
*cl
)
236 if (!cl
->backend
->request
)
239 err
= cl
->backend
->request(cl
);
243 uloop_timeout_set(&cl
->connection_timeout
, cl
->timeout_msecs
);
248 int uclient_read(struct uclient
*cl
, char *buf
, int len
)
250 if (!cl
->backend
->read
)
253 return cl
->backend
->read(cl
, buf
, len
);
256 void uclient_disconnect(struct uclient
*cl
)
258 uloop_timeout_cancel(&cl
->connection_timeout
);
260 if (!cl
->backend
->disconnect
)
263 cl
->backend
->disconnect(cl
);
266 static void __uclient_backend_change_state(struct uloop_timeout
*timeout
)
268 struct uclient
*cl
= container_of(timeout
, struct uclient
, timeout
);
270 if (cl
->error_code
&& cl
->cb
->error
)
271 cl
->cb
->error(cl
, cl
->error_code
);
272 else if (cl
->eof
&& cl
->cb
->data_eof
)
273 cl
->cb
->data_eof(cl
);
276 static void uclient_backend_change_state(struct uclient
*cl
)
278 cl
->timeout
.cb
= __uclient_backend_change_state
;
279 uloop_timeout_set(&cl
->timeout
, 1);
282 void __hidden
uclient_backend_set_error(struct uclient
*cl
, int code
)
287 uloop_timeout_cancel(&cl
->connection_timeout
);
288 cl
->error_code
= code
;
289 uclient_backend_change_state(cl
);
292 void __hidden
uclient_backend_set_eof(struct uclient
*cl
)
294 if (cl
->eof
|| cl
->error_code
)
297 uloop_timeout_cancel(&cl
->connection_timeout
);
299 uclient_backend_change_state(cl
);
302 void __hidden
uclient_backend_reset_state(struct uclient
*cl
)
304 cl
->data_eof
= false;
307 uloop_timeout_cancel(&cl
->timeout
);