2013-01-13 01:09:24 +01:00
|
|
|
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2005-2011 by Eric House (xwords@eehouse.org). All rights
|
|
|
|
* reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2017-12-07 04:24:52 +01:00
|
|
|
#include <errno.h>
|
2013-01-13 01:09:24 +01:00
|
|
|
#include <string.h>
|
2013-06-21 15:05:26 +02:00
|
|
|
#include <time.h>
|
2017-12-07 04:24:52 +01:00
|
|
|
#include <unistd.h>
|
2013-01-13 01:09:24 +01:00
|
|
|
|
|
|
|
#include "addrinfo.h"
|
2013-06-21 15:05:26 +02:00
|
|
|
#include "xwrelay_priv.h"
|
|
|
|
#include "tpool.h"
|
2013-07-23 15:21:57 +02:00
|
|
|
#include "udpager.h"
|
2017-12-07 04:24:52 +01:00
|
|
|
#include "mlock.h"
|
2013-06-21 15:05:26 +02:00
|
|
|
|
2013-06-25 16:58:15 +02:00
|
|
|
// static uint32_t s_prevCreated = 0L;
|
|
|
|
|
2013-06-21 15:05:26 +02:00
|
|
|
void
|
2015-11-21 08:15:04 +01:00
|
|
|
AddrInfo::construct( int sock, const AddrUnion* saddr, bool isTCP )
|
2013-06-21 15:05:26 +02:00
|
|
|
{
|
|
|
|
memset( this, 0, sizeof(*this) );
|
|
|
|
|
|
|
|
struct timespec tp;
|
|
|
|
clock_gettime( CLOCK_MONOTONIC, &tp );
|
2013-06-25 16:58:15 +02:00
|
|
|
/* convert to milliseconds */
|
2013-07-20 17:33:22 +02:00
|
|
|
m_createdMillis = (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
|
2013-06-25 16:58:15 +02:00
|
|
|
/* assert( m_created >= s_prevCreated ); */
|
|
|
|
/* s_prevCreated = m_created; */
|
2013-06-21 15:05:26 +02:00
|
|
|
|
2015-11-21 08:15:04 +01:00
|
|
|
m_socket = sock;
|
2013-06-21 15:05:26 +02:00
|
|
|
m_isTCP = isTCP;
|
|
|
|
memcpy( &m_saddr, saddr, sizeof(m_saddr) );
|
|
|
|
m_isValid = true;
|
|
|
|
}
|
2013-01-13 01:09:24 +01:00
|
|
|
|
2013-06-21 15:14:10 +02:00
|
|
|
bool
|
|
|
|
AddrInfo::isCurrent() const
|
|
|
|
{
|
2013-07-23 15:21:57 +02:00
|
|
|
bool result;
|
|
|
|
if ( isTCP() ) {
|
|
|
|
result = XWThreadPool::GetTPool()->IsCurrent( this );
|
|
|
|
} else {
|
|
|
|
result = UDPAger::Get()->IsCurrent( this );
|
|
|
|
}
|
|
|
|
return result;
|
2013-06-21 15:14:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-13 01:09:24 +01:00
|
|
|
bool
|
|
|
|
AddrInfo::equals( const AddrInfo& other ) const
|
|
|
|
{
|
|
|
|
bool equal = other.isTCP() == isTCP();
|
|
|
|
if ( equal ) {
|
|
|
|
if ( isTCP() ) {
|
|
|
|
equal = m_socket == other.m_socket;
|
2013-06-21 15:05:26 +02:00
|
|
|
if ( equal && created() != other.created() ) {
|
2017-12-04 04:18:36 +01:00
|
|
|
logf( XW_LOGINFO, "%s(): rejecting on time mismatch (%lx vs %lx)",
|
2013-06-21 15:05:26 +02:00
|
|
|
__func__, created(), other.created() );
|
|
|
|
equal = false;
|
|
|
|
}
|
2013-01-13 01:09:24 +01:00
|
|
|
} else {
|
2013-02-04 15:08:39 +01:00
|
|
|
// assert( m_socket == other.m_socket ); /* both same UDP socket */
|
|
|
|
/* what does equal mean on udp addresses? Same host, or same host AND game */
|
|
|
|
equal = m_clientToken == other.m_clientToken
|
|
|
|
&& 0 == memcmp( &m_saddr, &other.m_saddr, sizeof(m_saddr) );
|
2013-01-13 01:09:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:24:52 +01:00
|
|
|
static pthread_mutex_t s_refMutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static map<int, int > s_socketRefs;
|
|
|
|
|
|
|
|
void AddrInfo::ref() const
|
|
|
|
{
|
|
|
|
// logf( XW_LOGVERBOSE0, "%s(socket=%d)", __func__, m_socket );
|
|
|
|
MutexLock ml( &s_refMutex );
|
|
|
|
++s_socketRefs[m_socket];
|
|
|
|
printRefMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddrInfo::unref() const
|
|
|
|
{
|
|
|
|
// logf( XW_LOGVERBOSE0, "%s(socket=%d)", __func__, m_socket );
|
|
|
|
|
|
|
|
MutexLock ml( &s_refMutex );
|
|
|
|
assert( s_socketRefs[m_socket] > 0 );
|
|
|
|
--s_socketRefs[m_socket];
|
|
|
|
if ( s_socketRefs[m_socket] == 0 ) {
|
|
|
|
XWThreadPool::GetTPool()->CloseSocket( this );
|
|
|
|
}
|
|
|
|
printRefMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* private, and assumes have mutex */
|
|
|
|
void
|
|
|
|
AddrInfo::printRefMap() const
|
|
|
|
{
|
|
|
|
/* for ( map<int,int>::const_iterator iter = s_socketRefs.begin(); */
|
|
|
|
/* iter != s_socketRefs.end(); ++iter ) { */
|
|
|
|
/* int count = iter->second; */
|
|
|
|
/* if ( count > 0 ) { */
|
|
|
|
/* logf( XW_LOGVERBOSE0, "socket: %d; count: %d", iter->first, count ); */
|
|
|
|
/* } */
|
|
|
|
/* } */
|
|
|
|
}
|