Implemented a generic payload field lookup function
[strongswan.git] / src / libcharon / encoding / payloads / payload.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18
19 #include "payload.h"
20
21 #include <encoding/payloads/ike_header.h>
22 #include <encoding/payloads/sa_payload.h>
23 #include <encoding/payloads/nonce_payload.h>
24 #include <encoding/payloads/id_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/notify_payload.h>
27 #include <encoding/payloads/auth_payload.h>
28 #include <encoding/payloads/cert_payload.h>
29 #include <encoding/payloads/certreq_payload.h>
30 #include <encoding/payloads/encryption_payload.h>
31 #include <encoding/payloads/ts_payload.h>
32 #include <encoding/payloads/delete_payload.h>
33 #include <encoding/payloads/vendor_id_payload.h>
34 #include <encoding/payloads/cp_payload.h>
35 #include <encoding/payloads/configuration_attribute.h>
36 #include <encoding/payloads/eap_payload.h>
37 #include <encoding/payloads/unknown_payload.h>
38
39
40 ENUM_BEGIN(payload_type_names, NO_PAYLOAD, NO_PAYLOAD,
41 "NO_PAYLOAD");
42 ENUM_NEXT(payload_type_names, SECURITY_ASSOCIATION, EXTENSIBLE_AUTHENTICATION, NO_PAYLOAD,
43 "SECURITY_ASSOCIATION",
44 "KEY_EXCHANGE",
45 "ID_INITIATOR",
46 "ID_RESPONDER",
47 "CERTIFICATE",
48 "CERTIFICATE_REQUEST",
49 "AUTHENTICATION",
50 "NONCE",
51 "NOTIFY",
52 "DELETE",
53 "VENDOR_ID",
54 "TRAFFIC_SELECTOR_INITIATOR",
55 "TRAFFIC_SELECTOR_RESPONDER",
56 "ENCRYPTED",
57 "CONFIGURATION",
58 "EXTENSIBLE_AUTHENTICATION");
59 #ifdef ME
60 ENUM_NEXT(payload_type_names, ID_PEER, ID_PEER, EXTENSIBLE_AUTHENTICATION,
61 "ID_PEER");
62 ENUM_NEXT(payload_type_names, HEADER, CONFIGURATION_ATTRIBUTE, ID_PEER,
63 "HEADER",
64 "PROPOSAL_SUBSTRUCTURE",
65 "TRANSFORM_SUBSTRUCTURE",
66 "TRANSFORM_ATTRIBUTE",
67 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
68 "CONFIGURATION_ATTRIBUTE");
69 #else
70 ENUM_NEXT(payload_type_names, HEADER, CONFIGURATION_ATTRIBUTE, EXTENSIBLE_AUTHENTICATION,
71 "HEADER",
72 "PROPOSAL_SUBSTRUCTURE",
73 "TRANSFORM_SUBSTRUCTURE",
74 "TRANSFORM_ATTRIBUTE",
75 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
76 "CONFIGURATION_ATTRIBUTE");
77 #endif /* ME */
78 ENUM_END(payload_type_names, CONFIGURATION_ATTRIBUTE);
79
80 /* short forms of payload names */
81 ENUM_BEGIN(payload_type_short_names, NO_PAYLOAD, NO_PAYLOAD,
82 "--");
83 ENUM_NEXT(payload_type_short_names, SECURITY_ASSOCIATION, EXTENSIBLE_AUTHENTICATION, NO_PAYLOAD,
84 "SA",
85 "KE",
86 "IDi",
87 "IDr",
88 "CERT",
89 "CERTREQ",
90 "AUTH",
91 "No",
92 "N",
93 "D",
94 "V",
95 "TSi",
96 "TSr",
97 "E",
98 "CP",
99 "EAP");
100 #ifdef ME
101 ENUM_NEXT(payload_type_short_names, ID_PEER, ID_PEER, EXTENSIBLE_AUTHENTICATION,
102 "IDp");
103 ENUM_NEXT(payload_type_short_names, HEADER, CONFIGURATION_ATTRIBUTE, ID_PEER,
104 "HDR",
105 "PROP",
106 "TRANS",
107 "TRANSATTR",
108 "TSSUB",
109 "CPATTR");
110 #else
111 ENUM_NEXT(payload_type_short_names, HEADER, CONFIGURATION_ATTRIBUTE, EXTENSIBLE_AUTHENTICATION,
112 "HDR",
113 "PROP",
114 "TRANS",
115 "TRANSATTR",
116 "TSSUB",
117 "CPATTR");
118 #endif /* ME */
119 ENUM_END(payload_type_short_names, CONFIGURATION_ATTRIBUTE);
120
121 /*
122 * see header
123 */
124 payload_t *payload_create(payload_type_t type)
125 {
126 switch (type)
127 {
128 case HEADER:
129 return (payload_t*)ike_header_create();
130 case SECURITY_ASSOCIATION:
131 return (payload_t*)sa_payload_create();
132 case PROPOSAL_SUBSTRUCTURE:
133 return (payload_t*)proposal_substructure_create();
134 case TRANSFORM_SUBSTRUCTURE:
135 return (payload_t*)transform_substructure_create();
136 case TRANSFORM_ATTRIBUTE:
137 return (payload_t*)transform_attribute_create();
138 case NONCE:
139 return (payload_t*)nonce_payload_create();
140 case ID_INITIATOR:
141 return (payload_t*)id_payload_create(ID_INITIATOR);
142 case ID_RESPONDER:
143 return (payload_t*)id_payload_create(ID_RESPONDER);
144 #ifdef ME
145 case ID_PEER:
146 return (payload_t*)id_payload_create(ID_PEER);
147 #endif /* ME */
148 case AUTHENTICATION:
149 return (payload_t*)auth_payload_create();
150 case CERTIFICATE:
151 return (payload_t*)cert_payload_create();
152 case CERTIFICATE_REQUEST:
153 return (payload_t*)certreq_payload_create();
154 case TRAFFIC_SELECTOR_SUBSTRUCTURE:
155 return (payload_t*)traffic_selector_substructure_create();
156 case TRAFFIC_SELECTOR_INITIATOR:
157 return (payload_t*)ts_payload_create(TRUE);
158 case TRAFFIC_SELECTOR_RESPONDER:
159 return (payload_t*)ts_payload_create(FALSE);
160 case KEY_EXCHANGE:
161 return (payload_t*)ke_payload_create();
162 case NOTIFY:
163 return (payload_t*)notify_payload_create();
164 case DELETE:
165 return (payload_t*)delete_payload_create(0);
166 case VENDOR_ID:
167 return (payload_t*)vendor_id_payload_create();
168 case CONFIGURATION:
169 return (payload_t*)cp_payload_create();
170 case CONFIGURATION_ATTRIBUTE:
171 return (payload_t*)configuration_attribute_create();
172 case EXTENSIBLE_AUTHENTICATION:
173 return (payload_t*)eap_payload_create();
174 case ENCRYPTED:
175 return (payload_t*)encryption_payload_create();
176 default:
177 return (payload_t*)unknown_payload_create(type);
178 }
179 }
180
181 /**
182 * See header.
183 */
184 bool payload_is_known(payload_type_t type)
185 {
186 if (type == HEADER ||
187 (type >= SECURITY_ASSOCIATION && type <= EXTENSIBLE_AUTHENTICATION))
188 {
189 return TRUE;
190 }
191 #ifdef ME
192 if (type == ID_PEER)
193 {
194 return TRUE;
195 }
196 #endif
197 return FALSE;
198 }
199
200 /**
201 * See header.
202 */
203 void* payload_get_field(payload_t *payload, encoding_type_t type, u_int skip)
204 {
205 encoding_rule_t *rule;
206 size_t count;
207 int i;
208
209 payload->get_encoding_rules(payload, &rule, &count);
210 for (i = 0; i < count; i++)
211 {
212 if (rule[i].type == type && skip-- == 0)
213 {
214 return ((char*)payload) + rule[i].offset;
215 }
216 }
217 return NULL;
218 }