diff options
author | Martin Willi <martin@strongswan.org> | 2005-12-01 07:35:03 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2005-12-01 07:35:03 +0000 |
commit | d45ec1dedfa06155c344f5cce0ac7b2ec331c825 (patch) | |
tree | de2a74ae64525e4d45c3178ab7d16484297ce1dd /Source/charon/utils | |
parent | 2ef11339c720d06215f44555de020ea5ebdfd641 (diff) | |
download | strongswan-d45ec1dedfa06155c344f5cce0ac7b2ec331c825.tar.bz2 strongswan-d45ec1dedfa06155c344f5cce0ac7b2ec331c825.tar.xz |
- implemented sa_config
- uses identification
- and host
- untested
- ts need further tuning
Diffstat (limited to 'Source/charon/utils')
-rw-r--r-- | Source/charon/utils/Makefile.utils | 4 | ||||
-rw-r--r-- | Source/charon/utils/allocator.c | 39 | ||||
-rw-r--r-- | Source/charon/utils/allocator.h | 37 | ||||
-rw-r--r-- | Source/charon/utils/identification.c | 189 | ||||
-rw-r--r-- | Source/charon/utils/identification.h | 114 |
5 files changed, 383 insertions, 0 deletions
diff --git a/Source/charon/utils/Makefile.utils b/Source/charon/utils/Makefile.utils index e66a0601f..b9c00675b 100644 --- a/Source/charon/utils/Makefile.utils +++ b/Source/charon/utils/Makefile.utils @@ -42,3 +42,7 @@ $(BUILD_DIR)randomizer.o : $(UTILS_DIR)randomizer.c $(UTILS_DIR)randomizer.h OBJS+= $(BUILD_DIR)tester.o $(BUILD_DIR)tester.o : $(UTILS_DIR)tester.c $(UTILS_DIR)tester.h $(CC) $(CFLAGS) -c -o $@ $< + +OBJS+= $(BUILD_DIR)identification.o +$(BUILD_DIR)identification.o : $(UTILS_DIR)identification.c $(UTILS_DIR)identification.h + $(CC) $(CFLAGS) -c -o $@ $< diff --git a/Source/charon/utils/allocator.c b/Source/charon/utils/allocator.c index 46194e85f..8c314a88a 100644 --- a/Source/charon/utils/allocator.c +++ b/Source/charon/utils/allocator.c @@ -264,6 +264,25 @@ static void * clone_bytes(allocator_t *allocator,void * to_clone, size_t bytes, return new_space; } + +/** + * Implementation of allocator_t.clone_chunk. + */ +static chunk_t clone_chunk(allocator_t *allocator, chunk_t chunk, char * file, int line) +{ + private_allocator_t *this = (private_allocator_t *) allocator; + chunk_t clone = CHUNK_INITIALIZER; + + if (chunk.ptr && chunk.len > 0) + { + clone.ptr = this->allocate_special(this,chunk.len,file,line,TRUE); + clone.len = chunk.len; + memcpy(clone.ptr, chunk.ptr, chunk.len); + } + + return clone; +} + /** * Implementation of allocator_t.allocator_report_memory_leaks. */ @@ -305,6 +324,7 @@ static private_allocator_t allocator = { free_pointer: free_pointer, reallocate: reallocate, clone_bytes : clone_bytes, + clone_chunk : clone_chunk, report_memory_leaks: allocator_report_memory_leaks}, allocations: NULL, allocate_special : allocate_special, @@ -356,6 +376,25 @@ void * allocator_clone_bytes(void * pointer, size_t size) return (data); } + +/** + * Described in header + */ +static chunk_t clone_chunk(chunk_t chunk) +{ + chunk_t clone = CHUNK_INITIALIZER; + + if (chunk.ptr && chunk.len > 0) + { + clone.ptr = malloc(chunk.len); + if (clone.ptr == NULL) {exit(-1)}; + clone.len = chunk.len; + memcpy(clone.ptr, chunk.ptr, chunk.len); + } + + return clone; +} + /* * Described in header */ diff --git a/Source/charon/utils/allocator.h b/Source/charon/utils/allocator.h index f27610a16..a86311789 100644 --- a/Source/charon/utils/allocator.h +++ b/Source/charon/utils/allocator.h @@ -133,6 +133,22 @@ * - NULL if out of ressources */ void * (*clone_bytes) (allocator_t *this,void * to_clone, size_t bytes, char * file, int line); + + /** + * Clones a chunk with LEAK_DETECTION and returns a cloned chunk. + * + * @warning Use this function not directly, only with assigned macro + * #allocator_clone_chunk- + * + * @param this allocator_t object + * @param chunk chunk to clone + * @param file filename from which the memory is allocated + * @param line line number in specific file + * @return + * - pointer to reallocated memory area if successful + * - NULL if out of ressources + */ + chunk_t (*clone_chunk) (allocator_t *this, chunk_t chunk, char * file, int line); /** * Frees memory with LEAK_DETECTION. @@ -201,6 +217,16 @@ */ #define allocator_clone_bytes(old,bytes) (global_allocator->clone_bytes(global_allocator,old,bytes,__FILE__, __LINE__)) + + /** + * Macro to clone a chunk and its contents + * + * See #allocator_t.clone_chunk for description. + * + * @ingroup utils + */ + #define allocator_clone_chunk(chunk) (global_allocator->clone_chunk(global_allocator,chunk,__FILE__, __LINE__)) + /** * Macro to free some memory. * @@ -266,6 +292,17 @@ void * allocator_clone_bytes(void * pointer, size_t size); /** + * Clone a chunk and its contents. + * + * + * @param chunk chunk to clone + * @return cloned chunk + * + * @ingroup utils + */ + chunk_t allocator_clone_bytes(chunk_t chunk); + + /** * Frees memory used by chunk. * * @param chunk pointer of chunk to free diff --git a/Source/charon/utils/identification.c b/Source/charon/utils/identification.c new file mode 100644 index 000000000..270d96232 --- /dev/null +++ b/Source/charon/utils/identification.c @@ -0,0 +1,189 @@ +/** + * @file identification.c + * + * @brief Implementation of identification_t. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +#include "identification.h" + +#include <utils/allocator.h> + + +typedef struct private_identification_t private_identification_t; + +/** + * Private data of an identification_t object. + */ +struct private_identification_t { + /** + * Public interface. + */ + identification_t public; + + /** + * string representation of this id + */ + char *string; + + /** + * encoded representation of this id + */ + chunk_t encoded; + + /** + * type of this id + */ + id_type_t type; +}; + +/** + * implements identification_t.get_encoding + */ +static chunk_t get_encoding(private_identification_t *this) +{ + return this->encoded; +} + +/** + * implements identification_t.get_type + */ +static id_type_t get_type(private_identification_t *this) +{ + return this->type; +} + +/** + * implements identification_t.get_string + */ +static char *get_string(private_identification_t *this) +{ + return this->string; +} + +/** + * implements identification_t.destroy + */ +static void destroy(private_identification_t *this) +{ + allocator_free(this->string); + allocator_free(this->encoded.ptr); + allocator_free(this); +} + +/** + * Generic constructor used for the other twos + */ +static private_identification_t *identification_create() +{ + + private_identification_t *this = allocator_alloc_thing(private_identification_t); + + /* assign methods */ + this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding; + this->public.get_type = (id_type_t (*) (identification_t*))get_type; + this->public.get_string = (char* (*) (identification_t*))get_string; + this->public.destroy = (void (*) (identification_t*))destroy; + + this->string = NULL; + this->encoded = CHUNK_INITIALIZER; + + return this; +} + +/* + * Described in header. + */ +identification_t *identification_create_from_string(id_type_t type, char *string) +{ + private_identification_t *this = identification_create(); + switch (type) + { + case ID_IPV4_ADDR: + { + /* convert string */ + this->encoded.len = 4; + this->encoded.ptr = allocator_alloc(this->encoded.len); + if (inet_aton(string, ((struct in_addr*)(this->encoded.ptr))) == 0) + { + allocator_free(this->encoded.ptr); + allocator_free(this); + return NULL; + } + /* clone string */ + this->string = allocator_alloc(strlen(string)+1); + strcpy(this->string, string); + return &(this->public); + } + case ID_IPV6_ADDR: + case ID_FQDN: + case ID_RFC822_ADDR: + case ID_DER_ASN1_DN: + case ID_DER_ASN1_GN: + case ID_KEY_ID: + default: + { + /* not supported */ + allocator_free(this); + return NULL; + } + } +} + +/* + * Described in header. + */ +identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded) +{ + private_identification_t *this = identification_create(); + switch (type) + { + case ID_IPV4_ADDR: + { + char *tmp; + /* clone chunk */ + if (encoded.len != 4) + { + allocator_free(this); + return NULL; + } + this->encoded = allocator_clone_chunk(encoded); + tmp = inet_ntoa(*((struct in_addr*)(encoded.ptr))); + /* build string, must be cloned */ + this->string = allocator_alloc(strlen(tmp)+1); + strcpy(this->string, tmp); + return &(this->public); + } + case ID_IPV6_ADDR: + case ID_FQDN: + case ID_RFC822_ADDR: + case ID_DER_ASN1_DN: + case ID_DER_ASN1_GN: + case ID_KEY_ID: + default: + { + /* not supported */ + allocator_free(this); + return NULL; + } + } +} diff --git a/Source/charon/utils/identification.h b/Source/charon/utils/identification.h new file mode 100644 index 000000000..54e4e02da --- /dev/null +++ b/Source/charon/utils/identification.h @@ -0,0 +1,114 @@ +/** + * @file identification.h + * + * @brief Interface of identification_t. + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + + +#ifndef _IDENTIFICATION_H_ +#define _IDENTIFICATION_H_ + + +#include "types.h" + +#include <encoding/payloads/id_payload.h> + +typedef struct identification_t identification_t; + +/** + * @brief Generic identification, such as used in ID payload. + * + * The following types are possible: + * + * - ID_IPV4_ADDR + * - ID_FQDN (not implemented) + * - ID_RFC822_ADDR (not implemented) + * - ID_IPV6_ADDR (not implemented) + * - ID_DER_ASN1_DN (not implemented) + * - ID_DER_ASN1_GN (not implemented) + * - ID_KEY_ID (not implemented) + * + * @ingroup sa + */ +struct identification_t { + + /** + * @brief Get the encoding of this id, to send over + * the network. + * + * @warning Result points to internal data, do NOT free! + * + * @param this the identification_t_object + * @return a chunk containing the encoded bytes + */ + chunk_t (*get_encoding) (identification_t *this); + + /** + * @brief Get the type of this identification. + * + * @param this the identification_t_object + * @return id_type_t + */ + id_type_t (*get_type) (identification_t *this); + + /** + * @brief Get a string representation of this id. + * + * @warning Result points to internal data, do NOT free! + * + * @param this the identification_t_object + * @return string + */ + char *(*get_string) (identification_t *this); + + /** + * @brief Destroys a identification_t object. + * + * @param this identification_t object + */ + void (*destroy) (identification_t *this); +}; + +/** + * @brief Creates an identification_t object from a string. + * + * @param type type of this id, such as ID_IPV4_ADDR or ID_RFC822_ADDR + * @param string input string, which will be converted + * @return - created identification_t object, or + * - NULL if type not supported. + * + * @ingroup sa + */ +identification_t * identification_create_from_string(id_type_t type, char *string); + + +/** + * @brief Creates an identification_t object from an encoded chunk. + * + * @param type type of this id, such as ID_IPV4_ADDR or ID_RFC822_ADDR + * @param encoded encoded bytes, such as from identification_t.get_encoding + * @return - created identification_t object, or + * - NULL if type not supported. + * + * @ingroup sa + */ +identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded); + + +#endif //_IDENTIFICATION_H_ |