../svn-commit.tmp
This commit is contained in:
@@ -12,17 +12,13 @@
|
||||
# for more details.
|
||||
#
|
||||
|
||||
NETWORK_DIR= $(MAIN_DIR)network/
|
||||
NETWORK_DIR= $(CHARON_DIR)network/
|
||||
|
||||
|
||||
OBJS+= $(BUILD_DIR)packet.o
|
||||
CHARON_OBJS+= $(BUILD_DIR)packet.o
|
||||
$(BUILD_DIR)packet.o : $(NETWORK_DIR)packet.c $(NETWORK_DIR)packet.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
OBJS+= $(BUILD_DIR)socket.o
|
||||
CHARON_OBJS+= $(BUILD_DIR)socket.o
|
||||
$(BUILD_DIR)socket.o : $(NETWORK_DIR)socket.c $(NETWORK_DIR)socket.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
OBJS+= $(BUILD_DIR)host.o
|
||||
$(BUILD_DIR)host.o : $(NETWORK_DIR)host.c $(NETWORK_DIR)host.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
@@ -1,365 +0,0 @@
|
||||
/**
|
||||
* @file host.c
|
||||
*
|
||||
* @brief Implementation of host_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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"
|
||||
|
||||
#include <utils/allocator.h>
|
||||
|
||||
|
||||
typedef struct private_host_t private_host_t;
|
||||
|
||||
/**
|
||||
* @brief Private Data of a host object.
|
||||
*/
|
||||
struct private_host_t {
|
||||
/**
|
||||
* Public data
|
||||
*/
|
||||
host_t public;
|
||||
|
||||
/**
|
||||
* Address family to use, such as AF_INET or AF_INET6
|
||||
*/
|
||||
int family;
|
||||
|
||||
/**
|
||||
* string representation of host
|
||||
*/
|
||||
char *string;
|
||||
|
||||
/**
|
||||
* low-lewel structure, wich stores the address
|
||||
*/
|
||||
union {
|
||||
struct sockaddr address;
|
||||
struct sockaddr_in address4;
|
||||
};
|
||||
/**
|
||||
* length of address structure
|
||||
*/
|
||||
socklen_t socklen;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* implements host_t.get_sockaddr
|
||||
*/
|
||||
static sockaddr_t *get_sockaddr(private_host_t *this)
|
||||
{
|
||||
return &(this->address);
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_sockaddr_len
|
||||
*/
|
||||
static socklen_t *get_sockaddr_len(private_host_t *this)
|
||||
{
|
||||
return &(this->socklen);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of host_t.is_default_route.
|
||||
*/
|
||||
static bool is_default_route (private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
static u_int8_t default_route[4] = {0x00,0x00,0x00,0x00};
|
||||
|
||||
if (memcmp(default_route,&(this->address4.sin_addr.s_addr),4) == 0)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* empty chunk is returned */
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_address
|
||||
*/
|
||||
static char *get_address(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
char *string;
|
||||
/* we need to clone it, since inet_ntoa overwrites
|
||||
* internal buffer on subsequent calls
|
||||
*/
|
||||
allocator_free(this->string);
|
||||
string = inet_ntoa(this->address4.sin_addr);
|
||||
this->string = allocator_alloc(strlen(string)+1);
|
||||
strcpy(this->string, string);
|
||||
return this->string;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return "(family not supported)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
memcpy(address.ptr,&(this->address4.sin_addr.s_addr),4);
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* empty chunk is returned */
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static xfrm_address_t get_xfrm_addr(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
return (xfrm_address_t)(this->address4.sin_addr.s_addr);
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* todo */
|
||||
return (xfrm_address_t)(this->address4.sin_addr.s_addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int get_family(private_host_t *this)
|
||||
{
|
||||
return this->family;
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_port
|
||||
*/
|
||||
static u_int16_t get_port(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
return ntohs(this->address4.sin_port);
|
||||
}
|
||||
default:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements host_t.clone.
|
||||
*/
|
||||
static private_host_t *clone(private_host_t *this)
|
||||
{
|
||||
private_host_t *new = allocator_alloc_thing(private_host_t);
|
||||
|
||||
|
||||
memcpy(new, this, sizeof(private_host_t));
|
||||
if (this->string)
|
||||
{
|
||||
new->string = allocator_alloc(strlen(this->string)+1);
|
||||
strcpy(new->string, this->string);
|
||||
}
|
||||
return new;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Impelements host_t.equals
|
||||
*/
|
||||
static bool 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) &&
|
||||
(this->address4.sin_port == other->address4.sin_port))
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements host_t.destroy
|
||||
*/
|
||||
static void destroy(private_host_t *this)
|
||||
{
|
||||
allocator_free(this->string);
|
||||
allocator_free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty host_t object
|
||||
*/
|
||||
static private_host_t *host_create_empty()
|
||||
{
|
||||
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_family = (int (*) (host_t*))get_family;
|
||||
this->public.get_xfrm_addr = (xfrm_address_t (*) (host_t *))get_xfrm_addr;
|
||||
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.ip_equals = (bool (*) (host_t *,host_t *)) ip_equals;
|
||||
this->public.equals = (bool (*) (host_t *,host_t *)) equals;
|
||||
this->public.is_default_route = (bool (*) (host_t *)) is_default_route;
|
||||
this->public.destroy = (void (*) (host_t*))destroy;
|
||||
|
||||
this->string = NULL;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port)
|
||||
{
|
||||
private_host_t *this = host_create_empty();
|
||||
|
||||
this->family = family;
|
||||
|
||||
switch (family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
this->address4.sin_family = AF_INET;
|
||||
this->address4.sin_addr.s_addr = inet_addr(address);
|
||||
this->address4.sin_port = htons(port);
|
||||
this->socklen = sizeof(struct sockaddr_in);
|
||||
return &(this->public);
|
||||
}
|
||||
default:
|
||||
{
|
||||
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 = host_create_empty();
|
||||
|
||||
this->family = family;
|
||||
switch (family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
if (address.len != 4)
|
||||
{
|
||||
break;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
allocator_free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,225 +0,0 @@
|
||||
/**
|
||||
* @file host.h
|
||||
*
|
||||
* @brief Interface of host_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef HOST_H_
|
||||
#define HOST_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <linux/xfrm.h>
|
||||
|
||||
#include <types.h>
|
||||
|
||||
|
||||
typedef struct host_t host_t;
|
||||
|
||||
/**
|
||||
* @brief Representates a Host
|
||||
*
|
||||
* Host object, identifies a address:port pair and defines some
|
||||
* useful functions on it.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - host_create()
|
||||
* - host_create_from_chunk()
|
||||
* - host_create_from_sockaddr()
|
||||
*
|
||||
* @todo Add IPv6 support
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
struct host_t {
|
||||
|
||||
/**
|
||||
* @brief Build a clone of this host object.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return cloned host
|
||||
*/
|
||||
host_t *(*clone) (host_t *this);
|
||||
|
||||
/**
|
||||
* @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 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 family of the address
|
||||
*
|
||||
* @param this calling object
|
||||
* @return family
|
||||
*/
|
||||
int (*get_family) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the address of this host
|
||||
*
|
||||
* Mostly used for debugging purposes.
|
||||
* @warning string must NOT be freed
|
||||
*
|
||||
* @param this object
|
||||
* @return address string,
|
||||
*/
|
||||
char* (*get_address) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Checks if the ip address of host is set to default route.
|
||||
*
|
||||
* @param this calling object
|
||||
* @return
|
||||
* - TRUE if host has IP 0.0.0.0 for default route
|
||||
* - FALSE otherwise
|
||||
*/
|
||||
bool (*is_default_route) (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
|
||||
* @return address string,
|
||||
*/
|
||||
chunk_t (*get_address_as_chunk) (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 Compare the ips of two hosts hosts.
|
||||
*
|
||||
* @param this object to compare
|
||||
* @param other the other to compare
|
||||
* @return TRUE if addresses are equal.
|
||||
*/
|
||||
bool (*ip_equals) (host_t *this, host_t *other);
|
||||
|
||||
/**
|
||||
* @brief Compare two hosts, with port.
|
||||
*
|
||||
* @param this object to compare
|
||||
* @param other the other to compare
|
||||
* @return TRUE if addresses and ports are equal.
|
||||
*/
|
||||
bool (*equals) (host_t *this, host_t *other);
|
||||
|
||||
/**
|
||||
* @brief Destroy this host object
|
||||
*
|
||||
* @param this calling
|
||||
* @return SUCCESS in any case
|
||||
*/
|
||||
void (*destroy) (host_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from an address string
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @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
|
||||
* - host_t object
|
||||
* - NULL, if family not supported.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port);
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from an address chunk
|
||||
*
|
||||
* 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
|
||||
* - 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);
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from a sockaddr struct
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @param sockaddr sockaddr struct which contains family, address and port
|
||||
* @return
|
||||
* - host_t object
|
||||
* - NULL, if family not supported.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
|
||||
|
||||
|
||||
#endif /*HOST_H_*/
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
|
||||
#include <types.h>
|
||||
#include <network/host.h>
|
||||
#include <utils/host.h>
|
||||
|
||||
|
||||
typedef struct packet_t packet_t;
|
||||
|
||||
Reference in New Issue
Block a user