- implemented and tested ts_payload_t
This commit is contained in:
@@ -88,6 +88,32 @@ static char *get_address(private_host_t *this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of host_t.get_address_as_chunk.
|
||||
*/
|
||||
static chunk_t get_address_as_chunk(private_host_t *this)
|
||||
{
|
||||
chunk_t address = CHUNK_INITIALIZER;
|
||||
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
/* allocate 4 bytes for IPV4 address*/
|
||||
address.ptr = allocator_alloc(4);
|
||||
address.len = 4;
|
||||
struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
|
||||
memcpy(address.ptr,&(sin->sin_addr.s_addr),4);
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* empty chunk is returned */
|
||||
return address;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_port
|
||||
*/
|
||||
@@ -128,7 +154,7 @@ static private_host_t *clone(private_host_t *this)
|
||||
|
||||
|
||||
/*
|
||||
* see header
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port)
|
||||
{
|
||||
@@ -138,6 +164,7 @@ host_t *host_create(int family, char *address, u_int16_t port)
|
||||
this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
|
||||
this->public.clone = (host_t* (*) (host_t*))clone;
|
||||
this->public.get_address = (char* (*) (host_t *))get_address;
|
||||
this->public.get_address_as_chunk = (chunk_t (*) (host_t *)) get_address_as_chunk;
|
||||
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
|
||||
this->public.destroy = (void (*) (host_t*))destroy;
|
||||
|
||||
@@ -159,3 +186,40 @@ host_t *host_create(int family, char *address, u_int16_t port)
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
|
||||
{
|
||||
private_host_t *this = allocator_alloc_thing(private_host_t);
|
||||
|
||||
this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr;
|
||||
this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
|
||||
this->public.clone = (host_t* (*) (host_t*))clone;
|
||||
this->public.get_address = (char* (*) (host_t *))get_address;
|
||||
this->public.get_address_as_chunk = (chunk_t (*) (host_t *)) get_address_as_chunk;
|
||||
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
|
||||
this->public.destroy = (void (*) (host_t*))destroy;
|
||||
|
||||
this->family = family;
|
||||
|
||||
if (address.len == 4)
|
||||
{
|
||||
switch (family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
|
||||
sin->sin_family = AF_INET;
|
||||
memcpy(&(sin->sin_addr.s_addr),address.ptr,4);
|
||||
sin->sin_port = htons(port);
|
||||
this->socklen = sizeof(struct sockaddr_in);
|
||||
return (host_t*)this;
|
||||
}
|
||||
}
|
||||
}
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -79,12 +79,22 @@ struct host_t {
|
||||
* @brief get the address of this host
|
||||
*
|
||||
* Mostly used for debugging purposes.
|
||||
* @warging string must NOT be freed
|
||||
* @warning string must NOT be freed
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return address string,
|
||||
*/
|
||||
char* (*get_address) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the address of this host as chunk_t
|
||||
*
|
||||
* @warning returned chunk has to get destroyed by caller.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return address string,
|
||||
*/
|
||||
chunk_t (*get_address_as_chunk) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the port of this host
|
||||
@@ -121,4 +131,19 @@ struct host_t {
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port);
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @param family Address family to use for this object, such as AF_INET or AF_INET6
|
||||
* @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.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
|
||||
#endif /*HOST_H_*/
|
||||
|
||||
Reference in New Issue
Block a user