c132f3960647670b3e49fe77db73fad2ce6855de
2 * Copyright (C) 2010 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "tnc_imc_plugin.h"
17 #include "tnc_imc_manager.h"
20 #include <sys/types.h>
28 #include <utils/lexparser.h>
31 * load IMCs from a configuration file
33 static bool load_imcs(char *filename
)
40 DBG1(DBG_TNC
, "loading IMCs from '%s'", filename
);
41 fd
= open(filename
, O_RDONLY
);
44 DBG1(DBG_TNC
, "opening configuration file '%s' failed: %s", filename
,
48 if (fstat(fd
, &sb
) == -1)
50 DBG1(DBG_LIB
, "getting file size of '%s' failed: %s", filename
,
55 addr
= mmap(NULL
, sb
.st_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
56 if (addr
== MAP_FAILED
)
58 DBG1(DBG_LIB
, "mapping '%s' failed: %s", filename
, strerror(errno
));
62 src
= chunk_create(addr
, sb
.st_size
);
64 while (fetchline(&src
, &line
))
72 /* skip comments or empty lines */
73 if (*line
.ptr
== '#' || !eat_whitespace(&line
))
78 /* determine keyword */
79 if (!extract_token(&token
, ' ', &line
))
81 DBG1(DBG_TNC
, "line %d: keyword must be followed by a space",
86 /* only interested in IMCs */
87 if (!match("IMC", &token
))
92 /* advance to the IMC name and extract it */
93 if (!extract_token(&token
, '"', &line
) ||
94 !extract_token(&token
, '"', &line
))
96 DBG1(DBG_TNC
, "line %d: IMC name must be set in double quotes",
101 /* copy the IMC name */
102 name
= malloc(token
.len
+ 1);
103 memcpy(name
, token
.ptr
, token
.len
);
104 name
[token
.len
] = '\0';
106 /* advance to the IMC path and extract it */
107 if (!eat_whitespace(&line
))
109 DBG1(DBG_TNC
, "line %d: IMC path is missing", line_nr
);
113 if (!extract_token(&token
, ' ', &line
))
118 /* copy the IMC path */
119 path
= malloc(token
.len
+ 1);
120 memcpy(path
, token
.ptr
, token
.len
);
121 path
[token
.len
] = '\0';
123 /* load and register IMC instance */
124 imc
= tnc_imc_create(name
, path
);
131 if (!charon
->imcs
->add(charon
->imcs
, imc
))
136 DBG1(DBG_TNC
, "IMC %u \"%s\" loaded from '%s'", imc
->get_id(imc
),
139 munmap(addr
, sb
.st_size
);
144 METHOD(plugin_t
, destroy
, void,
145 tnc_imc_plugin_t
*this)
147 charon
->imcs
->destroy(charon
->imcs
);
154 plugin_t
*tnc_imc_plugin_create()
157 tnc_imc_plugin_t
*this;
165 /* Create IMC manager */
166 charon
->imcs
= tnc_imc_manager_create();
168 /* Load IMCs and abort if not all instances initalize successfully */
169 tnc_config
= lib
->settings
->get_str(lib
->settings
,
170 "charon.plugins.tnc-imc.tnc_config", "/etc/tnc_config");
171 if (!load_imcs(tnc_config
))
173 charon
->imcs
->destroy(charon
->imcs
);
177 return &this->plugin
;