some message code cleanups

This commit is contained in:
Martin Willi 2006-06-23 14:00:15 +00:00
parent eecb2da87d
commit 2891590b05
3 changed files with 46 additions and 38 deletions

View File

@ -710,6 +710,20 @@ static chunk_t get_packet_data (private_message_t *this)
return chunk_clone(this->packet->get_data(this->packet));
}
/**
* Implementation of message_t.is_encoded.
*/
static bool is_encoded(private_message_t *this)
{
chunk_t data = this->packet->get_data(this->packet);
if (data.ptr == NULL)
{
return FALSE;
}
return TRUE;
}
/**
* Implementation of message_t.parse_header.
*/
@ -1232,6 +1246,7 @@ message_t *message_create_from_packet(packet_t *packet)
this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
this->public.is_encoded = (bool (*) (message_t *this)) is_encoded;
this->public.destroy = (void(*)(message_t*))destroy;
/* private values */
@ -1272,24 +1287,3 @@ message_t *message_create()
{
return message_create_from_packet(NULL);
}
/*
* Described in Header.
*/
message_t *message_create_notify_reply(host_t *source, host_t *destination, exchange_type_t exchange_type, bool original_initiator,ike_sa_id_t *ike_sa_id,notify_message_type_t notify_type)
{
message_t *message = message_create_from_packet(NULL);
notify_payload_t *payload;
message->set_source(message, source->clone(source));
message->set_destination(message, destination->clone(destination));
message->set_exchange_type(message, exchange_type);
message->set_request(message, FALSE);
message->set_message_id(message,0);
message->set_ike_sa_id(message, ike_sa_id);
payload = notify_payload_create_from_protocol_and_type(PROTO_NONE, notify_type);
message->add_payload(message,(payload_t *) payload);
return message;
}

View File

@ -306,7 +306,7 @@ struct message_t {
iterator_t * (*get_payload_iterator) (message_t *this);
/**
* Returns a clone of the internal stored packet_t object.
* @brief Returns a clone of the internal stored packet_t object.
*
* @param this message_t object
* @return packet_t object as clone of internal one
@ -314,13 +314,25 @@ struct message_t {
packet_t * (*get_packet) (message_t *this);
/**
* Returns a clone of the internal stored packet_t data.
* @brief Returns a clone of the internal stored packet_t data.
*
* @param this message_t object
* @return clone of the internal stored packet_t data.
*/
chunk_t (*get_packet_data) (message_t *this);
/**
* @brief Check if a message is encoded.
*
* Check if the packet is in a generated (and encrypted) form available
* and can be passed down to the socket. If not, it has to be generated
* first.
*
* @param this message_t object
* @return TRUE if encoded, FALSE if not
*/
bool (*is_encoded) (message_t *this);
/**
* @brief Destroys a message and all including objects.
@ -364,13 +376,4 @@ message_t * message_create_from_packet(packet_t *packet);
*/
message_t * message_create(void);
/**
* @brief Creates an message_t object of type reply containing a notify payload.
*
* @return message_t object
*
* @ingroup encoding
*/
message_t *message_create_notify_reply(host_t *source, host_t *destination, exchange_type_t exchange_type, bool original_initiator,ike_sa_id_t *ike_sa_id,notify_message_type_t notify_type);
#endif /*MESSAGE_H_*/

View File

@ -89,15 +89,26 @@ static status_t execute(private_incoming_packet_job_t *this)
message->get_minor_version(message));
if ((message->get_exchange_type(message) == IKE_SA_INIT) && (message->get_request(message)))
{
notify_payload_t *notify;
message_t *response;
host_t *src, *dst;
message->get_ike_sa_id(message, &ike_sa_id);
ike_sa_id->switch_initiator(ike_sa_id);
response = message_create_notify_reply(message->get_destination(message),
message->get_source(message),
IKE_SA_INIT, FALSE, ike_sa_id,
INVALID_MAJOR_VERSION);
message->destroy(message);
ike_sa_id->destroy(ike_sa_id);
response = message_create();
src = message->get_source(message);
dst = message->get_destination(message);
response->set_source(response, src->clone(src));
response->set_destination(response, dst->clone(dst));
response->set_exchange_type(response, IKE_SA_INIT);
response->set_request(response, FALSE);
response->set_message_id(response, 0);
response->set_ike_sa_id(response, ike_sa_id);
notify = notify_payload_create_from_protocol_and_type(PROTO_NONE, INVALID_MAJOR_VERSION);
response->add_payload(response, (payload_t *)notify);
status = response->generate(response, NULL, NULL, &packet);
if (status != SUCCESS)
{