aboutsummaryrefslogtreecommitdiffstats
path: root/common/network
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2023-01-13 11:14:21 +0100
committerPierre Ossman <ossman@cendio.se>2023-02-04 14:03:13 +0100
commit5c1ac16776cd9f2a75e31086755e72186b3b3d41 (patch)
tree234a54c7f11c25ba59f8487118d13eb550c1cc64 /common/network
parent1af2a56f7583b301890f8ea213f262782cc3c9f1 (diff)
downloadtigervnc-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')
-rw-r--r--common/network/Socket.h4
-rw-r--r--common/network/TcpSocket.cxx35
-rw-r--r--common/network/TcpSocket.h4
-rw-r--r--common/network/UnixSocket.cxx16
-rw-r--r--common/network/UnixSocket.h4
5 files changed, 30 insertions, 33 deletions
diff --git a/common/network/Socket.h b/common/network/Socket.h
index 901bab13..117851c1 100644
--- a/common/network/Socket.h
+++ b/common/network/Socket.h
@@ -49,8 +49,8 @@ namespace network {
void cork(bool enable) { outstream->cork(enable); }
// information about the remote end of the socket
- virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
- virtual char* getPeerEndpoint() = 0; // <address>::<port>
+ virtual const char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
+ virtual const char* getPeerEndpoint() = 0; // <address>::<port>
// Was there a "?" in the ConnectionFilter used to accept this Socket?
void setRequiresQuery();
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;
}
diff --git a/common/network/TcpSocket.h b/common/network/TcpSocket.h
index 787de629..d008f194 100644
--- a/common/network/TcpSocket.h
+++ b/common/network/TcpSocket.h
@@ -55,8 +55,8 @@ namespace network {
TcpSocket(int sock);
TcpSocket(const char *name, int port);
- virtual char* getPeerAddress();
- virtual char* getPeerEndpoint();
+ virtual const char* getPeerAddress();
+ virtual const char* getPeerEndpoint();
protected:
bool enableNagles(bool enable);
diff --git a/common/network/UnixSocket.cxx b/common/network/UnixSocket.cxx
index 3c1443f8..e7793849 100644
--- a/common/network/UnixSocket.cxx
+++ b/common/network/UnixSocket.cxx
@@ -74,8 +74,8 @@ UnixSocket::UnixSocket(const char *path)
setFd(sock);
}
-char* UnixSocket::getPeerAddress() {
- struct sockaddr_un addr;
+const char* UnixSocket::getPeerAddress() {
+ static struct sockaddr_un addr;
socklen_t salen;
// AF_UNIX only has a single address (the server side).
@@ -85,27 +85,27 @@ char* UnixSocket::getPeerAddress() {
salen = sizeof(addr);
if (getpeername(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
vlog.error("unable to get peer name for socket");
- return rfb::strDup("");
+ return "";
}
if (salen > offsetof(struct sockaddr_un, sun_path))
- return rfb::strDup(addr.sun_path);
+ return addr.sun_path;
salen = sizeof(addr);
if (getsockname(getFd(), (struct sockaddr *)&addr, &salen) != 0) {
vlog.error("unable to get local name for socket");
- return rfb::strDup("");
+ return "";
}
if (salen > offsetof(struct sockaddr_un, sun_path))
- return rfb::strDup(addr.sun_path);
+ return addr.sun_path;
// socketpair() will create unnamed sockets
- return rfb::strDup("(unnamed UNIX socket)");
+ return "(unnamed UNIX socket)";
}
-char* UnixSocket::getPeerEndpoint() {
+const char* UnixSocket::getPeerEndpoint() {
return getPeerAddress();
}
diff --git a/common/network/UnixSocket.h b/common/network/UnixSocket.h
index d7c70005..e66afcd1 100644
--- a/common/network/UnixSocket.h
+++ b/common/network/UnixSocket.h
@@ -38,8 +38,8 @@ namespace network {
UnixSocket(int sock);
UnixSocket(const char *name);
- virtual char* getPeerAddress();
- virtual char* getPeerEndpoint();
+ virtual const char* getPeerAddress();
+ virtual const char* getPeerEndpoint();
};
class UnixListener : public SocketListener {