- code cleanup in network and config
- moved packet_t members to private, added getter and setters
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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_*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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_*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user