natpmp: fix error handling and don't die on errors in getpubliaddress

This fixes a case where the natpmp async could pure () instead of
recursing, which would lock ames. This also adds some more error
handling on the haskell side of things and disables the raw fprintfs
in the c bindings layer.
This commit is contained in:
Elliot Glaysher 2020-08-19 12:53:59 -04:00
parent 5c7f846d71
commit 1401d7bc5c
2 changed files with 18 additions and 11 deletions

View File

@ -55,22 +55,22 @@ int readNatResponseSynchronously(natpmp_t* natpmp, natpmpresp_t * response)
r = select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
sav_errno = errno;
if(r<0) {
fprintf(stderr, "select(): errno=%d '%s'\n",
sav_errno, strerror(sav_errno));
/* fprintf(stderr, "select(): errno=%d '%s'\n", */
/* sav_errno, strerror(sav_errno)); */
return 1;
}
r = readnatpmpresponseorretry(natpmp, response);
sav_errno = errno;
/* printf("readnatpmpresponseorretry returned %d (%s)\n", */
/* r, r==0?"OK":(r==NATPMP_TRYAGAIN?"TRY AGAIN":"FAILED")); */
if(r<0 && r!=NATPMP_TRYAGAIN) {
#ifdef ENABLE_STRNATPMPERR
fprintf(stderr, "readnatpmpresponseorretry() failed : %s\n",
strnatpmperr(r));
#endif
fprintf(stderr, " errno=%d '%s'\n",
sav_errno, strerror(sav_errno));
}
/* if(r<0 && r!=NATPMP_TRYAGAIN) { */
/* #ifdef ENABLE_STRNATPMPERR */
/* fprintf(stderr, "readnatpmpresponseorretry() failed : %s\n", */
/* strnatpmperr(r)); */
/* #endif */
/* fprintf(stderr, " errno=%d '%s'\n", */
/* sav_errno, strerror(sav_errno)); */
/* } */
} while(r==NATPMP_TRYAGAIN);
return r;

View File

@ -99,6 +99,8 @@ portThread :: forall e. (HasLogFunc e)
-> RIO e ()
portThread q stderr = do
initNatPmp >>= \case
Left ErrCannotGetGateway -> do
assumeOnPublicInternet
Left err -> do
likelyIPAddress >>= \case
Just ip@(192, 168, _, _) -> warnBehindRouterAndErr ip err
@ -123,7 +125,12 @@ portThread q stderr = do
foundRouter :: NatPmpHandle -> RIO e ()
foundRouter pmp = do
getPublicAddress pmp >>= \case
Left _ -> pure ()
Left ErrCannotGetGateway -> assumeOnPublicInternet
Left ErrNoGatewaySupport -> assumeOnPublicInternet
Left err -> do
stderr $ "port: received error when asking router for public ip: " ++
(tshow err)
loopErr q
Right addr -> do
let (a, b, c, d) = hostAddressToTuple addr
stderr $ "port: router reports that our public IP is " ++ (tshow a) ++