uclient.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. enum uclient_log_type {
  37. UCLIENT_LOG_SSL_ERROR,
  38. UCLIENT_LOG_SSL_VERIFY_ERROR,
  39. __UCLIENT_LOG_MAX
  40. };
  41. union uclient_addr {
  42. struct sockaddr sa;
  43. struct sockaddr_in sin;
  44. struct sockaddr_in6 sin6;
  45. };
  46. struct uclient_url {
  47. const struct uclient_backend *backend;
  48. int prefix;
  49. const char *host;
  50. const char *port;
  51. const char *location;
  52. const char *auth;
  53. };
  54. struct uclient {
  55. const struct uclient_backend *backend;
  56. const struct uclient_cb *cb;
  57. union uclient_addr local_addr, remote_addr;
  58. struct uclient_url *proxy_url;
  59. struct uclient_url *url;
  60. int timeout_msecs;
  61. void *priv;
  62. bool eof;
  63. bool data_eof;
  64. int error_code;
  65. int status_code;
  66. int seq;
  67. struct blob_attr *meta;
  68. struct uloop_timeout connection_timeout;
  69. struct uloop_timeout read_notify;
  70. struct uloop_timeout timeout;
  71. };
  72. struct uclient_cb {
  73. void (*data_read)(struct uclient *cl);
  74. void (*data_sent)(struct uclient *cl);
  75. void (*data_eof)(struct uclient *cl);
  76. void (*header_done)(struct uclient *cl);
  77. void (*error)(struct uclient *cl, int code);
  78. void (*log_msg)(struct uclient *cl, enum uclient_log_type type, const char *msg);
  79. };
  80. struct uclient *uclient_new(const char *url, const char *auth_str, const struct uclient_cb *cb);
  81. void uclient_free(struct uclient *cl);
  82. int uclient_set_url(struct uclient *cl, const char *url, const char *auth);
  83. int uclient_set_proxy_url(struct uclient *cl, const char *url_str, const char *auth_str);
  84. /**
  85. * Sets connection timeout.
  86. *
  87. * Provided timeout value will be used for:
  88. * 1) Receiving HTTP response
  89. * 2) Receiving data
  90. *
  91. * In case of timeout uclient will use error callback with
  92. * UCLIENT_ERROR_TIMEDOUT code.
  93. *
  94. * @param msecs timeout in milliseconds
  95. */
  96. int uclient_set_timeout(struct uclient *cl, int msecs);
  97. int uclient_connect(struct uclient *cl);
  98. void uclient_disconnect(struct uclient *cl);
  99. int uclient_read(struct uclient *cl, char *buf, int len);
  100. int uclient_write(struct uclient *cl, const char *buf, int len);
  101. int uclient_pending_bytes(struct uclient *cl, bool write);
  102. int uclient_request(struct uclient *cl);
  103. char *uclient_get_addr(char *dest, int *port, union uclient_addr *a);
  104. struct ustream_ssl_ctx *uclient_new_ssl_context(const struct ustream_ssl_ops **ops);
  105. /* HTTP */
  106. extern const struct uclient_backend uclient_backend_http;
  107. int uclient_http_reset_headers(struct uclient *cl);
  108. int uclient_http_set_header(struct uclient *cl, const char *name, const char *value);
  109. int uclient_http_set_request_type(struct uclient *cl, const char *type);
  110. int uclient_http_redirect(struct uclient *cl);
  111. static inline bool uclient_http_status_redirect(struct uclient *cl)
  112. {
  113. switch (cl->status_code) {
  114. case 301:
  115. case 302:
  116. case 307:
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }
  122. int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
  123. struct ustream_ssl_ctx *ctx, bool require_validation);
  124. int uclient_http_set_address_family(struct uclient *cl, int af);
  125. const char *uclient_strerror(unsigned err);
  126. #endif