122 int enable = fEnable?1:0;
130 ret = setsockopt(
_sock, level, option1, &enable,
sizeof(enable));
133 if ((ret < 0) && (option2 != -1))
135 enable = fEnable?1:0;
136 ret = setsockopt(
_sock, level, option2, &enable,
sizeof(enable));
147 int level = IPPROTO_IP;
152 option1 = IP_PKTINFO;
155 #ifdef IP_RECVDSTADDR 156 option2 = IP_RECVDSTADDR;
164 int level = IPPROTO_IPV6;
168 #ifdef IPV6_RECVPKTINFO 169 option1 = IPV6_RECVPKTINFO;
173 option2 = IPV6_PKTINFO;
184 if (family == AF_INET)
203 #ifdef IPV6_BINDV6ONLY 204 optname = IPV6_BINDV6ONLY;
206 optname = IPV6_V6ONLY;
211 result = setsockopt(sock, IPPROTO_IPV6, optname, (
char *)&enabled,
sizeof(enabled));
223 flags = ::fcntl(
_sock, F_GETFL, 0);
233 flags &= ~(O_NONBLOCK);
236 result = fcntl(
_sock , F_SETFL , flags);
247 sockaddr_storage addrLocal = {};
248 sockaddr_storage addrRemote = {};
259 len =
sizeof(addrLocal);
260 ret = ::getsockname(
_sock, (sockaddr*)&addrLocal, &len);
266 len =
sizeof(addrRemote);
267 ret = ::getpeername(
_sock, (sockaddr*)&addrRemote, &len);
282 ASSERT((socktype == SOCK_DGRAM)||(socktype==SOCK_STREAM));
284 sock = socket(addrlocal.
GetFamily(), socktype, 0);
299 ret = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &fAllow,
sizeof(fAllow));
324 return InitCommon(SOCK_DGRAM, local, role,
false);
329 return InitCommon(SOCK_STREAM, local, role, fSetReuseFlag);
HRESULT EnablePktInfoImpl(int level, int option1, int option2, bool fEnable)
socklen_t GetSockAddrLength() const
void SetRole(SocketRole role)
CSocketAddress _addrlocal
#define ChkIf(expr, hrerror)
HRESULT SetV6Only(int sock)
HRESULT EnablePktInfo_IPV4(bool fEnable)
int GetSocketHandle() const
SocketRole GetRole() const
const sockaddr * GetSockAddr() const
HRESULT EnablePktInfoOption(bool fEnable)
HRESULT InitCommon(int socktype, const CSocketAddress &addrlocal, SocketRole role, bool fSetReuseFlag)
CSocketAddress _addrremote
const CSocketAddress & GetRemoteAddress() const
HRESULT EnablePktInfo_IPV6(bool fEnable)
HRESULT UDPInit(const CSocketAddress &local, SocketRole role)
uint16_t GetFamily() const
HRESULT TCPInit(const CSocketAddress &local, SocketRole role, bool fSetReuseFlag)
HRESULT SetNonBlocking(bool fEnable)
#define ChkIfA(expr, hrerror)
const CSocketAddress & GetLocalAddress() const