4 * @brief Implementation of host_t.
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 typedef struct private_host_t private_host_t
;
31 * @brief Private Data of a host object.
33 struct private_host_t
{
40 * Address family to use, such as AF_INET or AF_INET6
45 * low-lewel structure, wich stores the address
50 * length of address structure
57 * implements host_t.get_sockaddr
59 static sockaddr_t
*get_sockaddr(private_host_t
*this)
61 return &(this->address
);
65 * implements host_t.get_sockaddr_len
67 static socklen_t
*get_sockaddr_len(private_host_t
*this)
69 return &(this->socklen
);
73 * Implementation of host_t.is_default_route.
75 static bool is_default_route (private_host_t
*this)
82 for (i
= 0; i
< 4;i
++)
84 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
85 if (*((&sin
->sin_addr
.s_addr
) + i
) != 0)
94 /* empty chunk is returned */
101 * implements host_t.get_address
103 static char *get_address(private_host_t
*this)
105 switch (this->family
)
109 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
110 return inet_ntoa(sin
->sin_addr
);
114 return "(family not supported)";
120 * Implementation of host_t.get_address_as_chunk.
122 static chunk_t
get_address_as_chunk(private_host_t
*this)
124 chunk_t address
= CHUNK_INITIALIZER
;
126 switch (this->family
)
130 /* allocate 4 bytes for IPV4 address*/
131 address
.ptr
= allocator_alloc(4);
133 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
134 memcpy(address
.ptr
,&(sin
->sin_addr
.s_addr
),4);
138 /* empty chunk is returned */
146 * implements host_t.get_port
148 static u_int16_t
get_port(private_host_t
*this)
150 switch (this->family
)
154 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
155 return ntohs(sin
->sin_port
);
165 * Implements host_t.destroy
167 static void destroy(private_host_t
*this)
169 allocator_free(this);
173 * Implements host_t.clone.
175 static private_host_t
*clone(private_host_t
*this)
177 private_host_t
*new = allocator_alloc_thing(private_host_t
);
179 memcpy(new, this, sizeof(private_host_t
));
185 * Impelements host_t.equals
187 static bool ip_is_equal(private_host_t
*this, private_host_t
*other
)
189 switch (this->family
)
194 struct sockaddr_in
*sin1
= (struct sockaddr_in
*)&(this->address
);
195 struct sockaddr_in
*sin2
= (struct sockaddr_in
*)&(other
->address
);
196 if ((sin1
->sin_family
== sin2
->sin_family
) &&
197 (sin1
->sin_addr
.s_addr
== sin2
->sin_addr
.s_addr
))
208 * Creates an empty host_t object
210 static private_host_t
*host_create_empty()
212 private_host_t
*this = allocator_alloc_thing(private_host_t
);
214 this->public.get_sockaddr
= (sockaddr_t
* (*) (host_t
*))get_sockaddr
;
215 this->public.get_sockaddr_len
= (socklen_t
*(*) (host_t
*))get_sockaddr_len
;
216 this->public.clone
= (host_t
* (*) (host_t
*))clone
;
217 this->public.get_address
= (char* (*) (host_t
*))get_address
;
218 this->public.get_address_as_chunk
= (chunk_t (*) (host_t
*)) get_address_as_chunk
;
219 this->public.get_port
= (u_int16_t (*) (host_t
*))get_port
;
220 this->public.ip_is_equal
= (bool (*) (host_t
*,host_t
*)) ip_is_equal
;
221 this->public.is_default_route
= (bool (*) (host_t
*)) is_default_route
;
222 this->public.destroy
= (void (*) (host_t
*))destroy
;
228 * Described in header.
230 host_t
*host_create(int family
, char *address
, u_int16_t port
)
232 private_host_t
*this = host_create_empty();
234 this->family
= family
;
241 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
242 sin
->sin_family
= AF_INET
;
243 sin
->sin_addr
.s_addr
= inet_addr(address
);
244 sin
->sin_port
= htons(port
);
245 this->socklen
= sizeof(struct sockaddr_in
);
246 return &(this->public);
250 allocator_free(this);
259 * Described in header.
261 host_t
*host_create_from_chunk(int family
, chunk_t address
, u_int16_t port
)
263 private_host_t
*this = host_create_empty();
265 this->family
= family
;
267 if (address
.len
== 4)
274 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
275 sin
->sin_family
= AF_INET
;
276 memcpy(&(sin
->sin_addr
.s_addr
),address
.ptr
,4);
277 sin
->sin_port
= htons(port
);
278 this->socklen
= sizeof(struct sockaddr_in
);
279 return &(this->public);
284 allocator_free(this);