- code cleanup in network and config

- moved packet_t members to private, added getter and setters
This commit is contained in:
Martin Willi
2005-12-06 16:00:07 +00:00
parent 4b41a0d404
commit 2b54748131
28 changed files with 345 additions and 170 deletions
@@ -216,7 +216,6 @@ struct private_configuration_manager_t {
/**
* Adds a new IKE_SA configuration.
*
*
* @param this calling object
* @param name name for the configuration
* @param init_config init_config_t object
@@ -227,7 +226,6 @@ struct private_configuration_manager_t {
/**
* Adds a new preshared secret.
*
*
* @param this calling object
* @param type type of identification
* @param id_string identification as string
@@ -238,7 +236,6 @@ struct private_configuration_manager_t {
/**
* Adds a new rsa private key.
*
*
* @param this calling object
* @param type type of identification
* @param id_string identification as string
@@ -250,7 +247,6 @@ struct private_configuration_manager_t {
/**
* Adds a new rsa public key.
*
*
* @param this calling object
* @param type type of identification
* @param id_string identification as string
+21 -22
View File
@@ -38,8 +38,9 @@ typedef struct configuration_manager_t configuration_manager_t;
* @b Constructors:
* - configuration_manager_create()
*
* @ingroup config
* @todo Build a (file) backend for the configuration manager.
*
* @ingroup config
*/
struct configuration_manager_t {
@@ -150,13 +151,13 @@ struct configuration_manager_t {
* The returned preshared secret MUST NOT be destroyed cause it's managed by
* this configuration_manager_t object.
*
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] preshared_secret the preshared secret will be written there.
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] preshared_secret the preshared secret will be written there.
*
* @return
* - NOT_FOUND if no preshared secrets for specific ID could be found
* - SUCCESS
* - NOT_FOUND if no preshared secrets for specific ID could be found
* - SUCCESS
*/
status_t (*get_shared_secret) (configuration_manager_t *this, identification_t *identification, chunk_t *preshared_secret);
@@ -166,13 +167,13 @@ struct configuration_manager_t {
* The returned rsa_public_key_t object MUST NOT be destroyed cause it's managed by
* this configuration_manager_t object.
*
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] public_key the public key will be written there
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] public_key the public key will be written there
*
* @return
* - NOT_FOUND if no key is configured for specific id
* - SUCCESS
* - NOT_FOUND if no key is configured for specific id
* - SUCCESS
*/
status_t (*get_rsa_public_key) (configuration_manager_t *this, identification_t *identification, rsa_public_key_t **public_key);
@@ -182,22 +183,20 @@ struct configuration_manager_t {
* The returned rsa_private_key_t object MUST NOT be destroyed cause it's managed by
* this configuration_manager_t object.
*
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] private_key the private key will be written there
* @param this calling object
* @param identification identification_t object identifiying the ID.
* @param[out] private_key the private key will be written there
*
* @return
* - NOT_FOUND if no key is configured for specific id
* - SUCCESS
* - NOT_FOUND if no key is configured for specific id
* - SUCCESS
*/
status_t (*get_rsa_private_key) (configuration_manager_t *this, identification_t *identification, rsa_private_key_t **private_key);
/**
* Destroys a configuration_manager_t object.
* @brief Destroys a configuration_manager_t object.
*
* @param this calling object
* @return
* - SUCCESS
* @param this calling object
*/
void (*destroy) (configuration_manager_t *this);
};
@@ -208,8 +207,8 @@ struct configuration_manager_t {
* @param first_retransmit_timeout first retransmit timeout in milliseconds
* @param max_retransmit_count max number of tries to retransmitted a requests (0 for infinite)
* @param half_open_ike_sa_timeout timeout after that a half open IKE_SA gets deleted
* @return
* - pointer to created configuration_manager_t object
* @return configuration_manager_t object
*
* @ingroup config
*/
configuration_manager_t *configuration_manager_create(u_int32_t first_retransmit_timeout,u_int32_t max_retransmit_count, u_int32_t half_open_ike_sa_timeout);
+4 -4
View File
@@ -42,7 +42,7 @@ typedef struct ike_proposal_t ike_proposal_t;
struct ike_proposal_t {
/**
* Encryption algorithm.
* */
*/
encryption_algorithm_t encryption_algorithm;
/**
@@ -177,7 +177,7 @@ struct init_config_t {
status_t (*select_proposal) (init_config_t *this, ike_proposal_t *proposals, size_t proposal_count, ike_proposal_t *selected_proposal);
/**
* Destroys a init_config_t object.
* @brief Destroys a init_config_t object.
*
* @param this calling object
*/
@@ -185,9 +185,9 @@ struct init_config_t {
};
/**
* Creates a init_config_t object.
* @brief Creates a init_config_t object.
*
* @return - pointer to created init_config_t object.
* @return init_config_t object.
*
* @ingroup config
*/
+1 -1
View File
@@ -270,7 +270,7 @@ struct sa_config_t {
* @param auth_method Method of authentication
* @param ike_sa_lifetime lifetime of this IKE_SA in milliseconds. IKE_SA will be deleted
* after this lifetime!
* @return created sa_config_t
* @return sa_config_t object
*
* @ingroup config
*/
+11 -8
View File
@@ -28,7 +28,7 @@
typedef enum ts_type_t ts_type_t;
/**
* Traffic selector Types.
* Traffic selector types.
*
* @ingroup config
*/
@@ -65,6 +65,10 @@ typedef struct traffic_selector_t traffic_selector_t;
* A traffic selector defines an range of addresses
* and a range of ports.
*
* @b Constructors:
* - traffic_selector_create_from_bytes()
* - traffic_selector_create_from_string()
*
* @ingroup config
*/
struct traffic_selector_t {
@@ -158,8 +162,7 @@ struct traffic_selector_t {
/**
* @brief Destroys the ts object
*
*
* @param this calling object
* @param this calling object
*/
void (*destroy) (traffic_selector_t *this);
};
@@ -174,8 +177,8 @@ struct traffic_selector_t {
* @param to_addr end of address range as string
* @param to_port port number in host order
* @return
* - created traffic_selector_t
* - NULL if invalid address strings
* - traffic_selector_t object
* - NULL if invalid address strings/protocol
*
* @ingroup config
*/
@@ -195,9 +198,9 @@ traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_ty
* @param to_addr end of address range as string, network
* @param to_port port number, host order
* @return
* - created traffic_selector_t
* - NULL if invalid address strings
*
* - traffic_selector_t object
* - NULL if invalid address input/protocol
*
* @ingroup config
*/
traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_address, int16_t from_port, chunk_t to_address, u_int16_t to_port);
+16 -25
View File
@@ -488,11 +488,7 @@ static void add_payload(private_message_t *this, payload_t *payload)
*/
static void set_source(private_message_t *this, host_t *host)
{
if (this->packet->source != NULL)
{
this->packet->source->destroy(this->packet->source);
}
this->packet->source = host;
this->packet->set_source(this->packet, host);
}
/**
@@ -500,11 +496,8 @@ static void set_source(private_message_t *this, host_t *host)
*/
static void set_destination(private_message_t *this, host_t *host)
{
if (this->packet->destination != NULL)
{
this->packet->destination->destroy(this->packet->destination);
}
this->packet->destination = host;
this->packet->set_destination(this->packet, host);
}
/**
@@ -512,7 +505,7 @@ static void set_destination(private_message_t *this, host_t *host)
*/
static host_t* get_source(private_message_t *this)
{
return this->packet->source;
return this->packet->get_source(this->packet);
}
/**
@@ -520,7 +513,7 @@ static host_t* get_source(private_message_t *this)
*/
static host_t * get_destination(private_message_t *this)
{
return this->packet->destination;
return this->packet->get_destination(this->packet);
}
/**
@@ -542,6 +535,7 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
payload_t *payload, *next_payload;
iterator_t *iterator;
status_t status;
chunk_t packet_data;
this->logger->log(this->logger, CONTROL, "Generating message of type %s, contains %d payloads",
mapping_find(exchange_type_m,this->exchange_type),
@@ -554,11 +548,11 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
return INVALID_STATE;
}
if (this->packet->source == NULL ||
this->packet->destination == NULL)
if (this->packet->get_source(this->packet) == NULL ||
this->packet->get_destination(this->packet) == NULL)
{
this->logger->log(this->logger, ERROR|MORE, "%s not defined",
!this->packet->source ? "source" : "destination");
!this->packet->get_source(this->packet) ? "source" : "destination");
return INVALID_STATE;
}
@@ -615,12 +609,7 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
ike_header->destroy(ike_header);
/* build packet */
if (this->packet->data.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Replace last generated packet data");
allocator_free(this->packet->data.ptr);
}
generator->write_to_chunk(generator, &(this->packet->data));
generator->write_to_chunk(generator, &packet_data);
generator->destroy(generator);
/* if last payload is of type encrypted, integrity checksum if necessary */
@@ -628,13 +617,15 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
{
this->logger->log(this->logger, CONTROL | MORE, "Build signature on whole message");
encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
status = encryption_payload->build_signature(encryption_payload, this->packet->data);
status = encryption_payload->build_signature(encryption_payload, packet_data);
if (status != SUCCESS)
{
return status;
}
}
this->packet->set_data(this->packet, packet_data);
/* clone packet for caller */
*packet = this->packet->clone(this->packet);
@@ -656,7 +647,7 @@ static packet_t *get_packet (private_message_t *this)
*/
static chunk_t get_packet_data (private_message_t *this)
{
return allocator_clone_chunk(this->packet->data);
return allocator_clone_chunk(this->packet->get_data(this->packet));
}
/**
@@ -930,7 +921,7 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
/* decrypt */
encryption_payload->set_transforms(encryption_payload, crypter, signer);
this->logger->log(this->logger, CONTROL | MORE, "Verify signature of encryption payload");
status = encryption_payload->verify_signature(encryption_payload, this->packet->data);
status = encryption_payload->verify_signature(encryption_payload, this->packet->get_data(this->packet));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "encryption payload signature invalid");
@@ -1206,7 +1197,7 @@ message_t *message_create_from_packet(packet_t *packet)
this->payloads = linked_list_create();
/* parser is created from data of packet */
this->parser = parser_create(this->packet->data);
this->parser = parser_create(this->packet->get_data(this->packet));
this->logger = charon->logger_manager->create_logger(charon->logger_manager, MESSAGE, NULL);
@@ -72,7 +72,7 @@ extern mapping_t auth_method_m[];
typedef struct auth_payload_t auth_payload_t;
/**
* @brief Object representing an IKEv2 AUTH payload.
* @brief Class representing an IKEv2 AUTH payload.
*
* The AUTH payload format is described in draft section 3.8.
*
@@ -67,7 +67,7 @@ extern mapping_t cert_encoding_m[];
typedef struct cert_payload_t cert_payload_t;
/**
* Object representing an IKEv2 CERT payload.
* @brief Class representing an IKEv2 CERT payload.
*
* The CERT payload format is described in draft section 3.6.
* This is just a dummy implementation to fullfill the standards
@@ -77,6 +77,8 @@ typedef struct cert_payload_t cert_payload_t;
* @b Constructors:
* - cert_payload_create()
*
* @todo Implement setters/getters for the different certificate encodings.
*
* @ingroup payloads
*/
struct cert_payload_t {
@@ -48,6 +48,8 @@ typedef struct certreq_payload_t certreq_payload_t;
* @b Constructors:
* - certreq_payload_create()
*
* @todo Implement payload functionality.
*
* @ingroup payloads
*/
struct certreq_payload_t {
@@ -46,6 +46,8 @@ typedef struct delete_payload_t delete_payload_t;
* @b Constructors:
* - delete_payload_create()
*
* @todo Implement better setter/getters
*
* @ingroup payloads
*/
struct delete_payload_t {
@@ -44,6 +44,8 @@ typedef struct eap_payload_t eap_payload_t;
* @b Constructors:
* - eap_payload_create()
*
* @todo Implement functionality for this payload
*
* @ingroup payloads
*/
struct eap_payload_t {
@@ -186,7 +186,7 @@ struct encryption_payload_t {
/**
* @brief Creates an empty encryption_payload_t object.
*
* @returnencryption_payload_t object
* @return encryption_payload_t object
*
* @ingroup payloads
*/
@@ -92,6 +92,8 @@ typedef struct notify_payload_t notify_payload_t;
* @b Constructors:
* - notify_payload_create()
* - notify_payload_create_from_protocol_and_type()
*
* @todo Build specified constructor/getter for notify's
*
* @ingroup payloads
*/
@@ -61,7 +61,7 @@ extern mapping_t protocol_id_m[];
typedef struct proposal_substructure_t proposal_substructure_t;
/**
* Object representing an IKEv2-PROPOSAL SUBSTRUCTURE.
* @brief Class representing an IKEv2-PROPOSAL SUBSTRUCTURE.
*
* The PROPOSAL SUBSTRUCTURE format is described in RFC section 3.3.1.
*
@@ -49,7 +49,7 @@ extern mapping_t transform_attribute_type_m[];
typedef struct transform_attribute_t transform_attribute_t;
/**
* Object representing an IKEv2- TRANSFORM Attribute.
* @brief Class representing an IKEv2- TRANSFORM Attribute.
*
* The TRANSFORM ATTRIBUTE format is described in RFC section 3.3.5.
*
@@ -95,7 +95,7 @@ extern mapping_t extended_sequence_numbers_m[];
typedef struct transform_substructure_t transform_substructure_t;
/**
* Object representing an IKEv2- TRANSFORM SUBSTRUCTURE.
* @brief Class representing an IKEv2- TRANSFORM SUBSTRUCTURE.
*
* The TRANSFORM SUBSTRUCTURE format is described in RFC section 3.3.2.
*
+1 -1
View File
@@ -63,7 +63,7 @@ struct private_host_t {
/**
* implements host_t.get_sockaddr
*/
static sockaddr_t *get_sockaddr(private_host_t *this)
static sockaddr_t *get_sockaddr(private_host_t *this)
{
return &(this->address);
}
+28 -6
View File
@@ -39,11 +39,19 @@ typedef struct host_t host_t;
/**
* @brief Representates a Host
*
* Host object, identifies a host and defines some useful functions on it.
* Host object, identifies a address:port pair and defines some
* useful functions on it.
*
* @b Constructors:
* - host_create()
* - host_create_from_chunk()
*
* @todo Add IPv6 support
*
* @ingroup network
*/
struct host_t {
/**
* @brief Build a clone of this host object.
*
@@ -78,11 +86,23 @@ struct host_t {
/**
* @brief Gets the address as xfrm_address_t.
*
* This function allows the conversion to an
* xfrm_address_t, used for netlink communication
* with the kernel.
*
* @see kernel_interface_t.
*
* @param this calling object
* @return address in xfrm_address_t format
*/
xfrm_address_t (*get_xfrm_addr) (host_t *this);
/**
* @brief Gets the address as xfrm_address_t.
* @brief Gets the family of the address
*
* @param this calling object
* @return family
*/
int (*get_family) (host_t *this);
@@ -154,8 +174,8 @@ struct host_t {
* @param address string of an address, such as "152.96.193.130"
* @param port port number
* @return
* - the host_t object, or
* - NULL, when family not supported.
* - host_t object
* - NULL, if family not supported.
*
* @ingroup network
*/
@@ -170,10 +190,12 @@ host_t *host_create(int family, char *address, u_int16_t port);
* @param address address as 4 byte chunk_t in networ order
* @param port port number
* @return
* - the host_t object, or
* - NULL, when family not supported or chunk_t length not 4 bytes.
* - host_t object
* - NULL, if family not supported or chunk_t length not 4 bytes.
*
* @ingroup network
*/
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
#endif /*HOST_H_*/
+97 -20
View File
@@ -37,45 +37,116 @@ struct private_packet_t {
* Public part of a packet_t object.
*/
packet_t public;
/**
* source address
*/
host_t *source;
/**
* destination address
*/
host_t *destination;
/**
* message data
*/
chunk_t data;
};
/**
* Implements packet_t.get_source
*/
static void set_source(private_packet_t *this, host_t *source)
{
if (this->source)
{
this->source->destroy(this->source);
}
this->source = source;
}
/**
* Implements packet_t.set_destination
*/
static void set_destination(private_packet_t *this, host_t *destination)
{
if (this->destination)
{
this->destination->destroy(this->destination);
}
this->destination = destination;
}
/**
* Implements packet_t.get_source
*/
static host_t *get_source(private_packet_t *this)
{
return this->source;
}
/**
* Implements packet_t.get_destination
*/
static host_t *get_destination(private_packet_t *this)
{
return this->destination;
}
/**
* Implements packet_t.get_data
*/
static chunk_t get_data(private_packet_t *this)
{
return this->data;
}
/**
* Implements packet_t.set_data
*/
static void set_data(private_packet_t *this, chunk_t data)
{
allocator_free(this->data.ptr);
this->data = data;
}
/**
* Implements packet_t.destroy.
*/
static void destroy(private_packet_t *this)
{
if (this->public.source != NULL)
if (this->source != NULL)
{
this->public.source->destroy(this->public.source);
this->source->destroy(this->source);
}
if (this->public.destination != NULL)
if (this->destination != NULL)
{
this->public.destination->destroy(this->public.destination);
this->destination->destroy(this->destination);
}
allocator_free(this->public.data.ptr);
allocator_free(this->data.ptr);
allocator_free(this);
}
/**
* Implements packet_t.clone.
*/
static packet_t *clone (private_packet_t *this)
static packet_t *clone(private_packet_t *this)
{
packet_t *other;
other = packet_create();
private_packet_t *other = (private_packet_t*)packet_create();
if (this->public.destination != NULL)
if (this->destination != NULL)
{
other->destination = this->public.destination->clone(this->public.destination);
other->destination = this->destination->clone(this->destination);
}
else
{
other->destination = NULL;
}
if (this->public.source != NULL)
if (this->source != NULL)
{
other->source = this->public.source->clone(this->public.source);
other->source = this->source->clone(this->source);
}
else
{
@@ -83,16 +154,16 @@ static packet_t *clone (private_packet_t *this)
}
/* only clone existing chunks :-) */
if (this->public.data.ptr != NULL)
if (this->data.ptr != NULL)
{
other->data.ptr = allocator_clone_bytes(this->public.data.ptr,this->public.data.len);
other->data.len = this->public.data.len;
other->data.ptr = allocator_clone_bytes(this->data.ptr,this->data.len);
other->data.len = this->data.len;
}
else
{
other->data = CHUNK_INITIALIZER;
}
return other;
return &(other->public);
}
@@ -103,12 +174,18 @@ packet_t *packet_create()
{
private_packet_t *this = allocator_alloc_thing(private_packet_t);
this->public.destroy = (void(*) (packet_t *)) destroy;
this->public.set_data = (void(*) (packet_t *,chunk_t)) set_data;
this->public.get_data = (chunk_t(*) (packet_t *)) get_data;
this->public.set_source = (void(*) (packet_t *,host_t*)) set_source;
this->public.get_source = (host_t*(*) (packet_t *)) get_source;
this->public.set_destination = (void(*) (packet_t *,host_t*)) set_destination;
this->public.get_destination = (host_t*(*) (packet_t *)) get_destination;
this->public.clone = (packet_t*(*) (packet_t *))clone;
this->public.destroy = (void(*) (packet_t *)) destroy;
this->public.destination = NULL;
this->public.source = NULL;
this->public.data = CHUNK_INITIALIZER;
this->destination = NULL;
this->source = NULL;
this->data = CHUNK_INITIALIZER;
return &(this->public);
}
+70 -14
View File
@@ -29,30 +29,85 @@
typedef struct packet_t packet_t;
/**
* @brief Abstraction of an UDP-Packet, contains data, sender and receiver.
*
* @b Constructors:
* - packet_create()
*
* @ingroup network
*/
struct packet_t {
/**
* source address structure
* @brief Set the source address.
*
* Set host_t is now owned by packet_t, it will destroy
* it if necessary.
*
* @param this calling object
* @param source address to set as source
*/
host_t *source;
void (*set_source) (packet_t *packet, host_t *source);
/**
* destination address structure
* @brief Set the destination address.
*
* Set host_t is now owned by packet_t, it will destroy
* it if necessary.
*
* @param this calling object
* @param source address to set as destination
*/
host_t *destination;
/**
* message data
*/
chunk_t data;
void (*set_destination) (packet_t *packet, host_t *destination);
/**
* @brief Clones a packet_t object.
* @brief Get the source address.
*
* Set host_t is still owned by packet_t, clone it
* if needed.
*
* @param this calling object
* @return source address
*/
host_t *(*get_source) (packet_t *packet);
/**
* @brief Get the destination address.
*
* Set host_t is still owned by packet_t, clone it
* if needed.
*
* @param this calling object
* @return destination address
*/
host_t *(*get_destination) (packet_t *packet);
/**
* @brief Get the data from the packet.
*
* The data pointed by the chunk is still owned
* by the packet. Clone it if needed.
*
* @param this calling object
* @return chunk containing the data
*/
chunk_t (*get_data) (packet_t *packet);
/**
* @brief Set the data in the packet.
*
* Supplied chunk data is now owned by the
* packet. It will free it.
*
* @param this calling object
* @param data chunk with data to set
*/
void (*set_data) (packet_t *packet, chunk_t data);
/**
* @brief Clones a packet_t object.
*
* @param packet calling object
* @param clone pointer to a packet_t object pointer where the new object is stored
@@ -60,7 +115,7 @@ struct packet_t {
packet_t* (*clone) (packet_t *packet);
/**
* @brief Destroy the packet, freeing contained data.
* @brief Destroy the packet, freeing contained data.
*
* @param packet packet to destroy
*/
@@ -70,10 +125,11 @@ struct packet_t {
/**
* @brief create an empty packet
*
* @return created packet_t object
* @return packet_t object
*
* @ingroup network
*/
packet_t *packet_create();
#endif /*PACKET_H_*/
+27 -20
View File
@@ -63,32 +63,32 @@ struct private_socket_t{
status_t receiver(private_socket_t *this, packet_t **packet)
{
char buffer[MAX_PACKET];
chunk_t data;
int oldstate;
host_t *source, *dest;
packet_t *pkt = packet_create();
/* add packet destroy handler for cancellation, enable cancellation */
pthread_cleanup_push((void(*)(void*))pkt->destroy, (void*)pkt);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
pkt->source = host_create(AF_INET, "0.0.0.0", 0);
pkt->destination = host_create(AF_INET, "0.0.0.0", 0);
source = host_create(AF_INET, "0.0.0.0", 0);
dest = host_create(AF_INET, "0.0.0.0", 0);
pkt->set_source(pkt, source);
pkt->set_destination(pkt, dest);
this->logger->log(this->logger, CONTROL|MORE, "going to read from socket");
/* do the read */
pkt->data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
pkt->source->get_sockaddr(pkt->source),
pkt->source->get_sockaddr_len(pkt->source));
data.len = recvfrom(this->socket_fd, buffer, MAX_PACKET, 0,
source->get_sockaddr(source),
source->get_sockaddr_len(source));
/* reset cancellation, remove packet destroy handler (without executing) */
pthread_setcancelstate(oldstate, NULL);
pthread_cleanup_pop(0);
/* TODO: get senders destination address, using
* IP_PKTINFO and recvmsg */
if (pkt->data.len < 0)
if (data.len < 0)
{
pkt->destroy(pkt);
this->logger->log(this->logger, ERROR, "error reading from socket: %s", strerror(errno));
@@ -96,12 +96,14 @@ status_t receiver(private_socket_t *this, packet_t **packet)
}
this->logger->log(this->logger, CONTROL, "received packet from %s:%d",
pkt->source->get_address(pkt->source),
pkt->source->get_port(pkt->source));
source->get_address(source),
source->get_port(source));
/* fill in packet */
pkt->data.ptr = allocator_alloc(pkt->data.len);
memcpy(pkt->data.ptr, buffer, pkt->data.len);
data.ptr = allocator_alloc(data.len);
memcpy(data.ptr, buffer, data.len);
pkt->set_data(pkt, data);
/* return packet */
*packet = pkt;
@@ -115,17 +117,22 @@ status_t receiver(private_socket_t *this, packet_t **packet)
status_t sender(private_socket_t *this, packet_t *packet)
{
ssize_t bytes_sent;
chunk_t data;
host_t *source, *dest;
source = packet->get_source(packet);
dest = packet->get_destination(packet);
data = packet->get_data(packet);
this->logger->log(this->logger, CONTROL, "sending packet to %s:%d",
packet->destination->get_address(packet->destination),
packet->destination->get_port(packet->destination));
dest->get_address(dest),
dest->get_port(dest));
/* send data */
bytes_sent = sendto(this->socket_fd, packet->data.ptr, packet->data.len,
0, packet->destination->get_sockaddr(packet->destination),
*(packet->destination->get_sockaddr_len(packet->destination)));
bytes_sent = sendto(this->socket_fd, data.ptr, data.len, 0,
dest->get_sockaddr(dest), *(dest->get_sockaddr_len(dest)));
if (bytes_sent != packet->data.len)
if (bytes_sent != data.len)
{
this->logger->log(this->logger, ERROR, "error writing to socket: %s", strerror(errno));
return FAILED;
+15 -7
View File
@@ -31,7 +31,7 @@
/**
* @brief Maximum size of a packet.
*
* 3000 Bytes should be sufficient, see IKEv2 draft
* 3000 Bytes should be sufficient, see IKEv2 draft.
*
* @ingroup network
*/
@@ -45,6 +45,13 @@ typedef struct socket_t socket_t;
*
* Receiver reads from here, sender writes to here.
*
* @b Constructors:
* - socket_create()
*
* @todo add IPv6 support
*
* @todo allow listening/sending to multiple sockets, depending on address
*
* @ingroup network
*/
struct socket_t {
@@ -57,8 +64,9 @@ struct socket_t {
*
* @param sock socket_t object to work on
* @param packet pinter gets address from allocated packet_t
* @return FAILED when unable to receive
* SUCCESS when packet successfully received
* @return
* - SUCCESS when packet successfully received
* - FAILED when unable to receive
*/
status_t (*receive) (socket_t *sock, packet_t **packet);
@@ -70,8 +78,9 @@ struct socket_t {
*
* @param sock socket_t object to work on
* @param packet[out] packet_t to send
* @return FAILED when unable to send
* SUCCESS when packet successfully sent
* @return
* - SUCCESS when packet successfully sent
* - FAILED when unable to send
*/
status_t (*send) (socket_t *sock, packet_t *packet);
@@ -81,7 +90,6 @@ struct socket_t {
* close sockets and destroy socket_t object
*
* @param sock socket_t to destroy
* @return SUCCESS
*/
void (*destroy) (socket_t *sock);
};
@@ -93,7 +101,7 @@ struct socket_t {
* on port.
*
* @param port port to bind socket to
* @return the created socket, or NULL on error
* @return socket_t object
*
* @ingroup network
*/
+9 -12
View File
@@ -37,22 +37,19 @@ void test_packet(tester_t *tester)
{
packet_t *packet = packet_create();
packet_t *packet2;
char * string_to_copy = "aha, soso";
chunk_t data;
char *string_to_copy = "aha, soso";
packet->data.ptr = allocator_alloc(strlen(string_to_copy) + 1);
tester->assert_true(tester,(packet->data.ptr != NULL),"NULL pointer check");
data.len = strlen(string_to_copy) + 1;
data.ptr = allocator_alloc(data.len);
memcpy(data.ptr, string_to_copy, data.len);
packet->data.len = strlen(string_to_copy) + 1;
strcpy(packet->data.ptr,string_to_copy);
tester->assert_true(tester,(packet != NULL),"NULL pointer check");
packet->set_data(packet, data);
packet2 = packet->clone(packet);
tester->assert_false(tester,(packet->data.ptr == packet2->data.ptr),"value pointer check");
data = packet2->get_data(packet2);
tester->assert_true(tester,(packet->data.len == (strlen(string_to_copy) + 1)),"value length check");
tester->assert_true(tester,(memcmp(packet->data.ptr,packet2->data.ptr,packet->data.len) == 0),"cloned value check");
tester->assert_true(tester,(data.len == (strlen(string_to_copy) + 1)),"value length check");
tester->assert_true(tester,(memcmp(data.ptr,string_to_copy,data.len) == 0),"cloned value check");
packet2->destroy(packet2);
packet->destroy(packet);
+10 -7
View File
@@ -58,15 +58,17 @@ void test_receiver(tester_t *tester)
job_t *job;
packet_t *received_packet;
receiver = receiver_create();
chunk_t test_data;
for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
{
packet = packet_create();
packet->destination = host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND);
packet->data.ptr = allocator_alloc_thing(int);
packet->data.len = ( sizeof(int));
*((int *) (packet->data.ptr)) = i;
charon->socket->send(charon->socket,packet);
packet->set_destination(packet, host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND));
test_data.ptr = allocator_alloc_thing(int);
test_data.len = ( sizeof(int));
*((int *) (test_data.ptr)) = i;
packet->set_data(packet, test_data);
charon->socket->send(charon->socket, packet);
packet->destroy(packet);
}
@@ -76,8 +78,9 @@ void test_receiver(tester_t *tester)
tester->assert_true(tester, (job->get_type(job) == INCOMING_PACKET), "job type check");
received_packet = ((incoming_packet_job_t *)(job))->get_packet((incoming_packet_job_t *)(job));
tester->assert_true(tester, (received_packet->data.len == (sizeof(int))), "received data length check");
tester->assert_true(tester, (i == *((int *)(received_packet->data.ptr))), "received data value check");
test_data = received_packet->get_data(received_packet);
tester->assert_true(tester, (test_data.len == (sizeof(int))), "received data length check");
tester->assert_true(tester, (i == *((int *)(test_data.ptr))), "received data value check");
received_packet->destroy(received_packet);
job->destroy(job);
+1 -1
View File
@@ -54,7 +54,7 @@ void test_rsa(tester_t *tester)
data.len = sizeof(test_data);
logger = charon->logger_manager->create_logger(charon->logger_manager, TESTER, NULL);
logger->enable_level(logger, FULL);
logger->disable_level(logger, FULL);
private_key = rsa_private_key_create();
+9 -6
View File
@@ -53,23 +53,26 @@ void test_sender(tester_t *tester)
sender_t *sender;
packet_t *packet;
packet_t *received_packet;
chunk_t packet_data;
sender = sender_create();
for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
{
packet = packet_create(AF_INET);
packet->destination = host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND);
packet->data.ptr = allocator_alloc_thing(int);
packet->data.len = ( sizeof(int));
*((int *) (packet->data.ptr)) = i;
packet->set_destination(packet, host_create(AF_INET,DESTINATION_IP,PORT_TO_SEND));
packet_data.ptr = allocator_alloc_thing(int);
packet_data.len = ( sizeof(int));
*((int *) (packet_data.ptr)) = i;
packet->set_data(packet, packet_data);
charon->send_queue->add(charon->send_queue,packet);
}
for (i = 0; i < NUMBER_OF_PACKETS_TO_SEND; i++)
{
charon->socket->receive(charon->socket,&received_packet);
tester->assert_true(tester, (received_packet->data.len == (sizeof(int))), "received data length check");
tester->assert_true(tester, (i == *((int *)(received_packet->data.ptr))), "received data value check");
packet_data = received_packet->get_data(received_packet);
tester->assert_true(tester, (packet_data.len == (sizeof(int))), "received data length check");
tester->assert_true(tester, (i == *((int *)(packet_data.ptr))), "received data value check");
received_packet->destroy(received_packet);
}
+8 -5
View File
@@ -38,14 +38,16 @@ void test_socket(tester_t *tester)
socket_t *skt = socket_create(4500);
packet_t *pkt = packet_create(AF_INET);
char *test_string = "Testing functionality of socket_t";
chunk_t data;
pkt->data.ptr = allocator_alloc(strlen(test_string) + 1);
memcpy(pkt->data.ptr,test_string,strlen(test_string) + 1);
pkt->data.len = strlen(test_string) + 1;
data.ptr = allocator_alloc(strlen(test_string) + 1);
memcpy(data.ptr,test_string,strlen(test_string) + 1);
data.len = strlen(test_string) + 1;
/* send to previously bound socket */
pkt->destination = host_create(AF_INET, "127.0.0.1", 4500);
pkt->set_destination(pkt, host_create(AF_INET, "127.0.0.1", 4500));
pkt->set_data(pkt, data);
/* send packet_count packets */
for (current = 0; current < packet_count; current++)
@@ -61,7 +63,8 @@ void test_socket(tester_t *tester)
for (current = 0; current < packet_count; current++)
{
skt->receive(skt, &pkt);
tester->assert_false(tester, strcmp(test_string, pkt->data.ptr), "packet exchange");
data = pkt->get_data(pkt);
tester->assert_false(tester, strcmp(test_string, data.ptr), "packet exchange");
pkt->destroy(pkt);
}
+1 -1
View File
@@ -252,7 +252,7 @@ int main()
tester->perform_tests(tester,all_tests);
// tester->perform_test(tester,&parser_test14);
// tester->perform_test(tester,&packet_test);
tester->destroy(tester);