Use uint64_t for data usage statistics.

It’s not uncommon nowadays that a single connection uses more than 2GiB
of data. This bug was only affecting logging and did not affect proper
behaviour of the program.
This commit is contained in:
Sam Hocevar 2019-07-05 10:28:46 +02:00
parent d9f661ee5d
commit da497404ab
2 changed files with 22 additions and 23 deletions

View File

@ -530,8 +530,8 @@ static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket
if (RINETD_BUFFER_SIZE == socket->recvPos) { if (RINETD_BUFFER_SIZE == socket->recvPos) {
return; return;
} }
int got = recv(socket->fd, socket->buffer + socket->recvPos, int wanted = RINETD_BUFFER_SIZE - socket->recvPos;
RINETD_BUFFER_SIZE - socket->recvPos, 0); int got = recv(socket->fd, socket->buffer + socket->recvPos, wanted, 0);
if (got < 0) { if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) { if (GetLastError() == WSAEWOULDBLOCK) {
return; return;
@ -545,7 +545,7 @@ static void handleRead(ConnectionInfo *cnx, Socket *socket, Socket *other_socket
handleClose(cnx, socket, other_socket); handleClose(cnx, socket, other_socket);
return; return;
} }
socket->recvBytes += got; socket->totalBytesIn += got;
socket->recvPos += got; socket->recvPos += got;
} }
@ -556,7 +556,7 @@ static void handleUdpRead(ConnectionInfo *cnx, char const *buffer, int bytes)
? bytes : RINETD_BUFFER_SIZE - socket->recvPos; ? bytes : RINETD_BUFFER_SIZE - socket->recvPos;
if (got > 0) { if (got > 0) {
memcpy(socket->buffer + socket->recvPos, buffer, got); memcpy(socket->buffer + socket->recvPos, buffer, got);
socket->recvBytes += got; socket->totalBytesIn += got;
socket->recvPos += got; socket->recvPos += got;
} }
} }
@ -579,9 +579,9 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
addrlen = (SOCKLEN_T)sizeof(cnx->remoteAddress); addrlen = (SOCKLEN_T)sizeof(cnx->remoteAddress);
} }
int wanted = other_socket->recvPos - socket->sentPos;
int got = sendto(socket->fd, other_socket->buffer + socket->sentPos, int got = sendto(socket->fd, other_socket->buffer + socket->sentPos,
other_socket->recvPos - socket->sentPos, 0, wanted, 0, addr, addrlen);
addr, addrlen);
if (got < 0) { if (got < 0) {
if (GetLastError() == WSAEWOULDBLOCK) { if (GetLastError() == WSAEWOULDBLOCK) {
return; return;
@ -593,8 +593,8 @@ static void handleWrite(ConnectionInfo *cnx, Socket *socket, Socket *other_socke
return; return;
} }
socket->sentPos += got; socket->sentPos += got;
socket->sentBytes += got; socket->totalBytesOut += got;
if (socket->sentPos == other_socket->recvPos) { if (got == wanted) {
socket->sentPos = other_socket->recvPos = 0; socket->sentPos = other_socket->recvPos = 0;
} }
} }
@ -695,12 +695,12 @@ static void handleAccept(ServerInfo const *srv)
cnx->local.fd = INVALID_SOCKET; cnx->local.fd = INVALID_SOCKET;
cnx->local.proto = srv->toProto; cnx->local.proto = srv->toProto;
cnx->local.recvPos = cnx->local.sentPos = 0; cnx->local.recvPos = cnx->local.sentPos = 0;
cnx->local.recvBytes = cnx->local.sentBytes = 0; cnx->local.totalBytesIn = cnx->local.totalBytesOut = 0;
cnx->remote.fd = nfd; cnx->remote.fd = nfd;
cnx->remote.proto = srv->fromProto; cnx->remote.proto = srv->fromProto;
cnx->remote.recvPos = cnx->remote.sentPos = 0; cnx->remote.recvPos = cnx->remote.sentPos = 0;
cnx->remote.recvBytes = cnx->remote.sentBytes = 0; cnx->remote.totalBytesIn = cnx->remote.totalBytesOut = 0;
cnx->remoteAddress = *(struct sockaddr_in *)&addr; cnx->remoteAddress = *(struct sockaddr_in *)&addr;
if (srv->fromProto == protoUdp) if (srv->fromProto == protoUdp)
cnx->remoteTimeout = time(NULL) + srv->serverTimeout; cnx->remoteTimeout = time(NULL) + srv->serverTimeout;
@ -980,12 +980,11 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
strftime(tstr, sizeof(tstr), "%d/%b/%Y:%H:%M:%S ", t); strftime(tstr, sizeof(tstr), "%d/%b/%Y:%H:%M:%S ", t);
char const *addressText = "?"; char const *addressText = "?";
int bytesOutput = 0; int64_t bytesOut = 0, bytesIn = 0;
int bytesInput = 0;
if (cnx != NULL) { if (cnx != NULL) {
addressText = inet_ntoa(cnx->remoteAddress.sin_addr); addressText = inet_ntoa(cnx->remoteAddress.sin_addr);
bytesOutput = cnx->remote.sentBytes; bytesOut = cnx->remote.totalBytesOut;
bytesInput = cnx->remote.recvBytes; bytesIn = cnx->remote.totalBytesIn;
} }
char const *fromHost = "?", *toHost = "?"; char const *fromHost = "?", *toHost = "?";
@ -1018,7 +1017,7 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
fprintf(logFile, "%s - - " fprintf(logFile, "%s - - "
"[%s %c%.2d%.2d] " "[%s %c%.2d%.2d] "
"\"GET /rinetd-services/%s/%d/%s/%d/%s HTTP/1.0\" " "\"GET /rinetd-services/%s/%d/%s/%d/%s HTTP/1.0\" "
"200 %d - - - %d\n", "200 %llu - - - %llu\n",
addressText, addressText,
tstr, tstr,
sign, sign,
@ -1027,19 +1026,19 @@ static void logEvent(ConnectionInfo const *cnx, ServerInfo const *srv, int resul
fromHost, (int)fromPort, fromHost, (int)fromPort,
toHost, (int)toPort, toHost, (int)toPort,
logMessages[result], logMessages[result],
bytesOutput, (unsigned long long int)bytesOut,
bytesInput); (unsigned long long int)bytesIn);
} else { } else {
/* Write an rinetd-specific log entry with a /* Write an rinetd-specific log entry with a
less goofy format. */ less goofy format. */
fprintf(logFile, "%s\t%s\t%s\t%d\t%s\t%d\t%d" fprintf(logFile, "%s\t%s\t%s\t%d\t%s\t%d\t%llu"
"\t%d\t%s\n", "\t%llu\t%s\n",
tstr, tstr,
addressText, addressText,
fromHost, (int)fromPort, fromHost, (int)fromPort,
toHost, (int)toPort, toHost, (int)toPort,
bytesInput, (unsigned long long int)bytesIn,
bytesOutput, (unsigned long long int)bytesOut,
logMessages[result]); logMessages[result]);
} }
} }

View File

@ -56,9 +56,9 @@ struct _socket
SOCKET fd; SOCKET fd;
int proto; int proto;
/* recv: received on this socket /* recv: received on this socket
sent: sent to this socket from the other buffer */ sent: sent through this socket from the other buffer */
int recvPos, sentPos; int recvPos, sentPos;
int recvBytes, sentBytes; uint64_t totalBytesIn, totalBytesOut;
char *buffer; char *buffer;
}; };