Exemplo n.º 1
0
void UPnP::ClosePort()
{
    if(remote_port_ == 0)
        return;

#ifdef _MSC_VER
    HRESULT hr;

    IUPnPNAT* upnpnat;
    hr = CoCreateInstance (CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void**)&upnpnat);
    if(FAILED(hr) || !upnpnat)
        return;

    IStaticPortMappingCollection* upnpspmc = NULL;
    hr = upnpnat->get_StaticPortMappingCollection(&upnpspmc);
    if(FAILED(hr) || !upnpspmc)
        return;

    BSTR bstrProtocol = A2BSTR("TCP");

    hr = upnpspmc->Remove(remote_port_, bstrProtocol);

    if(FAILED(hr))
        LOG.getlasterror("Automatisches Entfernen des Portforwardings mit UPnP fehlgeschlagen\nFehler");

    SysFreeString(bstrProtocol);

    if(FAILED(hr))
        return;
#else
    int hr;
    UPNPDev* devicelist = NULL;
#ifdef UPNPDISCOVER_SUCCESS
    int upnperror = 0;
    devicelist = upnpDiscover(2000, NULL, NULL, 0, 0 /* ipv6 */, &upnperror);
#else
    devicelist = upnpDiscover(2000, NULL, NULL, 0);
#endif
    if(!devicelist)
        return;

    UPNPUrls urls;
    IGDdatas data;
    hr = UPNP_GetValidIGD(devicelist, &urls, &data, NULL, 0);
    if(hr == 1 || hr == 2)
    {
        std::stringstream p;
        p << remote_port_;
        hr = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, p.str().c_str(), "TCP", NULL);
    }

    freeUPNPDevlist(devicelist);

    if(hr != 0)
        return;
#endif

    remote_port_ = 0;
}
Exemplo n.º 2
0
void UPnP::Destroy()
{
	IUPnPNAT * Nat = NULL;
	IStaticPortMappingCollection * PortMappingCollection = NULL;
	HRESULT Result;
	wchar_t Protocol[256];
	WORD Port;

	if(m_Port == 0) return;
	Port = m_Port;
	m_Port = 0;

#ifdef MFC
	TRACE("UPnP: Removing Port\n");
#endif

	wcscpy_s(Protocol, L"TCP");

	// Create IUPnPNat
	Result = CoCreateInstance(CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void **)&Nat);
	if(FAILED(Result))
	{
#ifdef MFC
		TRACE("UPnP: Unable to create UPnPNAT interface\n");
#endif
		return;
	}

	Result = Nat->get_StaticPortMappingCollection(&PortMappingCollection);

	if(!PortMappingCollection || FAILED(Result))
	{
		if(PortMappingCollection) PortMappingCollection->Release();
		Nat->Release();

#ifdef MFC
		TRACE("UPnP: Unable to acquire a static portmapping collection\n");
#endif
		return;
	}

	Result = PortMappingCollection->Remove(Port, Protocol);

	if(FAILED(Result))
	{
		PortMappingCollection->Release();
		Nat->Release();

#ifdef MFC
		TRACE("UPnP: Unable to remove port\n");
#endif
		return;
	}
}
Exemplo n.º 3
0
bool Mapper_WinUPnP::remove(const unsigned short port, const Protocol protocol) {
	IStaticPortMappingCollection* pSPMC = getStaticPortMappingCollection();
	if(!pSPMC)
		return false;

	/// @todo use a BSTR wrapper
	BSTR protocol_ = SysAllocString(Text::toT(protocols[protocol]).c_str());

	HRESULT hr = pSPMC->Remove(port, protocol_);
	pSPMC->Release();

	SysFreeString(protocol_);

	bool ret = SUCCEEDED(hr);
	if(ret && port == lastPort && protocol == lastProtocol) {
		lastPort = 0;
	}
	return ret;
}
Exemplo n.º 4
0
bool UPNPCheckDlg::WinUPnP_remove(const unsigned short port, const string& protocol, IUPnPNAT* pUN)
{
#ifdef HAVE_NATUPNP_H
	IStaticPortMappingCollection* pSPMC = WinUPnP_getStaticPortMappingCollection(pUN);
	if (!pSPMC)
		return false;
		
	/// @todo use a BSTR wrapper
	BSTR protocol_ = SysAllocString(Text::toT(protocol).c_str());
	
	HRESULT hr = pSPMC->Remove(port, protocol_);
	safe_release(pSPMC);
	SysFreeString(protocol_);
	
	return SUCCEEDED(hr);
#else
	return false;
#endif
}
Exemplo n.º 5
0
// Closes the UPnP ports defined when the object was created
HRESULT UPnP::ClosePorts(bool log) {
//	if(PortsAreOpen == false) {
//		return S_OK;
//	}

	HRESULT hr = E_FAIL;
	HRESULT hr2 = E_FAIL;

	CoInitializeEx ( NULL ,COINIT_MULTITHREADED);
	hr = CoCreateInstance (__uuidof(UPnPNAT),
		NULL,
		CLSCTX_INPROC_SERVER,
		__uuidof(IUPnPNAT),
		(void**)&pUN);

	if(SUCCEEDED(hr)) {

			if(bstrProtocol && bstrInternalClient && bstrDescription) {
				IStaticPortMappingCollection * pSPMC = NULL;
				hr2 = pUN->get_StaticPortMappingCollection (&pSPMC);

				if(SUCCEEDED(hr2) && pSPMC) {
					hr = pSPMC->Remove(PortNumber, bstrProtocol);
					if(SUCCEEDED(hr)) if(log)debug("UPNP: closed port %i",PortNumber);
					pSPMC->Release();
				}

				SysFreeString(bstrProtocol);
				SysFreeString(bstrInternalClient);
				SysFreeString(bstrDescription);
				SysFreeString(bstrExternalIP);
			}
			pUN->Release();
			pUN=NULL;
	}
	CoUninitialize();
	return hr;
}
Exemplo n.º 6
0
// Add a UPnP port
bool Win32UPnPAddPort(UINT outside_port, UINT inside_port, bool udp, char *local_ip, wchar_t *description, bool remove_before_add)
{
	bool ret = false;
	HRESULT hr;
	IUPnPNAT *nat = NULL;
	wchar_t ip_str[MAX_SIZE];
	BSTR bstr_ip, bstr_description, bstr_protocol;
	wchar_t *protocol_str = (udp ? L"UDP" : L"TCP");
	// Validate arguments
	if (outside_port == 0 || outside_port >= 65536 || inside_port == 0 || inside_port >= 65536 ||
		IsEmptyStr(local_ip) || UniIsEmptyStr(description))
	{
		return false;
	}

	StrToUni(ip_str, sizeof(ip_str), local_ip);
	bstr_ip = SysAllocString(ip_str);
	bstr_description = SysAllocString(description);
	bstr_protocol = SysAllocString(protocol_str);

	hr = CoCreateInstance(CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void **)&nat);

	if (SUCCEEDED(hr))
	{
		if (nat != NULL)
		{
			IStaticPortMappingCollection *collection = NULL;
			hr = nat->get_StaticPortMappingCollection(&collection);

			if (SUCCEEDED(hr))
			{
				if (collection != NULL)
				{
					IStaticPortMapping *mapping = NULL;

					if (remove_before_add)
					{
						hr = collection->Remove((long)outside_port, bstr_protocol);
					}

					hr = collection->Add((long)outside_port, bstr_protocol, (long)inside_port,
						bstr_ip, VARIANT_TRUE, bstr_description, &mapping);

					if (SUCCEEDED(hr))
					{
						ret = true;

						if (mapping != NULL)
						{
							mapping->Release();
						}
					}

					collection->Release();
				}
				else
				{
					WHERE;
				}
			}
			else
			{
				WHERE;
			}

			nat->Release();
		}
		else
		{
			WHERE;
		}
	}
	else
	{
		WHERE;
	}

	SysFreeString(bstr_ip);
	SysFreeString(bstr_description);
	SysFreeString(bstr_protocol);

	return ret;
}