diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/charon/config/sa_config.c | 8 | ||||
-rw-r--r-- | Source/charon/config/sa_config.h | 6 | ||||
-rw-r--r-- | Source/charon/config/traffic_selector.c | 19 | ||||
-rw-r--r-- | Source/charon/config/traffic_selector.h | 48 | ||||
-rw-r--r-- | Source/charon/encoding/payloads/traffic_selector_substructure.c | 67 | ||||
-rw-r--r-- | Source/charon/encoding/payloads/traffic_selector_substructure.h | 51 | ||||
-rw-r--r-- | Source/charon/encoding/payloads/ts_payload.c | 50 | ||||
-rw-r--r-- | Source/charon/encoding/payloads/ts_payload.h | 31 |
8 files changed, 223 insertions, 57 deletions
diff --git a/Source/charon/config/sa_config.c b/Source/charon/config/sa_config.c index f306127a0..51fc94a1e 100644 --- a/Source/charon/config/sa_config.c +++ b/Source/charon/config/sa_config.c @@ -96,7 +96,7 @@ static auth_method_t get_auth_method(private_sa_config_t *this) /** * implements sa_config_t.get_traffic_selectors */ -static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_t ***traffic_selectors) +static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_t **traffic_selectors[]) { iterator_t *iterator; traffic_selector_t *current_ts; @@ -118,7 +118,7 @@ static size_t get_traffic_selectors(private_sa_config_t *this, traffic_selector_ /** * implements sa_config_t.select_traffic_selectors */ -static size_t select_traffic_selectors(private_sa_config_t *this, traffic_selector_t **supplied, size_t count, traffic_selector_t ***selected) +static size_t select_traffic_selectors(private_sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[]) { iterator_t *iterator; traffic_selector_t *current_ts; @@ -307,8 +307,8 @@ sa_config_t *sa_config_create(id_type_t my_id_type, char *my_id, id_type_t other this->public.get_my_id = (identification_t*(*)(sa_config_t*))get_my_id; this->public.get_other_id = (identification_t*(*)(sa_config_t*))get_other_id; this->public.get_auth_method = (auth_method_t(*)(sa_config_t*))get_auth_method; - this->public.get_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t***))get_traffic_selectors; - this->public.select_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t**,size_t,traffic_selector_t***))select_traffic_selectors; + this->public.get_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t**[]))get_traffic_selectors; + this->public.select_traffic_selectors = (size_t(*)(sa_config_t*,traffic_selector_t*[],size_t,traffic_selector_t**[]))select_traffic_selectors; this->public.get_proposals = (size_t(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t**))get_proposals; this->public.select_proposal = (child_proposal_t*(*)(sa_config_t*,u_int8_t[4],u_int8_t[4],child_proposal_t*,size_t))select_proposal; this->public.add_traffic_selector = (void(*)(sa_config_t*,traffic_selector_t*))add_traffic_selector; diff --git a/Source/charon/config/sa_config.h b/Source/charon/config/sa_config.h index 490395682..b3fb41a67 100644 --- a/Source/charon/config/sa_config.h +++ b/Source/charon/config/sa_config.h @@ -125,7 +125,7 @@ struct sa_config_t { * @param[out]traffic_selectors pointer where traffic selectors will be allocated * @return number of returned traffic selectors */ - size_t (*get_traffic_selectors) (sa_config_t *this, traffic_selector_t ***traffic_selectors); + size_t (*get_traffic_selectors) (sa_config_t *this, traffic_selector_t **traffic_selectors[]); /** * @brief Select traffic selectors from a supplied list. @@ -142,7 +142,7 @@ struct sa_config_t { * @param[out]traffic_selectors pointer where selected traffic selectors will be allocated * @return number of selected traffic selectors */ - size_t (*select_traffic_selectors) (sa_config_t *this, traffic_selector_t **supplied, size_t count, traffic_selector_t ***selected); + size_t (*select_traffic_selectors) (sa_config_t *this, traffic_selector_t *supplied[], size_t count, traffic_selector_t **selected[]); /** * @brief Get the list of proposals for this config. @@ -153,7 +153,7 @@ struct sa_config_t { * @param[out]traffic_selectors pointer where proposals will be allocated * @return number of allocated proposals */ - size_t (*get_proposals) (sa_config_t *this, u_int8_t ah_spi[4], u_int8_t esp_spi[4], child_proposal_t **proposals); + size_t (*get_proposals) (sa_config_t *this, u_int8_t ah_spi[4], u_int8_t esp_spi[4], child_proposal_t *proposals[]); /** * @brief Select a proposal from a supplied list diff --git a/Source/charon/config/traffic_selector.c b/Source/charon/config/traffic_selector.c index 3a95c65c6..e0744899b 100644 --- a/Source/charon/config/traffic_selector.c +++ b/Source/charon/config/traffic_selector.c @@ -25,6 +25,7 @@ #include <utils/linked_list.h> #include <utils/allocator.h> #include <utils/identification.h> +#include <arpa/inet.h> typedef struct private_traffic_selector_t private_traffic_selector_t; @@ -198,6 +199,22 @@ static u_int16_t get_to_port(private_traffic_selector_t *this) } /** + * Implements traffic_selector_t.get_type. + */ +static ts_type_t get_type(private_traffic_selector_t *this) +{ + return this->type; +} + +/** + * Implements traffic_selector_t.get_protocol. + */ +static u_int8_t get_protocol(private_traffic_selector_t *this) +{ + return this->protocol; +} + +/** * Implements traffic_selector_t.clone. */ static traffic_selector_t *clone(private_traffic_selector_t *this) @@ -316,6 +333,8 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts this->public.get_to_address = (chunk_t(*)(traffic_selector_t*))get_to_address; this->public.get_from_port = (u_int16_t(*)(traffic_selector_t*))get_from_port; this->public.get_to_port = (u_int16_t(*)(traffic_selector_t*))get_to_port; + this->public.get_type = (ts_type_t(*)(traffic_selector_t*))get_type; + this->public.get_protocol = (u_int8_t(*)(traffic_selector_t*))get_protocol; this->public.clone = (traffic_selector_t*(*)(traffic_selector_t*))clone; this->public.destroy = (void(*)(traffic_selector_t*))destroy; diff --git a/Source/charon/config/traffic_selector.h b/Source/charon/config/traffic_selector.h index f26406481..5cda79f2a 100644 --- a/Source/charon/config/traffic_selector.h +++ b/Source/charon/config/traffic_selector.h @@ -24,7 +24,37 @@ #define _TRAFFIC_SELECTOR_H_ #include <types.h> -#include <encoding/payloads/traffic_selector_substructure.h> + +typedef enum ts_type_t ts_type_t; + +/** + * Traffic selector Types. + * + * @ingroup config + */ +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 +}; + +/** + * string mappings for ts_type_t + */ +extern mapping_t ts_type_m[]; typedef struct traffic_selector_t traffic_selector_t; @@ -110,6 +140,22 @@ struct traffic_selector_t { u_int16_t (*get_to_port) (traffic_selector_t *this); /** + * @brief Get the type of the traffic selector. + * + * @param this calling obect + * @return ts_type_t specifying the type + */ + ts_type_t (*get_type) (traffic_selector_t *this); + + /** + * @brief Get the protocol id of this ts. + * + * @param this calling obect + * @return protocol id + */ + u_int8_t (*get_protocol) (traffic_selector_t *this); + + /** * @brief Destroys the ts object * * 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_ |