implement IMC and IMV manager classes
[strongswan.git] / src / libcharon / plugins / tnccs_20 / tnccs_20.c
1 /*
2 * Copyright (C) 2010 Sansar Choinyanbuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "tnccs_20.h"
17
18 #include <debug.h>
19 #include <daemon.h>
20 #include <tnc/tncif.h>
21 #include <tnc/tnccs/tnccs.h>
22
23 typedef struct private_tnccs_20_t private_tnccs_20_t;
24
25 /**
26 * Private data of a tnccs_20_t object.
27 */
28 struct private_tnccs_20_t {
29
30 /**
31 * Public tls_t interface.
32 */
33 tls_t public;
34
35 /**
36 * TNCC if TRUE, TNCS if FALSE
37 */
38 bool is_server;
39
40 /**
41 * Connection ID assigned to this TNCCS connection
42 */
43 TNC_ConnectionID connection_id;
44 };
45
46 METHOD(tls_t, process, status_t,
47 private_tnccs_20_t *this, void *buf, size_t buflen)
48 {
49 if (this->is_server && !this->connection_id)
50 {
51 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
52 (tnccs_t*)this);
53 charon->imvs->notify_connection_change(charon->imvs,
54 this->connection_id, TNC_CONNECTION_STATE_CREATE);
55 }
56 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
57 buflen, this->connection_id);
58 DBG3(DBG_TNC, "%.*s", buflen, buf);
59
60 return NEED_MORE;
61 }
62
63 METHOD(tls_t, build, status_t,
64 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
65 {
66 char *msg;
67
68 if (!this->is_server && !this->connection_id)
69 {
70 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
71 (tnccs_t*)this);
72 charon->imcs->notify_connection_change(charon->imcs,
73 this->connection_id, TNC_CONNECTION_STATE_CREATE);
74 charon->imcs->notify_connection_change(charon->imcs,
75 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
76 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
77 }
78
79 msg = this->is_server ? "tncs-tncc 2.0" : "tncc-tncs 2.0";
80 DBG1(DBG_TNC, "sending TNCCS Batch (%d bytes) for Connection ID %u",
81 strlen(msg), this->connection_id);
82 DBG3(DBG_TNC, "%s", msg);
83 *msglen = strlen(msg);
84 memcpy(buf, msg, *msglen);
85 *buflen = *msglen;
86
87 return ALREADY_DONE;
88 }
89
90 METHOD(tls_t, is_server, bool,
91 private_tnccs_20_t *this)
92 {
93 return this->is_server;
94 }
95
96 METHOD(tls_t, get_purpose, tls_purpose_t,
97 private_tnccs_20_t *this)
98 {
99 return TLS_PURPOSE_EAP_TNC;
100 }
101
102 METHOD(tls_t, is_complete, bool,
103 private_tnccs_20_t *this)
104 {
105 return FALSE;
106 }
107
108 METHOD(tls_t, get_eap_msk, chunk_t,
109 private_tnccs_20_t *this)
110 {
111 return chunk_empty;
112 }
113
114 METHOD(tls_t, destroy, void,
115 private_tnccs_20_t *this)
116 {
117 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
118 free(this);
119 }
120
121 /**
122 * See header
123 */
124 tls_t *tnccs_20_create(bool is_server)
125 {
126 private_tnccs_20_t *this;
127
128 INIT(this,
129 .public = {
130 .process = _process,
131 .build = _build,
132 .is_server = _is_server,
133 .get_purpose = _get_purpose,
134 .is_complete = _is_complete,
135 .get_eap_msk = _get_eap_msk,
136 .destroy = _destroy,
137 },
138 .is_server = is_server,
139 );
140
141 return &this->public;
142 }