- new configuration support added to ike_sa and states
[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 typedef struct private_identification_t private_identification_t;
33
34 /**
35 * Private data of an identification_t object.
36 */
37 struct private_identification_t {
38 /**
39 * Public interface.
40 */
41 identification_t public;
42
43 /**
44 * string representation of this id
45 */
46 char *string;
47
48 /**
49 * encoded representation of this id
50 */
51 chunk_t encoded;
52
53 /**
54 * type of this id
55 */
56 id_type_t type;
57 };
58
59 /**
60 * implements identification_t.get_encoding
61 */
62 static chunk_t get_encoding(private_identification_t *this)
63 {
64 return this->encoded;
65 }
66
67 /**
68 * implements identification_t.get_type
69 */
70 static id_type_t get_type(private_identification_t *this)
71 {
72 return this->type;
73 }
74
75 /**
76 * implements identification_t.get_string
77 */
78 static char *get_string(private_identification_t *this)
79 {
80 return this->string;
81 }
82
83 /**
84 * Implementation of identification_t.equals.
85 */
86 static bool equals (private_identification_t *this,private_identification_t *other)
87 {
88 if (this->type == other->type)
89 {
90 if (this->encoded.len != other->encoded.len)
91 {
92 return FALSE;
93 }
94 if (memcmp(this->encoded.ptr,other->encoded.ptr,this->encoded.len) == 0)
95 {
96 return TRUE;
97 }
98 }
99 return FALSE;
100 }
101
102 /**
103 * implements identification_t.destroy
104 */
105 static void destroy(private_identification_t *this)
106 {
107 allocator_free(this->string);
108 allocator_free(this->encoded.ptr);
109 allocator_free(this);
110 }
111
112 /**
113 * Generic constructor used for the other twos
114 */
115 static private_identification_t *identification_create()
116 {
117
118 private_identification_t *this = allocator_alloc_thing(private_identification_t);
119
120 /* assign methods */
121 this->public.equals = (bool (*) (identification_t*,identification_t*))equals;
122 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
123 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
124 this->public.get_string = (char* (*) (identification_t*))get_string;
125 this->public.destroy = (void (*) (identification_t*))destroy;
126
127 this->string = NULL;
128 this->encoded = CHUNK_INITIALIZER;
129
130 return this;
131 }
132
133 /*
134 * Described in header.
135 */
136 identification_t *identification_create_from_string(id_type_t type, char *string)
137 {
138 private_identification_t *this = identification_create();
139 switch (type)
140 {
141 case ID_IPV4_ADDR:
142 {
143 /* convert string */
144 this->encoded.len = 4;
145 this->encoded.ptr = allocator_alloc(this->encoded.len);
146 if (inet_aton(string, ((struct in_addr*)(this->encoded.ptr))) == 0)
147 {
148 allocator_free(this->encoded.ptr);
149 allocator_free(this);
150 return NULL;
151 }
152 /* clone string */
153 this->string = allocator_alloc(strlen(string)+1);
154 strcpy(this->string, string);
155 return &(this->public);
156 }
157 case ID_IPV6_ADDR:
158 case ID_FQDN:
159 case ID_RFC822_ADDR:
160 case ID_DER_ASN1_DN:
161 case ID_DER_ASN1_GN:
162 case ID_KEY_ID:
163 default:
164 {
165 /* not supported */
166 allocator_free(this);
167 return NULL;
168 }
169 }
170 }
171
172 /*
173 * Described in header.
174 */
175 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
176 {
177 private_identification_t *this = identification_create();
178 switch (type)
179 {
180 case ID_IPV4_ADDR:
181 {
182 char *tmp;
183 /* clone chunk */
184 if (encoded.len != 4)
185 {
186 allocator_free(this);
187 return NULL;
188 }
189 this->encoded = allocator_clone_chunk(encoded);
190 tmp = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
191 /* build string, must be cloned */
192 this->string = allocator_alloc(strlen(tmp)+1);
193 strcpy(this->string, tmp);
194 return &(this->public);
195 }
196 case ID_IPV6_ADDR:
197 case ID_FQDN:
198 case ID_RFC822_ADDR:
199 case ID_DER_ASN1_DN:
200 case ID_DER_ASN1_GN:
201 case ID_KEY_ID:
202 default:
203 {
204 /* not supported */
205 allocator_free(this);
206 return NULL;
207 }
208 }
209 }