4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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 TCPProtocol non-inline non-template implementation
27 #include "UDPProtocol.hh"
28 //#include "UDPProtocol.ih"
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"
39 //#include "UDPProtocol.mpp"
41 ///////////////////////////////cc.p////////////////////////////////////////
43 prefix_ unsigned senf::UDPProtocol::available()
47 if (::ioctl(body().fd(),SIOCINQ,&n) < 0)
48 throw senf::SystemException(errno);
52 prefix_ bool senf::UDPProtocol::eof()
58 prefix_ bool senf::UDPProtocol::mcLoop()
62 socklen_t len (sizeof(value));
63 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
64 throw SystemException(errno);
68 prefix_ void senf::UDPProtocol::mcLoop(bool value)
72 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
73 throw SystemException(errno);
76 prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr)
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);
87 prefix_ void senf::UDPProtocol::mcAddMembership(INet4Address const & mcAddr,
88 INet4Address const & localAddr)
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);
99 prefix_ void senf::UDPProtocol::mcDropMembership(INet4Address const & mcAddr)
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);
110 prefix_ void senf::UDPProtocol::mcDropMembership(INet4Address const & mcAddr,
111 INet4Address const & localAddr)
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);
122 prefix_ void senf::UDPProtocol::mcIface(std::string iface)
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);
132 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
133 throw SystemException(errno);
136 prefix_ unsigned senf::UDPProtocol::mcTTL()
140 socklen_t len (sizeof(value));
141 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
142 throw SystemException(errno);
146 prefix_ void senf::UDPProtocol::mcTTL(unsigned value)
149 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
150 throw SystemException(errno);
154 ///////////////////////////////cc.e////////////////////////////////////////
156 //#include "UDPProtocol.mpp"
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