/tncs/src/tncs/iel/IMV.cpp

https://github.com/trustathsh/tnc-fhh · C++ · 328 lines · 155 code · 38 blank · 135 comment · 14 complexity · a8e31598fa3bc35536f8b86a55475fc5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2006-2011 Fachhochschule Hannover
  3. * (University of Applied Sciences and Arts, Hannover)
  4. * Faculty IV, Dept. of Computer Science
  5. * Ricklinger Stadtweg 118, 30459 Hannover, Germany
  6. *
  7. * Email: trust@f4-i.fh-hannover.de
  8. * Website: http://trust.inform.fh-hannover.de/
  9. *
  10. * This file is part of tnc@fhh, an open source
  11. * Trusted Network Connect implementation by the Trust@FHH
  12. * research group at the Fachhochschule Hannover.
  13. *
  14. * tnc@fhh is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * tnc@fhh is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with tnc@fhh; if not, see <http://www.gnu.org/licenses/>
  26. */
  27. #include "IMV.h"
  28. #include <log4cxx/logger.h>
  29. #include <iomanip>
  30. namespace tncfhh {
  31. namespace iel {
  32. /* for logging */
  33. static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("TNCS.IMV"));
  34. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  35. * *
  36. * IMV::IMV(TNC_ConnectionID, IMVProperties&) *
  37. * *
  38. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  39. IMV::IMV(TNC_ConnectionID conID, IMVProperties *imvProperties)
  40. :conID(conID), imvProperties(imvProperties), inReceiveMessage(false),
  41. inSolicitRecommendation(false), inBatchEnding(false),
  42. isProvideRecommendationSet(false), error(false),
  43. recommendation(TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION),
  44. evaluation(TNC_IMV_EVALUATION_RESULT_DONT_KNOW)
  45. {
  46. LOG4CXX_DEBUG(logger, "Creating IMV instance for IMVProperties " << this->imvProperties->getName() << " for connection " << conID);
  47. // call libIMV.so
  48. this->notifyConnectionChange(TNC_CONNECTION_STATE_CREATE);
  49. }
  50. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  51. * *
  52. * IMV::~IMV() *
  53. * *
  54. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  55. IMV::~IMV()
  56. {
  57. LOG4CXX_TRACE(logger, "destructor for IMV instance " << this->imvProperties->getName() << " connection " << this->conID);
  58. }
  59. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  60. * *
  61. * void IMV::notifyConnectionChange(TNC_ConnectionState) *
  62. * *
  63. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  64. void IMV::notifyConnectionChange(TNC_ConnectionState connectionState)
  65. {
  66. LOG4CXX_TRACE(logger, "notifyConnectionChange( " << connectionState << ") for IMV " << this->imvProperties->getName() << " connection " << this->conID);
  67. /* if the connection state is handshake, then reset the error and recommendation flags*/
  68. if (connectionState == TNC_CONNECTION_STATE_HANDSHAKE)
  69. {
  70. this->error = false;
  71. this->recommendation = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION;
  72. this->evaluation = TNC_IMV_EVALUATION_RESULT_DONT_KNOW;
  73. this->isProvideRecommendationSet = false;
  74. }
  75. /* if IMV has an error, don't call the function */
  76. if (this->error)
  77. {
  78. LOG4CXX_WARN(logger, "Not calling notifyConnectionChange for IMV " << this->imvProperties->getName() << ". IMV instance has an error.");
  79. return;
  80. }
  81. /* call function on IMVProperties */
  82. this->inNotifyConnectionChange = true;
  83. LOG4CXX_TRACE(logger, "call notifyConnectionChange for IMV " << this->imvProperties->getName() << ", newState is " << connectionState);
  84. TNC_Result result = this->imvProperties->call_TNC_IMV_NotifyConnectionChange(this->conID, connectionState);
  85. this->inNotifyConnectionChange = false;
  86. /* check result */
  87. if (result != TNC_RESULT_SUCCESS)
  88. {
  89. LOG4CXX_WARN(logger, "error in call notifyConnectionChange for IMV " << this->imvProperties->getName() << " connection " << this->conID << " . Result is " << result);
  90. this->handleOtherError();
  91. }
  92. }
  93. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  94. * *
  95. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  96. void IMV::receiveMessage(TNC_BufferReference message, TNC_UInt32 messageLength, TNC_MessageType messageType)
  97. {
  98. LOG4CXX_TRACE(logger, "receiveMessage for IMV " << this->imvProperties->getName());
  99. /* if IMV has an error, don't call the function */
  100. if (this->error)
  101. {
  102. LOG4CXX_WARN(logger, "Not calling receiveMessage for IMV " << this->imvProperties->getName() << ". IMV instance has an error.");
  103. return;
  104. }
  105. /* call function in IMVProperties */
  106. this->inReceiveMessage = true;
  107. LOG4CXX_TRACE(logger, "call receiveMessage for IMV " << this->imvProperties->getName() << ", length is " << messageLength << " messageType is 0x" << std::setw(8) << std::setfill('0') << std::hex << messageType);
  108. TNC_Result result = this->imvProperties->call_TNC_IMV_ReceiveMessage(this->conID, message, messageLength, messageType);
  109. this->inReceiveMessage = false;
  110. /* check result */
  111. if (result != TNC_RESULT_SUCCESS)
  112. {
  113. LOG4CXX_WARN(logger, "error in call receiveMessage for IMV " << this->imvProperties->getName() << " connection " << this->conID << " . Result is " << result);
  114. this->handleOtherError();
  115. }
  116. }
  117. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  118. * *
  119. * void IMV::solicitRecommendation() *
  120. * *
  121. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  122. void IMV::solicitRecommendation()
  123. {
  124. LOG4CXX_TRACE(logger, "solicitRecommendation for IMV " << this->imvProperties->getName());
  125. /* if IMV has an error, don't call the function */
  126. if (this->error)
  127. {
  128. LOG4CXX_WARN(logger, "Not calling solicitRecommendation for IMV " << this->imvProperties->getName() << ". IMV instance has an error.");
  129. return;
  130. }
  131. /* call function on libIMV.so */
  132. this->inSolicitRecommendation = true;
  133. LOG4CXX_TRACE(logger, "call solicitRecommendation for IMV " << this->imvProperties->getName() << "(id=" << this->imvProperties->getID() << ") and connection " << this->conID);
  134. TNC_Result result = this->imvProperties->call_TNC_IMV_SolicitRecommendation(this->conID);
  135. this->inSolicitRecommendation = false;
  136. /* check result */
  137. if (result != TNC_RESULT_SUCCESS)
  138. {
  139. LOG4CXX_WARN(logger, "error in call solicitRecommendation for IMV " << this->imvProperties->getName() << " connection " << this->conID << " . Result is " << result);
  140. this->handleOtherError();
  141. }
  142. }
  143. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  144. * *
  145. * void IMV::batchEnding() *
  146. * *
  147. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  148. void IMV::batchEnding()
  149. {
  150. LOG4CXX_TRACE(logger, "batchEnding for IMV " << this->imvProperties->getName() << ", connection is " << this->conID);
  151. /* if IMV has an error, don't call the function */
  152. if (this->error)
  153. {
  154. LOG4CXX_WARN(logger, "Not calling batchEnding for IMV " << this->imvProperties->getName() << " connection " << this->conID << ". IMV instance has an error.");
  155. return;
  156. }
  157. /* call funtion on libIMV.so */
  158. this->inBatchEnding = true;
  159. LOG4CXX_TRACE(logger, "call batchEnding for IMV " << this->imvProperties->getName() << "(id=" << this->imvProperties->getID() << ") and connection " << this->conID);
  160. TNC_Result result = this->imvProperties->call_TNC_IMV_BatchEnding(this->conID);
  161. this->inBatchEnding = false;
  162. /* check result */
  163. if (result != TNC_RESULT_SUCCESS)
  164. {
  165. LOG4CXX_WARN(logger, "error in call batchEnding for IMV " << this->imvProperties->getName() << " connection " << this->conID << " . Result is " << result);
  166. this->handleOtherError();
  167. }
  168. }
  169. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  170. * *
  171. * bool IMV::isInNotifyConnectionChange() *
  172. * *
  173. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  174. bool IMV::isInNotifyConnectionChange()
  175. {
  176. return this->inNotifyConnectionChange;
  177. }
  178. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  179. * *
  180. * bool IMV::isInReceiveMessage() *
  181. * *
  182. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  183. bool IMV::isInReceiveMessage()
  184. {
  185. return this->inReceiveMessage;
  186. }
  187. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  188. * *
  189. * bool IMV::isInSolicitRecommendation() *
  190. * *
  191. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  192. bool IMV::isInSolicitRecommendation()
  193. {
  194. return this->inSolicitRecommendation;
  195. }
  196. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  197. * *
  198. * bool IMV::isInBatchEnding() *
  199. * *
  200. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  201. bool IMV::isInBatchEnding()
  202. {
  203. return this->inBatchEnding;
  204. }
  205. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  206. * *
  207. * bool IMV::hasProvideRecommendation() *
  208. * *
  209. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  210. bool IMV::hasProvideRecommendation()
  211. {
  212. return this->isProvideRecommendationSet;
  213. }
  214. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  215. * *
  216. * void IMV::setProvideRecommendation(TNC_IMV_Action_Recommendation, *
  217. * TNC_IMV_Evaluation_Result) *
  218. * *
  219. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  220. void IMV::setProvideRecommendation(TNC_IMV_Action_Recommendation recommendation, TNC_IMV_Evaluation_Result evaluation)
  221. {
  222. this->recommendation = recommendation;
  223. this->evaluation = evaluation;
  224. this->isProvideRecommendationSet = true;
  225. }
  226. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  227. * *
  228. * TNC_IMV_Action_Recommendation IMV::getRecommendation() *
  229. * *
  230. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  231. TNC_IMV_Action_Recommendation IMV::getRecommendation()
  232. {
  233. return this->recommendation;
  234. }
  235. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  236. * *
  237. * TNC_IMV_Evaluation_Result IMV::getEvaluation() *
  238. * *
  239. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  240. TNC_IMV_Evaluation_Result IMV::getEvaluation()
  241. {
  242. return this->evaluation;
  243. }
  244. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  245. * *
  246. * const IMVProperties & IMV::getProperties() *
  247. * *
  248. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  249. const IMVProperties *IMV::getIMVProperties()
  250. {
  251. return this->imvProperties;
  252. }
  253. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  254. * *
  255. * bool IMV::hasError() *
  256. * *
  257. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  258. bool IMV::hasError() const
  259. {
  260. return this->error;
  261. }
  262. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  263. * *
  264. * TNC_ConnectionID IMV::getConnectionID() *
  265. * *
  266. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  267. TNC_ConnectionID IMV::getConnectionID()
  268. {
  269. return this->conID;
  270. }
  271. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  272. * *
  273. * void IMV::handleOtherError() *
  274. * *
  275. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  276. void IMV::handleOtherError()
  277. {
  278. // NOT_INITIALIZED and FATAL errors are handled by IMVProperties
  279. // any other error is treated as TNC_RESULT_OTHER, causing a permanent error
  280. // for this handshake on this connection
  281. // try to call notifyConnectionChange(DELETE), ignore return value
  282. this->imvProperties->call_TNC_IMV_NotifyConnectionChange(this->conID, TNC_CONNECTION_STATE_DELETE);
  283. // set error flag. can only be reseted by calling notifyConnectionChange(HANDSHAKE)
  284. this->error = true;
  285. this->recommendation = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION;
  286. this->evaluation = TNC_IMV_EVALUATION_RESULT_ERROR;
  287. this->isProvideRecommendationSet = true;
  288. }
  289. } // namespace iel
  290. } // namespace tncfhh