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 "../Utils/Exception.hh"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
43 ::memset( &ifr, 0, sizeof(ifr));
44 interface_name.copy( ifr.ifr_name, IFNAMSIZ);
45 doIoctl( ifr, SIOCGIFINDEX);
46 ifindex_ = ifr.ifr_ifindex;
49 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
52 ifindex_ = interface_index;
55 prefix_ std::string senf::NetdeviceController::interfaceName()
60 return std::string( ifr.ifr_name);
63 prefix_ void senf::NetdeviceController::interfaceName(const std::string & newname)
65 if (sizeof(newname) <= IFNAMSIZ) {
68 strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
70 doIoctl(ifr, SIOCSIFNAME);
71 } catch (senf::SystemException e) {
72 e << "Could not change the interface name. Is the interface really down?";
79 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
84 doIoctl( ifr, SIOCGIFHWADDR);
85 return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
88 prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) {
91 ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
92 std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
94 doIoctl(ifr, SIOCSIFHWADDR);
95 } catch (senf::SystemException e) {
96 e << "Could not change the interface MAC address. Is the interface really down?";
101 prefix_ int senf::NetdeviceController::mtu()
106 doIoctl( ifr, SIOCGIFMTU);
110 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
114 ifr.ifr_mtu = new_mtu;
115 doIoctl( ifr, SIOCSIFMTU);
118 prefix_ int senf::NetdeviceController::interfaceIndex()
124 prefix_ senf::NetdeviceController::~NetdeviceController()
129 prefix_ void senf::NetdeviceController::openSocket()
131 sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
133 SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
136 prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
139 ::memset( &ifr, 0, sizeof(ifr));
140 ifr.ifr_ifindex = ifindex_;
141 if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
142 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
143 << " could not discover the name of the interface with index " << ifindex_ << ".";
146 prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
149 if ( ::ioctl( sockfd_, request, &ifr ) < 0 )
150 SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
153 ///////////////////////////////cc.e////////////////////////////////////////
155 //#include "NetdeviceController.mpp"
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // comment-column: 40