registry.c 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "git2/errors.h"

#include "common.h"
#include "global.h"
#include "streams/tls.h"
#include "streams/mbedtls.h"
#include "streams/openssl.h"
#include "streams/stransport.h"

struct stream_registry {
	git_rwlock lock;
	git_stream_registration callbacks;
	git_stream_registration tls_callbacks;
};

static struct stream_registry stream_registry;

static void shutdown_stream_registry(void)
{
	git_rwlock_free(&stream_registry.lock);
}

int git_stream_registry_global_init(void)
{
	if (git_rwlock_init(&stream_registry.lock) < 0)
		return -1;

	git__on_shutdown(shutdown_stream_registry);
	return 0;
}

39 40 41
GIT_INLINE(void) stream_registration_cpy(
	git_stream_registration *target,
	git_stream_registration *src)
42
{
43 44 45 46 47 48 49 50 51
	if (src)
		memcpy(target, src, sizeof(git_stream_registration));
	else
		memset(target, 0, sizeof(git_stream_registration));
}

int git_stream_registry_lookup(git_stream_registration *out, git_stream_t type)
{
	git_stream_registration *target;
52 53 54 55
	int error = GIT_ENOTFOUND;

	assert(out);

56 57 58 59 60 61 62 63 64 65 66 67
	switch(type) {
	case GIT_STREAM_STANDARD:
		target = &stream_registry.callbacks;
		break;
	case GIT_STREAM_TLS:
		target = &stream_registry.tls_callbacks;
		break;
	default:
		assert(0);
		return -1;
	}

68
	if (git_rwlock_rdlock(&stream_registry.lock) < 0) {
69
		git_error_set(GIT_ERROR_OS, "failed to lock stream registry");
70 71 72 73
		return -1;
	}

	if (target->init) {
74
		stream_registration_cpy(out, target);
75 76 77 78 79 80 81
		error = 0;
	}

	git_rwlock_rdunlock(&stream_registry.lock);
	return error;
}

82
int git_stream_register(git_stream_t type, git_stream_registration *registration)
83 84 85
{
	assert(!registration || registration->init);

86
	GIT_ERROR_CHECK_VERSION(registration, GIT_STREAM_VERSION, "stream_registration");
87 88

	if (git_rwlock_wrlock(&stream_registry.lock) < 0) {
89
		git_error_set(GIT_ERROR_OS, "failed to lock stream registry");
90 91 92
		return -1;
	}

93 94 95 96 97
	if ((type & GIT_STREAM_STANDARD) == GIT_STREAM_STANDARD)
		stream_registration_cpy(&stream_registry.callbacks, registration);

	if ((type & GIT_STREAM_TLS) == GIT_STREAM_TLS)
		stream_registration_cpy(&stream_registry.tls_callbacks, registration);
98 99 100 101 102

	git_rwlock_wrunlock(&stream_registry.lock);
	return 0;
}

103 104 105

int git_stream_register_tls(
	int GIT_CALLBACK(ctor)(git_stream **out, const char *host, const char *port))
106 107 108 109 110 111 112 113
{
	git_stream_registration registration = {0};

	if (ctor) {
		registration.version = GIT_STREAM_VERSION;
		registration.init = ctor;
		registration.wrap = NULL;

114
		return git_stream_register(GIT_STREAM_TLS, &registration);
115
	} else {
116
		return git_stream_register(GIT_STREAM_TLS, NULL);
117 118
	}
}