add a function to get the local hostname

This commit is contained in:
Tim Felgentreff 2020-12-21 20:40:08 +01:00
parent 1e43f7b17a
commit a76d96f716
2 changed files with 22 additions and 0 deletions

View file

@ -114,6 +114,8 @@ extern void NetExit();
extern unsigned long NetResolveHost(const std::string &host);
/// Get local IPs
extern int NetSocketAddr(unsigned long *ips, int maxAddr);
/// Get local hostname
extern std::string NetGetHostname();
/// Open a UDP Socket port. (param in network format)
extern Socket NetOpenUDP(unsigned long ip, int port);

View file

@ -206,6 +206,26 @@ unsigned long NetResolveHost(const std::string &host)
return INADDR_NONE;
}
/**
* Return the system hostname.
*/
std::string NetGetHostname()
{
char hostname_buffer[256];
#ifdef _WIN32
DWORD hostname_size = (DWORD)sizeof(hostname_buffer);
if (GetComputerNameA(hostname_buffer, &hostname_size)) {
return std::string(hostname_buffer);
}
#else
size_t hostname_size = sizeof(hostname_buffer);
if (gethostname(hostname_buffer, hostname_size) == 0) {
return std::string(hostname_buffer);
}
#endif
return "localhost";
}
/**
** Get IP-addrs of local interfaces from Network file descriptor
**