4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief MulticastSocketProtocol non-inline non-template implementation */
26 #include "MulticastSocketProtocol.hh"
27 //#include "MulticastSocketProtocol.ih"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <net/if.h> // for if_nametoindex
34 //#include "MulticastSocketProtocol.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::MulticastSocketProtocol
41 prefix_ void senf::MulticastSocketProtocol::broadcastEnabled(bool v)
44 if (::setsockopt(fd(), SOL_SOCKET, SO_BROADCAST, &ivalue, sizeof(ivalue)) < 0)
45 throw SystemException("::setsockopt(SO_BROADCAST)");
48 prefix_ bool senf::MulticastSocketProtocol::broadcastEnabled()
51 ::socklen_t len (sizeof(value));
52 if (::getsockopt(fd(), SOL_SOCKET, SO_BROADCAST, &value, &len) < 0)
53 throw SystemException("::getsockopt(SO_BROADCAST)");
57 prefix_ bool senf::MulticastSocketProtocol::mcLoop()
61 socklen_t len (sizeof(value));
62 if (::getsockopt(fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
63 throw SystemException();
67 prefix_ void senf::MulticastSocketProtocol::mcLoop(bool value)
71 if (::setsockopt(fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
72 throw SystemException();
75 prefix_ void senf::MulticastSocketProtocol::mcIface(std::string const & iface)
78 struct ip_mreqn mreqn;
79 ::memset(&mreqn,sizeof(mreqn),0);
81 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
82 if (mreqn.imr_ifindex == 0)
83 throw SystemException(EINVAL);
85 if (::setsockopt(fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
86 throw SystemException();
89 prefix_ unsigned senf::MulticastSocketProtocol::mcTTL()
93 socklen_t len (sizeof(value));
94 if (::getsockopt(fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
95 throw SystemException();
99 prefix_ void senf::MulticastSocketProtocol::mcTTL(unsigned value)
102 if (::setsockopt(fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
103 throw SystemException();
106 ///////////////////////////////////////////////////////////////////////////
107 // senf::INet4MulticastSocketProtocol
109 prefix_ void senf::INet4MulticastSocketProtocol::mcAddMembership(INet4Address const & mcAddr)
112 struct ip_mreqn mreqn;
113 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
114 mreqn.imr_address.s_addr = htons(INADDR_ANY);
115 mreqn.imr_ifindex = 0;
116 if (::setsockopt(fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
117 throw SystemException("::setsockopt(IP_ADD_MEMBERSHIP");
120 prefix_ void senf::INet4MulticastSocketProtocol::mcAddMembership(INet4Address const & mcAddr,
121 INet4Address const & localAddr)
124 struct ip_mreqn mreqn;
125 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
126 mreqn.imr_address.s_addr = localAddr.inaddr();
127 mreqn.imr_ifindex = 0;
128 if (::setsockopt(fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
129 throw SystemException("::setsockopt(IP_ADD_MEMBERSHIP");
132 prefix_ void senf::INet4MulticastSocketProtocol::mcAddMembership(INet4Address const & mcAddr,
133 std::string const & iface)
136 struct ip_mreqn mreqn;
137 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
138 mreqn.imr_address.s_addr = htons(INADDR_ANY);
139 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
140 if (mreqn.imr_ifindex == 0)
141 throw SystemException("::if_nametoindex()",ENOENT);
142 if (::setsockopt(fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
143 throw SystemException("::setsockopt(IP_ADD_MEMBERSHIP");
146 prefix_ void senf::INet4MulticastSocketProtocol::mcDropMembership(INet4Address const & mcAddr)
149 struct ip_mreqn mreqn;
150 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
151 mreqn.imr_address.s_addr = htons(INADDR_ANY);
152 mreqn.imr_ifindex = 0;
153 if (::setsockopt(fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
154 throw SystemException();
157 prefix_ void senf::INet4MulticastSocketProtocol::mcDropMembership(INet4Address const & mcAddr,
158 INet4Address const & localAddr)
161 struct ip_mreqn mreqn;
162 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
163 mreqn.imr_address.s_addr = localAddr.inaddr();
164 mreqn.imr_ifindex = 0;
165 if (::setsockopt(fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
166 throw SystemException();
169 prefix_ void senf::INet4MulticastSocketProtocol::mcDropMembership(INet4Address const & mcAddr,
170 std::string const & iface)
173 struct ip_mreqn mreqn;
174 mreqn.imr_multiaddr.s_addr = mcAddr.inaddr();
175 mreqn.imr_address.s_addr = htons(INADDR_ANY);
176 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
177 if (mreqn.imr_ifindex == 0)
178 throw SystemException("::if_nametoindex()",ENOENT);
179 if (::setsockopt(fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
180 throw SystemException();
183 ///////////////////////////////////////////////////////////////////////////
184 // senf::INet6MulticastSocketProtocol
186 prefix_ void senf::INet6MulticastSocketProtocol::mcAddMembership(INet6Address const & mcAddr)
189 struct ipv6_mreq mreqn;
190 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
191 mreqn.ipv6mr_interface = 0;
192 if (::setsockopt(fd(),SOL_IP,IPV6_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
193 throw SystemException("::setsockopt(IPV6_ADD_MEMBERSHIP");
196 prefix_ void senf::INet6MulticastSocketProtocol::mcAddMembership(INet6Address const & mcAddr,
197 std::string const & iface)
199 struct ipv6_mreq mreqn;
200 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
201 mreqn.ipv6mr_interface = if_nametoindex(iface.c_str());
202 if (mreqn.ipv6mr_interface == 0)
203 throw SystemException("::if_nametoindex()",ENOENT);
204 if (::setsockopt(fd(),SOL_IP,IPV6_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
205 throw SystemException("::setsockopt(IPV6_ADD_MEMBERSHIP");
208 prefix_ void senf::INet6MulticastSocketProtocol::mcDropMembership(INet6Address const & mcAddr)
211 struct ipv6_mreq mreqn;
212 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
213 mreqn.ipv6mr_interface = 0;
214 if (::setsockopt(fd(),SOL_IP,IPV6_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
215 throw SystemException();
219 senf::INet6MulticastSocketProtocol::mcDropMembership(INet6Address const & mcAddr,
220 std::string const & iface)
223 struct ipv6_mreq mreqn;
224 std::copy(mcAddr.begin(), mcAddr.end(), mreqn.ipv6mr_multiaddr.s6_addr);
225 mreqn.ipv6mr_interface = if_nametoindex(iface.c_str());
226 if (mreqn.ipv6mr_interface == 0)
227 throw SystemException("::if_nametoindex()",ENOENT);
228 if (::setsockopt(fd(),SOL_IP,IPV6_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
229 throw SystemException();
232 ///////////////////////////////cc.e////////////////////////////////////////
234 //#include "MulticastSocketProtocol.mpp"
240 // comment-column: 40
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"