4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Thorsten Horstmann <tho@berlios.de>
29 \brief NetdeviceController non-inline non-template implementation
32 #include "NetdeviceController.hh"
33 //#include "NetdeviceController.ih"
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
39 #include <boost/weak_ptr.hpp>
40 #include <senf/Utils/Exception.hh>
41 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 #define doIoctl(ifr, request, errorMsg) \
47 if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 ) \
48 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
51 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
55 ::memset( &ifr, 0, sizeof(ifr));
56 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
57 doIoctl(ifr, SIOCGIFINDEX, "Could not discover the index of interface \"" + interface_name + "\"");
58 ifindex_ = ifr.ifr_ifindex;
61 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
64 ifindex_ = interface_index;
67 prefix_ std::string senf::NetdeviceController::interfaceName()
72 return std::string( ifr.ifr_name);
75 prefix_ void senf::NetdeviceController::interfaceName(std::string const & newname)
77 if (sizeof(newname) <= IFNAMSIZ) {
80 strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
81 doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
86 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
91 doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
92 return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
95 prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & newAddress) {
98 ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
99 std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
100 doIoctl(ifr, SIOCSIFHWADDR, "Could not change the interface MAC address. Is the interface really down?");
103 prefix_ int senf::NetdeviceController::mtu()
108 doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
112 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
116 ifr.ifr_mtu = new_mtu;
117 doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
120 prefix_ int senf::NetdeviceController::txqueuelen()
125 doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
129 prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
133 ifr.ifr_qlen = new_txqueuelen;
134 doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
138 prefix_ bool senf::NetdeviceController::promisc()
143 doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
144 return ifr.ifr_flags & IFF_PROMISC;
147 prefix_ void senf::NetdeviceController::promisc(bool mode)
151 doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
153 ifr.ifr_flags |= IFF_PROMISC;
155 ifr.ifr_flags &= ~IFF_PROMISC;
156 doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
159 prefix_ bool senf::NetdeviceController::isUp()
164 doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
165 return ifr.ifr_flags & IFF_UP;
168 prefix_ void senf::NetdeviceController::up()
172 doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
173 ifr.ifr_flags |= IFF_UP;
174 doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
177 prefix_ void senf::NetdeviceController::down()
181 doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
182 ifr.ifr_flags &= ~IFF_UP;
183 doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
186 prefix_ int senf::NetdeviceController::interfaceIndex()
192 prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
195 ::memset( &ifr, 0, sizeof(ifr));
196 ifr.ifr_ifindex = ifindex_;
197 if ( ::ioctl( sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
198 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
199 << " could not discover the name of the interface with index " << ifindex_ << ".";
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 // senf::NetdeviceController::SockFd
207 prefix_ senf::NetdeviceController::SockFd::SockFd()
208 : fd (::socket(PF_INET, SOCK_DGRAM, 0))
211 SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
214 prefix_ senf::NetdeviceController::SockFd::~SockFd()
219 prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
221 static boost::weak_ptr<SockFd> sockfd;
222 SockFd::ptr p (sockfd.lock());
224 sockfd = p = SockFd::ptr(new SockFd());
228 //-/////////////////////////////////////////////////////////////////////////////////////////////////
230 //#include "NetdeviceController.mpp"
236 // c-file-style: "senf"
237 // indent-tabs-mode: nil
238 // ispell-local-dictionary: "american"
239 // compile-command: "scons -u test"
240 // comment-column: 40