ha plugin does not need Linux headers anymore
[strongswan.git] / src / libimcv / imcv.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "imcv.h"
16
17 #include "utils.h"
18 #include <debug.h>
19
20 #include <syslog.h>
21
22 #define IMCV_DEBUG_LEVEL 1
23
24 /**
25 * Reference count for IMC/IMV instances
26 */
27 refcount_t ref = 0;
28
29 /**
30 * Global configuration of imcv dbg function
31 */
32 static int imcv_debug_level;
33 static bool imcv_stderr_quiet;
34
35 /**
36 * imvc dbg function
37 */
38 static void imcv_dbg(debug_t group, level_t level, char *fmt, ...)
39 {
40 int priority = LOG_INFO;
41 char buffer[8192];
42 char *current = buffer, *next;
43 va_list args;
44
45 if (level <= imcv_debug_level)
46 {
47 if (!imcv_stderr_quiet)
48 {
49 va_start(args, fmt);
50 fprintf(stderr, "[HSR] ");
51 vfprintf(stderr, fmt, args);
52 fprintf(stderr, "\n");
53 va_end(args);
54 }
55
56 /* write in memory buffer first */
57 va_start(args, fmt);
58 vsnprintf(buffer, sizeof(buffer), fmt, args);
59 va_end(args);
60
61 /* do a syslog with every line */
62 while (current)
63 {
64 next = strchr(current, '\n');
65 if (next)
66 {
67 *(next++) = '\0';
68 }
69 syslog(priority, "[HSR] %s\n", current);
70 current = next;
71 }
72 }
73 }
74
75 /**
76 * Described in header.
77 */
78 bool libimcv_init(void)
79 {
80 /* initialize libstrongswan library only once */
81 if (lib)
82 {
83 /* did main program initialize libstrongswan? */
84 if (ref == 0)
85 {
86 ref_get(&ref);
87 }
88 }
89 else
90 {
91 /* we are the first to initialize libstrongswan */
92 if (!library_init(NULL))
93 {
94 return FALSE;
95 }
96
97 if (!lib->plugins->load(lib->plugins, NULL, "random"))
98 {
99 library_deinit();
100 return FALSE;
101 }
102
103 /* set the debug level and stderr output */
104 imcv_debug_level = lib->settings->get_int(lib->settings,
105 "libimcv.debug_level", IMCV_DEBUG_LEVEL);
106 imcv_stderr_quiet = lib->settings->get_int(lib->settings,
107 "libimcv.stderr_quiet", FALSE);
108
109 /* activate the imcv debugging hook */
110 dbg = imcv_dbg;
111 openlog("imcv", 0, LOG_DAEMON);
112
113 DBG1(DBG_LIB, "libimcv initialized");
114 }
115 ref_get(&ref);
116
117 return TRUE;
118 }
119
120 /**
121 * Described in header.
122 */
123 void libimcv_deinit(void)
124 {
125 if (ref_put(&ref))
126 {
127 DBG1(DBG_LIB, "libimcv terminated");
128 library_deinit();
129 }
130 }
131
132