36 #include <arpa/inet.h> 37 #include <sys/ioctl.h> 38 #include <sys/types.h> 39 #include <sys/socket.h> 45 const std::string & sDevice,
58 const std::string & sDevice,
80 IPPROTO_IP,IP_MULTICAST_TTL,
84 throw makeException<SocketException>(
"setsockopt IP_MULTICAST_TTL: %s",
91 std::uint8_t u8Option = 1;
94 if(setsockopt(
iSock_,IPPROTO_IP,IP_MULTICAST_LOOP,
96 sizeof(u8Option)) < 0)
98 throw makeException<SocketException>(
"setsockopt IP_MULTICAST_LOOP: %s",
106 memset(&mreq,0,
sizeof(mreq));
108 memcpy(&mreq.imr_multiaddr,
110 sizeof(mreq.imr_multiaddr));
115 memset(&ifr,0,
sizeof(ifr));
116 strncpy(ifr.ifr_name,sDevice.c_str(),IFNAMSIZ);
119 if(ioctl(
iSock_,SIOCGIFADDR,&ifr) < 0)
121 throw makeException<SocketException>(
"ioctl SIOCGIFADDR: %s",
129 &reinterpret_cast<sockaddr_in*>(&ifr.ifr_addr)->sin_addr.s_addr,
130 sizeof(in_addr)) < 0)
132 throw makeException<SocketException>(
"setsockopt IP_MULTICAST_IF: %s",
136 mreq.imr_interface.s_addr =
137 reinterpret_cast<sockaddr_in*
>(&ifr.ifr_addr)->
sin_addr.s_addr;
141 mreq.imr_interface.s_addr = INADDR_ANY;
147 reinterpret_cast<void*>(&mreq),
150 throw makeException<SocketException>(
"setsockopt IP_ADD_MEMBERSHIP: %s",
163 sizeof(iOption)) < 0)
165 throw makeException<SocketException>(
"setsockopt IPV6_MULTICAST_HOPS: %s",
177 sizeof(iOption)) < 0)
179 throw makeException<SocketException>(
"setsockopt IPV6_MULTICAST_LOOP: %s",
186 memset(&mreq6,0,
sizeof(mreq6));
188 memcpy(&mreq6.ipv6mr_multiaddr,
189 &reinterpret_cast<sockaddr_in6 *>(address.
getSockAddr())->sin6_addr,
190 sizeof(mreq6.ipv6mr_multiaddr));
192 mreq6.ipv6mr_interface = 0;
196 unsigned int iIndex{if_nametoindex(sDevice.c_str())};
204 throw makeException<SocketException>(
"setsockopt IPV6_MULTICAST_IF: %s",
208 mreq6.ipv6mr_interface = iIndex;
214 reinterpret_cast<void *>(&mreq6),
217 throw makeException<SocketException>(
"setsockopt IPV6_ADD_MEMBERSHIP: %s",
223 throw makeException<SocketException>(
"Unknown address family");
233 reinterpret_cast<void*>(&iOption),
234 sizeof(iOption)) < 0)
236 throw makeException<SocketException>(
"setsockopt SO_REUSEADDR: %s",
245 throw makeException<SocketException>(
"bind: %s",
257 memset(&msg,0,
sizeof(msg));
259 msg.msg_iov =
const_cast<iovec *
>(iov);
260 msg.msg_iovlen = iovcnt;
265 return sendmsg(
iSock_,&msg,flags);
272 return ::recv(
iSock_,buf,len,flags);
ssize_t recv(void *buf, size_t len, int flags=0)
socklen_t getAddrLength() const
sockaddr * getSockAddr() const
ssize_t send(const iovec *iov, int iovcnt, int flags=0) const
void open(const INETAddr &address, bool bReuseAddress=false, const std::string &sDevice="", std::uint8_t u8TTL=1, bool bLoop=false)