summaryrefslogtreecommitdiff
path: root/src/streams/tls.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2018-11-18 10:29:07 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2018-11-28 15:46:57 +0000
commitdf2cc1087f6de8718319e5bcc65ca8e0e07b717e (patch)
tree02264b646329a5eeca8940fa8557e714e303a50f /src/streams/tls.c
parent0467606ff4dbf57401c8b58188652df821ec865b (diff)
downloadlibgit2-df2cc1087f6de8718319e5bcc65ca8e0e07b717e.tar.gz
stream: provide generic registration API
Update the new stream registration API to be `git_stream_register` which takes a registration structure and a TLS boolean. This allows callers to register non-TLS streams as well as TLS streams. Provide `git_stream_register_tls` that takes just the init callback for backward compatibliity.
Diffstat (limited to 'src/streams/tls.c')
-rw-r--r--src/streams/tls.c77
1 files changed, 11 insertions, 66 deletions
diff --git a/src/streams/tls.c b/src/streams/tls.c
index fe0725272..0e10697cd 100644
--- a/src/streams/tls.c
+++ b/src/streams/tls.c
@@ -9,66 +9,23 @@
#include "common.h"
#include "global.h"
+#include "streams/registry.h"
#include "streams/tls.h"
#include "streams/mbedtls.h"
#include "streams/openssl.h"
#include "streams/stransport.h"
-struct git_tls_stream_registration {
- git_rwlock lock;
- git_stream_registration callbacks;
-};
-
-static struct git_tls_stream_registration stream_registration;
-
-static void shutdown_ssl(void)
-{
- git_rwlock_free(&stream_registration.lock);
-}
-
-int git_tls_stream_global_init(void)
-{
- if (git_rwlock_init(&stream_registration.lock) < 0)
- return -1;
-
- git__on_shutdown(shutdown_ssl);
- return 0;
-}
-
-int git_stream_register_tls(git_stream_registration *registration)
-{
- assert(!registration || registration->init);
-
- if (git_rwlock_wrlock(&stream_registration.lock) < 0) {
- giterr_set(GITERR_OS, "failed to lock stream registration");
- return -1;
- }
-
- if (registration)
- memcpy(&stream_registration.callbacks, registration,
- sizeof(git_stream_registration));
- else
- memset(&stream_registration.callbacks, 0,
- sizeof(git_stream_registration));
-
- git_rwlock_wrunlock(&stream_registration.lock);
- return 0;
-}
-
int git_tls_stream_new(git_stream **out, const char *host, const char *port)
{
int (*init)(git_stream **, const char *, const char *) = NULL;
+ git_stream_registration custom = {0};
+ int error;
assert(out && host && port);
- if (git_rwlock_rdlock(&stream_registration.lock) < 0) {
- giterr_set(GITERR_OS, "failed to lock stream registration");
- return -1;
- }
-
- if (stream_registration.callbacks.init) {
- init = stream_registration.callbacks.init;
- } else {
+ if ((error = git_stream_registry_lookup(&custom, 1)) == 0) {
+ init = custom.init;
+ } else if (error == GIT_ENOTFOUND) {
#ifdef GIT_SECURE_TRANSPORT
init = git_stransport_stream_new;
#elif defined(GIT_OPENSSL)
@@ -76,11 +33,8 @@ int git_tls_stream_new(git_stream **out, const char *host, const char *port)
#elif defined(GIT_MBEDTLS)
init = git_mbedtls_stream_new;
#endif
- }
-
- if (git_rwlock_rdunlock(&stream_registration.lock) < 0) {
- giterr_set(GITERR_OS, "failed to unlock stream registration");
- return -1;
+ } else {
+ return error;
}
if (!init) {
@@ -94,16 +48,12 @@ int git_tls_stream_new(git_stream **out, const char *host, const char *port)
int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host)
{
int (*wrap)(git_stream **, git_stream *, const char *) = NULL;
+ git_stream_registration custom = {0};
assert(out && in);
- if (git_rwlock_rdlock(&stream_registration.lock) < 0) {
- giterr_set(GITERR_OS, "failed to lock stream registration");
- return -1;
- }
-
- if (stream_registration.callbacks.wrap) {
- wrap = stream_registration.callbacks.wrap;
+ if (git_stream_registry_lookup(&custom, 1) == 0) {
+ wrap = custom.wrap;
} else {
#ifdef GIT_SECURE_TRANSPORT
wrap = git_stransport_stream_wrap;
@@ -114,11 +64,6 @@ int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host)
#endif
}
- if (git_rwlock_rdunlock(&stream_registration.lock) < 0) {
- giterr_set(GITERR_OS, "failed to unlock stream registration");
- return -1;
- }
-
if (!wrap) {
giterr_set(GITERR_SSL, "there is no TLS stream available");
return -1;