1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 17:47:37 +00:00

refactor: use std::string in tr_net_strerror() (#2706)

This commit is contained in:
Charles Kerr 2022-02-24 18:53:01 -06:00 committed by GitHub
parent 943b57979f
commit e94ddc82fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 48 deletions

View file

@ -511,8 +511,7 @@ tr_socket_t tr_fdSocketCreate(tr_session* session, int domain, int type)
if ((s == TR_BAD_SOCKET) && (sockerrno != EAFNOSUPPORT))
{
char err_buf[512];
tr_logAddError(_("Couldn't create socket: %s"), tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_logAddError(_("Couldn't create socket: %s"), tr_net_strerror(sockerrno).c_str());
}
}

View file

@ -44,26 +44,19 @@ tr_address const tr_in6addr_any = { TR_AF_INET6, { IN6ADDR_ANY_INIT } };
tr_address const tr_inaddr_any = { TR_AF_INET, { { { { INADDR_ANY } } } } };
char* tr_net_strerror(char* buf, size_t buflen, int err)
std::string tr_net_strerror(int err)
{
*buf = '\0';
#ifdef _WIN32
DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, 0, buf, buflen, nullptr);
while (len > 0 && buf[len - 1] >= '\0' && buf[len - 1] <= ' ')
{
buf[--len] = '\0';
}
auto buf = std::array<char, 512>{};
auto const len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, 0, std::data(buf), std::size(buf), nullptr);
return std::string{ tr_strvStrip(std::data(buf)) };
#else
tr_strlcpy(buf, tr_strerror(err), buflen);
return std::string{ tr_strerror(err) };
#endif
return buf;
}
char const* tr_address_and_port_to_string(char* buf, size_t buflen, tr_address const* addr, tr_port port)
@ -242,9 +235,7 @@ void tr_netSetTOS([[maybe_unused]] tr_socket_t s, [[maybe_unused]] int tos, tr_a
if (setsockopt(s, IPPROTO_IP, IP_TOS, (void const*)&tos, sizeof(tos)) == -1)
{
char err_buf[512];
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno);
tr_logAddNamedInfo("Net", "Can't set TOS '%d': %s", tos, err_buf);
tr_logAddNamedInfo("Net", "Can't set TOS '%d': %s", tos, tr_net_strerror(sockerrno).c_str());
}
#endif
}
@ -253,9 +244,7 @@ void tr_netSetTOS([[maybe_unused]] tr_socket_t s, [[maybe_unused]] int tos, tr_a
#if defined(IPV6_TCLASS) && !defined(_WIN32)
if (setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, (void const*)&tos, sizeof(tos)) == -1)
{
char err_buf[512];
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno);
tr_logAddNamedInfo("Net", "Can't set IPv6 QoS '%d': %s", tos, err_buf);
tr_logAddNamedInfo("Net", "Can't set IPv6 QoS '%d': %s", tos, tr_net_strerror(sockerrno).c_str());
}
#endif
}
@ -272,12 +261,11 @@ void tr_netSetCongestionControl([[maybe_unused]] tr_socket_t s, [[maybe_unused]]
if (setsockopt(s, IPPROTO_TCP, TCP_CONGESTION, (void const*)algorithm, strlen(algorithm) + 1) == -1)
{
char err_buf[512];
tr_logAddNamedInfo(
"Net",
"Can't set congestion control algorithm '%s': %s",
algorithm,
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_net_strerror(sockerrno).c_str());
}
#endif
@ -338,7 +326,6 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
static int const domains[NUM_TR_AF_INET_TYPES] = { AF_INET, AF_INET6 };
struct sockaddr_storage sock;
struct sockaddr_storage source_sock;
char err_buf[512];
if (!tr_address_is_valid_for_peers(addr, port))
{
@ -361,7 +348,7 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
tr_logAddInfo(
"Unable to set SO_RCVBUF on socket %" PRIdMAX ": %s",
(intmax_t)s,
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_net_strerror(sockerrno).c_str());
}
}
@ -384,7 +371,7 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
_("Couldn't set source address %s on %" PRIdMAX ": %s"),
tr_address_to_string(source_addr),
(intmax_t)s,
tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_net_strerror(sockerrno).c_str());
tr_netClose(session, s);
return ret;
}
@ -405,7 +392,7 @@ struct tr_peer_socket tr_netOpenPeerSocket(tr_session* session, tr_address const
tr_address_to_string(addr),
(int)ntohs(port),
tmperrno,
tr_net_strerror(err_buf, sizeof(err_buf), tmperrno));
tr_net_strerror(tmperrno).c_str());
}
tr_netClose(session, s);
@ -517,8 +504,7 @@ static tr_socket_t tr_netBindTCPImpl(tr_address const* addr, tr_port port, bool
char const* const fmt = hint == nullptr ? _("Couldn't bind port %d on %s: %s") :
_("Couldn't bind port %d on %s: %s (%s)");
char err_buf[512];
tr_logAddError(fmt, port, tr_address_to_string(addr), tr_net_strerror(err_buf, sizeof(err_buf), err), hint);
tr_logAddError(fmt, port, tr_address_to_string(addr), tr_net_strerror(err).c_str(), hint);
}
tr_netCloseSocket(fd);

