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 "NetworkInformation.h"
00040 #include "ariba/config.h"
00041
00042 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00043 #include <bluetooth/bluetooth.h>
00044 #include <bluetooth/hci.h>
00045 #include <bluetooth/hci_lib.h>
00046 #endif
00047
00048 namespace ariba {
00049 namespace communication {
00050
00051 use_logging_cpp(NetworkInformation);
00052
00053 NetworkInformation::NetworkInformation() : infoSocket( -1 ){
00054
00055 infoSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00056 }
00057
00058 NetworkInformation::~NetworkInformation(){
00059
00060 close( infoSocket );
00061 }
00062
00063 NetworkInterfaceList NetworkInformation::getInterfaces(){
00064
00065 NetworkInterfaceList retlist;
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 struct ifconf ifconf;
00078 int ifnum = 5;
00079 int lastlen = 0;
00080
00081 ifconf.ifc_buf = NULL;
00082 ifconf.ifc_len = 0;
00083
00084 while( true ){
00085
00086 ifconf.ifc_len = sizeof(struct ifreq) * ifnum;
00087 ifconf.ifc_buf = (char*)realloc( ifconf.ifc_buf, ifconf.ifc_len );
00088
00089 int ret = ioctl( infoSocket, SIOCGIFCONF, &ifconf );
00090 if (ret < 0){
00091 logging_error( "getting interface list failed with: " <<
00092 strerror(errno));
00093
00094
00095
00096 if(errno == EBADF){
00097 close( infoSocket );
00098 infoSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00099 }
00100
00101 return retlist;
00102 }
00103
00104 if( ifconf.ifc_len > lastlen ){
00105 lastlen = ifconf.ifc_len;
00106 ifnum += 10;
00107 continue;
00108 }
00109
00110 break;
00111 }
00112
00113 struct ifreq* ifr = ifconf.ifc_req;
00114
00115 for (int i = 0; i<ifconf.ifc_len; i+=sizeof(struct ifreq), ifr++){
00116 NetworkInterface interface;
00117 interface.name = string( ifr->ifr_name );
00118 retlist.push_back( interface );
00119 }
00120
00121 free( ifconf.ifc_buf );
00122
00123
00124
00125
00126
00127
00128
00129
00130 struct ifaddrs* ifap;
00131 getifaddrs( &ifap );
00132
00133 for( struct ifaddrs* p = ifap; p != NULL; p=p->ifa_next ){
00134 NetworkInterface interface;
00135 interface.name = string( p->ifa_name );
00136
00137 if( find(retlist.begin(), retlist.end(), interface) == retlist.end() )
00138 retlist.push_back( interface );
00139 }
00140
00141 freeifaddrs( ifap );
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 NetworkInterfaceList::iterator i = retlist.begin();
00152 NetworkInterfaceList::iterator iend = retlist.end();
00153
00154 for( ; i != iend; i++ ){
00155
00156 NetworkInterface* interface = &(*i);
00157 struct ifreq ifr;
00158
00159 memset( &ifr, 0, sizeof(struct ifreq) );
00160 strcpy( ifr.ifr_name, i->name.c_str() );
00161
00162
00163 {
00164 if( ioctl(infoSocket, SIOCGIFINDEX, &ifr) ){
00165 logging_error( "could not get interface index for " <<
00166 i->name << ": " << strerror(errno) );
00167 return retlist;
00168 }
00169
00170 interface->index = ifr.ifr_ifindex;
00171 }
00172
00173 {
00174 if( ioctl(infoSocket, SIOCGIFFLAGS, &ifr) ){
00175 logging_error( "could not get interface flags for " <<
00176 i->name << ": " << strerror(errno) );
00177 return retlist;
00178 }
00179
00180 interface->isRunning = (ifr.ifr_flags & IFF_RUNNING) != 0 ? true : false;
00181 interface->isUp = (ifr.ifr_flags & IFF_UP) != 0 ? true : false;
00182 interface->isLoopback = (ifr.ifr_flags & IFF_LOOPBACK) != 0 ? true : false;
00183 interface->isBroadcast = (ifr.ifr_flags & IFF_BROADCAST) != 0 ? true : false;
00184 interface->isMulticast = (ifr.ifr_flags & IFF_MULTICAST) != 0 ? true : false;
00185 }
00186
00187 {
00188 if( ioctl(infoSocket, SIOCGIFMTU, &ifr) ){
00189 logging_error( "could not get mtu for " <<
00190 i->name << ": " << strerror(errno) );
00191 return retlist;
00192 }
00193
00194 interface->mtu = ifr.ifr_mtu;
00195 }
00196
00197 {
00198 if( ioctl(infoSocket, SIOCGIFTXQLEN, &ifr) ){
00199 logging_error( "could not get tx queue length for " <<
00200 i->name << ": " << strerror(errno) );
00201 return retlist;
00202 }
00203
00204 interface->txQueueLen = ifr.ifr_qlen;
00205 }
00206
00207 }
00208
00209
00210
00211
00212
00213 #ifdef HAVE_BLUETOOTH_BLUETOOTH_H
00214
00215 int btsock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
00216 if(btsock < 0){
00217 logging_error("failed getting bluetooth raw socket");
00218 return retlist;
00219 }
00220
00221 struct hci_dev_list_req* btlist = NULL;
00222 struct hci_dev_req* btdev = NULL;
00223
00224 btlist = (hci_dev_list_req*)malloc(HCI_MAX_DEV *
00225 sizeof(struct hci_dev_list_req) + sizeof(struct hci_dev_req));
00226
00227 btlist->dev_num = HCI_MAX_DEV;
00228 btdev = btlist->dev_req;
00229
00230 if(ioctl(btsock, HCIGETDEVLIST, btlist) < 0){
00231 logging_error("failed getting requesting bluetooth devices");
00232 free(btlist);
00233 close(btsock);
00234 return retlist;
00235 }
00236
00237 btdev = btlist->dev_req;
00238
00239 for(int i=0; i<btlist->dev_num; i++, btdev++){
00240 struct hci_dev_info di;
00241 NetworkInterface interface;
00242
00243 if(hci_devinfo(btdev->dev_id, &di) < 0) continue;
00244 if(hci_test_bit(HCI_RAW, &di.flags)) continue;
00245
00246 interface.name = string(di.name);
00247 interface.index = di.dev_id;
00248 interface.mtu = di.sco_mtu;
00249 interface.isBroadcast = false;
00250 interface.isLoopback = false;
00251 interface.isMulticast = false;
00252 interface.isUp = hci_test_bit(HCI_UP, &di.flags);
00253 interface.isRunning = hci_test_bit(HCI_RUNNING, &di.flags);
00254
00255 retlist.push_back( interface );
00256 }
00257
00258 free(btlist);
00259 close(btsock);
00260 #endif
00261
00262 return retlist;
00263 }
00264
00265 NetworkInterface NetworkInformation::getInterface(int index){
00266
00267 NetworkInterfaceList ifaces = getInterfaces();
00268 NetworkInterfaceList::iterator i = ifaces.begin();
00269 NetworkInterfaceList::iterator iend = ifaces.end();
00270
00271 for( ; i != iend; i++ ){
00272 if( i->index == index ) return *i;
00273 }
00274
00275 return NetworkInterface::UNDEFINED;
00276 }
00277
00278 NetworkInterface NetworkInformation::getInterface(string name){
00279
00280 NetworkInterfaceList ifaces = getInterfaces();
00281 NetworkInterfaceList::iterator i = ifaces.begin();
00282 NetworkInterfaceList::iterator iend = ifaces.end();
00283
00284 for( ; i != iend; i++ ){
00285 if( i->name.compare(name) == 0 ) return *i;
00286 }
00287
00288 return NetworkInterface::UNDEFINED;
00289 }
00290
00291 }}