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.
23 // Definition of non-inline non-template functions
25 #include "INetProtocol.hh"
26 //#include "INetProtocol.ih"
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <net/if.h> // for if_nametoindex
32 #include "Utils/Exception.hh"
34 //#include "INetProtocol.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // satcom::lib::INet4Protocol
41 prefix_ void satcom::lib::IPv4Protocol::connect(INet4Address const & address)
44 if (::connect(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
45 throw SystemException(errno);
48 prefix_ void satcom::lib::IPv4Protocol::bind(INet4Address const & address)
51 if (::bind(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
52 throw SystemException(errno);
55 prefix_ bool satcom::lib::IPv4Protocol::mcLoop()
59 socklen_t len (sizeof(value));
60 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
61 throw SystemException(errno);
65 prefix_ void satcom::lib::IPv4Protocol::mcLoop(bool value)
69 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
70 throw SystemException(errno);
73 prefix_ void satcom::lib::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr)
76 struct ip_mreqn mreqn;
77 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
78 mreqn.imr_address.s_addr = htons(INADDR_ANY);
79 mreqn.imr_ifindex = 0;
80 if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
81 throw SystemException(errno);
84 prefix_ void satcom::lib::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr,
85 INet4Address const & localAddr)
88 struct ip_mreqn mreqn;
89 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
90 mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
91 mreqn.imr_ifindex = 0;
92 if (::setsockopt(body().fd(),SOL_IP,IP_ADD_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
93 throw SystemException(errno);
96 prefix_ void satcom::lib::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr)
99 struct ip_mreqn mreqn;
100 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
101 mreqn.imr_address.s_addr = htons(INADDR_ANY);
102 mreqn.imr_ifindex = 0;
103 if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
104 throw SystemException(errno);
107 prefix_ void satcom::lib::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr,
108 INet4Address const & localAddr)
111 struct ip_mreqn mreqn;
112 mreqn.imr_multiaddr = reinterpret_cast<struct sockaddr_in const *>(mcAddr.sockaddr_p())->sin_addr;
113 mreqn.imr_address = reinterpret_cast<struct sockaddr_in const *>(localAddr.sockaddr_p())->sin_addr;
114 mreqn.imr_ifindex = 0;
115 if (::setsockopt(body().fd(),SOL_IP,IP_DROP_MEMBERSHIP,&mreqn,sizeof(mreqn)) < 0)
116 throw SystemException(errno);
119 prefix_ void satcom::lib::IPv4Protocol::mcIface(std::string iface)
122 struct ip_mreqn mreqn;
123 ::memset(&mreqn,sizeof(mreqn),0);
124 if (!iface.empty()) {
125 mreqn.imr_ifindex = if_nametoindex(iface.c_str());
126 if (mreqn.imr_ifindex == 0)
127 throw SystemException(EINVAL);
129 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
130 throw SystemException(errno);
133 prefix_ unsigned satcom::lib::IPv4Protocol::mcTTL()
137 socklen_t len (sizeof(value));
138 if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
139 throw SystemException(errno);
143 prefix_ void satcom::lib::IPv4Protocol::mcTTL(unsigned value)
146 if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
147 throw SystemException(errno);
151 ///////////////////////////////cc.e////////////////////////////////////////
153 //#include "INetProtocol.mpp"
158 // c-file-style: "satcom"