4 * @brief Implementation of interfaces_t.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
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
27 #include "interfaces.h"
29 typedef struct private_interfaces_t private_interfaces_t
;
32 * Private data of an interfaces_t object.
34 struct private_interfaces_t
{
37 * Public part of a interfaces_t object.
42 * port that gets added to the host_t obbjects
49 linked_list_t
*addresses
;
53 * Implements interfaces_t.get_addresses
55 static linked_list_t
* get_addresses(private_interfaces_t
*this)
57 return this->addresses
;
61 * Implements interfaces_t.is_local_address
63 static bool is_local_address(private_interfaces_t
*this, host_t
*host
)
68 if (host
->is_anyaddr(host
))
73 iterator
= this->addresses
->create_iterator(this->addresses
, TRUE
);
74 while (iterator
->iterate(iterator
, (void**)&lhost
))
76 if (host
->get_family(host
) == lhost
->get_family(lhost
) &&
77 streq(host
->get_address(host
), lhost
->get_address(lhost
)))
79 iterator
->destroy(iterator
);
84 iterator
->destroy(iterator
);
89 * Implements interfaces_t.destroy.
91 static void destroy(private_interfaces_t
*this)
94 while (this->addresses
->remove_last(this->addresses
, (void**)&host
) == SUCCESS
)
98 this->addresses
->destroy(this->addresses
);
102 static status_t
initialize(private_interfaces_t
*this)
104 struct ifaddrs
*list
;
108 if (getifaddrs(&list
) < 0)
113 for (cur
= list
; cur
!= NULL
; cur
= cur
->ifa_next
)
115 if (!(cur
->ifa_flags
& IFF_UP
))
118 if (cur
->ifa_addr
== NULL
|| cur
->ifa_addr
->sa_family
!= AF_INET
)
121 host
= host_create_from_sockaddr(cur
->ifa_addr
);
123 host
->set_port(host
, this->port
);
124 this->addresses
->insert_last(this->addresses
, (void*) host
);
133 * Documented in header
135 interfaces_t
*interfaces_create(u_int16_t port
)
137 private_interfaces_t
*this = malloc_thing(private_interfaces_t
);
141 this->public.get_addresses
= (linked_list_t
* (*) (interfaces_t
*)) get_addresses
;
142 this->public.is_local_address
= (bool (*) (interfaces_t
*, host_t
*)) is_local_address
;
143 this->public.destroy
= (void (*) (interfaces_t
*)) destroy
;
145 this->addresses
= linked_list_create();
147 if (initialize(this) != SUCCESS
)
152 return &this->public;