Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

NetdeviceController.cc

Go to the documentation of this file.
00001 // $Id: NetdeviceController.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Thorsten Horstmann <tho@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00027 #include "NetdeviceController.hh"
00028 //#include "NetdeviceController.ih"
00029 
00030 // Custom includes
00031 #include <sys/socket.h>
00032 #include <sys/ioctl.h>
00033 #include <net/if.h>
00034 #include <boost/weak_ptr.hpp>
00035 #include <senf/Utils/Exception.hh>
00036 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
00037 
00038 #define prefix_
00039 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00040 
00041 #define doIoctl(ifr, request, errorMsg)                                                         \
00042     if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )                                            \
00043         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
00044 
00045 
00046 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
00047     : sockfd_ (sockfd())
00048 {
00049     struct ifreq ifr;
00050     ::memset( &ifr, 0, sizeof(ifr));
00051     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
00052     doIoctl(ifr, SIOCGIFINDEX, "Could not discover the index of interface \"" + interface_name + "\"");
00053     ifindex_ = ifr.ifr_ifindex;
00054 }
00055 
00056 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
00057     : sockfd_ (sockfd())
00058 {
00059     ifindex_ = interface_index;
00060 }
00061 
00062 prefix_ std::string senf::NetdeviceController::interfaceName()
00063     const
00064 {
00065     struct ifreq ifr;
00066     ifrName( ifr);
00067     return std::string( ifr.ifr_name);
00068 }
00069 
00070 prefix_ void senf::NetdeviceController::interfaceName(std::string const & newname)
00071 {
00072     if (sizeof(newname) <= IFNAMSIZ) {
00073         struct ifreq ifr;
00074         ifrName(ifr);
00075         strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
00076         doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
00077     }
00078     return;
00079 }
00080 
00081 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
00082     const
00083 {
00084     struct ifreq ifr;
00085     ifrName( ifr);
00086     doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
00087     return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
00088 }
00089 
00090 prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & newAddress) {
00091     struct ifreq ifr;
00092     ifrName( ifr);
00093     ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
00094     std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
00095     doIoctl(ifr, SIOCSIFHWADDR, "Could not change the interface MAC address. Is the interface really down?");
00096 }
00097 
00098 prefix_ int senf::NetdeviceController::mtu()
00099     const
00100 {
00101     struct ifreq ifr;
00102     ifrName( ifr);
00103     doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
00104     return ifr.ifr_mtu;
00105 }
00106 
00107 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
00108 {
00109     struct ifreq ifr;
00110     ifrName( ifr);
00111     ifr.ifr_mtu = new_mtu;
00112     doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
00113 }
00114 
00115 prefix_ int senf::NetdeviceController::txqueuelen()
00116     const
00117 {
00118     struct ifreq ifr;
00119     ifrName( ifr);
00120     doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
00121     return ifr.ifr_qlen;
00122 }
00123 
00124 prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
00125 {
00126     struct ifreq ifr;
00127     ifrName( ifr);
00128     ifr.ifr_qlen = new_txqueuelen;
00129     doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
00130 }
00131 
00132 
00133 prefix_ bool senf::NetdeviceController::promisc()
00134     const
00135 {
00136     struct ifreq ifr;
00137     ifrName( ifr);
00138     doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
00139     return ifr.ifr_flags & IFF_PROMISC;
00140 }
00141 
00142 prefix_ void senf::NetdeviceController::promisc(bool mode)
00143 {
00144     struct ifreq ifr;
00145     ifrName( ifr);
00146     doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
00147     if (mode)
00148         ifr.ifr_flags |= IFF_PROMISC;
00149     else
00150         ifr.ifr_flags &= ~IFF_PROMISC;
00151     doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
00152 }
00153 
00154 prefix_ bool senf::NetdeviceController::isUp()
00155     const
00156 {
00157     struct ifreq ifr;
00158     ifrName(ifr);
00159     doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
00160     return ifr.ifr_flags & IFF_UP;
00161 }
00162 
00163 prefix_ void senf::NetdeviceController::up()
00164 {
00165     struct ifreq ifr;
00166     ifrName(ifr);
00167     doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
00168     ifr.ifr_flags |= IFF_UP;
00169     doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
00170 }
00171 
00172 prefix_ void senf::NetdeviceController::down()
00173 {
00174     struct ifreq ifr;
00175     ifrName(ifr);
00176     doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
00177     ifr.ifr_flags &= ~IFF_UP;
00178     doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
00179 }
00180 
00181 prefix_ int senf::NetdeviceController::interfaceIndex()
00182     const
00183 {
00184     return ifindex_;
00185 }
00186 
00187 prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
00188     const
00189 {
00190     ::memset( &ifr, 0, sizeof(ifr));
00191     ifr.ifr_ifindex = ifindex_;
00192     if ( ::ioctl( sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
00193         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
00194         << " could not discover the name of the interface with index " << ifindex_ << ".";
00195 }
00196 
00197 #undef doIoctl
00198 
00199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00200 // senf::NetdeviceController::SockFd
00201 
00202 prefix_ senf::NetdeviceController::SockFd::SockFd()
00203     : fd (::socket(PF_INET, SOCK_DGRAM, 0))
00204 {
00205     if ( fd < 0)
00206         SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
00207 }
00208 
00209 prefix_ senf::NetdeviceController::SockFd::~SockFd()
00210 {
00211     ::close(fd);
00212 }
00213 
00214 prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
00215 {
00216     static boost::weak_ptr<SockFd> sockfd;
00217     SockFd::ptr p (sockfd.lock());
00218     if (!p)
00219          sockfd = p = SockFd::ptr(new SockFd());
00220     return p;
00221 }
00222 
00223 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00224 #undef prefix_
00225 //#include "NetdeviceController.mpp"
00226 
00227 
00228 // Local Variables:
00229 // mode: c++
00230 // fill-column: 100
00231 // c-file-style: "senf"
00232 // indent-tabs-mode: nil
00233 // ispell-local-dictionary: "american"
00234 // compile-command: "scons -u test"
00235 // comment-column: 40
00236 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research