aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Hutter <jhutter@hsr.ch>2005-11-21 13:44:16 +0000
committerJan Hutter <jhutter@hsr.ch>2005-11-21 13:44:16 +0000
commitb9459040889b6f8617afddbaf1ce324dcda196b9 (patch)
treefaa12c3d831496aecaa6560e50ac73a0e3bb00e8
parent75ab06685ffdf823027c6249f4622a91252ca549 (diff)
downloadstrongswan-b9459040889b6f8617afddbaf1ce324dcda196b9.tar.bz2
strongswan-b9459040889b6f8617afddbaf1ce324dcda196b9.tar.xz
- added clone functionality
-rw-r--r--Source/charon/payloads/proposal_substructure.c126
-rw-r--r--Source/charon/payloads/proposal_substructure.h11
-rw-r--r--Source/charon/payloads/transform_attribute.c60
-rw-r--r--Source/charon/payloads/transform_attribute.h11
-rw-r--r--Source/charon/payloads/transform_substructure.c106
-rw-r--r--Source/charon/payloads/transform_substructure.h12
6 files changed, 260 insertions, 66 deletions
diff --git a/Source/charon/payloads/proposal_substructure.c b/Source/charon/payloads/proposal_substructure.c
index 32c856744..757ce9ad7 100644
--- a/Source/charon/payloads/proposal_substructure.c
+++ b/Source/charon/payloads/proposal_substructure.c
@@ -170,34 +170,6 @@ static status_t verify(private_proposal_substructure_t *this)
}
/**
- * Implements payload_t's and proposal_substructure_t's destroy function.
- * See #payload_s.destroy or proposal_substructure_s.destroy for description.
- */
-static status_t destroy(private_proposal_substructure_t *this)
-{
- /* all proposals are getting destroyed */
- while (this->transforms->get_count(this->transforms) > 0)
- {
- transform_substructure_t *current_transform;
- if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
- {
- break;
- }
- current_transform->destroy(current_transform);
- }
- this->transforms->destroy(this->transforms);
-
- if (this->spi.ptr != NULL)
- {
- allocator_free(this->spi.ptr);
- }
-
- allocator_free(this);
-
- return SUCCESS;
-}
-
-/**
* Implements payload_t's get_encoding_rules function.
* See #payload_s.get_encoding_rules for description.
*/
@@ -386,6 +358,103 @@ static status_t compute_length (private_proposal_substructure_t *this)
return SUCCESS;
}
+/**
+ * Implements proposal_substructure_t's clone function.
+ * See #proposal_substructure_s.clone for description.
+ */
+static status_t clone(private_proposal_substructure_t *this, private_proposal_substructure_t **clone)
+{
+ private_proposal_substructure_t * new_clone;
+ linked_list_iterator_t *transforms;
+ status_t status;
+
+ new_clone = (private_proposal_substructure_t *) proposal_substructure_create();
+
+ new_clone->next_payload = this->next_payload;
+ new_clone->proposal_number = this->proposal_number;
+ new_clone->protocol_id = this->protocol_id;
+ new_clone->spi_size = this->spi_size;
+ if (this->spi.ptr != NULL)
+ {
+ new_clone->spi.ptr = allocator_clone_bytes(this->spi.ptr,this->spi.len);
+ if (new_clone->spi.ptr == NULL)
+ {
+ new_clone->public.destroy(&(new_clone->public));
+ return OUT_OF_RES;
+ }
+ new_clone->spi.len = this->spi.len;
+ }
+
+ status = this->transforms->create_iterator(this->transforms,&transforms,FALSE);
+ if (status != SUCCESS)
+ {
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+
+ while (transforms->has_next(transforms))
+ {
+ transform_substructure_t *current_transform;
+ transform_substructure_t *current_transform_clone;
+ status = transforms->current(transforms,(void **) &current_transform);
+ if (status != SUCCESS)
+ {
+ transforms->destroy(transforms);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+ status = current_transform->clone(current_transform,&current_transform_clone);
+ if (status != SUCCESS)
+ {
+ transforms->destroy(transforms);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+
+ status = new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
+ if (status != SUCCESS)
+ {
+ transforms->destroy(transforms);
+ current_transform_clone->destroy(current_transform_clone);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+ }
+
+ transforms->destroy(transforms);
+
+ *clone = new_clone;
+
+ return SUCCESS;
+}
+
+/**
+ * Implements payload_t's and proposal_substructure_t's destroy function.
+ * See #payload_s.destroy or proposal_substructure_s.destroy for description.
+ */
+static status_t destroy(private_proposal_substructure_t *this)
+{
+ /* all proposals are getting destroyed */
+ while (this->transforms->get_count(this->transforms) > 0)
+ {
+ transform_substructure_t *current_transform;
+ if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
+ {
+ break;
+ }
+ current_transform->destroy(current_transform);
+ }
+ this->transforms->destroy(this->transforms);
+
+ if (this->spi.ptr != NULL)
+ {
+ allocator_free(this->spi.ptr);
+ }
+
+ allocator_free(this);
+
+ return SUCCESS;
+}
/*
* Described in header
@@ -416,6 +485,7 @@ proposal_substructure_t *proposal_substructure_create()
this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
this->public.set_spi = (status_t (*) (proposal_substructure_t *,chunk_t))set_spi;
this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
+ this->public.clone = (status_t (*) (proposal_substructure_t *, proposal_substructure_t **)) clone;
this->public.destroy = (status_t (*) (proposal_substructure_t *)) destroy;
/* private functions */
diff --git a/Source/charon/payloads/proposal_substructure.h b/Source/charon/payloads/proposal_substructure.h
index c8bd2c4c5..fae988cce 100644
--- a/Source/charon/payloads/proposal_substructure.h
+++ b/Source/charon/payloads/proposal_substructure.h
@@ -151,6 +151,17 @@ struct proposal_substructure_s {
status_t (*set_spi) (proposal_substructure_t *this, chunk_t spi);
/**
+ * @brief Clones an proposal_substructure_t object.
+ *
+ * @param this proposal_substructure_t object to clone
+ * @param clone cloned object will be written there
+ * @return
+ * - SUCCESS
+ * - OUT_OF_RES
+ */
+ status_t (*clone) (proposal_substructure_t *this,proposal_substructure_t **clone);
+
+ /**
* @brief Destroys an proposal_substructure_t object.
*
* @param this proposal_substructure_t object to destroy
diff --git a/Source/charon/payloads/transform_attribute.c b/Source/charon/payloads/transform_attribute.c
index 275f85857..3aa1af7da 100644
--- a/Source/charon/payloads/transform_attribute.c
+++ b/Source/charon/payloads/transform_attribute.c
@@ -123,21 +123,6 @@ static status_t verify(private_transform_attribute_t *this)
}
/**
- * Implements payload_t's and transform_attribute_t's destroy function.
- * See #payload_s.destroy or transform_attribute_s.destroy for description.
- */
-static status_t destroy(private_transform_attribute_t *this)
-{
- if (this->attribute_value.ptr != NULL)
- {
- allocator_free(this->attribute_value.ptr);
- }
- allocator_free(this);
-
- return SUCCESS;
-}
-
-/**
* Implements payload_t's get_encoding_rules function.
* See #payload_s.get_encoding_rules for description.
*/
@@ -292,6 +277,50 @@ static u_int16_t get_attribute_type (private_transform_attribute_t *this)
return this->attribute_type;
}
+/**
+ * Implements transform_attribute_t's clone function.
+ * See transform_attribute_s.clone for description.
+ */
+static status_t clone(private_transform_attribute_t *this,transform_attribute_t **clone)
+{
+ private_transform_attribute_t *new_clone;
+
+ new_clone = (private_transform_attribute_t *) transform_attribute_create();
+
+ new_clone->attribute_format = this->attribute_format;
+ new_clone->attribute_type = this->attribute_type;
+ new_clone->attribute_length_or_value = this->attribute_length_or_value;
+
+ if (!new_clone->attribute_format)
+ {
+ new_clone->attribute_value.ptr = allocator_clone_bytes(this->attribute_value.ptr,this->attribute_value.len);
+ new_clone->attribute_value.len = this->attribute_value.len;
+ if (new_clone->attribute_value.ptr == NULL)
+ {
+ new_clone->public.destroy(&(new_clone->public));
+ return OUT_OF_RES;
+ }
+ }
+
+ *clone = (transform_attribute_t *) new_clone;
+ return SUCCESS;
+}
+
+/**
+ * Implements payload_t's and transform_attribute_t's destroy function.
+ * See #payload_s.destroy or transform_attribute_s.destroy for description.
+ */
+static status_t destroy(private_transform_attribute_t *this)
+{
+ if (this->attribute_value.ptr != NULL)
+ {
+ allocator_free(this->attribute_value.ptr);
+ }
+ allocator_free(this);
+
+ return SUCCESS;
+}
+
/*
* Described in header
*/
@@ -319,6 +348,7 @@ transform_attribute_t *transform_attribute_create()
this->public.get_value = (u_int16_t (*) (transform_attribute_t *)) get_value;
this->public.set_attribute_type = (status_t (*) (transform_attribute_t *,u_int16_t type)) set_attribute_type;
this->public.get_attribute_type = (u_int16_t (*) (transform_attribute_t *)) get_attribute_type;
+ this->public.clone = (status_t (*) (transform_attribute_t *,transform_attribute_t **)) clone;
this->public.destroy = (status_t (*) (transform_attribute_t *)) destroy;
/* set default values of the fields */
diff --git a/Source/charon/payloads/transform_attribute.h b/Source/charon/payloads/transform_attribute.h
index 0dea2b7b2..6f7af7543 100644
--- a/Source/charon/payloads/transform_attribute.h
+++ b/Source/charon/payloads/transform_attribute.h
@@ -120,6 +120,17 @@ struct transform_attribute_s {
u_int16_t (*get_attribute_type) (transform_attribute_t *this);
/**
+ * @brief Clones an transform_attribute_t object.
+ *
+ * @param this transform_attribute_t object to clone
+ * @param clone the new clone will be written there
+ * @return
+ * - OUT_OF_RES
+ * - SUCCESS
+ */
+ status_t (*clone) (transform_attribute_t *this,transform_attribute_t **clone);
+
+ /**
* @brief Destroys an transform_attribute_t object.
*
* @param this transform_attribute_t object to destroy
diff --git a/Source/charon/payloads/transform_substructure.c b/Source/charon/payloads/transform_substructure.c
index 74cb08587..207aafae6 100644
--- a/Source/charon/payloads/transform_substructure.c
+++ b/Source/charon/payloads/transform_substructure.c
@@ -289,29 +289,6 @@ static status_t verify(private_transform_substructure_t *this)
}
/**
- * Implements payload_t's and transform_substructure_t's destroy function.
- * See #payload_s.destroy or transform_substructure_s.destroy for description.
- */
-static status_t destroy(private_transform_substructure_t *this)
-{
- /* all proposals are getting destroyed */
- while (this->attributes->get_count(this->attributes) > 0)
- {
- transform_attribute_t *current_attribute;
- if (this->attributes->remove_last(this->attributes,(void **)&current_attribute) != SUCCESS)
- {
- break;
- }
- current_attribute->destroy(current_attribute);
- }
- this->attributes->destroy(this->attributes);
-
- allocator_free(this);
-
- return SUCCESS;
-}
-
-/**
* Implements payload_t's get_encoding_rules function.
* See #payload_s.get_encoding_rules for description.
*/
@@ -466,6 +443,88 @@ static status_t compute_length (private_transform_substructure_t *this)
return SUCCESS;
}
+/**
+ * Implements transform_substructure_t's clone function.
+ * See transform_substructure_s.clone for description.
+ */
+static status_t clone(private_transform_substructure_t *this,transform_substructure_t **clone)
+{
+ private_transform_substructure_t *new_clone;
+ linked_list_iterator_t *attributes;
+ status_t status;
+
+ new_clone = (private_transform_substructure_t *) transform_substructure_create();
+
+ new_clone->next_payload = this->next_payload;
+ new_clone->transform_type = this->transform_type;
+ new_clone->transform_id = this->transform_id;
+
+ status = this->attributes->create_iterator(this->attributes,&attributes,FALSE);
+ if (status != SUCCESS)
+ {
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+
+ while (attributes->has_next(attributes))
+ {
+ transform_attribute_t *current_attribute;
+ transform_attribute_t *current_attribute_clone;
+ status = attributes->current(attributes,(void **) &current_attribute);
+ if (status != SUCCESS)
+ {
+ attributes->destroy(attributes);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+ status = current_attribute->clone(current_attribute,&current_attribute_clone);
+ if (status != SUCCESS)
+ {
+ attributes->destroy(attributes);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+
+ status = new_clone->public.add_transform_attribute(&(new_clone->public),current_attribute_clone);
+ if (status != SUCCESS)
+ {
+ attributes->destroy(attributes);
+ current_attribute_clone->destroy(current_attribute_clone);
+ new_clone->public.destroy(&(new_clone->public));
+ return status;
+ }
+ }
+
+ attributes->destroy(attributes);
+
+ *clone = new_clone;
+ return SUCCESS;
+}
+
+
+/**
+ * Implements payload_t's and transform_substructure_t's destroy function.
+ * See #payload_s.destroy or transform_substructure_s.destroy for description.
+ */
+static status_t destroy(private_transform_substructure_t *this)
+{
+ /* all proposals are getting destroyed */
+ while (this->attributes->get_count(this->attributes) > 0)
+ {
+ transform_attribute_t *current_attribute;
+ if (this->attributes->remove_last(this->attributes,(void **)&current_attribute) != SUCCESS)
+ {
+ break;
+ }
+ current_attribute->destroy(current_attribute);
+ }
+ this->attributes->destroy(this->attributes);
+
+ allocator_free(this);
+
+ return SUCCESS;
+}
+
/*
* Described in header
*/
@@ -495,6 +554,7 @@ transform_substructure_t *transform_substructure_create()
this->public.get_transform_type = (u_int8_t (*) (transform_substructure_t *)) get_transform_type;
this->public.set_transform_id = (status_t (*) (transform_substructure_t *,u_int16_t)) set_transform_id;
this->public.get_transform_id = (u_int16_t (*) (transform_substructure_t *)) get_transform_id;
+ this->public.clone = (status_t (*) (transform_substructure_t *,transform_substructure_t **)) clone;
this->public.destroy = (status_t (*) (transform_substructure_t *)) destroy;
/* private functions */
diff --git a/Source/charon/payloads/transform_substructure.h b/Source/charon/payloads/transform_substructure.h
index ca4f2ee3d..f4ef42261 100644
--- a/Source/charon/payloads/transform_substructure.h
+++ b/Source/charon/payloads/transform_substructure.h
@@ -264,6 +264,18 @@ struct transform_substructure_s {
u_int16_t (*get_transform_id) (transform_substructure_t *this);
/**
+ * @brief Clones an transform_substructure_t object.
+ *
+ * @param this transform_substructure_t object to clone
+ * @param clone pointer to a transform_substructure_t object pointer
+ * where the new object is stored to.
+ * @return
+ * - OUT_OF_RES
+ * - SUCCESS in any case
+ */
+ status_t (*clone) (transform_substructure_t *this,transform_substructure_t **clone);
+
+ /**
* @brief Destroys an transform_substructure_t object.
*
* @param this transform_substructure_t object to destroy