uclient.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <netinet/in.h>
  21. #include <libubox/blob.h>
  22. #include <libubox/ustream.h>
  23. #include <libubox/ustream-ssl.h>
  24. #define UCLIENT_DEFAULT_TIMEOUT_MS 30000
  25. struct uclient_cb;
  26. struct uclient_backend;
  27. enum uclient_error_code {
  28. UCLIENT_ERROR_UNKNOWN,
  29. UCLIENT_ERROR_CONNECT,
  30. UCLIENT_ERROR_TIMEDOUT,
  31. UCLIENT_ERROR_SSL_INVALID_CERT,
  32. UCLIENT_ERROR_SSL_CN_MISMATCH,
  33. UCLIENT_ERROR_MISSING_SSL_CONTEXT,
  34. __UCLIENT_ERROR_MAX
  35. };
  36. union uclient_addr {
  37. struct sockaddr sa;
  38. struct sockaddr_in sin;
  39. struct sockaddr_in6 sin6;
  40. };
  41. struct uclient_url {
  42. const struct uclient_backend *backend;
  43. int prefix;
  44. const char *host;
  45. const char *port;
  46. const char *location;
  47. const char *auth;
  48. };
  49. struct uclient {
  50. const struct uclient_backend *backend;
  51. const struct uclient_cb *cb;
  52. union uclient_addr local_addr, remote_addr;
  53. struct uclient_url *proxy_url;
  54. struct uclient_url *url;
  55. int timeout_msecs;
  56. void *priv;
  57. bool eof;
  58. bool data_eof;
  59. int error_code;
  60. int status_code;
  61. int seq;
  62. struct blob_attr *meta;
  63. struct uloop_timeout connection_timeout;
  64. struct uloop_timeout timeout;
  65. };
  66. struct uclient_cb {
  67. void (*data_read)(struct uclient *cl);
  68. void (*data_sent)(struct uclient *cl);
  69. void (*data_eof)(struct uclient *cl);
  70. void (*header_done)(struct uclient *cl);
  71. void (*error)(struct uclient *cl, int code);
  72. };
  73. struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
  74. void uclient_free(struct uclient *cl);
  75. int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
  76. int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str);
  77. /**
  78. * Sets connection timeout.
  79. *
  80. * Provided timeout value will be used for:
  81. * 1) Receiving HTTP response
  82. * 2) Receiving data
  83. *
  84. * In case of timeout uclient will use error callback with
  85. * UCLIENT_ERROR_TIMEDOUT code.
  86. *
  87. * @param msecs timeout in milliseconds
  88. */
  89. int uclient_set_timeout(struct uclient *cl, int msecs);
  90. int uclient_connect(struct uclient *cl);
  91. void uclient_disconnect(struct uclient *cl);
  92. int uclient_read(struct uclient *cl, char *buf, int len);
  93. int uclient_write(struct uclient *cl, const char *buf, int len);
  94. int uclient_request(struct uclient *cl);
  95. char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
  96. /* HTTP */
  97. extern const struct uclient_backend uclient_backend_http;
  98. int uclient_http_reset_headers(struct uclient *cl);
  99. int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
  100. int uclient_http_set_request_type(struct uclient *cl, const char *type);
  101. int uclient_http_redirect(struct uclient *cl);
  102. int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
  103. struct ustream_ssl_ctx *ctx, bool require_validation);
  104. int uclient_http_set_address_family(struct uclient *cl, int af);
  105. const char *uclient_strerror(unsigned err);
  106. #endif