d91d14170bffbb156892a98245f6bc78a5dbe9b5
[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 <senf/Utils/Exception.hh>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
40     : sockfd_ (sockfd())
41 {
42     struct ifreq ifr;
43     ::memset( &ifr, 0, sizeof(ifr));
44     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
45     doIoctl( ifr, SIOCGIFINDEX);
46     ifindex_ = ifr.ifr_ifindex;
47 }
48
49 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
50     : sockfd_ (sockfd())
51 {
52     ifindex_ = interface_index;
53 }
54
55 prefix_ std::string senf::NetdeviceController::interfaceName()
56     const
57 {
58     struct ifreq ifr;
59     ifrName( ifr);
60     return std::string( ifr.ifr_name);
61 }
62
63 prefix_ void senf::NetdeviceController::interfaceName(const std::string & newname)
64 {
65     if (sizeof(newname) <= IFNAMSIZ) {
66         struct ifreq ifr;
67         ifrName(ifr);
68         strncpy(ifr. ifr_newname, newname.c_str(), IFNAMSIZ);
69         try {
70             doIoctl(ifr, SIOCSIFNAME);
71         } catch (senf::SystemException e) {
72             e << "Could not change the interface name. Is the interface really down?";
73             throw ;
74         }
75     }
76     return;
77 }
78
79 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
80     const
81 {
82     struct ifreq ifr;
83     ifrName( ifr);
84     doIoctl( ifr, SIOCGIFHWADDR);
85     return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
86 }
87
88 prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) {
89     struct ifreq ifr;
90     ifrName( ifr);
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);
93     try {
94         doIoctl(ifr, SIOCSIFHWADDR);
95     } catch (senf::SystemException e) {
96         e << "Could not change the interface MAC address. Is the interface really down?";
97         throw ;
98     }
99 }
100
101 prefix_ int senf::NetdeviceController::mtu()
102     const
103 {
104     struct ifreq ifr;
105     ifrName( ifr);
106     doIoctl( ifr, SIOCGIFMTU);
107     return ifr.ifr_mtu;
108 }
109
110 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
111 {
112     struct ifreq ifr;
113     ifrName( ifr);
114     ifr.ifr_mtu = new_mtu;
115     doIoctl( ifr, SIOCSIFMTU);
116 }
117
118 prefix_ int senf::NetdeviceController::txqueuelen()
119     const
120 {
121     struct ifreq ifr;
122     ifrName( ifr);
123     doIoctl( ifr, SIOCGIFTXQLEN);
124     return ifr.ifr_qlen;
125 }
126
127 prefix_ void senf::NetdeviceController::txqueuelen(int new_txqueuelen)
128 {
129     struct ifreq ifr;
130     ifrName( ifr);
131     ifr.ifr_qlen = new_txqueuelen;
132     doIoctl( ifr, SIOCSIFTXQLEN);
133 }
134
135
136 prefix_ bool senf::NetdeviceController::promisc()
137     const
138 {
139     struct ifreq ifr;
140     ifrName( ifr);
141     doIoctl( ifr, SIOCGIFFLAGS);
142     return ifr.ifr_flags & IFF_PROMISC;
143 }
144
145 prefix_ void senf::NetdeviceController::promisc(bool mode)
146 {
147     struct ifreq ifr;
148     ifrName( ifr);
149     doIoctl( ifr, SIOCGIFFLAGS);
150     if (mode)
151         ifr.ifr_flags |= IFF_PROMISC;
152     else
153         ifr.ifr_flags &= ~IFF_PROMISC;
154     doIoctl( ifr, SIOCSIFFLAGS);
155 }
156
157 prefix_ bool senf::NetdeviceController::isUp()
158     const
159 {
160     struct ifreq ifr;
161     ifrName(ifr);
162     doIoctl(ifr, SIOCGIFFLAGS);
163     return ifr.ifr_flags & IFF_UP;
164 }
165
166 prefix_ void senf::NetdeviceController::up()
167 {
168     struct ifreq ifr;
169     ifrName(ifr);
170     doIoctl(ifr, SIOCGIFFLAGS);
171     ifr.ifr_flags |= IFF_UP;
172     doIoctl(ifr, SIOCSIFFLAGS);
173 }
174
175 prefix_ void senf::NetdeviceController::down()
176 {
177     struct ifreq ifr;
178     ifrName(ifr);
179     doIoctl(ifr, SIOCGIFFLAGS);
180     ifr.ifr_flags &= ~IFF_UP;
181     doIoctl(ifr, SIOCSIFFLAGS);
182 }
183
184 prefix_ int senf::NetdeviceController::interfaceIndex()
185     const
186 {
187     return ifindex_;
188 }
189
190 prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
191     const
192 {
193     ::memset( &ifr, 0, sizeof(ifr));
194     ifr.ifr_ifindex = ifindex_;
195     if ( ::ioctl( sockfd_->fd, SIOCGIFNAME, &ifr ) < 0 )
196         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
197         << " could not discover the name of the interface with index " << ifindex_ << ".";
198 }
199
200 prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
201     const
202 {
203     if ( ::ioctl( sockfd_->fd, request, &ifr ) < 0 )
204         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
205 }
206
207 ///////////////////////////////////////////////////////////////////////////
208 // senf::NetdeviceController::SockFd
209
210 prefix_ senf::NetdeviceController::SockFd::SockFd()
211     : fd (::socket(PF_INET, SOCK_DGRAM, 0))
212 {
213     if ( fd < 0)
214         SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
215 }
216
217 prefix_ senf::NetdeviceController::SockFd::~SockFd()
218 {
219     ::close(fd);
220 }
221
222 prefix_ senf::NetdeviceController::SockFd::ptr senf::NetdeviceController::sockfd()
223 {
224     static boost::weak_ptr<SockFd> sockfd;
225     SockFd::ptr p (sockfd.lock());
226     if (!p)
227          sockfd = p = SockFd::ptr(new SockFd());
228     return p;
229 }
230
231 ///////////////////////////////cc.e////////////////////////////////////////
232 #undef prefix_
233 //#include "NetdeviceController.mpp"
234
235 \f
236 // Local Variables:
237 // mode: c++
238 // fill-column: 100
239 // c-file-style: "senf"
240 // indent-tabs-mode: nil
241 // ispell-local-dictionary: "american"
242 // compile-command: "scons -u test"
243 // comment-column: 40
244 // End: