-/-
[senf.git] / Socket / NetdeviceController.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.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 "../Utils/Exception.hh"
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ senf::NetdeviceController::NetdeviceController(std::string const & interface_name)
40 {
41     openSocket();
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 {
51     openSocket();
52     ifindex_ = interface_index;
53 }
54
55 prefix_ std::string senf::NetdeviceController::interfaceName()
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 {
64     struct ifreq ifr;
65     ifrName( ifr);
66     doIoctl( ifr, SIOCGIFHWADDR);
67     return senf::MACAddress::from_data( ifr.ifr_hwaddr.sa_data);
68 }
69
70 prefix_ int senf::NetdeviceController::mtu()
71 {
72     struct ifreq ifr;
73     ifrName( ifr);
74     doIoctl( ifr, SIOCGIFMTU);
75     return ifr.ifr_mtu;
76 }
77
78 prefix_ void senf::NetdeviceController::mtu(int new_mtu)
79 {
80     struct ifreq ifr;
81     ifrName( ifr);
82     ifr.ifr_mtu = new_mtu;
83     doIoctl( ifr, SIOCSIFMTU);
84 }
85
86 prefix_ int senf::NetdeviceController::interfaceIndex()
87 {
88     return ifindex_;
89 }
90
91 prefix_ senf::NetdeviceController::~NetdeviceController()
92 {
93     close( sockfd_);
94 }
95
96 prefix_ void senf::NetdeviceController::openSocket()
97 {
98     sockfd_ = ::socket( PF_INET, SOCK_DGRAM, 0);
99     if ( sockfd_ < 0)
100         throwErrno();
101 }
102
103 prefix_ void senf::NetdeviceController::ifrName(ifreq& ifr)
104 {
105     ::memset( &ifr, 0, sizeof(ifr));
106     ifr.ifr_ifindex = ifindex_;
107     if ( ::ioctl( sockfd_, SIOCGIFNAME, &ifr ) < 0 )
108         throwErrno();
109 }
110
111
112 prefix_ void senf::NetdeviceController::doIoctl(ifreq& ifr, int request)
113 {
114     if ( ::ioctl( sockfd_, request, &ifr ) < 0 )
115         throwErrno();
116 }
117
118 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120 //#include "NetdeviceController.mpp"
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // comment-column: 40
131 // End: