summaryrefslogtreecommitdiff
path: root/bouncer/main.cpp
blob: 13e1ced67babe0ab1bb161bcdab845c3615e2730 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "core.h"
#include "dns.h"

#ifdef USE_GNUTLS
static gnutls_dh_params_t dh_params;
gnutls_certificate_credentials_t g_serverCreds, g_clientCreds;

bool initTLS() {
	int ret;
	ret = gnutls_global_init();
	if (ret != GNUTLS_E_SUCCESS) {
		printf("gnutls_global_init failure: %s\n", gnutls_strerror(ret));
		return false;
	}

	unsigned int bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_LEGACY);

	ret = gnutls_dh_params_init(&dh_params);
	if (ret != GNUTLS_E_SUCCESS) {
		printf("dh_params_init failure: %s\n", gnutls_strerror(ret));
		return false;
	}

	ret = gnutls_dh_params_generate2(dh_params, bits);
	if (ret != GNUTLS_E_SUCCESS) {
		printf("dh_params_generate2 failure: %s\n", gnutls_strerror(ret));
		return false;
	}

	gnutls_certificate_allocate_credentials(&g_clientCreds);
	ret = gnutls_certificate_set_x509_key_file(g_clientCreds, "ssl_test.crt", "ssl_test.key", GNUTLS_X509_FMT_PEM);
	if (ret != GNUTLS_E_SUCCESS) {
		printf("set_x509_key_file failure: %s\n", gnutls_strerror(ret));
		return false;
	}
	gnutls_certificate_set_dh_params(g_clientCreds, dh_params);

	gnutls_certificate_allocate_credentials(&g_serverCreds);

	return true;
}

void cleanupTLS() {
	gnutls_certificate_free_credentials(g_clientCreds);
	gnutls_certificate_free_credentials(g_serverCreds);
	gnutls_dh_params_deinit(dh_params);
	gnutls_global_deinit();
}
#endif

int main(int argc, char **argv) {
#ifdef USE_GNUTLS
	if (!initTLS())
		return EXIT_FAILURE;
#endif

	DNS::start();

	Bouncer bounce;
	bounce.loadConfig();

	int errcode = bounce.execute();

	DNS::stop();

#ifdef USE_GNUTLS
	cleanupTLS();
#endif

	if (errcode < 0) {
		printf("(Bouncer::execute failed with %d)\n", errcode);
		return EXIT_FAILURE;
	} else {
		return EXIT_SUCCESS;
	}
}