4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief NetdeviceController non-inline non-template implementation
27 #include "NetdeviceController.hh"
28 //#include "NetdeviceController.ih"
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
34 #include <boost/weak_ptr.hpp>
35 #include <senf/Utils/Exception.hh>
36 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 #define doIoctl(ifr, request, errorMsg) \
42 if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 ) \
43 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
46 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
50 ::memset( &ifr, 0, sizeof(ifr));
51 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
52 doIoctl(ifr, SIOCGIFINDEX, "Could not discover the index of interface \"" + interface_name + "\"");
53 ifindex_ = ifr.ifr_ifindex;
56 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
59 ifindex_ = interface_index;
62 prefix_ std::string senf::NetdeviceController::interfaceName()
67 return std::string( ifr.ifr_name);
70 prefix_ void senf::NetdeviceController::interfaceName(std::string const & newname)
72 if (sizeof(newname) <= IFNAMSIZ) {
75 strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
76 doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
81 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
86 doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
87 return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
90 prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & newAddress) {
93 ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
94 std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
95 doIoctl(ifr, SIOCSIFHWADDR, "Could not change the interface MAC address. Is the interface really down?");
98 prefix_ int senf::NetdeviceController::mtu()
103 doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
107 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
111 ifr.ifr_mtu = new_mtu;
112 doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
115 prefix_ int senf::NetdeviceController::txqueuelen()
120 doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
124 prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
128 ifr.ifr_qlen = new_txqueuelen;
129 doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
133 prefix_ bool senf::NetdeviceController::promisc()
138 doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
139 return ifr.ifr_flags & IFF_PROMISC;
142 prefix_ void senf::NetdeviceController::promisc(bool mode)
146 doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
148 ifr.ifr_flags |= IFF_PROMISC;
150 ifr.ifr_flags &= ~IFF_PROMISC;
151 doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
154 prefix_ bool senf::NetdeviceController::isUp()
159 doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
160 return ifr.ifr_flags & IFF_UP;
163 prefix_ void senf::NetdeviceController::up()
167 doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
168 ifr.ifr_flags |= IFF_UP;
169 doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
172 prefix_ void senf::NetdeviceController::down()
176 doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
177 ifr.ifr_flags &= ~IFF_UP;
178 doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
181 prefix_ int senf::NetdeviceController::interfaceIndex()
187 prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
190 ::memset( &ifr, 0, sizeof(ifr));
191 ifr.ifr_ifindex = ifindex_;
192 if ( ::ioctl( sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
193 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
194 << " could not discover the name of the interface with index " << ifindex_ << ".";
199 //-/////////////////////////////////////////////////////////////////////////////////////////////////
200 // senf::NetdeviceController::SockFd
202 prefix_ senf::NetdeviceController::SockFd::SockFd()
203 : fd (::socket(PF_INET, SOCK_DGRAM, 0))
206 SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
209 prefix_ senf::NetdeviceController::SockFd::~SockFd()
214 prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
216 static boost::weak_ptr<SockFd> sockfd;
217 SockFd::ptr p (sockfd.lock());
219 sockfd = p = SockFd::ptr(new SockFd());
223 //-/////////////////////////////////////////////////////////////////////////////////////////////////
225 //#include "NetdeviceController.mpp"
231 // c-file-style: "senf"
232 // indent-tabs-mode: nil
233 // ispell-local-dictionary: "american"
234 // compile-command: "scons -u test"
235 // comment-column: 40