Skip to content

Commit

Permalink
Sync deps: libcat
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Dec 7, 2023
1 parent 58d7207 commit fb15f29
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion ext/deps/libcat/include/cat_ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ typedef struct cat_ssl_s {
cat_buffer_t write_buffer;
/* options */
cat_bool_t allow_self_signed;
/* internals */
cat_ssl_context_t *context; // for free data before SSL_free()
} cat_ssl_t;

typedef enum cat_ssl_ret_e {
Expand Down Expand Up @@ -236,7 +238,7 @@ CAT_API void cat_ssl_context_set_no_compression(cat_ssl_context_t *context);
CAT_API void cat_ssl_context_set_security_level(cat_ssl_context_t *context, int level);
#endif
#ifdef CAT_SSL_HAVE_TLS_ALPN
CAT_API cat_bool_t cas_ssl_context_set_apln_protocols(cat_ssl_context_t *context, cat_bool_t is_client, const char *alpn_protocols);
CAT_API cat_bool_t cas_ssl_context_set_alpn_protocols(cat_ssl_context_t *context, cat_bool_t is_client, const char *alpn_protocols);
#endif

/* connection */
Expand Down
4 changes: 4 additions & 0 deletions ext/deps/libcat/include/cat_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ static cat_always_inline void cat_string_close(cat_string_t *string)

static cat_always_inline void cat_string_move_uncleaned(cat_string_t *from, cat_string_t *to)
{
// release possible old string first
cat_string_close(to);
to->value = from->value;
to->length = from->length;
// do not do it, let caller judge whether to do it, so it's "uncleaned"
// from->value = NULL;
// from->length = NULL;
}

CAT_API size_t cat_strnlen(const char *s, size_t n);
Expand Down
9 changes: 7 additions & 2 deletions ext/deps/libcat/src/cat_coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@
#endif

#ifdef CAT_COROUTINE_USE_ASAN
# ifdef _MSC_VER
// for warning -Wstrict-prototypes/C4255
# define __sanitizer_acquire_crash_state() __sanitizer_acquire_crash_state(void)
# define __sanitizer_get_report_path() __sanitizer_get_report_path(void)
# pragma warning(push)
# pragma warning(disable: 4255)
# endif
# if !defined(_MSC_VER) || _MSC_VER >= 1933
# include <sanitizer/common_interface_defs.h>
# else // workaround
// should be fixed in VS 2022: https://developercommunity.visualstudio.com/t/ASan-API-headers-not-in-include-path-whe/1517192
# include <../crt/src/sanitizer/common_interface_defs.h>
# endif
# ifdef _MSC_VER
# pragma warning(pop)
# endif
#endif

/* context */
Expand Down
8 changes: 6 additions & 2 deletions ext/deps/libcat/src/cat_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,9 @@ static cat_bool_t cat_socket_enable_crypto_impl(cat_socket_t *socket, const cat_
}
}
if (ioptions.certificate != NULL) {
cat_ssl_context_set_certificate(context, ioptions.certificate, ioptions.certificate_key);
if (!cat_ssl_context_set_certificate(context, ioptions.certificate, ioptions.certificate_key)) {
goto _setup_error;
}
}
if (ioptions.no_ticket) {
cat_ssl_context_set_no_ticket(context);
Expand All @@ -2206,7 +2208,9 @@ static cat_bool_t cat_socket_enable_crypto_impl(cat_socket_t *socket, const cat_
#endif
#ifdef CAT_SSL_HAVE_TLS_ALPN
if (ioptions.alpn_protocols != NULL) {
cas_ssl_context_set_apln_protocols(context, ioptions.is_client, ioptions.alpn_protocols);
if (!cas_ssl_context_set_alpn_protocols(context, ioptions.is_client, ioptions.alpn_protocols)) {
goto _setup_error;
}
}
#endif
/* create ssl connection */
Expand Down
19 changes: 15 additions & 4 deletions ext/deps/libcat/src/cat_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ CAT_API cat_ssl_context_t *cat_ssl_context_create(cat_ssl_method_t method, cat_s
return NULL;
}

CAT_API void cat_ssl_context_close(cat_ssl_context_t *context)
static cat_always_inline void cat_ssl_context_close_data(cat_ssl_context_t *context)
{
SSL_CTX_free(context->ctx);
if (CAT_REF_DEL(context) != 0) {
return;
}
Expand All @@ -241,6 +240,12 @@ CAT_API void cat_ssl_context_close(cat_ssl_context_t *context)
cat_free(context);
}

CAT_API void cat_ssl_context_close(cat_ssl_context_t *context)
{
SSL_CTX_free(context->ctx);
cat_ssl_context_close_data(context);
}

CAT_API void cat_ssl_context_set_protocols(cat_ssl_context_t *context, cat_ssl_protocols_t protocols)
{
cat_ssl_ctx_t *ctx = context->ctx;
Expand Down Expand Up @@ -436,7 +441,7 @@ static int cat_ssl_server_alpn_callback(
return SSL_TLSEXT_ERR_OK;
}

CAT_API cat_bool_t cas_ssl_context_set_apln_protocols(cat_ssl_context_t *context, cat_bool_t is_client, const char *alpn_protocols)
CAT_API cat_bool_t cas_ssl_context_set_alpn_protocols(cat_ssl_context_t *context, cat_bool_t is_client, const char *alpn_protocols)
{
CAT_LOG_DEBUG(SSL, "SSL_CTX_set_alpn_protos(%p, \"%s\")", context, alpn_protocols);
cat_string_t alpn;
Expand Down Expand Up @@ -697,6 +702,7 @@ CAT_API cat_ssl_t *cat_ssl_create(cat_ssl_t *ssl, cat_ssl_context_t *context)
cat_ssl_update_last_error(CAT_ESSL, "SSL_new() failed");
goto _new_failed;
}
CAT_REF_ADD(context);

/* malloc for SSL handle */
if (ssl == NULL) {
Expand Down Expand Up @@ -736,6 +742,7 @@ CAT_API cat_ssl_t *cat_ssl_create(cat_ssl_t *ssl, cat_ssl_context_t *context)

/* init ssl fields */
ssl->connection = connection;
ssl->context = context;
ssl->allow_self_signed = cat_false;

return ssl;
Expand All @@ -745,8 +752,11 @@ CAT_API cat_ssl_t *cat_ssl_create(cat_ssl_t *ssl, cat_ssl_context_t *context)
BIO_free(ssl->nbio);
_set_ex_data_failed:
_new_bio_pair_failed:
CAT_REF_DEL(context);
/* When context can be passed as a parameter,
* its reference count must be greater than or equal to 1. */
CAT_ASSERT(CAT_REF_GET(context) >= 1);
SSL_free(connection);
ssl->connection = NULL;
#if CAT_ALLOC_HANDLE_ERRORS
_malloc_failed:
#endif
Expand All @@ -763,6 +773,7 @@ CAT_API void cat_ssl_close(cat_ssl_t *ssl)
cat_buffer_close(&ssl->read_buffer);
/* ibio will be free'd by SSL_free */
BIO_free(ssl->nbio);
cat_ssl_context_close_data(ssl->context);
/* implicitly frees internal_bio */
SSL_free(ssl->connection);
/* free */
Expand Down

0 comments on commit fb15f29

Please sign in to comment.