source: source/ariba/communication/networkinfo/AddressDiscovery.cpp@ 6956

Last change on this file since 6956 was 6919, checked in by mies, 15 years ago

Fixed tons of warnings when using CXXFLAGS="-Wall"!

File size: 4.3 KB
Line 
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
57namespace ariba {
58namespace communication {
59
60mac_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
75int 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
89void 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
95void 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 if (addr->sa_family == AF_INET) {
110 // look for ipv4
111 char straddr[INET_ADDRSTRLEN];
112 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
113 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
114 ip_address ip = straddr;
115 if (ip.is_loopback()) continue;
116 address_vf vf = ip;
117 endpoints.add( vf );
118 } else
119 if (addr->sa_family == AF_INET6) {
120 // look for ipv6
121 char straddr[INET6_ADDRSTRLEN];
122 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
123 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
124 ip_address ip = straddr;
125 if (ip.is_loopback()) continue;
126 if (ip.is_link_local()) continue;
127 address_vf vf = ip;
128 endpoints.add( vf );
129 }
130 }
131}
132
133void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
134 discover_ip_addresses( endpoints );
135 discover_bluetooth( endpoints );
136}
137
138}} // namespace ariba, communication
Note: See TracBrowser for help on using the repository browser.