Commit b08a72f0 authored by john selbie's avatar john selbie

first pass at modern c++ port

parent 4d96b141
...@@ -6,7 +6,7 @@ DEFINES := -DNDEBUG ...@@ -6,7 +6,7 @@ DEFINES := -DNDEBUG
# CLANG compiler works fine # CLANG compiler works fine
# CXX := /usr/bin/clang++ # CXX := /usr/bin/clang++
STANDARD_FLAGS := -Wall -Wuninitialized STANDARD_FLAGS := -Wall -Wuninitialized
RELEASE_FLAGS := -O2 RELEASE_FLAGS := -O2
DEBUG_FLAGS := -g DEBUG_FLAGS := -g
......
include ../common.inc include ../common.inc
PROJECT_TARGET := libcommon.a PROJECT_TARGET := libcommon.a
PROJECT_SRCS := atomichelpers.cpp cmdlineparser.cpp common.cpp fasthash.cpp getconsolewidth.cpp getmillisecondcounter.cpp logger.cpp prettyprint.cpp refcountobject.cpp stringhelper.cpp PROJECT_SRCS := atomichelpers.cpp cmdlineparser.cpp common.cpp crc32.cpp fasthash.cpp getconsolewidth.cpp getmillisecondcounter.cpp logger.cpp prettyprint.cpp stringhelper.cpp
PROJECT_OBJS := $(subst .cpp,.o,$(PROJECT_SRCS)) PROJECT_OBJS := $(subst .cpp,.o,$(PROJECT_SRCS))
INCLUDES := $(BOOST_INCLUDE) INCLUDES := $(BOOST_INCLUDE)
PRECOMP_H_GCH := commonincludes.hpp.gch PRECOMP_H_GCH := commonincludes.hpp.gch
......
...@@ -48,14 +48,12 @@ ...@@ -48,14 +48,12 @@
#include <math.h> #include <math.h>
#include <sys/termios.h> #include <sys/termios.h>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/scoped_ptr.hpp>
#include <map> #include <map>
#include <vector> #include <vector>
#include <list> #include <list>
#include <string> #include <string>
#include <memory>
#ifdef IS_LINUX #ifdef IS_LINUX
...@@ -130,13 +128,6 @@ inline void cta_noop(const char* psz) ...@@ -130,13 +128,6 @@ inline void cta_noop(const char* psz)
#include "chkmacros.h" #include "chkmacros.h"
// ---------------------------------------------
// Unless there's good reason, put additional header files after hresult.h and chkmacros.h
#include "refcountobject.h"
#include "objectfactory.h"
#include "logger.h" #include "logger.h"
......
#include "commonincludes.hpp"
/* ========================================================================
* Table of CRC-32's of all single-byte values (made by make_crc_table)
*/
static const uint32_t crc_table[256] = {
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
0x2d02ef8dL
};
/* ========================================================================= */
#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
#define DO2(buf) DO1(buf); DO1(buf);
#define DO4(buf) DO2(buf); DO2(buf);
#define DO8(buf) DO4(buf); DO4(buf);
/* ========================================================================= */
uint32_t crc32(uint32_t crc, uint8_t* buf, size_t len)
{
if (buf == NULL) return 0L;
crc = crc ^ 0xffffffffL;
while (len >= 8)
{
DO8(buf);
len -= 8;
}
if (len) do {
DO1(buf);
} while (--len);
return crc ^ 0xffffffffL;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: crc32.h
* Author: jselbie
*
* Created on September 3, 2018, 11:22 PM
*/
#ifndef CRC32_H
#define CRC32_H
uint32_t crc32(uint32_t crc, uint8_t* buf, size_t len);
#endif /* CRC32_H */
/*
Copyright 2011 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _OBJECTFACTORY_H
#define _OBJECTFACTORY_H
template <typename T>
class CObjectFactory
{
public:
template <typename X>
static HRESULT CreateInstanceNoInit(X** ppInstance)
{
T* pT = NULL;
if (ppInstance == NULL)
{
return E_INVALIDARG;
}
pT = new T();
if (pT == NULL)
{
return E_OUTOFMEMORY;
}
pT->AddRef();
*ppInstance = pT;
return S_OK;
}
template <typename I>
static HRESULT CreateInstance(I** ppI)
{
T* pInstance = NULL;
HRESULT hr = S_OK;
ChkIf(ppI == NULL, E_NOINTERFACE);
Chk(CreateInstanceNoInit(&pInstance));
Chk(pInstance->Initialize());
*ppI = pInstance;
pInstance = NULL;
Cleanup:
// Cleanup
if (pInstance)
{
pInstance->Release();
pInstance = NULL;
}
return hr;
}
template <typename A, typename I>
static HRESULT CreateInstance(A paramA, I** ppI)
{
T* pInstance = NULL;
HRESULT hr = S_OK;
ChkIf(ppI == NULL, E_NOINTERFACE);
Chk(CreateInstanceNoInit(&pInstance));
Chk(pInstance->Initialize(paramA));
*ppI = pInstance;
pInstance = NULL;
Cleanup:
// Cleanup
if (pInstance)
{
pInstance->Release();
pInstance = NULL;
}
return hr;
}
template <typename A, typename B, typename I>
static HRESULT CreateInstance(A paramA, B paramB, I** ppI)
{
T* pInstance = NULL;
HRESULT hr = S_OK;
ChkIf(ppI == NULL, E_NOINTERFACE);
Chk(CreateInstanceNoInit(&pInstance));
Chk(pInstance->Initialize(paramA, paramB));
*ppI = pInstance;
pInstance = NULL;
Cleanup:
// Cleanup
if (pInstance)
{
pInstance->Release();
pInstance = NULL;
}
return hr;
}
};
#endif /* _OBJECTFACTORY_H */
/*
Copyright 2011 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "commonincludes.hpp"
#include "refcountobject.h"
#include "atomichelpers.h"
CBasicRefCount::CBasicRefCount()
{
m_nRefs = 0;
}
CBasicRefCount::~CBasicRefCount()
{
;
}
int CBasicRefCount::InternalAddRef()
{
return AtomicIncrement(&m_nRefs);
}
int CBasicRefCount::InternalRelease()
{
int refcount = AtomicDecrement(&m_nRefs);
if (refcount == 0)
{
OnFinalRelease();
}
return refcount;
}
void CBasicRefCount::OnFinalRelease()
{
delete this;
}
/*
Copyright 2011 John Selbie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _REFCOUNTOBJECT_H
#define _REFCOUNTOBJECT_H
class IRefCounted
{
public:
virtual int AddRef() = 0;
virtual int Release() = 0;
};
class CBasicRefCount
{
protected:
int m_nRefs;
public:
CBasicRefCount();
virtual ~CBasicRefCount();
int InternalAddRef();
int InternalRelease();
virtual void OnFinalRelease();
};
#define ADDREF_AND_RELEASE_IMPL() \
inline int AddRef() {return InternalAddRef();} \
inline int Release() {return InternalRelease();}
template <class T>
class CRefCountedPtr
{
protected:
T* m_ptr;
public:
CRefCountedPtr() : m_ptr(NULL)
{
;
}
CRefCountedPtr(T* ptr) : m_ptr(ptr)
{
if (m_ptr)
{
m_ptr->AddRef();
}
}
CRefCountedPtr(const CRefCountedPtr<T>& sp) : m_ptr(sp.m_ptr)
{
if (m_ptr)
{
m_ptr->AddRef();
}
}
~CRefCountedPtr()
{
if (m_ptr)
{
m_ptr->Release();
m_ptr = NULL;
}
}
T* GetPointer()
{
return m_ptr;
}
operator T*() const
{
return m_ptr;
}
// std::vector chokes because it references &element to infer behavior
// Use GetPointerPointer instead
//T** operator&()
//{
// return &m_ptr;
//}
T** GetPointerPointer()
{
return &m_ptr;
}
T* operator->() const
{
return m_ptr;
}
T* operator = (T* ptr)
{
if (ptr)
{
ptr->AddRef();
}
if (m_ptr)
{
m_ptr->Release();
}
m_ptr = ptr;
return m_ptr;
}
T* operator = (const CRefCountedPtr<T>& sp)
{
if (sp.m_ptr)
{
sp.m_ptr->AddRef();
}
if (m_ptr)
{
m_ptr->Release();
}
m_ptr = sp.m_ptr;
return m_ptr;
}
bool operator ! () const
{
return (m_ptr == NULL);
}
bool operator != (T* ptr) const
{
return (ptr != m_ptr);
}
bool operator == (T* ptr) const
{
return (ptr == m_ptr);
}
// manual release
void ReleaseAndClear()
{
if (m_ptr)
{
m_ptr->Release();
m_ptr = NULL;
}
}
void Attach(T* ptr)
{
if (m_ptr)
{
m_ptr->Release();
}
m_ptr = ptr;
}
T* Detach()
{
T* ptr = m_ptr;
m_ptr = NULL;
return ptr;
}
HRESULT CopyTo(T** ppT)
{
if (ppT == NULL)
{
return E_POINTER;
}
*ppT = m_ptr;
if (m_ptr)
{
m_ptr->AddRef();
}
return S_OK;
}
};
#endif /* _REFCOUNTOBJECT_H */
...@@ -24,8 +24,6 @@ ...@@ -24,8 +24,6 @@
#ifdef HAS_EPOLL #ifdef HAS_EPOLL
class CEpoll : class CEpoll :
public CBasicRefCount,
public CObjectFactory<CEpoll>,
public IPolling public IPolling
{ {
private: private:
...@@ -36,8 +34,6 @@ private: ...@@ -36,8 +34,6 @@ private:
size_t _pendingCount; // number of valid events in _events size_t _pendingCount; // number of valid events in _events
size_t _currentEventIndex; // which one to process next size_t _currentEventIndex; // which one to process next
uint32_t ToNativeFlags(uint32_t eventflags); uint32_t ToNativeFlags(uint32_t eventflags);
uint32_t FromNativeFlags(uint32_t eventflags); uint32_t FromNativeFlags(uint32_t eventflags);
...@@ -51,8 +47,6 @@ public: ...@@ -51,8 +47,6 @@ public:
CEpoll(); CEpoll();
~CEpoll(); ~CEpoll();
ADDREF_AND_RELEASE_IMPL();
}; };
...@@ -243,8 +237,6 @@ Cleanup: ...@@ -243,8 +237,6 @@ Cleanup:
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
class CPoll : class CPoll :
public CBasicRefCount,
public CObjectFactory<CPoll>,
public IPolling public IPolling
{ {
private: private:
...@@ -272,8 +264,6 @@ public: ...@@ -272,8 +264,6 @@ public:
CPoll(); CPoll();
~CPoll(); ~CPoll();
ADDREF_AND_RELEASE_IMPL();
}; };
CPoll::CPoll() : CPoll::CPoll() :
...@@ -516,11 +506,10 @@ bool CPoll::FindNextEvent(PollEvent* pEvent) ...@@ -516,11 +506,10 @@ bool CPoll::FindNextEvent(PollEvent* pEvent)
HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPolling) HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, std::shared_ptr<IPolling>& spPolling)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
ChkIfA(ppPolling == NULL, E_INVALIDARG);
#ifdef HAS_EPOLL #ifdef HAS_EPOLL
if (type == IPOLLING_TYPE_BEST) if (type == IPOLLING_TYPE_BEST)
...@@ -540,12 +529,17 @@ HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPol ...@@ -540,12 +529,17 @@ HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPol
#ifndef HAS_EPOLL #ifndef HAS_EPOLL
ChkA(E_FAIL); ChkA(E_FAIL);
#else #else
ChkA(CEpoll::CreateInstance(maxSockets, ppPolling)); auto spEpoll = std::make_shared<CEpoll>();
ChkA(spEpoll->Initialize(maxSockets));
spPolling = spEpoll;
#endif #endif
} }
else if (type == IPOLLING_TYPE_POLL) else if (type == IPOLLING_TYPE_POLL)
{ {
ChkA(CPoll::CreateInstance(maxSockets, ppPolling));
auto spPoll = std::make_shared<CPoll>();
ChkA(spPoll->Initialize(maxSockets));
spPolling = spPoll;
} }
else else
{ {
...@@ -553,6 +547,7 @@ HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPol ...@@ -553,6 +547,7 @@ HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPol
} }
Cleanup: Cleanup:
return hr; return hr;
} }
...@@ -35,9 +35,10 @@ const uint32_t IPOLLING_PRI = 0x01 << 5; ...@@ -35,9 +35,10 @@ const uint32_t IPOLLING_PRI = 0x01 << 5;
const uint32_t IPOLLING_ERROR = 0x01 << 6; const uint32_t IPOLLING_ERROR = 0x01 << 6;
class IPolling : public IRefCounted class IPolling
{ {
public: public:
virtual ~IPolling() = default;
virtual HRESULT Initialize(size_t maxSockets) = 0; virtual HRESULT Initialize(size_t maxSockets) = 0;
virtual HRESULT Close() = 0; virtual HRESULT Close() = 0;
virtual HRESULT Add(int fd, uint32_t eventflags) = 0; virtual HRESULT Add(int fd, uint32_t eventflags) = 0;
...@@ -51,7 +52,7 @@ const uint32_t IPOLLING_TYPE_BEST = 0x01 << 0; ...@@ -51,7 +52,7 @@ const uint32_t IPOLLING_TYPE_BEST = 0x01 << 0;
const uint32_t IPOLLING_TYPE_EPOLL = 0x01 << 1; const uint32_t IPOLLING_TYPE_EPOLL = 0x01 << 1;
const uint32_t IPOLLING_TYPE_POLL = 0x01 << 2; const uint32_t IPOLLING_TYPE_POLL = 0x01 << 2;
HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, IPolling** ppPolling); HRESULT CreatePollingInstance(uint32_t type, size_t maxSockets, std::shared_ptr<IPolling>& spPolling);
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,6 @@
#ifndef STUNSOCKET_H #ifndef STUNSOCKET_H
#define STUNSOCKET_H #define STUNSOCKET_H
class CStunSocket class CStunSocket
{ {
private: private:
...@@ -41,7 +39,6 @@ private: ...@@ -41,7 +39,6 @@ private:
HRESULT SetV6Only(int sock); HRESULT SetV6Only(int sock);
public: public:
CStunSocket(); CStunSocket();
~CStunSocket(); ~CStunSocket();
...@@ -62,16 +59,11 @@ public: ...@@ -62,16 +59,11 @@ public:
HRESULT EnablePktInfoOption(bool fEnable); HRESULT EnablePktInfoOption(bool fEnable);
HRESULT SetNonBlocking(bool fEnable); HRESULT SetNonBlocking(bool fEnable);
void UpdateAddresses(); void UpdateAddresses();
HRESULT UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag); HRESULT UDPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
HRESULT TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag); HRESULT TCPInit(const CSocketAddress& local, SocketRole role, bool fSetReuseFlag);
}; };
typedef boost::shared_ptr<CStunSocket> CRefCountedStunSocket;
#endif /* STUNSOCKET_H */ #endif /* STUNSOCKET_H */
...@@ -671,11 +671,12 @@ void WaitForAppExitSignal() ...@@ -671,11 +671,12 @@ void WaitForAppExitSignal()
HRESULT StartUDP(CRefCountedPtr<CStunServer>& spServer, CStunServerConfig& config) HRESULT StartUDP(std::shared_ptr<CStunServer>& spServer, CStunServerConfig& config)
{ {
HRESULT hr; HRESULT hr;
hr = CStunServer::CreateInstance(config, spServer.GetPointerPointer()); auto spTemp = std::make_shared<CStunServer>();
hr = spTemp->Initialize(config);
if (FAILED(hr)) if (FAILED(hr))
{ {
Logging::LogMsg(LL_ALWAYS, "Unable to initialize UDP server (error code = x%x)", hr); Logging::LogMsg(LL_ALWAYS, "Unable to initialize UDP server (error code = x%x)", hr);
...@@ -683,22 +684,24 @@ HRESULT StartUDP(CRefCountedPtr<CStunServer>& spServer, CStunServerConfig& confi ...@@ -683,22 +684,24 @@ HRESULT StartUDP(CRefCountedPtr<CStunServer>& spServer, CStunServerConfig& confi
return hr; return hr;
} }
hr = spServer->Start(); hr = spTemp->Start();
if (FAILED(hr)) if (FAILED(hr))
{ {
Logging::LogMsg(LL_ALWAYS, "Unable to start UDP server (error code = x%x)", hr); Logging::LogMsg(LL_ALWAYS, "Unable to start UDP server (error code = x%x)", hr);
LogHR(LL_ALWAYS, hr); LogHR(LL_ALWAYS, hr);
return hr; return hr;
} }
spServer = spTemp;
return S_OK; return S_OK;
} }
HRESULT StartTCP(CRefCountedPtr<CTCPServer>& spTCPServer, CStunServerConfig& config) HRESULT StartTCP(std::shared_ptr<CTCPServer>& spTCPServer, CStunServerConfig& config)
{ {
HRESULT hr; HRESULT hr;
hr = CTCPServer::CreateInstance(config, spTCPServer.GetPointerPointer()); auto spTemp = std::make_shared<CTCPServer>();
hr = spTemp->Initialize(config);
if (FAILED(hr)) if (FAILED(hr))
{ {
Logging::LogMsg(LL_ALWAYS, "Unable to initialize TCP server (error code = x%x)", hr); Logging::LogMsg(LL_ALWAYS, "Unable to initialize TCP server (error code = x%x)", hr);
...@@ -706,7 +709,7 @@ HRESULT StartTCP(CRefCountedPtr<CTCPServer>& spTCPServer, CStunServerConfig& con ...@@ -706,7 +709,7 @@ HRESULT StartTCP(CRefCountedPtr<CTCPServer>& spTCPServer, CStunServerConfig& con
return hr; return hr;
} }
hr = spTCPServer->Start(); hr = spTemp->Start();
if (FAILED(hr)) if (FAILED(hr))
{ {
Logging::LogMsg(LL_ALWAYS, "Unable to start TCP server (error code = x%x)", hr); Logging::LogMsg(LL_ALWAYS, "Unable to start TCP server (error code = x%x)", hr);
...@@ -714,8 +717,8 @@ HRESULT StartTCP(CRefCountedPtr<CTCPServer>& spTCPServer, CStunServerConfig& con ...@@ -714,8 +717,8 @@ HRESULT StartTCP(CRefCountedPtr<CTCPServer>& spTCPServer, CStunServerConfig& con
return hr; return hr;
} }
spTCPServer = spTemp;
return S_OK; return S_OK;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
...@@ -725,11 +728,8 @@ int main(int argc, char** argv) ...@@ -725,11 +728,8 @@ int main(int argc, char** argv)
std::vector<StartupArgs> argsVector; std::vector<StartupArgs> argsVector;
int serverindex = 1; int serverindex = 1;
typedef CRefCountedPtr<CStunServer> UdpServerPtr; std::vector<std::shared_ptr<CStunServer>> udpServers;
typedef CRefCountedPtr<CTCPServer> TcpServerPtr; std::vector<std::shared_ptr<CTCPServer>> tcpServers;
std::vector<UdpServerPtr> udpServers;
std::vector<TcpServerPtr> tcpServers;
// block sigpipe so that socket send calls from raising SIGPIPE // block sigpipe so that socket send calls from raising SIGPIPE
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
...@@ -738,6 +738,8 @@ int main(int argc, char** argv) ...@@ -738,6 +738,8 @@ int main(int argc, char** argv)
// Block SIGTERM and SIGINT such that the child threads will never get that signal (so that subsequent WaitForAppExitSignal hooks on *this* thread) // Block SIGTERM and SIGINT such that the child threads will never get that signal (so that subsequent WaitForAppExitSignal hooks on *this* thread)
BlockSignal(SIGTERM); BlockSignal(SIGTERM);
BlockSignal(SIGINT); BlockSignal(SIGINT);
ASSERT(false);
#ifdef DEBUG #ifdef DEBUG
...@@ -786,7 +788,7 @@ int main(int argc, char** argv) ...@@ -786,7 +788,7 @@ int main(int argc, char** argv)
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
for (std::vector<StartupArgs>::iterator itor = argsVector.begin(); itor != argsVector.end(); itor++) for (auto itor = argsVector.begin(); itor != argsVector.end(); itor++)
{ {
CStunServerConfig config; CStunServerConfig config;
StartupArgs args = *itor; StartupArgs args = *itor;
...@@ -805,7 +807,7 @@ int main(int argc, char** argv) ...@@ -805,7 +807,7 @@ int main(int argc, char** argv)
if (config.fTCP) if (config.fTCP)
{ {
TcpServerPtr spTcpServer; std::shared_ptr<CTCPServer> spTcpServer;
hr = StartTCP(spTcpServer, config); hr = StartTCP(spTcpServer, config);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
...@@ -815,7 +817,7 @@ int main(int argc, char** argv) ...@@ -815,7 +817,7 @@ int main(int argc, char** argv)
} }
else else
{ {
UdpServerPtr spUdpServer; std::shared_ptr<CStunServer> spUdpServer;
hr = StartUDP(spUdpServer, config); hr = StartUDP(spUdpServer, config);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
...@@ -841,17 +843,17 @@ int main(int argc, char** argv) ...@@ -841,17 +843,17 @@ int main(int argc, char** argv)
Logging::LogMsg(LL_DEBUG, "Server is exiting"); Logging::LogMsg(LL_DEBUG, "Server is exiting");
for (std::vector<UdpServerPtr>::iterator itor = udpServers.begin(); itor != udpServers.end(); itor++) for (auto itor = udpServers.begin(); itor != udpServers.end(); itor++)
{ {
Logging::LogMsg(LL_DEBUG, "Shutting down UDP server"); Logging::LogMsg(LL_DEBUG, "Shutting down UDP server");
UdpServerPtr server = *itor; auto server = *itor;
server->Stop(); server->Stop();
} }
for (std::vector<TcpServerPtr>::iterator itor = tcpServers.begin(); itor != tcpServers.end(); itor++) for (auto itor = tcpServers.begin(); itor != tcpServers.end(); itor++)
{ {
Logging::LogMsg(LL_DEBUG, "Shutting down TCP server"); Logging::LogMsg(LL_DEBUG, "Shutting down TCP server");
TcpServerPtr server = *itor; auto server = *itor;
server->Stop(); server->Stop();
} }
......
...@@ -97,16 +97,16 @@ HRESULT CStunServer::Initialize(const CStunServerConfig& config) ...@@ -97,16 +97,16 @@ HRESULT CStunServer::Initialize(const CStunServerConfig& config)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
int socketcount = 0; int socketcount = 0;
CRefCountedPtr<IStunAuth> _spAuth; std::shared_ptr<IStunAuth> _spAuth;
TransportAddressSet tsa = {}; TransportAddressSet tsa = {};
boost::shared_ptr<RateLimiter> spLimiter; std::shared_ptr<RateLimiter> spLimiter;
// cleanup any thing that's going on now // cleanup any thing that's going on now
Shutdown(); Shutdown();
// optional code: create an authentication provider and initialize it here (if you want authentication) // optional code: create an authentication provider and initialize it here (if you want authentication)
// set the _spAuth member to reference it // set the _spAuth member to reference it
// Chk(CYourAuthProvider::CreateInstanceNoInit(&_spAuth)); // _spAuth = std::make_shared<CYourAuthProvider>();
// Create the sockets and initialize the TSA thing // Create the sockets and initialize the TSA thing
if (config.fHasPP) if (config.fHasPP)
...@@ -139,7 +139,7 @@ HRESULT CStunServer::Initialize(const CStunServerConfig& config) ...@@ -139,7 +139,7 @@ HRESULT CStunServer::Initialize(const CStunServerConfig& config)
{ {
Logging::LogMsg(LL_DEBUG, "Creating rate limiter for ddos protection\n"); Logging::LogMsg(LL_DEBUG, "Creating rate limiter for ddos protection\n");
// hard coding to 25000 ip addresses // hard coding to 25000 ip addresses
spLimiter = boost::shared_ptr<RateLimiter>(new RateLimiter(25000, config.fMultiThreadedMode)); spLimiter = std::shared_ptr<RateLimiter>(new RateLimiter(25000, config.fMultiThreadedMode));
} }
if (config.fMultiThreadedMode == false) if (config.fMultiThreadedMode == false)
...@@ -208,7 +208,7 @@ HRESULT CStunServer::Shutdown() ...@@ -208,7 +208,7 @@ HRESULT CStunServer::Shutdown()
} }
_threads.clear(); _threads.clear();
_spAuth.ReleaseAndClear(); _spAuth.reset();
return S_OK; return S_OK;
} }
......
...@@ -56,34 +56,26 @@ public: ...@@ -56,34 +56,26 @@ public:
}; };
class CStunServer : class CStunServer
public CBasicRefCount,
public CObjectFactory<CStunServer>,
public IRefCounted
{ {
private: private:
CStunSocket _arrSockets[4]; CStunSocket _arrSockets[4];
std::vector<CStunSocketThread*> _threads; std::vector<CStunSocketThread*> _threads;
CStunServer(); std::shared_ptr<IStunAuth> _spAuth;
~CStunServer();
friend class CObjectFactory<CStunServer>;
CRefCountedPtr<IStunAuth> _spAuth;
HRESULT AddSocket(TransportAddressSet* pTSA, SocketRole role, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise, bool fSetReuseFlag); HRESULT AddSocket(TransportAddressSet* pTSA, SocketRole role, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise, bool fSetReuseFlag);
public: public:
CStunServer();
~CStunServer();
HRESULT Initialize(const CStunServerConfig& config); HRESULT Initialize(const CStunServerConfig& config);
HRESULT Shutdown(); HRESULT Shutdown();
HRESULT Start(); HRESULT Start();
HRESULT Stop(); HRESULT Stop();
ADDREF_AND_RELEASE_IMPL();
}; };
......
...@@ -47,7 +47,7 @@ void CStunSocketThread::ClearSocketArray() ...@@ -47,7 +47,7 @@ void CStunSocketThread::ClearSocketArray()
_socks.clear(); _socks.clear();
} }
HRESULT CStunSocketThread::Init(CStunSocket* arrayOfFourSockets, TransportAddressSet* pTSA, IStunAuth* pAuth, SocketRole rolePrimaryRecv, boost::shared_ptr<RateLimiter>& spLimiter) HRESULT CStunSocketThread::Init(CStunSocket* arrayOfFourSockets, TransportAddressSet* pTSA, std::shared_ptr<IStunAuth> spAuth, SocketRole rolePrimaryRecv, std::shared_ptr<RateLimiter>& spLimiter)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
...@@ -94,7 +94,7 @@ HRESULT CStunSocketThread::Init(CStunSocket* arrayOfFourSockets, TransportAddres ...@@ -94,7 +94,7 @@ HRESULT CStunSocketThread::Init(CStunSocket* arrayOfFourSockets, TransportAddres
_rotation = 0; _rotation = 0;
_spAuth.Attach(pAuth); _spAuth = spAuth;
_spLimiter = spLimiter; _spLimiter = spLimiter;
...@@ -391,7 +391,7 @@ HRESULT CStunSocketThread::ProcessRequestAndSendResponse() ...@@ -391,7 +391,7 @@ HRESULT CStunSocketThread::ProcessRequestAndSendResponse()
// msgIn and msgOut are already initialized // msgIn and msgOut are already initialized
Chk(CStunRequestHandler::ProcessRequest(_msgIn, _msgOut, &_tsa, _spAuth)); Chk(CStunRequestHandler::ProcessRequest(_msgIn, _msgOut, &_tsa, _spAuth.get()));
ASSERT(_tsa.set[_msgOut.socketrole].fValid); ASSERT(_tsa.set[_msgOut.socketrole].fValid);
ASSERT(_arrSendSockets[_msgOut.socketrole].IsValid()); ASSERT(_arrSendSockets[_msgOut.socketrole].IsValid());
......
...@@ -33,7 +33,7 @@ public: ...@@ -33,7 +33,7 @@ public:
CStunSocketThread(); CStunSocketThread();
~CStunSocketThread(); ~CStunSocketThread();
HRESULT Init(CStunSocket* arrayOfFourSockets, TransportAddressSet* pTSA, IStunAuth* pAuth, SocketRole rolePrimaryRecv, boost::shared_ptr<RateLimiter>& _spRateLimiter); HRESULT Init(CStunSocket* arrayOfFourSockets, TransportAddressSet* pTSA, std::shared_ptr<IStunAuth> spAuth, SocketRole rolePrimaryRecv, std::shared_ptr<RateLimiter>& _spRateLimiter);
HRESULT Start(); HRESULT Start();
HRESULT SignalForStop(bool fPostMessages); HRESULT SignalForStop(bool fPostMessages);
...@@ -61,7 +61,7 @@ private: ...@@ -61,7 +61,7 @@ private:
TransportAddressSet _tsa; TransportAddressSet _tsa;
CRefCountedPtr<IStunAuth> _spAuth; std::shared_ptr<IStunAuth> _spAuth;
// pre-allocated objects for the thread // pre-allocated objects for the thread
CStunMessageReader _reader; CStunMessageReader _reader;
...@@ -71,7 +71,7 @@ private: ...@@ -71,7 +71,7 @@ private:
StunMessageIn _msgIn; StunMessageIn _msgIn;
StunMessageOut _msgOut; StunMessageOut _msgOut;
boost::shared_ptr<RateLimiter> _spLimiter; std::shared_ptr<RateLimiter> _spLimiter;
HRESULT InitThreadBuffers(); HRESULT InitThreadBuffers();
void UninitThreadBuffers(); void UninitThreadBuffers();
......
...@@ -46,7 +46,7 @@ CTCPStunThread::CTCPStunThread() ...@@ -46,7 +46,7 @@ CTCPStunThread::CTCPStunThread()
void CTCPStunThread::Reset() void CTCPStunThread::Reset()
{ {
_spPolling.ReleaseAndClear(); _spPolling.reset();
CloseListenSockets(); CloseListenSockets();
ClosePipes(); ClosePipes();
...@@ -56,7 +56,7 @@ void CTCPStunThread::Reset() ...@@ -56,7 +56,7 @@ void CTCPStunThread::Reset()
memset(&_tsaListen, '\0', sizeof(_tsaListen)); memset(&_tsaListen, '\0', sizeof(_tsaListen));
_fNeedToExit = false; _fNeedToExit = false;
_spAuth.ReleaseAndClear(); _spAuth.reset();
_role = RolePP; _role = RolePP;
memset(&_tsa, '\0', sizeof(_tsa)); memset(&_tsa, '\0', sizeof(_tsa));
...@@ -227,7 +227,7 @@ CStunSocket* CTCPStunThread::GetListenSocket(int sock) ...@@ -227,7 +227,7 @@ CStunSocket* CTCPStunThread::GetListenSocket(int sock)
HRESULT CTCPStunThread::Init(const TransportAddressSet& tsaListen, const TransportAddressSet& tsaHandler, IStunAuth* pAuth, int maxConnections, boost::shared_ptr<RateLimiter>& spLimiter) HRESULT CTCPStunThread::Init(const TransportAddressSet& tsaListen, const TransportAddressSet& tsaHandler, std::shared_ptr<IStunAuth> spAuth, int maxConnections, std::shared_ptr<RateLimiter>& spLimiter)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
int ret; int ret;
...@@ -255,14 +255,14 @@ HRESULT CTCPStunThread::Init(const TransportAddressSet& tsaListen, const Transpo ...@@ -255,14 +255,14 @@ HRESULT CTCPStunThread::Init(const TransportAddressSet& tsaListen, const Transpo
_tsaListen = tsaListen; _tsaListen = tsaListen;
_tsa = tsaHandler; _tsa = tsaHandler;
_spAuth.Attach(pAuth); _spAuth = spAuth;
ChkA(CreateListenSockets()); ChkA(CreateListenSockets());
ChkA(CreatePipes()); ChkA(CreatePipes());
// +5 for listening sockets and pipe // +5 for listening sockets and pipe
ChkA(CreatePollingInstance(IPOLLING_TYPE_BEST, (size_t)(_maxConnections + 5), _spPolling.GetPointerPointer())); ChkA(CreatePollingInstance(IPOLLING_TYPE_BEST, (size_t)(_maxConnections + 5), _spPolling));
// add listen socket to epoll // add listen socket to epoll
...@@ -611,7 +611,7 @@ HRESULT CTCPStunThread::ReceiveBytesForConnection(StunConnection* pConn) ...@@ -611,7 +611,7 @@ HRESULT CTCPStunThread::ReceiveBytesForConnection(StunConnection* pConn)
allowed_to_pass = this->RateCheck(msgIn.addrRemote); allowed_to_pass = this->RateCheck(msgIn.addrRemote);
ChkIf(allowed_to_pass == false, E_FAIL); ChkIf(allowed_to_pass == false, E_FAIL);
Chk(CStunRequestHandler::ProcessRequest(msgIn, msgOut, &_tsa, _spAuth)); Chk(CStunRequestHandler::ProcessRequest(msgIn, msgOut, &_tsa, _spAuth.get()));
// success - transition to the response state // success - transition to the response state
pConn->_state = ConnectionState_Transmitting; pConn->_state = ConnectionState_Transmitting;
...@@ -832,7 +832,7 @@ HRESULT CTCPServer::Initialize(const CStunServerConfig& config) ...@@ -832,7 +832,7 @@ HRESULT CTCPServer::Initialize(const CStunServerConfig& config)
HRESULT hr = S_OK; HRESULT hr = S_OK;
TransportAddressSet tsaListenAll; TransportAddressSet tsaListenAll;
TransportAddressSet tsaHandler; TransportAddressSet tsaHandler;
boost::shared_ptr<RateLimiter> spLimiter; std::shared_ptr<RateLimiter> spLimiter;
ChkIfA(_threads[0] != NULL, E_UNEXPECTED); // we can't already be initialized, right? ChkIfA(_threads[0] != NULL, E_UNEXPECTED); // we can't already be initialized, right?
...@@ -855,7 +855,7 @@ HRESULT CTCPServer::Initialize(const CStunServerConfig& config) ...@@ -855,7 +855,7 @@ HRESULT CTCPServer::Initialize(const CStunServerConfig& config)
if (config.fEnableDosProtection) if (config.fEnableDosProtection)
{ {
spLimiter = boost::shared_ptr<RateLimiter>(new RateLimiter(20000, config.fMultiThreadedMode)); spLimiter = std::make_shared<RateLimiter>(20000, config.fMultiThreadedMode);
} }
if (config.fMultiThreadedMode == false) if (config.fMultiThreadedMode == false)
...@@ -908,7 +908,7 @@ HRESULT CTCPServer::Shutdown() ...@@ -908,7 +908,7 @@ HRESULT CTCPServer::Shutdown()
_threads[role] = NULL; _threads[role] = NULL;
} }
_spAuth.ReleaseAndClear(); _spAuth.reset();
return S_OK; return S_OK;
} }
......
...@@ -39,10 +39,10 @@ class CTCPStunThread ...@@ -39,10 +39,10 @@ class CTCPStunThread
HRESULT NotifyThreadViaPipe(); HRESULT NotifyThreadViaPipe();
void ClosePipes(); void ClosePipes();
CRefCountedPtr<IPolling> _spPolling; std::shared_ptr<IPolling> _spPolling;
bool _fListenSocketsOnEpoll; bool _fListenSocketsOnEpoll;
boost::shared_ptr<RateLimiter> _spLimiter; std::shared_ptr<RateLimiter> _spLimiter;
// epoll helpers // epoll helpers
HRESULT SetListenSocketsOnEpoll(bool fEnable); HRESULT SetListenSocketsOnEpoll(bool fEnable);
...@@ -57,7 +57,7 @@ class CTCPStunThread ...@@ -57,7 +57,7 @@ class CTCPStunThread
bool _fNeedToExit; bool _fNeedToExit;
CRefCountedPtr<IStunAuth> _spAuth; std::shared_ptr<IStunAuth> _spAuth;
SocketRole _role; SocketRole _role;
TransportAddressSet _tsa; // this TransportAddressSet _tsa; // this
...@@ -88,8 +88,6 @@ class CTCPStunThread ...@@ -88,8 +88,6 @@ class CTCPStunThread
StunThreadConnectionMap* _pOldConnList; StunThreadConnectionMap* _pOldConnList;
time_t _timeLastSweep; time_t _timeLastSweep;
StunConnection* AcceptConnection(CStunSocket* pListenSocket); StunConnection* AcceptConnection(CStunSocket* pListenSocket);
void ProcessConnectionEvent(int sock, uint32_t eventflags); void ProcessConnectionEvent(int sock, uint32_t eventflags);
...@@ -115,21 +113,18 @@ public: ...@@ -115,21 +113,18 @@ public:
// tsaListen are the set of addresses we listen to connections on (either 1 address or 4 addresses) // tsaListen are the set of addresses we listen to connections on (either 1 address or 4 addresses)
// tsaHandler is what gets passed to the CStunRequestHandler for formation of the "other-address" attribute // tsaHandler is what gets passed to the CStunRequestHandler for formation of the "other-address" attribute
HRESULT Init(const TransportAddressSet& tsaListen, const TransportAddressSet& tsaHandler, IStunAuth* pAuth, int maxConnections, boost::shared_ptr<RateLimiter>& spLimiter); HRESULT Init(const TransportAddressSet& tsaListen, const TransportAddressSet& tsaHandler, std::shared_ptr<IStunAuth> spAuth, int maxConnections, std::shared_ptr<RateLimiter>& spLimiter);
HRESULT Start(); HRESULT Start();
HRESULT Stop(); HRESULT Stop();
}; };
class CTCPServer : class CTCPServer
public CBasicRefCount,
public CObjectFactory<CTCPServer>,
public IRefCounted
{ {
private: private:
CTCPStunThread* _threads[4]; CTCPStunThread* _threads[4];
CRefCountedPtr<IStunAuth> _spAuth; std::shared_ptr<IStunAuth> _spAuth;
void InitTSA(TransportAddressSet* pTSA, SocketRole role, bool fValid, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise); void InitTSA(TransportAddressSet* pTSA, SocketRole role, bool fValid, const CSocketAddress& addrListen, const CSocketAddress& addrAdvertise);
...@@ -143,9 +138,6 @@ public: ...@@ -143,9 +138,6 @@ public:
HRESULT Shutdown(); HRESULT Shutdown();
HRESULT Start(); HRESULT Start();
HRESULT Stop(); HRESULT Stop();
ADDREF_AND_RELEASE_IMPL();
}; };
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
CBuffer::CBuffer() : CBuffer::CBuffer() :
_data(NULL), _data(nullptr),
_size(0), _size(0),
_allocatedSize(0) _allocatedSize(0)
{ {
...@@ -31,8 +31,8 @@ _allocatedSize(0) ...@@ -31,8 +31,8 @@ _allocatedSize(0)
void CBuffer::Reset() void CBuffer::Reset()
{ {
_spAllocation.reset(); _spAllocation.clear();
_data = NULL; _data = nullptr;
_size = 0; _size = 0;
_allocatedSize = 0; _allocatedSize = 0;
...@@ -51,13 +51,13 @@ HRESULT CBuffer::InitWithAllocation(size_t size) ...@@ -51,13 +51,13 @@ HRESULT CBuffer::InitWithAllocation(size_t size)
Reset(); Reset();
// deliberately not checking for 0. Ok to allocate a 0 byte array // deliberately not checking for 0. Ok to allocate a 0 byte array
boost::scoped_array<uint8_t> spAlloc(new uint8_t[size+2]); // add two bytes for null termination (makes debugging ascii and unicode strings easier), but these two bytes are invisible to the caller (not included in _allocatedSize) std::vector<uint8_t> spAlloc(size+2); // add two bytes for null termination (makes debugging ascii and unicode strings easier), but these two bytes are invisible to the caller (not included in _allocatedSize)
_spAllocation.swap(spAlloc); _spAllocation.swap(spAlloc);
spAlloc.reset(); spAlloc.clear();
_data = _spAllocation.get(); _data = _spAllocation.data();
if (_data) if (_data)
{ {
...@@ -65,17 +65,17 @@ HRESULT CBuffer::InitWithAllocation(size_t size) ...@@ -65,17 +65,17 @@ HRESULT CBuffer::InitWithAllocation(size_t size)
_data[size+1] = 0; _data[size+1] = 0;
} }
_size = (_data != NULL) ? size : 0; _size = (_data != nullptr) ? size : 0;
_allocatedSize = _size; _allocatedSize = _size;
return (_data != NULL) ? S_OK : E_FAIL; return (_data != nullptr) ? S_OK : E_FAIL;
} }
HRESULT CBuffer::InitNoAlloc(uint8_t* pByteArray, size_t size) HRESULT CBuffer::InitNoAlloc(uint8_t* pByteArray, size_t size)
{ {
Reset(); Reset();
if (pByteArray == NULL) if (pByteArray == nullptr)
{ {
size = 0; size = 0;
} }
...@@ -91,7 +91,7 @@ HRESULT CBuffer::InitWithAllocAndCopy(uint8_t* pByteArray, size_t size) ...@@ -91,7 +91,7 @@ HRESULT CBuffer::InitWithAllocAndCopy(uint8_t* pByteArray, size_t size)
HRESULT hr = S_OK; HRESULT hr = S_OK;
Reset(); Reset();
if (pByteArray == NULL) if (pByteArray == nullptr)
{ {
size = 0; size = 0;
} }
...@@ -142,7 +142,7 @@ HRESULT CBuffer::SetSize(size_t size) ...@@ -142,7 +142,7 @@ HRESULT CBuffer::SetSize(size_t size)
bool CBuffer::IsValid() bool CBuffer::IsValid()
{ {
return (_data != NULL); return (_data != nullptr);
} }
......
...@@ -30,7 +30,7 @@ private: ...@@ -30,7 +30,7 @@ private:
uint8_t* _data; uint8_t* _data;
size_t _size; size_t _size;
size_t _allocatedSize; size_t _allocatedSize;
boost::scoped_array<uint8_t> _spAllocation; std::vector<uint8_t> _spAllocation;
// disallow copy and assignment. // disallow copy and assignment.
CBuffer(const CBuffer&); CBuffer(const CBuffer&);
...@@ -62,7 +62,7 @@ public: ...@@ -62,7 +62,7 @@ public:
bool IsValid(); bool IsValid();
}; };
typedef boost::shared_ptr<CBuffer> CRefCountedBuffer; typedef std::shared_ptr<CBuffer> CRefCountedBuffer;
#endif #endif
...@@ -60,9 +60,10 @@ struct AuthResponse ...@@ -60,9 +60,10 @@ struct AuthResponse
}; };
class IStunAuth : public IRefCounted class IStunAuth
{ {
public: public:
virtual ~IStunAuth() = default;
virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse) = 0; virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse) = 0;
}; };
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "atomichelpers.h" #include "atomichelpers.h"
#include "stunbuilder.h" #include "stunbuilder.h"
#include <boost/crc.hpp>
#ifndef __APPLE__ #ifndef __APPLE__
#include <openssl/md5.h> #include <openssl/md5.h>
...@@ -32,6 +31,8 @@ ...@@ -32,6 +31,8 @@
#include <CommonCrypto/CommonCrypto.h> #include <CommonCrypto/CommonCrypto.h>
#endif #endif
#include "crc32.h"
#include "stunauth.h" #include "stunauth.h"
...@@ -419,10 +420,9 @@ HRESULT CStunMessageBuilder::AddChangeRequest(const StunChangeRequestAttribute& ...@@ -419,10 +420,9 @@ HRESULT CStunMessageBuilder::AddChangeRequest(const StunChangeRequestAttribute&
HRESULT CStunMessageBuilder::AddFingerprintAttribute() HRESULT CStunMessageBuilder::AddFingerprintAttribute()
{ {
boost::crc_32_type result;
uint32_t value; uint32_t value;
CRefCountedBuffer spBuffer; CRefCountedBuffer spBuffer;
void* pData = NULL; uint8_t* pData = NULL;
size_t length = 0; size_t length = 0;
int offset; int offset;
...@@ -442,9 +442,8 @@ HRESULT CStunMessageBuilder::AddFingerprintAttribute() ...@@ -442,9 +442,8 @@ HRESULT CStunMessageBuilder::AddFingerprintAttribute()
ASSERT(length > 8); ASSERT(length > 8);
length = length-8; length = length-8;
result.process_bytes(pData, length);
value = ::crc32(0, pData, length);
value = result.checksum();
value = value ^ STUN_FINGERPRINT_XOR; value = value ^ STUN_FINGERPRINT_XOR;
offset = -(int)(sizeof(value)); offset = -(int)(sizeof(value));
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "stunreader.h" #include "stunreader.h"
#include "stunutils.h" #include "stunutils.h"
#include "socketaddress.h" #include "socketaddress.h"
#include <boost/crc.hpp>
#ifndef __APPLE__ #ifndef __APPLE__
#include <openssl/evp.h> #include <openssl/evp.h>
...@@ -34,6 +33,7 @@ ...@@ -34,6 +33,7 @@
#include "stunauth.h" #include "stunauth.h"
#include "fasthash.h" #include "fasthash.h"
#include "crc32.h"
...@@ -102,7 +102,6 @@ bool CStunMessageReader::IsFingerprintAttributeValid() ...@@ -102,7 +102,6 @@ bool CStunMessageReader::IsFingerprintAttributeValid()
StunAttribute* pAttrib = _mapAttributes.Lookup(STUN_ATTRIBUTE_FINGERPRINT); StunAttribute* pAttrib = _mapAttributes.Lookup(STUN_ATTRIBUTE_FINGERPRINT);
CRefCountedBuffer spBuffer; CRefCountedBuffer spBuffer;
size_t size=0; size_t size=0;
boost::crc_32_type crc;
uint32_t computedValue=1; uint32_t computedValue=1;
uint32_t readValue=0; uint32_t readValue=0;
uint8_t* ptr = NULL; uint8_t* ptr = NULL;
...@@ -125,8 +124,7 @@ bool CStunMessageReader::IsFingerprintAttributeValid() ...@@ -125,8 +124,7 @@ bool CStunMessageReader::IsFingerprintAttributeValid()
ptr = spBuffer->GetData(); ptr = spBuffer->GetData();
ChkIfA(ptr==NULL, E_FAIL); ChkIfA(ptr==NULL, E_FAIL);
crc.process_bytes(ptr, size-8); // -8 because we're assuming the fingerprint attribute is 8 bytes and is the last attribute in the stream computedValue = ::crc32(0, ptr, size-8);
computedValue = crc.checksum();
computedValue = computedValue ^ STUN_FINGERPRINT_XOR; computedValue = computedValue ^ STUN_FINGERPRINT_XOR;
readValue = *(uint32_t*)(ptr+pAttrib->offset); readValue = *(uint32_t*)(ptr+pAttrib->offset);
......
include ../common.inc include ../common.inc
PROJECT_TARGET := stuntestcode PROJECT_TARGET := stuntestcode
PROJECT_OBJS := testatomichelpers.o testbuilder.o testclientlogic.o testcmdline.o testcode.o testdatastream.o testfasthash.o testintegrity.o testmessagehandler.o testpolling.o testratelimiter.o testreader.o testrecvfromex.o PROJECT_OBJS := testatomichelpers.o testbuilder.o testclientlogic.o testcmdline.o testcode.o testdatastream.o testfasthash.o testintegrity.o testmessagehandler.o testpolling.o testratelimiter.o testreader.o testrecvfromex.o
INCLUDES := $(BOOST_INCLUDE) $(OPENSSL_INCLUDE) -I../common -I../stuncore -I../networkutils INCLUDES := $(BOOST_INCLUDE) $(OPENSSL_INCLUDE) -I../common -I../stuncore -I../networkutils
LIB_PATH := -L../networkutils -L../stuncore -L../common LIB_PATH := -L../networkutils -L../stuncore -L../common
......
...@@ -172,8 +172,7 @@ HRESULT CTestClientLogic::CommonInit(NatBehavior behavior, NatFiltering filterin ...@@ -172,8 +172,7 @@ HRESULT CTestClientLogic::CommonInit(NatBehavior behavior, NatFiltering filterin
_addrMappedAP = addrMapped; _addrMappedAP = addrMapped;
_addrMappedAA = addrMapped; _addrMappedAA = addrMapped;
_spClientLogic = boost::shared_ptr<CStunClientLogic>(new CStunClientLogic()); _spClientLogic = std::make_shared<CStunClientLogic>();
switch (behavior) switch (behavior)
{ {
......
...@@ -46,7 +46,7 @@ private: ...@@ -46,7 +46,7 @@ private:
TransportAddressSet _tsa; TransportAddressSet _tsa;
boost::shared_ptr<CStunClientLogic> _spClientLogic; std::shared_ptr<CStunClientLogic> _spClientLogic;
......
...@@ -73,19 +73,19 @@ void RunUnitTests() ...@@ -73,19 +73,19 @@ void RunUnitTests()
{ {
std::vector<IUnitTest*> vecTests; std::vector<IUnitTest*> vecTests;
boost::shared_ptr<CTestDataStream> spTestDataStream(new CTestDataStream); std::shared_ptr<CTestDataStream> spTestDataStream(new CTestDataStream);
boost::shared_ptr<CTestReader> spTestReader(new CTestReader); std::shared_ptr<CTestReader> spTestReader(new CTestReader);
boost::shared_ptr<CTestBuilder> spTestBuilder(new CTestBuilder); std::shared_ptr<CTestBuilder> spTestBuilder(new CTestBuilder);
boost::shared_ptr<CTestIntegrity> spTestIntegrity(new CTestIntegrity); std::shared_ptr<CTestIntegrity> spTestIntegrity(new CTestIntegrity);
boost::shared_ptr<CTestMessageHandler> spTestMessageHandler(new CTestMessageHandler); std::shared_ptr<CTestMessageHandler> spTestMessageHandler(new CTestMessageHandler);
boost::shared_ptr<CTestCmdLineParser> spTestCmdLineParser(new CTestCmdLineParser); std::shared_ptr<CTestCmdLineParser> spTestCmdLineParser(new CTestCmdLineParser);
boost::shared_ptr<CTestClientLogic> spTestClientLogic(new CTestClientLogic); std::shared_ptr<CTestClientLogic> spTestClientLogic(new CTestClientLogic);
boost::shared_ptr<CTestRecvFromExIPV4> spTestRecvFromEx4(new CTestRecvFromExIPV4); std::shared_ptr<CTestRecvFromExIPV4> spTestRecvFromEx4(new CTestRecvFromExIPV4);
boost::shared_ptr<CTestRecvFromExIPV6> spTestRecvFromEx6(new CTestRecvFromExIPV6); std::shared_ptr<CTestRecvFromExIPV6> spTestRecvFromEx6(new CTestRecvFromExIPV6);
boost::shared_ptr<CTestFastHash> spTestFastHash(new CTestFastHash); std::shared_ptr<CTestFastHash> spTestFastHash(new CTestFastHash);
boost::shared_ptr<CTestPolling> spTestPolling(new CTestPolling); std::shared_ptr<CTestPolling> spTestPolling(new CTestPolling);
boost::shared_ptr<CTestAtomicHelpers> spTestAtomicHelpers(new CTestAtomicHelpers); std::shared_ptr<CTestAtomicHelpers> spTestAtomicHelpers(new CTestAtomicHelpers);
boost::shared_ptr<CTestRateLimiter> spTestRateLimiter(new CTestRateLimiter); std::shared_ptr<CTestRateLimiter> spTestRateLimiter(new CTestRateLimiter);
vecTests.push_back(spTestDataStream.get()); vecTests.push_back(spTestDataStream.get());
vecTests.push_back(spTestReader.get()); vecTests.push_back(spTestReader.get());
......
...@@ -91,8 +91,8 @@ HRESULT CMockAuthLong::DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse ...@@ -91,8 +91,8 @@ HRESULT CMockAuthLong::DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse
CTestMessageHandler::CTestMessageHandler() CTestMessageHandler::CTestMessageHandler()
{ {
CMockAuthShort::CreateInstanceNoInit(_spAuthShort.GetPointerPointer()); _spAuthShort = std::make_shared<CMockAuthShort>();
CMockAuthLong::CreateInstanceNoInit(_spAuthLong.GetPointerPointer()); _spAuthLong = std::make_shared<CMockAuthLong>();
ToAddr(c_szIPLocal, c_portLocal, &_addrLocal); ToAddr(c_szIPLocal, c_portLocal, &_addrLocal);
ToAddr(c_szIPMapped, c_portMapped, &_addrMapped); ToAddr(c_szIPMapped, c_portMapped, &_addrMapped);
...@@ -104,7 +104,7 @@ CTestMessageHandler::CTestMessageHandler() ...@@ -104,7 +104,7 @@ CTestMessageHandler::CTestMessageHandler()
} }
HRESULT CTestMessageHandler::SendHelper(CStunMessageBuilder& builderRequest, CStunMessageReader* pReaderResponse, IStunAuth* pAuth) HRESULT CTestMessageHandler::SendHelper(CStunMessageBuilder& builderRequest, CStunMessageReader* pReaderResponse, std::shared_ptr<IStunAuth> spAuth)
{ {
CRefCountedBuffer spBufferRequest; CRefCountedBuffer spBufferRequest;
CRefCountedBuffer spBufferResponse(new CBuffer(MAX_STUN_MESSAGE_SIZE)); CRefCountedBuffer spBufferResponse(new CBuffer(MAX_STUN_MESSAGE_SIZE));
...@@ -130,7 +130,7 @@ HRESULT CTestMessageHandler::SendHelper(CStunMessageBuilder& builderRequest, CSt ...@@ -130,7 +130,7 @@ HRESULT CTestMessageHandler::SendHelper(CStunMessageBuilder& builderRequest, CSt
msgOut.spBufferOut = spBufferResponse; msgOut.spBufferOut = spBufferResponse;
ChkA(CStunRequestHandler::ProcessRequest(msgIn, msgOut, &tas, pAuth)); ChkA(CStunRequestHandler::ProcessRequest(msgIn, msgOut, &tas, spAuth.get()));
ChkIf(CStunMessageReader::BodyValidated != pReaderResponse->AddBytes(spBufferResponse->GetData(), spBufferResponse->GetSize()), E_FAIL); ChkIf(CStunMessageReader::BodyValidated != pReaderResponse->AddBytes(spBufferResponse->GetData(), spBufferResponse->GetSize()), E_FAIL);
......
...@@ -21,24 +21,18 @@ ...@@ -21,24 +21,18 @@
class CMockAuthShort : class CMockAuthShort :
public CBasicRefCount,
public CObjectFactory<CMockAuthShort>,
public IStunAuth public IStunAuth
{ {
public: public:
virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse); virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse);
ADDREF_AND_RELEASE_IMPL();
}; };
class CMockAuthLong : class CMockAuthLong :
public CBasicRefCount,
public CObjectFactory<CMockAuthLong>,
public IStunAuth public IStunAuth
{ {
public: public:
virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse); virtual HRESULT DoAuthCheck(AuthAttributes* pAuthAttributes, AuthResponse* pResponse);
ADDREF_AND_RELEASE_IMPL();
}; };
...@@ -48,8 +42,8 @@ public: ...@@ -48,8 +42,8 @@ public:
class CTestMessageHandler : public IUnitTest class CTestMessageHandler : public IUnitTest
{ {
private: private:
CRefCountedPtr<CMockAuthShort> _spAuthShort; std::shared_ptr<CMockAuthShort> _spAuthShort;
CRefCountedPtr<CMockAuthLong> _spAuthLong; std::shared_ptr<CMockAuthLong> _spAuthLong;
CSocketAddress _addrLocal; CSocketAddress _addrLocal;
...@@ -78,7 +72,7 @@ private: ...@@ -78,7 +72,7 @@ private:
HRESULT ValidateOtherAddress(CStunMessageReader& reader, const CSocketAddress& addrExpected); HRESULT ValidateOtherAddress(CStunMessageReader& reader, const CSocketAddress& addrExpected);
HRESULT SendHelper(CStunMessageBuilder& builderRequest, CStunMessageReader* pReaderResponse, IStunAuth* pAuth); HRESULT SendHelper(CStunMessageBuilder& builderRequest, CStunMessageReader* pReaderResponse, std::shared_ptr<IStunAuth> spAuth);
public: public:
CTestMessageHandler(); CTestMessageHandler();
......
...@@ -52,7 +52,7 @@ Cleanup: ...@@ -52,7 +52,7 @@ Cleanup:
void CTestPolling::TestUnInit() void CTestPolling::TestUnInit()
{ {
size_t size = _pipes.size(); size_t size = _pipes.size();
_spPolling.ReleaseAndClear(); _spPolling.reset();
for (size_t index = 0; index < size; index++) for (size_t index = 0; index < size; index++)
{ {
...@@ -116,7 +116,7 @@ HRESULT CTestPolling::TestInit(size_t sizePolling, size_t sizePipeArray) ...@@ -116,7 +116,7 @@ HRESULT CTestPolling::TestInit(size_t sizePolling, size_t sizePipeArray)
TestUnInit(); TestUnInit();
ChkA(CreatePollingInstance(_polltype, sizePolling, _spPolling.GetPointerPointer())); ChkA(CreatePollingInstance(_polltype, sizePolling, _spPolling));
for (size_t index = 0; index < sizePipeArray; index++) for (size_t index = 0; index < sizePipeArray; index++)
{ {
......
...@@ -29,7 +29,7 @@ class CTestPolling : public IUnitTest ...@@ -29,7 +29,7 @@ class CTestPolling : public IUnitTest
{ {
public: public:
CRefCountedPtr<IPolling> _spPolling; std::shared_ptr<IPolling> _spPolling;
std::vector<PipePair> _pipes; std::vector<PipePair> _pipes;
uint32_t _polltype; uint32_t _polltype;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment