Confdump Agent  1.4.0
NetworkInterface.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 extern "C"
00019 {
00020 #include <stdio.h>
00021 #include <sys/types.h>
00022 #include <sys/socket.h>
00023 #include <sys/ioctl.h>
00024 #include <netinet/in.h>
00025 #include <net/if.h>
00026 }
00027 
00028 #include <istream>
00029 #include <iostream>
00030 #include <sstream>
00031 #include <iomanip>
00032 #include <map>
00033 
00034 #include "QString"
00035 #include "boost/shared_ptr.hpp"
00036 #include "boost/bind.hpp"
00037 
00038 #include "Confdump/System/LinuxDumper.hpp"
00039 #include "Confdump/System/Linux/NetworkInterface.hpp"
00040 
00041 namespace Confdump
00042 {
00043 
00044 namespace System
00045 {
00046 
00047 namespace Linux
00048 {
00049 
00050     NetworkInterface::NetworkInterface(QString name)
00051      : name_( name )
00052     {
00053         readMacAddress();
00054     }
00055 
00056     NetworkInterface::~NetworkInterface()
00057     {
00058     }
00059 
00060     void NetworkInterface::readMacAddress()
00061     {
00062         using namespace boost::system;
00063 
00064         int fd = -1;
00065         boost::shared_ptr<void> fdGuard( static_cast<void*>( 0 ), boost::bind( close, fd ) );
00066         fd = socket( AF_INET, SOCK_DGRAM, 0 ); // We need any socket as ioctl anchor
00067         if ( fd == -1 )
00068             throw system_error( errno, system_category, "socket( AF_INET, SOCK_DGRAM, 0 )" );
00069 
00070         struct ifreq ifr;
00071         ifr.ifr_addr.sa_family = AF_INET;
00072         strncpy( ifr.ifr_name, name_.toStdString().c_str(), IFNAMSIZ - 1 );
00073         if ( ioctl( fd, SIOCGIFHWADDR, &ifr ) == -1 )
00074             throw system_error( errno, system_category, "ioctl( SIOCGIFHWADDR )" );
00075 
00076         std::ostringstream oss;
00077         oss << std::hex << std::setw( 2 ) << std::setfill( '0' ) << (ifr.ifr_hwaddr.sa_data[0]&0xff);
00078         for ( int i = 1; i < 6; ++i )
00079             oss << std::setw( 1 ) << ":" << std::setw( 2 ) << (ifr.ifr_hwaddr.sa_data[i]&0xff);
00080 
00081         macAddress_ = QString::fromStdString( oss.str() );
00082     }
00083 
00084 
00085 }
00086 }
00087 }
00088 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends