Move include files in debian packge into 'senf' subdirectory
[senf.git] / Socket / Protocols / INet / UDPProtocol.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@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 TCPProtocol non-inline non-template implementation
25  */
26
27 #include "UDPProtocol.hh"
28 //#include "UDPProtocol.ih"
29
30 // Custom includes
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <sys/ioctl.h>
35 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
36 #include <net/if.h> // for if_nametoindex
37 #include "../../../Socket/SocketHandle.hh"
38
39 //#include "UDPProtocol.mpp"
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 prefix_ unsigned senf::UDPProtocol::available()
44     const
45 {
46     int n;
47     if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
48         throw senf::SystemException(errno);
49     return n;
50 }
51
52 prefix_ bool senf::UDPProtocol::eof()
53     const
54 {
55     return false;
56 }
57
58 prefix_ bool senf::UDPProtocol::mcLoop()
59     const
60 {
61     int value;
62     socklen_t len (sizeof(value));
63     if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
64         throw SystemException(errno);
65     return value;
66 }
67
68 prefix_ void senf::UDPProtocol::mcLoop(bool value)
69     const
70 {
71     int ivalue (value);
72     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
73         throw SystemException(errno);
74 }
75
76 prefix_ void senf::UDPProtocol::mcAddMembership(INet4SocketAddress const & mcAddr)
77     const
78 {
79     struct ip_mreqn mreqn;
80     mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
81     mreqn.imr_address.s_addr = htons(INADDR_ANY);
82     mreqn.imr_ifindex = 0;
83     if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
84         throw SystemException(errno);
85 }
86
87 prefix_ void senf::UDPProtocol::mcAddMembership(INet4SocketAddress const & mcAddr,
88                                                         INet4SocketAddress const & localAddr)
89     const
90 {
91     struct ip_mreqn mreqn;
92     mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
93     mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
94     mreqn.imr_ifindex = 0;
95     if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
96         throw SystemException(errno);
97 }
98
99 prefix_ void senf::UDPProtocol::mcDropMembership(INet4SocketAddress const & mcAddr)
100     const
101 {
102     struct ip_mreqn mreqn;
103     mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
104     mreqn.imr_address.s_addr = htons(INADDR_ANY);
105     mreqn.imr_ifindex = 0;
106     if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
107         throw SystemException(errno);
108 }
109
110 prefix_ void senf::UDPProtocol::mcDropMembership(INet4SocketAddress const & mcAddr,
111                                                          INet4SocketAddress const & localAddr)
112     const
113 {
114     struct ip_mreqn mreqn;
115     mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
116     mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
117     mreqn.imr_ifindex = 0;
118     if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
119         throw SystemException(errno);
120 }
121
122 prefix_ void senf::UDPProtocol::mcIface(std::string const & iface)
123     const
124 {
125     struct ip_mreqn mreqn;
126     ::memset(&mreqn,sizeof(mreqn),0);
127     if (!iface.empty()) {
128         mreqn.imr_ifindex = if_nametoindex(iface.c_str());
129         if (mreqn.imr_ifindex == 0)
130             throw SystemException(EINVAL);
131     }
132     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
133         throw SystemException(errno);
134 }
135
136 prefix_ unsigned senf::UDPProtocol::mcTTL()
137     const
138 {
139     int value;
140     socklen_t len (sizeof(value));
141     if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
142         throw SystemException(errno);
143     return value;
144 }
145
146 prefix_ void senf::UDPProtocol::mcTTL(unsigned value)
147     const
148 {
149     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
150         throw SystemException(errno);
151 }
152
153
154 ///////////////////////////////cc.e////////////////////////////////////////
155 #undef prefix_
156 //#include "UDPProtocol.mpp"
157
158 \f
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // c-file-style: "senf"
163 // indent-tabs-mode: nil
164 // ispell-local-dictionary: "american"
165 // compile-command: "scons -u test"
166 // comment-column: 40
167 // End: