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
00041 namespace ariba {
00042 namespace communication {
00043
00044 use_logging_cpp(NetworkInformation);
00045
00046 NetworkInformation::NetworkInformation() : infoSocket( -1 ){
00047
00048 infoSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00049 }
00050
00051 NetworkInformation::~NetworkInformation(){
00052
00053 close( infoSocket );
00054 }
00055
00056 NetworkInterfaceList NetworkInformation::getInterfaces(){
00057
00058 NetworkInterfaceList retlist;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 struct ifconf ifconf;
00071 int ifnum = 5;
00072 int lastlen = 0;
00073
00074 ifconf.ifc_buf = NULL;
00075 ifconf.ifc_len = 0;
00076
00077 while( true ){
00078
00079 ifconf.ifc_len = sizeof(struct ifreq) * ifnum;
00080 ifconf.ifc_buf = (char*)realloc( ifconf.ifc_buf, ifconf.ifc_len );
00081
00082 int ret = ioctl( infoSocket, SIOCGIFCONF, &ifconf );
00083 if (ret < 0){
00084 logging_error( "getting interface list failed with: " <<
00085 strerror(errno));
00086 return retlist;
00087 }
00088
00089 if( ifconf.ifc_len > lastlen ){
00090 lastlen = ifconf.ifc_len;
00091 ifnum += 10;
00092 continue;
00093 }
00094
00095 break;
00096 }
00097
00098 struct ifreq* ifr = ifconf.ifc_req;
00099
00100 for (int i = 0; i<ifconf.ifc_len; i+=sizeof(struct ifreq), ifr++){
00101 NetworkInterface interface;
00102 interface.name = string( ifr->ifr_name );
00103 retlist.push_back( interface );
00104 }
00105
00106 free( ifconf.ifc_buf );
00107
00108
00109
00110
00111
00112
00113
00114
00115 struct ifaddrs* ifap;
00116 getifaddrs( &ifap );
00117
00118 for( struct ifaddrs* p = ifap; p != NULL; p=p->ifa_next ){
00119 NetworkInterface interface;
00120 interface.name = string( p->ifa_name );
00121
00122 if( find(retlist.begin(), retlist.end(), interface) == retlist.end() )
00123 retlist.push_back( interface );
00124 }
00125
00126 freeifaddrs( ifap );
00127
00128
00129
00130
00131
00132
00133
00134
00135 NetworkInterfaceList::iterator i = retlist.begin();
00136 NetworkInterfaceList::iterator iend = retlist.end();
00137
00138 for( ; i != iend; i++ ){
00139
00140 NetworkInterface* interface = &(*i);
00141 struct ifreq ifr;
00142
00143 memset( &ifr, 0, sizeof(struct ifreq) );
00144 strcpy( ifr.ifr_name, i->name.c_str() );
00145
00146
00147 {
00148 if( ioctl(infoSocket, SIOCGIFINDEX, &ifr) ){
00149 logging_error( "could not get interface index for " <<
00150 i->name << ": " << strerror(errno) );
00151 return retlist;
00152 }
00153
00154 interface->index = ifr.ifr_ifindex;
00155 }
00156
00157 {
00158 if( ioctl(infoSocket, SIOCGIFFLAGS, &ifr) ){
00159 logging_error( "could not get interface flags for " <<
00160 i->name << ": " << strerror(errno) );
00161 return retlist;
00162 }
00163
00164 interface->isRunning = (ifr.ifr_flags & IFF_RUNNING) != 0 ? true : false;
00165 interface->isUp = (ifr.ifr_flags & IFF_UP) != 0 ? true : false;
00166 interface->isLoopback = (ifr.ifr_flags & IFF_LOOPBACK) != 0 ? true : false;
00167 interface->isBroadcast = (ifr.ifr_flags & IFF_BROADCAST) != 0 ? true : false;
00168 interface->isMulticast = (ifr.ifr_flags & IFF_MULTICAST) != 0 ? true : false;
00169 }
00170
00171 {
00172 if( ioctl(infoSocket, SIOCGIFMTU, &ifr) ){
00173 logging_error( "could not get mtu for " <<
00174 i->name << ": " << strerror(errno) );
00175 return retlist;
00176 }
00177
00178 interface->mtu = ifr.ifr_mtu;
00179 }
00180
00181 {
00182 if( ioctl(infoSocket, SIOCGIFTXQLEN, &ifr) ){
00183 logging_error( "could not get tx queue length for " <<
00184 i->name << ": " << strerror(errno) );
00185 return retlist;
00186 }
00187
00188 interface->txQueueLen = ifr.ifr_qlen;
00189 }
00190
00191 }
00192
00193 return retlist;
00194 }
00195
00196 NetworkInterface NetworkInformation::getInterface(int index){
00197
00198 NetworkInterfaceList ifaces = getInterfaces();
00199 NetworkInterfaceList::iterator i = ifaces.begin();
00200 NetworkInterfaceList::iterator iend = ifaces.end();
00201
00202 for( ; i != iend; i++ ){
00203 if( i->index == index ) return *i;
00204 }
00205
00206 return NetworkInterface::UNDEFINED;
00207 }
00208
00209 NetworkInterface NetworkInformation::getInterface(string name){
00210
00211 NetworkInterfaceList ifaces = getInterfaces();
00212 NetworkInterfaceList::iterator i = ifaces.begin();
00213 NetworkInterfaceList::iterator iend = ifaces.end();
00214
00215 for( ; i != iend; i++ ){
00216 if( i->name.compare(name) == 0 ) return *i;
00217 }
00218
00219 return NetworkInterface::UNDEFINED;
00220 }
00221
00222 }}