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 INet[46]Protocol non-inline non-template implementation */
26 #include "INetProtocol.hh"
27 //#include "INetProtocol.ih"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <net/if.h> // for if_nametoindex
33 #include "Utils/Exception.hh"
35 //#include "INetProtocol.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::INet4Protocol
42 prefix_ void senf::IPv4Protocol::connect(INet4Address const & address)
45 if (::connect(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
46 throw SystemException(errno);
49 prefix_ void senf::IPv4Protocol::bind(INet4Address const & address)
52 if (::bind(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
53 throw SystemException(errno);
56 prefix_ bool senf::IPv4Protocol::mcLoop()
60 socklen_t len (sizeof(value));
61 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
62 throw SystemException(errno);
66 prefix_ void senf::IPv4Protocol::mcLoop(bool value)
70 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
71 throw SystemException(errno);
74 prefix_ void senf::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr)
77 struct ip_mreqn mreqn;
78 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
79 mreqn.imr_address.s_addr = htons(INADDR_ANY);
80 mreqn.imr_ifindex = 0;
81 if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
82 throw SystemException(errno);
85 prefix_ void senf::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr,
86 INet4Address const & localAddr)
89 struct ip_mreqn mreqn;
90 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
91 mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
92 mreqn.imr_ifindex = 0;
93 if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
94 throw SystemException(errno);
97 prefix_ void senf::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr)
100 struct ip_mreqn mreqn;
101 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
102 mreqn.imr_address.s_addr = htons(INADDR_ANY);
103 mreqn.imr_ifindex = 0;
104 if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
105 throw SystemException(errno);
108 prefix_ void senf::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr,
109 INet4Address const & localAddr)
112 struct ip_mreqn mreqn;
113 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
114 mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
115 mreqn.imr_ifindex = 0;
116 if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
117 throw SystemException(errno);
120 prefix_ void senf::IPv4Protocol::mcIface(std::string iface)
123 struct ip_mreqn mreqn;
124 ::memset(&mreqn,sizeof(mreqn),0);
125 if (!iface.empty()) {
126 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
127 if (mreqn.imr_ifindex == 0)
128 throw SystemException(EINVAL);
130 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
131 throw SystemException(errno);
134 prefix_ unsigned senf::IPv4Protocol::mcTTL()
138 socklen_t len (sizeof(value));
139 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
140 throw SystemException(errno);
144 prefix_ void senf::IPv4Protocol::mcTTL(unsigned value)
147 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
148 throw SystemException(errno);
152 ///////////////////////////////cc.e////////////////////////////////////////
154 //#include "INetProtocol.mpp"
159 // c-file-style: "senf"