View file

@ -156,6 +156,6 @@ void tr_netSetTOS(tr_socket_t sock, int tos, tr_address_type type);
* @brief get a human-representable string representing the network error.
* @param err an errno on Unix/Linux and an WSAError on win32)
*/
char* tr_net_strerror(char* buf, size_t buflen, int err);
std::string tr_net_strerror(int err);
unsigned char const* tr_globalIPv6(tr_session const* session);

View file

@ -313,9 +313,7 @@ static void event_read_cb(evutil_socket_t fd, short /*event*/, void* vio)
what |= BEV_EVENT_ERROR;
}
char errstr[512];
tr_net_strerror(errstr, sizeof(errstr), e);
dbgmsg(io, "event_read_cb err: res:%d, what:%hd, errno:%d (%s)", res, what, e, errstr);
dbgmsg(io, "event_read_cb err: res:%d, what:%hd, errno:%d (%s)", res, what, e, tr_net_strerror(e).c_str());
if (io->gotError != nullptr)
{
@ -326,12 +324,10 @@ static void event_read_cb(evutil_socket_t fd, short /*event*/, void* vio)
static int tr_evbuffer_write(tr_peerIo* io, int fd, size_t howmuch)
{
char errstr[256];
EVUTIL_SET_SOCKET_ERROR(0);
int const n = evbuffer_write_atmost(io->outbuf, fd, howmuch);
int const e = EVUTIL_SOCKET_ERROR();
dbgmsg(io, "wrote %d to peer (%s)", n, (n == -1 ? tr_net_strerror(errstr, sizeof(errstr), e) : ""));
dbgmsg(io, "wrote %d to peer (%s)", n, (n == -1 ? tr_net_strerror(e).c_str() : ""));
return n;
}
@ -404,9 +400,8 @@ RESCHEDULE:
return;
FAIL:
char errstr[1024];
tr_net_strerror(errstr, sizeof(errstr), e);
dbgmsg(io, "event_write_cb got an error. res is %d, what is %hd, errno is %d (%s)", res, what, e, errstr);
auto const errmsg = tr_net_strerror(e);
dbgmsg(io, "event_write_cb got an err. res:%d, what:%hd, errno:%d (%s)", res, what, e, errmsg.c_str());
if (io->gotError != nullptr)
{
@ -1253,8 +1248,7 @@ static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
res = evbuffer_read(io->inbuf, io->socket.handle.tcp, (int)howmuch);
int const e = EVUTIL_SOCKET_ERROR();
char errstr[512];
dbgmsg(io, "read %d from peer (%s)", res, res == -1 ? tr_net_strerror(errstr, sizeof(errstr), e) : "");
dbgmsg(io, "read %d from peer (%s)", res, res == -1 ? tr_net_strerror(e).c_str() : "");
if (evbuffer_get_length(io->inbuf) != 0)
{
@ -1270,8 +1264,7 @@ static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
what |= BEV_EVENT_EOF;
}
tr_net_strerror(errstr, sizeof(errstr), e);
dbgmsg(io, "tr_peerIoTryRead err: res:%d what:%hd, errno:%d (%s)", res, what, e, errstr);
dbgmsg(io, "tr_peerIoTryRead err: res:%d what:%hd, errno:%d (%s)", res, what, e, tr_net_strerror(e).c_str());
io->gotError(io, what, io->userData);
}
@ -1319,11 +1312,9 @@ static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
if (n < 0 && io->gotError != nullptr && e != 0 && e != EPIPE && e != EAGAIN && e != EINTR && e != EINPROGRESS)
{
char errstr[512];
short const what = BEV_EVENT_WRITING | BEV_EVENT_ERROR;
tr_net_strerror(errstr, sizeof(errstr), e);
dbgmsg(io, "tr_peerIoTryWrite err: res:%d, what:%hd, errno:%d (%s)", n, what, e, errstr);
dbgmsg(io, "tr_peerIoTryWrite err: res:%d, what:%hd, errno:%d (%s)", n, what, e, tr_net_strerror(e).c_str());
io->gotError(io, what, io->userData);
}

View file

@ -37,14 +37,13 @@ static void set_socket_buffers(tr_socket_t fd, bool large)
int sbuf = 0;
socklen_t rbuf_len = sizeof(rbuf);
socklen_t sbuf_len = sizeof(sbuf);
char err_buf[512];
int size = large ? RECV_BUFFER_SIZE : SMALL_BUFFER_SIZE;
int rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&size), sizeof(size));
if (rc < 0)
{
tr_logAddNamedError("UDP", "Failed to set receive buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_logAddNamedError("UDP", "Failed to set receive buffer: %s", tr_net_strerror(sockerrno).c_str());
}
size = large ? SEND_BUFFER_SIZE : SMALL_BUFFER_SIZE;
@ -52,7 +51,7 @@ static void set_socket_buffers(tr_socket_t fd, bool large)
if (rc < 0)
{
tr_logAddNamedError("UDP", "Failed to set send buffer: %s", tr_net_strerror(err_buf, sizeof(err_buf), sockerrno));
tr_logAddNamedError("UDP", "Failed to set send buffer: %s", tr_net_strerror(sockerrno).c_str());
}
if (large)