Path: blob/master/modules/upnp/upnp_device_miniupnp.cpp
10277 views
/**************************************************************************/1/* upnp_device_miniupnp.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifndef WEB_ENABLED3132#include "upnp_device_miniupnp.h"3334#include "upnp_miniupnp.h"3536#include <miniupnpc/upnpcommands.h>3738void UPNPDeviceMiniUPNP::make_default() {39UPNPDevice::_create = UPNPDeviceMiniUPNP::_create;40}4142String UPNPDeviceMiniUPNP::query_external_address() const {43ERR_FAIL_COND_V_MSG(!is_valid_gateway(), "", "The Internet Gateway Device must be valid.");4445char addr[16];46int i = UPNP_GetExternalIPAddress(47igd_control_url.utf8().get_data(),48igd_service_type.utf8().get_data(),49(char *)&addr);5051ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, "", "Couldn't get external IP address.");5253return String(addr);54}5556int UPNPDeviceMiniUPNP::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {57ERR_FAIL_COND_V_MSG(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY, "The Internet Gateway Device must be valid.");58ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");59ERR_FAIL_COND_V_MSG(port_internal < 0 || port_internal > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 0 and 65535 (inclusive)."); // Needs to allow 0 because 0 signifies "use external port as internal port"60ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");61ERR_FAIL_COND_V_MSG(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION, "The port mapping's lease duration can't be negative.");6263if (port_internal < 1) {64port_internal = port;65}6667int i = UPNP_AddPortMapping(68igd_control_url.utf8().get_data(),69igd_service_type.utf8().get_data(),70itos(port).utf8().get_data(),71itos(port_internal).utf8().get_data(),72igd_our_addr.utf8().get_data(),73desc.is_empty() ? nullptr : desc.utf8().get_data(),74proto.utf8().get_data(),75nullptr, // Remote host, always nullptr as IGDs don't support it76duration > 0 ? itos(duration).utf8().get_data() : nullptr);7778ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't add port mapping.");7980return UPNP::UPNP_RESULT_SUCCESS;81}8283int UPNPDeviceMiniUPNP::delete_port_mapping(int port, String proto) const {84ERR_FAIL_COND_V_MSG(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT, "The port number must be set between 1 and 65535 (inclusive).");85ERR_FAIL_COND_V_MSG(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL, "The protocol must be either TCP or UDP.");8687int i = UPNP_DeletePortMapping(88igd_control_url.utf8().get_data(),89igd_service_type.utf8().get_data(),90itos(port).utf8().get_data(),91proto.utf8().get_data(),92nullptr // Remote host, always nullptr as IGDs don't support it93);9495ERR_FAIL_COND_V_MSG(i != UPNPCOMMAND_SUCCESS, UPNPMiniUPNP::upnp_result(i), "Couldn't delete port mapping.");9697return UPNP::UPNP_RESULT_SUCCESS;98}99100void UPNPDeviceMiniUPNP::set_description_url(const String &url) {101description_url = url;102}103104String UPNPDeviceMiniUPNP::get_description_url() const {105return description_url;106}107108void UPNPDeviceMiniUPNP::set_service_type(const String &type) {109service_type = type;110}111112String UPNPDeviceMiniUPNP::get_service_type() const {113return service_type;114}115116void UPNPDeviceMiniUPNP::set_igd_control_url(const String &url) {117igd_control_url = url;118}119120String UPNPDeviceMiniUPNP::get_igd_control_url() const {121return igd_control_url;122}123124void UPNPDeviceMiniUPNP::set_igd_service_type(const String &type) {125igd_service_type = type;126}127128String UPNPDeviceMiniUPNP::get_igd_service_type() const {129return igd_service_type;130}131132void UPNPDeviceMiniUPNP::set_igd_our_addr(const String &addr) {133igd_our_addr = addr;134}135136String UPNPDeviceMiniUPNP::get_igd_our_addr() const {137return igd_our_addr;138}139140void UPNPDeviceMiniUPNP::set_igd_status(IGDStatus status) {141igd_status = status;142}143144UPNPDeviceMiniUPNP::IGDStatus UPNPDeviceMiniUPNP::get_igd_status() const {145return igd_status;146}147148bool UPNPDeviceMiniUPNP::is_valid_gateway() const {149return igd_status == IGD_STATUS_OK;150}151152#endif // WEB_ENABLED153154155