Skip to content

Commit ce59dda

Browse files
committed
several bugfixes/enhancements from 'PROCITEC GmbH' company
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]>
1 parent 10e9a12 commit ce59dda

File tree

4 files changed

+304
-67
lines changed

4 files changed

+304
-67
lines changed

src/PassiveSocket.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,26 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u
8080
// If no IP Address (interface ethn) is supplied, or the loop back is
8181
// specified then bind to any interface, else bind to specified interface.
8282
//--------------------------------------------------------------------------
83-
if ((pInterface == NULL) || (!strlen(pInterface)))
83+
if (pInterface && pInterface[0])
8484
{
85-
m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
85+
inet_pton(AF_INET, pInterface, &inAddr);
8686
}
8787
else
8888
{
89-
if ((inAddr = inet_addr(pInterface)) != INADDR_NONE)
90-
{
91-
m_stMulticastGroup.sin_addr.s_addr = inAddr;
92-
}
89+
inAddr = INADDR_ANY;
90+
}
91+
92+
if (pGroup && pGroup[0] != 0)
93+
{
94+
m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
95+
}
96+
else
97+
{
98+
m_stMulticastGroup.sin_addr.s_addr = inAddr;
9399
}
94100

95101
// multicast address/port is the server
96102
memcpy(&m_stServerSockaddr,&m_stMulticastGroup,sizeof(m_stServerSockaddr));
97-
98103
ClearSystemError();
99104

100105
//--------------------------------------------------------------------------
@@ -103,13 +108,13 @@ bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, u
103108
m_bIsServerSide = true;
104109
if (bind(m_socket, (struct sockaddr *)&m_stMulticastGroup, sizeof(m_stMulticastGroup)) == 0)
105110
{
106-
if ( pGroup )
111+
if ( pGroup && pGroup[0] != 0 )
107112
{
108113
//----------------------------------------------------------------------
109114
// Join the multicast group
110115
//----------------------------------------------------------------------
111-
m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr(pGroup);
112-
m_stMulticastRequest.imr_interface.s_addr = m_stMulticastGroup.sin_addr.s_addr;
116+
inet_pton(AF_INET, pGroup, &m_stMulticastRequest.imr_multiaddr.s_addr);
117+
m_stMulticastRequest.imr_interface.s_addr = inAddr;
113118

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

149154
//------------------------------------------------------------------------------
150155
//
151-
// Listen() -
156+
// Listen() - Create a listening socket (server) at local ip address 'x.x.x.x' or 'localhost'
157+
// waiting for an incoming connection from client(s)
158+
// also see .h
152159
//
153160
//------------------------------------------------------------------------------
154161
bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog)
@@ -189,7 +196,8 @@ bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBa
189196
}
190197
else
191198
{
192-
if ((inAddr = inet_addr(pAddr)) != INADDR_NONE)
199+
inet_pton(AF_INET, pAddr, &inAddr);
200+
if (inAddr != INADDR_NONE)
193201
{
194202
m_stServerSockaddr.sin_addr.s_addr = inAddr;
195203
}
@@ -268,7 +276,7 @@ CSimpleSocket *CPassiveSocket::Accept()
268276
errno = 0;
269277
socket = accept(m_socket, (struct sockaddr *)&m_stClientSockaddr, (socklen_t *)&nSockLen);
270278

271-
if (socket != -1)
279+
if (socket != INVALID_SOCKET)
272280
{
273281
pClientSocket = new CSimpleSocket();
274282
if ( !pClientSocket )

src/PassiveSocket.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class CPassiveSocket : public CSimpleSocket {
9797
return BindMulticast(pInterface, pNoGroup, nPort);
9898
}
9999

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

0 commit comments

Comments
 (0)