diff --git a/Source/charon/utils/host.c b/Source/charon/utils/host.c index d56ff2b95..100cea688 100644 --- a/Source/charon/utils/host.c +++ b/Source/charon/utils/host.c @@ -53,20 +53,62 @@ struct private_host_s { }; - -sockaddr_t *get_sockaddr(private_host_t *this) +/** + * implements host_t.get_sockaddr + */ +static sockaddr_t *get_sockaddr(private_host_t *this) { return &(this->address); } -socklen_t *get_sockaddr_len(private_host_t *this) +/** + * implements host_t.get_sockaddr_len + */ +static socklen_t *get_sockaddr_len(private_host_t *this) { return &(this->socklen); } /** - * Implements host_t-function destroy. - * @see host_t.destroy. + * implements host_t.get_address + */ +static char *get_address(private_host_t *this) +{ + switch (this->family) + { + case AF_INET: + { + struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address); + return inet_ntoa(sin->sin_addr); + } + default: + { + return "(family not supported)"; + } + } +} + +/** + * implements host_t.get_port + */ +static u_int16_t get_port(private_host_t *this) +{ + switch (this->family) + { + case AF_INET: + { + struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address); + return ntohs(sin->sin_port); + } + default: + { + return 0; + } + } +} + +/** + * Implements host_t.destroy */ static status_t destroy(private_host_t *this) { @@ -75,8 +117,7 @@ static status_t destroy(private_host_t *this) } /** - * Implements host_t-function clone. - * @see host_t.clone. + * Implements host_t.clone. */ static status_t clone(private_host_t *this, host_t **other) { @@ -108,6 +149,8 @@ host_t *host_create(int family, char *address, u_int16_t port) 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 = (status_t (*) (host_t*, host_t**))clone; + this->public.get_address = (char* (*) (host_t *))get_address; + this->public.get_port = (u_int16_t (*) (host_t *))get_port; this->public.destroy = (status_t (*) (host_t*))destroy; this->family = family; diff --git a/Source/charon/utils/host.h b/Source/charon/utils/host.h index 1ef9676a3..27d20d731 100644 --- a/Source/charon/utils/host.h +++ b/Source/charon/utils/host.h @@ -37,22 +37,84 @@ */ typedef struct host_s host_t; struct host_s { + /** + * @brief Build a clone of this host object. + * + * @param this object to clone + * @param [out]other address where to allocate the clone + * @return + * - SUCCESS, or + * - OUT_OF_RES + */ + status_t (*clone) (host_t *this, host_t **other); + /** + * @brief Get a pointer to the internal sockaddr struct. + * + * This is used for sending and receiving via sockets. + * + * @param this object to clone + * @return pointer to the internal sockaddr structure + */ sockaddr_t *(*get_sockaddr) (host_t *this); + + /** + * @brief Get the length of the sockaddr struct. + * + * Sepending on the family, the length of the sockaddr struct + * is different. Use this function to get the length of the sockaddr + * struct returned by get_sock_addr. + * + * This is used for sending and receiving via sockets. + * + * @param this object to clone + * @return length of the sockaddr struct + */ socklen_t *(*get_sockaddr_len) (host_t *this); + + /** + * @brief get the address of this host + * + * Mostly used for debugging purposes. + * @warging string must NOT be freed + * + * @param this object to clone + * @return address string, + */ + char* (*get_address) (host_t *this); + + /** + * @brief get the port of this host + * + * Mostly used for debugging purposes. + * + * @param this object to clone + * @return port number + */ + u_int16_t (*get_port) (host_t *this); + + /** + * @brief Destroy this host object + * + * @param this calling + * @return SUCCESS in any case + */ status_t (*destroy) (host_t *this); }; /** - * @brief Constructor to create a logger_t object. + * @brief Constructor to create a host_t object + * + * currently supports only IPv4! * - * @param logger_name Name for the logger_t object - * @param log_level or'ed set of log_levels to assign to the new logger_t object - * @param output FILE * if log has to go on a file output, NULL for syslog - * @return logger_t object or NULL if failed + * @param family Address family to use for this object, such as AF_INET or AF_INET6 + * @param address string of an address, such as "152.96.193.130" + * @param port port number + * @return the host_t object or NULL, when + * not enough ressources, or + * family not supported. */ host_t *host_create(int family, char *address, u_int16_t port); - #endif /*HOST_H_*/