From e17b206b837391bb492258c79861d7933b422125 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sat, 17 Jun 2023 10:48:22 +0200 Subject: [PATCH] Fix invalid use of inet_ntoa inet_ntoa returns a pointer to a fixed buffer. If called multiple times during the evaluating of an expression the buffer will be overwritten and the addresses will appear duplicated. --- src/TraceroutePacket.cpp | 14 ++++++++------ src/TraceroutePacket.h | 8 ++++---- src/TracerouteReader.cpp | 5 +---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/TraceroutePacket.cpp b/src/TraceroutePacket.cpp index 49f6af4..063175a 100644 --- a/src/TraceroutePacket.cpp +++ b/src/TraceroutePacket.cpp @@ -28,16 +28,18 @@ */ #include "TraceroutePacket.h" +#include +#include -TraceroutePacket::TraceroutePacket(char* localSourceAddress, char* localDestinationAddress, - u_int16_t localSourcePort, u_int16_t localDestinationPort) +TraceroutePacket::TraceroutePacket(struct sockaddr_in *remoteAddress, struct sockaddr_in *localAddress) { - sourceAddress = localSourceAddress; - sourcePort = localSourcePort; - destinationAddress = localDestinationAddress; - destinationPort = localDestinationPort; + sourcePort = ntohs(remoteAddress->sin_port); + destinationPort = ntohs(localAddress->sin_port); + inet_ntop(AF_INET, &remoteAddress->sin_addr, sourceAddress, sizeof(sourceAddress)); + inet_ntop(AF_INET, &localAddress->sin_addr, destinationAddress, sizeof(destinationAddress)); } + char* TraceroutePacket::getSourceAddress(void) { return sourceAddress; } diff --git a/src/TraceroutePacket.h b/src/TraceroutePacket.h index 94d5e6e..70d640a 100644 --- a/src/TraceroutePacket.h +++ b/src/TraceroutePacket.h @@ -31,12 +31,13 @@ #define TRACEROUTE_PACKET_H #include +#include class TraceroutePacket { private: - char* sourceAddress; - char* destinationAddress; + char sourceAddress[INET_ADDRSTRLEN]; + char destinationAddress[INET_ADDRSTRLEN]; u_int16_t sourcePort; u_int16_t destinationPort; @@ -44,8 +45,7 @@ class TraceroutePacket { /* void setSourceAddress(char* localSourceAddress); */ /* void setSourcePort(u_int16_t localSourcePort); */ /* void setDestinationPort(u_int16_t localDestinationPort); */ - TraceroutePacket(char* sourceAddress, char* destinationAddress, - u_int16_t sourcePort, u_int16_t destinationPort); + TraceroutePacket(struct sockaddr_in *remoteAddress, struct sockaddr_in *localAddress); char* getSourceAddress(void); char* getDestinationAddress(void); u_int16_t getSourcePort(void); diff --git a/src/TracerouteReader.cpp b/src/TracerouteReader.cpp index 3adcb13..6bf2366 100644 --- a/src/TracerouteReader.cpp +++ b/src/TracerouteReader.cpp @@ -76,8 +76,5 @@ class TraceroutePacket* TracerouteReader::readPacket(int fd) { } } - return new TraceroutePacket(inet_ntoa(remoteAddress.sin_addr), - inet_ntoa(localAddress.sin_addr), - ntohs(remoteAddress.sin_port), - ntohs(localAddress.sin_port)); + return new TraceroutePacket(&remoteAddress, &localAddress); }