Fix networking on FreeBSD (#1998)

stable-8
Kevin Zheng 2018-02-17 11:59:24 +01:00 committed by Lukas Werling
parent 1f20add428
commit d57e0e08d2
2 changed files with 10 additions and 4 deletions

View File

@ -523,6 +523,11 @@ C4NetIO::HostAddress::AddressFamily C4NetIO::HostAddress::GetFamily() const
gen.sa_family == AF_INET6 ? IPv6 : UnknownFamily; gen.sa_family == AF_INET6 ? IPv6 : UnknownFamily;
} }
size_t C4NetIO::HostAddress::GetAddrLen() const
{
return GetFamily() == IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
}
void C4NetIO::EndpointAddress::SetPort(uint16_t port) void C4NetIO::EndpointAddress::SetPort(uint16_t port)
{ {
switch (gen.sa_family) switch (gen.sa_family)
@ -1195,7 +1200,7 @@ bool C4NetIOTCP::Connect(const C4NetIO::addr_t &addr) // (mt-safe)
#endif #endif
// connect (async) // connect (async)
if (::connect(nsock, &addr, sizeof addr) == SOCKET_ERROR) if (::connect(nsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
{ {
if (!HaveWouldBlockError()) // expected if (!HaveWouldBlockError()) // expected
{ {
@ -1368,7 +1373,7 @@ C4NetIOTCP::Peer *C4NetIOTCP::Accept(SOCKET nsock, const addr_t &ConnectAddr) //
addr_t caddr = ConnectAddr; addr_t caddr = ConnectAddr;
// accept incoming connection? // accept incoming connection?
C4NetIO::addr_t addr; socklen_t iAddrSize = sizeof addr; C4NetIO::addr_t addr; socklen_t iAddrSize = addr.GetAddrLen();
if (nsock == INVALID_SOCKET) if (nsock == INVALID_SOCKET)
{ {
// accept from listener // accept from listener
@ -1497,7 +1502,7 @@ bool C4NetIOTCP::Listen(uint16_t inListenPort)
// bind listen socket // bind listen socket
addr_t addr = addr_t::Any; addr_t addr = addr_t::Any;
addr.SetPort(inListenPort); addr.SetPort(inListenPort);
if (::bind(lsock, &addr, sizeof(addr)) == SOCKET_ERROR) if (::bind(lsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
{ {
SetError("socket bind failed", true); SetError("socket bind failed", true);
closesocket(lsock); lsock = INVALID_SOCKET; closesocket(lsock); lsock = INVALID_SOCKET;
@ -2122,7 +2127,7 @@ bool C4NetIOSimpleUDP::Send(const C4NetIOPacket &rPacket)
// send it // send it
C4NetIO::addr_t addr = rPacket.getAddr(); C4NetIO::addr_t addr = rPacket.getAddr();
if (::sendto(sock, getBufPtr<char>(rPacket), rPacket.getSize(), 0, if (::sendto(sock, getBufPtr<char>(rPacket), rPacket.getSize(), 0,
&addr, sizeof(addr)) &addr, addr.GetAddrLen())
!= int(rPacket.getSize()) && != int(rPacket.getSize()) &&
!HaveWouldBlockError()) !HaveWouldBlockError())
{ {

View File

@ -94,6 +94,7 @@ public:
HostAddress(const sockaddr *addr) { SetHost(addr); } HostAddress(const sockaddr *addr) { SetHost(addr); }
AddressFamily GetFamily() const; AddressFamily GetFamily() const;
size_t GetAddrLen() const;
void SetScopeId(int scopeId); void SetScopeId(int scopeId);
int GetScopeId() const; int GetScopeId() const;