Utils: Implement helper macros to add file/line information to SystemException's
[senf.git] / 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 "../Utils/Exception.hh"
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
39 {
40     openSocket();
41     struct ifreq ifr;
42     ::memset( &ifr, 0, sizeof(ifr));
43     interface_name.copy( ifr.ifr_name, IFNAMSIZ);
44     doIoctl( ifr, SIOCGIFINDEX);
45     ifindex_ = ifr.ifr_ifindex;
46 }
47
48 prefix_ senf::NetdeviceController::NetdeviceController(int interface_index)
49 {
50     openSocket();
51     ifindex_ = interface_index;
52 }
53
54 prefix_ std::string senf::NetdeviceController::interfaceName()
55     const
56 {
57     struct ifreq ifr;
58     ifrName( ifr);
59     return std::string( ifr.ifr_name);
60 }
61
62 prefix_ senf::MACAddress senf::NetdeviceController::hardwareAddress()
63     const
64 {
65     struct ifreq ifr;
66     ifrName( ifr);
67     doIoctl( ifr, SIOCGIFHWADDR);
68     return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
69 }
70
71 prefix_ void senf::NetdeviceController::hardwareAddress(const MACAddress &newAddress) {
72     struct ifreq ifr;
73     ifrName( ifr);
74     ifr.ifr_hwaddr.sa_family = 1; // TODO: lookup named constant; PF_LOCAL ???
75     std::copy(newAddress.begin(), newAddress.end(), ifr.ifr_hwaddr.sa_data);
76     doIoctl( ifr, SIOCSIFHWADDR);
77 }
78
79 prefix_ int senf::NetdeviceController::mtu()
80     const
81 {
82     struct ifreq ifr;
83     ifrName( ifr);
84     doIoctl( ifr, SIOCGIFMTU);
85     return ifr.ifr_mtu;
86 }
87
88 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
89     const
90 {
91     struct ifreq ifr;
92     ifrName( ifr);
93     ifr.ifr_mtu = new_mtu;
94     doIoctl( ifr, SIOCSIFMTU);
95 }
96
97 prefix_ int senf::NetdeviceController::interfaceIndex()
98     const
99 {
100     return ifindex_;
101 }
102
103 prefix_ senf::NetdeviceController::~NetdeviceController()
104 {
105     close( sockfd_);
106 }
107
108 prefix_ void senf::NetdeviceController::openSocket()
109 {
110     sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
111     if ( sockfd_ < 0)
112         SENF_THROW_SYSTEM_EXCEPTION("Could not open socket for NetdeviceController.");
113 }
114
115 prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
116     const
117 {
118     ::memset( &ifr, 0, sizeof(ifr));
119     ifr.ifr_ifindex = ifindex_;
120     if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
121         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController")
122         << " could not discover the name of the interface with index " << ifindex_ << ".";
123 }
124
125 prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
126     const
127 {
128     if ( ::ioctl( sockfd_, request, &ifr ) < 0 )
129         SENF_THROW_SYSTEM_EXCEPTION("NetdeviceController::doIoctl failed.");
130 }
131
132 ///////////////////////////////cc.e////////////////////////////////////////
133 #undef prefix_
134 //#include "NetdeviceController.mpp"
135
136 \f
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // c-file-style: "senf"
141 // indent-tabs-mode: nil
142 // ispell-local-dictionary: "american"
143 // compile-command: "scons -u test"
144 // comment-column: 40
145 // End: