Renamed namespaces satcom::lib and satcom::pkf to senf
[senf.git] / Socket / INetProtocol.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 // Definition of non-inline non-template functions
24
25 #include "INetProtocol.hh"
26 //#include "INetProtocol.ih"
27
28 // Custom includes
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <net/if.h> // for if_nametoindex
32 #include "Utils/Exception.hh"
33
34 //#include "INetProtocol.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::INet4Protocol
40
41 prefix_ void senf::IPv4Protocol::connect(INet4Address const & address)
42     const
43 {
44     if (::connect(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
45         throw SystemException(errno);
46 }
47
48 prefix_ void senf::IPv4Protocol::bind(INet4Address const & address)
49     const
50 {
51     if (::bind(body().fd(),address.sockaddr_p(), address.sockaddr_len()) < 0)
52         throw SystemException(errno);
53 }
54
55 prefix_ bool senf::IPv4Protocol::mcLoop()
56     const
57 {
58     int value;
59     socklen_t len (sizeof(value));
60     if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&value,&len) < 0)
61         throw SystemException(errno);
62     return value;
63 }
64
65 prefix_ void senf::IPv4Protocol::mcLoop(bool value)
66     const
67 {
68     int ivalue (value);
69     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_LOOP,&ivalue,sizeof(ivalue)) < 0)
70         throw SystemException(errno);
71 }
72
73 prefix_ void senf::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr)
74     const
75 {
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);
82 }
83
84 prefix_ void senf::IPv4Protocol::mcAddMembership(INet4Address const & mcAddr,
85                                                         INet4Address const & localAddr)
86     const
87 {
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);
94 }
95
96 prefix_ void senf::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr)
97     const
98 {
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);
105 }
106
107 prefix_ void senf::IPv4Protocol::mcDropMembership(INet4Address const & mcAddr,
108                                                          INet4Address const & localAddr)
109     const
110 {
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);
117 }
118
119 prefix_ void senf::IPv4Protocol::mcIface(std::string iface)
120     const
121 {
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);
128     }
129     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_IF,&mreqn,sizeof(mreqn)) < 0)
130         throw SystemException(errno);
131 }
132
133 prefix_ unsigned senf::IPv4Protocol::mcTTL()
134     const
135 {
136     int value;
137     socklen_t len (sizeof(value));
138     if (::getsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,&len) < 0)
139         throw SystemException(errno);
140     return value;
141 }
142
143 prefix_ void senf::IPv4Protocol::mcTTL(unsigned value)
144     const
145 {
146     if (::setsockopt(body().fd(),SOL_IP,IP_MULTICAST_TTL,&value,sizeof(value)) < 0)
147         throw SystemException(errno);
148 }
149
150
151 ///////////////////////////////cc.e////////////////////////////////////////
152 #undef prefix_
153 //#include "INetProtocol.mpp"
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // c-file-style: "senf"
159 // End: