strongswan/Source/charon/network/host.c

162 lines
3.1 KiB
C
Raw Normal View History

2005-11-16 16:11:08 +00:00
/**
* @file host.c
*
2005-11-28 20:29:47 +00:00
* @brief Implementation of host_t.
2005-11-16 16:11:08 +00:00
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "host.h"
2005-11-23 09:24:35 +00:00
#include <utils/allocator.h>
2005-11-16 16:11:08 +00:00
2005-11-24 09:17:51 +00:00
typedef struct private_host_t private_host_t;
2005-11-16 16:11:08 +00:00
/**
2005-11-24 09:17:51 +00:00
* @brief Private Data of a host object.
2005-11-16 16:11:08 +00:00
*/
2005-11-24 09:17:51 +00:00
struct private_host_t {
2005-11-16 16:11:08 +00:00
/**
* Public data
*/
host_t public;
/**
* Address family to use, such as AF_INET or AF_INET6
*/
int family;
/**
* low-lewel structure, wich stores the address
*/
sockaddr_t address;
/**
* length of address structure
*/
socklen_t socklen;
};
2005-11-21 11:45:04 +00:00
/**
* implements host_t.get_sockaddr
*/
static sockaddr_t *get_sockaddr(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
return &(this->address);
}
2005-11-21 11:45:04 +00:00
/**
* implements host_t.get_sockaddr_len
*/
static socklen_t *get_sockaddr_len(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
return &(this->socklen);
}
/**
2005-11-21 11:45:04 +00:00
* 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
2005-11-16 16:11:08 +00:00
*/
2005-11-28 20:29:47 +00:00
static void destroy(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
allocator_free(this);
}
/**
2005-11-21 11:45:04 +00:00
* Implements host_t.clone.
2005-11-16 16:11:08 +00:00
*/
2005-11-28 20:29:47 +00:00
static private_host_t *clone(private_host_t *this)
2005-11-16 16:11:08 +00:00
{
private_host_t *new = allocator_alloc_thing(private_host_t);
memcpy(new, this, sizeof(private_host_t));
2005-11-28 20:29:47 +00:00
return new;
2005-11-16 16:11:08 +00:00
}
/*
* see header
*/
host_t *host_create(int family, char *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;
2005-11-28 20:29:47 +00:00
this->public.clone = (host_t* (*) (host_t*))clone;
2005-11-21 11:45:04 +00:00
this->public.get_address = (char* (*) (host_t *))get_address;
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
2005-11-28 20:29:47 +00:00
this->public.destroy = (void (*) (host_t*))destroy;
2005-11-16 16:11:08 +00:00
this->family = family;
switch (family)
{
/* IPv4 */
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in*)&(this->address);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(address);
sin->sin_port = htons(port);
this->socklen = sizeof(struct sockaddr_in);
return (host_t*)this;
}
}
allocator_free(this);
return NULL;
}