+
+#ifndef WOLFSSL_SSL_H
+static long
+debug_cb(BIO *bio, int cmd, const char *argp, size_t len, int argi, long argl,
+ int ret, size_t *processed)
+{
+ struct ustream_ssl_ctx *ctx = (void *)BIO_get_callback_arg(bio);
+ char buf[256];
+ char *str, *sep;
+ ssize_t cur_len;
+
+ if (cmd != (BIO_CB_WRITE|BIO_CB_RETURN))
+ goto out;
+
+ while (1) {
+ cur_len = BIO_get_mem_data(bio, (void *)&str);
+ if (!cur_len)
+ break;
+
+ sep = memchr(str, '\n', cur_len);
+ if (!sep)
+ break;
+
+ cur_len = sep + 1 - str;
+ if (cur_len >= (ssize_t)sizeof(buf))
+ cur_len = sizeof(buf) - 1;
+
+ cur_len = BIO_read(bio, buf, cur_len);
+ if (cur_len <= 1)
+ break;
+
+ cur_len--;
+ buf[cur_len] = 0;
+ if (ctx->debug_cb)
+ ctx->debug_cb(ctx->debug_cb_priv, 1, buf);
+ }
+
+out:
+ return ret;
+}
+#endif
+
+__hidden void __ustream_ssl_set_debug(struct ustream_ssl_ctx *ctx, int level,
+ ustream_ssl_debug_cb cb, void *cb_priv)
+{
+#ifndef WOLFSSL_SSL_H
+ if (!ctx->debug_bio)
+ ctx->debug_bio = BIO_new(BIO_s_mem());
+
+ ctx->debug_cb = cb;
+ ctx->debug_cb_priv = cb_priv;
+ SSL_CTX_set_msg_callback(ctx->ssl, SSL_trace);
+ SSL_CTX_set_msg_callback_arg(ctx->ssl, ctx->debug_bio);
+
+ BIO_set_callback_ex(ctx->debug_bio, debug_cb);
+ BIO_set_callback_arg(ctx->debug_bio, (void *)ctx);
+#endif
+}