http: make ustream_ssl optional, only use provided ssl context
[project/uclient.git] / uclient.h
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 #ifndef __LIBUBOX_UCLIENT_H
19 #define __LIBUBOX_UCLIENT_H
20
21 #include <netinet/in.h>
22
23 #include <libubox/blob.h>
24 #include <libubox/ustream.h>
25 #include <libubox/ustream-ssl.h>
26
27 struct uclient_cb;
28 struct uclient_backend;
29
30 enum uclient_error_code {
31 UCLIENT_ERROR_UNKNOWN,
32 UCLIENT_ERROR_CONNECT,
33 UCLIENT_ERROR_SSL_INVALID_CERT,
34 UCLIENT_ERROR_SSL_CN_MISMATCH,
35 UCLIENT_ERROR_MISSING_SSL_CONTEXT,
36 };
37
38 union uclient_addr {
39 struct sockaddr sa;
40 struct sockaddr_in sin;
41 struct sockaddr_in6 sin6;
42 };
43
44 struct uclient {
45 const struct uclient_backend *backend;
46 const struct uclient_cb *cb;
47
48 union uclient_addr local_addr, remote_addr;
49
50 struct uclient_url *url;
51 void *priv;
52
53 bool eof;
54 int error_code;
55 int status_code;
56 struct blob_attr *meta;
57
58 struct uloop_timeout timeout;
59 };
60
61 struct uclient_cb {
62 void (*data_read)(struct uclient *cl);
63 void (*data_sent)(struct uclient *cl);
64 void (*data_eof)(struct uclient *cl);
65 void (*header_done)(struct uclient *cl);
66 void (*error)(struct uclient *cl, int code);
67 };
68
69 struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
70 void uclient_free(struct uclient *cl);
71
72 int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
73 int uclient_connect(struct uclient *cl);
74
75 int uclient_read(struct uclient *cl, char *buf, int len);
76 int uclient_write(struct uclient *cl, char *buf, int len);
77 int uclient_request(struct uclient *cl);
78
79 char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
80
81 /* HTTP */
82 extern const struct uclient_backend uclient_backend_http;
83
84 int uclient_http_reset_headers(struct uclient *cl);
85 int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
86 int uclient_http_set_request_type(struct uclient *cl, const char *type);
87 bool uclient_http_redirect(struct uclient *cl);
88
89 int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
90 struct ustream_ssl_ctx *ctx, bool require_validation);
91
92 #endif