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 memset(&di, 0, sizeof(struct hci_dev_info));
00080 di.dev_id = dev_id;
00081 if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
00082 mac_address mac;
00083 mac.bluetooth( di.bdaddr );
00084 address_vf vf = mac;
00085 set->add(vf);
00086 #endif
00087 return 0;
00088 }
00089
00090 void AddressDiscovery::discover_bluetooth( endpoint_set& endpoints ) {
00091 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00092 hci_for_each_dev(HCI_UP, &AddressDiscovery::dev_info, (long)&endpoints );
00093 #endif
00094 }
00095
00096 void AddressDiscovery::discover_ip_addresses( endpoint_set& endpoints ) {
00097 struct ifaddrs* ifaceBuffer = NULL;
00098 void* tmpAddrPtr = NULL;
00099
00100 int ret = getifaddrs( &ifaceBuffer );
00101 if( ret != 0 ) return;
00102
00103 for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next ) {
00104
00105
00106 if(i == NULL) continue;
00107 struct sockaddr* addr = i->ifa_addr;
00108 if (addr==NULL) continue;
00109
00110
00111 string device = string(i->ifa_name);
00112 if(device.find_first_of("tun") == 0) continue;
00113
00114 if (addr->sa_family == AF_INET) {
00115
00116 char straddr[INET_ADDRSTRLEN];
00117 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
00118 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00119 ip_address ip = straddr;
00120 if (ip.is_loopback()) continue;
00121 address_vf vf = ip;
00122 endpoints.add( vf );
00123 } else
00124 if (addr->sa_family == AF_INET6) {
00125
00126 char straddr[INET6_ADDRSTRLEN];
00127 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
00128 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00129 ip_address ip = straddr;
00130 if (ip.is_loopback()) continue;
00131 if (ip.is_link_local()) continue;
00132 address_vf vf = ip;
00133 endpoints.add( vf );
00134 }
00135 }
00136
00137 freeifaddrs(ifaceBuffer);
00138 }
00139
00140 void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
00141 discover_ip_addresses( endpoints );
00142 discover_bluetooth( endpoints );
00143 }
00144
00145 }}