2008-01-25 01:11:39 -05:00
|
|
|
/*
|
2010-07-31 14:14:10 -04:00
|
|
|
* tcponudp/udpstunner.cc
|
2008-01-25 01:11:39 -05:00
|
|
|
*
|
2010-07-31 14:14:10 -04:00
|
|
|
* libretroshare.
|
2008-01-25 01:11:39 -05:00
|
|
|
*
|
2010-07-31 14:14:10 -04:00
|
|
|
* Copyright 2010 by Robert Fernie
|
2008-01-25 01:11:39 -05:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
2010-07-31 14:14:10 -04:00
|
|
|
* License Version 3 as published by the Free Software Foundation.
|
2008-01-25 01:11:39 -05:00
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
#include "tcponudp/udpstunner.h"
|
2008-01-25 01:11:39 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2008-07-10 12:29:18 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
#include "util/rsrandom.h"
|
2011-06-29 14:02:44 -04:00
|
|
|
#include "util/rsprint.h"
|
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
static const int STUN_TTL = 64;
|
|
|
|
|
2011-07-12 10:16:46 -04:00
|
|
|
#define TOU_STUN_MIN_PEERS 20
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
/*
|
2011-06-29 14:02:44 -04:00
|
|
|
* #define DEBUG_UDP_STUNNER 1
|
2011-07-12 10:16:46 -04:00
|
|
|
* #define DEBUG_UDP_STUNNER_FILTER 1
|
2008-01-25 01:11:39 -05:00
|
|
|
*/
|
2011-07-12 10:16:46 -04:00
|
|
|
|
2011-07-15 07:13:20 -04:00
|
|
|
//#define DEBUG_UDP_STUNNER 1
|
2008-01-25 01:11:39 -05:00
|
|
|
|
2009-08-04 19:22:44 -04:00
|
|
|
const int32_t TOU_STUN_MAX_FAIL_COUNT = 3; /* 3 tries (could be higher?) */
|
|
|
|
const int32_t TOU_STUN_MAX_SEND_RATE = 5; /* every 5 seconds */
|
|
|
|
const int32_t TOU_STUN_MAX_RECV_RATE = 25; /* every 25 seconds */
|
2011-06-29 14:02:44 -04:00
|
|
|
// TIMEOUT is now tied to STUN RATE ... const int32_t TOU_STUN_ADDR_MAX_AGE = 120; /* 2 minutes */
|
|
|
|
|
|
|
|
const int32_t TOU_STUN_DEFAULT_TARGET_RATE = 15; /* 20 secs is minimum to keep a NAT UDP port open */
|
|
|
|
const double TOU_SUCCESS_LPF_FACTOR = 0.90;
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2011-07-27 09:55:19 -04:00
|
|
|
#define EXCLUSIVE_MODE_TIMEOUT 300
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
UdpStunner::UdpStunner(UdpPublisher *pub)
|
2011-07-04 18:59:39 -04:00
|
|
|
:UdpSubReceiver(pub), stunMtx("UdpSubReceiver"), eaddrKnown(false), eaddrStable(false),
|
2011-06-29 14:02:44 -04:00
|
|
|
mStunLastRecvResp(0), mStunLastRecvAny(0),
|
|
|
|
mStunLastSendStun(0), mStunLastSendAny(0)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
mAcceptLocalNet = false;
|
2011-07-17 09:48:37 -04:00
|
|
|
mSimExclusiveNat = false;
|
|
|
|
mSimSymmetricNat = false;
|
|
|
|
mSimUnstableExt = false;
|
2011-06-29 14:02:44 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* these parameters determine the rate we attempt stuns */
|
|
|
|
mPassiveStunMode = false;
|
|
|
|
mSuccessRate = 0.0;
|
|
|
|
mTargetStunPeriod = TOU_STUN_DEFAULT_TARGET_RATE;
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
mExclusiveMode = false;
|
|
|
|
mExclusiveModeTS = 0;
|
2011-07-17 12:24:45 -04:00
|
|
|
mForceRestun = false;
|
2011-07-13 07:41:25 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
|
|
|
|
// For Local Testing Only (Releases should have the #define disabled)
|
|
|
|
void UdpStunner::SetAcceptLocalNet()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
|
|
|
mAcceptLocalNet = true;
|
|
|
|
}
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
// For Local Testing Only (Releases should have the #define disabled)
|
|
|
|
void UdpStunner::SimExclusiveNat()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
|
|
|
mSimExclusiveNat = true;
|
|
|
|
mSimUnstableExt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UdpStunner::SimSymmetricNat()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
|
|
|
mSimSymmetricNat = true;
|
|
|
|
mSimUnstableExt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
#endif
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
|
2011-07-27 09:55:19 -04:00
|
|
|
|
|
|
|
int UdpStunner::grabExclusiveMode(std::string holder) /* returns seconds since last send/recv */
|
2011-07-13 07:41:25 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2011-07-27 09:55:19 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2011-07-27 09:55:19 -04:00
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode();"
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-27 09:55:19 -04:00
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
if (mExclusiveMode)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2011-07-27 09:55:19 -04:00
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() FAILED";
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-07-27 09:55:19 -04:00
|
|
|
|
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() FAILED, already held by: " << mExclusiveHolder;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() Was Grabbed: " << now - mExclusiveModeTS;
|
|
|
|
std::cerr << " secs ago";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
/* This can happen if AUTH, but START never received! (occasionally).
|
|
|
|
*/
|
|
|
|
if (now - mExclusiveModeTS > EXCLUSIVE_MODE_TIMEOUT)
|
|
|
|
{
|
|
|
|
mExclusiveMode = false;
|
|
|
|
mForceRestun = true;
|
|
|
|
|
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() Held for too Long... TIMEOUT & Stun Forced";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mExclusiveMode = true;
|
|
|
|
mExclusiveModeTS = now;
|
2011-07-27 09:55:19 -04:00
|
|
|
mExclusiveHolder = holder;
|
2011-07-13 07:41:25 -04:00
|
|
|
|
|
|
|
int lastcomms = mStunLastRecvAny;
|
|
|
|
if (mStunLastSendAny > lastcomms)
|
|
|
|
{
|
|
|
|
lastcomms = mStunLastSendAny;
|
|
|
|
}
|
|
|
|
|
|
|
|
int commsage = now - lastcomms;
|
|
|
|
|
|
|
|
/* cannot return 0, as this indicates error */
|
|
|
|
if (commsage == 0)
|
|
|
|
{
|
|
|
|
commsage = 1;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2011-07-27 09:55:19 -04:00
|
|
|
#endif
|
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() SUCCESS. last comms: " << commsage;
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << " ago";
|
|
|
|
std::cerr << std::endl;
|
2011-07-27 09:55:19 -04:00
|
|
|
std::cerr << "UdpStunner::grabExclusiveMode() Exclusive held by: " << mExclusiveHolder;
|
|
|
|
std::cerr << std::endl;
|
2011-07-13 07:41:25 -04:00
|
|
|
|
|
|
|
return commsage;
|
|
|
|
}
|
|
|
|
|
2011-07-27 09:55:19 -04:00
|
|
|
int UdpStunner::releaseExclusiveMode(std::string holder, bool forceStun)
|
2011-07-13 07:41:25 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
|
|
|
if (!mExclusiveMode)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2011-07-27 09:55:19 -04:00
|
|
|
#endif
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << "UdpStunner::cancelExclusiveMode() ERROR, not in exclusive Mode";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
mExclusiveMode = false;
|
2011-07-17 12:24:45 -04:00
|
|
|
if (forceStun)
|
|
|
|
{
|
|
|
|
mForceRestun = true;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
2011-07-17 12:24:45 -04:00
|
|
|
/* if we are simulating an exclusive NAT, then immediately after we release - it'll become unstable.
|
2011-07-17 09:48:37 -04:00
|
|
|
* In reality, it will only become unstable if we have tried a UDP connection.
|
2011-07-17 12:24:45 -04:00
|
|
|
* so we use the forceStun parameter (which is true when a UDP connection has been tried).
|
2011-07-17 09:48:37 -04:00
|
|
|
*/
|
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
if ((mSimExclusiveNat) && (forceStun))
|
2011-07-17 09:48:37 -04:00
|
|
|
{
|
|
|
|
mSimUnstableExt = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-07-27 09:55:19 -04:00
|
|
|
if (mExclusiveHolder != holder)
|
|
|
|
{
|
|
|
|
std::cerr << "UdpStunner::cancelExclusiveMode() ERROR release MisMatch: ";
|
|
|
|
std::cerr << " Original Grabber: ";
|
|
|
|
std::cerr << mExclusiveHolder;
|
|
|
|
std::cerr << " Releaser: ";
|
|
|
|
std::cerr << holder;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2011-07-27 09:55:19 -04:00
|
|
|
#endif
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << "UdpStunner::cancelExclusiveMode() Canceled. Was in ExclusiveMode for: " << now - mExclusiveModeTS;
|
|
|
|
std::cerr << " secs";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-12 10:16:46 -04:00
|
|
|
void UdpStunner::setTargetStunPeriod(int32_t sec_per_stun)
|
2011-06-29 14:02:44 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
2011-07-12 10:16:46 -04:00
|
|
|
if (sec_per_stun < 0)
|
2011-06-29 14:02:44 -04:00
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
mPassiveStunMode = false;
|
|
|
|
mTargetStunPeriod = TOU_STUN_DEFAULT_TARGET_RATE;
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
if (sec_per_stun == 0)
|
|
|
|
{
|
|
|
|
mPassiveStunMode = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mPassiveStunMode = false;
|
|
|
|
}
|
|
|
|
mTargetStunPeriod = sec_per_stun;
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
/* higher level interface */
|
2010-07-31 14:14:10 -04:00
|
|
|
int UdpStunner::recvPkt(void *data, int size, struct sockaddr_in &from)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
/* print packet information */
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::recvPkt(" << size << ") from: " << from;
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2008-01-25 01:11:39 -05:00
|
|
|
|
|
|
|
/* check for STUN packet */
|
2008-03-20 22:02:58 -04:00
|
|
|
if (UdpStun_isStunPacket(data, size))
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
mStunLastRecvAny = time(NULL);
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::recvPkt() is Stun Packet";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
2008-06-13 11:42:08 -04:00
|
|
|
#endif
|
2008-01-25 01:11:39 -05:00
|
|
|
|
|
|
|
/* respond */
|
2008-02-28 05:43:33 -05:00
|
|
|
locked_handleStunPkt(data, size, from);
|
2008-01-25 01:11:39 -05:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-01-25 01:11:39 -05:00
|
|
|
}
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
int UdpStunner::status(std::ostream &out)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2008-01-25 01:11:39 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
out << "UdpStunner::status() TargetStunPeriod: " << mTargetStunPeriod;
|
|
|
|
out << " SuccessRate: " << mSuccessRate;
|
|
|
|
out << std::endl;
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "UdpStunner::status()" << std::endl;
|
|
|
|
out << "UdpStunner::potentialpeers:" << std::endl;
|
|
|
|
std::list<TouStunPeer>::iterator it;
|
|
|
|
for(it = mStunList.begin(); it != mStunList.end(); it++)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "\t" << it->id << std::endl;
|
2008-01-25 01:11:39 -05:00
|
|
|
}
|
|
|
|
out << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
int UdpStunner::tick()
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::tick()" << std::endl;
|
2008-01-25 01:11:39 -05:00
|
|
|
#endif
|
2011-06-29 14:02:44 -04:00
|
|
|
|
|
|
|
if (checkStunDesired())
|
|
|
|
{
|
|
|
|
attemptStun();
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
status(std::cerr);
|
|
|
|
#endif
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************* STUN Handling ********************************/
|
|
|
|
|
|
|
|
/* respond */
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::locked_handleStunPkt(void *data, int size, struct sockaddr_in &from)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
if (size == 20) /* request */
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::handleStunPkt() got Request from: ";
|
|
|
|
std::cerr << inet_ntoa(from.sin_addr) << ":" << ntohs(from.sin_port);
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* generate a response */
|
|
|
|
int len;
|
2008-03-20 22:02:58 -04:00
|
|
|
void *pkt = UdpStun_generate_stun_reply(&from, &len);
|
2008-01-25 01:11:39 -05:00
|
|
|
if (!pkt)
|
|
|
|
return false;
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
mStunLastSendAny = now;
|
2008-01-25 01:11:39 -05:00
|
|
|
int sentlen = sendPkt(pkt, len, from, STUN_TTL);
|
|
|
|
free(pkt);
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::handleStunPkt() sent Response size:" << sentlen;
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (len == sentlen);
|
|
|
|
}
|
|
|
|
else if (size == 28)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::handleStunPkt() got Response";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* got response */
|
|
|
|
struct sockaddr_in eAddr;
|
2008-03-20 22:02:58 -04:00
|
|
|
bool good = UdpStun_response(data, size, eAddr);
|
2008-01-25 01:11:39 -05:00
|
|
|
if (good)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::handleStunPkt() got Ext Addr: ";
|
|
|
|
std::cerr << inet_ntoa(eAddr.sin_addr) << ":" << ntohs(eAddr.sin_port);
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << " from: " << from;
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-28 10:58:54 -05:00
|
|
|
locked_recvdStun(from, eAddr);
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::handleStunPkt() Bad Packet";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::externalAddr(struct sockaddr_in &external, uint8_t &stable)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
if (eaddrKnown)
|
|
|
|
{
|
2011-07-13 07:41:25 -04:00
|
|
|
/* address timeout
|
|
|
|
* no timeout if in exclusive mode
|
|
|
|
*/
|
|
|
|
if ((time(NULL) - eaddrTime > (mTargetStunPeriod * 2)) && (!mExclusiveMode))
|
2009-08-04 19:22:44 -04:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::externalAddr() eaddr expired";
|
2009-08-04 19:22:44 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
eaddrKnown = false;
|
2009-08-04 19:22:44 -04:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:24:45 -04:00
|
|
|
|
|
|
|
/* Force Restun is triggered after an Exclusive Mode... as Ext Address is likely to have changed
|
|
|
|
* Until the Restun has got an address - we act as if we don't have an external address
|
|
|
|
*/
|
|
|
|
if (mForceRestun)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
external = eaddr;
|
2008-02-28 10:58:54 -05:00
|
|
|
|
|
|
|
if (eaddrStable)
|
|
|
|
stable = 1;
|
|
|
|
else
|
|
|
|
stable = 0;
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::externalAddr() eaddr:" << inet_ntoa(external.sin_addr);
|
2009-08-04 19:22:44 -04:00
|
|
|
std::cerr << ":" << ntohs(external.sin_port) << " stable: " << (int) stable;
|
|
|
|
std::cerr << std::endl;
|
2009-10-29 20:41:24 -04:00
|
|
|
#endif
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
return true;
|
|
|
|
}
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::externalAddr() eaddr unknown";
|
2009-08-04 19:22:44 -04:00
|
|
|
std::cerr << std::endl;
|
2009-10-29 20:41:24 -04:00
|
|
|
#endif
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2008-01-25 01:11:39 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
int UdpStunner::doStun(struct sockaddr_in stun_addr)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::doStun()";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* send out a stun packet -> save in the local variable */
|
|
|
|
|
|
|
|
#define MAX_STUN_SIZE 64
|
|
|
|
char stundata[MAX_STUN_SIZE];
|
|
|
|
int tmplen = MAX_STUN_SIZE;
|
2008-03-20 22:02:58 -04:00
|
|
|
bool done = UdpStun_generate_stun_pkt(stundata, &tmplen);
|
2008-01-25 01:11:39 -05:00
|
|
|
if (!done)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::doStun() Failed";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
//pqioutput(PQL_ALERT, pqistunzone, "pqistunner::stun() Failed!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send it off */
|
2011-08-12 09:42:30 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
int sentlen =
|
|
|
|
#endif
|
|
|
|
sendPkt(stundata, tmplen, stun_addr, STUN_TTL);
|
2008-01-25 01:11:39 -05:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
{
|
2011-08-12 09:42:30 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
mStunLastSendStun = now;
|
|
|
|
mStunLastSendAny = now;
|
2010-07-31 14:14:10 -04:00
|
|
|
}
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-01-25 01:11:39 -05:00
|
|
|
std::ostringstream out;
|
2011-06-29 14:02:44 -04:00
|
|
|
out << "UdpStunner::doStun() Sent Stun Packet(" << sentlen << ") ";
|
2008-01-25 01:11:39 -05:00
|
|
|
out << " to:";
|
2010-07-31 14:14:10 -04:00
|
|
|
out << inet_ntoa(stun_addr.sin_addr) << ":" << ntohs(stun_addr.sin_port);
|
2008-01-25 01:11:39 -05:00
|
|
|
|
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
|
|
|
|
//pqioutput(PQL_ALERT, pqistunzone, out.str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-03-20 22:02:58 -04:00
|
|
|
/******************************* STUN Handling ********************************/
|
|
|
|
/***** These next functions are generic and not dependent on class variables **/
|
|
|
|
/******************************* STUN Handling ********************************/
|
|
|
|
|
|
|
|
bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
/* check what type it is */
|
|
|
|
if (size < 28)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-04 13:24:15 -05:00
|
|
|
if (htons(((uint16_t *) stun_pkt)[0]) != 0x0101)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
/* not a response */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through the packet */
|
|
|
|
/* for now assume the address follows the header directly */
|
|
|
|
/* all stay in netbyteorder! */
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = ((uint32_t *) stun_pkt)[6];
|
|
|
|
addr.sin_port = ((uint16_t *) stun_pkt)[11];
|
|
|
|
|
|
|
|
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2008-01-25 01:11:39 -05:00
|
|
|
std::ostringstream out;
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "UdpStunner::response() Recvd a Stun Response, ext_addr: ";
|
|
|
|
out << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port);
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-20 22:02:58 -04:00
|
|
|
bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
if (*len < 20)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* just the header */
|
2008-02-04 13:24:15 -05:00
|
|
|
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0001);
|
|
|
|
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(20); /* only header */
|
2008-01-25 01:11:39 -05:00
|
|
|
/* transaction id - should be random */
|
2008-02-04 13:24:15 -05:00
|
|
|
((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0020);
|
|
|
|
((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0121);
|
|
|
|
((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0111);
|
|
|
|
((uint32_t *) stun_pkt)[4] = (uint32_t) htonl(0x1010);
|
2008-01-25 01:11:39 -05:00
|
|
|
*len = 20;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-20 22:02:58 -04:00
|
|
|
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
|
|
|
/* just the header */
|
|
|
|
void *stun_pkt = malloc(28);
|
2008-02-04 13:24:15 -05:00
|
|
|
((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0101);
|
|
|
|
((uint16_t *) stun_pkt)[1] = (uint16_t) htons(28); /* only header + 8 byte addr */
|
2008-01-25 01:11:39 -05:00
|
|
|
/* transaction id - should be random */
|
2008-02-04 13:24:15 -05:00
|
|
|
((uint32_t *) stun_pkt)[1] = (uint32_t) htonl(0x0f20);
|
|
|
|
((uint32_t *) stun_pkt)[2] = (uint32_t) htonl(0x0f21);
|
|
|
|
((uint32_t *) stun_pkt)[3] = (uint32_t) htonl(0x0f11);
|
|
|
|
((uint32_t *) stun_pkt)[4] = (uint32_t) htonl(0x1010);
|
2008-01-25 01:11:39 -05:00
|
|
|
/* now add address
|
|
|
|
* 0 1 2 3
|
|
|
|
* <INET> <port>
|
|
|
|
* <inet address>
|
|
|
|
*/
|
|
|
|
|
2008-02-04 13:24:15 -05:00
|
|
|
/* THESE SHOULD BE NET ORDER ALREADY */
|
|
|
|
((uint16_t *) stun_pkt)[10] = AF_INET;
|
2008-01-25 01:11:39 -05:00
|
|
|
((uint16_t *) stun_pkt)[11] = stun_addr->sin_port;
|
2008-02-04 13:24:15 -05:00
|
|
|
((uint32_t *) stun_pkt)[6] = stun_addr->sin_addr.s_addr;
|
2008-01-25 01:11:39 -05:00
|
|
|
|
|
|
|
*len = 28;
|
|
|
|
return stun_pkt;
|
|
|
|
}
|
|
|
|
|
2008-03-20 22:02:58 -04:00
|
|
|
bool UdpStun_isStunPacket(void *data, int size)
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::isStunPacket() ?";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (size < 20)
|
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::isStunPacket() (size < 20) -> false";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* match size field */
|
2008-02-04 13:24:15 -05:00
|
|
|
uint16_t pktsize = ntohs(((uint16_t *) data)[1]);
|
2008-01-25 01:11:39 -05:00
|
|
|
if (size != pktsize)
|
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::isStunPacket() (size != pktsize) -> false";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-04 13:24:15 -05:00
|
|
|
if ((size == 20) && (0x0001 == ntohs(((uint16_t *) data)[0])))
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::isStunPacket() (size=20 & data[0]=0x0001) -> true";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* request */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-04 13:24:15 -05:00
|
|
|
if ((size == 28) && (0x0101 == ntohs(((uint16_t *) data)[0])))
|
2008-01-25 01:11:39 -05:00
|
|
|
{
|
2011-07-12 10:16:46 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER_FILTER
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "UdpStunner::isStunPacket() (size=28 & data[0]=0x0101) -> true";
|
2008-01-25 01:11:39 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* response */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
|
|
|
|
/******************************* STUN Handling ********************************
|
2011-06-29 14:02:44 -04:00
|
|
|
* KeepAlive has been replaced by a targetStunRate. Set this to zero to disable.
|
2008-02-28 05:43:33 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/******************************* STUN Handling ********************************/
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::addStunPeer(const struct sockaddr_in &remote, const char *peerid)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
|
|
|
/* add to the list */
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::addStunPeer()";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
bool toStore = true;
|
2010-07-31 14:14:10 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
/* only store if we're active */
|
|
|
|
toStore = !mPassiveStunMode;
|
|
|
|
}
|
2009-08-04 19:22:44 -04:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
if (toStore)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
storeStunPeer(remote, peerid, 0);
|
2008-02-28 05:43:33 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::storeStunPeer(const struct sockaddr_in &remote, const char *peerid, bool sent)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::storeStunPeer()";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2008-02-28 05:43:33 -05:00
|
|
|
|
|
|
|
std::list<TouStunPeer>::iterator it;
|
|
|
|
for(it = mStunList.begin(); it != mStunList.end(); it++)
|
|
|
|
{
|
|
|
|
if ((remote.sin_addr.s_addr == it->remote.sin_addr.s_addr) &&
|
|
|
|
(remote.sin_port == it->remote.sin_port))
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::storeStunPeer() Peer Already There!";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* already there */
|
2009-08-04 19:22:44 -04:00
|
|
|
if (sent)
|
|
|
|
{
|
|
|
|
it->failCount += 1;
|
|
|
|
it->lastsend = time(NULL);
|
|
|
|
}
|
2008-02-28 05:43:33 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TouStunPeer peer(std::string(peerid), remote);
|
2009-08-04 19:22:44 -04:00
|
|
|
if (sent)
|
|
|
|
{
|
|
|
|
peer.failCount += 1;
|
|
|
|
peer.lastsend = time(NULL);
|
|
|
|
}
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
mStunList.push_back(peer);
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::storeStunPeer() Added Peer";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
bool UdpStunner::checkStunDesired()
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::checkStunDesired()";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
time_t now;
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
if (mPassiveStunMode)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::checkStunDesired() In Passive Mode";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false; /* all good */
|
|
|
|
}
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
if (mExclusiveMode)
|
|
|
|
{
|
|
|
|
return false; /* no pings in exclusive mode */
|
|
|
|
}
|
2011-07-17 12:24:45 -04:00
|
|
|
|
|
|
|
if (mForceRestun)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
if (!eaddrKnown)
|
|
|
|
{
|
2011-07-13 07:41:25 -04:00
|
|
|
/* check properly! (this will limit it to two successful stuns) */
|
|
|
|
if (!locked_checkExternalAddress())
|
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-07-13 07:41:25 -04:00
|
|
|
std::cerr << "UdpStunner::checkStunDesired() YES, we don't have extAddr Yet";
|
|
|
|
std::cerr << std::endl;
|
2011-06-29 14:02:44 -04:00
|
|
|
#endif
|
2011-07-13 07:41:25 -04:00
|
|
|
return true; /* want our external address */
|
|
|
|
}
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
/* check if we need to send one now */
|
|
|
|
now = time(NULL);
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
/* based on SuccessRate & TargetStunRate, we work out if we should send one
|
|
|
|
*
|
|
|
|
* if we have 100% success rate, then we can delay until exactly TARGET RATE.
|
|
|
|
* if we have 0% success rate, then try at double TARGET RATE.
|
|
|
|
*
|
2011-07-13 07:41:25 -04:00
|
|
|
* generalised to a rate_scale parameter below...
|
2011-06-29 14:02:44 -04:00
|
|
|
*/
|
2011-07-13 07:41:25 -04:00
|
|
|
|
|
|
|
#define RATE_SCALE (3.0)
|
|
|
|
double stunPeriod = (mTargetStunPeriod / (RATE_SCALE)) * (1.0 + mSuccessRate * (RATE_SCALE - 1.0));
|
2011-06-29 14:02:44 -04:00
|
|
|
time_t nextStun = mStunLastRecvResp + (int) stunPeriod;
|
|
|
|
|
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::checkStunDesired() TargetStunPeriod: " << mTargetStunPeriod;
|
|
|
|
std::cerr << " SuccessRate: " << mSuccessRate;
|
|
|
|
std::cerr << " DesiredStunPeriod: " << stunPeriod;
|
|
|
|
std::cerr << " NextStun: " << nextStun - now << " secs";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (now >= nextStun)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::checkStunDesired() Stun is Desired";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-06-29 14:02:44 -04:00
|
|
|
return true;
|
2008-02-28 05:43:33 -05:00
|
|
|
}
|
2011-06-29 14:02:44 -04:00
|
|
|
else
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::checkStunDesired() Stun is Not Needed";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
|
|
|
|
bool UdpStunner::attemptStun()
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
TouStunPeer peer;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::attemptStun()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-28 05:43:33 -05:00
|
|
|
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2011-06-29 14:02:44 -04:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i = 0; ((i < mStunList.size()) && (mStunList.size() > 0) && (!found)); i++)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2011-06-29 14:02:44 -04:00
|
|
|
/* extract entry */
|
|
|
|
peer = mStunList.front();
|
|
|
|
mStunList.pop_front();
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
/* check if expired */
|
|
|
|
if (peer.failCount > TOU_STUN_MAX_FAIL_COUNT)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::attemptStun() Peer has expired, dropping";
|
|
|
|
std::cerr << std::endl;
|
2008-02-28 05:43:33 -05:00
|
|
|
#endif
|
2011-06-29 14:02:44 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Peer Okay, check last send time.
|
|
|
|
if (now - peer.lastsend < TOU_STUN_MAX_SEND_RATE)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::attemptStun() Peer was sent to Too Recently, pushing back";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
mStunList.push_back(peer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we have found a peer! */
|
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::attemptStun() Found Peer to Stun.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
peer.failCount++;
|
|
|
|
peer.lastsend = now;
|
|
|
|
mStunList.push_back(peer);
|
|
|
|
mSuccessRate *= TOU_SUCCESS_LPF_FACTOR;
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END OF WHILE LOOP.
|
|
|
|
|
|
|
|
if (mStunList.size() < 1)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2011-06-29 14:02:44 -04:00
|
|
|
std::cerr << "UdpStunner::attemptStun() No Peers in List. FAILED";
|
2008-02-28 05:43:33 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2011-06-29 14:02:44 -04:00
|
|
|
return false;
|
2008-02-28 05:43:33 -05:00
|
|
|
}
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-02-28 05:43:33 -05:00
|
|
|
locked_printStunList();
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
} // END OF MUTEX LOCKING.
|
2008-02-28 05:43:33 -05:00
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
doStun(peer.remote);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-02-28 05:43:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::locked_recvdStun(const struct sockaddr_in &remote, const struct sockaddr_in &extaddr)
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-02-28 10:58:54 -05:00
|
|
|
std::ostringstream out;
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "UdpStunner::locked_recvdStun() from:";
|
|
|
|
out << inet_ntoa(remote.sin_addr) << ":" << ntohs(remote.sin_port);
|
2008-02-28 10:58:54 -05:00
|
|
|
out << " claiming ExtAddr is:";
|
2010-07-31 14:14:10 -04:00
|
|
|
out << inet_ntoa(extaddr.sin_addr) << ":" << ntohs(extaddr.sin_port);
|
2008-02-28 10:58:54 -05:00
|
|
|
|
|
|
|
std::cerr << out.str() << std::endl;
|
2008-02-28 05:43:33 -05:00
|
|
|
#endif
|
|
|
|
|
2011-07-17 09:48:37 -04:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
struct sockaddr_in fakeExtaddr = extaddr;
|
|
|
|
if (mSimUnstableExt)
|
|
|
|
{
|
|
|
|
std::cerr << "UdpStunner::locked_recvdStun() TEST SIM UNSTABLE EXT: Forcing Port to be wrong to sim an ExclusiveNat";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
#define UNSTABLE_PORT_RANGE 100
|
|
|
|
|
|
|
|
fakeExtaddr.sin_port = htons(ntohs(fakeExtaddr.sin_port) - (UNSTABLE_PORT_RANGE / 2) + RSRandom::random_u32() % UNSTABLE_PORT_RANGE);
|
|
|
|
if (!mSimSymmetricNat)
|
|
|
|
{
|
|
|
|
mSimUnstableExt = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-28 10:58:54 -05:00
|
|
|
bool found = true;
|
2008-02-28 05:43:33 -05:00
|
|
|
std::list<TouStunPeer>::iterator it;
|
|
|
|
for(it = mStunList.begin(); it != mStunList.end(); it++)
|
|
|
|
{
|
|
|
|
if ((remote.sin_addr.s_addr == it->remote.sin_addr.s_addr) &&
|
|
|
|
(remote.sin_port == it->remote.sin_port))
|
|
|
|
{
|
|
|
|
it->failCount = 0;
|
2011-07-17 09:48:37 -04:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
it->eaddr = fakeExtaddr;
|
|
|
|
#else
|
2008-02-28 10:58:54 -05:00
|
|
|
it->eaddr = extaddr;
|
2011-07-17 09:48:37 -04:00
|
|
|
#endif
|
2008-02-28 10:58:54 -05:00
|
|
|
it->response = true;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
break;
|
2008-02-28 05:43:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:24:45 -04:00
|
|
|
/* We've received a Stun, so the ForceStun can be cancelled */
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
mForceRestun = false;
|
|
|
|
}
|
|
|
|
|
2011-06-29 14:02:44 -04:00
|
|
|
/* if not found.. should we add it back in? */
|
|
|
|
|
|
|
|
/* How do we calculate the success rate?
|
|
|
|
* Don't want to count all the stuns?
|
|
|
|
* Low Pass filter won't work either...
|
|
|
|
* at send...
|
|
|
|
* mSuccessRate = 0.95 * mSuccessRate.
|
|
|
|
* at recv...
|
|
|
|
* mSuccessRate = 0.95 * mSuccessRate + 0.05;
|
|
|
|
*
|
|
|
|
* But if we split into a two stage eqn. it'll work!
|
|
|
|
* a
|
|
|
|
* mSuccessRate = 0.95 * mSuccessRate.
|
|
|
|
* at recv...
|
|
|
|
* mSuccessRate += 0.05;
|
|
|
|
*/
|
|
|
|
|
|
|
|
mSuccessRate += (1.0-TOU_SUCCESS_LPF_FACTOR);
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
mStunLastRecvResp = now;
|
|
|
|
mStunLastRecvAny = now;
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-03-05 11:32:18 -05:00
|
|
|
locked_printStunList();
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-03-05 11:32:18 -05:00
|
|
|
|
2011-07-13 07:41:25 -04:00
|
|
|
if (!mExclusiveMode)
|
|
|
|
{
|
|
|
|
locked_checkExternalAddress();
|
|
|
|
}
|
2008-02-28 10:58:54 -05:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::locked_checkExternalAddress()
|
2008-02-28 10:58:54 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-02-28 10:58:54 -05:00
|
|
|
std::ostringstream out;
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "UdpStunner::locked_checkExternalAddress()";
|
2008-02-28 10:58:54 -05:00
|
|
|
std::cerr << out.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool found1 = false;
|
|
|
|
bool found2 = false;
|
2009-08-04 19:22:44 -04:00
|
|
|
time_t now = time(NULL);
|
|
|
|
/* iterator backwards - as these are the most recent */
|
|
|
|
std::list<TouStunPeer>::reverse_iterator it;
|
|
|
|
std::list<TouStunPeer>::reverse_iterator p1;
|
|
|
|
std::list<TouStunPeer>::reverse_iterator p2;
|
|
|
|
for(it = mStunList.rbegin(); it != mStunList.rend(); it++)
|
2008-02-28 10:58:54 -05:00
|
|
|
{
|
2009-08-04 19:22:44 -04:00
|
|
|
/* check:
|
|
|
|
1) have response.
|
|
|
|
2) have eaddr.
|
|
|
|
3) no fails.
|
|
|
|
4) recent age.
|
|
|
|
*/
|
|
|
|
|
|
|
|
time_t age = (now - it->lastsend);
|
2011-06-29 14:02:44 -04:00
|
|
|
if (it->response &&
|
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
( mAcceptLocalNet || isExternalNet(&(it->eaddr.sin_addr))) &&
|
|
|
|
#else
|
|
|
|
(isExternalNet(&(it->eaddr.sin_addr))) &&
|
|
|
|
#endif
|
|
|
|
(it->failCount == 0) && (age < (mTargetStunPeriod * 2)))
|
2008-02-28 10:58:54 -05:00
|
|
|
{
|
|
|
|
if (!found1)
|
|
|
|
{
|
|
|
|
p1 = it;
|
|
|
|
found1 = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p2 = it;
|
|
|
|
found2 = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found1 && found2)
|
|
|
|
{
|
|
|
|
if ((p1->eaddr.sin_addr.s_addr == p2->eaddr.sin_addr.s_addr) &&
|
|
|
|
(p1->eaddr.sin_port == p2->eaddr.sin_port))
|
|
|
|
{
|
|
|
|
eaddrStable = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eaddrStable = false;
|
|
|
|
}
|
|
|
|
eaddrKnown = true;
|
|
|
|
eaddr = p1->eaddr;
|
2009-08-04 19:22:44 -04:00
|
|
|
eaddrTime = now;
|
2008-02-28 10:58:54 -05:00
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
|
|
|
std::cerr << "UdpStunner::locked_checkExternalAddress() Found State:";
|
2008-02-28 10:58:54 -05:00
|
|
|
if (eaddrStable)
|
|
|
|
std::cerr << " Stable NAT translation (GOOD!) ";
|
|
|
|
else
|
|
|
|
std::cerr << " unStable (symmetric NAT translation (BAD!) ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-02-28 10:58:54 -05:00
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::locked_printStunList()
|
2008-02-28 05:43:33 -05:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
#ifdef DEBUG_UDP_STUNNER
|
2008-02-28 05:43:33 -05:00
|
|
|
std::ostringstream out;
|
|
|
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
out << "locked_printStunList()" << std::endl;
|
2011-06-29 14:02:44 -04:00
|
|
|
out << "\tLastSendStun: " << now - mStunLastSendStun << std::endl;
|
|
|
|
out << "\tLastSendAny: " << now - mStunLastSendAny << std::endl;
|
|
|
|
out << "\tLastRecvResp: " << now - mStunLastRecvResp << std::endl;
|
|
|
|
out << "\tLastRecvAny: " << now - mStunLastRecvAny << std::endl;
|
2008-02-28 05:43:33 -05:00
|
|
|
|
|
|
|
std::list<TouStunPeer>::iterator it;
|
|
|
|
for(it = mStunList.begin(); it != mStunList.end(); it++)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
out << "id:" << RsUtil::BinToHex(it->id) << " addr: " << inet_ntoa(it->remote.sin_addr);
|
2008-02-28 05:43:33 -05:00
|
|
|
out << ":" << htons(it->remote.sin_port);
|
2010-07-31 14:14:10 -04:00
|
|
|
out << " eaddr: " << inet_ntoa(it->eaddr.sin_addr);
|
2008-03-03 09:01:52 -05:00
|
|
|
out << ":" << htons(it->eaddr.sin_port);
|
2008-02-28 05:43:33 -05:00
|
|
|
out << " failCount: " << it->failCount;
|
|
|
|
out << " lastSend: " << now - it->lastsend;
|
|
|
|
out << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << out.str();
|
2008-07-09 05:55:09 -04:00
|
|
|
#endif
|
2008-02-28 05:43:33 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::getStunPeer(int idx, std::string &id,
|
2009-08-04 19:22:44 -04:00
|
|
|
struct sockaddr_in &remote, struct sockaddr_in &eaddr,
|
|
|
|
uint32_t &failCount, time_t &lastSend)
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2009-08-04 19:22:44 -04:00
|
|
|
|
|
|
|
std::list<TouStunPeer>::iterator it;
|
|
|
|
int i;
|
2011-06-29 14:02:44 -04:00
|
|
|
for(i=0, it=mStunList.begin(); (i<idx) && (it!=mStunList.end()); it++, i++) ;
|
2009-08-04 19:22:44 -04:00
|
|
|
|
|
|
|
if (it != mStunList.end())
|
|
|
|
{
|
|
|
|
id = RsUtil::BinToHex(it->id);
|
|
|
|
remote = it->remote;
|
|
|
|
eaddr = it->eaddr;
|
|
|
|
failCount = it->failCount;
|
|
|
|
lastSend = it->lastsend;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
bool UdpStunner::needStunPeers()
|
2009-08-04 19:22:44 -04:00
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
RsStackMutex stack(stunMtx); /********** LOCK MUTEX *********/
|
2009-08-04 19:22:44 -04:00
|
|
|
|
|
|
|
return (mStunList.size() < TOU_STUN_MIN_PEERS);
|
|
|
|
}
|
|
|
|
|
2008-02-28 05:43:33 -05:00
|
|
|
|