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

Last change on this file since 9991 was 8620, checked in by Christoph Mayer, 14 years ago

-mem leaks

File size: 4.5 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 memset(&di, 0, sizeof(struct hci_dev_info));
80 di.dev_id = dev_id;
81 if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
82 mac_address mac;
83 mac.bluetooth( di.bdaddr );
84 address_vf vf = mac;
85 set->add(vf);
86#endif
87 return 0;
88}
89
90void AddressDiscovery::discover_bluetooth( endpoint_set& endpoints ) {
91#ifdef HAVE_BLUETOOTH_BLUETOOTH_H
92 hci_for_each_dev(HCI_UP, &AddressDiscovery::dev_info, (long)&endpoints );
93#endif
94}
95
96void AddressDiscovery::discover_ip_addresses( endpoint_set& endpoints ) {
97 struct ifaddrs* ifaceBuffer = NULL;
98 void* tmpAddrPtr = NULL;
99
100 int ret = getifaddrs( &ifaceBuffer );
101 if( ret != 0 ) return;
102
103 for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next ) {
104
105 // ignore devices that are disabled or have no ip
106 if(i == NULL) continue;
107 struct sockaddr* addr = i->ifa_addr;
108 if (addr==NULL) continue;
109
110 // ignore tun devices
111 string device = string(i->ifa_name);
112 if(device.find_first_of("tun") == 0) continue;
113
114 if (addr->sa_family == AF_INET) {
115 // look for ipv4
116 char straddr[INET_ADDRSTRLEN];
117 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
118 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
119 ip_address ip = straddr;
120 if (ip.is_loopback()) continue;
121 address_vf vf = ip;
122 endpoints.add( vf );
123 } else
124 if (addr->sa_family == AF_INET6) {
125 // look for ipv6
126 char straddr[INET6_ADDRSTRLEN];
127 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
128 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
129 ip_address ip = straddr;
130 if (ip.is_loopback()) continue;
131 if (ip.is_link_local()) continue;
132 address_vf vf = ip;
133 endpoints.add( vf );
134 }
135 }
136
137 freeifaddrs(ifaceBuffer);
138}
139
140void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
141 discover_ip_addresses( endpoints );
142 discover_bluetooth( endpoints );
143}
144
145}} // namespace ariba, communication
Note: See TracBrowser for help on using the repository browser.