- kernel interface hacks, works partially
[strongswan.git] / Source / charon / 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
27 #include "identification.h"
28
29 #include <utils/allocator.h>
30
31
32 /**
33 * String mappings for id_type_t.
34 */
35 mapping_t id_type_m[] = {
36 {ID_IPV4_ADDR, "ID_IPV4_ADDR"},
37 {ID_FQDN, "ID_FQDN"},
38 {ID_RFC822_ADDR, "ID_RFC822_ADDR"},
39 {ID_IPV6_ADDR, "ID_IPV6_ADDR"},
40 {ID_DER_ASN1_DN, "ID_DER_ASN1_DN"},
41 {ID_DER_ASN1_GN, "ID_DER_ASN1_GN"},
42 {ID_KEY_ID, "ID_KEY_ID"},
43 {MAPPING_END, NULL}
44 };
45
46
47
48 typedef struct private_identification_t private_identification_t;
49
50 /**
51 * Private data of an identification_t object.
52 */
53 struct private_identification_t {
54 /**
55 * Public interface.
56 */
57 identification_t public;
58
59 /**
60 * string representation of this id
61 */
62 char *string;
63
64 /**
65 * encoded representation of this id
66 */
67 chunk_t encoded;
68
69 /**
70 * type of this id
71 */
72 id_type_t type;
73 };
74
75 /**
76 * implements identification_t.get_encoding
77 */
78 static chunk_t get_encoding(private_identification_t *this)
79 {
80 return this->encoded;
81 }
82
83 /**
84 * implements identification_t.get_type
85 */
86 static id_type_t get_type(private_identification_t *this)
87 {
88 return this->type;
89 }
90
91 /**
92 * implements identification_t.get_string
93 */
94 static char *get_string(private_identification_t *this)
95 {
96 return this->string;
97 }
98
99 /**
100 * Implementation of identification_t.equals.
101 */
102 static bool equals (private_identification_t *this,private_identification_t *other)
103 {
104 if (this->type == other->type)
105 {
106 if (this->encoded.len != other->encoded.len)
107 {
108 return FALSE;
109 }
110 if (memcmp(this->encoded.ptr,other->encoded.ptr,this->encoded.len) == 0)
111 {
112 return TRUE;
113 }
114 }
115 return FALSE;
116 }
117
118 /**
119 * implements identification_t.destroy
120 */
121 static void destroy(private_identification_t *this)
122 {
123 allocator_free(this->string);
124 allocator_free(this->encoded.ptr);
125 allocator_free(this);
126 }
127
128 /**
129 * Generic constructor used for the other twos
130 */
131 static private_identification_t *identification_create()
132 {
133 private_identification_t *this = allocator_alloc_thing(private_identification_t);
134
135 /* assign methods */
136 this->public.equals = (bool (*) (identification_t*,identification_t*))equals;
137 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
138 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
139 this->public.get_string = (char* (*) (identification_t*))get_string;
140 this->public.destroy = (void (*) (identification_t*))destroy;
141
142 this->string = NULL;
143 this->encoded = CHUNK_INITIALIZER;
144
145 return this;
146 }
147
148 /*
149 * Described in header.
150 */
151 identification_t *identification_create_from_string(id_type_t type, char *string)
152 {
153 private_identification_t *this = identification_create();
154
155 this->type = type;
156 switch (type)
157 {
158 case ID_IPV4_ADDR:
159 {
160 /* convert string */
161 this->encoded.len = 4;
162 this->encoded.ptr = allocator_alloc(this->encoded.len);
163 if (inet_aton(string, ((struct in_addr*)(this->encoded.ptr))) == 0)
164 {
165 allocator_free(this->encoded.ptr);
166 allocator_free(this);
167 return NULL;
168 }
169 /* clone string */
170 this->string = allocator_alloc(strlen(string)+1);
171 strcpy(this->string, string);
172 return &(this->public);
173 }
174 case ID_IPV6_ADDR:
175 case ID_FQDN:
176 case ID_RFC822_ADDR:
177 case ID_DER_ASN1_DN:
178 case ID_DER_ASN1_GN:
179 case ID_KEY_ID:
180 default:
181 {
182 /* not supported */
183 allocator_free(this);
184 return NULL;
185 }
186 }
187 }
188
189 /*
190 * Described in header.
191 */
192 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
193 {
194 char *string;
195 private_identification_t *this = identification_create();
196
197 this->encoded = allocator_clone_chunk(encoded);
198
199 this->type = type;
200 switch (type)
201 {
202 case ID_IPV4_ADDR:
203 {
204 string = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
205 break;
206 }
207 case ID_IPV6_ADDR:
208 {
209 string = "[ID_IPV6_ADDR]";
210 break;
211 }
212 case ID_FQDN:
213 {
214 string = "[ID_FQDN]";
215 break;
216 }
217 case ID_RFC822_ADDR:
218 {
219 string = "[ID_RFC822_ADDR]";
220 break;
221 }
222 case ID_DER_ASN1_DN:
223 {
224 string = "[ID_DER_ASN1_DN]";
225 break;
226 }
227 case ID_DER_ASN1_GN:
228 {
229 string = "[ID_DER_ASN1_GN]";
230 break;
231 }
232 case ID_KEY_ID:
233 {
234 string = "[ID_KEY_ID]";
235 break;
236 }
237 default:
238 {
239 string = "[unknown id_type_t]";
240 }
241 }
242
243 /* build string, must be cloned */
244 this->string = allocator_alloc(strlen(string)+1);
245 strcpy(this->string, string);
246
247 return &(this->public);
248 }