435178e6a4fc361dc695241c386f7024a539b849
[senf.git] / senf / Socket / NetdeviceController.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief NetdeviceController non-inline non-template implementation
25  */
26
27 #include "NetdeviceController.hh"
28 //#include "NetdeviceController.ih"
29
30 // Custom includes
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <net/if.h>
34 #include <boost/weak_ptr.hpp>
35 #include <senf/Utils/Exception.hh>
36 #include <senf/Socket/Protocols/Raw/MACAddress.hh>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 #define doIoctl(ifr, request, errorMsg)                                                         \
42     if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )                                            \
43         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController: " errorMsg)
44
45
46 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
47     : sockfd_ (sockfd())
48 {
49     struct ifreq ifr;
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;
54 }
55
56 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
57     : sockfd_ (sockfd())
58 {
59     ifindex_ = interface_index;
60 }
61
62 prefix_ std::string senf::NetdeviceController::interfaceName()
63     const
64 {
65     struct ifreq ifr;
66     ifrName( ifr);
67     return std::string( ifr.ifr_name);
68 }
69
70 prefix_ void senf::NetdeviceController::interfaceName(std::string const & newname)
71 {
72     if (sizeof(newname) <= IFNAMSIZ) {
73         struct ifreq ifr;
74         ifrName(ifr);
75         strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
76         doIoctl(ifr, SIOCSIFNAME, "Could not change the interface name. Is the interface really down?");
77     }
78     return;
79 }
80
81 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
82     const
83 {
84     struct ifreq ifr;
85     ifrName( ifr);
86     doIoctl( ifr, SIOCGIFHWADDR, "Could not discover hardwareAddress");
87     return MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
88 }
89
90 prefix_ void senf::NetdeviceController::hardwareAddress(MACAddress const & newAddress) {
91     struct ifreq ifr;
92     ifrName( ifr);
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?");
96 }
97
98 prefix_ int senf::NetdeviceController::mtu()
99     const
100 {
101     struct ifreq ifr;
102     ifrName( ifr);
103     doIoctl( ifr, SIOCGIFMTU, "Could not discover mtu");
104     return ifr.ifr_mtu;
105 }
106
107 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
108 {
109     struct ifreq ifr;
110     ifrName( ifr);
111     ifr.ifr_mtu = new_mtu;
112     doIoctl( ifr, SIOCSIFMTU, "Could not set mtu");
113 }
114
115 prefix_ int senf::NetdeviceController::txqueuelen()
116     const
117 {
118     struct ifreq ifr;
119     ifrName( ifr);
120     doIoctl( ifr, SIOCGIFTXQLEN, "Could not discover txqueuelen");
121     return ifr.ifr_qlen;
122 }
123
124 prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
125 {
126     struct ifreq ifr;
127     ifrName( ifr);
128     ifr.ifr_qlen = new_txqueuelen;
129     doIoctl( ifr, SIOCSIFTXQLEN, "Could not set txqueuelen");
130 }
131
132
133 prefix_ bool senf::NetdeviceController::promisc()
134     const
135 {
136     struct ifreq ifr;
137     ifrName( ifr);
138     doIoctl( ifr, SIOCGIFFLAGS, "Could not discover promisc mode");
139     return ifr.ifr_flags & IFF_PROMISC;
140 }
141
142 prefix_ void senf::NetdeviceController::promisc(bool mode)
143 {
144     struct ifreq ifr;
145     ifrName( ifr);
146     doIoctl( ifr, SIOCGIFFLAGS, "Could not set promisc mode");
147     if (mode)
148         ifr.ifr_flags |= IFF_PROMISC;
149     else
150         ifr.ifr_flags &= ~IFF_PROMISC;
151     doIoctl( ifr, SIOCSIFFLAGS, "Could not set promisc mode");
152 }
153
154 prefix_ bool senf::NetdeviceController::isUp()
155     const
156 {
157     struct ifreq ifr;
158     ifrName(ifr);
159     doIoctl(ifr, SIOCGIFFLAGS, "Could not discover interface status");
160     return ifr.ifr_flags & IFF_UP;
161 }
162
163 prefix_ void senf::NetdeviceController::up()
164 {
165     struct ifreq ifr;
166     ifrName(ifr);
167     doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
168     ifr.ifr_flags |= IFF_UP;
169     doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
170 }
171
172 prefix_ void senf::NetdeviceController::down()
173 {
174     struct ifreq ifr;
175     ifrName(ifr);
176     doIoctl(ifr, SIOCGIFFLAGS, "Could not set interface status");
177     ifr.ifr_flags &= ~IFF_UP;
178     doIoctl(ifr, SIOCSIFFLAGS, "Could not set interface status");
179 }
180
181 prefix_ int senf::NetdeviceController::interfaceIndex()
182     const
183 {
184     return ifindex_;
185 }
186
187 prefix_ void senf::NetdeviceController::ifrName(ifreq & ifr)
188     const
189 {
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_ << ".";
195 }
196
197 #undef doIoctl
198
199 ///////////////////////////////////////////////////////////////////////////
200 // senf::NetdeviceController::SockFd
201
202 prefix_ senf::NetdeviceController::SockFd::SockFd()
203     : fd (::socket(PF_INET, SOCK_DGRAM, 0))
204 {
205     if ( fd < 0)
206         SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
207 }
208
209 prefix_ senf::NetdeviceController::SockFd::~SockFd()
210 {
211     ::close(fd);
212 }
213
214 prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
215 {
216     static boost::weak_ptr<SockFd> sockfd;
217     SockFd::ptr p (sockfd.lock());
218     if (!p)
219          sockfd = p = SockFd::ptr(new SockFd());
220     return p;
221 }
222
223 ///////////////////////////////cc.e////////////////////////////////////////
224 #undef prefix_
225 //#include "NetdeviceController.mpp"
226
227 \f
228 // Local Variables:
229 // mode: c++
230 // fill-column: 100
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
236 // End: