2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "git2/errors.h"
12 #include "streams/tls.h"
13 #include "streams/mbedtls.h"
14 #include "streams/openssl.h"
15 #include "streams/stransport.h"
17 struct stream_registry
{
19 git_stream_registration callbacks
;
20 git_stream_registration tls_callbacks
;
23 static struct stream_registry stream_registry
;
25 static void shutdown_stream_registry(void)
27 git_rwlock_free(&stream_registry
.lock
);
30 int git_stream_registry_global_init(void)
32 if (git_rwlock_init(&stream_registry
.lock
) < 0)
35 git__on_shutdown(shutdown_stream_registry
);
39 GIT_INLINE(void) stream_registration_cpy(
40 git_stream_registration
*target
,
41 git_stream_registration
*src
)
44 memcpy(target
, src
, sizeof(git_stream_registration
));
46 memset(target
, 0, sizeof(git_stream_registration
));
49 int git_stream_registry_lookup(git_stream_registration
*out
, git_stream_t type
)
51 git_stream_registration
*target
;
52 int error
= GIT_ENOTFOUND
;
57 case GIT_STREAM_STANDARD
:
58 target
= &stream_registry
.callbacks
;
61 target
= &stream_registry
.tls_callbacks
;
68 if (git_rwlock_rdlock(&stream_registry
.lock
) < 0) {
69 git_error_set(GIT_ERROR_OS
, "failed to lock stream registry");
74 stream_registration_cpy(out
, target
);
78 git_rwlock_rdunlock(&stream_registry
.lock
);
82 int git_stream_register(git_stream_t type
, git_stream_registration
*registration
)
84 assert(!registration
|| registration
->init
);
86 GIT_ERROR_CHECK_VERSION(registration
, GIT_STREAM_VERSION
, "stream_registration");
88 if (git_rwlock_wrlock(&stream_registry
.lock
) < 0) {
89 git_error_set(GIT_ERROR_OS
, "failed to lock stream registry");
93 if ((type
& GIT_STREAM_STANDARD
) == GIT_STREAM_STANDARD
)
94 stream_registration_cpy(&stream_registry
.callbacks
, registration
);
96 if ((type
& GIT_STREAM_TLS
) == GIT_STREAM_TLS
)
97 stream_registration_cpy(&stream_registry
.tls_callbacks
, registration
);
99 git_rwlock_wrunlock(&stream_registry
.lock
);
104 int git_stream_register_tls(
105 int GIT_CALLBACK(ctor
)(git_stream
**out
, const char *host
, const char *port
))
107 git_stream_registration registration
= {0};
110 registration
.version
= GIT_STREAM_VERSION
;
111 registration
.init
= ctor
;
112 registration
.wrap
= NULL
;
114 return git_stream_register(GIT_STREAM_TLS
, ®istration
);
116 return git_stream_register(GIT_STREAM_TLS
, NULL
);