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 #ifndef __LIBUBOX_UCLIENT_H
19 #define __LIBUBOX_UCLIENT_H
21 #include <netinet/in.h>
23 #include <libubox/blob.h>
24 #include <libubox/ustream.h>
25 #include <libubox/ustream-ssl.h>
27 #define UCLIENT_DEFAULT_TIMEOUT_MS 30000
30 struct uclient_backend
;
32 enum uclient_error_code
{
33 UCLIENT_ERROR_UNKNOWN
,
34 UCLIENT_ERROR_CONNECT
,
35 UCLIENT_ERROR_TIMEDOUT
,
36 UCLIENT_ERROR_SSL_INVALID_CERT
,
37 UCLIENT_ERROR_SSL_CN_MISMATCH
,
38 UCLIENT_ERROR_MISSING_SSL_CONTEXT
,
43 struct sockaddr_in sin
;
44 struct sockaddr_in6 sin6
;
48 const struct uclient_backend
*backend
;
59 const struct uclient_backend
*backend
;
60 const struct uclient_cb
*cb
;
62 union uclient_addr local_addr
, remote_addr
;
64 struct uclient_url
*proxy_url
;
65 struct uclient_url
*url
;
74 struct blob_attr
*meta
;
76 struct uloop_timeout connection_timeout
;
77 struct uloop_timeout timeout
;
81 void (*data_read
)(struct uclient
*cl
);
82 void (*data_sent
)(struct uclient
*cl
);
83 void (*data_eof
)(struct uclient
*cl
);
84 void (*header_done
)(struct uclient
*cl
);
85 void (*error
)(struct uclient
*cl
, int code
);
88 struct uclient
*uclient_new(const char *url
, const char *auth_str
, const struct uclient_cb
*cb
);
89 void uclient_free(struct uclient
*cl
);
91 int uclient_set_url(struct uclient
*cl
, const char *url
, const char *auth
);
92 int uclient_set_proxy_url(struct uclient
*cl
, const char *url_str
, const char *auth_str
);
96 * Sets connection timeout.
98 * Provided timeout value will be used for:
99 * 1) Receiving HTTP response
102 * In case of timeout uclient will use error callback with
103 * UCLIENT_ERROR_TIMEDOUT code.
105 * @param msecs timeout in milliseconds
107 int uclient_set_timeout(struct uclient
*cl
, int msecs
);
109 int uclient_connect(struct uclient
*cl
);
110 void uclient_disconnect(struct uclient
*cl
);
112 int uclient_read(struct uclient
*cl
, char *buf
, int len
);
113 int uclient_write(struct uclient
*cl
, const char *buf
, int len
);
114 int uclient_request(struct uclient
*cl
);
116 char *uclient_get_addr(char *dest
, int *port
, union uclient_addr
*a
);
119 extern const struct uclient_backend uclient_backend_http
;
121 int uclient_http_reset_headers(struct uclient
*cl
);
122 int uclient_http_set_header(struct uclient
*cl
, const char *name
, const char *value
);
123 int uclient_http_set_request_type(struct uclient
*cl
, const char *type
);
124 bool uclient_http_redirect(struct uclient
*cl
);
126 int uclient_http_set_ssl_ctx(struct uclient
*cl
, const struct ustream_ssl_ops
*ops
,
127 struct ustream_ssl_ctx
*ctx
, bool require_validation
);