05d9d76f93416b03008708fd377061ed3cac1e2e
[strongswan.git] / Source / lib / utils / identification.c
1 /**
2 * @file identification.c
3 *
4 * @brief Implementation of identification_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <string.h>
27
28 #include "identification.h"
29
30 /**
31 * String mappings for id_type_t.
32 */
33 mapping_t id_type_m[] = {
34 {ID_IPV4_ADDR, "ID_IPV4_ADDR"},
35 {ID_FQDN, "ID_FQDN"},
36 {ID_RFC822_ADDR, "ID_RFC822_ADDR"},
37 {ID_IPV6_ADDR, "ID_IPV6_ADDR"},
38 {ID_DER_ASN1_DN, "ID_DER_ASN1_DN"},
39 {ID_DER_ASN1_GN, "ID_DER_ASN1_GN"},
40 {ID_KEY_ID, "ID_KEY_ID"},
41 {MAPPING_END, NULL}
42 };
43
44
45 typedef struct private_identification_t private_identification_t;
46
47 /**
48 * Private data of an identification_t object.
49 */
50 struct private_identification_t {
51 /**
52 * Public interface.
53 */
54 identification_t public;
55
56 /**
57 * String representation of this ID.
58 */
59 char *string;
60
61 /**
62 * Encoded representation of this ID.
63 */
64 chunk_t encoded;
65
66 /**
67 * Type of this ID.
68 */
69 id_type_t type;
70 };
71
72 static private_identification_t *identification_create();
73
74 /**
75 * Implementation of identification_t.get_encoding.
76 */
77 static chunk_t get_encoding(private_identification_t *this)
78 {
79 return this->encoded;
80 }
81
82 /**
83 * Implementation of identification_t.get_type.
84 */
85 static id_type_t get_type(private_identification_t *this)
86 {
87 return this->type;
88 }
89
90 /**
91 * Implementation of identification_t.get_string.
92 */
93 static char *get_string(private_identification_t *this)
94 {
95 return this->string;
96 }
97
98 /**
99 * Implementation of identification_t.equals.
100 */
101 static bool equals (private_identification_t *this,private_identification_t *other)
102 {
103 if (this->type == other->type)
104 {
105 if (this->encoded.len != other->encoded.len)
106 {
107 return FALSE;
108 }
109 if (memcmp(this->encoded.ptr,other->encoded.ptr,this->encoded.len) == 0)
110 {
111 return TRUE;
112 }
113 }
114 return FALSE;
115 }
116
117 /**
118 * Implementation of identification_t.belongs_to.
119 */
120 static bool belongs_to(private_identification_t *this, private_identification_t *other)
121 {
122 if (this->public.equals(&this->public, &other->public))
123 {
124 return TRUE;
125 }
126
127 if (this->type == other->type && this->type == ID_IPV4_ADDR)
128 {
129 /* is this %any (0.0.0.0)?*/
130 if (*((u_int32_t*)this->encoded.ptr) == 0)
131 {
132 return TRUE;
133 }
134 /* TODO: Do we need subnet support? */
135 }
136 return FALSE;
137 }
138
139 /**
140 * Implementation of identification_t.clone.
141 */
142 static identification_t *clone(private_identification_t *this)
143 {
144 private_identification_t *clone = identification_create();
145
146 clone->type = this->type;
147 clone->encoded = chunk_clone(this->encoded);
148 clone->string = malloc(strlen(this->string) + 1);
149 strcpy(clone->string, this->string);
150
151 return &clone->public;
152 }
153
154 /**
155 * Implementation of identification_t.destroy.
156 */
157 static void destroy(private_identification_t *this)
158 {
159 free(this->string);
160 free(this->encoded.ptr);
161 free(this);
162 }
163
164 /*
165 * Generic constructor used for the other constructors.
166 *
167 * @return private_identification_t object
168 */
169 static private_identification_t *identification_create()
170 {
171 private_identification_t *this = malloc_thing(private_identification_t);
172
173 this->public.equals = (bool (*) (identification_t*,identification_t*))equals;
174 this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to;
175 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
176 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
177 this->public.get_string = (char* (*) (identification_t*))get_string;
178 this->public.clone = (identification_t* (*) (identification_t*))clone;
179 this->public.destroy = (void (*) (identification_t*))destroy;
180
181 this->string = NULL;
182 this->encoded = CHUNK_INITIALIZER;
183
184 return this;
185 }
186
187
188 /*
189 * Described in header.
190 */
191 identification_t *identification_create_from_string(id_type_t type, char *string)
192 {
193 private_identification_t *this = identification_create();
194
195 this->type = type;
196 switch (type)
197 {
198 case ID_IPV4_ADDR:
199 {
200 /* convert string */
201 this->encoded.len = 4;
202 this->encoded.ptr = malloc(this->encoded.len);
203 if (inet_aton(string, ((struct in_addr*)(this->encoded.ptr))) == 0)
204 {
205 free(this->encoded.ptr);
206 free(this);
207 return NULL;
208 }
209 /* clone string */
210 this->string = malloc(strlen(string)+1);
211 strcpy(this->string, string);
212 return &(this->public);
213 }
214 case ID_IPV6_ADDR:
215 case ID_FQDN:
216 case ID_RFC822_ADDR:
217 case ID_DER_ASN1_DN:
218 case ID_DER_ASN1_GN:
219 case ID_KEY_ID:
220 default:
221 {
222 /* not supported */
223 free(this);
224 return NULL;
225 }
226 }
227 }
228
229 /*
230 * Described in header.
231 */
232 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
233 {
234 char *string;
235 private_identification_t *this = identification_create();
236
237 this->encoded = chunk_clone(encoded);
238
239 this->type = type;
240 switch (type)
241 {
242 case ID_IPV4_ADDR:
243 {
244 string = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
245 break;
246 }
247 case ID_IPV6_ADDR:
248 {
249 string = "[ID_IPV6_ADDR]";
250 break;
251 }
252 case ID_FQDN:
253 {
254 string = "[ID_FQDN]";
255 break;
256 }
257 case ID_RFC822_ADDR:
258 {
259 string = "[ID_RFC822_ADDR]";
260 break;
261 }
262 case ID_DER_ASN1_DN:
263 {
264 string = "[ID_DER_ASN1_DN]";
265 break;
266 }
267 case ID_DER_ASN1_GN:
268 {
269 string = "[ID_DER_ASN1_GN]";
270 break;
271 }
272 case ID_KEY_ID:
273 {
274 string = "[ID_KEY_ID]";
275 break;
276 }
277 default:
278 {
279 string = "[unknown id_type_t]";
280 }
281 }
282
283 /* build string, must be cloned since
284 * inet_ntoa points to a subsequently
285 * overwritten buffer */
286 this->string = malloc(strlen(string)+1);
287 strcpy(this->string, string);
288
289 return &(this->public);
290 }