2010-07-31 18:14:10 +00:00
|
|
|
#ifndef RS_UDP_STUN_H
|
|
|
|
#define RS_UDP_STUN_H
|
2008-01-25 06:11:39 +00:00
|
|
|
/*
|
2010-07-31 18:14:10 +00:00
|
|
|
* tcponudp/udpstunner.h
|
2008-01-25 06:11:39 +00:00
|
|
|
*
|
2010-07-31 18:14:10 +00:00
|
|
|
* libretroshare.
|
2008-01-25 06:11:39 +00:00
|
|
|
*
|
2010-07-31 18:14:10 +00:00
|
|
|
* Copyright 2010 by Robert Fernie
|
2008-01-25 06:11:39 +00: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 18:14:10 +00:00
|
|
|
* License Version 3 as published by the Free Software Foundation.
|
2008-01-25 06:11:39 +00: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-06-29 18:02:44 +00:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include "util/rswin.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WINDOWS_SYS
|
2010-07-31 18:14:10 +00:00
|
|
|
#include <netinet/in.h>
|
2011-06-29 18:02:44 +00:00
|
|
|
#endif
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2010-07-31 18:14:10 +00:00
|
|
|
#include "tcponudp/rsudpstack.h"
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
#include <string>
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2010-07-31 18:14:10 +00:00
|
|
|
/* UdpStun.
|
|
|
|
* Stuns peers to determine external addresses.
|
2008-01-25 06:11:39 +00:00
|
|
|
*/
|
|
|
|
|
2008-02-28 10:43:33 +00:00
|
|
|
class TouStunPeer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TouStunPeer()
|
2009-08-04 23:22:44 +00:00
|
|
|
:response(false), lastsend(0), failCount(0)
|
|
|
|
{
|
|
|
|
eaddr.sin_addr.s_addr = 0;
|
|
|
|
eaddr.sin_port = 0;
|
|
|
|
return;
|
|
|
|
}
|
2008-02-28 10:43:33 +00:00
|
|
|
|
|
|
|
TouStunPeer(std::string id_in, const struct sockaddr_in &addr)
|
2009-08-04 23:22:44 +00:00
|
|
|
:id(id_in), remote(addr), response(false), lastsend(0), failCount(0)
|
|
|
|
{
|
|
|
|
eaddr.sin_addr.s_addr = 0;
|
|
|
|
eaddr.sin_port = 0;
|
|
|
|
return;
|
|
|
|
}
|
2008-02-28 10:43:33 +00:00
|
|
|
|
|
|
|
std::string id;
|
2008-02-28 15:58:54 +00:00
|
|
|
struct sockaddr_in remote, eaddr;
|
|
|
|
bool response;
|
2008-02-28 10:43:33 +00:00
|
|
|
time_t lastsend;
|
|
|
|
uint32_t failCount;
|
|
|
|
};
|
2011-06-29 18:02:44 +00:00
|
|
|
|
|
|
|
/*
|
2011-07-14 14:56:33 +00:00
|
|
|
* FOR TESTING ONLY.
|
2011-06-29 18:02:44 +00:00
|
|
|
* #define UDPSTUN_ALLOW_LOCALNET 1
|
|
|
|
*/
|
2008-02-28 10:43:33 +00:00
|
|
|
|
2011-07-14 14:56:33 +00:00
|
|
|
#define UDPSTUN_ALLOW_LOCALNET 1
|
|
|
|
|
2010-07-31 18:14:10 +00:00
|
|
|
class UdpStunner: public UdpSubReceiver
|
2008-01-25 06:11:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-07-31 18:14:10 +00:00
|
|
|
UdpStunner(UdpPublisher *pub);
|
|
|
|
virtual ~UdpStunner() { return; }
|
2008-02-28 10:43:33 +00:00
|
|
|
|
2011-06-29 18:02:44 +00:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
// For Local Testing Mode.
|
|
|
|
void SetAcceptLocalNet();
|
|
|
|
#endif
|
|
|
|
|
2011-07-13 11:41:25 +00:00
|
|
|
int setExclusiveMode(); /* returns seconds since last send/recv */
|
|
|
|
int cancelExclusiveMode();
|
|
|
|
|
|
|
|
|
2011-07-12 14:16:46 +00:00
|
|
|
void setTargetStunPeriod(int32_t sec_per_stun);
|
2008-01-25 06:11:39 +00:00
|
|
|
bool addStunPeer(const struct sockaddr_in &remote, const char *peerid);
|
2009-08-04 23:22:44 +00:00
|
|
|
bool getStunPeer(int idx, std::string &id,
|
|
|
|
struct sockaddr_in &remote, struct sockaddr_in &eaddr,
|
|
|
|
uint32_t &failCount, time_t &lastSend);
|
|
|
|
|
|
|
|
bool needStunPeers();
|
2008-02-28 10:43:33 +00:00
|
|
|
|
2008-02-28 15:58:54 +00:00
|
|
|
bool externalAddr(struct sockaddr_in &remote, uint8_t &stable);
|
2008-01-25 06:11:39 +00:00
|
|
|
|
|
|
|
/* Packet IO */
|
2010-07-31 18:14:10 +00:00
|
|
|
virtual int recvPkt(void *data, int size, struct sockaddr_in &from);
|
|
|
|
virtual int status(std::ostream &out);
|
2008-01-25 06:11:39 +00:00
|
|
|
|
|
|
|
/* monitoring / updates */
|
|
|
|
int tick();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-06-29 18:02:44 +00:00
|
|
|
bool checkStunDesired();
|
|
|
|
bool attemptStun();
|
2008-01-25 06:11:39 +00:00
|
|
|
|
|
|
|
int doStun(struct sockaddr_in stun_addr);
|
2011-06-29 18:02:44 +00:00
|
|
|
bool storeStunPeer(const struct sockaddr_in &remote, const char *peerid, bool sent);
|
|
|
|
|
|
|
|
|
|
|
|
/* STUN handling */
|
|
|
|
bool locked_handleStunPkt(void *data, int size, struct sockaddr_in &from);
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2008-02-28 10:43:33 +00:00
|
|
|
bool locked_printStunList();
|
2008-02-28 15:58:54 +00:00
|
|
|
bool locked_recvdStun(const struct sockaddr_in &remote, const struct sockaddr_in &extaddr);
|
|
|
|
bool locked_checkExternalAddress();
|
|
|
|
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2010-07-31 18:14:10 +00:00
|
|
|
RsMutex stunMtx; /* for all class data (below) */
|
2008-02-28 15:58:54 +00:00
|
|
|
|
2008-01-25 06:11:39 +00:00
|
|
|
struct sockaddr_in eaddr; /* external addr */
|
2010-07-31 18:14:10 +00:00
|
|
|
|
2008-01-25 06:11:39 +00:00
|
|
|
bool eaddrKnown;
|
2008-02-28 15:58:54 +00:00
|
|
|
bool eaddrStable; /* if true then usable. if false -> Symmettric NAT */
|
2009-08-04 23:22:44 +00:00
|
|
|
time_t eaddrTime;
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2011-06-29 18:02:44 +00:00
|
|
|
time_t mStunLastRecvResp;
|
|
|
|
time_t mStunLastRecvAny;
|
|
|
|
time_t mStunLastSendStun;
|
|
|
|
time_t mStunLastSendAny;
|
2008-02-28 10:43:33 +00:00
|
|
|
|
|
|
|
std::list<TouStunPeer> mStunList; /* potentials */
|
2008-01-25 06:11:39 +00:00
|
|
|
|
2011-06-29 18:02:44 +00:00
|
|
|
#ifdef UDPSTUN_ALLOW_LOCALNET
|
|
|
|
// For Local Testing Mode.
|
|
|
|
bool mAcceptLocalNet;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool mPassiveStunMode;
|
|
|
|
uint32_t mTargetStunPeriod;
|
|
|
|
double mSuccessRate;
|
|
|
|
|
2011-07-13 11:41:25 +00:00
|
|
|
bool mExclusiveMode; /* when this is switched on, the stunner stays silent (and extAddr is maintained) */
|
|
|
|
time_t mExclusiveModeTS;
|
2008-01-25 06:11:39 +00:00
|
|
|
};
|
|
|
|
|
2008-03-21 02:02:58 +00:00
|
|
|
/* generic stun functions */
|
|
|
|
|
|
|
|
bool UdpStun_isStunPacket(void *data, int size);
|
|
|
|
bool UdpStun_response(void *stun_pkt, int size, struct sockaddr_in &addr);
|
|
|
|
void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len);
|
|
|
|
bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len);
|
|
|
|
|
2008-01-25 06:11:39 +00:00
|
|
|
#endif
|