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 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
|
---|
52 | #include <bluetooth/bluetooth.h>
|
---|
53 | #include <bluetooth/hci.h>
|
---|
54 | #include <bluetooth/hci_lib.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | namespace ariba {
|
---|
58 | namespace communication {
|
---|
59 |
|
---|
60 | mac_address AddressDiscovery::getMacFromIF( const char* name ) {
|
---|
61 | mac_address addr;
|
---|
62 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
|
---|
63 | int s;
|
---|
64 | struct ifreq buffer;
|
---|
65 | s = socket(PF_INET, SOCK_DGRAM, 0);
|
---|
66 | memset(&buffer, 0x00, sizeof(buffer));
|
---|
67 | strcpy(buffer.ifr_name, name);
|
---|
68 | ioctl(s, SIOCGIFHWADDR, &buffer);
|
---|
69 | close(s);
|
---|
70 | addr.assign( (uint8_t*)buffer.ifr_hwaddr.sa_data, 6 );
|
---|
71 | #endif
|
---|
72 | return addr;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int AddressDiscovery::dev_info(int s, int dev_id, long arg) {
|
---|
76 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
|
---|
77 | endpoint_set* set = (endpoint_set*)arg;
|
---|
78 | struct hci_dev_info di;
|
---|
79 | di.dev_id = dev_id;
|
---|
80 | if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
|
---|
81 | mac_address mac;
|
---|
82 | mac.bluetooth( di.bdaddr );
|
---|
83 | address_vf vf = mac;
|
---|
84 | set->add(vf);
|
---|
85 | #endif
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void AddressDiscovery::discover_bluetooth( endpoint_set& endpoints ) {
|
---|
90 | #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
|
---|
91 | hci_for_each_dev(HCI_UP, &AddressDiscovery::dev_info, (long)&endpoints );
|
---|
92 | #endif
|
---|
93 | }
|
---|
94 |
|
---|
95 | void AddressDiscovery::discover_ip_addresses( endpoint_set& endpoints ) {
|
---|
96 | struct ifaddrs* ifaceBuffer = NULL;
|
---|
97 | void* tmpAddrPtr = NULL;
|
---|
98 |
|
---|
99 | int ret = getifaddrs( &ifaceBuffer );
|
---|
100 | if( ret != 0 ) return;
|
---|
101 |
|
---|
102 | for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next ) {
|
---|
103 |
|
---|
104 | // ignore devices that are disabled or have no ip
|
---|
105 | if(i == NULL) continue;
|
---|
106 | struct sockaddr* addr = i->ifa_addr;
|
---|
107 | if (addr==NULL) continue;
|
---|
108 |
|
---|
109 | // ignore tun devices
|
---|
110 | string device = string(i->ifa_name);
|
---|
111 | if(device.find_first_of("tun") == 0) continue;
|
---|
112 |
|
---|
113 | if (addr->sa_family == AF_INET) {
|
---|
114 | // look for ipv4
|
---|
115 | char straddr[INET_ADDRSTRLEN];
|
---|
116 | tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
|
---|
117 | inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
|
---|
118 | ip_address ip = straddr;
|
---|
119 | if (ip.is_loopback()) continue;
|
---|
120 | address_vf vf = ip;
|
---|
121 | endpoints.add( vf );
|
---|
122 | } else
|
---|
123 | if (addr->sa_family == AF_INET6) {
|
---|
124 | // look for ipv6
|
---|
125 | char straddr[INET6_ADDRSTRLEN];
|
---|
126 | tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
|
---|
127 | inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
|
---|
128 | ip_address ip = straddr;
|
---|
129 | if (ip.is_loopback()) continue;
|
---|
130 | if (ip.is_link_local()) continue;
|
---|
131 | address_vf vf = ip;
|
---|
132 | endpoints.add( vf );
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
|
---|
138 | discover_ip_addresses( endpoints );
|
---|
139 | discover_bluetooth( endpoints );
|
---|
140 | }
|
---|
141 |
|
---|
142 | }} // namespace ariba, communication
|
---|