diff --git a/lib/data/ponvif/ponvif.cpp b/lib/data/ponvif/ponvif.cpp index 56132f3d..4c842cb5 100644 --- a/lib/data/ponvif/ponvif.cpp +++ b/lib/data/ponvif/ponvif.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include /* See NOTES */ #include @@ -2287,6 +2288,54 @@ accessToken pOnvif::_makeToken(void) return _passwordDigest(m_pUsername, m_pPassword, tTS); } +/* + A camera behind NAT / port forwarding reports its services at its LAN address, e.g. + http://192.168.200.118:8080/onvif/media while it is reached as 2.54.237.28:8080: GetVideoSources + went to an address that cannot be reached ("pOnvif CREATE FAILURE!"). A service address on another + host than the reached one is moved to the reached host:port, keeping its path. Same host: unchanged. +*/ +static std::string onvifHostOnly(const std::string & hostPort) +{ + if (! hostPort.empty() && hostPort[0] == '[') // [IPv6]:port + { + size_t e = hostPort.find(']'); + return (e == std::string::npos) ? hostPort : hostPort.substr(0, e + 1); + } + size_t c = hostPort.rfind(':'); + return (c == std::string::npos) ? hostPort : hostPort.substr(0, c); +} + +bool pOnvif::reachableXAddr(char * xaddr, size_t size) +{ + if (xaddr == NULL || xaddr[0] == (char) 0 || m_pIpAddress == NULL || m_pIpAddress[0] == (char) 0) + { + return false; + } + const char * tHost = strstr(xaddr, "://"); + if (tHost == NULL) + { + return false; + } + tHost = tHost + 3; + const char * tPath = strchr(tHost, '/'); + std::string tXHost = onvifHostOnly(std::string(tHost, tPath ? (size_t) (tPath - tHost) : strlen(tHost))); + std::string tRHost = onvifHostOnly(std::string(m_pIpAddress)); + if (strcasecmp(tXHost.c_str(), tRHost.c_str()) == 0) + { + return false; + } + std::string tNew = std::string("http://") + m_pIpAddress + (tPath ? tPath : "/"); + if (tNew.size() >= size) + { + return false; + } + char tB[CTEXT * 2 + 48]; + snprintf(tB, sizeof(tB), "Service address %.*s -> %s (device reached at another address: NAT)", (int) CTEXT, xaddr, tNew.c_str()); + LOG(tB); + memcpy(xaddr, tNew.c_str(), tNew.size() + 1); + return true; +} + void pOnvif::_getOnvifVersion(nodeCapability & caps) { if (caps.Device != NULL) @@ -2300,6 +2349,27 @@ void pOnvif::_getOnvifVersion(nodeCapability & caps) }; }; }; + // Service addresses on the LAN side of a NAT -> the address the device is reached at + if (caps.PTZ != NULL) + { + reachableXAddr(caps.PTZ->XAddr, sizeof(caps.PTZ->XAddr)); + }; + if (caps.Media != NULL) + { + reachableXAddr(caps.Media->XAddr, sizeof(caps.Media->XAddr)); + }; + if (caps.Device != NULL) + { + reachableXAddr(caps.Device->XAddr, sizeof(caps.Device->XAddr)); + }; + if (caps.Imaging != NULL) + { + reachableXAddr(caps.Imaging->XAddr, sizeof(caps.Imaging->XAddr)); + }; + if (caps.Events != NULL) + { + reachableXAddr(caps.Events->XAddr, sizeof(caps.Events->XAddr)); + }; // Only use service addresses the device actually reported if (caps.PTZ != NULL && caps.PTZ->XAddr[0] != (char) 0) { diff --git a/lib/data/ponvif/ponvif.h b/lib/data/ponvif/ponvif.h index 37f706f6..13444944 100644 --- a/lib/data/ponvif/ponvif.h +++ b/lib/data/ponvif/ponvif.h @@ -520,6 +520,7 @@ class pOnvif private: void cleanNodeCapability( nodeCapability *ptr ); void setGenericSpeedSpaces(void); + bool reachableXAddr(char * xaddr, size_t size); // LAN service address -> reached address (NAT) curlRequestsManager * m_pHttpAccessManager; basicMutex m_mResponseProtector; CURL * m_pCURL;