1 | // [License]
|
---|
2 | // The Ariba-Underlay Copyright
|
---|
3 | //
|
---|
4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
5 | //
|
---|
6 | // Institute of Telematics
|
---|
7 | // UniversitÀt Karlsruhe (TH)
|
---|
8 | // Zirkel 2, 76128 Karlsruhe
|
---|
9 | // Germany
|
---|
10 | //
|
---|
11 | // Redistribution and use in source and binary forms, with or without
|
---|
12 | // modification, are permitted provided that the following conditions are
|
---|
13 | // met:
|
---|
14 | //
|
---|
15 | // 1. Redistributions of source code must retain the above copyright
|
---|
16 | // notice, this list of conditions and the following disclaimer.
|
---|
17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
18 | // notice, this list of conditions and the following disclaimer in the
|
---|
19 | // documentation and/or other materials provided with the distribution.
|
---|
20 | //
|
---|
21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
32 | //
|
---|
33 | // The views and conclusions contained in the software and documentation
|
---|
34 | // are those of the authors and should not be interpreted as representing
|
---|
35 | // official policies, either expressed or implied, of the Institute of
|
---|
36 | // Telematics.
|
---|
37 | // [License]
|
---|
38 |
|
---|
39 | #include "AddressDiscovery.h"
|
---|
40 | #include "ariba/config.h"
|
---|
41 |
|
---|
42 | #include <sys/types.h>
|
---|
43 | #include <sys/socket.h>
|
---|
44 | #include <sys/ioctl.h>
|
---|
45 | #include <sys/socket.h>
|
---|
46 | #include <arpa/inet.h>
|
---|
47 | #include <netinet/in.h>
|
---|
48 | #include <net/if.h>
|
---|
49 | #include <ifaddrs.h>
|
---|
50 |
|
---|
51 | #include <string>
|
---|
52 | #include <boost/asio/ip/address.hpp>
|
---|
53 | #include <boost/foreach.hpp>
|
---|
54 |
|
---|
55 | #include "ariba/utility/addressing2/tcpip_endpoint.hpp"
|
---|
56 | #include "ariba/utility/addressing/mac_address.hpp"
|
---|
57 |
|
---|
58 | #ifdef HAVE_LIBBLUETOOTH
|
---|
59 | #include <bluetooth/bluetooth.h>
|
---|
60 | #include <bluetooth/hci.h>
|
---|
61 | #include <bluetooth/hci_lib.h>
|
---|
62 | #endif
|
---|
63 |
|
---|
64 | namespace ariba {
|
---|
65 | namespace communication {
|
---|
66 |
|
---|
67 |
|
---|
68 | using namespace std;
|
---|
69 | using namespace addressing2;
|
---|
70 | using namespace boost::asio::ip;
|
---|
71 |
|
---|
72 | using ariba::addressing::mac_address;
|
---|
73 |
|
---|
74 | mac_address getMacFromIF( const char* name )
|
---|
75 | {
|
---|
76 | mac_address addr;
|
---|
77 | #ifdef HAVE_LIBBLUETOOTH
|
---|
78 | int s;
|
---|
79 | struct ifreq buffer;
|
---|
80 | s = socket(PF_INET, SOCK_DGRAM, 0);
|
---|
81 | memset(&buffer, 0x00, sizeof(buffer));
|
---|
82 | strcpy(buffer.ifr_name, name);
|
---|
83 | ioctl(s, SIOCGIFHWADDR, &buffer);
|
---|
84 | close(s);
|
---|
85 | addr.assign( (uint8_t*)buffer.ifr_hwaddr.sa_data, 6 );
|
---|
86 | #endif
|
---|
87 | return addr;
|
---|
88 | }
|
---|
89 |
|
---|
90 | int dev_info(int s, int dev_id, long arg)
|
---|
91 | {
|
---|
92 | #ifdef HAVE_LIBBLUETOOTH
|
---|
93 | // endpoint_set* set = (endpoint_set*)arg;
|
---|
94 | // struct hci_dev_info di;
|
---|
95 | // memset(&di, 0, sizeof(struct hci_dev_info));
|
---|
96 | // di.dev_id = dev_id;
|
---|
97 | // if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
|
---|
98 | // mac_address mac;
|
---|
99 | // mac.bluetooth( di.bdaddr );
|
---|
100 | // address_vf vf = mac;
|
---|
101 | // set->add(vf);
|
---|
102 | #endif
|
---|
103 | return 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | void discover_bluetooth(
|
---|
107 | EndpointSetPtr listenOn_endpoints,
|
---|
108 | EndpointSetPtr discovered_endpoints )
|
---|
109 | {
|
---|
110 | #ifdef HAVE_LIBBLUETOOTH
|
---|
111 | // FIXME aktuell bluetooth
|
---|
112 | // hci_for_each_dev(HCI_UP, &AddressDiscovery::dev_info, (long)&endpoints );
|
---|
113 | #endif
|
---|
114 | }
|
---|
115 |
|
---|
116 | void discover_ip_addresses(
|
---|
117 | EndpointSetPtr listenOn_endpoints,
|
---|
118 | EndpointSetPtr discovered_endpoints )
|
---|
119 | {
|
---|
120 | bool discover_ipv4 = false;
|
---|
121 | bool discover_ipv6 = false;
|
---|
122 | vector<uint16_t> ipv4_ports;
|
---|
123 | vector<uint16_t> ipv6_ports;
|
---|
124 |
|
---|
125 | /* analyze listenOn_endpoints */
|
---|
126 | BOOST_FOREACH( TcpIP_EndpointPtr endp, listenOn_endpoints->get_tcpip_endpoints() )
|
---|
127 | {
|
---|
128 | // BRANCH: IPv4 any [0.0.0.0]
|
---|
129 | if ( endp->to_asio().address() == address_v4::any() )
|
---|
130 | {
|
---|
131 | // add port
|
---|
132 | ipv4_ports.push_back(endp->to_asio().port());
|
---|
133 |
|
---|
134 | discover_ipv4 = true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // BRANCH: IPv6 any [::]
|
---|
138 | else if ( endp->to_asio().address() == address_v6::any() )
|
---|
139 | {
|
---|
140 | // add port
|
---|
141 | ipv6_ports.push_back(endp->to_asio().port());
|
---|
142 |
|
---|
143 | discover_ipv6 = true;
|
---|
144 |
|
---|
145 |
|
---|
146 | // NOTE: on linux the ipv6-any address [::] catches ipv4 as well
|
---|
147 | ipv4_ports.push_back(endp->to_asio().port());
|
---|
148 | discover_ipv4 = true;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // BRANCH: explicit ip address
|
---|
152 | else
|
---|
153 | {
|
---|
154 | // ---> don't discover anything, just add it directly
|
---|
155 | discovered_endpoints->add_endpoint(endp);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /* discover addresses */
|
---|
161 | if ( discover_ipv4 || discover_ipv6 )
|
---|
162 | {
|
---|
163 | struct ifaddrs* ifaceBuffer = NULL;
|
---|
164 | void* tmpAddrPtr = NULL;
|
---|
165 |
|
---|
166 | int ret = getifaddrs( &ifaceBuffer );
|
---|
167 | if( ret != 0 ) return;
|
---|
168 |
|
---|
169 | for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next )
|
---|
170 | {
|
---|
171 | // ignore devices that are disabled or have no ip
|
---|
172 | if(i == NULL) continue;
|
---|
173 | struct sockaddr* addr = i->ifa_addr;
|
---|
174 | if (addr==NULL) continue;
|
---|
175 |
|
---|
176 | // // ignore tun devices // XXX why?
|
---|
177 | // string device = string(i->ifa_name);
|
---|
178 | // if(device.find_first_of("tun") == 0) continue;
|
---|
179 |
|
---|
180 | // IPv4
|
---|
181 | if ( discover_ipv4 && addr->sa_family == AF_INET )
|
---|
182 | {
|
---|
183 | char straddr[INET_ADDRSTRLEN];
|
---|
184 | tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
|
---|
185 | inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
|
---|
186 |
|
---|
187 | address ip_addr = address::from_string(straddr);
|
---|
188 |
|
---|
189 | // skip loopback address
|
---|
190 | if ( ip_addr.to_v4() == address_v4::loopback() )
|
---|
191 | continue;
|
---|
192 |
|
---|
193 | // add endpoint for this address and every given ipv4 port
|
---|
194 | BOOST_FOREACH( uint16_t port, ipv4_ports )
|
---|
195 | {
|
---|
196 | tcp::endpoint tcpip_endp(ip_addr, port);
|
---|
197 | TcpIP_EndpointPtr endp(new tcpip_endpoint(tcpip_endp));
|
---|
198 |
|
---|
199 | discovered_endpoints->add_endpoint(endp);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | // IPv6
|
---|
204 | else if ( discover_ipv6 && addr->sa_family == AF_INET6 )
|
---|
205 | {
|
---|
206 | // look for ipv6
|
---|
207 | char straddr[INET6_ADDRSTRLEN];
|
---|
208 | tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
|
---|
209 | inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
|
---|
210 |
|
---|
211 | address ip_addr = address::from_string(straddr);
|
---|
212 |
|
---|
213 | // skip loopback address
|
---|
214 | if ( ip_addr.to_v6() == address_v6::loopback() )
|
---|
215 | continue;
|
---|
216 |
|
---|
217 | // add endpoint for this address and every given ipv4 port
|
---|
218 | BOOST_FOREACH( uint16_t port, ipv6_ports )
|
---|
219 | {
|
---|
220 | tcp::endpoint tcpip_endp(ip_addr, port);
|
---|
221 | TcpIP_EndpointPtr endp(new tcpip_endpoint(tcpip_endp));
|
---|
222 |
|
---|
223 | discovered_endpoints->add_endpoint(endp);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | freeifaddrs(ifaceBuffer);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 |
|
---|
234 | EndpointSetPtr AddressDiscovery::discover_endpoints(EndpointSetPtr listenOn_endpoints)
|
---|
235 | {
|
---|
236 | EndpointSetPtr discovered_endpoints(new addressing2::endpoint_set());
|
---|
237 |
|
---|
238 | discover_ip_addresses( listenOn_endpoints, discovered_endpoints );
|
---|
239 | discover_bluetooth( listenOn_endpoints, discovered_endpoints );
|
---|
240 |
|
---|
241 | return discovered_endpoints;
|
---|
242 | }
|
---|
243 |
|
---|
244 | }} // namespace ariba, communication
|
---|