4 * @brief host object, identifies a host and defines some useful functions on it.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <utils/allocator.h>
28 * @brief The logger object.
30 typedef struct private_host_s private_host_t
;
31 struct private_host_s
{
38 * Address family to use, such as AF_INET or AF_INET6
43 * low-lewel structure, wich stores the address
48 * length of address structure
55 * implements host_t.get_sockaddr
57 static sockaddr_t
*get_sockaddr(private_host_t
*this)
59 return &(this->address
);
63 * implements host_t.get_sockaddr_len
65 static socklen_t
*get_sockaddr_len(private_host_t
*this)
67 return &(this->socklen
);
71 * implements host_t.get_address
73 static char *get_address(private_host_t
*this)
79 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
80 return inet_ntoa(sin
->sin_addr
);
84 return "(family not supported)";
90 * implements host_t.get_port
92 static u_int16_t
get_port(private_host_t
*this)
98 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
99 return ntohs(sin
->sin_port
);
109 * Implements host_t.destroy
111 static status_t
destroy(private_host_t
*this)
113 allocator_free(this);
118 * Implements host_t.clone.
120 static status_t
clone(private_host_t
*this, host_t
**other
)
122 private_host_t
*new = allocator_alloc_thing(private_host_t
);
129 memcpy(new, this, sizeof(private_host_t
));
130 *other
= (host_t
*)new;
139 host_t
*host_create(int family
, char *address
, u_int16_t port
)
141 private_host_t
*this = allocator_alloc_thing(private_host_t
);
147 this->public.get_sockaddr
= (sockaddr_t
* (*) (host_t
*))get_sockaddr
;
148 this->public.get_sockaddr_len
= (socklen_t
*(*) (host_t
*))get_sockaddr_len
;
149 this->public.clone
= (status_t (*) (host_t
*, host_t
**))clone
;
150 this->public.get_address
= (char* (*) (host_t
*))get_address
;
151 this->public.get_port
= (u_int16_t (*) (host_t
*))get_port
;
152 this->public.destroy
= (status_t (*) (host_t
*))destroy
;
154 this->family
= family
;
161 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
162 sin
->sin_family
= AF_INET
;
163 sin
->sin_addr
.s_addr
= inet_addr(address
);
164 sin
->sin_port
= htons(port
);
165 this->socklen
= sizeof(struct sockaddr_in
);
166 return (host_t
*)this;
169 allocator_free(this);