mirror of
https://github.com/strongswan/strongswan.git
synced 2025-10-15 00:00:16 -04:00
- host_t for storing host address
This commit is contained in:
parent
146e568fe7
commit
050ce09b6f
130
Source/charon/utils/host.c
Normal file
130
Source/charon/utils/host.c
Normal file
@ -0,0 +1,130 @@
|
||||
/**
|
||||
* @file host.c
|
||||
*
|
||||
* @brief host object, identifies a host and defines some useful functions on it.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 "allocator.h"
|
||||
|
||||
/**
|
||||
* @brief The logger object.
|
||||
*/
|
||||
typedef struct private_host_s private_host_t;
|
||||
struct private_host_s {
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
sockaddr_t *get_sockaddr(private_host_t *this)
|
||||
{
|
||||
return &(this->address);
|
||||
}
|
||||
|
||||
socklen_t *get_sockaddr_len(private_host_t *this)
|
||||
{
|
||||
return &(this->socklen);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements host_t-function destroy.
|
||||
* @see host_t.destroy.
|
||||
*/
|
||||
static status_t destroy(private_host_t *this)
|
||||
{
|
||||
allocator_free(this);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements host_t-function clone.
|
||||
* @see host_t.clone.
|
||||
*/
|
||||
static status_t clone(private_host_t *this, host_t **other)
|
||||
{
|
||||
private_host_t *new = allocator_alloc_thing(private_host_t);
|
||||
|
||||
if (new == NULL)
|
||||
{
|
||||
return OUT_OF_RES;
|
||||
}
|
||||
|
||||
memcpy(new, this, sizeof(private_host_t));
|
||||
*other = (host_t*)new;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* see header
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port)
|
||||
{
|
||||
private_host_t *this = allocator_alloc_thing(private_host_t);
|
||||
if (this == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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.destroy = (status_t (*) (host_t*))destroy;
|
||||
|
||||
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;
|
||||
}
|
58
Source/charon/utils/host.h
Normal file
58
Source/charon/utils/host.h
Normal file
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @file host.h
|
||||
*
|
||||
* @brief host object, identifies a host and defines some useful functions on it.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 "../types.h"
|
||||
|
||||
/**
|
||||
* @brief The logger object
|
||||
*/
|
||||
typedef struct host_s host_t;
|
||||
struct host_s {
|
||||
status_t (*clone) (host_t *this, host_t **other);
|
||||
sockaddr_t *(*get_sockaddr) (host_t *this);
|
||||
socklen_t *(*get_sockaddr_len) (host_t *this);
|
||||
status_t (*destroy) (host_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a logger_t object.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port);
|
||||
|
||||
|
||||
|
||||
#endif /*HOST_H_*/
|
Loading…
x
Reference in New Issue
Block a user