ce4cedcbcef248fb92584d23fc29390ea1a414f6
[strongswan.git] / src / charon-tkm / src / tkm / tkm.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <tkm/client.h>
18 #include <tkm/constants.h>
19
20 #include "tkm.h"
21
22 #define IKE_SOCKET "/tmp/tkm.rpc.ike"
23 #define EES_SOCKET "/tmp/tkm.rpc.ees"
24
25 typedef struct private_tkm_t private_tkm_t;
26
27 extern result_type ees_server_init(const char * const address);
28 extern void ees_server_finalize(void);
29 extern void ehandler_init(void);
30
31 /*
32 * Private additions to tkm_t.
33 */
34 struct private_tkm_t {
35
36 /**
37 * Public members of tkm_t.
38 */
39 tkm_t public;
40 };
41
42 /**
43 * Single instance of tkm_t.
44 */
45 tkm_t *tkm = NULL;
46
47 /**
48 * Described in header.
49 */
50 bool tkm_init()
51 {
52 private_tkm_t *this;
53
54 active_requests_type max_requests;
55 nc_id_type nc;
56 dh_id_type dh;
57 cc_id_type cc;
58 ae_id_type ae;
59 isa_id_type isa;
60 esa_id_type esa;
61
62 /* initialize TKM client library */
63 tkmlib_init();
64
65 ehandler_init();
66
67 if (ike_init(IKE_SOCKET) != TKM_OK)
68 {
69 tkmlib_final();
70 return FALSE;
71 }
72 /* init esa event service */
73 if (ees_server_init(EES_SOCKET) != TKM_OK)
74 {
75 tkmlib_final();
76 return FALSE;
77 }
78
79 if (ike_tkm_reset() != TKM_OK)
80 {
81 ees_server_finalize();
82 tkmlib_final();
83 return FALSE;
84 }
85
86 /* get limits from tkm */
87 if (ike_tkm_limits(&max_requests, &nc, &dh, &cc, &ae, &isa, &esa) != TKM_OK)
88 {
89 ees_server_finalize();
90 tkmlib_final();
91 return FALSE;
92 }
93
94 const tkm_limits_t limits = {nc, dh, cc, isa, ae, esa};
95
96 INIT(this,
97 .public = {
98 .idmgr = tkm_id_manager_create(limits),
99 .chunk_map = tkm_chunk_map_create(),
100 },
101 );
102 tkm = &this->public;
103
104 return TRUE;
105 }
106
107 /**
108 * Described in header.
109 */
110 void tkm_deinit()
111 {
112 if (!tkm)
113 {
114 return;
115 }
116 private_tkm_t *this = (private_tkm_t*)tkm;
117 this->public.idmgr->destroy(this->public.idmgr);
118 this->public.chunk_map->destroy(this->public.chunk_map);
119
120 ees_server_finalize();
121
122 tkmlib_final();
123 free(this);
124 tkm = NULL;
125 }