00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "AddressDiscovery.h"
00040 #include "ariba/config.h"
00041
00042 #include <sys/types.h>
00043 #include <sys/socket.h>
00044 #include <sys/ioctl.h>
00045 #include <sys/socket.h>
00046 #include <arpa/inet.h>
00047 #include <netinet/in.h>
00048 #include <net/if.h>
00049 #include <ifaddrs.h>
00050
00051 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00052 #include <bluetooth/bluetooth.h>
00053 #include <bluetooth/hci.h>
00054 #include <bluetooth/hci_lib.h>
00055 #endif
00056
00057 namespace ariba {
00058 namespace communication {
00059
00060 mac_address AddressDiscovery::getMacFromIF( const char* name ) {
00061 mac_address addr;
00062 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00063 int s;
00064 struct ifreq buffer;
00065 s = socket(PF_INET, SOCK_DGRAM, 0);
00066 memset(&buffer, 0x00, sizeof(buffer));
00067 strcpy(buffer.ifr_name, name);
00068 ioctl(s, SIOCGIFHWADDR, &buffer);
00069 close(s);
00070 addr.assign( (uint8_t*)buffer.ifr_hwaddr.sa_data, 6 );
00071 #endif
00072 return addr;
00073 }
00074
00075 int AddressDiscovery::dev_info(int s, int dev_id, long arg) {
00076 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00077 endpoint_set* set = (endpoint_set*)arg;
00078 struct hci_dev_info di;
00079 di.dev_id = dev_id;
00080 if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
00081 mac_address mac;
00082 mac.bluetooth( di.bdaddr );
00083 address_vf vf = mac;
00084 set->add(vf);
00085 #endif
00086 return 0;
00087 }
00088
00089 void AddressDiscovery::discover_bluetooth( endpoint_set& endpoints ) {
00090 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00091 hci_for_each_dev(HCI_UP, &AddressDiscovery::dev_info, (long)&endpoints );
00092 #endif
00093 }
00094
00095 void AddressDiscovery::discover_ip_addresses( endpoint_set& endpoints ) {
00096 struct ifaddrs* ifaceBuffer = NULL;
00097 void* tmpAddrPtr = NULL;
00098
00099 int ret = getifaddrs( &ifaceBuffer );
00100 if( ret != 0 ) return;
00101
00102 for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next ) {
00103
00104
00105 if(i == NULL) continue;
00106 struct sockaddr* addr = i->ifa_addr;
00107 if (addr==NULL) continue;
00108
00109
00110 string device = string(i->ifa_name);
00111 if(device.find_first_of("tun") == 0) continue;
00112
00113 if (addr->sa_family == AF_INET) {
00114
00115 char straddr[INET_ADDRSTRLEN];
00116 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
00117 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00118 ip_address ip = straddr;
00119 if (ip.is_loopback()) continue;
00120 address_vf vf = ip;
00121 endpoints.add( vf );
00122 } else
00123 if (addr->sa_family == AF_INET6) {
00124
00125 char straddr[INET6_ADDRSTRLEN];
00126 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
00127 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00128 ip_address ip = straddr;
00129 if (ip.is_loopback()) continue;
00130 if (ip.is_link_local()) continue;
00131 address_vf vf = ip;
00132 endpoints.add( vf );
00133 }
00134 }
00135 }
00136
00137 void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
00138 discover_ip_addresses( endpoints );
00139 discover_bluetooth( endpoints );
00140 }
00141
00142 }}