racket-nat-pmp
If you find that this library lacks some feature you need, or you have a suggestion for improving it, please don’t hesitate to get in touch with me!
1 Introduction
This library implements NAT-PMP, the Apple/IETF protocol for opening TCP and UDP ports on home routers.
Using this library, you can discover the external IP address of your home router, and can manage port mappings from the public internet to internal TCP and UDP ports.
2 References
NAT-PMP is currently defined in an Internet-Draft.
3 What to require
All the functionality below can be accessed with a single require:
(require (planet tonyg/nat-pmp:1:=2)) |
3.1 Getting information on local interfaces and gateways
NAT-PMP depends on being able to learn the IP address of the current default gateway. Currently, this library learns this information by running the system utility netstat and parsing its output.
procedure
As an additional convenience, this library provides utilities for discovering and classifying local interface addresses.
procedure
procedure
(localhost-ip-address? addr) → boolean?
addr : string?
procedure
(private-ip-address? addr) → boolean?
addr : string?
3.2 Timeouts
Requests made to the gateway using NAT-PMP will eventually time out if the gateway does not support NAT-PMP. In this case, an exception is raised.
3.3 Discovering the current external IP address
procedure
3.4 Low-level interface: mapping ports manually
If you can’t or don’t want to use the persistent mapping support of this library, these routines let you explicitly manage mappings with the gateway.
procedure
(map-port! protocol local-port requested-port lifetime-seconds) → mapping? protocol : (or/c 'udp 'tcp) local-port : integer? requested-port : integer? lifetime-seconds : integer?
procedure
(unmap-port! protocol local-port) → void?
protocol : (or/c 'udp 'tcp) local-port : integer?
procedure
(unmap-all-ports! protocol) → void?
protocol : (or/c 'udp 'tcp)
procedure
(refresh-mapping! mapping) → mapping?
mapping : mapping?
procedure
(delete-mapping! mapping) → void?
mapping : mapping?
struct
(struct mapping (protocol internal-port external-port lifetime) #:extra-constructor-name make-mapping #:prefab) protocol : (or/c 'udp 'tcp) internal-port : integer? external-port : integer? lifetime : integer?
3.5 High-level interface: persistent mappings
procedure
(make-persistent-mapping protocol local-port requested-port [ #:refresh-interval refresh-interval #:on-mapping on-mapping]) → persistent-mapping? protocol : (or/c 'udp 'tcp) local-port : integer? requested-port : integer? refresh-interval : integer? = 7200 on-mapping : (-> mapping? any/c) = void
Every time the externally mapped port changes (including when the mapping is first established!) the #:on-mapping callback is called with the updated mapping information. Note that the callback is invoked directly from the mapping’s thread - if it raises an exception, it will kill the persistent mapping.
procedure
p : persistent-mapping?
procedure
p : persistent-mapping?
procedure
p : persistent-mapping?
struct
(struct persistent-mapping (thread) #:extra-constructor-name make-persistent-mapping #:prefab) thread : thread?