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 * string representation of host
50 * low-lewel structure, wich stores the address
55 * length of address structure
62 * implements host_t.get_sockaddr
64 static sockaddr_t
*get_sockaddr(private_host_t
*this)
66 return &(this->address
);
70 * implements host_t.get_sockaddr_len
72 static socklen_t
*get_sockaddr_len(private_host_t
*this)
74 return &(this->socklen
);
78 * Implementation of host_t.is_default_route.
80 static bool is_default_route (private_host_t
*this)
86 static u_int8_t default_route
[4] = {0x00,0x00,0x00,0x00};
87 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
89 if (memcmp(default_route
,&(sin
->sin_addr
.s_addr
),4) == 0)
97 /* empty chunk is returned */
104 * implements host_t.get_address
106 static char *get_address(private_host_t
*this)
108 switch (this->family
)
113 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
114 allocator_free(this->string
);
115 string
= inet_ntoa(sin
->sin_addr
);
116 this->string
= allocator_alloc(strlen(string
)+1);
117 strcpy(this->string
, string
);
122 return "(family not supported)";
128 * Implementation of host_t.get_address_as_chunk.
130 static chunk_t
get_address_as_chunk(private_host_t
*this)
132 chunk_t address
= CHUNK_INITIALIZER
;
134 switch (this->family
)
138 /* allocate 4 bytes for IPV4 address*/
139 address
.ptr
= allocator_alloc(4);
141 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
142 memcpy(address
.ptr
,&(sin
->sin_addr
.s_addr
),4);
146 /* empty chunk is returned */
154 * implements host_t.get_port
156 static u_int16_t
get_port(private_host_t
*this)
158 switch (this->family
)
162 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
163 return ntohs(sin
->sin_port
);
174 * Implements host_t.clone.
176 static private_host_t
*clone(private_host_t
*this)
178 private_host_t
*new = allocator_alloc_thing(private_host_t
);
181 memcpy(new, this, sizeof(private_host_t
));
184 new->string
= allocator_alloc(strlen(this->string
)+1);
185 strcpy(new->string
, this->string
);
192 * Impelements host_t.equals
194 static bool ip_is_equal(private_host_t
*this, private_host_t
*other
)
196 switch (this->family
)
201 struct sockaddr_in
*sin1
= (struct sockaddr_in
*)&(this->address
);
202 struct sockaddr_in
*sin2
= (struct sockaddr_in
*)&(other
->address
);
203 if ((sin1
->sin_family
== sin2
->sin_family
) &&
204 (sin1
->sin_addr
.s_addr
== sin2
->sin_addr
.s_addr
))
214 * Implements host_t.destroy
216 static void destroy(private_host_t
*this)
218 allocator_free(this->string
);
219 allocator_free(this);
223 * Creates an empty host_t object
225 static private_host_t
*host_create_empty()
227 private_host_t
*this = allocator_alloc_thing(private_host_t
);
229 this->public.get_sockaddr
= (sockaddr_t
* (*) (host_t
*))get_sockaddr
;
230 this->public.get_sockaddr_len
= (socklen_t
*(*) (host_t
*))get_sockaddr_len
;
231 this->public.clone
= (host_t
* (*) (host_t
*))clone
;
232 this->public.get_address
= (char* (*) (host_t
*))get_address
;
233 this->public.get_address_as_chunk
= (chunk_t (*) (host_t
*)) get_address_as_chunk
;
234 this->public.get_port
= (u_int16_t (*) (host_t
*))get_port
;
235 this->public.ip_is_equal
= (bool (*) (host_t
*,host_t
*)) ip_is_equal
;
236 this->public.is_default_route
= (bool (*) (host_t
*)) is_default_route
;
237 this->public.destroy
= (void (*) (host_t
*))destroy
;
245 * Described in header.
247 host_t
*host_create(int family
, char *address
, u_int16_t port
)
249 private_host_t
*this = host_create_empty();
251 this->family
= family
;
258 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
259 sin
->sin_family
= AF_INET
;
260 sin
->sin_addr
.s_addr
= inet_addr(address
);
261 sin
->sin_port
= htons(port
);
262 this->socklen
= sizeof(struct sockaddr_in
);
263 return &(this->public);
267 allocator_free(this);
276 * Described in header.
278 host_t
*host_create_from_chunk(int family
, chunk_t address
, u_int16_t port
)
280 private_host_t
*this = host_create_empty();
282 this->family
= family
;
284 if (address
.len
== 4)
291 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&(this->address
);
292 sin
->sin_family
= AF_INET
;
293 memcpy(&(sin
->sin_addr
.s_addr
),address
.ptr
,4);
294 sin
->sin_port
= htons(port
);
295 this->socklen
= sizeof(struct sockaddr_in
);
296 return &(this->public);
301 allocator_free(this);