123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #include "upnpdevice.h"
- #include "upnp.h"
- #include <miniupnpc/upnpcommands.h>
- String UPNPDevice::query_external_address() const {
- ERR_FAIL_COND_V(!is_valid_gateway(), "");
- char addr[16];
- int i = UPNP_GetExternalIPAddress(
- igd_control_url.utf8().get_data(),
- igd_service_type.utf8().get_data(),
- (char *)&addr);
- ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, "");
- return String(addr);
- }
- int UPNPDevice::add_port_mapping(int port, int port_internal, String desc, String proto, int duration) const {
- ERR_FAIL_COND_V(!is_valid_gateway(), UPNP::UPNP_RESULT_INVALID_GATEWAY);
- ERR_FAIL_COND_V(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT);
- ERR_FAIL_COND_V(port_internal < 0 || port_internal > 65535, UPNP::UPNP_RESULT_INVALID_PORT);
- ERR_FAIL_COND_V(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL);
- ERR_FAIL_COND_V(duration < 0, UPNP::UPNP_RESULT_INVALID_DURATION);
- if (port_internal < 1) {
- port_internal = port;
- }
- int i = UPNP_AddPortMapping(
- igd_control_url.utf8().get_data(),
- igd_service_type.utf8().get_data(),
- itos(port).utf8().get_data(),
- itos(port_internal).utf8().get_data(),
- igd_our_addr.utf8().get_data(),
- desc.empty() ? 0 : desc.utf8().get_data(),
- proto.utf8().get_data(),
- NULL,
- duration > 0 ? itos(duration).utf8().get_data() : 0);
- ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i));
- return UPNP::UPNP_RESULT_SUCCESS;
- }
- int UPNPDevice::delete_port_mapping(int port, String proto) const {
- ERR_FAIL_COND_V(port < 1 || port > 65535, UPNP::UPNP_RESULT_INVALID_PORT);
- ERR_FAIL_COND_V(proto != "UDP" && proto != "TCP", UPNP::UPNP_RESULT_INVALID_PROTOCOL);
- int i = UPNP_DeletePortMapping(
- igd_control_url.utf8().get_data(),
- igd_service_type.utf8().get_data(),
- itos(port).utf8().get_data(),
- proto.utf8().get_data(),
- NULL
- );
- ERR_FAIL_COND_V(i != UPNPCOMMAND_SUCCESS, UPNP::upnp_result(i));
- return UPNP::UPNP_RESULT_SUCCESS;
- }
- void UPNPDevice::set_description_url(const String &url) {
- description_url = url;
- }
- String UPNPDevice::get_description_url() const {
- return description_url;
- }
- void UPNPDevice::set_service_type(const String &type) {
- service_type = type;
- }
- String UPNPDevice::get_service_type() const {
- return service_type;
- }
- void UPNPDevice::set_igd_control_url(const String &url) {
- igd_control_url = url;
- }
- String UPNPDevice::get_igd_control_url() const {
- return igd_control_url;
- }
- void UPNPDevice::set_igd_service_type(const String &type) {
- igd_service_type = type;
- }
- String UPNPDevice::get_igd_service_type() const {
- return igd_service_type;
- }
- void UPNPDevice::set_igd_our_addr(const String &addr) {
- igd_our_addr = addr;
- }
- String UPNPDevice::get_igd_our_addr() const {
- return igd_our_addr;
- }
- void UPNPDevice::set_igd_status(IGDStatus status) {
- igd_status = status;
- }
- UPNPDevice::IGDStatus UPNPDevice::get_igd_status() const {
- return igd_status;
- }
- bool UPNPDevice::is_valid_gateway() const {
- return igd_status == IGD_STATUS_OK;
- }
- void UPNPDevice::_bind_methods() {
- ClassDB::bind_method(D_METHOD("is_valid_gateway"), &UPNPDevice::is_valid_gateway);
- ClassDB::bind_method(D_METHOD("query_external_address"), &UPNPDevice::query_external_address);
- ClassDB::bind_method(D_METHOD("add_port_mapping", "port", "port_internal", "desc", "proto", "duration"), &UPNPDevice::add_port_mapping, DEFVAL(0), DEFVAL(""), DEFVAL("UDP"), DEFVAL(0));
- ClassDB::bind_method(D_METHOD("delete_port_mapping", "port", "proto"), &UPNPDevice::delete_port_mapping, DEFVAL("UDP"));
- ClassDB::bind_method(D_METHOD("set_description_url", "url"), &UPNPDevice::set_description_url);
- ClassDB::bind_method(D_METHOD("get_description_url"), &UPNPDevice::get_description_url);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "description_url"), "set_description_url", "get_description_url");
- ClassDB::bind_method(D_METHOD("set_service_type", "type"), &UPNPDevice::set_service_type);
- ClassDB::bind_method(D_METHOD("get_service_type"), &UPNPDevice::get_service_type);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "service_type"), "set_service_type", "get_service_type");
- ClassDB::bind_method(D_METHOD("set_igd_control_url", "url"), &UPNPDevice::set_igd_control_url);
- ClassDB::bind_method(D_METHOD("get_igd_control_url"), &UPNPDevice::get_igd_control_url);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_control_url"), "set_igd_control_url", "get_igd_control_url");
- ClassDB::bind_method(D_METHOD("set_igd_service_type", "type"), &UPNPDevice::set_igd_service_type);
- ClassDB::bind_method(D_METHOD("get_igd_service_type"), &UPNPDevice::get_igd_service_type);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_service_type"), "set_igd_service_type", "get_igd_service_type");
- ClassDB::bind_method(D_METHOD("set_igd_our_addr", "addr"), &UPNPDevice::set_igd_our_addr);
- ClassDB::bind_method(D_METHOD("get_igd_our_addr"), &UPNPDevice::get_igd_our_addr);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "igd_our_addr"), "set_igd_our_addr", "get_igd_our_addr");
- ClassDB::bind_method(D_METHOD("set_igd_status", "status"), &UPNPDevice::set_igd_status);
- ClassDB::bind_method(D_METHOD("get_igd_status"), &UPNPDevice::get_igd_status);
- ADD_PROPERTY(PropertyInfo(Variant::INT, "igd_status", PROPERTY_HINT_ENUM), "set_igd_status", "get_igd_status");
- BIND_ENUM_CONSTANT(IGD_STATUS_OK);
- BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_ERROR);
- BIND_ENUM_CONSTANT(IGD_STATUS_HTTP_EMPTY);
- BIND_ENUM_CONSTANT(IGD_STATUS_NO_URLS);
- BIND_ENUM_CONSTANT(IGD_STATUS_NO_IGD);
- BIND_ENUM_CONSTANT(IGD_STATUS_DISCONNECTED);
- BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_DEVICE);
- BIND_ENUM_CONSTANT(IGD_STATUS_INVALID_CONTROL);
- BIND_ENUM_CONSTANT(IGD_STATUS_MALLOC_ERROR);
- BIND_ENUM_CONSTANT(IGD_STATUS_UNKNOWN_ERROR);
- }
- UPNPDevice::UPNPDevice() {
- description_url = "";
- service_type = "";
- igd_control_url = "";
- igd_service_type = "";
- igd_our_addr = "";
- igd_status = IGD_STATUS_UNKNOWN_ERROR;
- }
- UPNPDevice::~UPNPDevice() {
- }
|