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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-06-22 06:36:28 +00:00
|
|
|
* Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-16 16:11:08 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#include <string.h>
|
2005-11-16 16:11:08 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#include "host.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;
|
|
|
|
|
2005-12-02 16:09:04 +00:00
|
|
|
/**
|
|
|
|
* string representation of host
|
|
|
|
*/
|
|
|
|
char *string;
|
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
/**
|
|
|
|
* low-lewel structure, wich stores the address
|
|
|
|
*/
|
2005-12-04 19:05:52 +00:00
|
|
|
union {
|
|
|
|
struct sockaddr address;
|
|
|
|
struct sockaddr_in address4;
|
|
|
|
};
|
2005-11-16 16:11:08 +00:00
|
|
|
/**
|
|
|
|
* length of address structure
|
|
|
|
*/
|
|
|
|
socklen_t socklen;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-11-21 11:45:04 +00:00
|
|
|
/**
|
|
|
|
* implements host_t.get_sockaddr
|
|
|
|
*/
|
2005-12-06 16:00:07 +00:00
|
|
|
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-12-01 17:16:10 +00:00
|
|
|
/**
|
2006-05-18 06:02:28 +00:00
|
|
|
* Implementation of host_t.is_anyaddr.
|
2005-12-01 17:16:10 +00:00
|
|
|
*/
|
2006-05-18 06:02:28 +00:00
|
|
|
static bool is_anyaddr(private_host_t *this)
|
2005-12-01 17:16:10 +00:00
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2006-05-18 06:02:28 +00:00
|
|
|
static u_int8_t default_route[4] = {0x00, 0x00, 0x00, 0x00};
|
2005-12-02 16:09:04 +00:00
|
|
|
|
2006-05-18 06:02:28 +00:00
|
|
|
return !memcmp(default_route, &(this->address4.sin_addr.s_addr), 4);
|
2005-12-01 17:16:10 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* empty chunk is returned */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
/**
|
2006-07-18 12:53:54 +00:00
|
|
|
* implements host_t.get_string
|
2005-11-21 11:45:04 +00:00
|
|
|
*/
|
2006-07-18 12:53:54 +00:00
|
|
|
static char *get_string(private_host_t *this)
|
2005-11-21 11:45:04 +00:00
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2005-12-02 16:09:04 +00:00
|
|
|
char *string;
|
2005-12-04 19:05:52 +00:00
|
|
|
/* we need to clone it, since inet_ntoa overwrites
|
|
|
|
* internal buffer on subsequent calls
|
|
|
|
*/
|
2006-05-18 06:02:28 +00:00
|
|
|
if (this->string == NULL)
|
|
|
|
{
|
|
|
|
string = is_anyaddr(this)? "%any" : inet_ntoa(this->address4.sin_addr);
|
|
|
|
this->string = malloc(strlen(string)+1);
|
|
|
|
strcpy(this->string, string);
|
|
|
|
}
|
2005-12-02 16:09:04 +00:00
|
|
|
return this->string;
|
2005-11-21 11:45:04 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return "(family not supported)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-29 15:23:04 +00:00
|
|
|
/**
|
2006-07-18 12:53:54 +00:00
|
|
|
* Implementation of host_t.get_address.
|
2005-11-29 15:23:04 +00:00
|
|
|
*/
|
2006-07-18 12:53:54 +00:00
|
|
|
static chunk_t get_address(private_host_t *this)
|
2005-11-29 15:23:04 +00:00
|
|
|
{
|
|
|
|
chunk_t address = CHUNK_INITIALIZER;
|
|
|
|
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2006-06-22 06:36:28 +00:00
|
|
|
/* allocate 4 bytes for IPv4 address*/
|
2006-07-18 12:53:54 +00:00
|
|
|
address.ptr = (char*)&(this->address4.sin_addr.s_addr);
|
2005-11-29 15:23:04 +00:00
|
|
|
address.len = 4;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* empty chunk is returned */
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
2005-12-04 19:05:52 +00:00
|
|
|
}
|
|
|
|
|
2006-07-18 12:53:54 +00:00
|
|
|
/**
|
|
|
|
* implements host_t.get_family
|
|
|
|
*/
|
2005-12-04 19:05:52 +00:00
|
|
|
static int get_family(private_host_t *this)
|
|
|
|
{
|
|
|
|
return this->family;
|
2005-11-29 15:23:04 +00:00
|
|
|
}
|
|
|
|
|
2005-11-21 11:45:04 +00:00
|
|
|
/**
|
|
|
|
* implements host_t.get_port
|
|
|
|
*/
|
|
|
|
static u_int16_t get_port(private_host_t *this)
|
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
return ntohs(this->address4.sin_port);
|
2005-11-21 11:45:04 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/**
|
|
|
|
* implements host_t.set_port
|
|
|
|
*/
|
|
|
|
static void set_port(private_host_t *this, u_int16_t port)
|
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
this->address4.sin_port = htons(port);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2006-07-18 12:53:54 +00:00
|
|
|
/*TODO*/
|
2006-06-22 06:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
/**
|
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
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
private_host_t *new = malloc_thing(private_host_t);
|
2005-12-02 16:09:04 +00:00
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
|
|
|
|
memcpy(new, this, sizeof(private_host_t));
|
2005-12-02 16:09:04 +00:00
|
|
|
if (this->string)
|
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
new->string = malloc(strlen(this->string)+1);
|
2005-12-02 16:09:04 +00:00
|
|
|
strcpy(new->string, this->string);
|
|
|
|
}
|
2005-11-28 20:29:47 +00:00
|
|
|
return new;
|
2005-11-16 16:11:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-23 15:25:43 +00:00
|
|
|
/**
|
|
|
|
* Impelements host_t.ip_equals
|
|
|
|
*/
|
|
|
|
static bool ip_equals(private_host_t *this, private_host_t *other)
|
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
if ((this->address4.sin_family == other->address4.sin_family) &&
|
|
|
|
(this->address4.sin_addr.s_addr == other->address4.sin_addr.s_addr))
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-11-16 16:11:08 +00:00
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/**
|
|
|
|
* Implements host_t.get_differences
|
|
|
|
*/
|
2006-07-18 12:53:54 +00:00
|
|
|
static host_diff_t get_differences(private_host_t *this, private_host_t *other)
|
2006-06-22 06:36:28 +00:00
|
|
|
{
|
2006-07-18 12:53:54 +00:00
|
|
|
host_diff_t ret = HOST_DIFF_NONE;
|
2006-06-22 06:36:28 +00:00
|
|
|
|
|
|
|
if (!this->public.ip_equals(&this->public, &other->public))
|
|
|
|
{
|
|
|
|
ret |= HOST_DIFF_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->public.get_port(&this->public) != other->public.get_port(&other->public))
|
|
|
|
{
|
|
|
|
ret |= HOST_DIFF_PORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-12-01 07:35:03 +00:00
|
|
|
/**
|
|
|
|
* Impelements host_t.equals
|
|
|
|
*/
|
2006-03-23 15:25:43 +00:00
|
|
|
static bool equals(private_host_t *this, private_host_t *other)
|
2005-12-01 07:35:03 +00:00
|
|
|
{
|
|
|
|
switch (this->family)
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
if ((this->address4.sin_family == other->address4.sin_family) &&
|
2006-03-23 15:25:43 +00:00
|
|
|
(this->address4.sin_addr.s_addr == other->address4.sin_addr.s_addr) &&
|
|
|
|
(this->address4.sin_port == other->address4.sin_port))
|
2005-12-01 07:35:03 +00:00
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-12-02 16:09:04 +00:00
|
|
|
/**
|
|
|
|
* Implements host_t.destroy
|
|
|
|
*/
|
|
|
|
static void destroy(private_host_t *this)
|
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->string);
|
|
|
|
free(this);
|
2005-12-02 16:09:04 +00:00
|
|
|
}
|
2005-12-01 07:35:03 +00:00
|
|
|
|
2005-12-01 17:16:10 +00:00
|
|
|
/**
|
|
|
|
* Creates an empty host_t object
|
2005-11-16 16:11:08 +00:00
|
|
|
*/
|
2006-05-09 07:34:25 +00:00
|
|
|
static private_host_t *host_create_empty(void)
|
2005-11-16 16:11:08 +00:00
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
private_host_t *this = malloc_thing(private_host_t);
|
2005-11-16 16:11:08 +00:00
|
|
|
|
|
|
|
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-12-04 19:05:52 +00:00
|
|
|
this->public.get_family = (int (*) (host_t*))get_family;
|
2006-07-18 12:53:54 +00:00
|
|
|
this->public.get_string = (char* (*) (host_t *))get_string;
|
|
|
|
this->public.get_address = (chunk_t (*) (host_t *)) get_address;
|
2005-11-21 11:45:04 +00:00
|
|
|
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
|
2006-06-22 06:36:28 +00:00
|
|
|
this->public.set_port = (void (*) (host_t *,u_int16_t))set_port;
|
2006-07-18 12:53:54 +00:00
|
|
|
this->public.get_differences = (host_diff_t (*) (host_t *,host_t *)) get_differences;
|
2006-03-23 15:25:43 +00:00
|
|
|
this->public.ip_equals = (bool (*) (host_t *,host_t *)) ip_equals;
|
|
|
|
this->public.equals = (bool (*) (host_t *,host_t *)) equals;
|
2006-05-18 06:02:28 +00:00
|
|
|
this->public.is_anyaddr = (bool (*) (host_t *)) is_anyaddr;
|
2005-11-28 20:29:47 +00:00
|
|
|
this->public.destroy = (void (*) (host_t*))destroy;
|
2005-11-16 16:11:08 +00:00
|
|
|
|
2005-12-02 16:09:04 +00:00
|
|
|
this->string = NULL;
|
|
|
|
|
2005-12-01 17:16:10 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
host_t *host_create(int family, char *address, u_int16_t port)
|
|
|
|
{
|
|
|
|
private_host_t *this = host_create_empty();
|
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
this->family = family;
|
|
|
|
|
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
case AF_INET:
|
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
this->address4.sin_family = AF_INET;
|
|
|
|
this->address4.sin_addr.s_addr = inet_addr(address);
|
|
|
|
this->address4.sin_port = htons(port);
|
2005-11-16 16:11:08 +00:00
|
|
|
this->socklen = sizeof(struct sockaddr_in);
|
2005-12-01 17:16:10 +00:00
|
|
|
return &(this->public);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-12-01 17:16:10 +00:00
|
|
|
return NULL;
|
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-01 17:16:10 +00:00
|
|
|
|
2005-11-16 16:11:08 +00:00
|
|
|
}
|
2005-11-29 15:23:04 +00:00
|
|
|
|
2006-06-22 06:36:28 +00:00
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
host_t *host_create_from_hdr(u_long address, u_short port)
|
|
|
|
{
|
|
|
|
private_host_t *this = host_create_empty();
|
|
|
|
|
|
|
|
this->family = AF_INET;
|
|
|
|
|
|
|
|
this->address4.sin_family = AF_INET;
|
|
|
|
this->address4.sin_addr.s_addr = address;
|
|
|
|
this->address4.sin_port = port;
|
|
|
|
this->socklen = sizeof(struct sockaddr_in);
|
|
|
|
return &(this->public);
|
|
|
|
}
|
|
|
|
|
2005-11-29 15:23:04 +00:00
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
|
|
|
|
{
|
2005-12-01 17:16:10 +00:00
|
|
|
private_host_t *this = host_create_empty();
|
2005-11-29 15:23:04 +00:00
|
|
|
|
|
|
|
this->family = family;
|
2005-12-04 19:05:52 +00:00
|
|
|
switch (family)
|
2005-11-29 15:23:04 +00:00
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
/* IPv4 */
|
|
|
|
case AF_INET:
|
2005-11-29 15:23:04 +00:00
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
if (address.len != 4)
|
2005-11-29 15:23:04 +00:00
|
|
|
{
|
2005-12-04 19:05:52 +00:00
|
|
|
break;
|
2005-11-29 15:23:04 +00:00
|
|
|
}
|
2005-12-04 19:05:52 +00:00
|
|
|
this->address4.sin_family = AF_INET;
|
|
|
|
memcpy(&(this->address4.sin_addr.s_addr),address.ptr,4);
|
|
|
|
this->address4.sin_port = htons(port);
|
|
|
|
this->socklen = sizeof(struct sockaddr_in);
|
|
|
|
return &(this->public);
|
2005-11-29 15:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-11-29 15:23:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-02-16 16:24:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
|
|
|
|
{
|
|
|
|
chunk_t address;
|
|
|
|
|
|
|
|
switch (sockaddr->sa_family)
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)sockaddr;
|
|
|
|
address.ptr = (void*)&(sin->sin_addr.s_addr);
|
|
|
|
address.len = 4;
|
|
|
|
return host_create_from_chunk(AF_INET, address, ntohs(sin->sin_port));
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|