source: source/ariba/communication/networkinfo/AddressDiscovery.hpp@ 5554

Last change on this file since 5554 was 5406, checked in by mies, 15 years ago
File size: 2.6 KB
RevLine 
[5284]1#ifndef ADDRESSDISCOVERY_HPP_
2#define ADDRESSDISCOVERY_HPP_
3
4#include "ariba/utility/addressing/addressing.hpp"
5
6#include <sys/types.h>
7#include <sys/socket.h>
[5406]8#include <sys/ioctl.h>
9#include <sys/socket.h>
10
[5284]11#include <arpa/inet.h>
[5406]12
[5284]13#include <netinet/in.h>
[5406]14
[5284]15#include <net/if.h>
[5406]16
[5284]17#include <ifaddrs.h>
18
[5406]19#include <bluetooth/bluetooth.h>
20#include <bluetooth/hci.h>
21#include <bluetooth/hci_lib.h>
22
[5284]23using namespace ariba::addressing;
24
25mac_address getMacFromIF( const char* name ) {
26 int s;
27 struct ifreq buffer;
28 s = socket(PF_INET, SOCK_DGRAM, 0);
29 memset(&buffer, 0x00, sizeof(buffer));
30 strcpy(buffer.ifr_name, name);
31 ioctl(s, SIOCGIFHWADDR, &buffer);
32 close(s);
33 mac_address addr;
34 addr.assign( (uint8_t*)buffer.ifr_hwaddr.sa_data, 6 );
35 return addr;
36}
37
[5406]38
39int dev_info(int s, int dev_id, long arg) {
40 endpoint_set* set = (endpoint_set*)arg;
41 struct hci_dev_info di;
42 di.dev_id = dev_id;
43 if (ioctl(s, HCIGETDEVINFO, (void *) &di)) return 0;
44 mac_address mac;
45 mac.bluetooth( di.bdaddr );
46 address_vf vf = mac;
47 set->add(vf);
48 return 0;
49}
50
51void discover_bluetooth( endpoint_set& endpoints ) {
52 hci_for_each_dev(HCI_UP, &dev_info, (long)&endpoints );
53}
54
55void discover_ip_addresses( endpoint_set& endpoints ) {
[5284]56 struct ifaddrs* ifaceBuffer = NULL;
57 struct ifaddrs* tmpAddr = NULL;
58 void* tmpAddrPtr = NULL;
59 char straddr [INET_ADDRSTRLEN];
60
61 int ret = getifaddrs( &ifaceBuffer );
62 if( ret != 0 ) return;
63
64 for( struct ifaddrs* i=ifaceBuffer; i != NULL; i=i->ifa_next ) {
65
66 // ignore devices that are disabled or have no ip
67 if(i == NULL) continue;
68 struct sockaddr* addr = i->ifa_addr;
69
70 // only look at IPv4, not IPv6 addresses
71 if (addr->sa_family == AF_INET) {
72 if (addr==NULL) continue;
73 tmpAddrPtr= &((struct sockaddr_in*)addr)->sin_addr;
74 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
75 ip_address ip = straddr;
76 if (ip.is_loopback()) continue;
77 address_vf vf = ip;
78 endpoints.add( vf );
79 } else
80 if (addr->sa_family == AF_INET6) {
81 if (addr==NULL) continue;
82 tmpAddrPtr= &((struct sockaddr_in6*)addr)->sin6_addr;
83 inet_ntop( i->ifa_addr->sa_family, tmpAddrPtr, straddr, sizeof(straddr) );
84 ip_address ip = straddr;
85 if (ip.is_loopback()) continue;
86 address_vf vf = ip;
87 endpoints.add( vf );
88 } else
89 if (i->ifa_name[0]=='p' && i->ifa_name[1]=='a' && i->ifa_name[2]=='n') {
90 mac_address mac = getMacFromIF(i->ifa_name);
91 address_vf vf = mac;
[5406]92// endpoints.add( vf );
[5284]93 }
94 }
95}
96
[5406]97void discover_endpoints( endpoint_set& endpoints ) {
98 discover_ip_addresses( endpoints );
99 discover_bluetooth( endpoints );
100}
101
[5284]102#endif /* ADDRESSDISCOVERY_HPP_ */
Note: See TracBrowser for help on using the repository browser.