switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / NetdeviceController.cc
index ceb132f..28422c7 100644 (file)
@@ -2,23 +2,28 @@
 //
 // Copyright (C) 2007
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
-//     Thorsten Horstmann <tho@berlios.de>
 //
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the
-// Free Software Foundation, Inc.,
-// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Thorsten Horstmann <tho@berlios.de>
 
 /** \file
     \brief NetdeviceController non-inline non-template implementation
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <net/if.h>
+#include <boost/weak_ptr.hpp>
 #include <senf/Utils/Exception.hh>
+#include <senf/Socket/Protocols/Raw/MACAddress.hh>
 
 #define prefix_
-///////////////////////////////cc.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
+
+#define doIoctl(ifr, request, errorMsg)                                                         \
+    if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )                                            \
+        SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
+
 
 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
     : sockfd_ (sockfd())
@@ -42,7 +54,7 @@ prefix_ senf::NetdeviceController::NetdeviceController(std::string const & inter
     struct ifreq ifr;
     ::memset( &ifr, 0, sizeof(ifr));
     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
-    doIoctl( ifr, SIOCGIFINDEX);
+    doIoctl(ifr, SIOCGIFINDEX, "Could not discover the index of interface \"" + interface_name + "\"");
     ifindex_ = ifr.ifr_ifindex;
 }
 
@@ -60,18 +72,13 @@ prefix_ std::string senf::NetdeviceController::interfaceName()
     return std::string( ifr.ifr_name);
 }
 
-prefix_ void senf::NetdeviceController::interfaceName(const std::string & newname)
+prefix_ void senf::NetdeviceController::interfaceName(std::string const & newname)
 {
     if (sizeof(newname) <= IFNAMSIZ) {
         struct ifreq ifr;
         ifrName(ifr);
         strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
-        try {
-            doIoctl(ifr, SIOCSIFNAME);
-        } catch (senf::SystemException e) {
-            e << "Could not change the interface name. Is the interface really down?";
-            throw ;
-        }
+        doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
     }
     return;
 }
@@ -81,21 +88,16 @@ prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
 {
     struct ifreq ifr;
     ifrName( ifr);
-    doIoctl( ifr, SIOCGIFHWADDR);
-    return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
+    doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
+    return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
 }
 
-prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) {
+prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & newAddress) {
     struct ifreq ifr;
     ifrName( ifr);
     ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
     std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
-    try {
-        doIoctl(ifr, SIOCSIFHWADDR);
-    } catch (senf::SystemException e) {
-        e << "Could not change the interface MAC address. Is the interface really down?";
-        throw ;
-    }
+    doIoctl(ifr, SIOCSIFHWADDR, "Could not change the interface MAC address. Is the interface really down?");
 }
 
 prefix_ int senf::NetdeviceController::mtu()
@@ -103,7 +105,7 @@ prefix_ int senf::NetdeviceController::mtu()
 {
     struct ifreq ifr;
     ifrName( ifr);
-    doIoctl( ifr, SIOCGIFMTU);
+    doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
     return ifr.ifr_mtu;
 }
 
@@ -112,15 +114,33 @@ prefix_ void senf::NetdeviceController::mtu(int new_mtu)
     struct ifreq ifr;
     ifrName( ifr);
     ifr.ifr_mtu = new_mtu;
-    doIoctl( ifr, SIOCSIFMTU);
+    doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
+}
+
+prefix_ int senf::NetdeviceController::txqueuelen()
+    const
+{
+    struct ifreq ifr;
+    ifrName( ifr);
+    doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
+    return ifr.ifr_qlen;
+}
+
+prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
+{
+    struct ifreq ifr;
+    ifrName( ifr);
+    ifr.ifr_qlen = new_txqueuelen;
+    doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
 }
 
+
 prefix_ bool senf::NetdeviceController::promisc()
     const
 {
     struct ifreq ifr;
     ifrName( ifr);
-    doIoctl( ifr, SIOCGIFFLAGS);
+    doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
     return ifr.ifr_flags & IFF_PROMISC;
 }
 
@@ -128,12 +148,12 @@ prefix_ void senf::NetdeviceController::promisc(bool mode)
 {
     struct ifreq ifr;
     ifrName( ifr);
-    doIoctl( ifr, SIOCGIFFLAGS);
+    doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
     if (mode)
         ifr.ifr_flags |= IFF_PROMISC;
     else
         ifr.ifr_flags &= ~IFF_PROMISC;
-    doIoctl( ifr, SIOCSIFFLAGS);
+    doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
 }
 
 prefix_ bool senf::NetdeviceController::isUp()
@@ -141,7 +161,7 @@ prefix_ bool senf::NetdeviceController::isUp()
 {
     struct ifreq ifr;
     ifrName(ifr);
-    doIoctl(ifr, SIOCGIFFLAGS);
+    doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
     return ifr.ifr_flags & IFF_UP;
 }
 
@@ -149,18 +169,18 @@ prefix_ void senf::NetdeviceController::up()
 {
     struct ifreq ifr;
     ifrName(ifr);
-    doIoctl(ifr, SIOCGIFFLAGS);
+    doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
     ifr.ifr_flags |= IFF_UP;
-    doIoctl(ifr, SIOCSIFFLAGS);
+    doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
 }
 
 prefix_ void senf::NetdeviceController::down()
 {
     struct ifreq ifr;
     ifrName(ifr);
-    doIoctl(ifr, SIOCGIFFLAGS);
+    doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
     ifr.ifr_flags &= ~IFF_UP;
-    doIoctl(ifr, SIOCSIFFLAGS);
+    doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
 }
 
 prefix_ int senf::NetdeviceController::interfaceIndex()
@@ -169,7 +189,7 @@ prefix_ int senf::NetdeviceController::interfaceIndex()
     return ifindex_;
 }
 
-prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
+prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
     const
 {
     ::memset( &ifr, 0, sizeof(ifr));
@@ -179,14 +199,9 @@ prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
         << " could not discover the name of the interface with index " << ifindex_ << ".";
 }
 
-prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
-    const
-{
-    if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )
-        SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
-}
+#undef doIoctl
 
-///////////////////////////////////////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::NetdeviceController::SockFd
 
 prefix_ senf::NetdeviceController::SockFd::SockFd()
@@ -210,11 +225,11 @@ prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd
     return p;
 }
 
-///////////////////////////////cc.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 //#include "NetdeviceController.mpp"
 
-
+\f
 // Local Variables:
 // mode: c++
 // fill-column: 100