- implemented sa_config
[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 * implements identification_t.destroy
85 */
86 static void destroy(private_identification_t *this)
87 {
88 allocator_free(this->string);
89 allocator_free(this->encoded.ptr);
90 allocator_free(this);
91 }
92
93 /**
94 * Generic constructor used for the other twos
95 */
96 static private_identification_t *identification_create()
97 {
98
99 private_identification_t *this = allocator_alloc_thing(private_identification_t);
100
101 /* assign methods */
102 this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding;
103 this->public.get_type = (id_type_t (*) (identification_t*))get_type;
104 this->public.get_string = (char* (*) (identification_t*))get_string;
105 this->public.destroy = (void (*) (identification_t*))destroy;
106
107 this->string = NULL;
108 this->encoded = CHUNK_INITIALIZER;
109
110 return this;
111 }
112
113 /*
114 * Described in header.
115 */
116 identification_t *identification_create_from_string(id_type_t type, char *string)
117 {
118 private_identification_t *this = identification_create();
119 switch (type)
120 {
121 case ID_IPV4_ADDR:
122 {
123 /* convert string */
124 this->encoded.len = 4;
125 this->encoded.ptr = allocator_alloc(this->encoded.len);
126 if (inet_aton(string, ((struct in_addr*)(this->encoded.ptr))) == 0)
127 {
128 allocator_free(this->encoded.ptr);
129 allocator_free(this);
130 return NULL;
131 }
132 /* clone string */
133 this->string = allocator_alloc(strlen(string)+1);
134 strcpy(this->string, string);
135 return &(this->public);
136 }
137 case ID_IPV6_ADDR:
138 case ID_FQDN:
139 case ID_RFC822_ADDR:
140 case ID_DER_ASN1_DN:
141 case ID_DER_ASN1_GN:
142 case ID_KEY_ID:
143 default:
144 {
145 /* not supported */
146 allocator_free(this);
147 return NULL;
148 }
149 }
150 }
151
152 /*
153 * Described in header.
154 */
155 identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded)
156 {
157 private_identification_t *this = identification_create();
158 switch (type)
159 {
160 case ID_IPV4_ADDR:
161 {
162 char *tmp;
163 /* clone chunk */
164 if (encoded.len != 4)
165 {
166 allocator_free(this);
167 return NULL;
168 }
169 this->encoded = allocator_clone_chunk(encoded);
170 tmp = inet_ntoa(*((struct in_addr*)(encoded.ptr)));
171 /* build string, must be cloned */
172 this->string = allocator_alloc(strlen(tmp)+1);
173 strcpy(this->string, tmp);
174 return &(this->public);
175 }
176 case ID_IPV6_ADDR:
177 case ID_FQDN:
178 case ID_RFC822_ADDR:
179 case ID_DER_ASN1_DN:
180 case ID_DER_ASN1_GN:
181 case ID_KEY_ID:
182 default:
183 {
184 /* not supported */
185 allocator_free(this);
186 return NULL;
187 }
188 }
189 }