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 struct ifaddrs* tmpAddr = 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 if (addr->sa_family == AF_INET) {
00111
00112 char straddr[INET_ADDRSTRLEN];
00113 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
00114 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00115 ip_address ip = straddr;
00116 if (ip.is_loopback()) continue;
00117 address_vf vf = ip;
00118 endpoints.add( vf );
00119 } else
00120 if (addr->sa_family == AF_INET6) {
00121
00122 char straddr[INET6_ADDRSTRLEN];
00123 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
00124 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
00125 ip_address ip = straddr;
00126 if (ip.is_loopback()) continue;
00127 if (ip.is_link_local()) continue;
00128 address_vf vf = ip;
00129 endpoints.add( vf );
00130 }
00131 }
00132 }
00133
00134 void AddressDiscovery::discover_endpoints( endpoint_set& endpoints ) {
00135 discover_ip_addresses( endpoints );
00136 discover_bluetooth( endpoints );
00137 }
00138
00139 }}