9cf54b087ac964e0b3fe39bb07775403bc1558b3
[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 <daemon.h>
18
19 #include <tkm/client.h>
20 #include <tkm/constants.h>
21
22 #include "tkm.h"
23
24 #define IKE_SOCKET "/tmp/tkm.rpc.ike"
25 #define EES_SOCKET "/tmp/tkm.rpc.ees"
26
27 typedef struct private_tkm_t private_tkm_t;
28
29 extern result_type ees_server_init(const char * const address);
30 extern void ees_server_finalize(void);
31 extern void ehandler_init(void);
32
33 /*
34 * Private additions to tkm_t.
35 */
36 struct private_tkm_t {
37
38 /**
39 * Public members of tkm_t.
40 */
41 tkm_t public;
42 };
43
44 /**
45 * Single instance of tkm_t.
46 */
47 tkm_t *tkm = NULL;
48
49 /**
50 * Described in header.
51 */
52 bool tkm_init()
53 {
54 private_tkm_t *this;
55
56 active_requests_type max_requests;
57 nc_id_type nc;
58 dh_id_type dh;
59 cc_id_type cc;
60 ae_id_type ae;
61 isa_id_type isa;
62 esa_id_type esa;
63
64 /* initialize TKM client library */
65 tkmlib_init();
66 ehandler_init();
67
68 const char * const ikesock = lib->settings->get_str(lib->settings,
69 "%s.ike_socket",
70 IKE_SOCKET,
71 charon->name);
72 if (ike_init(ikesock) != TKM_OK)
73 {
74 tkmlib_final();
75 return FALSE;
76 }
77 DBG1(DBG_DMN, "connected to TKM via socket '%s'", ikesock);
78
79 const char * const eessock = lib->settings->get_str(lib->settings,
80 "%s.ees_socket",
81 EES_SOCKET,
82 charon->name);
83 ees_server_init(eessock);
84 DBG1(DBG_DMN, "serving EES requests on socket '%s'", eessock);
85
86 if (ike_tkm_reset() != TKM_OK)
87 {
88 ees_server_finalize();
89 tkmlib_final();
90 return FALSE;
91 }
92
93 /* get limits from tkm */
94 if (ike_tkm_limits(&max_requests, &nc, &dh, &cc, &ae, &isa, &esa) != TKM_OK)
95 {
96 ees_server_finalize();
97 tkmlib_final();
98 return FALSE;
99 }
100
101 const tkm_limits_t limits = {nc, dh, cc, isa, ae, esa};
102
103 INIT(this,
104 .public = {
105 .idmgr = tkm_id_manager_create(limits),
106 .chunk_map = tkm_chunk_map_create(),
107 },
108 );
109 tkm = &this->public;
110
111 return TRUE;
112 }
113
114 /**
115 * Described in header.
116 */
117 void tkm_deinit()
118 {
119 if (!tkm)
120 {
121 return;
122 }
123 private_tkm_t *this = (private_tkm_t*)tkm;
124 this->public.idmgr->destroy(this->public.idmgr);
125 this->public.chunk_map->destroy(this->public.chunk_map);
126
127 ees_server_finalize();
128
129 tkmlib_final();
130 free(this);
131 tkm = NULL;
132 }