Network: Remove static buffer form of NetworkAddress::GetAddressAsString

This is used in multiple threads concurrently
This commit is contained in:
Jonathan G Rennison
2020-05-06 23:23:03 +01:00
parent af09391bfb
commit ef7e658dee
7 changed files with 45 additions and 31 deletions

View File

@@ -91,7 +91,6 @@ public:
const char *GetHostname();
void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
const char *GetAddressAsString(bool with_family = true);
const sockaddr_storage *GetAddress();
/**
@@ -179,4 +178,17 @@ public:
static const char *AddressFamilyAsString(int family);
};
/**
* The use of a struct is so that when used as an argument to /seprintf/etc, the buffer lives
* on the stack with a lifetime which lasts until the end of the statement.
* This avoids using a static buffer which is thread-unsafe, or needing to call malloc, which would then nee to be freed.
*/
struct NetworkAddressDumper {
const char *GetAddressAsString(NetworkAddress *addr, bool with_family = true);
private:
/* 6 = for the : and 5 for the decimal port number */
char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7];
};
#endif /* NETWORK_CORE_ADDRESS_H */