pOnvif: use the reached address for services reported on the LAN side of a NAT #25

Merged
ClaudeAI merged 1 commits from fix/onvif-nat-xaddr into master 54 minutes ago
  1. 70
      lib/data/ponvif/ponvif.cpp
  2. 1
      lib/data/ponvif/ponvif.h

70
lib/data/ponvif/ponvif.cpp

@ -7,6 +7,7 @@
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */ #include <sys/types.h> /* See NOTES */
#include <sys/types.h> #include <sys/types.h>
@ -2287,6 +2288,54 @@ accessToken pOnvif::_makeToken(void)
return _passwordDigest(m_pUsername, m_pPassword, tTS); 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) void pOnvif::_getOnvifVersion(nodeCapability & caps)
{ {
if (caps.Device != NULL) 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 // Only use service addresses the device actually reported
if (caps.PTZ != NULL && caps.PTZ->XAddr[0] != (char) 0) if (caps.PTZ != NULL && caps.PTZ->XAddr[0] != (char) 0)
{ {

1
lib/data/ponvif/ponvif.h

@ -520,6 +520,7 @@ class pOnvif
private: private:
void cleanNodeCapability( nodeCapability *ptr ); void cleanNodeCapability( nodeCapability *ptr );
void setGenericSpeedSpaces(void); void setGenericSpeedSpaces(void);
bool reachableXAddr(char * xaddr, size_t size); // LAN service address -> reached address (NAT)
curlRequestsManager * m_pHttpAccessManager; curlRequestsManager * m_pHttpAccessManager;
basicMutex m_mResponseProtector; basicMutex m_mResponseProtector;
CURL * m_pCURL; CURL * m_pCURL;

Loading…
Cancel
Save