summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/tnccs_11/messages/tnccs_msg.c
blob: fa5ce82394c1de421d56df685d7d36c90af158a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2006 Mike McCauley (mikem@open.com.au)
 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "tnccs_msg.h"
#include "imc_imv_msg.h"
#include "tnccs_error_msg.h"
#include "tnccs_preferred_language_msg.h"
#include "tnccs_reason_strings_msg.h"
#include "tnccs_recommendation_msg.h"
#include "tnccs_tncs_contact_info_msg.h"

#include <library.h>
#include <utils/debug.h>

ENUM(tnccs_msg_type_names, IMC_IMV_MSG, TNCCS_MSG_ROOF,
	"IMC-IMV",
	"TNCCS-Recommendation",
	"TNCCS-Error",
	"TNCCS-PreferredLanguage",
	"TNCCS-ReasonStrings",
	"TNCCS-TNCSContactInfo"
);

/**
 * See header
 */
tnccs_msg_t* tnccs_msg_create_from_node(xmlNodePtr node, linked_list_t *errors)
{
	char *error_msg, buf[BUF_LEN];
	tnccs_error_type_t error_type = TNCCS_ERROR_MALFORMED_BATCH;
	tnccs_msg_t *msg;
	tnccs_msg_type_t type = IMC_IMV_MSG;

	if (streq((char*)node->name, "IMC-IMV-Message"))
	{
		DBG2(DBG_TNC, "processing %N message", tnccs_msg_type_names, type);
		return imc_imv_msg_create_from_node(node, errors);
	}
	else if (streq((char*)node->name, "TNCC-TNCS-Message"))
	{
		bool found = FALSE;
		xmlNsPtr ns = node->ns;
		xmlNodePtr cur = node->xmlChildrenNode;
		xmlNodePtr xml_msg_node = NULL;

		while (cur)
		{
			if (streq(cur->name, "Type") && cur->ns == ns)
			{
				xmlChar *content = xmlNodeGetContent(cur);

				type = strtol(content, NULL, 16);
				xmlFree(content);
				found = TRUE;
			}
			else if (streq(cur->name, "XML") && cur->ns == ns)
			{
				xml_msg_node = cur->xmlChildrenNode;
			}
			cur = cur->next;
		}
		if (!found)
		{
			error_msg = "Type is missing in TNCC-TNCS-Message";
			goto fatal;
		}
		if (!xml_msg_node)
		{
			error_msg = "XML node is missing in TNCC-TNCS-Message";
			goto fatal;
		}
		cur = xml_msg_node;

		/* skip empty and blank nodes */
		while (cur && xmlIsBlankNode(cur))
		{
			cur = cur->next;
		}
		if (!cur)
		{
			error_msg = "XML node is empty";
			goto fatal;
		}

		/* check if TNCCS message type and node name agree */
		if (type >= TNCCS_MSG_RECOMMENDATION && type <= TNCCS_MSG_ROOF)
		{
			DBG2(DBG_TNC, "processing %N message", tnccs_msg_type_names, type);
			if (cur->ns != ns)
			{
				error_msg = "node is not in the TNCCS message namespace";
				goto fatal;
			}
			if (type != enum_from_name(tnccs_msg_type_names, (char*)cur->name))
			{
				error_msg = buf;
				snprintf(buf, BUF_LEN, "expected '%N' node but was '%s'",
						 tnccs_msg_type_names, type, (char*)cur->name);
				goto fatal;
			}
		}

		switch (type)
		{
			case TNCCS_MSG_RECOMMENDATION:
				return tnccs_recommendation_msg_create_from_node(cur, errors);
			case TNCCS_MSG_ERROR:
				return tnccs_error_msg_create_from_node(cur);
			case TNCCS_MSG_PREFERRED_LANGUAGE:
				return tnccs_preferred_language_msg_create_from_node(cur, errors);
			case TNCCS_MSG_REASON_STRINGS:
				return tnccs_reason_strings_msg_create_from_node(cur, errors);
			case TNCCS_MSG_TNCS_CONTACT_INFO:
				return tnccs_tncs_contact_info_msg_create_from_node(cur, errors);
			default:
				DBG1(DBG_TNC, "ignoring TNCC-TNCS-Message with type %d", type);
				return NULL;
		}
	}
	DBG1(DBG_TNC, "ignoring unknown message node '%s'", (char*)node->name);
	return NULL;

fatal:
	msg = tnccs_error_msg_create(error_type, error_msg);
	errors->insert_last(errors, msg);
	return NULL;
}