Skip to content

Commit

Permalink
several bugfixes/enhancements from 'PROCITEC GmbH' company
Browse files Browse the repository at this point in the history
with kind permission of Procitec:
* bugfixes in multicast UDP operation
* enhancend for multithreaded use:
 - use GetAddrInfo[Static]() instead of deprecated GETHOSTBYNAME()
 - GetIPv4AddrInfoStatic() to allow address resolution without
   a connection instance
* docs and other minor enhancements

Signed-off-by: hayati ayguen <[email protected]>
  • Loading branch information
hayguen committed May 3, 2018
1 parent 10e9a12 commit ce59dda
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 67 deletions.
34 changes: 21 additions & 13 deletions src/PassiveSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,26 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u
// If no IP Address (interface ethn) is supplied, or the loop back is
// specified then bind to any interface, else bind to specified interface.
//--------------------------------------------------------------------------
if ((pInterface == NULL) || (!strlen(pInterface)))
if (pInterface && pInterface[0])
{
m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
inet_pton(AF_INET, pInterface, &inAddr);
}
else
{
if ((inAddr = inet_addr(pInterface)) != INADDR_NONE)
{
m_stMulticastGroup.sin_addr.s_addr = inAddr;
}
inAddr = INADDR_ANY;
}

if (pGroup && pGroup[0] != 0)
{
m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
m_stMulticastGroup.sin_addr.s_addr = inAddr;
}

// multicast address/port is the server
memcpy(&m_stServerSockaddr,&m_stMulticastGroup,sizeof(m_stServerSockaddr));

ClearSystemError();

//--------------------------------------------------------------------------
Expand All @@ -103,13 +108,13 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u
m_bIsServerSide = true;
if (bind(m_socket, (struct sockaddr *)&m_stMulticastGroup, sizeof(m_stMulticastGroup)) == 0)
{
if ( pGroup )
if ( pGroup && pGroup[0] != 0 )
{
//----------------------------------------------------------------------
// Join the multicast group
//----------------------------------------------------------------------
m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr(pGroup);
m_stMulticastRequest.imr_interface.s_addr = m_stMulticastGroup.sin_addr.s_addr;
inet_pton(AF_INET, pGroup, &m_stMulticastRequest.imr_multiaddr.s_addr);
m_stMulticastRequest.imr_interface.s_addr = inAddr;

if ( SETSOCKOPT(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void *)&m_stMulticastRequest,
Expand Down Expand Up @@ -148,7 +153,9 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u

//------------------------------------------------------------------------------
//
// Listen() -
// Listen() - Create a listening socket (server) at local ip address 'x.x.x.x' or 'localhost'
// waiting for an incoming connection from client(s)
// also see .h
//
//------------------------------------------------------------------------------
bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog)
Expand Down Expand Up @@ -189,7 +196,8 @@ bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBa
}
else
{
if ((inAddr = inet_addr(pAddr)) != INADDR_NONE)
inet_pton(AF_INET, pAddr, &inAddr);
if (inAddr != INADDR_NONE)
{
m_stServerSockaddr.sin_addr.s_addr = inAddr;
}
Expand Down Expand Up @@ -268,7 +276,7 @@ CSimpleSocket *CPassiveSocket::Accept()
errno = 0;
socket = accept(m_socket, (struct sockaddr *)&m_stClientSockaddr, (socklen_t *)&nSockLen);

if (socket != -1)
if (socket != INVALID_SOCKET)
{
pClientSocket = new CSimpleSocket();
if ( !pClientSocket )
Expand Down
3 changes: 2 additions & 1 deletion src/PassiveSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class CPassiveSocket : public CSimpleSocket {
return BindMulticast(pInterface, pNoGroup, nPort);
}

/// Create a listening socket at local ip address 'x.x.x.x' or 'localhost'
/// Create a listening socket (server) at local ip address 'x.x.x.x' or 'localhost'
/// waiting for an incoming connection from client(s)
/// if pAddr is NULL on port nPort.
///
/// @param pAddr specifies the IP address on which to listen.
Expand Down
Loading

0 comments on commit ce59dda

Please sign in to comment.