mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
Remove miniupnp and add libupnp code
Conflicts: libretroshare/src/libretroshare.pro git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1744 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
b6c2b6897a
commit
4fe1bd8801
@ -1,24 +1,15 @@
|
||||
TEMPLATE = lib
|
||||
CONFIG += static
|
||||
CONFIG += static release
|
||||
TARGET = retroshare
|
||||
CONFIG += release
|
||||
|
||||
DEFINES *= MINIUPNPC_VERSION=13
|
||||
DEFINES -= PQI_USE_XPGP
|
||||
DEFINES += RS_USE_PGPSSL
|
||||
|
||||
UPNPC_DIR = ../../../../miniupnpc-1.3
|
||||
GPG_ERROR_DIR = ../../../../libgpg-error-1.7
|
||||
GPGME_DIR = ../../../../gpgme-1.1.8
|
||||
|
||||
profiling {
|
||||
QMAKE_CXXFLAGS -= -fomit-frame-pointer
|
||||
QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer
|
||||
}
|
||||
|
||||
DEFINES -= PQI_USE_XPGP
|
||||
DEFINES *= RS_USE_PGPSSL
|
||||
|
||||
################################# Linux ##########################################
|
||||
|
||||
debug {
|
||||
@ -32,23 +23,31 @@ debug {
|
||||
|
||||
linux-g++ {
|
||||
OBJECTS_DIR = temp/linux-g++/obj
|
||||
DESTDIR = lib.linux-g++
|
||||
DESTDIR = lib
|
||||
QMAKE_CXXFLAGS *= -Wall
|
||||
QMAKE_CC = g++
|
||||
|
||||
SSL_DIR = /usr/include/openssl
|
||||
UPNPC_DIR = ../../../../miniupnpc-1.3
|
||||
GPG_ERROR_DIR = ../../../../libgpg-error-1.7
|
||||
GPGME_DIR = ../../../../gpgme-1.1.8
|
||||
UPNP_DIR = /usr/include/upnp
|
||||
HEADERS += usr/include/gpg-error.h
|
||||
HEADERS += usr/include/gpgme.h
|
||||
INCLUDEPATH += . $${SSL_DIR} $${UPNP_DIR}
|
||||
|
||||
CONFIG += version_detail_bash_script
|
||||
}
|
||||
|
||||
linux-g++-64 {
|
||||
OBJECTS_DIR = temp/linux-g++-64/obj
|
||||
DESTDIR = lib.linux-g++-64
|
||||
DESTDIR = lib
|
||||
QMAKE_CXXFLAGS *= -Wall
|
||||
QMAKE_CC = g++
|
||||
|
||||
SSL_DIR = /usr/include/openssl
|
||||
UPNP_DIR = /usr/include/upnp
|
||||
HEADERS += usr/include/gpg-error.h
|
||||
HEADERS += usr/include/gpgme.h
|
||||
INCLUDEPATH += . $${SSL_DIR} $${UPNP_DIR}
|
||||
|
||||
CONFIG += version_detail_bash_script
|
||||
}
|
||||
|
||||
@ -98,8 +97,6 @@ win32 {
|
||||
}
|
||||
################################### COMMON stuff ##################################
|
||||
|
||||
INCLUDEPATH += . $${SSL_DIR} $${UPNPC_DIR} $${GPGME_DIR}/src $${GPG_ERROR_DIR}/src
|
||||
|
||||
#DEPENDPATH += . \
|
||||
# util \
|
||||
# tcponudp \
|
||||
@ -252,7 +249,7 @@ HEADERS += dbase/cachestrapper.h \
|
||||
tcponudp/udplayer.h \
|
||||
tcponudp/udpsorter.h \
|
||||
upnp/upnphandler.h \
|
||||
upnp/upnputil.h \
|
||||
upnp/UPnPBase.h \
|
||||
util/rsdebug.h \
|
||||
util/rsdir.h \
|
||||
util/rsnet.h \
|
||||
@ -287,9 +284,9 @@ SOURCES = \
|
||||
ft/ftdata.cc \
|
||||
ft/ftfileprovider.cc \
|
||||
ft/ftdwlqueue.cc \
|
||||
upnp/upnputil.c \
|
||||
dht/opendhtmgr.cc \
|
||||
upnp/upnphandler.cc \
|
||||
upnp/UPnPBase.cpp \
|
||||
dht/opendht.cc \
|
||||
dht/opendhtstr.cc \
|
||||
dht/b64.c \
|
||||
|
1651
libretroshare/src/upnp/UPnPBase.cpp
Normal file
1651
libretroshare/src/upnp/UPnPBase.cpp
Normal file
File diff suppressed because it is too large
Load Diff
632
libretroshare/src/upnp/UPnPBase.h
Normal file
632
libretroshare/src/upnp/UPnPBase.h
Normal file
@ -0,0 +1,632 @@
|
||||
//
|
||||
// This file is part of the aMule Project.
|
||||
//
|
||||
// Copyright (c) 2004-2009 Marcelo Roberto Jimenez ( phoenix@amule.org )
|
||||
// Copyright (c) 2006-2009 aMule Team ( admin@amule.org / http://www.amule.org )
|
||||
//
|
||||
// Any parts of this program derived from the xMule, lMule or eMule project,
|
||||
// or contributed by third-party developers are copyrighted by their
|
||||
// respective authors.
|
||||
//
|
||||
// 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
//
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "upnp.h"
|
||||
#include "upnptools.h"
|
||||
#include "upnpdebug.h"
|
||||
#include "util/rsthreads.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <util/rsthreads.h>
|
||||
|
||||
#ifdef UPNP_C
|
||||
std::string stdEmptyString;
|
||||
#else // UPNP_C
|
||||
extern std::string stdEmptyString;
|
||||
#endif // UPNP_C
|
||||
|
||||
|
||||
/**
|
||||
* Case insensitive std::string comparison
|
||||
*/
|
||||
bool stdStringIsEqualCI(
|
||||
const std::string &s1,
|
||||
const std::string &s2);
|
||||
|
||||
|
||||
class CUPnPPortMapping
|
||||
{
|
||||
private:
|
||||
std::string m_ex_port;
|
||||
std::string m_in_port;
|
||||
std::string m_protocol;
|
||||
std::string m_enabled;
|
||||
std::string m_description;
|
||||
std::string m_key;
|
||||
|
||||
public:
|
||||
CUPnPPortMapping(
|
||||
int in_port = 0,
|
||||
int ex_port = 0,
|
||||
const std::string &protocol = stdEmptyString,
|
||||
bool enabled = false,
|
||||
const std::string &description = stdEmptyString);
|
||||
~CUPnPPortMapping() {}
|
||||
|
||||
const std::string &getExPort() const
|
||||
{ return m_ex_port; }
|
||||
const std::string &getInPort() const
|
||||
{ return m_in_port; }
|
||||
const std::string &getProtocol() const
|
||||
{ return m_protocol; }
|
||||
const std::string &getEnabled() const
|
||||
{ return m_enabled; }
|
||||
const std::string &getDescription() const
|
||||
{ return m_description; }
|
||||
const std::string &getKey() const
|
||||
{ return m_key; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPControlPoint;
|
||||
|
||||
|
||||
class CUPnPLib
|
||||
{
|
||||
public:
|
||||
static const std::string &UPNP_ROOT_DEVICE;
|
||||
static const std::string &UPNP_DEVICE_IGW;
|
||||
static const std::string &UPNP_DEVICE_WAN;
|
||||
static const std::string &UPNP_DEVICE_WAN_CONNECTION;
|
||||
static const std::string &UPNP_DEVICE_LAN;
|
||||
static const std::string &UPNP_SERVICE_LAYER3_FORWARDING;
|
||||
static const std::string &UPNP_SERVICE_WAN_COMMON_INTERFACE_CONFIG;
|
||||
static const std::string &UPNP_SERVICE_WAN_IP_CONNECTION;
|
||||
static const std::string &UPNP_SERVICE_WAN_PPP_CONNECTION;
|
||||
CUPnPControlPoint &m_ctrlPoint;
|
||||
|
||||
public:
|
||||
CUPnPLib(CUPnPControlPoint &ctrlPoint);
|
||||
~CUPnPLib() {}
|
||||
|
||||
// Convenience function so we don't have to write explicit calls
|
||||
// to char2unicode every time
|
||||
std::string GetUPnPErrorMessage(int code) const;
|
||||
|
||||
// Convenience function to avoid repetitive processing of error
|
||||
// messages
|
||||
std::string processUPnPErrorMessage(
|
||||
const std::string &messsage,
|
||||
int code,
|
||||
const DOMString errorString,
|
||||
IXML_Document *doc) const;
|
||||
|
||||
// Processing response to actions
|
||||
void ProcessActionResponse(
|
||||
IXML_Document *RespDoc,
|
||||
const std::string &actionName) const;
|
||||
|
||||
// IXML_Element
|
||||
IXML_Element *Element_GetRootElement(
|
||||
IXML_Document *doc) const;
|
||||
IXML_Element *Element_GetFirstChild(
|
||||
IXML_Element *parent) const;
|
||||
IXML_Element *Element_GetNextSibling(
|
||||
IXML_Element *child) const;
|
||||
const DOMString Element_GetTag(
|
||||
IXML_Element *element) const;
|
||||
const std::string Element_GetTextValue(
|
||||
IXML_Element *element) const;
|
||||
const std::string Element_GetChildValueByTag(
|
||||
IXML_Element *element,
|
||||
const DOMString tag) const;
|
||||
IXML_Element *Element_GetFirstChildByTag(
|
||||
IXML_Element *element,
|
||||
const DOMString tag) const;
|
||||
IXML_Element *Element_GetNextSiblingByTag(
|
||||
IXML_Element *element,
|
||||
const DOMString tag) const;
|
||||
const std::string Element_GetAttributeByTag(
|
||||
IXML_Element *element,
|
||||
const DOMString tag) const;
|
||||
};
|
||||
|
||||
|
||||
class CUPnPControlPoint;
|
||||
|
||||
/*
|
||||
* Even though we can retrieve the upnpLib handler from the upnpControlPoint,
|
||||
* we must pass it separetly at this point, because the class CUPnPControlPoint
|
||||
* must be declared after.
|
||||
*
|
||||
* CUPnPLib can only be removed from the constructor once we agree to link to
|
||||
* UPnPLib explicitly, making this dlopen() stuff unnecessary.
|
||||
*/
|
||||
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
|
||||
class CXML_List : public std::map<const std::string, T *>
|
||||
{
|
||||
public:
|
||||
CXML_List(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *parent,
|
||||
const std::string &url);
|
||||
~CXML_List();
|
||||
};
|
||||
|
||||
|
||||
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
|
||||
CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::CXML_List(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *parent,
|
||||
const std::string &url)
|
||||
{
|
||||
IXML_Element *elementList =
|
||||
upnpLib.Element_GetFirstChildByTag(parent, XML_LIST_NAME);
|
||||
unsigned int i = 0;
|
||||
for ( IXML_Element *element = upnpLib.Element_GetFirstChildByTag(elementList, XML_ELEMENT_NAME);
|
||||
element;
|
||||
element = upnpLib.Element_GetNextSiblingByTag(element, XML_ELEMENT_NAME)) {
|
||||
// Add a new element to the element list
|
||||
T *upnpElement = new T(upnpControlPoint, upnpLib, element, url);
|
||||
(*this)[upnpElement->GetKey()] = upnpElement;
|
||||
++i;
|
||||
}
|
||||
std::ostringstream msg;
|
||||
msg << "\n " << XML_LIST_NAME << ": " <<
|
||||
i << " " << XML_ELEMENT_NAME << "s.";
|
||||
std::cerr << msg;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, char const *XML_ELEMENT_NAME, char const *XML_LIST_NAME>
|
||||
CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::~CXML_List()
|
||||
{
|
||||
typename CXML_List<T, XML_ELEMENT_NAME, XML_LIST_NAME>::iterator it;
|
||||
for(it = this->begin(); it != this->end(); ++it) {
|
||||
delete (*it).second;
|
||||
}
|
||||
}
|
||||
|
||||
extern const char s_argument[];
|
||||
extern const char s_argumentList[];
|
||||
extern const char s_action[];
|
||||
extern const char s_actionList[];
|
||||
extern const char s_allowedValue[];
|
||||
extern const char s_allowedValueList[];
|
||||
extern const char s_stateVariable[];
|
||||
extern const char s_serviceStateTable[];
|
||||
extern const char s_service[];
|
||||
extern const char s_serviceList[];
|
||||
extern const char s_device[];
|
||||
extern const char s_deviceList[];
|
||||
|
||||
#ifdef UPNP_C
|
||||
const char s_argument[] = "argument";
|
||||
const char s_argumentList[] = "argumentList";
|
||||
const char s_action[] = "action";
|
||||
const char s_actionList[] = "actionList";
|
||||
const char s_allowedValue[] = "allowedValue";
|
||||
const char s_allowedValueList[] = "allowedValueList";
|
||||
const char s_stateVariable[] = "stateVariable";
|
||||
const char s_serviceStateTable[] = "serviceStateTable";
|
||||
const char s_service[] = "service";
|
||||
const char s_serviceList[] = "serviceList";
|
||||
const char s_device[] = "device";
|
||||
const char s_deviceList[] = "deviceList";
|
||||
#endif // UPNP_C
|
||||
|
||||
|
||||
class CUPnPArgument;
|
||||
typedef CXML_List<CUPnPArgument, s_argument, s_argumentList> ArgumentList;
|
||||
class CUPnPAction;
|
||||
typedef CXML_List<CUPnPAction, s_action, s_actionList> ActionList;
|
||||
class CUPnPStateVariable;
|
||||
typedef CXML_List<CUPnPStateVariable, s_stateVariable, s_serviceStateTable> ServiceStateTable;
|
||||
class CUPnPAllowedValue;
|
||||
typedef CXML_List<CUPnPAllowedValue, s_allowedValue, s_allowedValueList> AllowedValueList;
|
||||
class CUPnPService;
|
||||
typedef CXML_List<CUPnPService, s_service, s_serviceList> ServiceList;
|
||||
class CUPnPDevice;
|
||||
typedef CXML_List<CUPnPDevice, s_device, s_deviceList> DeviceList;
|
||||
|
||||
|
||||
class CUPnPError
|
||||
{
|
||||
private:
|
||||
IXML_Element *m_root;
|
||||
const std::string m_ErrorCode;
|
||||
const std::string m_ErrorDescription;
|
||||
public:
|
||||
CUPnPError(
|
||||
const CUPnPLib &upnpLib,
|
||||
IXML_Document *errorDoc);
|
||||
~CUPnPError() {}
|
||||
const std::string &getErrorCode() const
|
||||
{ return m_ErrorCode; }
|
||||
const std::string &getErrorDescription() const
|
||||
{ return m_ErrorDescription; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPArgument
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
const std::string m_name;
|
||||
const std::string m_direction;
|
||||
bool m_retval;
|
||||
const std::string m_relatedStateVariable;
|
||||
|
||||
public:
|
||||
CUPnPArgument(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *argument,
|
||||
const std::string &SCPDURL);
|
||||
~CUPnPArgument() {}
|
||||
const std::string &GetName() const
|
||||
{ return m_name; }
|
||||
const std::string &GetDirection() const
|
||||
{ return m_direction; }
|
||||
bool GetRetVal() const
|
||||
{ return m_retval; }
|
||||
const std::string &GetRelatedStateVariable() const
|
||||
{ return m_relatedStateVariable; }
|
||||
const std::string &GetKey() const
|
||||
{ return m_name; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
class CUPnPAction
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
ArgumentList m_ArgumentList;
|
||||
const std::string m_name;
|
||||
|
||||
public:
|
||||
CUPnPAction(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *action,
|
||||
const std::string &SCPDURL);
|
||||
~CUPnPAction() {}
|
||||
const std::string &GetName() const
|
||||
{ return m_name; }
|
||||
const std::string &GetKey() const
|
||||
{ return m_name; }
|
||||
const ArgumentList &GetArgumentList() const
|
||||
{ return m_ArgumentList; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPAllowedValue
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
const std::string m_allowedValue;
|
||||
|
||||
public:
|
||||
CUPnPAllowedValue(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *allowedValue,
|
||||
const std::string &SCPDURL);
|
||||
~CUPnPAllowedValue() {}
|
||||
const std::string &GetAllowedValue() const
|
||||
{ return m_allowedValue; }
|
||||
const std::string &GetKey() const
|
||||
{ return m_allowedValue; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPStateVariable
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
AllowedValueList m_AllowedValueList;
|
||||
const std::string m_name;
|
||||
const std::string m_dataType;
|
||||
const std::string m_defaultValue;
|
||||
const std::string m_sendEvents;
|
||||
|
||||
public:
|
||||
CUPnPStateVariable(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *stateVariable,
|
||||
const std::string &URLBase);
|
||||
~CUPnPStateVariable() {}
|
||||
const std::string &GetNname() const
|
||||
{ return m_name; }
|
||||
const std::string &GetDataType() const
|
||||
{ return m_dataType; }
|
||||
const std::string &GetDefaultValue() const
|
||||
{ return m_defaultValue; }
|
||||
const std::string &GetKey() const
|
||||
{ return m_name; }
|
||||
const AllowedValueList &GetAllowedValueList() const
|
||||
{ return m_AllowedValueList; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPSCPD
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
ActionList m_ActionList;
|
||||
ServiceStateTable m_ServiceStateTable;
|
||||
const std::string m_SCPDURL;
|
||||
|
||||
public:
|
||||
CUPnPSCPD(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *scpd,
|
||||
const std::string &SCPDURL);
|
||||
~CUPnPSCPD() {}
|
||||
const ActionList &GetActionList() const
|
||||
{ return m_ActionList; }
|
||||
const ServiceStateTable &GetServiceStateTable() const
|
||||
{ return m_ServiceStateTable; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPArgumentValue
|
||||
{
|
||||
private:
|
||||
std::string m_argument;
|
||||
std::string m_value;
|
||||
|
||||
public:
|
||||
CUPnPArgumentValue();
|
||||
CUPnPArgumentValue(const std::string &argument, const std::string &value);
|
||||
~CUPnPArgumentValue() {}
|
||||
|
||||
const std::string &GetArgument() const { return m_argument; }
|
||||
const std::string &GetValue() const { return m_value; }
|
||||
const std::string &SetArgument(const std::string& argument) { return m_argument = argument; }
|
||||
const std::string &SetValue(const std::string &value) { return m_value = value; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPService
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
CUPnPLib &m_upnpLib;
|
||||
const std::string m_serviceType;
|
||||
const std::string m_serviceId;
|
||||
const std::string m_SCPDURL;
|
||||
const std::string m_controlURL;
|
||||
const std::string m_eventSubURL;
|
||||
std::string m_absSCPDURL;
|
||||
std::string m_absControlURL;
|
||||
std::string m_absEventSubURL;
|
||||
int m_timeout;
|
||||
Upnp_SID m_SID;
|
||||
std::auto_ptr<CUPnPSCPD> m_SCPD;
|
||||
|
||||
public:
|
||||
CUPnPService(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *service,
|
||||
const std::string &URLBase);
|
||||
~CUPnPService();
|
||||
|
||||
const std::string &GetServiceType() const
|
||||
{ return m_serviceType; }
|
||||
const std::string &GetServiceId() const
|
||||
{ return m_serviceId; }
|
||||
const std::string &GetSCPDURL() const
|
||||
{ return m_SCPDURL; }
|
||||
const std::string &GetAbsSCPDURL() const
|
||||
{ return m_absSCPDURL; }
|
||||
const std::string &GetControlURL() const
|
||||
{ return m_controlURL; }
|
||||
const std::string &GetEventSubURL() const
|
||||
{ return m_eventSubURL; }
|
||||
const std::string &GetAbsControlURL() const
|
||||
{ return m_absControlURL; }
|
||||
const std::string &GetAbsEventSubURL() const
|
||||
{ return m_absEventSubURL; }
|
||||
int GetTimeout() const
|
||||
{ return m_timeout; }
|
||||
void SetTimeout(int t)
|
||||
{ m_timeout = t; }
|
||||
int *GetTimeoutAddr()
|
||||
{ return &m_timeout; }
|
||||
char *GetSID()
|
||||
{ return m_SID; }
|
||||
void SetSID(const char *s)
|
||||
{ memcpy(m_SID, s, sizeof(Upnp_SID)); }
|
||||
const std::string &GetKey() const
|
||||
{ return m_serviceId; }
|
||||
bool IsSubscribed() const
|
||||
{ return m_SCPD.get() != NULL; }
|
||||
void SetSCPD(CUPnPSCPD *SCPD)
|
||||
{ m_SCPD.reset(SCPD); }
|
||||
|
||||
bool Execute(
|
||||
const std::string &ActionName,
|
||||
const std::vector<CUPnPArgumentValue> &ArgValue) const;
|
||||
const std::string GetStateVariable(
|
||||
const std::string &stateVariableName);
|
||||
|
||||
//callback Function
|
||||
static int GetServiceVarStatusAsyncCallback(
|
||||
Upnp_EventType EventType,
|
||||
void* Event,
|
||||
void* Cookie);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class CUPnPDevice
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
|
||||
// Please, lock these lists before use
|
||||
DeviceList m_DeviceList;
|
||||
ServiceList m_ServiceList;
|
||||
|
||||
const std::string m_deviceType;
|
||||
const std::string m_friendlyName;
|
||||
const std::string m_manufacturer;
|
||||
const std::string m_manufacturerURL;
|
||||
const std::string m_modelDescription;
|
||||
const std::string m_modelName;
|
||||
const std::string m_modelNumber;
|
||||
const std::string m_modelURL;
|
||||
const std::string m_serialNumber;
|
||||
const std::string m_UDN;
|
||||
const std::string m_UPC;
|
||||
std::string m_presentationURL;
|
||||
|
||||
public:
|
||||
CUPnPDevice(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *device,
|
||||
const std::string &URLBase);
|
||||
~CUPnPDevice() {}
|
||||
|
||||
const std::string &GetUDN() const
|
||||
{ return m_UDN; }
|
||||
const std::string &GetDeviceType() const
|
||||
{ return m_deviceType; }
|
||||
const std::string &GetFriendlyName() const
|
||||
{ return m_friendlyName; }
|
||||
const std::string &GetPresentationURL() const
|
||||
{ return m_presentationURL; }
|
||||
const std::string &GetKey() const
|
||||
{ return m_UDN; }
|
||||
};
|
||||
|
||||
|
||||
class CUPnPRootDevice : public CUPnPDevice
|
||||
{
|
||||
private:
|
||||
const CUPnPControlPoint &m_UPnPControlPoint;
|
||||
const std::string m_URLBase;
|
||||
const std::string m_location;
|
||||
int m_expires;
|
||||
|
||||
public:
|
||||
CUPnPRootDevice(
|
||||
const CUPnPControlPoint &upnpControlPoint,
|
||||
CUPnPLib &upnpLib,
|
||||
IXML_Element *rootDevice,
|
||||
const std::string &OriginalURLBase,
|
||||
const std::string &FixedURLBase,
|
||||
const char *location,
|
||||
int expires);
|
||||
~CUPnPRootDevice() {}
|
||||
|
||||
const std::string &GetURLBase() const
|
||||
{ return m_URLBase; }
|
||||
const std::string &GetLocation() const
|
||||
{ return m_location; }
|
||||
int GetExpires() const
|
||||
{ return m_expires; }
|
||||
void SetExpires(int expires)
|
||||
{ m_expires = expires; }
|
||||
};
|
||||
|
||||
|
||||
typedef std::map<const std::string, CUPnPRootDevice *> RootDeviceMap;
|
||||
typedef std::map<const std::string, CUPnPService *> ServiceMap;
|
||||
typedef std::map<const std::string, CUPnPPortMapping> PortMappingMap;
|
||||
|
||||
|
||||
class CUPnPControlPoint
|
||||
{
|
||||
private:
|
||||
// upnp stuff
|
||||
CUPnPLib m_upnpLib;
|
||||
UpnpClient_Handle m_UPnPClientHandle;
|
||||
RootDeviceMap m_RootDeviceMap;
|
||||
ServiceMap m_ServiceMap;
|
||||
PortMappingMap m_ActivePortMappingsMap;
|
||||
RsMutex m_RootDeviceListMutex;
|
||||
bool m_IGWDeviceDetected;
|
||||
CUPnPService *m_WanService;
|
||||
RsMutex m_WaitForSearchTimeoutMutex;
|
||||
|
||||
public:
|
||||
RsMutex m_getStateVariableMutex;
|
||||
std::string m_getStateVariableLastResult;
|
||||
static CUPnPControlPoint *s_CtrlPoint;
|
||||
CUPnPControlPoint(unsigned short udpPort);
|
||||
~CUPnPControlPoint();
|
||||
char* getInternalIpAddress();
|
||||
std::string getExternalAddress();
|
||||
void Subscribe(CUPnPService &service);
|
||||
void Unsubscribe(CUPnPService &service);
|
||||
bool AddPortMappings(
|
||||
std::vector<CUPnPPortMapping> &upnpPortMapping);
|
||||
bool DeletePortMappings(
|
||||
std::vector<CUPnPPortMapping> &upnpPortMapping);
|
||||
|
||||
UpnpClient_Handle GetUPnPClientHandle() const
|
||||
{ return m_UPnPClientHandle; }
|
||||
|
||||
bool GetIGWDeviceDetected() const
|
||||
{ return m_IGWDeviceDetected; }
|
||||
void SetIGWDeviceDetected(bool b)
|
||||
{ m_IGWDeviceDetected = b; }
|
||||
bool WanServiceDetected() const
|
||||
{ return !m_ServiceMap.empty(); }
|
||||
void SetWanService(CUPnPService *service)
|
||||
{ m_WanService = service; }
|
||||
|
||||
// Callback function
|
||||
static int Callback(
|
||||
Upnp_EventType EventType,
|
||||
void* Event,
|
||||
void* Cookie);
|
||||
void OnEventReceived(
|
||||
const std::string &Sid,
|
||||
int EventKey,
|
||||
IXML_Document *ChangedVariables);
|
||||
|
||||
private:
|
||||
void AddRootDevice(
|
||||
IXML_Element *rootDevice,
|
||||
const std::string &urlBase,
|
||||
const char *location,
|
||||
int expires);
|
||||
void RemoveRootDevice(
|
||||
const char *udn);
|
||||
void RefreshPortMappings();
|
||||
bool PrivateAddPortMapping(
|
||||
CUPnPPortMapping &upnpPortMapping);
|
||||
bool PrivateDeletePortMapping(
|
||||
CUPnPPortMapping &upnpPortMapping);
|
||||
};
|
||||
|
||||
// File_checked_for_headers
|
@ -13,183 +13,59 @@ extern "C" {
|
||||
#include "upnp/upnphandler.h"
|
||||
#include "upnp/upnputil.h"
|
||||
|
||||
struct WanDevice {
|
||||
char UDN[250];
|
||||
char DescDocURL[250];
|
||||
char FriendlyName[250];
|
||||
char PresURL[250];
|
||||
int AdvrTimeOut;
|
||||
};
|
||||
|
||||
class uPnPConfigData
|
||||
{
|
||||
public:
|
||||
struct UPNPDev * devlist;
|
||||
struct UPNPUrls urls;
|
||||
struct IGDdatas data;
|
||||
struct WanDevice * WanDevice;
|
||||
char lanaddr[16]; /* my ip address on the LAN */
|
||||
};
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "util/rsnet.h"
|
||||
|
||||
UpnpClient_Handle ctrlpt_handle = -1;
|
||||
|
||||
bool upnphandler::initUPnPState()
|
||||
{
|
||||
/* allocate memory */
|
||||
uPnPConfigData *upcd = new uPnPConfigData;
|
||||
std::cerr << "upnphandler::initUPnPState" << std::endl;
|
||||
cUPnPControlPoint = new CUPnPControlPoint(2000);
|
||||
|
||||
#if MINIUPNPC_VERSION >= 11
|
||||
/* Starting from version 1.1, miniupnpc api has a new parameter (int sameport) */
|
||||
upcd->devlist = upnpDiscover(2000, NULL, NULL, 0);
|
||||
#else
|
||||
upcd->devlist = upnpDiscover(2000, NULL, NULL);
|
||||
#endif
|
||||
|
||||
if(upcd->devlist)
|
||||
{
|
||||
struct UPNPDev * device;
|
||||
printf("List of UPNP devices found on the network :\n");
|
||||
for(device=upcd->devlist;device;device=device->pNext)
|
||||
{
|
||||
printf("\n desc: %s\n st: %s\n",
|
||||
device->descURL, device->st);
|
||||
}
|
||||
putchar('\n');
|
||||
if(UPNP_GetValidIGD(upcd->devlist, &(upcd->urls),
|
||||
&(upcd->data), upcd->lanaddr,
|
||||
sizeof(upcd->lanaddr)))
|
||||
{
|
||||
printf("Found valid IGD : %s\n",
|
||||
upcd->urls.controlURL);
|
||||
printf("Local LAN ip address : %s\n",
|
||||
upcd->lanaddr);
|
||||
bool IGWDetected = cUPnPControlPoint->GetIGWDeviceDetected();
|
||||
|
||||
/* MODIFY STATE */
|
||||
dataMtx.lock(); /* LOCK MUTEX */
|
||||
std::cerr << "upnphandler::initUPnPState cUPnPControlPoint internal ip adress : ";
|
||||
std::cerr << cUPnPControlPoint->getInternalIpAddress() << std::endl;
|
||||
|
||||
/* convert to ipaddress. */
|
||||
inet_aton(upcd->lanaddr, &(upnp_iaddr.sin_addr));
|
||||
//const char ipaddr = cUPnPControlPoint->getInternalIpAddress().c_str();
|
||||
inet_aton(cUPnPControlPoint->getInternalIpAddress(), &(upnp_iaddr.sin_addr));
|
||||
upnp_iaddr.sin_port = htons(iport);
|
||||
|
||||
if (IGWDetected) {
|
||||
upnpState = RS_UPNP_S_READY;
|
||||
if (upnpConfig)
|
||||
{
|
||||
delete upnpConfig;
|
||||
}
|
||||
upnpConfig = upcd; /* */
|
||||
|
||||
dataMtx.unlock(); /* UNLOCK MUTEX */
|
||||
|
||||
|
||||
/* done -> READY */
|
||||
return 1;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "No valid UPNP Internet Gateway Device found.\n");
|
||||
}
|
||||
|
||||
|
||||
freeUPNPDevlist(upcd->devlist);
|
||||
upcd->devlist = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "No IGD UPnP Device found on the network !\n");
|
||||
}
|
||||
|
||||
/* MODIFY STATE */
|
||||
dataMtx.lock(); /* LOCK MUTEX */
|
||||
|
||||
} else {
|
||||
upnpState = RS_UPNP_S_UNAVAILABLE;
|
||||
delete upcd;
|
||||
upnpConfig = NULL;
|
||||
}
|
||||
|
||||
dataMtx.unlock(); /* UNLOCK MUTEX */
|
||||
|
||||
/* done, FAILED -> NOT AVAILABLE */
|
||||
/* done, NOT AVAILABLE YET */
|
||||
|
||||
if (upnpState == RS_UPNP_S_READY) {
|
||||
std::cerr << "upnphandler::initUPnPState READY" << std::endl;
|
||||
} else {
|
||||
std::cerr << "upnphandler::initUPnPState UNAVAILABLE" << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool upnphandler::printUPnPState()
|
||||
{
|
||||
std::cerr << "upnphandler::printUPnPState() ... locking";
|
||||
std::cerr << std::endl;
|
||||
|
||||
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
|
||||
|
||||
std::cerr << "upnphandler::printUPnPState() ... locked";
|
||||
std::cerr << std::endl;
|
||||
|
||||
uPnPConfigData *config = upnpConfig;
|
||||
if ((upnpState >= RS_UPNP_S_READY) && (config))
|
||||
{
|
||||
DisplayInfos(&(config -> urls), &(config->data));
|
||||
GetConnectionStatus(&(config -> urls), &(config->data));
|
||||
ListRedirections(&(config -> urls), &(config->data));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "UPNP not Ready" << std::endl;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
bool upnphandler::checkUPnPActive()
|
||||
{
|
||||
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
|
||||
|
||||
uPnPConfigData *config = upnpConfig;
|
||||
if ((upnpState > RS_UPNP_S_READY) && (config))
|
||||
{
|
||||
char eprot1[] = "TCP";
|
||||
char eprot2[] = "UDP";
|
||||
|
||||
char in_addr[256];
|
||||
char in_port1[256];
|
||||
char in_port2[256];
|
||||
char eport1[256];
|
||||
char eport2[256];
|
||||
|
||||
struct sockaddr_in localAddr = upnp_iaddr;
|
||||
uint32_t linaddr = ntohl(localAddr.sin_addr.s_addr);
|
||||
|
||||
snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port));
|
||||
snprintf(in_port2, 256, "%d", ntohs(localAddr.sin_port));
|
||||
|
||||
snprintf(in_addr, 256, "%d.%d.%d.%d",
|
||||
((linaddr >> 24) & 0xff),
|
||||
((linaddr >> 16) & 0xff),
|
||||
((linaddr >> 8) & 0xff),
|
||||
((linaddr >> 0) & 0xff));
|
||||
|
||||
snprintf(eport1, 256, "%d", eport_curr);
|
||||
snprintf(eport2, 256, "%d", eport_curr);
|
||||
|
||||
std::cerr << "upnphandler::checkUPnPState()";
|
||||
std::cerr << " Checking Redirection: InAddr: " << in_addr;
|
||||
std::cerr << " InPort: " << in_port1;
|
||||
std::cerr << " ePort: " << eport1;
|
||||
std::cerr << " eProt: " << eprot1;
|
||||
std::cerr << std::endl;
|
||||
|
||||
|
||||
bool tcpOk = TestRedirect(&(config -> urls), &(config->data),
|
||||
in_addr, in_port1, eport1, eprot1);
|
||||
bool udpOk = TestRedirect(&(config -> urls), &(config->data),
|
||||
in_addr, in_port2, eport2, eprot2);
|
||||
|
||||
if ((!tcpOk) || (!udpOk))
|
||||
{
|
||||
std::cerr << "upnphandler::checkUPnPState() ... Redirect Expired, restarting";
|
||||
std::cerr << std::endl;
|
||||
|
||||
toStop = true;
|
||||
toStart = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
class upnpThreadData
|
||||
{
|
||||
public:
|
||||
@ -219,8 +95,6 @@ extern "C" void* doSetupUPnP(void* p)
|
||||
data->handler->start_upnp();
|
||||
}
|
||||
|
||||
data->handler->printUPnPState();
|
||||
|
||||
delete data;
|
||||
pthread_exit(NULL);
|
||||
|
||||
@ -247,11 +121,9 @@ bool upnphandler::start_upnp()
|
||||
{
|
||||
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
|
||||
|
||||
uPnPConfigData *config = upnpConfig;
|
||||
if (!((upnpState >= RS_UPNP_S_READY) && (config)))
|
||||
if (!(upnpState >= RS_UPNP_S_READY))
|
||||
{
|
||||
std::cerr << "upnphandler::start_upnp() Not Ready";
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "upnphandler::start_upnp() Not Ready" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -280,16 +152,13 @@ bool upnphandler::start_upnp()
|
||||
/* our port */
|
||||
char in_addr[256];
|
||||
char in_port1[256];
|
||||
char in_port2[256];
|
||||
char eport1[256];
|
||||
char eport2[256];
|
||||
|
||||
upnp_iaddr.sin_port = htons(iport);
|
||||
struct sockaddr_in localAddr = upnp_iaddr;
|
||||
uint32_t linaddr = ntohl(localAddr.sin_addr.s_addr);
|
||||
|
||||
snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port));
|
||||
snprintf(in_port2, 256, "%d", ntohs(localAddr.sin_port));
|
||||
snprintf(in_addr, 256, "%d.%d.%d.%d",
|
||||
((linaddr >> 24) & 0xff),
|
||||
((linaddr >> 16) & 0xff),
|
||||
@ -297,7 +166,6 @@ bool upnphandler::start_upnp()
|
||||
((linaddr >> 0) & 0xff));
|
||||
|
||||
snprintf(eport1, 256, "%d", eport_curr);
|
||||
snprintf(eport2, 256, "%d", eport_curr);
|
||||
|
||||
std::cerr << "Attempting Redirection: InAddr: " << in_addr;
|
||||
std::cerr << " InPort: " << in_port1;
|
||||
@ -305,32 +173,35 @@ bool upnphandler::start_upnp()
|
||||
std::cerr << " eProt: " << eprot1;
|
||||
std::cerr << std::endl;
|
||||
|
||||
if (!SetRedirectAndTest(&(config -> urls), &(config->data),
|
||||
in_addr, in_port1, eport1, eprot1))
|
||||
{
|
||||
//build port mapping config
|
||||
std::vector<CUPnPPortMapping> upnpPortMapping1;
|
||||
CUPnPPortMapping cUPnPPortMapping1 = CUPnPPortMapping(eport_curr, ntohs(localAddr.sin_port), "TCP", true, "tcp retroshare redirection");
|
||||
upnpPortMapping1.push_back(cUPnPPortMapping1);
|
||||
bool res = cUPnPControlPoint->AddPortMappings(upnpPortMapping1);
|
||||
|
||||
if (res) {
|
||||
upnpState = RS_UPNP_S_ACTIVE;
|
||||
} else {
|
||||
upnpState = RS_UPNP_S_TCP_FAILED;
|
||||
}
|
||||
else if (!SetRedirectAndTest(&(config -> urls), &(config->data),
|
||||
in_addr, in_port2, eport2, eprot2))
|
||||
{
|
||||
std::vector<CUPnPPortMapping> upnpPortMapping2;
|
||||
CUPnPPortMapping cUPnPPortMapping2 = CUPnPPortMapping(eport_curr, ntohs(localAddr.sin_port), "UDP", true, "udp retroshare redirection");
|
||||
upnpPortMapping2.push_back(cUPnPPortMapping2);
|
||||
bool res2 = cUPnPControlPoint->AddPortMappings(upnpPortMapping2);
|
||||
if (res) {
|
||||
upnpState = RS_UPNP_S_ACTIVE;
|
||||
} else {
|
||||
upnpState = RS_UPNP_S_UDP_FAILED;
|
||||
}
|
||||
else
|
||||
{
|
||||
upnpState = RS_UPNP_S_ACTIVE;
|
||||
}
|
||||
|
||||
|
||||
/* now store the external address */
|
||||
char externalIPAddress[32];
|
||||
UPNP_GetExternalIPAddress(config -> urls.controlURL,
|
||||
config->data.servicetype,
|
||||
externalIPAddress);
|
||||
|
||||
std::string externalAdress = cUPnPControlPoint->getExternalAddress();
|
||||
sockaddr_clear(&upnp_eaddr);
|
||||
|
||||
if(externalIPAddress[0])
|
||||
if(!externalAdress.empty())
|
||||
{
|
||||
const char* externalIPAddress = externalAdress.c_str();
|
||||
|
||||
std::cerr << "Stored External address: " << externalIPAddress;
|
||||
std::cerr << ":" << eport_curr;
|
||||
std::cerr << std::endl;
|
||||
@ -355,8 +226,7 @@ bool upnphandler::shutdown_upnp()
|
||||
{
|
||||
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
|
||||
|
||||
uPnPConfigData *config = upnpConfig;
|
||||
if (!((upnpState >= RS_UPNP_S_READY) && (config)))
|
||||
if (!(upnpState >= RS_UPNP_S_READY))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -378,18 +248,23 @@ bool upnphandler::shutdown_upnp()
|
||||
std::cerr << " Prot: " << eprot1;
|
||||
std::cerr << std::endl;
|
||||
|
||||
RemoveRedirect(&(config -> urls), &(config->data),
|
||||
eport1, eprot1);
|
||||
|
||||
std::vector<CUPnPPortMapping> upnpPortMapping1;
|
||||
CUPnPPortMapping *cUPnPPortMapping1 = &CUPnPPortMapping(eport_curr, 0, eprot1, true, "tcp redirection");
|
||||
upnpPortMapping1.push_back(*cUPnPPortMapping1);
|
||||
cUPnPControlPoint->DeletePortMappings(upnpPortMapping1);
|
||||
|
||||
std::cerr << "Attempting To Remove Redirection: port: " << eport2;
|
||||
std::cerr << " Prot: " << eprot2;
|
||||
std::cerr << std::endl;
|
||||
|
||||
RemoveRedirect(&(config -> urls), &(config->data),
|
||||
eport2, eprot2);
|
||||
std::vector<CUPnPPortMapping> upnpPortMapping2;
|
||||
CUPnPPortMapping *cUPnPPortMapping2 = &CUPnPPortMapping(eport_curr, 0, eprot2, true, "tcp redirection");
|
||||
upnpPortMapping2.push_back(*cUPnPPortMapping2);
|
||||
cUPnPControlPoint->DeletePortMappings(upnpPortMapping2);
|
||||
|
||||
upnpState = RS_UPNP_S_READY;
|
||||
//destroy the upnp object
|
||||
cUPnPControlPoint->~CUPnPControlPoint();
|
||||
upnpState = RS_UPNP_S_UNINITIALISED;
|
||||
toStop = false;
|
||||
}
|
||||
|
||||
@ -405,11 +280,8 @@ bool upnphandler::shutdown_upnp()
|
||||
|
||||
upnphandler::upnphandler()
|
||||
:toEnable(false), toStart(false), toStop(false),
|
||||
eport(0), eport_curr(0),
|
||||
upnpState(RS_UPNP_S_UNINITIALISED),
|
||||
upnpConfig(NULL)
|
||||
eport(0), eport_curr(0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
upnphandler::~upnphandler()
|
||||
@ -444,8 +316,6 @@ void upnphandler::enable(bool active)
|
||||
/* make background thread to startup UPnP */
|
||||
background_setup_upnp(true, false);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -490,9 +360,9 @@ bool upnphandler::getActive()
|
||||
/* the address that the listening port is on */
|
||||
void upnphandler::setInternalPort(unsigned short iport_in)
|
||||
{
|
||||
// std::cerr << "UPnPHandler::setInternalAddress() pre Lock!" << std::endl;
|
||||
std::cerr << "UPnPHandler::setInternalAddress() pre Lock!" << std::endl;
|
||||
dataMtx.lock(); /*** LOCK MUTEX ***/
|
||||
// std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl;
|
||||
std::cerr << "UPnPHandler::setInternalAddress() postLock!" << std::endl;
|
||||
|
||||
std::cerr << "UPnPHandler::setInternalPort(" << iport_in << ") current port: ";
|
||||
std::cerr << iport << std::endl;
|
||||
@ -512,9 +382,9 @@ void upnphandler::setInternalPort(unsigned short iport_in)
|
||||
|
||||
void upnphandler::setExternalPort(unsigned short eport_in)
|
||||
{
|
||||
// std::cerr << "UPnPHandler::getExternalPort() pre Lock!" << std::endl;
|
||||
std::cerr << "UPnPHandler::setExternalPort() pre Lock!" << std::endl;
|
||||
dataMtx.lock(); /*** LOCK MUTEX ***/
|
||||
// std::cerr << "UPnPHandler::getExternalPort() postLock!" << std::endl;
|
||||
std::cerr << "UPnPHandler::setExternalPort() postLock!" << std::endl;
|
||||
|
||||
std::cerr << "UPnPHandler::setExternalPort(" << eport_in << ") current port: ";
|
||||
std::cerr << eport << std::endl;
|
||||
@ -565,5 +435,3 @@ bool upnphandler::getExternalAddress(struct sockaddr_in &addr)
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,15 +3,15 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
/* platform independent networking... */
|
||||
#include "pqi/pqinetwork.h"
|
||||
#include "pqi/pqiassist.h"
|
||||
|
||||
#include "util/rsthreads.h"
|
||||
|
||||
#include "upnp.h"
|
||||
#include "upnp/UPnPBase.h"
|
||||
|
||||
class upnpentry
|
||||
{
|
||||
public:
|
||||
@ -67,21 +67,23 @@ virtual bool getExternalAddress(struct sockaddr_in &addr);
|
||||
/* Public functions - for background thread operation,
|
||||
* but effectively private from rest of RS, as in derived class
|
||||
*/
|
||||
unsigned int upnpState;
|
||||
|
||||
bool start_upnp();
|
||||
bool shutdown_upnp();
|
||||
|
||||
bool initUPnPState();
|
||||
bool printUPnPState();
|
||||
|
||||
private:
|
||||
|
||||
bool background_setup_upnp(bool, bool);
|
||||
bool checkUPnPActive();
|
||||
|
||||
/* Mutex for data below */
|
||||
RsMutex dataMtx;
|
||||
|
||||
private:
|
||||
|
||||
CUPnPControlPoint *cUPnPControlPoint;
|
||||
|
||||
bool background_setup_upnp(bool, bool);
|
||||
|
||||
|
||||
bool toEnable; /* overall on/off switch */
|
||||
bool toStart; /* if set start forwarding */
|
||||
bool toStop; /* if set stop forwarding */
|
||||
@ -91,9 +93,6 @@ bool checkUPnPActive();
|
||||
unsigned short eport_curr; /* current forwarded */
|
||||
|
||||
/* info from upnp */
|
||||
unsigned int upnpState;
|
||||
uPnPConfigData *upnpConfig;
|
||||
|
||||
struct sockaddr_in upnp_iaddr;
|
||||
struct sockaddr_in upnp_eaddr;
|
||||
|
||||
@ -102,4 +101,7 @@ bool checkUPnPActive();
|
||||
|
||||
};
|
||||
|
||||
/* info from upnp */
|
||||
int CtrlPointCallbackEventHandler(Upnp_EventType ,void* , void*);
|
||||
|
||||
#endif /* _RS_UPNP_IFACE_H */
|
||||
|
@ -1,290 +0,0 @@
|
||||
|
||||
#include "upnp/upnputil.h"
|
||||
|
||||
/* protofix() checks if protocol is "UDP" or "TCP"
|
||||
* returns NULL if not */
|
||||
const char * protofix(const char * proto)
|
||||
{
|
||||
static const char proto_tcp[4] = { 'T', 'C', 'P', 0};
|
||||
static const char proto_udp[4] = { 'U', 'D', 'P', 0};
|
||||
int i, b;
|
||||
for(i=0, b=1; i<4; i++)
|
||||
b = b && ( (proto[i] == proto_tcp[i])
|
||||
|| (proto[i] == (proto_tcp[i] | 32)) );
|
||||
if(b)
|
||||
return proto_tcp;
|
||||
for(i=0, b=1; i<4; i++)
|
||||
b = b && ( (proto[i] == proto_udp[i])
|
||||
|| (proto[i] == (proto_udp[i] | 32)) );
|
||||
if(b)
|
||||
return proto_udp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DisplayInfos(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data)
|
||||
{
|
||||
char externalIPAddress[16];
|
||||
char connectionType[64];
|
||||
char status[64];
|
||||
char lastconnerror[64];
|
||||
unsigned int uptime;
|
||||
unsigned int brUp, brDown;
|
||||
UPNP_GetConnectionTypeInfo(urls->controlURL,
|
||||
data->servicetype,
|
||||
connectionType);
|
||||
if(connectionType[0])
|
||||
printf("Connection Type : %s\n", connectionType);
|
||||
else
|
||||
printf("GetConnectionTypeInfo failed.\n");
|
||||
UPNP_GetStatusInfo(urls->controlURL, data->servicetype, status, &uptime, lastconnerror);
|
||||
printf("Status : %s, uptime=%u LastConnError %s\n", status, uptime, lastconnerror);
|
||||
UPNP_GetLinkLayerMaxBitRates(urls->controlURL_CIF, data->servicetype_CIF,
|
||||
&brDown, &brUp);
|
||||
printf("MaxBitRateDown : %u bps MaxBitRateUp %u bps\n", brDown, brUp);
|
||||
UPNP_GetExternalIPAddress(urls->controlURL,
|
||||
data->servicetype,
|
||||
externalIPAddress);
|
||||
if(externalIPAddress[0])
|
||||
printf("ExternalIPAddress = %s\n", externalIPAddress);
|
||||
else
|
||||
printf("GetExternalIPAddress failed.\n");
|
||||
}
|
||||
|
||||
void GetConnectionStatus(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data)
|
||||
{
|
||||
unsigned int bytessent, bytesreceived, packetsreceived, packetssent;
|
||||
DisplayInfos(urls, data);
|
||||
bytessent = UPNP_GetTotalBytesSent(urls->controlURL_CIF, data->servicetype_CIF);
|
||||
bytesreceived = UPNP_GetTotalBytesReceived(urls->controlURL_CIF, data->servicetype_CIF);
|
||||
packetssent = UPNP_GetTotalPacketsSent(urls->controlURL_CIF, data->servicetype_CIF);
|
||||
packetsreceived = UPNP_GetTotalPacketsReceived(urls->controlURL_CIF, data->servicetype_CIF);
|
||||
printf("Bytes: Sent: %8u\tRecv: %8u\n", bytessent, bytesreceived);
|
||||
printf("Packets: Sent: %8u\tRecv: %8u\n", packetssent, packetsreceived);
|
||||
}
|
||||
|
||||
void ListRedirections(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data)
|
||||
{
|
||||
int r;
|
||||
int i = 0;
|
||||
char index[6];
|
||||
char intClient[16];
|
||||
char intPort[6];
|
||||
char extPort[6];
|
||||
char protocol[4];
|
||||
char desc[80];
|
||||
char enabled[6];
|
||||
char rHost[64];
|
||||
char duration[16];
|
||||
/*unsigned int num=0;
|
||||
UPNP_GetPortMappingNumberOfEntries(urls->controlURL, data->servicetype, &num);
|
||||
printf("PortMappingNumberOfEntries : %u\n", num);*/
|
||||
do {
|
||||
snprintf(index, 6, "%d", i);
|
||||
rHost[0] = '\0'; enabled[0] = '\0';
|
||||
duration[0] = '\0'; desc[0] = '\0';
|
||||
extPort[0] = '\0'; intPort[0] = '\0'; intClient[0] = '\0';
|
||||
r = UPNP_GetGenericPortMappingEntry(urls->controlURL, data->servicetype,
|
||||
index,
|
||||
extPort, intClient, intPort,
|
||||
protocol, desc, enabled,
|
||||
rHost, duration);
|
||||
if(r==0)
|
||||
printf("%02d - %s %s->%s:%s\tenabled=%s leaseDuration=%s\n"
|
||||
" desc='%s' rHost='%s'\n",
|
||||
i, protocol, extPort, intClient, intPort,
|
||||
enabled, duration,
|
||||
desc, rHost);
|
||||
i++;
|
||||
} while(r==0);
|
||||
}
|
||||
|
||||
/* Test function
|
||||
* 1 - get connection type
|
||||
* 2 - get extenal ip address
|
||||
* 3 - Add port mapping
|
||||
* 4 - get this port mapping from the IGD */
|
||||
bool SetRedirectAndTest(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * iaddr,
|
||||
const char * iport,
|
||||
const char * eport,
|
||||
const char * proto)
|
||||
{
|
||||
char externalIPAddress[16];
|
||||
char intClient[16];
|
||||
char intPort[6];
|
||||
char leaseDuration[] = "3600"; /* 60 mins */
|
||||
int r;
|
||||
int ok = 1;
|
||||
|
||||
if(!iaddr || !iport || !eport || !proto)
|
||||
{
|
||||
fprintf(stderr, "Wrong arguments\n");
|
||||
return 0;
|
||||
}
|
||||
proto = protofix(proto);
|
||||
if(!proto)
|
||||
{
|
||||
fprintf(stderr, "invalid protocol\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
UPNP_GetExternalIPAddress(urls->controlURL,
|
||||
data->servicetype,
|
||||
externalIPAddress);
|
||||
if(externalIPAddress[0])
|
||||
printf("ExternalIPAddress = %s\n", externalIPAddress);
|
||||
else
|
||||
printf("GetExternalIPAddress failed.\n");
|
||||
|
||||
// Unix at the moment!
|
||||
#if MINIUPNPC_VERSION >= 13
|
||||
/* Starting from miniupnpc version 1.2, lease duration parameter is gone */
|
||||
r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
|
||||
eport, iport, iaddr, 0, proto, NULL);
|
||||
#else
|
||||
#if MINIUPNPC_VERSION >= 12
|
||||
/* Starting from miniupnpc version 1.2, lease duration parameter is gone */
|
||||
r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
|
||||
eport, iport, iaddr, 0, proto);
|
||||
#else
|
||||
/* The lease parameter is also gone in minupnpc 1.0 */
|
||||
r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
|
||||
eport, iport, iaddr,0, 0, proto);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
|
||||
// eport, iport, iaddr, 0, leaseDuration, proto);
|
||||
|
||||
// r = UPNP_AddPortMapping(urls->controlURL, data->servicetype,
|
||||
// eport, iport, iaddr, 0, proto);
|
||||
if(r==0)
|
||||
{
|
||||
printf("AddPortMapping(%s, %s, %s) failed\n", eport, iport, iaddr);
|
||||
//this seems to trigger for unknown reasons sometimes.
|
||||
//rely on Checking it afterwards...
|
||||
//should check IP address then!
|
||||
//ok = 0;
|
||||
}
|
||||
|
||||
UPNP_GetSpecificPortMappingEntry(urls->controlURL,
|
||||
data->servicetype,
|
||||
eport, proto,
|
||||
intClient, intPort);
|
||||
if(intClient[0])
|
||||
printf("InternalIP:Port = %s:%s\n", intClient, intPort);
|
||||
else
|
||||
{
|
||||
printf("GetSpecificPortMappingEntry failed.\n");
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
if ((strcmp(iaddr, intClient) != 0) || (strcmp(iport, intPort) != 0))
|
||||
{
|
||||
printf("PortMappingEntry to wrong location! FAILED\n");
|
||||
printf("IP1:\"%s\"\n", iaddr);
|
||||
printf("IP2:\"%s\"\n", intClient);
|
||||
printf("PORT1:\"%s\"\n", iport);
|
||||
printf("PORT2:\"%s\"\n", intPort);
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
printf("external %s:%s is redirected to internal %s:%s\n",
|
||||
externalIPAddress, eport, intClient, intPort);
|
||||
|
||||
if (ok)
|
||||
{
|
||||
printf("uPnP Forward/Mapping Succeeded\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("uPnP Forward/Mapping Failed\n");
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool TestRedirect(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * iaddr,
|
||||
const char * iport,
|
||||
const char * eport,
|
||||
const char * proto)
|
||||
{
|
||||
char intClient[16];
|
||||
char intPort[6];
|
||||
int ok = 1;
|
||||
|
||||
if(!iaddr || !iport || !eport || !proto)
|
||||
{
|
||||
fprintf(stderr, "Wrong arguments\n");
|
||||
return 0;
|
||||
}
|
||||
proto = protofix(proto);
|
||||
if(!proto)
|
||||
{
|
||||
fprintf(stderr, "invalid protocol\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
UPNP_GetSpecificPortMappingEntry(urls->controlURL,
|
||||
data->servicetype,
|
||||
eport, proto,
|
||||
intClient, intPort);
|
||||
if(intClient[0])
|
||||
printf("uPnP Check: InternalIP:Port = %s:%s\n", intClient, intPort);
|
||||
else
|
||||
{
|
||||
printf("GetSpecificPortMappingEntry failed.\n");
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
printf("uPnP Check: External port %s is redirected to internal %s:%s\n",
|
||||
eport, intClient, intPort);
|
||||
|
||||
if (ok)
|
||||
{
|
||||
printf("uPnP Check: uPnP Forward/Mapping still Active\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("uPnP Check: Forward/Mapping has been Dropped\n");
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
RemoveRedirect(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * eport,
|
||||
const char * proto)
|
||||
{
|
||||
if(!proto || !eport)
|
||||
{
|
||||
fprintf(stderr, "invalid arguments\n");
|
||||
return 0;
|
||||
}
|
||||
proto = protofix(proto);
|
||||
if(!proto)
|
||||
{
|
||||
fprintf(stderr, "protocol invalid\n");
|
||||
return 0;
|
||||
}
|
||||
#if MINIUPNPC_VERSION >= 13
|
||||
UPNP_DeletePortMapping(urls->controlURL, data->servicetype, eport, proto, NULL);
|
||||
#else
|
||||
UPNP_DeletePortMapping(urls->controlURL, data->servicetype, eport, proto);
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* EOF */
|
@ -1,55 +0,0 @@
|
||||
|
||||
#ifndef MINIUPNP_UTIL_H_
|
||||
#define MINIUPNP_UTIL_H_
|
||||
|
||||
/* $Id: upnpc.c,v 1.50 2007/04/26 19:00:10 nanard Exp $ */
|
||||
/* Project : miniupnp
|
||||
* Author : Thomas Bernard
|
||||
* Copyright (c) 2005 Thomas Bernard
|
||||
* This software is subject to the conditions detailed in the
|
||||
* LICENCE file provided in this distribution.
|
||||
* */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
#include <miniwget.h>
|
||||
#include <miniupnpc.h>
|
||||
#include <upnpcommands.h>
|
||||
|
||||
/* protofix() checks if protocol is "UDP" or "TCP"
|
||||
* returns NULL if not */
|
||||
const char * protofix(const char * proto);
|
||||
void DisplayInfos(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data);
|
||||
|
||||
void GetConnectionStatus(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data);
|
||||
|
||||
void ListRedirections(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data);
|
||||
|
||||
bool SetRedirectAndTest(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * iaddr,
|
||||
const char * iport,
|
||||
const char * eport,
|
||||
const char * proto);
|
||||
|
||||
bool TestRedirect(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * iaddr,
|
||||
const char * iport,
|
||||
const char * eport,
|
||||
const char * proto);
|
||||
|
||||
bool RemoveRedirect(struct UPNPUrls * urls,
|
||||
struct IGDdatas * data,
|
||||
const char * eport,
|
||||
const char * proto);
|
||||
|
||||
/* EOF */
|
||||
#endif
|
@ -13,15 +13,16 @@ MOC_DIR = temp/moc
|
||||
# Put lib dir in QMAKE_LFLAGS so it appears before -L/usr/lib
|
||||
linux-g++ {
|
||||
OBJECTS_DIR = temp/linux-g++/obj
|
||||
QMAKE_LFLAGS += -L"../../../../lib/linux-g++"
|
||||
CONFIG += version_detail_bash_script
|
||||
LIBS += -L"../../../../lib" -lretroshare -lssl -lcrypto -lgpgme -lpthread -lminiupnpc -lz
|
||||
LIBS += ../../libretroshare/src/lib/libretroshare.a
|
||||
LIBS += -lssl -lcrypto -lgpgme -lpthread -lz -lupnp
|
||||
}
|
||||
|
||||
linux-g++-64 {
|
||||
OBJECTS_DIR = temp/linux-g++-64/obj
|
||||
QMAKE_LFLAGS += -L"../../../../lib/linux-g++-64"
|
||||
CONFIG += version_detail_bash_script
|
||||
LIBS += -L"../../../../lib" -lretroshare -lssl -lcrypto -lgpgme -lpthread -lminiupnpc -lz
|
||||
LIBS += ../../libretroshare/src/lib/libretroshare.a
|
||||
LIBS += -lssl -lcrypto -lgpgme -lpthread -lz -lupnp
|
||||
}
|
||||
|
||||
version_detail_bash_script {
|
||||
|
Loading…
Reference in New Issue
Block a user