1 /** 2 UPNP network functions. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.upnp; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.reference; 24 import godot.upnpdevice; 25 /** 26 UPNP network functions. 27 28 Provides UPNP functionality to discover $(D UPNPDevice)s on the local network and execute commands on them, like managing port mappings (port forwarding) and querying the local and remote network IP address. Note that methods on this class are synchronous and block the calling thread. 29 */ 30 @GodotBaseClass struct UPNP 31 { 32 enum string _GODOT_internal_name = "UPNP"; 33 public: 34 @nogc nothrow: 35 union { godot_object _godot_object; Reference _GODOT_base; } 36 alias _GODOT_base this; 37 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 38 package(godot) __gshared bool _classBindingInitialized = false; 39 package(godot) static struct _classBinding 40 { 41 __gshared: 42 @GodotName("get_device_count") GodotMethod!(long) getDeviceCount; 43 @GodotName("get_device") GodotMethod!(UPNPDevice, long) getDevice; 44 @GodotName("add_device") GodotMethod!(void, UPNPDevice) addDevice; 45 @GodotName("set_device") GodotMethod!(void, long, UPNPDevice) setDevice; 46 @GodotName("remove_device") GodotMethod!(void, long) removeDevice; 47 @GodotName("clear_devices") GodotMethod!(void) clearDevices; 48 @GodotName("get_gateway") GodotMethod!(UPNPDevice) getGateway; 49 @GodotName("discover") GodotMethod!(long, long, long, String) discover; 50 @GodotName("query_external_address") GodotMethod!(String) queryExternalAddress; 51 @GodotName("add_port_mapping") GodotMethod!(long, long, long, String, String, long) addPortMapping; 52 @GodotName("delete_port_mapping") GodotMethod!(long, long, String) deletePortMapping; 53 @GodotName("set_discover_multicast_if") GodotMethod!(void, String) setDiscoverMulticastIf; 54 @GodotName("get_discover_multicast_if") GodotMethod!(String) getDiscoverMulticastIf; 55 @GodotName("set_discover_local_port") GodotMethod!(void, long) setDiscoverLocalPort; 56 @GodotName("get_discover_local_port") GodotMethod!(long) getDiscoverLocalPort; 57 @GodotName("set_discover_ipv6") GodotMethod!(void, bool) setDiscoverIpv6; 58 @GodotName("is_discover_ipv6") GodotMethod!(bool) isDiscoverIpv6; 59 } 60 bool opEquals(in UPNP other) const { return _godot_object.ptr is other._godot_object.ptr; } 61 UPNP opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 62 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 63 mixin baseCasts; 64 static UPNP _new() 65 { 66 static godot_class_constructor constructor; 67 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("UPNP"); 68 if(constructor is null) return typeof(this).init; 69 return cast(UPNP)(constructor()); 70 } 71 @disable new(size_t s); 72 /// 73 enum UPNPResult : int 74 { 75 /** 76 UPNP command or discovery was successful. 77 */ 78 upnpResultSuccess = 0, 79 /** 80 Not authorized to use the command on the $(D UPNPDevice). May be returned when the user disabled UPNP on their router. 81 */ 82 upnpResultNotAuthorized = 1, 83 /** 84 No port mapping was found for the given port, protocol combination on the given $(D UPNPDevice). 85 */ 86 upnpResultPortMappingNotFound = 2, 87 /** 88 Inconsistent parameters. 89 */ 90 upnpResultInconsistentParameters = 3, 91 /** 92 No such entry in array. May be returned if a given port, protocol combination is not found on an $(D UPNPDevice). 93 */ 94 upnpResultNoSuchEntryInArray = 4, 95 /** 96 The action failed. 97 */ 98 upnpResultActionFailed = 5, 99 /** 100 The $(D UPNPDevice) does not allow wildcard values for the source IP address. 101 */ 102 upnpResultSrcIpWildcardNotPermitted = 6, 103 /** 104 The $(D UPNPDevice) does not allow wildcard values for the external port. 105 */ 106 upnpResultExtPortWildcardNotPermitted = 7, 107 /** 108 The $(D UPNPDevice) does not allow wildcard values for the internal port. 109 */ 110 upnpResultIntPortWildcardNotPermitted = 8, 111 /** 112 The remote host value must be a wildcard. 113 */ 114 upnpResultRemoteHostMustBeWildcard = 9, 115 /** 116 The external port value must be a wildcard. 117 */ 118 upnpResultExtPortMustBeWildcard = 10, 119 /** 120 No port maps are available. May also be returned if port mapping functionality is not available. 121 */ 122 upnpResultNoPortMapsAvailable = 11, 123 /** 124 Conflict with other mechanism. May be returned instead of `UPNP_RESULT_CONFLICT_WITH_OTHER_MAPPING` if a port mapping conflicts with an existing one. 125 */ 126 upnpResultConflictWithOtherMechanism = 12, 127 /** 128 Conflict with an existing port mapping. 129 */ 130 upnpResultConflictWithOtherMapping = 13, 131 /** 132 External and internal port values must be the same. 133 */ 134 upnpResultSamePortValuesRequired = 14, 135 /** 136 Only permanent leases are supported. Do not use the `duration` parameter when adding port mappings. 137 */ 138 upnpResultOnlyPermanentLeaseSupported = 15, 139 /** 140 Invalid gateway. 141 */ 142 upnpResultInvalidGateway = 16, 143 /** 144 Invalid port. 145 */ 146 upnpResultInvalidPort = 17, 147 /** 148 Invalid protocol. 149 */ 150 upnpResultInvalidProtocol = 18, 151 /** 152 Invalid duration. 153 */ 154 upnpResultInvalidDuration = 19, 155 /** 156 Invalid arguments. 157 */ 158 upnpResultInvalidArgs = 20, 159 /** 160 Invalid response. 161 */ 162 upnpResultInvalidResponse = 21, 163 /** 164 Invalid parameter. 165 */ 166 upnpResultInvalidParam = 22, 167 /** 168 HTTP error. 169 */ 170 upnpResultHttpError = 23, 171 /** 172 Socket error. 173 */ 174 upnpResultSocketError = 24, 175 /** 176 Error allocating memory. 177 */ 178 upnpResultMemAllocError = 25, 179 /** 180 No gateway available. You may need to call $(D discover) first, or discovery didn't detect any valid IGDs (InternetGatewayDevices). 181 */ 182 upnpResultNoGateway = 26, 183 /** 184 No devices available. You may need to call $(D discover) first, or discovery didn't detect any valid $(D UPNPDevice)s. 185 */ 186 upnpResultNoDevices = 27, 187 /** 188 Unknown error. 189 */ 190 upnpResultUnknownError = 28, 191 } 192 /// 193 enum Constants : int 194 { 195 upnpResultSuccess = 0, 196 upnpResultNotAuthorized = 1, 197 upnpResultPortMappingNotFound = 2, 198 upnpResultInconsistentParameters = 3, 199 upnpResultNoSuchEntryInArray = 4, 200 upnpResultActionFailed = 5, 201 upnpResultSrcIpWildcardNotPermitted = 6, 202 upnpResultExtPortWildcardNotPermitted = 7, 203 upnpResultIntPortWildcardNotPermitted = 8, 204 upnpResultRemoteHostMustBeWildcard = 9, 205 upnpResultExtPortMustBeWildcard = 10, 206 upnpResultNoPortMapsAvailable = 11, 207 upnpResultConflictWithOtherMechanism = 12, 208 upnpResultConflictWithOtherMapping = 13, 209 upnpResultSamePortValuesRequired = 14, 210 upnpResultOnlyPermanentLeaseSupported = 15, 211 upnpResultInvalidGateway = 16, 212 upnpResultInvalidPort = 17, 213 upnpResultInvalidProtocol = 18, 214 upnpResultInvalidDuration = 19, 215 upnpResultInvalidArgs = 20, 216 upnpResultInvalidResponse = 21, 217 upnpResultInvalidParam = 22, 218 upnpResultHttpError = 23, 219 upnpResultSocketError = 24, 220 upnpResultMemAllocError = 25, 221 upnpResultNoGateway = 26, 222 upnpResultNoDevices = 27, 223 upnpResultUnknownError = 28, 224 } 225 /** 226 Returns the number of discovered $(D UPNPDevice)s. 227 */ 228 long getDeviceCount() const 229 { 230 checkClassBinding!(typeof(this))(); 231 return ptrcall!(long)(_classBinding.getDeviceCount, _godot_object); 232 } 233 /** 234 Returns the $(D UPNPDevice) at the given `index`. 235 */ 236 Ref!UPNPDevice getDevice(in long index) const 237 { 238 checkClassBinding!(typeof(this))(); 239 return ptrcall!(UPNPDevice)(_classBinding.getDevice, _godot_object, index); 240 } 241 /** 242 Adds the given $(D UPNPDevice) to the list of discovered devices. 243 */ 244 void addDevice(UPNPDevice device) 245 { 246 checkClassBinding!(typeof(this))(); 247 ptrcall!(void)(_classBinding.addDevice, _godot_object, device); 248 } 249 /** 250 Sets the device at `index` from the list of discovered devices to `device`. 251 */ 252 void setDevice(in long index, UPNPDevice device) 253 { 254 checkClassBinding!(typeof(this))(); 255 ptrcall!(void)(_classBinding.setDevice, _godot_object, index, device); 256 } 257 /** 258 Removes the device at `index` from the list of discovered devices. 259 */ 260 void removeDevice(in long index) 261 { 262 checkClassBinding!(typeof(this))(); 263 ptrcall!(void)(_classBinding.removeDevice, _godot_object, index); 264 } 265 /** 266 Clears the list of discovered devices. 267 */ 268 void clearDevices() 269 { 270 checkClassBinding!(typeof(this))(); 271 ptrcall!(void)(_classBinding.clearDevices, _godot_object); 272 } 273 /** 274 Returns the default gateway. That is the first discovered $(D UPNPDevice) that is also a valid IGD (InternetGatewayDevice). 275 */ 276 Ref!UPNPDevice getGateway() const 277 { 278 checkClassBinding!(typeof(this))(); 279 return ptrcall!(UPNPDevice)(_classBinding.getGateway, _godot_object); 280 } 281 /** 282 Discovers local $(D UPNPDevice)s. Clears the list of previously discovered devices. 283 Filters for IGD (InternetGatewayDevice) type devices by default, as those manage port forwarding. `timeout` is the time to wait for responses in milliseconds. `ttl` is the time-to-live; only touch this if you know what you're doing. 284 See $(D upnpresult) for possible return values. 285 */ 286 long discover(StringArg2)(in long timeout = 2000, in long ttl = 2, in StringArg2 device_filter = "InternetGatewayDevice") 287 { 288 checkClassBinding!(typeof(this))(); 289 return ptrcall!(long)(_classBinding.discover, _godot_object, timeout, ttl, device_filter); 290 } 291 /** 292 Returns the external $(D IP) address of the default gateway (see $(D getGateway)) as string. Returns an empty string on error. 293 */ 294 String queryExternalAddress() const 295 { 296 checkClassBinding!(typeof(this))(); 297 return ptrcall!(String)(_classBinding.queryExternalAddress, _godot_object); 298 } 299 /** 300 Adds a mapping to forward the external `port` (between 1 and 65535) on the default gateway (see $(D getGateway)) to the `internal_port` on the local machine for the given protocol `proto` (either `TCP` or `UDP`, with UDP being the default). If a port mapping for the given port and protocol combination already exists on that gateway device, this method tries to overwrite it. If that is not desired, you can retrieve the gateway manually with $(D getGateway) and call $(D addPortMapping) on it, if any. 301 If `internal_port` is `0` (the default), the same port number is used for both the external and the internal port (the `port` value). 302 The description (`desc`) is shown in some router UIs and can be used to point out which application added the mapping, and the lifetime of the mapping can be limited by `duration`. However, some routers are incompatible with one or both of these, so use with caution and add fallback logic in case of errors to retry without them if in doubt. 303 See $(D upnpresult) for possible return values. 304 */ 305 long addPortMapping(StringArg2, StringArg3)(in long port, in long port_internal = 0, in StringArg2 desc = "", in StringArg3 proto = "UDP", in long duration = 0) const 306 { 307 checkClassBinding!(typeof(this))(); 308 return ptrcall!(long)(_classBinding.addPortMapping, _godot_object, port, port_internal, desc, proto, duration); 309 } 310 /** 311 Deletes the port mapping for the given port and protocol combination on the default gateway (see $(D getGateway)) if one exists. `port` must be a valid port between 1 and 65535, `proto` can be either `TCP` or `UDP`. See $(D upnpresult) for possible return values. 312 */ 313 long deletePortMapping(StringArg1)(in long port, in StringArg1 proto = "UDP") const 314 { 315 checkClassBinding!(typeof(this))(); 316 return ptrcall!(long)(_classBinding.deletePortMapping, _godot_object, port, proto); 317 } 318 /** 319 320 */ 321 void setDiscoverMulticastIf(StringArg0)(in StringArg0 m_if) 322 { 323 checkClassBinding!(typeof(this))(); 324 ptrcall!(void)(_classBinding.setDiscoverMulticastIf, _godot_object, m_if); 325 } 326 /** 327 328 */ 329 String getDiscoverMulticastIf() const 330 { 331 checkClassBinding!(typeof(this))(); 332 return ptrcall!(String)(_classBinding.getDiscoverMulticastIf, _godot_object); 333 } 334 /** 335 336 */ 337 void setDiscoverLocalPort(in long port) 338 { 339 checkClassBinding!(typeof(this))(); 340 ptrcall!(void)(_classBinding.setDiscoverLocalPort, _godot_object, port); 341 } 342 /** 343 344 */ 345 long getDiscoverLocalPort() const 346 { 347 checkClassBinding!(typeof(this))(); 348 return ptrcall!(long)(_classBinding.getDiscoverLocalPort, _godot_object); 349 } 350 /** 351 352 */ 353 void setDiscoverIpv6(in bool ipv6) 354 { 355 checkClassBinding!(typeof(this))(); 356 ptrcall!(void)(_classBinding.setDiscoverIpv6, _godot_object, ipv6); 357 } 358 /** 359 360 */ 361 bool isDiscoverIpv6() const 362 { 363 checkClassBinding!(typeof(this))(); 364 return ptrcall!(bool)(_classBinding.isDiscoverIpv6, _godot_object); 365 } 366 /** 367 Multicast interface to use for discovery. Uses the default multicast interface if empty. 368 */ 369 @property String discoverMulticastIf() 370 { 371 return getDiscoverMulticastIf(); 372 } 373 /// ditto 374 @property void discoverMulticastIf(String v) 375 { 376 setDiscoverMulticastIf(v); 377 } 378 /** 379 If `0`, the local port to use for discovery is chosen automatically by the system. If `1`, discovery will be done from the source port 1900 (same as destination port). Otherwise, the value will be used as the port. 380 */ 381 @property long discoverLocalPort() 382 { 383 return getDiscoverLocalPort(); 384 } 385 /// ditto 386 @property void discoverLocalPort(long v) 387 { 388 setDiscoverLocalPort(v); 389 } 390 /** 391 If `true`, IPv6 is used for $(D UPNPDevice) discovery. 392 */ 393 @property bool discoverIpv6() 394 { 395 return isDiscoverIpv6(); 396 } 397 /// ditto 398 @property void discoverIpv6(bool v) 399 { 400 setDiscoverIpv6(v); 401 } 402 }