diff options
Diffstat (limited to 'Source/charon/encoding')
4 files changed, 150 insertions, 49 deletions
diff --git a/Source/charon/encoding/payloads/traffic_selector_substructure.c b/Source/charon/encoding/payloads/traffic_selector_substructure.c index d2c9af54d..e012b8f3c 100644 --- a/Source/charon/encoding/payloads/traffic_selector_substructure.c +++ b/Source/charon/encoding/payloads/traffic_selector_substructure.c @@ -82,6 +82,11 @@ struct private_traffic_selector_substructure_t { * Ending address. */ chunk_t ending_address; + + /** + * update length + */ + void (*compute_length) (private_traffic_selector_substructure_t *this); }; /** @@ -252,7 +257,7 @@ static void set_start_host (private_traffic_selector_substructure_t *this,host_t allocator_free_chunk(&(this->starting_address)); } this->starting_address = start_host->get_address_as_chunk(start_host); - this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH + this->starting_address.len + this->ending_address.len; + this->compute_length(this); } /** @@ -274,7 +279,27 @@ static void set_end_host (private_traffic_selector_substructure_t *this,host_t * allocator_free_chunk(&(this->ending_address)); } this->ending_address = end_host->get_address_as_chunk(end_host); - this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH + this->starting_address.len + this->ending_address.len; + this->compute_length(this); +} + +/** + * Implementation of traffic_selector_substructure_t.get_traffic_selector. + */ +static traffic_selector_t *get_traffic_selector(private_traffic_selector_substructure_t *this) +{ + traffic_selector_t *ts; + ts = traffic_selector_create_from_bytes(this->ip_protocol_id, this->ts_type, + this->starting_address, this->start_port, + this->ending_address, this->end_port); + return ts; +} + +/** + * Implementation of private_ts_payload_t.compute_length + */ +void compute_length(private_traffic_selector_substructure_t *this) +{ + this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH + this->ending_address.len + this->starting_address.len; } /** @@ -282,24 +307,15 @@ static void set_end_host (private_traffic_selector_substructure_t *this,host_t * */ static void destroy(private_traffic_selector_substructure_t *this) { - - if (this->starting_address.ptr != NULL) - { - allocator_free_chunk(&(this->starting_address)); - } - - if (this->ending_address.ptr != NULL) - { - allocator_free_chunk(&(this->ending_address)); - } - + allocator_free(this->starting_address.ptr); + allocator_free(this->ending_address.ptr); allocator_free(this); } /* * Described in header */ -traffic_selector_substructure_t *traffic_selector_substructure_create(bool is_initiator) +traffic_selector_substructure_t *traffic_selector_substructure_create() { private_traffic_selector_substructure_t *this = allocator_alloc_thing(private_traffic_selector_substructure_t); @@ -322,10 +338,13 @@ traffic_selector_substructure_t *traffic_selector_substructure_create(bool is_in this->public.set_start_host = (void (*) (traffic_selector_substructure_t *, host_t *))set_start_host; this->public.get_end_host = (host_t * (*) (traffic_selector_substructure_t *))get_end_host; this->public.set_end_host = (void (*) (traffic_selector_substructure_t *, host_t *))set_end_host; + this->public.get_traffic_selector = (traffic_selector_t* (*)(traffic_selector_substructure_t*))get_traffic_selector; + /* private functions */ + this->compute_length = compute_length; /* private variables */ - this->payload_length =TRAFFIC_SELECTOR_HEADER_LENGTH; + this->payload_length = TRAFFIC_SELECTOR_HEADER_LENGTH; this->start_port = 0; this->end_port = 0; this->starting_address = CHUNK_INITIALIZER; @@ -336,3 +355,21 @@ traffic_selector_substructure_t *traffic_selector_substructure_create(bool is_in return (&(this->public)); } + +/* + * Described in header + */ +traffic_selector_substructure_t *traffic_selector_substructure_create_from_traffic_selector(traffic_selector_t *traffic_selector) +{ + private_traffic_selector_substructure_t *this = (private_traffic_selector_substructure_t*)traffic_selector_substructure_create(); + this->ts_type = traffic_selector->get_type(traffic_selector); + this->ip_protocol_id = traffic_selector->get_protocol(traffic_selector); + this->start_port = traffic_selector->get_from_port(traffic_selector); + this->end_port = traffic_selector->get_to_port(traffic_selector); + this->starting_address = traffic_selector->get_from_address(traffic_selector); + this->ending_address = traffic_selector->get_to_address(traffic_selector); + + this->compute_length(this); + + return &(this->public); +} diff --git a/Source/charon/encoding/payloads/traffic_selector_substructure.h b/Source/charon/encoding/payloads/traffic_selector_substructure.h index 43e697415..0c95cb95b 100644 --- a/Source/charon/encoding/payloads/traffic_selector_substructure.h +++ b/Source/charon/encoding/payloads/traffic_selector_substructure.h @@ -27,6 +27,7 @@ #include <types.h> #include <encoding/payloads/payload.h> #include <network/host.h> +#include <config/traffic_selector.h> /** * Length of a TRAFFIC SELECTOR SUBSTRUCTURE without start and end address. @@ -35,35 +36,6 @@ */ #define TRAFFIC_SELECTOR_HEADER_LENGTH 8 - -typedef enum ts_type_t ts_type_t; - -/** - * Traffic selector Types. - * - * @ingroup payloads - */ -enum ts_type_t { - /* - * A range of IPv4 addresses, represented by two four (4) octet - * values. The first value is the beginning IPv4 address - * (inclusive) and the second value is the ending IPv4 address - * (inclusive). All addresses falling between the two specified - * addresses are considered to be within the list. - */ - TS_IPV4_ADDR_RANGE = 7, - /* - * A range of IPv6 addresses, represented by two sixteen (16) - * octet values. The first value is the beginning IPv6 address - * (inclusive) and the second value is the ending IPv6 address - * (inclusive). All addresses falling between the two specified - * addresses are considered to be within the list. - */ - TS_IPV6_ADDR_RANGE = 8 -}; - -extern mapping_t ts_type_m[]; - typedef struct traffic_selector_substructure_t traffic_selector_substructure_t; /** @@ -153,6 +125,16 @@ struct traffic_selector_substructure_t { void (*set_end_host) (traffic_selector_substructure_t *this,host_t *end_host); /** + * @brief Get a traffic_selector_t from this substructure. + * + * @warning traffic_selector_t must be destroyed after usage. + * + * @param this calling traffic_selector_substructure_t object + * @return contained traffic_selector_t + */ + traffic_selector_t *(*get_traffic_selector) (traffic_selector_substructure_t *this); + + /** * @brief Destroys an traffic_selector_substructure_t object. * * @param this traffic_selector_substructure_t object to destroy @@ -171,5 +153,16 @@ struct traffic_selector_substructure_t { */ traffic_selector_substructure_t *traffic_selector_substructure_create(); +/** + * @brief Creates an initialized traffif selector substructure using + * the values from a traffic_selector_t. + * + * @param traffic_selector traffic_selector_t to use for initialization + * @return created traffic_selector_substructure_t object + * + * @ingroup payloads + */ +traffic_selector_substructure_t *traffic_selector_substructure_create_from_traffic_selector(traffic_selector_t *traffic_selector); + #endif //TRAFFIC_SELECTOR_SUBSTRUCTURE_H_ diff --git a/Source/charon/encoding/payloads/ts_payload.c b/Source/charon/encoding/payloads/ts_payload.c index 2e7df5b6d..6153e9504 100644 --- a/Source/charon/encoding/payloads/ts_payload.c +++ b/Source/charon/encoding/payloads/ts_payload.c @@ -225,7 +225,7 @@ static void set_initiator (private_ts_payload_t *this,bool is_initiator) static void add_traffic_selector_substructure (private_ts_payload_t *this,traffic_selector_substructure_t *traffic_selector) { this->traffic_selectors->insert_last(this->traffic_selectors,traffic_selector); - this->number_of_traffic_selectors= this->traffic_selectors->get_count(this->traffic_selectors); + this->number_of_traffic_selectors = this->traffic_selectors->get_count(this->traffic_selectors); } /** @@ -236,6 +236,28 @@ static iterator_t * create_traffic_selector_substructure_iterator (private_ts_pa return this->traffic_selectors->create_iterator(this->traffic_selectors,forward); } +static size_t get_traffic_selectors(private_ts_payload_t *this, traffic_selector_t **traffic_selectors[]) +{ + traffic_selector_t **ts; + iterator_t *iterator; + int i = 0; + + //ts = allocator_alloc(sizeof(traffic_selector_t*) * this->number_of_traffic_selectors); + iterator = this->traffic_selectors->create_iterator(this->traffic_selectors, TRUE); + int x = this->traffic_selectors->get_count(this->traffic_selectors); + while (iterator->has_next) + { + traffic_selector_substructure_t *ts_substructure; + iterator->current(iterator, (void**)&ts_substructure); + //ts[i] = ts_substructure->get_traffic_selector(ts_substructure); + i++; + } + + /* return values */ + //*traffic_selectors = ts; + return this->number_of_traffic_selectors; +} + /** * Implementation of private_ts_payload_t.compute_length. */ @@ -301,6 +323,7 @@ ts_payload_t *ts_payload_create(bool is_initiator) this->public.set_initiator = (void (*) (ts_payload_t *,bool)) set_initiator; this->public.add_traffic_selector_substructure = (void (*) (ts_payload_t *,traffic_selector_substructure_t *)) add_traffic_selector_substructure; this->public.create_traffic_selector_substructure_iterator = (iterator_t* (*) (ts_payload_t *,bool)) create_traffic_selector_substructure_iterator; + this->public.get_traffic_selectors = (size_t (*) (ts_payload_t *, traffic_selector_t**[])) get_traffic_selectors; /* private functions */ this->compute_length = compute_length; @@ -311,7 +334,28 @@ ts_payload_t *ts_payload_create(bool is_initiator) this->payload_length =TS_PAYLOAD_HEADER_LENGTH; this->is_initiator = is_initiator; this->number_of_traffic_selectors=0; - this->traffic_selectors = linked_list_create(); + this->traffic_selectors = linked_list_create(); + + return &(this->public); +} - return (&(this->public)); +/* + * Described in header + */ +ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator, traffic_selector_t *traffic_selectors[], size_t count) +{ + int i; + private_ts_payload_t *this; + + this = (private_ts_payload_t*)ts_payload_create(is_initiator); + + for (i = 0; i < count; i++) + { + traffic_selector_substructure_t *ts_substructure; + ts_substructure = traffic_selector_substructure_create_from_traffic_selector(traffic_selectors[i]); + this->public.add_traffic_selector_substructure(&(this->public), ts_substructure); + } + + return &(this->public); } + diff --git a/Source/charon/encoding/payloads/ts_payload.h b/Source/charon/encoding/payloads/ts_payload.h index 02cd3a410..158196a6c 100644 --- a/Source/charon/encoding/payloads/ts_payload.h +++ b/Source/charon/encoding/payloads/ts_payload.h @@ -26,6 +26,7 @@ #include <types.h> #include <utils/iterator.h> +#include <config/traffic_selector.h> #include <encoding/payloads/payload.h> #include <encoding/payloads/traffic_selector_substructure.h> @@ -100,6 +101,18 @@ struct ts_payload_t { * @return created iterator_t object */ iterator_t *(*create_traffic_selector_substructure_iterator) (ts_payload_t *this, bool forward); + + /** + * @brief Create an array of the nested traffic_selector_t's. + * + * @warning Array must be freed after usage. + * @warnging traffic selector must be destroyed after usage. + * + * @param this calling ts_payload_t object + * @param[out] address of the array of traffic_selectors will be written here. + * @return number of ts in the allocated array + */ + size_t (*get_traffic_selectors) (ts_payload_t *this, traffic_selector_t **traffic_selectors[]); /** * @brief Destroys an ts_payload_t object. @@ -110,18 +123,32 @@ struct ts_payload_t { }; /** - * @brief Creates an empty id_payload_t object. + * @brief Creates an empty ts_payload_t object. * * * @param is_initiator * - TRUE if this payload is of type TSi * - FALSE if this payload is of type TSr * - * @return created id_payload_t object + * @return created ts_payload_t object * * @ingroup payloads */ ts_payload_t *ts_payload_create(bool is_initiator); +/** + * @brief Creates ts_payload with the specified traffic_selectors. + * + * + * @param is_initiator + * - TRUE if this payload is of type TSi + * - FALSE if this payload is of type TSr + * + * @return created ts_payload_t object + * + * @ingroup payloads + */ +ts_payload_t *ts_payload_create_from_traffic_selectors(bool is_initiator, traffic_selector_t *traffic_selectors[], size_t count); + #endif //TS_PAYLOAD_H_ |