diff options
author | Pierre Ossman <ossman@cendio.se> | 2023-01-13 11:14:21 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2023-02-04 14:03:13 +0100 |
commit | 5c1ac16776cd9f2a75e31086755e72186b3b3d41 (patch) | |
tree | 234a54c7f11c25ba59f8487118d13eb550c1cc64 /common/network/TcpSocket.cxx | |
parent | 1af2a56f7583b301890f8ea213f262782cc3c9f1 (diff) | |
download | tigervnc-5c1ac16776cd9f2a75e31086755e72186b3b3d41.tar.gz tigervnc-5c1ac16776cd9f2a75e31086755e72186b3b3d41.zip |
Return static char buffer from some methods
This mimics how some system functions (like inet_ntop()) work, and
avoids complexity around ownership of the returned string buffer.
The downside is that the string must be consumed directly as it will be
overwritten on the next call, but that is not an issue with the current
usage.
Diffstat (limited to 'common/network/TcpSocket.cxx')
-rw-r--r-- | common/network/TcpSocket.cxx | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/common/network/TcpSocket.cxx b/common/network/TcpSocket.cxx index cc82faef..5241d94a 100644 --- a/common/network/TcpSocket.cxx +++ b/common/network/TcpSocket.cxx @@ -211,17 +211,17 @@ TcpSocket::TcpSocket(const char *host, int port) enableNagles(false); } -char* TcpSocket::getPeerAddress() { +const char* TcpSocket::getPeerAddress() { vnc_sockaddr_t sa; socklen_t sa_size = sizeof(sa); if (getpeername(getFd(), &sa.u.sa, &sa_size) != 0) { vlog.error("unable to get peer name for socket"); - return rfb::strDup(""); + return ""; } if (sa.u.sa.sa_family == AF_INET6) { - char buffer[INET6_ADDRSTRLEN + 2]; + static char buffer[INET6_ADDRSTRLEN + 2]; int ret; buffer[0] = '['; @@ -231,12 +231,12 @@ char* TcpSocket::getPeerAddress() { NI_NUMERICHOST); if (ret != 0) { vlog.error("unable to convert peer name to a string"); - return rfb::strDup(""); + return ""; } strcat(buffer, "]"); - return rfb::strDup(buffer); + return buffer; } if (sa.u.sa.sa_family == AF_INET) { @@ -245,18 +245,18 @@ char* TcpSocket::getPeerAddress() { name = inet_ntoa(sa.u.sin.sin_addr); if (name == NULL) { vlog.error("unable to convert peer name to a string"); - return rfb::strDup(""); + return ""; } - return rfb::strDup(name); + return name; } vlog.error("unknown address family for socket"); - return rfb::strDup(""); + return ""; } -char* TcpSocket::getPeerEndpoint() { - rfb::CharArray address; address.buf = getPeerAddress(); +const char* TcpSocket::getPeerEndpoint() { + static char buffer[INET6_ADDRSTRLEN + 2 + 32]; vnc_sockaddr_t sa; socklen_t sa_size = sizeof(sa); int port; @@ -270,9 +270,8 @@ char* TcpSocket::getPeerEndpoint() { else port = 0; - int buflen = strlen(address.buf) + 32; - char* buffer = new char[buflen]; - sprintf(buffer, "%s::%d", address.buf, port); + sprintf(buffer, "%s::%d", getPeerAddress(), port); + return buffer; } @@ -569,33 +568,31 @@ patternMatchIP(const TcpFilter::Pattern& pattern, vnc_sockaddr_t *sa) { bool TcpFilter::verifyConnection(Socket* s) { - rfb::CharArray name; vnc_sockaddr_t sa; socklen_t sa_size = sizeof(sa); if (getpeername(s->getFd(), &sa.u.sa, &sa_size) != 0) return false; - name.buf = s->getPeerAddress(); std::list<TcpFilter::Pattern>::iterator i; for (i=filter.begin(); i!=filter.end(); i++) { if (patternMatchIP(*i, &sa)) { switch ((*i).action) { case Accept: - vlog.debug("ACCEPT %s", name.buf); + vlog.debug("ACCEPT %s", s->getPeerAddress()); return true; case Query: - vlog.debug("QUERY %s", name.buf); + vlog.debug("QUERY %s", s->getPeerAddress()); s->setRequiresQuery(); return true; case Reject: - vlog.debug("REJECT %s", name.buf); + vlog.debug("REJECT %s", s->getPeerAddress()); return false; } } } - vlog.debug("[REJECT] %s", name.buf); + vlog.debug("[REJECT] %s", s->getPeerAddress()); return false; } |