Skip to content

King should open ames ports via NAT-PMP#3261

Merged
eglaysher merged 20 commits into
release/next-verefrom
king-natpmp
Aug 17, 2020
Merged

King should open ames ports via NAT-PMP#3261
eglaysher merged 20 commits into
release/next-verefrom
king-natpmp

Conversation

@eglaysher

Copy link
Copy Markdown
Contributor

This vendors libnatpmp and writes Haskell bindings to it. It then adds support to king haskell to use this to open up the ames port to the world. This way, people behind consumer grade residential routers should be able to get direct routes.

This is on by default, but can be disabled in hosting contexts with --no-port-forwarding.

This patch also does a minor reorganization of the RIO environments, adding a middle "Running" environment between KingEnv and PierEnv, address some TODOs. I did this since this was the natural place to also put a PortControllerApi.

@joemfb: concept, along with bindings.c, the one new C file I added (though it's mostly code adapted from libnatpmp's test suite).
@pilfer-pandex: all the haskell code here. This is the first time I've written any hs2c or used the FFI.

(Not yet hooked up to anything.)
There was a TODO in runShips about how the different layers of the
RIO environment had to be changed, so that there was a layer between
KingEnv and PierEnv for things shared between individual Piers, but
which weren't used outside of any PierEnv. This addresses those
TODOs by creating RunningEnv, which for now just owns MultiEyreApi
and makes it so we don't have to pass the entire thing around
explicitly.

The IP handling stuff will go in RunningEnv in a following patch.
This sets up a separate thread to handle scheduling of lease
renewals.
Tested with a comet trying to receive traffic from a planet in the
cloud. (h/t ~master-morzod)
@tacryt-socryp

Copy link
Copy Markdown
Contributor

How widely supported is NATPMP as opposed to PCP? It appears this has been a successor protocol since 2013: https://tools.ietf.org/html/rfc6887

@eglaysher

Copy link
Copy Markdown
Contributor Author

I'm not aware of anyone that uses PCP. It's either NAT-PMP, or the older XML based UPnP IGD protocol. A quick peek at a two torrent clients show that neither supports PCP.

@eglaysher eglaysher changed the base branch from master to release/next-vere August 7, 2020 21:10
@eglaysher

Copy link
Copy Markdown
Contributor Author

Friendly ping.

@pilfer-pandex pilfer-pandex left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of comments, mostly nits.

Comment thread pkg/hs/natpmp-static/hsrc_lib/Network/NATPMP.hsc Outdated
Comment thread pkg/hs/natpmp-static/hsrc_lib/Network/NATPMP.hsc Outdated
Comment thread pkg/hs/natpmp-static/hsrc_lib/Network/NATPMP.hsc Outdated
Comment thread pkg/hs/natpmp-static/hsrc_lib/Network/NATPMP.hsc
Comment thread pkg/hs/natpmp-static/hsrc_lib/Network/NATPMP.hsc Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs
":", err, ", disabling NAT-PMP")
loopErr q
Right _ -> do
let filteredPort = filterPort p nextRenew

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this amount to quadratic behavior?

@eglaysher

Copy link
Copy Markdown
Contributor Author

Did most of the nits plus separated out the TQueue from the heap. The things I left unaddressed and unresolved were things I don't understand the point you're trying to make.

Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Left err -> do
ip <- likelyIPAddress
case ip of
Just (192, 168, c, d) -> do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. IPv4 allocates two blocks for private networks: 10/8 and 192.168/16. You handle only the second, but we should be handling both.

Comment thread pkg/hs/urbit-king/lib/Urbit/King/App.hs Outdated
Comment on lines +70 to +85
typedef struct {
uint16_t type; /* NATPMP_RESPTYPE_* */
uint16_t resultcode; /* NAT-PMP response code */
uint32_t epoch; /* Seconds since start of epoch */
union {
struct {
//in_addr_t addr;
struct in_addr addr;
} publicaddress;
struct {
uint16_t privateport;
uint16_t mappedpublicport;
uint32_t lifetime;
} newportmapping;
} pnu;
} natpmpresp_t;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a language that has a proper datatype description language

Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs
Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/urbit-king/test/AmesTests.hs

@joemfb joemfb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As requested, I've reviewed the high-level flow in haskell, and the binding.c. Everything LGTM.

Except for a couple nits (typo, and requested error printf):

Comment thread pkg/hs/urbit-king/lib/Urbit/Vere/Ports.hs Outdated
Comment thread pkg/hs/natpmp-static/cbits/binding.c Outdated
@eglaysher eglaysher requested a review from joemfb August 17, 2020 14:59
Comment thread pkg/hs/natpmp-static/cbits/binding.c
@eglaysher eglaysher requested a review from joemfb August 17, 2020 18:05

@joemfb joemfb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eglaysher eglaysher merged commit 5ee3284 into release/next-vere Aug 17, 2020
belisarius222 pushed a commit that referenced this pull request Dec 1, 2020
King should open ames ports via NAT-PMP
@tacryt-socryp tacryt-socryp deleted the king-natpmp branch April 30, 2021 22:24
pkova added a commit to urbit/vere that referenced this pull request Apr 24, 2024
Same as urbit/urbit#3261 but for vere. From what
I can tell this NAT-PMP stuff is fairly well supported by routers, works
on my machine at least.
pkova added a commit to urbit/vere that referenced this pull request May 10, 2024
Same as urbit/urbit#3261 but for vere. From what
I can tell this NAT-PMP stuff is fairly well supported by routers, works
on my machine at least.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants