2014-11-23 03:25:09 +00:00
|
|
|
// Emacs style mode select -*- C++ -*-
|
2011-06-25 11:28:38 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// $Id$
|
|
|
|
|
//
|
2026-01-05 01:52:47 +00:00
|
|
|
// Copyright (C) 2006-2026 by The Odamex Team.
|
2011-06-25 11:28:38 +00:00
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// DESCRIPTION:
|
2014-11-23 03:25:09 +00:00
|
|
|
// Utility functions
|
2011-06-25 11:28:38 +00:00
|
|
|
//
|
2014-11-23 03:25:09 +00:00
|
|
|
// AUTHORS:
|
2011-06-25 11:28:38 +00:00
|
|
|
// Russell Rice (russell at odamex dot net)
|
|
|
|
|
// Michael Wood (mwoodj at knology dot net)
|
|
|
|
|
//
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2011-06-27 06:48:13 +00:00
|
|
|
#include <iostream>
|
2015-05-03 23:10:50 +00:00
|
|
|
|
2025-04-09 14:42:08 -05:00
|
|
|
#ifdef _WIN32
|
2020-10-19 21:37:16 -04:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
|
#define NOMINMAX
|
|
|
|
|
#include <WinSock2.h>
|
|
|
|
|
#include <Windows.h>
|
2015-05-03 23:10:50 +00:00
|
|
|
#else
|
2011-06-25 11:28:38 +00:00
|
|
|
#include <sys/time.h>
|
2015-05-03 23:10:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef max
|
|
|
|
|
#undef max
|
|
|
|
|
#endif
|
2011-06-25 11:28:38 +00:00
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
|
|
#include "net_utils.h"
|
|
|
|
|
|
2014-11-23 03:25:09 +00:00
|
|
|
namespace odalpapi
|
|
|
|
|
{
|
2012-04-15 23:53:53 +00:00
|
|
|
|
2020-10-19 21:37:16 -04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
|
|
|
|
|
/* FILETIME of Jan 1 1970 00:00:00. */
|
|
|
|
|
static const unsigned __int64 epoch = ((unsigned __int64)116444736000000000ULL);
|
|
|
|
|
|
|
|
|
|
int gettimeofday(struct timeval* tp, ...)
|
|
|
|
|
{
|
|
|
|
|
FILETIME file_time;
|
|
|
|
|
SYSTEMTIME system_time;
|
|
|
|
|
ULARGE_INTEGER ularge;
|
|
|
|
|
|
|
|
|
|
GetSystemTime(&system_time);
|
|
|
|
|
SystemTimeToFileTime(&system_time, &file_time);
|
|
|
|
|
ularge.LowPart = file_time.dwLowDateTime;
|
|
|
|
|
ularge.HighPart = file_time.dwHighDateTime;
|
|
|
|
|
|
|
|
|
|
tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
|
|
|
|
|
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-06-25 11:28:38 +00:00
|
|
|
// GetMillisNow()
|
|
|
|
|
|
|
|
|
|
// denis - use this unless you want your program
|
|
|
|
|
// to get confused every 49 days due to DWORD limit
|
|
|
|
|
uint64_t _UnwrapTime(uint32_t now32)
|
|
|
|
|
{
|
|
|
|
|
static uint64_t last = 0;
|
|
|
|
|
uint64_t now = now32;
|
2011-10-18 08:18:24 +00:00
|
|
|
static uint64_t max = std::numeric_limits<uint32_t>::max();
|
2011-06-25 11:28:38 +00:00
|
|
|
|
|
|
|
|
if(now < last%max)
|
|
|
|
|
last += (max-(last%max)) + now;
|
|
|
|
|
else
|
|
|
|
|
last = now;
|
|
|
|
|
|
|
|
|
|
return last;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t _Millis()
|
|
|
|
|
{
|
2014-11-23 03:25:09 +00:00
|
|
|
struct timeval tp;
|
|
|
|
|
|
|
|
|
|
gettimeofday(&tp, (struct timezone*)NULL);
|
2011-06-25 11:28:38 +00:00
|
|
|
|
2014-11-23 03:25:09 +00:00
|
|
|
return (tp.tv_sec * 1000 + (tp.tv_usec / 1000));
|
2011-06-25 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t GetMillisNow()
|
|
|
|
|
{
|
2014-11-23 03:25:09 +00:00
|
|
|
return _UnwrapTime(_Millis());
|
2011-06-25 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 14:42:08 -05:00
|
|
|
int32_t OdaAddrToComponents(const std::string& HostPort, std::string &AddrOut,
|
2015-08-16 00:21:11 +00:00
|
|
|
uint16_t &PortOut)
|
|
|
|
|
{
|
|
|
|
|
size_t colon;
|
|
|
|
|
|
|
|
|
|
if (HostPort.empty())
|
|
|
|
|
return 1;
|
2025-04-09 14:42:08 -05:00
|
|
|
|
2015-08-16 00:21:11 +00:00
|
|
|
colon = HostPort.find(':');
|
|
|
|
|
|
|
|
|
|
if(colon != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
long tmp_port;
|
|
|
|
|
|
|
|
|
|
if(colon + 1 >= HostPort.length())
|
|
|
|
|
return 2;
|
2025-04-09 14:42:08 -05:00
|
|
|
|
2015-08-16 00:21:11 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
std::istringstream(HostPort.substr(colon + 1)) >> tmp_port;
|
|
|
|
|
PortOut = tmp_port;
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddrOut = HostPort;
|
2025-04-09 14:42:08 -05:00
|
|
|
|
2015-08-16 00:21:11 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2025-04-09 14:42:08 -05:00
|
|
|
|
2015-08-16 00:21:11 +00:00
|
|
|
AddrOut = HostPort.substr(0, colon);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-04-15 23:53:53 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2011-06-25 11:28:38 +00:00
|
|
|
// ???
|
|
|
|
|
// ---
|