mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-01-07 19:13:20 +00:00
Major agent update.
This commit is contained in:
@@ -686,12 +686,11 @@ ILibAsyncServerSocket_ServerModule ILibCreateAsyncServerSocketModuleWithMemoryEx
|
||||
//
|
||||
// Set the socket to non-block mode, so we can play nice and share the thread
|
||||
//
|
||||
int flags = 1;
|
||||
#ifdef _WIN32_WCE
|
||||
ioctlsocket(RetVal->ListenSocket, FIONBIO, &flags);
|
||||
#elif WIN32
|
||||
#ifdef WIN32
|
||||
u_long flags = 1;
|
||||
ioctlsocket(RetVal->ListenSocket, FIONBIO, (u_long *)(&flags));
|
||||
#elif _POSIX
|
||||
int flags = 1;
|
||||
flags = fcntl(RetVal->ListenSocket, F_GETFL, 0);
|
||||
fcntl(RetVal->ListenSocket, F_SETFL, O_NONBLOCK | flags);
|
||||
#endif
|
||||
|
||||
@@ -177,4 +177,3 @@ void ILibAsyncServerSocket_GetLocal(ILibAsyncServerSocket_ServerModule ServerSoc
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -46,7 +46,6 @@ limitations under the License.
|
||||
#include "ILibParsers.h"
|
||||
#include "ILibAsyncSocket.h"
|
||||
#include "ILibRemoteLogging.h"
|
||||
#include "ILibAsyncSocket.h"
|
||||
|
||||
#ifndef MICROSTACK_NOTLS
|
||||
#include <openssl/err.h>
|
||||
@@ -742,6 +741,7 @@ void ILibAsyncSocket_Disconnect(ILibAsyncSocket_SocketModule socketModule)
|
||||
#endif
|
||||
|
||||
struct ILibAsyncSocketModule *module = (struct ILibAsyncSocketModule*)socketModule;
|
||||
if (module == NULL) { return; }
|
||||
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(module->Transport.ChainLink.ParentChain), ILibRemoteLogging_Modules_Microstack_AsyncSocket, ILibRemoteLogging_Flags_VerbosityLevel_1, "AsyncSocket[%p] << DISCONNECT", (void*)module);
|
||||
|
||||
@@ -942,6 +942,12 @@ void ILibAsyncSocket_ConnectTo(void* socketModule, struct sockaddr *localInterfa
|
||||
}
|
||||
|
||||
#ifdef MICROSTACK_PROXY
|
||||
void ILibAsyncSocket_ClearProxySettings(void *socketModule)
|
||||
{
|
||||
struct ILibAsyncSocketModule *module = (struct ILibAsyncSocketModule*)socketModule;
|
||||
memset(&(module->ProxyAddress), 0, sizeof(struct sockaddr_in6));
|
||||
}
|
||||
|
||||
//! Connect using an HTTPS proxy. If "proxyAddress" is set to NULL, this call acts just to a normal connect call without a proxy.
|
||||
/*!
|
||||
\param socketModule ILibAsyncSocket Client to initiate the connection
|
||||
@@ -1401,7 +1407,11 @@ void ILibAsyncSocket_PreSelect(void* socketModule,fd_set *readset, fd_set *write
|
||||
sem_post(&(module->SendLock));
|
||||
h(module, module->user);
|
||||
sem_wait(&(module->SendLock));
|
||||
if (module->timeout_milliSeconds != 0) { *blocktime = module->timeout_milliSeconds; }
|
||||
if (module->timeout_milliSeconds != 0)
|
||||
{
|
||||
*blocktime = module->timeout_milliSeconds;
|
||||
module->timeout_lastActivity = ILibGetUptime();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -2329,4 +2339,3 @@ STACK_OF(X509) *ILibAsyncSocket_SslGetCerts(ILibAsyncSocket_SocketModule socketM
|
||||
return SSL_get_peer_cert_chain(sm->ssl);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -174,6 +174,7 @@ void ILibAsyncSocket_ResetTotalBytesSent(ILibAsyncSocket_SocketModule socketModu
|
||||
void ILibAsyncSocket_ConnectTo(void* socketModule, struct sockaddr *localInterface, struct sockaddr *remoteAddress, ILibAsyncSocket_OnInterrupt InterruptPtr, void *user);
|
||||
|
||||
#ifdef MICROSTACK_PROXY
|
||||
void ILibAsyncSocket_ClearProxySettings(void *socketModule);
|
||||
void ILibAsyncSocket_ConnectToProxy(void* socketModule, struct sockaddr *localInterface, struct sockaddr *remoteAddress, struct sockaddr *proxyAddress, char* proxyUser, char* proxyPass, ILibAsyncSocket_OnInterrupt InterruptPtr, void *user);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -117,9 +117,7 @@ ILibAsyncUDPSocket_SocketModule ILibAsyncUDPSocket_CreateEx(void *Chain, int Buf
|
||||
#endif
|
||||
|
||||
// Initialize the UDP socket data structure
|
||||
data = (struct ILibAsyncUDPSocket_Data*)malloc(sizeof(struct ILibAsyncUDPSocket_Data));
|
||||
if (data == NULL) return NULL;
|
||||
memset(data, 0, sizeof(struct ILibAsyncUDPSocket_Data));
|
||||
data = (struct ILibAsyncUDPSocket_Data*)ILibMemory_Allocate(sizeof(struct ILibAsyncUDPSocket_Data), 0, NULL, NULL);
|
||||
data->OnData = OnData;
|
||||
data->OnSendOK = OnSendOK;
|
||||
data->user1 = user;
|
||||
|
||||
@@ -155,4 +155,3 @@ SOCKET ILibAsyncUDPSocket_GetSocket(ILibAsyncUDPSocket_SocketModule module);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -35,15 +35,39 @@ limitations under the License.
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/hmac.h>
|
||||
#else
|
||||
#include "md5.h"
|
||||
#include "sha1.h"
|
||||
#include "microstack/sha.h"
|
||||
#ifdef WIN32
|
||||
void BCRYPT_INIT(BCRYPT_CTX* ctx, void* alg)
|
||||
{
|
||||
memset(ctx, 0, sizeof(BCRYPT_CTX));
|
||||
BCryptOpenAlgorithmProvider(&(ctx->hAlg), (LPCWSTR)alg, NULL, 0);
|
||||
BCryptGetProperty(ctx->hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&(ctx->cbHashObject), sizeof(DWORD), &(ctx->cbData), 0);
|
||||
ctx->pbHashObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, ctx->cbHashObject);
|
||||
BCryptGetProperty(ctx->hAlg, BCRYPT_HASH_LENGTH, (PBYTE)&(ctx->cbHash), sizeof(DWORD), &(ctx->cbData), 0);
|
||||
BCryptCreateHash(ctx->hAlg, &(ctx->hHash), ctx->pbHashObject, ctx->cbHashObject, NULL, 0, 0);
|
||||
}
|
||||
void BCRYPT_UPDATE(BCRYPT_CTX* ctx, void* data, size_t dataLen)
|
||||
{
|
||||
BCryptHashData(ctx->hHash, (PBYTE)data, (ULONG)dataLen, 0);
|
||||
}
|
||||
void BCRYPT_FINAL(char *h, BCRYPT_CTX* ctx)
|
||||
{
|
||||
BCryptFinishHash(ctx->hHash, (PUCHAR)h, ctx->cbHash, 0);
|
||||
BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
|
||||
BCryptDestroyHash(ctx->hHash);
|
||||
HeapFree(GetProcessHeap(), 0, ctx->pbHashObject);
|
||||
}
|
||||
#else
|
||||
#include "microstack/nossl/md5.h"
|
||||
#include "microstack/nossl/sha1.h"
|
||||
#include "microstack/nossl/sha.h"
|
||||
#include <time.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
char utils_HexTable[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
|
||||
char utils_HexTable2[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
|
||||
|
||||
|
||||
void __fastcall util_md5(char* data, int datalen, char* result)
|
||||
{
|
||||
MD5_CTX c;
|
||||
@@ -1006,4 +1030,4 @@ int __fastcall util_rsaverify(X509 *cert, char* data, int datalen, char* sign, i
|
||||
return r;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -58,9 +58,47 @@ void __fastcall util_randomtext(int length, char* result);
|
||||
#define UTIL_SHA512_HASHSIZE 64
|
||||
|
||||
#ifdef MICROSTACK_NOTLS
|
||||
#include "md5.h"
|
||||
#include "sha1.h"
|
||||
#include "microstack/SHA.h"
|
||||
#ifdef WIN32
|
||||
#include <bcrypt.h>
|
||||
|
||||
typedef struct BCRYPT_CTX
|
||||
{
|
||||
BCRYPT_ALG_HANDLE hAlg;
|
||||
BCRYPT_HASH_HANDLE hHash;
|
||||
DWORD cbData;
|
||||
DWORD cbHash;
|
||||
DWORD cbHashObject;
|
||||
PBYTE pbHashObject;
|
||||
}BCRYPT_CTX;
|
||||
#define SHA512_CTX BCRYPT_CTX
|
||||
#define SHA384_CTX BCRYPT_CTX
|
||||
#define SHA256_CTX BCRYPT_CTX
|
||||
#define SHA_CTX BCRYPT_CTX
|
||||
#define MD5_CTX BCRYPT_CTX
|
||||
|
||||
void BCRYPT_INIT(BCRYPT_CTX* ctx, void* alg);
|
||||
void BCRYPT_UPDATE(BCRYPT_CTX* ctx, void* data, size_t dataLen);
|
||||
void BCRYPT_FINAL(char *h, BCRYPT_CTX* ctx);
|
||||
#define SHA512_Init(ctx) BCRYPT_INIT(ctx, BCRYPT_SHA512_ALGORITHM)
|
||||
#define SHA384_Init(ctx) BCRYPT_INIT(ctx, BCRYPT_SHA384_ALGORITHM)
|
||||
#define SHA256_Init(ctx) BCRYPT_INIT(ctx, BCRYPT_SHA256_ALGORITHM)
|
||||
#define SHA1_Init(ctx) BCRYPT_INIT(ctx, BCRYPT_SHA1_ALGORITHM)
|
||||
#define MD5_Init(ctx) BCRYPT_INIT(ctx, BCRYPT_MD5_ALGORITHM)
|
||||
#define SHA512_Update(ctx, data, len) BCRYPT_UPDATE(ctx, data, len)
|
||||
#define SHA384_Update(ctx, data, len) BCRYPT_UPDATE(ctx, data, len)
|
||||
#define SHA256_Update(ctx, data, len) BCRYPT_UPDATE(ctx, data, len)
|
||||
#define SHA1_Update(ctx, data, len) BCRYPT_UPDATE(ctx, data, len)
|
||||
#define MD5_Update(ctx, data, len) BCRYPT_UPDATE(ctx, data, len)
|
||||
#define SHA512_Final(md, ctx) BCRYPT_FINAL(md, ctx)
|
||||
#define SHA384_Final(md, ctx) BCRYPT_FINAL(md, ctx)
|
||||
#define SHA256_Final(md, ctx) BCRYPT_FINAL(md, ctx)
|
||||
#define SHA1_Final(md, ctx) BCRYPT_FINAL(md, ctx)
|
||||
#define MD5_Final(md, ctx) BCRYPT_FINAL(md, ctx)
|
||||
|
||||
#else
|
||||
#include "microstack/nossl/md5.h"
|
||||
#include "microstack/nossl/SHA.h"
|
||||
#include "microstack/nossl/sha1.h"
|
||||
|
||||
#define SHA256_CTX SHA256Context
|
||||
#define SHA512_CTX SHA512Context
|
||||
@@ -77,6 +115,7 @@ void __fastcall util_randomtext(int length, char* result);
|
||||
#define SHA512_Update(ctx, data, len) SHA512Input(ctx, (uint8_t*)data, len)
|
||||
#define SHA512_Final(md, ctx) SHA512Result (ctx, md)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef MICROSTACK_NOTLS
|
||||
|
||||
@@ -20,6 +20,12 @@ limitations under the License.
|
||||
#include <iphlpapi.h>
|
||||
#include <Dbghelp.h>
|
||||
#endif
|
||||
|
||||
#if defined(WIN32) && !defined(_WIN32_WCE)
|
||||
#define _CRTDBG_MAP_ALLOC
|
||||
#include <crtdbg.h>
|
||||
#endif
|
||||
|
||||
#include "ILibParsers.h"
|
||||
|
||||
#ifdef _POSIX
|
||||
@@ -57,21 +63,24 @@ void CALLBACK ILibIPAddressMonitor_dispatch(
|
||||
IN DWORD dwFlags
|
||||
)
|
||||
{
|
||||
if (dwError == 0 && lpOverlapped->hEvent != NULL)
|
||||
if (ILibMemory_CanaryOK(lpOverlapped))
|
||||
{
|
||||
_ILibIPAddressMonitor *obj = (_ILibIPAddressMonitor*)lpOverlapped->hEvent;
|
||||
ILibChain_RunOnMicrostackThread(obj->chainLink.ParentChain, ILibIPAddressMonitor_MicrostackThreadDispatch, obj);
|
||||
}
|
||||
else if (lpOverlapped->hEvent == NULL)
|
||||
{
|
||||
free(lpOverlapped);
|
||||
if (dwError == 0 && lpOverlapped->hEvent != NULL)
|
||||
{
|
||||
_ILibIPAddressMonitor *obj = (_ILibIPAddressMonitor*)lpOverlapped->hEvent;
|
||||
ILibChain_RunOnMicrostackThread(obj->chainLink.ParentChain, ILibIPAddressMonitor_MicrostackThreadDispatch, obj);
|
||||
}
|
||||
else if (lpOverlapped->hEvent == NULL)
|
||||
{
|
||||
ILibMemory_Free(lpOverlapped);
|
||||
}
|
||||
}
|
||||
}
|
||||
void ILibIPAddressMonitor_MicrostackThreadDispatch(void *chain, void *user)
|
||||
{
|
||||
_ILibIPAddressMonitor *obj = (_ILibIPAddressMonitor*)user;
|
||||
if (obj->onUpdate != NULL) { obj->onUpdate(obj, obj->user); }
|
||||
WSAIoctl(obj->mSocket, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &(obj->bytesReturned), (LPWSAOVERLAPPED)&(obj->reserved), ILibIPAddressMonitor_dispatch);
|
||||
WSAIoctl(obj->mSocket, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &(obj->bytesReturned), obj->reserved, ILibIPAddressMonitor_dispatch);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -128,7 +137,7 @@ ILibIPAddressMonitor ILibIPAddressMonitor_Create(void *chain, ILibIPAddressMonit
|
||||
obj->onUpdate = handler;
|
||||
obj->user = user;
|
||||
#ifdef WIN32
|
||||
obj->reserved = ILibMemory_Allocate(sizeof(OVERLAPPED), 0, NULL, NULL);
|
||||
obj->reserved = (OVERLAPPED*)ILibMemory_SmartAllocate(sizeof(OVERLAPPED)); // This leaks due to a bug in Windows, where the WSAIoctl frequently doesn't event on shutdown. If we free try to free it manually, it will cause a crash, because windows will have an invalid overlapped object.
|
||||
obj->reserved->hEvent = (HANDLE)obj;
|
||||
obj->mSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
WSAIoctl(obj->mSocket, SIO_ADDRESS_LIST_CHANGE, NULL, 0, NULL, 0, &(obj->bytesReturned), obj->reserved, ILibIPAddressMonitor_dispatch);
|
||||
@@ -155,4 +164,4 @@ ILibIPAddressMonitor ILibIPAddressMonitor_Create(void *chain, ILibIPAddressMonit
|
||||
obj->chainLink.DestroyHandler = ILibIPAddressMonitor_Destroy;
|
||||
ILibAddToChain(chain, obj);
|
||||
return((ILibIPAddressMonitor)obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,4 +393,3 @@ void ILibMulticastSocket_WakeOnLan(void *module, char* mac)
|
||||
ILibMulticastSocket_Broadcast((struct ILibMulticastSocket_StateModule*)module, ILibScratchPad, 102, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,4 +39,3 @@ void ILibSetTTL(void *module, int ttl);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1047,6 +1047,7 @@ typedef struct ILibBaseChain
|
||||
|
||||
void *Timer;
|
||||
void *Reserved;
|
||||
ILibLinkedList OnDestroyEventSinks;
|
||||
ILibLinkedList Links;
|
||||
ILibLinkedList LinksPendingDelete;
|
||||
ILibHashtable ChainStash;
|
||||
@@ -1063,7 +1064,73 @@ typedef struct ILibBaseChain
|
||||
void *node;
|
||||
}ILibBaseChain;
|
||||
|
||||
void* ILibMemory_AllocateA_Init(void *buffer)
|
||||
{
|
||||
((void**)buffer)[0] = (char*)buffer + sizeof(void*);
|
||||
return(buffer);
|
||||
}
|
||||
void* ILibMemory_Init(void *ptr, size_t primarySize, size_t extraSize, ILibMemory_Types memType)
|
||||
{
|
||||
if (ptr == NULL) { ILIBCRITICALEXIT(254); }
|
||||
memset(ptr, 0, primarySize + extraSize + sizeof(ILibMemory_Header) + (extraSize > 0 ? sizeof(ILibMemory_Header) : 0));
|
||||
|
||||
void *primary = ILibMemory_FromRaw(ptr);
|
||||
|
||||
((ILibMemory_Header*)ptr)->size = primarySize;
|
||||
((ILibMemory_Header*)ptr)->extraSize = extraSize;
|
||||
((ILibMemory_Header*)ptr)->CANARY = ILibMemory_Canary;
|
||||
((ILibMemory_Header*)ptr)->memoryType = memType;
|
||||
|
||||
if (extraSize > 0)
|
||||
{
|
||||
ILibMemory_Header *extra = (ILibMemory_Header*)ILibMemory_RawPtr(ILibMemory_Extra(primary));
|
||||
extra->size = extraSize;
|
||||
extra->extraSize = 0;
|
||||
extra->CANARY = ILibMemory_Canary;
|
||||
extra->memoryType = ILibMemory_Types_OTHER;
|
||||
}
|
||||
|
||||
return(primary);
|
||||
}
|
||||
void ILibMemory_Free(void *ptr)
|
||||
{
|
||||
if (ILibMemory_CanaryOK(ptr) && ILibMemory_MemType(ptr) == ILibMemory_Types_HEAP)
|
||||
{
|
||||
if (ILibMemory_ExtraSize(ptr) > 0)
|
||||
{
|
||||
memset(ILibMemory_RawPtr(ILibMemory_Extra(ptr)), 0, sizeof(ILibMemory_Header));
|
||||
}
|
||||
memset(ILibMemory_RawPtr(ptr), 0, sizeof(ILibMemory_Header));
|
||||
free(ILibMemory_RawPtr(ptr));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
int ILibMemory_CanaryOK(void *ptr)
|
||||
{
|
||||
__try
|
||||
{
|
||||
return(ILibMemory_Ex_CanaryOK(ptr));
|
||||
}
|
||||
__except (EXCEPTION_EXECUTE_HANDLER)
|
||||
{
|
||||
return(0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const int ILibMemory_CHAIN_CONTAINERSIZE = sizeof(ILibBaseChain);
|
||||
void ILibMemory_AllocateTemp_Sink(void *obj)
|
||||
{
|
||||
ILibMemory_Free(obj);
|
||||
}
|
||||
void* ILibMemory_AllocateTemp(void* chain, size_t sz)
|
||||
{
|
||||
char *ret = ILibMemory_SmartAllocate(sz);
|
||||
ILibLifeTime_AddEx(ILibGetBaseTimer(chain), ret, 0, ILibMemory_AllocateTemp_Sink, ILibMemory_AllocateTemp_Sink);
|
||||
return(ret);
|
||||
}
|
||||
|
||||
void* ILibMemory_AllocateA_InitMem(void *buffer, size_t bufferLen)
|
||||
{
|
||||
char *retVal = ((char*)buffer + 8 + sizeof(void*));
|
||||
@@ -1079,11 +1146,11 @@ void* ILibMemory_AllocateA_Get(void *buffer, size_t sz)
|
||||
{
|
||||
char *retVal = NULL;
|
||||
|
||||
if (ILibMemory_AllocateA_Size(buffer) > (int)sz)
|
||||
if (ILibMemory_AllocateA_Size(buffer) > sz)
|
||||
{
|
||||
retVal = ILibMemory_AllocateA_Next(buffer);
|
||||
ILibMemory_AllocateA_Size(buffer) -= (int)sz;
|
||||
ILibMemory_AllocateA_Next(buffer) = (char*)ILibMemory_AllocateA_Next(buffer) + (int)sz;
|
||||
ILibMemory_AllocateA_Size(buffer) -= sz;
|
||||
ILibMemory_AllocateA_Next(buffer) = (char*)ILibMemory_AllocateA_Next(buffer) + sz;
|
||||
}
|
||||
|
||||
return(retVal);
|
||||
@@ -1141,16 +1208,6 @@ ILibHashtable ILibChain_GetBaseHashtable(void* chain)
|
||||
return(b->ChainStash);
|
||||
}
|
||||
|
||||
void ILibChain_OnDestroyEvent_Sink(void *object)
|
||||
{
|
||||
ILibChain_Link *link = (ILibChain_Link*)object;
|
||||
ILibChain_DestroyEvent e = (ILibChain_DestroyEvent)((void**)link->ExtraMemoryPtr)[0];
|
||||
|
||||
if (e != NULL)
|
||||
{
|
||||
e(link->ParentChain, ((void**)link->ExtraMemoryPtr)[1]);
|
||||
}
|
||||
}
|
||||
//! Add an event handler to be dispatched when the Microstack Chain is shutdown
|
||||
/*!
|
||||
\param chain Microstack Chain to add an event handler to
|
||||
@@ -1159,20 +1216,16 @@ void ILibChain_OnDestroyEvent_Sink(void *object)
|
||||
*/
|
||||
void ILibChain_OnDestroyEvent_AddHandler(void *chain, ILibChain_DestroyEvent sink, void *user)
|
||||
{
|
||||
ILibChain_Link *link = ILibChain_Link_Allocate(sizeof(ILibChain_Link), 2 * sizeof(void*));
|
||||
link->ParentChain = chain;
|
||||
link->DestroyHandler = (ILibChain_Destroy)&ILibChain_OnDestroyEvent_Sink;
|
||||
((void**)link->ExtraMemoryPtr)[0] = sink;
|
||||
((void**)link->ExtraMemoryPtr)[1] = user;
|
||||
ILibBaseChain *bchain = (ILibBaseChain*)chain;
|
||||
void **data = (void**)ILibMemory_Allocate(sizeof(void*) * 2, 0, NULL, NULL);
|
||||
data[0] = sink;
|
||||
data[1] = user;
|
||||
|
||||
if (ILibIsChainRunning(chain) == 0)
|
||||
if (bchain->OnDestroyEventSinks == NULL)
|
||||
{
|
||||
ILibAddToChain(chain, link);
|
||||
}
|
||||
else
|
||||
{
|
||||
ILibChain_SafeAdd(chain, link);
|
||||
bchain->OnDestroyEventSinks = ILibLinkedList_Create();
|
||||
}
|
||||
ILibLinkedList_AddTail(bchain->OnDestroyEventSinks, data);
|
||||
}
|
||||
void ILibChain_OnStartEvent_Sink(void* object, fd_set *readset, fd_set *writeset, fd_set *errorset, int* blocktime)
|
||||
{
|
||||
@@ -1530,12 +1583,17 @@ void ILibChain_RunOnMicrostackThreadSink(void *obj)
|
||||
void* chain = ((void**)obj)[0];
|
||||
ILibChain_StartEvent handler = (ILibChain_StartEvent)((void**)obj)[1];
|
||||
void* user = ((void**)obj)[2];
|
||||
void* freeOnShutdown = ((void**)obj)[3];
|
||||
|
||||
if (ILibIsChainBeingDestroyed(chain) == 0)
|
||||
{
|
||||
// Only Dispatch if the chain is still running
|
||||
if (handler != NULL) { handler(chain, user); }
|
||||
}
|
||||
else if (freeOnShutdown != NULL)
|
||||
{
|
||||
free(user);
|
||||
}
|
||||
free(obj);
|
||||
}
|
||||
//! Dispatch an operation to the Microstack Chain thread
|
||||
@@ -1544,15 +1602,16 @@ void ILibChain_RunOnMicrostackThreadSink(void *obj)
|
||||
\param handler Event to dispatch on the microstack thread
|
||||
\param user Custom user data to dispatch to the microstack thread
|
||||
*/
|
||||
void ILibChain_RunOnMicrostackThreadEx(void *chain, ILibChain_StartEvent handler, void *user)
|
||||
void ILibChain_RunOnMicrostackThreadEx2(void *chain, ILibChain_StartEvent handler, void *user, int freeOnShutdown)
|
||||
{
|
||||
void** value = NULL;
|
||||
|
||||
value = (void**)ILibMemory_Allocate(3 * sizeof(void*), 0, NULL, NULL);
|
||||
value = (void**)ILibMemory_Allocate(4 * sizeof(void*), 0, NULL, NULL);
|
||||
|
||||
value[0] = chain;
|
||||
value[1] = handler;
|
||||
value[2] = user;
|
||||
value[3] = (void*)(uint64_t)freeOnShutdown;
|
||||
ILibLifeTime_AddEx(ILibGetBaseTimer(chain), value, 0, &ILibChain_RunOnMicrostackThreadSink, &ILibChain_RunOnMicrostackThreadSink);
|
||||
}
|
||||
#ifdef WIN32
|
||||
@@ -1989,11 +2048,16 @@ void ILib_WindowsExceptionDebug(CONTEXT *exceptionContext)
|
||||
psym->MaxNameLen = MAX_SYM_NAME;
|
||||
pimg->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
||||
|
||||
len = sprintf_s(buffer, sizeof(buffer), "FATAL EXCEPTION [%s] @ ", (g_ILibCrashID!=NULL? g_ILibCrashID:""));
|
||||
len = sprintf_s(buffer, sizeof(buffer), "FATAL EXCEPTION [%s] @ ", (g_ILibCrashID != NULL ? g_ILibCrashID : ""));
|
||||
#ifdef WIN64
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "[FuncAddr: 0x%016llx / BaseAddr: 0x%016llx / Delta: %lld]\n", (unsigned __int64)StackFrame.AddrPC.Offset, (unsigned __int64)&ILibCreateChain, (unsigned __int64)&ILibCreateChain - (unsigned __int64)StackFrame.AddrPC.Offset);
|
||||
#else
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "[FuncAddr: 0x%08x / BaseAddr: 0x%08x / Delta: %d]\n", (unsigned __int32)StackFrame.AddrPC.Offset, (unsigned __int32)&ILibCreateChain, (unsigned __int32)&ILibCreateChain - (unsigned __int32)StackFrame.AddrPC.Offset);
|
||||
#endif
|
||||
|
||||
if (SymFromAddr(GetCurrentProcess(), StackFrame.AddrPC.Offset, &tmp, psym))
|
||||
{
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "[%s", (char*)(psym->Name));
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, " [%s", (char*)(psym->Name));
|
||||
if (SymGetLineFromAddr64(GetCurrentProcess(), StackFrame.AddrPC.Offset, &tmp2, pimg))
|
||||
{
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, " => %s:%d]\n", (char*)(pimg->FileName), pimg->LineNumber);
|
||||
@@ -2003,11 +2067,6 @@ void ILib_WindowsExceptionDebug(CONTEXT *exceptionContext)
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "]\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
util_tohex((char*)&(StackFrame.AddrPC.Offset), sizeof(DWORD64), imgBuffer);
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "[FuncAddr: 0x%s]\n", imgBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2078,6 +2137,7 @@ void ILib_POSIX_CrashHandler(int code)
|
||||
if ((strings[i])[c] == ']') { (strings[i])[c] = 0; }
|
||||
if ((strings[i])[c] == 0) { break; }
|
||||
}
|
||||
|
||||
if (pipe(fd) == 0)
|
||||
{
|
||||
pid = vfork();
|
||||
@@ -2126,8 +2186,15 @@ char* ILib_POSIX_InstallCrashHandler(char *exename)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void ILibChain_DebugDelta(char *buffer, int bufferLen, uint64_t delta)
|
||||
{
|
||||
ILibChain_DebugOffset(buffer, bufferLen, (uint64_t)&ILibCreateChain - delta);
|
||||
}
|
||||
|
||||
void ILibChain_DebugOffset(char *buffer, int bufferLen, uint64_t addrOffset)
|
||||
{
|
||||
#ifndef _NOILIBSTACKDEBUG
|
||||
#ifdef WIN32
|
||||
int len = 0;
|
||||
char symBuffer[4096];
|
||||
@@ -2141,11 +2208,19 @@ void ILibChain_DebugOffset(char *buffer, int bufferLen, uint64_t addrOffset)
|
||||
pimg->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
||||
SymInitialize(GetCurrentProcess(), NULL, TRUE);
|
||||
|
||||
sprintf_s(buffer, bufferLen, "[Unable to decode function address]");
|
||||
#ifdef WIN64
|
||||
sprintf_s(buffer, bufferLen, "[Unable to decode function address, BaseAddr: 0x%016llx]", (unsigned __int64)&ILibCreateChain);
|
||||
#else
|
||||
sprintf_s(buffer, bufferLen, "[Unable to decode function address, BaseAddr: 0x%08x]", (unsigned __int32)&ILibCreateChain);
|
||||
#endif
|
||||
|
||||
if (SymFromAddr(GetCurrentProcess(), (DWORD64)addrOffset, &tmp, psym))
|
||||
{
|
||||
len += sprintf_s(buffer + len, bufferLen - len, "[%s", (char*)(psym->Name));
|
||||
#ifdef WIN64
|
||||
len += sprintf_s(buffer + len, bufferLen - len, "[BaseAddr: 0x%016llx, %s", (unsigned __int64)&ILibCreateChain, (char*)(psym->Name));
|
||||
#else
|
||||
len += sprintf_s(buffer + len, bufferLen - len, "[BaseAddr: 0x%08x, %s", (unsigned __int32)&ILibCreateChain, (char*)(psym->Name));
|
||||
#endif
|
||||
if (SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)addrOffset, &tmp2, pimg))
|
||||
{
|
||||
len += sprintf_s(buffer + len, bufferLen - len, " => %s:%d]\n", (char*)(pimg->FileName), pimg->LineNumber);
|
||||
@@ -2155,8 +2230,54 @@ void ILibChain_DebugOffset(char *buffer, int bufferLen, uint64_t addrOffset)
|
||||
len += sprintf_s(buffer + len, bufferLen - len, "]\n");
|
||||
}
|
||||
}
|
||||
#else
|
||||
char addrtmp[255];
|
||||
int len = 0;
|
||||
pid_t pid;
|
||||
int fd[2];
|
||||
|
||||
sprintf_s(addrtmp, sizeof(addrtmp), "0x%016"PRIx64, addrOffset);
|
||||
((char**)ILib_POSIX_CrashParamBuffer)[1] = addrtmp;
|
||||
|
||||
if (pipe(fd) == 0)
|
||||
{
|
||||
pid = vfork();
|
||||
if (pid == 0)
|
||||
{
|
||||
dup2(fd[1], STDOUT_FILENO);
|
||||
close(fd[1]);
|
||||
|
||||
execv("/usr/bin/addr2line", (char**)ILib_POSIX_CrashParamBuffer);
|
||||
if (write(STDOUT_FILENO, "??:0", 4)) {}
|
||||
exit(0);
|
||||
}
|
||||
if (pid > 0)
|
||||
{
|
||||
char tmp[8192];
|
||||
int rlen;
|
||||
|
||||
rlen = read(fd[0], tmp, 8192);
|
||||
if (rlen > 0 && tmp[0] != '?')
|
||||
{
|
||||
memcpy_s(buffer + len, bufferLen - len, "=> ", 3);
|
||||
len += 3;
|
||||
memcpy_s(buffer + len, bufferLen - len, tmp, rlen);
|
||||
len += rlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy_s(buffer + len, bufferLen - len, "=> NOT FOUND", 12);
|
||||
len += 12;
|
||||
}
|
||||
close(fd[0]);
|
||||
}
|
||||
|
||||
buffer[len] = 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
char* ILibChain_Debug(void *chain, char* buffer, int bufferLen)
|
||||
{
|
||||
char *retVal = NULL;
|
||||
@@ -2584,6 +2705,25 @@ ILibExportMethod void ILibStartChain(void *Chain)
|
||||
#endif
|
||||
}
|
||||
|
||||
// Before we start, lets signal that the chain is stopping
|
||||
if (chain->OnDestroyEventSinks != NULL)
|
||||
{
|
||||
void *n = ILibLinkedList_GetNode_Head(chain->OnDestroyEventSinks);
|
||||
while (n != NULL)
|
||||
{
|
||||
void **edata = (void**)ILibLinkedList_GetDataFromNode(n);
|
||||
if (edata != NULL)
|
||||
{
|
||||
((ILibChain_DestroyEvent)edata[0])(chain, edata[1]);
|
||||
free(edata);
|
||||
}
|
||||
n = ILibLinkedList_GetNextNode(n);
|
||||
}
|
||||
ILibLinkedList_Destroy(chain->OnDestroyEventSinks);
|
||||
chain->OnDestroyEventSinks = NULL;
|
||||
}
|
||||
|
||||
|
||||
// Because many modules in the chain are using the base chain timer which is the first node
|
||||
// in the chain in the base timer), these modules may start cleaning up their timers. So, we
|
||||
// are going to make an exception and Destroy the base timer last, something that would usualy
|
||||
@@ -4681,7 +4821,7 @@ struct packetheader* ILibParsePacketHeader(char* buffer, int offset, int length)
|
||||
RetVal->StatusCode = (int)atoi(tempbuffer);
|
||||
free(tempbuffer);
|
||||
RetVal->StatusData = StartLine->FirstResult->NextResult->NextResult->data;
|
||||
RetVal->StatusDataLength = StartLine->FirstResult->NextResult->NextResult->datalength;
|
||||
RetVal->StatusDataLength = (int)((f->data + f->datalength) - RetVal->StatusData);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -5218,7 +5358,7 @@ ILibParseUriResult ILibParseUriEx (const char* URI, size_t URILen, char** Addr,
|
||||
}
|
||||
AddrStruct->sin6_port = (unsigned short)htons(lport);
|
||||
}
|
||||
else
|
||||
else if(laddr!=NULL)
|
||||
{
|
||||
// IPv4
|
||||
AddrStruct->sin6_family = AF_INET;
|
||||
@@ -5557,7 +5697,7 @@ void ILibencodeblock( unsigned char in[3], unsigned char out[4], int len )
|
||||
{
|
||||
out[0] = cb64[ in[0] >> 2 ];
|
||||
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
|
||||
out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
|
||||
out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | (len>2?((in[2] & 0xc0) >> 6):0) ] : '=');
|
||||
out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
|
||||
}
|
||||
|
||||
@@ -5590,10 +5730,26 @@ int ILibBase64Encode(unsigned char* input, const int inputlen, unsigned char** o
|
||||
out = *output;
|
||||
in = input;
|
||||
|
||||
if (input == NULL || inputlen == 0) { *output = NULL; return 0; }
|
||||
while ((in+3) <= (input+inputlen)) { ILibencodeblock(in, out, 3); in += 3; out += 4; }
|
||||
if ((input+inputlen)-in == 1) { ILibencodeblock(in, out, 1); out += 4; }
|
||||
else if ((input+inputlen)-in == 2) { ILibencodeblock(in, out, 2); out += 4; }
|
||||
if (input == NULL || inputlen == 0)
|
||||
{
|
||||
*output = NULL; return 0;
|
||||
}
|
||||
while ((in+3) <= (input+inputlen))
|
||||
{
|
||||
ILibencodeblock(in, out, 3);
|
||||
in += 3;
|
||||
out += 4;
|
||||
}
|
||||
if ((input+inputlen)-in == 1)
|
||||
{
|
||||
ILibencodeblock(in, out, 1);
|
||||
out += 4;
|
||||
}
|
||||
else if ((input+inputlen)-in == 2)
|
||||
{
|
||||
ILibencodeblock(in, out, 2);
|
||||
out += 4;
|
||||
}
|
||||
*out = 0;
|
||||
|
||||
return (int)(out-*output);
|
||||
@@ -6056,15 +6212,15 @@ void ILibLifeTime_Remove(void *LifeTimeToken, void *data)
|
||||
node = ILibLinkedList_GetNextNode(node);
|
||||
}
|
||||
}
|
||||
if (removed == 0)
|
||||
{
|
||||
//
|
||||
// The item wasn't in the list, so maybe it is pending to be triggered
|
||||
//
|
||||
ILibLinkedList_Lock(UPnPLifeTime->Reserved);
|
||||
ILibLinkedList_AddTail(UPnPLifeTime->Reserved, data);
|
||||
ILibLinkedList_UnLock(UPnPLifeTime->Reserved);
|
||||
}
|
||||
}
|
||||
if (removed == 0)
|
||||
{
|
||||
//
|
||||
// The item wasn't in the list, so maybe it is pending to be triggered
|
||||
//
|
||||
ILibLinkedList_Lock(UPnPLifeTime->Reserved);
|
||||
ILibLinkedList_AddTail(UPnPLifeTime->Reserved, data);
|
||||
ILibLinkedList_UnLock(UPnPLifeTime->Reserved);
|
||||
}
|
||||
ILibLinkedList_UnLock(UPnPLifeTime->ObjectList);
|
||||
|
||||
@@ -7040,7 +7196,7 @@ void* ILibSparseArray_Add(ILibSparseArray sarray, int index, void *data)
|
||||
else if(root->bucket[i].index < 0)
|
||||
{
|
||||
// Need to use Linked List
|
||||
ILibSparseArray_Node* n = (ILibSparseArray_Node*)malloc(sizeof(ILibSparseArray_Node));
|
||||
ILibSparseArray_Node* n = (ILibSparseArray_Node*)ILibMemory_Allocate(sizeof(ILibSparseArray_Node), 0, NULL, NULL);
|
||||
n->index = index;
|
||||
n->ptr = data;
|
||||
n = (ILibSparseArray_Node*)ILibLinkedList_SortedInsert(root->bucket[i].ptr, &ILibSparseArray_Comparer, n);
|
||||
@@ -7063,7 +7219,7 @@ void* ILibSparseArray_Add(ILibSparseArray sarray, int index, void *data)
|
||||
else
|
||||
{
|
||||
// We need to create a linked list, add the old value, then insert our new value (No return value)
|
||||
ILibSparseArray_Node* n = (ILibSparseArray_Node*)malloc(sizeof(ILibSparseArray_Node));
|
||||
ILibSparseArray_Node* n = (ILibSparseArray_Node*)ILibMemory_Allocate(sizeof(ILibSparseArray_Node), 0, NULL, NULL);
|
||||
n->index = root->bucket[i].index;
|
||||
n->ptr = root->bucket[i].ptr;
|
||||
|
||||
@@ -7071,7 +7227,7 @@ void* ILibSparseArray_Add(ILibSparseArray sarray, int index, void *data)
|
||||
root->bucket[i].ptr = ILibLinkedList_Create();
|
||||
ILibLinkedList_AddHead(root->bucket[i].ptr, n);
|
||||
|
||||
n = (ILibSparseArray_Node*)malloc(sizeof(ILibSparseArray_Node));
|
||||
n = (ILibSparseArray_Node*)(ILibSparseArray_Node*)ILibMemory_Allocate(sizeof(ILibSparseArray_Node), 0, NULL, NULL);
|
||||
n->index = index;
|
||||
n->ptr = data;
|
||||
ILibLinkedList_SortedInsert(root->bucket[i].ptr, &ILibSparseArray_Comparer, n);
|
||||
@@ -7852,6 +8008,7 @@ long long ILibGetUptime()
|
||||
if (hlib == NULL) return 0;
|
||||
pILibGetUptimeGetTickCount64 = (ULONGLONG(*)())GetProcAddress(hlib, "GetTickCount64");
|
||||
ILibGetUptimeFirst = 0;
|
||||
FreeLibrary(hlib);
|
||||
}
|
||||
if (pILibGetUptimeGetTickCount64 != NULL) return pILibGetUptimeGetTickCount64();
|
||||
|
||||
@@ -8810,8 +8967,14 @@ int ILibInetCompare(struct sockaddr* addr1, struct sockaddr* addr2, int compare)
|
||||
int ILibResolveEx3(char* hostname, char *service, struct sockaddr_in6* addr6, int addr6Count)
|
||||
{
|
||||
int r, i = 0;
|
||||
#ifdef WIN32
|
||||
ADDRINFOW *result = NULL, *current = NULL;
|
||||
ADDRINFOW hints;
|
||||
#else
|
||||
struct addrinfo *result = NULL, *current = NULL;
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
|
||||
int len = 0;
|
||||
|
||||
if (addr6 != NULL && addr6Count > 0)
|
||||
@@ -8823,19 +8986,29 @@ int ILibResolveEx3(char* hostname, char *service, struct sockaddr_in6* addr6, in
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
#ifndef WIN32
|
||||
#ifdef WIN32
|
||||
if (hostname[0] == '[')
|
||||
{
|
||||
int hostnameLen = strnlen_s(hostname, 4096);
|
||||
char *newHost = alloca(hostnameLen);
|
||||
int hostnameLen = (int)strnlen_s(hostname, 4096);
|
||||
char *newHost = _alloca((size_t)hostnameLen);
|
||||
memcpy_s(newHost, hostnameLen, hostname + 1, hostnameLen - 2);
|
||||
newHost[hostnameLen - 2] = 0;
|
||||
hostname = newHost;
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t hnameLen = hostname == NULL ? 0 : (2 + (2 * MultiByteToWideChar(CP_UTF8, 0, (LPCCH)hostname, -1, NULL, 0)));
|
||||
size_t svcLen = service == NULL ? 0 : (2 + (2 * MultiByteToWideChar(CP_UTF8, 0, (LPCCH)service, -1, NULL, 0)));
|
||||
PCWSTR hname = hnameLen == 0 ? NULL : _alloca(hnameLen);
|
||||
PCWSTR svc = svcLen == 0 ? NULL : _alloca(svcLen);
|
||||
if (hname != NULL) { MultiByteToWideChar(CP_UTF8, 0, (LPCCH)hostname, -1, (LPWSTR)hname, (int)hnameLen); }
|
||||
if (svc != NULL) { MultiByteToWideChar(CP_UTF8, 0, (LPCCH)service, -1, (LPWSTR)svc, (int)svcLen); }
|
||||
r = GetAddrInfoW(hname, svc, &hints, &result);
|
||||
if (r != 0) { if (result != NULL) { FreeAddrInfoW(result); } return r; }
|
||||
#else
|
||||
r = getaddrinfo(hostname, service, &hints, &result);
|
||||
if (r != 0) { if (result != NULL) { freeaddrinfo(result); } return r; }
|
||||
#endif
|
||||
|
||||
if (result == NULL) return -1;
|
||||
|
||||
// Determine Number of results
|
||||
@@ -8865,7 +9038,11 @@ int ILibResolveEx3(char* hostname, char *service, struct sockaddr_in6* addr6, in
|
||||
current = current->ai_next;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
FreeAddrInfoW(result);
|
||||
#else
|
||||
freeaddrinfo(result);
|
||||
#endif
|
||||
return i;
|
||||
}
|
||||
int ILibResolveEx2(char* hostname, unsigned short port, struct sockaddr_in6* addr6, int count)
|
||||
|
||||
@@ -160,7 +160,7 @@ long ILibGetTimeStamp();
|
||||
#endif
|
||||
void ILibGetTimeOfDay(struct timeval *tp);
|
||||
|
||||
/*
|
||||
|
||||
// Implemented in Windows using events.
|
||||
#define sem_t HANDLE
|
||||
#define sem_init(x,pShared,InitValue) *x=CreateSemaphore(NULL,InitValue,SEM_MAX_COUNT,NULL)
|
||||
@@ -168,15 +168,15 @@ long ILibGetTimeStamp();
|
||||
#define sem_wait(x) WaitForSingleObject(*x,INFINITE)
|
||||
#define sem_trywait(x) ((WaitForSingleObject(*x,0)==WAIT_OBJECT_0)?0:1)
|
||||
#define sem_post(x) ReleaseSemaphore(*x,1,NULL)
|
||||
*/
|
||||
|
||||
// Implemented in Windows using critical section.
|
||||
#define sem_t CRITICAL_SECTION
|
||||
#define sem_init(x,pShared,InitValue) InitializeCriticalSection(x);
|
||||
#define sem_destroy(x) (DeleteCriticalSection(x))
|
||||
#define sem_wait(x) EnterCriticalSection(x)
|
||||
#define sem_trywait(x) TryEnterCriticalSection(x)
|
||||
#define sem_post(x) LeaveCriticalSection(x)
|
||||
|
||||
//// Implemented in Windows using critical section.
|
||||
//#define sem_t CRITICAL_SECTION
|
||||
//#define sem_init(x,pShared,InitValue) InitializeCriticalSection(x);
|
||||
//#define sem_destroy(x) (DeleteCriticalSection(x))
|
||||
//#define sem_wait(x) EnterCriticalSection(x)
|
||||
//#define sem_trywait(x) TryEnterCriticalSection(x)
|
||||
//#define sem_post(x) LeaveCriticalSection(x)
|
||||
|
||||
#define strncasecmp(x,y,z) _strnicmp(x,y,z)
|
||||
#define strcasecmp(x,y) _stricmp(x,y)
|
||||
@@ -301,18 +301,56 @@ int ILibIsRunningOnChainThread(void* chain);
|
||||
ILibChain_Link* ILibChain_Link_Allocate(int structSize, int extraMemorySize);
|
||||
int ILibChain_Link_GetExtraMemorySize(ILibChain_Link* link);
|
||||
|
||||
typedef enum ILibMemory_Types
|
||||
{
|
||||
ILibMemory_Types_HEAP = 0,
|
||||
ILibMemory_Types_STACK = 1,
|
||||
ILibMemory_Types_OTHER = 2
|
||||
}ILibMemory_Types;
|
||||
|
||||
typedef struct ILibMemory_Header
|
||||
{
|
||||
size_t size;
|
||||
size_t extraSize;
|
||||
int CANARY;
|
||||
ILibMemory_Types memoryType;
|
||||
}ILibMemory_Header;
|
||||
|
||||
#define ILibMemory_Canary (((int*)((char*)(const char*)"broe"))[0])
|
||||
#define ILibMemory_RawPtr(ptr) ((char*)(ptr) - sizeof(ILibMemory_Header))
|
||||
#define ILibMemory_RawSize(ptr) (ILibMemory_Size((ptr)) + ILibMemory_ExtraSize((ptr)) + sizeof(ILibMemory_Header))
|
||||
#define ILibMemory_MemType(ptr) (((ILibMemory_Header*)ILibMemory_RawPtr((ptr)))->memoryType)
|
||||
#define ILibMemory_Size(ptr) (((ILibMemory_Header*)ILibMemory_RawPtr((ptr)))->size)
|
||||
#define ILibMemory_ExtraSize(ptr) (((ILibMemory_Header*)ILibMemory_RawPtr((ptr)))->extraSize)
|
||||
#define ILibMemory_Ex_CanaryOK(ptr) ((((ILibMemory_Header*)ILibMemory_RawPtr((ptr)))->CANARY) == ILibMemory_Canary)
|
||||
#ifdef WIN32
|
||||
#define ILibMemory_AllocateA(bufferLen) ILibMemory_AllocateA_InitMem(_alloca(8+bufferLen+sizeof(void*)), (size_t)(8+bufferLen+sizeof(void*)))
|
||||
int ILibMemory_CanaryOK(void *ptr);
|
||||
#else
|
||||
#define ILibMemory_AllocateA(bufferLen) ILibMemory_AllocateA_InitMem(alloca(8+bufferLen+sizeof(void*)), (size_t)(8+bufferLen+sizeof(void*)))
|
||||
#define ILibMemory_CanaryOK(ptr) ILibMemory_Ex_CanaryOK(ptr)
|
||||
#endif
|
||||
#define ILibMemory_AllocateA_Size(buffer) (((unsigned int*)((char*)(buffer)-4))[0])
|
||||
#define ILibMemory_AllocateA_Next(buffer) (((void**)((char*)(buffer)-4-sizeof(void*)))[0])
|
||||
#define ILibMemory_AllocateA_Raw(buffer) ((void*)((char*)(buffer)-4-sizeof(void*)-4))
|
||||
#define ILibMemory_AllocateA_RawSize(buffer) (((unsigned int*)((char*)(buffer)-4-sizeof(void*)-4))[0])
|
||||
|
||||
#define ILibMemory_Extra(ptr) (ILibMemory_ExtraSize(ptr)>0?((char*)(ptr) + ILibMemory_Size((ptr)) + sizeof(ILibMemory_Header)):NULL)
|
||||
#define ILibMemory_FromRaw(ptr) ((char*)(ptr) + sizeof(ILibMemory_Header))
|
||||
|
||||
void* ILibMemory_Init(void *ptr, size_t primarySize, size_t extraSize, ILibMemory_Types memType);
|
||||
#define ILibMemory_SmartAllocate(len) ILibMemory_Init(malloc(len+sizeof(ILibMemory_Header)), (int)len, 0, ILibMemory_Types_HEAP)
|
||||
#define ILibMemory_SmartAllocateEx(primaryLen, extraLen) ILibMemory_Init(malloc(primaryLen + extraLen + sizeof(ILibMemory_Header) + (extraLen>0?sizeof(ILibMemory_Header):0)), (int)primaryLen, (int)extraLen, ILibMemory_Types_HEAP)
|
||||
void ILibMemory_Free(void *ptr);
|
||||
void* ILibMemory_AllocateTemp(void* chain, size_t sz);
|
||||
|
||||
#ifdef WIN32
|
||||
#define ILibMemory_AllocateA(bufferLen) ILibMemory_AllocateA_Init(ILibMemory_Init(_alloca(bufferLen + sizeof(void*) + (2*sizeof(ILibMemory_Header))), bufferLen, sizeof(void*), ILibMemory_Types_STACK))
|
||||
#else
|
||||
#define ILibMemory_AllocateA(bufferLen) ILibMemory_AllocateA_Init(ILibMemory_Init(alloca(bufferLen + sizeof(void*) + (2*sizeof(ILibMemory_Header))), bufferLen, sizeof(void*), ILibMemory_Types_STACK))
|
||||
#endif
|
||||
#define ILibMemory_AllocateA_Size(buffer) ILibMemory_Size(buffer)
|
||||
#define ILibMemory_AllocateA_Next(buffer) (((void**)buffer)[0])
|
||||
#define ILibMemory_AllocateA_Raw(buffer) ILibMemory_RawPtr(buffer)
|
||||
#define ILibMemory_AllocateA_RawSize(buffer) ILibMemory_RawSize(buffer);
|
||||
|
||||
void* ILibMemory_AllocateA_Get(void *buffer, size_t sz);
|
||||
void* ILibMemory_AllocateA_InitMem(void *buffer, size_t bufferLen);
|
||||
void* ILibMemory_AllocateA_Init(void *buffer);
|
||||
|
||||
void* ILibMemory_Allocate(int containerSize, int extraMemorySize, void** allocatedContainer, void **extraMemory);
|
||||
int ILibMemory_GetExtraMemorySize(void* extraMemory);
|
||||
ILibExportMethod void* ILibMemory_GetExtraMemory(void *container, int containerSize);
|
||||
@@ -857,8 +895,10 @@ int ILibIsRunningOnChainThread(void* chain);
|
||||
ILibExportMethod void ILibChain_EndContinue(void *chain);
|
||||
|
||||
void ILibForceUnBlockChain(void *Chain);
|
||||
void ILibChain_RunOnMicrostackThreadEx(void *chain, ILibChain_StartEvent handler, void *user);
|
||||
void ILibChain_RunOnMicrostackThreadEx2(void *chain, ILibChain_StartEvent handler, void *user, int freeOnShutdown);
|
||||
#define ILibChain_RunOnMicrostackThreadEx(chain, handler, user) ILibChain_RunOnMicrostackThreadEx2(chain, handler, user, 0)
|
||||
#define ILibChain_RunOnMicrostackThread(chain, handler, user) if(ILibIsRunningOnChainThread(chain)==0){ILibChain_RunOnMicrostackThreadEx(chain, handler, user);}else{handler(chain,user);}
|
||||
#define ILibChain_RunOnMicrostackThread2(chain, handler, user, freeOnShutdown) if(ILibIsRunningOnChainThread(chain)==0){ILibChain_RunOnMicrostackThreadEx2(chain, handler, user, freeOnShutdown);}else{handler(chain,user);}
|
||||
#ifdef WIN32
|
||||
HANDLE ILibChain_GetMicrostackThreadHandle(void *chain);
|
||||
#endif
|
||||
@@ -1280,6 +1320,7 @@ int ILibIsRunningOnChainThread(void* chain);
|
||||
//
|
||||
// Used to log critical problems
|
||||
//
|
||||
void ILibChain_DebugDelta(char *buffer, int bufferLen, uint64_t addrOffset);
|
||||
void ILibChain_DebugOffset(char *buffer, int bufferLen, uint64_t addrOffset);
|
||||
char* ILibChain_Debug(void *chain, char* buffer, int bufferLen);
|
||||
extern char* g_ILibCrashID;
|
||||
|
||||
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#ifdef MEMORY_CHECK
|
||||
#include <assert.h>
|
||||
#ifdef MEMORY_CHECK
|
||||
#define MEMCHECK(x) x
|
||||
#else
|
||||
#define MEMCHECK(x)
|
||||
@@ -30,7 +30,6 @@ limitations under the License.
|
||||
#include "ILibParsers.h"
|
||||
#include "ILibRemoteLogging.h"
|
||||
#include "ILibProcessPipe.h"
|
||||
#include <assert.h>
|
||||
#ifndef WIN32
|
||||
#include <fcntl.h> /* Obtain O_* constant definitions */
|
||||
#include <unistd.h>
|
||||
@@ -67,7 +66,7 @@ typedef struct ILibProcessPipe_PipeObject
|
||||
char* buffer;
|
||||
int bufferSize;
|
||||
|
||||
int readOffset;
|
||||
int readOffset, readNewOffset;
|
||||
int totalRead;
|
||||
int processingLoop;
|
||||
|
||||
@@ -276,7 +275,20 @@ void ILibProcessPipe_Manager_WindowsRunLoopEx(void *arg)
|
||||
if (x == WAIT_FAILED || (x-WAIT_OBJECT_0) == 0) { break; }
|
||||
data = (ILibProcessPipe_WaitHandle*)hList[x + FD_SETSIZE];
|
||||
manager->activeWaitHandle = data;
|
||||
if (data != NULL && data->callback != NULL) { data->callback(data->event, data->user); }
|
||||
if (data != NULL && data->callback != NULL)
|
||||
{
|
||||
if (data->callback(data->event, data->user) == FALSE)
|
||||
{
|
||||
// FALSE means to remove the WaitHandle
|
||||
void *node = ILibLinkedList_GetNode_Search(manager->ActivePipes, ILibProcessPipe_Manager_WindowsWaitHandles_Remove_Comparer, data->event);
|
||||
if (node != NULL)
|
||||
{
|
||||
free(ILibLinkedList_GetDataFromNode(node));
|
||||
ILibLinkedList_Remove(node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ResetEvent(manager->updateEvent);
|
||||
}
|
||||
@@ -503,21 +515,40 @@ ILibProcessPipe_PipeObject* ILibProcessPipe_CreatePipe(ILibProcessPipe_Manager m
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
typedef struct ILibProcessPipe_Process_Destroy_WinRunThread_Data
|
||||
{
|
||||
ILibProcessPipe_Process_Object *pj;
|
||||
HANDLE h;
|
||||
}ILibProcessPipe_Process_Destroy_WinRunThread_Data;
|
||||
void __stdcall ILibProcessPipe_Process_Destroy_WinRunThread(ULONG_PTR obj)
|
||||
{
|
||||
ILibProcessPipe_Process_Object *p = (ILibProcessPipe_Process_Object*)obj;
|
||||
if (p->exiting != 0) { return; }
|
||||
if (p->stdIn != NULL) { ILibProcessPipe_FreePipe(p->stdIn); }
|
||||
if (p->stdOut != NULL) { ILibProcessPipe_FreePipe(p->stdOut); }
|
||||
if (p->stdErr != NULL) { ILibProcessPipe_FreePipe(p->stdErr); }
|
||||
free(p);
|
||||
ILibProcessPipe_Process_Destroy_WinRunThread_Data *data = (ILibProcessPipe_Process_Destroy_WinRunThread_Data*)obj;
|
||||
if (ILibMemory_CanaryOK(data) && ILibMemory_CanaryOK(data->pj))
|
||||
{
|
||||
if (data->pj->exiting == 0)
|
||||
{
|
||||
if (ILibMemory_CanaryOK(data) && ILibMemory_CanaryOK(data->pj) && data->pj->stdIn != NULL) { ILibProcessPipe_FreePipe(data->pj->stdIn); }
|
||||
if (ILibMemory_CanaryOK(data) && ILibMemory_CanaryOK(data->pj) && data->pj->stdOut != NULL) { ILibProcessPipe_FreePipe(data->pj->stdOut); }
|
||||
if (ILibMemory_CanaryOK(data) && ILibMemory_CanaryOK(data->pj) && data->pj->stdErr != NULL) { ILibProcessPipe_FreePipe(data->pj->stdErr); }
|
||||
if (ILibMemory_CanaryOK(data) && ILibMemory_CanaryOK(data->pj)) { ILibMemory_Free(data->pj); }
|
||||
}
|
||||
}
|
||||
SetEvent(data->h);
|
||||
}
|
||||
#endif
|
||||
void ILibProcessPipe_Process_Destroy(ILibProcessPipe_Process_Object *p)
|
||||
{
|
||||
if (!ILibMemory_CanaryOK(p)) { return; }
|
||||
|
||||
#ifdef WIN32
|
||||
ILibProcessPipe_Process_Destroy_WinRunThread_Data *data = ILibMemory_AllocateA(sizeof(ILibProcessPipe_Process_Destroy_WinRunThread_Data));
|
||||
data->pj = p;
|
||||
data->h = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
// We can't destroy this now, because we're on the MicrostackThread. We must destroy this on the WindowsRunLoop Thread.
|
||||
QueueUserAPC((PAPCFUNC)ILibProcessPipe_Process_Destroy_WinRunThread, p->parent->workerThread, (ULONG_PTR)p);
|
||||
QueueUserAPC((PAPCFUNC)ILibProcessPipe_Process_Destroy_WinRunThread, p->parent->workerThread, (ULONG_PTR)data);
|
||||
WaitForSingleObjectEx(data->h, 3000, TRUE);
|
||||
CloseHandle(data->h);
|
||||
#else
|
||||
if (p->exiting != 0) { return; }
|
||||
if (p->stdIn != NULL) { ILibProcessPipe_FreePipe(p->stdIn); }
|
||||
@@ -542,15 +573,12 @@ void ILibProcessPipe_Process_BrokenPipeSink(ILibProcessPipe_Pipe sender)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
void* ILibProcessPipe_Process_KillEx(ILibProcessPipe_Process p)
|
||||
{
|
||||
void *retVal = ILibProcessPipe_Process_Kill(p);
|
||||
ILibProcessPipe_Process_Destroy((ILibProcessPipe_Process_Object*)p);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void ILibProcessPipe_Process_SoftKill(ILibProcessPipe_Process p)
|
||||
{
|
||||
ILibProcessPipe_Process_Object* j = (ILibProcessPipe_Process_Object*)p;
|
||||
if (!ILibMemory_CanaryOK(p)) { return; }
|
||||
|
||||
#ifdef WIN32
|
||||
TerminateProcess(j->hProcess, 1067);
|
||||
#else
|
||||
@@ -559,36 +587,8 @@ void ILibProcessPipe_Process_SoftKill(ILibProcessPipe_Process p)
|
||||
waitpid((pid_t)j->PID, &code, 0);
|
||||
#endif
|
||||
}
|
||||
void* ILibProcessPipe_Process_Kill(ILibProcessPipe_Process p)
|
||||
{
|
||||
ILibProcessPipe_Process_Object* j = (ILibProcessPipe_Process_Object*)p;
|
||||
|
||||
#ifdef WIN32
|
||||
// First things first, unhook all the pipes from the Windows Run Loop
|
||||
if (ILibIsChainBeingDestroyed(j->chain) == 0)
|
||||
{
|
||||
if (j->stdIn != NULL && j->stdIn->mOverlapped != NULL) { ILibProcessPipe_WaitHandle_Remove(j->parent, j->stdIn->mOverlapped->hEvent); }
|
||||
if (j->stdOut != NULL && j->stdOut->mOverlapped != NULL) { ILibProcessPipe_WaitHandle_Remove(j->parent, j->stdOut->mOverlapped->hEvent); }
|
||||
if (j->stdErr != NULL && j->stdErr->mOverlapped != NULL) { ILibProcessPipe_WaitHandle_Remove(j->parent, j->stdErr->mOverlapped->hEvent); }
|
||||
if (j->hProcess != NULL) { ILibProcessPipe_WaitHandle_Remove(j->parent, j->hProcess); TerminateProcess(j->hProcess, 1067); }
|
||||
}
|
||||
else
|
||||
{
|
||||
TerminateProcess(j->hProcess, 1067);
|
||||
}
|
||||
#else
|
||||
int code;
|
||||
if (j->stdIn != NULL) { j->stdIn->brokenPipeHandler = NULL; }
|
||||
if (j->stdOut != NULL) { j->stdOut->brokenPipeHandler = NULL; }
|
||||
if (j->stdErr != NULL) { j->stdErr->brokenPipeHandler = NULL; }
|
||||
|
||||
kill((pid_t)j->PID, SIGKILL);
|
||||
waitpid((pid_t)j->PID, &code, 0);
|
||||
#endif
|
||||
return(j->userObject);
|
||||
}
|
||||
|
||||
ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_Manager pipeManager, char* target, char* const* parameters, ILibProcessPipe_SpawnTypes spawnType, int extraMemorySize)
|
||||
ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx3(ILibProcessPipe_Manager pipeManager, char* target, char* const* parameters, ILibProcessPipe_SpawnTypes spawnType, void *sid, int extraMemorySize)
|
||||
{
|
||||
ILibProcessPipe_Process_Object* retVal = NULL;
|
||||
|
||||
@@ -598,18 +598,19 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
SECURITY_ATTRIBUTES saAttr;
|
||||
char* parms = NULL;
|
||||
DWORD sessionId;
|
||||
HANDLE token=NULL, userToken=NULL, procHandle=NULL;
|
||||
HANDLE token = NULL, userToken = NULL, procHandle = NULL;
|
||||
int allocParms = 0;
|
||||
|
||||
ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
|
||||
ZeroMemory(&info, sizeof(STARTUPINFOA));
|
||||
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && (sessionId = WTSGetActiveConsoleSessionId()) == 0xFFFFFFFF) { return(NULL); } // No session attached to console, but requested to execute as logged in user
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_SPECIFIED_USER && spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && (sessionId = WTSGetActiveConsoleSessionId()) == 0xFFFFFFFF) { return(NULL); } // No session attached to console, but requested to execute as logged in user
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DEFAULT)
|
||||
{
|
||||
procHandle = GetCurrentProcess();
|
||||
procHandle = GetCurrentProcess();
|
||||
if (OpenProcessToken(procHandle, TOKEN_DUPLICATE, &token) == 0) { ILIBMARKPOSITION(2); return(NULL); }
|
||||
if (DuplicateTokenEx(token, MAXIMUM_ALLOWED, 0, SecurityImpersonation, TokenPrimary, &userToken) == 0) { CloseHandle(token); ILIBMARKPOSITION(2); return(NULL); }
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_SPECIFIED_USER) { sessionId = (DWORD)(uint64_t)sid; }
|
||||
if (SetTokenInformation(userToken, (TOKEN_INFORMATION_CLASS)TokenSessionId, &sessionId, sizeof(sessionId)) == 0) { CloseHandle(token); CloseHandle(userToken); ILIBMARKPOSITION(2); return(NULL); }
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_WINLOGON) { info.lpDesktop = "Winsta0\\Winlogon"; }
|
||||
}
|
||||
@@ -628,13 +629,13 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
sz += ((int)strnlen_s(parameters[i++], _MAX_PATH) + 1);
|
||||
}
|
||||
sz += (i - 1); // Need to make room for delimiter characters
|
||||
parms = (char*)malloc(sz);
|
||||
parms = (char*)malloc(sz);
|
||||
i = 0; len = 0;
|
||||
allocParms = 1;
|
||||
|
||||
while (parameters[i] != NULL)
|
||||
{
|
||||
len += sprintf_s(parms + len, sz - len, "%s%s", (i==0)?"":" ", parameters[i]);
|
||||
len += sprintf_s(parms + len, sz - len, "%s%s", (i == 0) ? "" : " ", parameters[i]);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
@@ -646,37 +647,45 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
pid_t pid;
|
||||
#endif
|
||||
|
||||
if ((retVal = (ILibProcessPipe_Process_Object*)malloc(sizeof(ILibProcessPipe_Process_Object))) == NULL) { ILIBCRITICALEXIT(254); }
|
||||
memset(retVal, 0, sizeof(ILibProcessPipe_Process_Object));
|
||||
|
||||
retVal->stdErr = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdErr->mProcess = retVal;
|
||||
retVal = (ILibProcessPipe_Process_Object*)ILibMemory_SmartAllocate(sizeof(ILibProcessPipe_Process_Object));
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
retVal->stdErr = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdErr->mProcess = retVal;
|
||||
}
|
||||
retVal->parent = (ILibProcessPipe_Manager_Object*)pipeManager;
|
||||
retVal->chain = retVal->parent->ChainLink.ParentChain;
|
||||
#ifdef WIN32
|
||||
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdIn->mProcess = retVal;
|
||||
retVal->stdOut = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdOut->mProcess = retVal;
|
||||
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdIn->mPipe_WriteEnd));
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdOut->mPipe_ReadEnd));
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdErr->mPipe_ReadEnd));
|
||||
|
||||
info.cb = sizeof(STARTUPINFOA);
|
||||
info.hStdError = retVal->stdErr->mPipe_WriteEnd;
|
||||
info.hStdInput = retVal->stdIn->mPipe_ReadEnd;
|
||||
info.hStdOutput = retVal->stdOut->mPipe_WriteEnd;
|
||||
info.dwFlags |= STARTF_USESTDHANDLES;
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdIn->mProcess = retVal;
|
||||
retVal->stdOut = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdOut->mProcess = retVal;
|
||||
|
||||
if((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessA(target, parms, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo)) ||
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdIn->mPipe_WriteEnd));
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdOut->mPipe_ReadEnd));
|
||||
ILibProcessPipe_PipeObject_DisableInherit(&(retVal->stdErr->mPipe_ReadEnd));
|
||||
|
||||
info.hStdError = retVal->stdErr->mPipe_WriteEnd;
|
||||
info.hStdInput = retVal->stdIn->mPipe_ReadEnd;
|
||||
info.hStdOutput = retVal->stdOut->mPipe_WriteEnd;
|
||||
info.dwFlags |= STARTF_USESTDHANDLES;
|
||||
}
|
||||
|
||||
if ((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessA(target, parms, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo)) ||
|
||||
(spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessAsUserA(userToken, target, parms, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo)))
|
||||
{
|
||||
ILibProcessPipe_FreePipe(retVal->stdErr);
|
||||
ILibProcessPipe_FreePipe(retVal->stdOut);
|
||||
ILibProcessPipe_FreePipe(retVal->stdIn);
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
ILibProcessPipe_FreePipe(retVal->stdErr);
|
||||
ILibProcessPipe_FreePipe(retVal->stdOut);
|
||||
ILibProcessPipe_FreePipe(retVal->stdIn);
|
||||
}
|
||||
if (allocParms != 0) { free(parms); }
|
||||
free(retVal);
|
||||
ILibMemory_Free(retVal);
|
||||
if (token != NULL) { CloseHandle(token); }
|
||||
if (userToken != NULL) { CloseHandle(userToken); }
|
||||
return(NULL);
|
||||
@@ -684,14 +693,21 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
|
||||
|
||||
if (allocParms != 0) { free(parms); }
|
||||
CloseHandle(retVal->stdOut->mPipe_WriteEnd); retVal->stdOut->mPipe_WriteEnd = NULL;
|
||||
CloseHandle(retVal->stdErr->mPipe_WriteEnd); retVal->stdErr->mPipe_WriteEnd = NULL;
|
||||
CloseHandle(retVal->stdIn->mPipe_ReadEnd); retVal->stdIn->mPipe_ReadEnd = NULL;
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
CloseHandle(retVal->stdOut->mPipe_WriteEnd); retVal->stdOut->mPipe_WriteEnd = NULL;
|
||||
CloseHandle(retVal->stdErr->mPipe_WriteEnd); retVal->stdErr->mPipe_WriteEnd = NULL;
|
||||
CloseHandle(retVal->stdIn->mPipe_ReadEnd); retVal->stdIn->mPipe_ReadEnd = NULL;
|
||||
}
|
||||
|
||||
retVal->hProcess = processInfo.hProcess;
|
||||
if (processInfo.hThread != NULL) CloseHandle(processInfo.hThread);
|
||||
retVal->PID = processInfo.dwProcessId;
|
||||
|
||||
if (token != NULL) { CloseHandle(token); token = NULL; }
|
||||
if (userToken != NULL) { CloseHandle(userToken); userToken = NULL; }
|
||||
#else
|
||||
int UID = (int)(uint64_t)sid;
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_TERM)
|
||||
{
|
||||
int pipe;
|
||||
@@ -700,8 +716,8 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
w.ws_col = CONSOLE_SCREEN_WIDTH;
|
||||
w.ws_xpixel = 0;
|
||||
w.ws_ypixel = 0;
|
||||
pid = forkpty(&pipe, NULL, NULL, &w);
|
||||
|
||||
pid = forkpty(&pipe, NULL, NULL, &w);
|
||||
|
||||
retVal->stdIn = ILibProcessPipe_Pipe_CreateFromExistingWithExtraMemory(pipeManager, pipe, extraMemorySize);
|
||||
retVal->stdIn->mProcess = retVal;
|
||||
retVal->stdOut = ILibProcessPipe_Pipe_CreateFromExistingWithExtraMemory(pipeManager, pipe, extraMemorySize);
|
||||
@@ -710,54 +726,76 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_
|
||||
}
|
||||
else
|
||||
{
|
||||
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdIn->mProcess = retVal;
|
||||
retVal->stdOut = ILibProcessPipe_CreatePipe(pipeManager, 4096, (ILibProcessPipe_GenericBrokenPipeHandler) ILibProcessPipe_Process_BrokenPipeSink, extraMemorySize);
|
||||
retVal->stdOut->mProcess = retVal;
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
|
||||
retVal->stdIn->mProcess = retVal;
|
||||
retVal->stdOut = ILibProcessPipe_CreatePipe(pipeManager, 4096, (ILibProcessPipe_GenericBrokenPipeHandler)ILibProcessPipe_Process_BrokenPipeSink, extraMemorySize);
|
||||
retVal->stdOut->mProcess = retVal;
|
||||
}
|
||||
pid = vfork();
|
||||
}
|
||||
if(pid < 0)
|
||||
if (pid < 0)
|
||||
{
|
||||
ILibProcessPipe_FreePipe(retVal->stdErr);
|
||||
ILibProcessPipe_FreePipe(retVal->stdOut);
|
||||
ILibProcessPipe_FreePipe(retVal->stdIn);
|
||||
free(retVal);
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
ILibProcessPipe_FreePipe(retVal->stdErr);
|
||||
ILibProcessPipe_FreePipe(retVal->stdOut);
|
||||
ILibProcessPipe_FreePipe(retVal->stdIn);
|
||||
}
|
||||
ILibMemory_Free(retVal);
|
||||
return(NULL);
|
||||
}
|
||||
if(pid==0)
|
||||
if (pid == 0)
|
||||
{
|
||||
close(retVal->stdErr->mPipe_ReadEnd); //close read end of stderr pipe
|
||||
dup2(retVal->stdErr->mPipe_WriteEnd, STDERR_FILENO);
|
||||
close(retVal->stdErr->mPipe_WriteEnd);
|
||||
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
close(retVal->stdErr->mPipe_ReadEnd); //close read end of stderr pipe
|
||||
dup2(retVal->stdErr->mPipe_WriteEnd, STDERR_FILENO);
|
||||
close(retVal->stdErr->mPipe_WriteEnd);
|
||||
}
|
||||
if (spawnType == ILibProcessPipe_SpawnTypes_TERM)
|
||||
{
|
||||
putenv("TERM=xterm");
|
||||
}
|
||||
else
|
||||
{
|
||||
close(retVal->stdIn->mPipe_WriteEnd); //close write end of stdin pipe
|
||||
close(retVal->stdOut->mPipe_ReadEnd); //close read end of stdout pipe
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
close(retVal->stdIn->mPipe_WriteEnd); //close write end of stdin pipe
|
||||
close(retVal->stdOut->mPipe_ReadEnd); //close read end of stdout pipe
|
||||
|
||||
dup2(retVal->stdIn->mPipe_ReadEnd, STDIN_FILENO);
|
||||
dup2(retVal->stdOut->mPipe_WriteEnd, STDOUT_FILENO);
|
||||
dup2(retVal->stdIn->mPipe_ReadEnd, STDIN_FILENO);
|
||||
dup2(retVal->stdOut->mPipe_WriteEnd, STDOUT_FILENO);
|
||||
|
||||
close(retVal->stdIn->mPipe_ReadEnd);
|
||||
close(retVal->stdOut->mPipe_WriteEnd);
|
||||
close(retVal->stdIn->mPipe_ReadEnd);
|
||||
close(retVal->stdOut->mPipe_WriteEnd);
|
||||
}
|
||||
}
|
||||
if (UID != -1)
|
||||
{
|
||||
setuid((uid_t)UID);
|
||||
}
|
||||
execv(target, parameters);
|
||||
exit(1);
|
||||
}
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_TERM)
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_TERM && spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
close(retVal->stdIn->mPipe_ReadEnd);
|
||||
close(retVal->stdOut->mPipe_WriteEnd);
|
||||
}
|
||||
close(retVal->stdErr->mPipe_WriteEnd);
|
||||
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
|
||||
{
|
||||
close(retVal->stdErr->mPipe_WriteEnd);
|
||||
}
|
||||
retVal->PID = pid;
|
||||
#endif
|
||||
return retVal;
|
||||
}
|
||||
int ILibProcessPipe_Process_IsDetached(ILibProcessPipe_Process p)
|
||||
{
|
||||
return(((ILibProcessPipe_Process_Object*)p)->stdErr == NULL && ((ILibProcessPipe_Process_Object*)p)->stdIn == NULL && ((ILibProcessPipe_Process_Object*)p)->stdOut == NULL);
|
||||
}
|
||||
void ILibProcessPipe_Pipe_SwapBuffers(ILibProcessPipe_Pipe obj, char* newBuffer, int newBufferLen, int newBufferReadOffset, int newBufferTotalBytesRead, char **oldBuffer, int *oldBufferLen, int *oldBufferReadOffset, int *oldBufferTotalBytesRead)
|
||||
{
|
||||
ILibProcessPipe_PipeObject *pipeObject = (ILibProcessPipe_PipeObject*)obj;
|
||||
@@ -1167,20 +1205,20 @@ DWORD ILibProcessPipe_Pipe_BackgroundReader(void *arg)
|
||||
{
|
||||
ILibProcessPipe_PipeObject *pipeObject = (ILibProcessPipe_PipeObject*)arg;
|
||||
DWORD bytesRead = 0;
|
||||
int consumed;
|
||||
int consumed = 0;
|
||||
|
||||
while (pipeObject->PAUSED == 0 || WaitForSingleObject(pipeObject->mPipe_Reader_ResumeEvent, INFINITE) == WAIT_OBJECT_0)
|
||||
{
|
||||
// Pipe is in ACTIVE state
|
||||
consumed = 0;
|
||||
pipeObject->PAUSED = 0;
|
||||
|
||||
while (pipeObject->readOffset != 0 && pipeObject->PAUSED == 0)
|
||||
while(consumed != 0 && pipeObject->PAUSED == 0 && (pipeObject->totalRead - pipeObject->readOffset)>0)
|
||||
{
|
||||
((ILibProcessPipe_GenericReadHandler)pipeObject->handler)(pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead - pipeObject->readOffset, &consumed, pipeObject->user1, pipeObject->user2);
|
||||
if (consumed == 0)
|
||||
{
|
||||
memmove_s(pipeObject->buffer, pipeObject->bufferSize, pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead - pipeObject->readOffset);
|
||||
pipeObject->readNewOffset = pipeObject->totalRead - pipeObject->readOffset;
|
||||
pipeObject->totalRead -= pipeObject->readOffset;
|
||||
pipeObject->readOffset = 0;
|
||||
}
|
||||
@@ -1189,6 +1227,7 @@ DWORD ILibProcessPipe_Pipe_BackgroundReader(void *arg)
|
||||
// Entire buffer consumed
|
||||
pipeObject->readOffset = 0;
|
||||
pipeObject->totalRead = 0;
|
||||
pipeObject->readNewOffset = 0;
|
||||
consumed = 0;
|
||||
}
|
||||
else
|
||||
@@ -1199,12 +1238,16 @@ DWORD ILibProcessPipe_Pipe_BackgroundReader(void *arg)
|
||||
}
|
||||
|
||||
if (pipeObject->PAUSED == 1) { continue; }
|
||||
if (!ReadFile(pipeObject->mPipe_ReadEnd, pipeObject->buffer + pipeObject->readOffset, pipeObject->bufferSize - pipeObject->readOffset, &bytesRead, NULL)) { break; }
|
||||
if (!ReadFile(pipeObject->mPipe_ReadEnd, pipeObject->buffer + pipeObject->readOffset + pipeObject->readNewOffset, pipeObject->bufferSize - pipeObject->readOffset - pipeObject->readNewOffset, &bytesRead, NULL)) { break; }
|
||||
|
||||
consumed = 0;
|
||||
pipeObject->totalRead += bytesRead;
|
||||
((ILibProcessPipe_GenericReadHandler)pipeObject->handler)(pipeObject->buffer + pipeObject->readOffset, pipeObject->totalRead - pipeObject->readOffset, &consumed, pipeObject->user1, pipeObject->user2);
|
||||
pipeObject->readOffset += consumed;
|
||||
if (consumed == 0)
|
||||
{
|
||||
pipeObject->readNewOffset = pipeObject->totalRead - pipeObject->readOffset;
|
||||
}
|
||||
}
|
||||
|
||||
if (pipeObject->brokenPipeHandler != NULL) { pipeObject->brokenPipeHandler(pipeObject); }
|
||||
@@ -1311,16 +1354,19 @@ void ILibProcessPipe_Process_UpdateUserObject(ILibProcessPipe_Process module, vo
|
||||
void ILibProcessPipe_Process_AddHandlers(ILibProcessPipe_Process module, int bufferSize, ILibProcessPipe_Process_ExitHandler exitHandler, ILibProcessPipe_Process_OutputHandler stdOut, ILibProcessPipe_Process_OutputHandler stdErr, ILibProcessPipe_Process_SendOKHandler sendOk, void *user)
|
||||
{
|
||||
ILibProcessPipe_Process_Object* j = (ILibProcessPipe_Process_Object*)module;
|
||||
j->userObject = user;
|
||||
j->exitHandler = exitHandler;
|
||||
if (j != NULL && ILibMemory_CanaryOK(j))
|
||||
{
|
||||
j->userObject = user;
|
||||
j->exitHandler = exitHandler;
|
||||
|
||||
ILibProcessPipe_Process_StartPipeReader(j->stdOut, bufferSize, &ILibProcessPipe_Process_PipeHandler_StdOut, j, stdOut);
|
||||
ILibProcessPipe_Process_StartPipeReader(j->stdErr, bufferSize, &ILibProcessPipe_Process_PipeHandler_StdOut, j, stdErr);
|
||||
ILibProcessPipe_Process_SetWriteHandler(j->stdIn, &ILibProcessPipe_Process_PipeHandler_StdIn, j, sendOk);
|
||||
|
||||
ILibProcessPipe_Process_StartPipeReader(j->stdOut, bufferSize, &ILibProcessPipe_Process_PipeHandler_StdOut, j, stdOut);
|
||||
ILibProcessPipe_Process_StartPipeReader(j->stdErr, bufferSize, &ILibProcessPipe_Process_PipeHandler_StdOut, j, stdErr);
|
||||
ILibProcessPipe_Process_SetWriteHandler(j->stdIn, &ILibProcessPipe_Process_PipeHandler_StdIn, j, sendOk);
|
||||
|
||||
#ifdef WIN32
|
||||
ILibProcessPipe_WaitHandle_Add(j->parent, j->hProcess, j, &ILibProcessPipe_Process_OnExit);
|
||||
ILibProcessPipe_WaitHandle_Add(j->parent, j->hProcess, j, &ILibProcessPipe_Process_OnExit);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
ILibTransport_DoneState ILibProcessPipe_Pipe_Write(ILibProcessPipe_Pipe po, char* buffer, int bufferLen, ILibTransport_MemoryOwnership ownership)
|
||||
@@ -1328,6 +1374,11 @@ ILibTransport_DoneState ILibProcessPipe_Pipe_Write(ILibProcessPipe_Pipe po, char
|
||||
ILibProcessPipe_PipeObject* pipeObject = (ILibProcessPipe_PipeObject*)po;
|
||||
ILibTransport_DoneState retVal = ILibTransport_DoneState_ERROR;
|
||||
|
||||
if (pipeObject == NULL)
|
||||
{
|
||||
return(ILibTransport_DoneState_ERROR);
|
||||
}
|
||||
|
||||
if (pipeObject->WriteBuffer == NULL)
|
||||
{
|
||||
pipeObject->WriteBuffer = ILibQueue_Create();
|
||||
@@ -1391,8 +1442,14 @@ ILibTransport_DoneState ILibProcessPipe_Pipe_Write(ILibProcessPipe_Pipe po, char
|
||||
ILibTransport_DoneState ILibProcessPipe_Process_WriteStdIn(ILibProcessPipe_Process p, char* buffer, int bufferLen, ILibTransport_MemoryOwnership ownership)
|
||||
{
|
||||
ILibProcessPipe_Process_Object *j = (ILibProcessPipe_Process_Object*)p;
|
||||
|
||||
return(ILibProcessPipe_Pipe_Write(j->stdIn, buffer, bufferLen, ownership));
|
||||
if (ILibMemory_CanaryOK(j))
|
||||
{
|
||||
return(ILibProcessPipe_Pipe_Write(j->stdIn, buffer, bufferLen, ownership));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(ILibTransport_DoneState_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
void ILibProcessPipe_Pipe_ReadSink(char *buffer, int bufferLen, int* bytesConsumed, void* user1, void* user2)
|
||||
@@ -1410,4 +1467,3 @@ DWORD ILibProcessPipe_Process_GetPID(ILibProcessPipe_Process p) { return(p != NU
|
||||
#else
|
||||
pid_t ILibProcessPipe_Process_GetPID(ILibProcessPipe_Process p) { return(p != NULL ? (pid_t)((ILibProcessPipe_Process_Object*)p)->PID : 0); }
|
||||
#endif
|
||||
|
||||
|
||||
@@ -39,6 +39,8 @@ typedef enum ILibProcessPipe_SpawnTypes
|
||||
ILibProcessPipe_SpawnTypes_USER = 1,
|
||||
ILibProcessPipe_SpawnTypes_WINLOGON = 2,
|
||||
ILibProcessPipe_SpawnTypes_TERM = 3,
|
||||
ILibProcessPipe_SpawnTypes_DETACHED = 4,
|
||||
ILibProcessPipe_SpawnTypes_SPECIFIED_USER = 5
|
||||
}ILibProcessPipe_SpawnTypes;
|
||||
|
||||
|
||||
@@ -66,12 +68,12 @@ void ILibProcessPipe_Pipe_AddPipeReadHandler(ILibProcessPipe_Pipe targetPipe, in
|
||||
void ILibProcessPipe_Pipe_SetBrokenPipeHandler(ILibProcessPipe_Pipe targetPipe, ILibProcessPipe_Pipe_BrokenPipeHandler handler);
|
||||
|
||||
ILibProcessPipe_Manager ILibProcessPipe_Manager_Create(void *chain);
|
||||
ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx2(ILibProcessPipe_Manager pipeManager, char* target, char* const * parameters, ILibProcessPipe_SpawnTypes spawnType, int extraMemorySize);
|
||||
int ILibProcessPipe_Process_IsDetached(ILibProcessPipe_Process p);
|
||||
ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx3(ILibProcessPipe_Manager pipeManager, char* target, char* const* parameters, ILibProcessPipe_SpawnTypes spawnType, void *sessionId, int extraMemorySize);
|
||||
#define ILibProcessPipe_Manager_SpawnProcess(pipeManager, target, parameters) ILibProcessPipe_Manager_SpawnProcessEx2(pipeManager, target, parameters, ILibProcessPipe_SpawnTypes_DEFAULT, 0)
|
||||
#define ILibProcessPipe_Manager_SpawnProcessEx(pipeManager, target, parameters, spawnType) ILibProcessPipe_Manager_SpawnProcessEx2(pipeManager, target, parameters, spawnType, 0)
|
||||
#define ILibProcessPipe_Manager_SpawnProcessEx2(pipeManager, target, parameters, spawnType, extraMemorySize) ILibProcessPipe_Manager_SpawnProcessEx3(pipeManager, target, parameters, spawnType, NULL, extraMemorySize)
|
||||
#define ILibProcessPipe_Manager_SpawnProcessWithExtraPipeMemory(pipeManager, target, parameters, memorySize) ILibProcessPipe_Manager_SpawnProcessEx2(pipeManager, target, parameters, ILibProcessPipe_SpawnTypes_DEFAULT, memorySize)
|
||||
void* ILibProcessPipe_Process_Kill(ILibProcessPipe_Process p);
|
||||
void* ILibProcessPipe_Process_KillEx(ILibProcessPipe_Process p);
|
||||
void ILibProcessPipe_Process_SoftKill(ILibProcessPipe_Process p);
|
||||
void ILibProcessPipe_Process_AddHandlers(ILibProcessPipe_Process module, int bufferSize, ILibProcessPipe_Process_ExitHandler exitHandler, ILibProcessPipe_Process_OutputHandler stdOut, ILibProcessPipe_Process_OutputHandler stdErr, ILibProcessPipe_Process_SendOKHandler sendOk, void *user);
|
||||
void ILibProcessPipe_Process_UpdateUserObject(ILibProcessPipe_Process module, void *userObj);
|
||||
@@ -94,4 +96,4 @@ void ILibProcessPipe_WaitHandle_Add(ILibProcessPipe_Manager mgr, HANDLE event, v
|
||||
void ILibProcessPipe_WaitHandle_Remove(ILibProcessPipe_Manager mgr, HANDLE event);
|
||||
#endif
|
||||
#define ILibTransports_ProcessPipe 0x60
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -27,7 +27,7 @@ limitations under the License.
|
||||
#include "ILibCrypto.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
char ILibScratchPad_RemoteLogging[255];
|
||||
char ILibScratchPad_RemoteLogging[520];
|
||||
//! Converts a sockaddr to a friendly string, using static memory, for logging purposes
|
||||
/*!
|
||||
\param addr sockaddr to convert
|
||||
@@ -157,6 +157,8 @@ ILibRemoteLogging ILibRemoteLogging_Create(ILibRemoteLogging_OnWrite onOutput)
|
||||
void ILibRemoteLogging_RegisterCommandSink(ILibRemoteLogging logger, ILibRemoteLogging_Modules module, ILibRemoteLogging_OnCommand sink)
|
||||
{
|
||||
ILibRemoteLogging_Module *obj = (ILibRemoteLogging_Module*)logger;
|
||||
if (module < 0) { return; }
|
||||
|
||||
sem_wait(&(obj->LogSyncLock));
|
||||
obj->CommandSink[ILibWhichPowerOfTwo((int)module)] = sink;
|
||||
sem_post(&(obj->LogSyncLock));
|
||||
@@ -669,7 +671,7 @@ ILibTransport* ILibRemoteLogging_CreateFileTransport(ILibRemoteLogging loggingMo
|
||||
retVal->localFilePath = ILibString_Copy(path, pathLen);
|
||||
|
||||
retVal->logFile = ILibLinkedList_FileBacked_Create(retVal->localFilePath, 2048000, 4096);
|
||||
if (retVal->logFile->flags != 0)
|
||||
if (retVal->logFile != NULL && retVal->logFile->flags != 0)
|
||||
{
|
||||
modules = (unsigned short)(retVal->logFile->flags & 0xFFFF);
|
||||
flags = (unsigned short)(retVal->logFile->flags >> 16);
|
||||
|
||||
@@ -225,6 +225,7 @@ FILE* ILibSimpleDataStore_OpenFileEx(char* filePath, int forceTruncateIfNonZero)
|
||||
{
|
||||
f = fopen(filePath, "wb+");
|
||||
}
|
||||
if (f == NULL) { return NULL; } // If we failed to open the file, stop now.
|
||||
if (flock(fileno(f), LOCK_EX | LOCK_NB) != 0) { fclose(f); return NULL; } // Request exclusive lock on this file, no blocking.
|
||||
#endif
|
||||
|
||||
|
||||
@@ -2217,20 +2217,7 @@ void ILibWebClient_OnDisconnectSink(ILibAsyncSocket_SocketModule socketModule, v
|
||||
{
|
||||
// Still Requests to be made
|
||||
if (wcdo->InitialRequestAnswered == 0 && wcdo->CancelRequest == 0)
|
||||
{
|
||||
// Error
|
||||
wr->OnResponse(
|
||||
wcdo,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
ILibWebClient_ReceiveStatus_Complete,
|
||||
wr->user1,
|
||||
wr->user2,
|
||||
&(wcdo->PAUSE));
|
||||
|
||||
{
|
||||
if (wcdo->IsOrphan != 0 || wcdo->IsWebSocket != 0)
|
||||
{
|
||||
ILibWebClient_FinishedResponse(socketModule, wcdo);
|
||||
@@ -2238,6 +2225,18 @@ void ILibWebClient_OnDisconnectSink(ILibAsyncSocket_SocketModule socketModule, v
|
||||
}
|
||||
else
|
||||
{
|
||||
// Error
|
||||
wr->OnResponse(
|
||||
wcdo,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
ILibWebClient_ReceiveStatus_Complete,
|
||||
wr->user1,
|
||||
wr->user2,
|
||||
&(wcdo->PAUSE));
|
||||
ILibWebClient_FinishedResponse(socketModule, wcdo);
|
||||
}
|
||||
}
|
||||
@@ -2315,6 +2314,7 @@ void ILibWebClient_PreProcess(void* WebClientModule, fd_set *readset, fd_set *wr
|
||||
else
|
||||
{
|
||||
// Don't use proxy
|
||||
ILibAsyncSocket_ClearProxySettings(wcm->socks[i]);
|
||||
ILibAsyncSocket_ConnectTo(
|
||||
wcm->socks[i],
|
||||
NULL,
|
||||
@@ -3670,11 +3670,11 @@ void* ILibWebClient_Digest_GenerateTableEx(ILibWebClient_StateObject state, void
|
||||
ILibWebClientDataObject *wcdo = (ILibWebClientDataObject*)state;
|
||||
char* authenticate = ILibGetHeaderLineSP(((ILibWebClientDataObject*)state)->header, "WWW-Authenticate", 16);
|
||||
|
||||
if (wcdo->DigestData == NULL) { wcdo->DigestData = ILibMemory_AllocateA_InitMem(ILibMemory_Allocate(1024, 0, NULL, NULL), 1024); }
|
||||
if (wcdo->DigestData == NULL) { wcdo->DigestData = ILibMemory_SmartAllocate(1024); }
|
||||
if (authenticate != NULL)
|
||||
{
|
||||
ILibMemory_AllocateA_InitMem(ILibMemory_AllocateA_Raw(wcdo->DigestData), ILibMemory_AllocateA_RawSize(wcdo->DigestData));
|
||||
strncpy_s(wcdo->DigestData, ILibMemory_AllocateA_Size(wcdo->DigestData), authenticate, strnlen_s(authenticate, sizeof(ILibScratchPad)));
|
||||
ILibMemory_Init(ILibMemory_RawPtr(wcdo->DigestData), ILibMemory_RawSize(wcdo->DigestData), 0, ILibMemory_Types_HEAP);
|
||||
strncpy_s(wcdo->DigestData, ILibMemory_Size(wcdo->DigestData), authenticate, strnlen_s(authenticate, sizeof(ILibScratchPad)));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -57,6 +57,7 @@ extern "C" {
|
||||
#define WEBSOCKET_RSV1 0x04000
|
||||
#define WEBSOCKET_RSV2 0x02000
|
||||
#define WEBSOCKET_RSV3 0x01000
|
||||
#define WEBSOCKET_RSV 0x07000
|
||||
#define WEBSOCKET_OPCODE 0x00F00
|
||||
#define WEBSOCKET_MASK 0x00080
|
||||
#define WEBSOCKET_PLEN 0x0007F
|
||||
|
||||
@@ -866,7 +866,7 @@ void ILibStun_SctpDisconnect(struct ILibStun_Module *obj, int session);
|
||||
void ILibStun_SendIceRequest(struct ILibStun_IceState *IceState, int SlotNumber, int useCandidate, struct sockaddr_in6* remoteInterface);
|
||||
void ILibStun_SendIceRequestEx(struct ILibStun_IceState *IceState, char* TransactionID, int useCandidate, struct sockaddr_in6* remoteInterface);
|
||||
void ILibStun_ICE_Start(struct ILibStun_IceState *state, int SelectedSlot);
|
||||
unsigned int crc32c(unsigned int crci, const void *buf, unsigned int len);
|
||||
uint32_t crc32c(uint32_t crci, const unsigned char *buf, uint32_t len);
|
||||
int ILibStun_GetDtlsSessionSlotForIceState(struct ILibStun_Module *obj, struct ILibStun_IceState* ice);
|
||||
void ILibStun_InitiateDTLS(struct ILibStun_IceState *IceState, int IceSlot, struct sockaddr_in6* remoteInterface);
|
||||
void ILibStun_PeriodicStunCheck(struct ILibStun_Module* obj);
|
||||
@@ -1628,7 +1628,7 @@ void ILibWebRTC_CloseDataChannel_Timeout(void *obj)
|
||||
o->reconfigFailures = 0;
|
||||
sem_post(&(o->Lock));
|
||||
|
||||
ILibWebRTC_PropagateChannelCloseEx(dup, o);
|
||||
if (dup != NULL) { ILibWebRTC_PropagateChannelCloseEx(dup, o); }
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -2643,24 +2643,23 @@ void ILibStun_ICE_FinalizeConnectivityChecks(void *object)
|
||||
if (obj->IceStates[i]->peerHasActiveOffer == 0)
|
||||
{
|
||||
// Since this list is in priority order, we'll nominate the highest priority candidate that received a response
|
||||
struct sockaddr_in dest;
|
||||
struct sockaddr_in *dest = (struct sockaddr_in *)ILibMemory_AllocateA(8 + sizeof(struct sockaddr_in6));
|
||||
|
||||
memset(&dest, 0, sizeof(struct sockaddr_in));
|
||||
dest.sin_family = AF_INET;
|
||||
dest.sin_port = obj->IceStates[i]->hostcandidates[x].port;
|
||||
dest.sin_addr.s_addr = obj->IceStates[i]->hostcandidates[x].addr;
|
||||
ILibStun_SendIceRequest(obj->IceStates[i], i, 1, (struct sockaddr_in6*)&dest);
|
||||
dest->sin_family = AF_INET;
|
||||
dest->sin_port = obj->IceStates[i]->hostcandidates[x].port;
|
||||
dest->sin_addr.s_addr = obj->IceStates[i]->hostcandidates[x].addr;
|
||||
ILibStun_SendIceRequest(obj->IceStates[i], i, 1, (struct sockaddr_in6*)dest);
|
||||
|
||||
if (obj->IceStates[i]->dtlsInitiator != 0)
|
||||
{
|
||||
// Simultaneously initiate DTLS
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_DTLS, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Initiating DTLS to: %s:%u", ILibRemoteLogging_ConvertAddress((struct sockaddr*)&dest), ntohs(dest.sin_port));
|
||||
ILibStun_InitiateDTLS(obj->IceStates[i], i, (struct sockaddr_in6*)&dest);
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_DTLS, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Initiating DTLS to: %s:%u", ILibRemoteLogging_ConvertAddress((struct sockaddr*)&dest), ntohs(dest->sin_port));
|
||||
ILibStun_InitiateDTLS(obj->IceStates[i], i, (struct sockaddr_in6*)dest);
|
||||
}
|
||||
else
|
||||
{
|
||||
// We are DTLS Server, not Client
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_DTLS, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Waiting for DTLS from: %s:%u", ILibRemoteLogging_ConvertAddress((struct sockaddr*)&dest), ntohs(dest.sin_port));
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(obj->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_DTLS, ILibRemoteLogging_Flags_VerbosityLevel_1, "...Waiting for DTLS from: %s:%u", ILibRemoteLogging_ConvertAddress((struct sockaddr*)&dest), ntohs(dest->sin_port));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -3113,7 +3112,7 @@ ILibTransport_DoneState ILibStun_SendSctpPacket(struct ILibStun_Module *obj, int
|
||||
|
||||
// Compute and put the CRC at the right place
|
||||
((unsigned int*)buffer)[2] = 0;
|
||||
((unsigned int*)buffer)[2] = crc32c(0, buffer, (unsigned int)bufferLength);
|
||||
((unsigned int*)buffer)[2] = crc32c(0, (unsigned char*)buffer, (uint32_t)bufferLength);
|
||||
|
||||
return(ILibStun_SendDtls(obj, session, buffer, bufferLength));
|
||||
// ILibStun_ProcessSctpPacket(obj, -1, buffer, bufferLength); // DEBUG
|
||||
@@ -4249,7 +4248,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
|
||||
// Check size and the RCTP (RFC4960) checksum using CRC32c
|
||||
crc = ((unsigned int*)buffer)[2];
|
||||
((unsigned int*)buffer)[2] = 0;
|
||||
if (crc != crc32c(0, buffer, (unsigned int)bufferLength)) return;
|
||||
if (crc != crc32c(0, (unsigned char*)buffer, (uint32_t)bufferLength)) return;
|
||||
sem_wait(&(o->Lock));
|
||||
|
||||
// Decode the rest of the header
|
||||
@@ -4361,6 +4360,7 @@ void ILibStun_ProcessSctpPacket(struct ILibStun_Module *obj, int session, char*
|
||||
{
|
||||
sem_post(&(o->Lock));
|
||||
obj->OnWebRTCDataChannelClosed(obj, o, streamId);
|
||||
if (obj->dTlsSessions[session] == NULL) { return; }
|
||||
sem_wait(&(o->Lock));
|
||||
}
|
||||
}
|
||||
@@ -6781,8 +6781,11 @@ void ILibTURN_ProcessStunFormattedPacket(struct ILibTURN_TurnClientObject *turn,
|
||||
int val;
|
||||
|
||||
ILibGetEntryEx(turn->transactionData, TransactionID, 12, (void**)&ptr, &val);
|
||||
if (ptr->Handler != NULL) { ((ILibTURN_OnCreateChannelBindingHandler)ptr->Handler)(turn, (unsigned short)val, 0, ptr->user); }
|
||||
free(ptr);
|
||||
if (ptr != NULL)
|
||||
{
|
||||
if (ptr->Handler != NULL) { ((ILibTURN_OnCreateChannelBindingHandler)ptr->Handler)(turn, (unsigned short)val, 0, ptr->user); }
|
||||
free(ptr);
|
||||
}
|
||||
ILibDeleteEntry(turn->transactionData, TransactionID, 12);
|
||||
break;
|
||||
}
|
||||
@@ -6792,9 +6795,12 @@ void ILibTURN_ProcessStunFormattedPacket(struct ILibTURN_TurnClientObject *turn,
|
||||
int val;
|
||||
|
||||
ILibGetEntryEx(turn->transactionData, TransactionID, 12, (void**)&ptr, &val);
|
||||
if (ptr->Handler != NULL) { ((ILibTURN_OnCreateChannelBindingHandler)ptr->Handler)(turn, (unsigned short)val, 1, ptr->user); }
|
||||
free(ptr);
|
||||
ILibDeleteEntry(turn->transactionData, TransactionID, 12);
|
||||
if (ptr != NULL)
|
||||
{
|
||||
if (ptr->Handler != NULL) { ((ILibTURN_OnCreateChannelBindingHandler)ptr->Handler)(turn, (unsigned short)val, 1, ptr->user); }
|
||||
free(ptr);
|
||||
ILibDeleteEntry(turn->transactionData, TransactionID, 12);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TURN_DATA: // Data-Indication
|
||||
@@ -7468,14 +7474,13 @@ int ILibSCTP_Debug_SetDebugCallback(void* dtlsSession, char* debugFieldName, ILi
|
||||
}
|
||||
#endif
|
||||
|
||||
/* crc32c.c -- compute CRC-32C using the Intel crc32 instruction
|
||||
* Copyright (C) 2013 Mark Adler
|
||||
* Version 1.1 1 Aug 2013 Mark Adler
|
||||
*/
|
||||
/* zlib.h -- interface of the 'zlib' general purpose compression library
|
||||
version 1.2.11, January 15th, 2017
|
||||
|
||||
Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
|
||||
|
||||
/*
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the author be held liable for any damages
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
@@ -7490,35 +7495,117 @@ appreciated but is not required.
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
|
||||
Mark Adler
|
||||
madler@alumni.caltech.edu
|
||||
Jean-loup Gailly Mark Adler
|
||||
jloup@gzip.org madler@alumni.caltech.edu
|
||||
|
||||
|
||||
The data format used by the zlib library is described by RFCs (Request for
|
||||
Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950
|
||||
(zlib format), rfc1951 (deflate format) and rfc1952 (gzip format).
|
||||
|
||||
***************************************************************************
|
||||
***************************************************************************
|
||||
*
|
||||
* The CRC32/zlib implementation below was modified March 2018 by Intel Corp, to implement CRC32C (Castagnoli CRC32)
|
||||
* Original source obtained from: https://zlib.net/zlib-1.2.11.tar.gz
|
||||
* More information about zlib can be found at: https://zlib.net/
|
||||
*
|
||||
*/
|
||||
|
||||
// Software only version of CRC32C
|
||||
|
||||
// Table for a quadword-at-a-time software crc.
|
||||
int crc32c_once_sw = 0;
|
||||
unsigned int crc32c_table[8][256];
|
||||
|
||||
// Construct table for software CRC-32C calculation.
|
||||
void crc32c_init_sw(void)
|
||||
uint32_t crc_table[1][256] =
|
||||
{
|
||||
unsigned int n, crc, k;
|
||||
for (n = 0; n < 256; n++) { crc = n; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0x82f63b78 : crc >> 1; crc32c_table[0][n] = crc; }
|
||||
for (n = 0; n < 256; n++) { crc = crc32c_table[0][n]; for (k = 1; k < 8; k++) { crc = crc32c_table[0][crc & 0xff] ^ (crc >> 8); crc32c_table[k][n] = crc; } }
|
||||
{
|
||||
0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L,
|
||||
0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL,
|
||||
0x8AD958CFL, 0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL,
|
||||
0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, 0x5E133C24L,
|
||||
0x105EC76FL, 0xE235446CL, 0xF165B798L, 0x030E349BL,
|
||||
0xD7C45070L, 0x25AFD373L, 0x36FF2087L, 0xC494A384L,
|
||||
0x9A879FA0L, 0x68EC1CA3L, 0x7BBCEF57L, 0x89D76C54L,
|
||||
0x5D1D08BFL, 0xAF768BBCL, 0xBC267848L, 0x4E4DFB4BL,
|
||||
0x20BD8EDEL, 0xD2D60DDDL, 0xC186FE29L, 0x33ED7D2AL,
|
||||
0xE72719C1L, 0x154C9AC2L, 0x061C6936L, 0xF477EA35L,
|
||||
0xAA64D611L, 0x580F5512L, 0x4B5FA6E6L, 0xB93425E5L,
|
||||
0x6DFE410EL, 0x9F95C20DL, 0x8CC531F9L, 0x7EAEB2FAL,
|
||||
0x30E349B1L, 0xC288CAB2L, 0xD1D83946L, 0x23B3BA45L,
|
||||
0xF779DEAEL, 0x05125DADL, 0x1642AE59L, 0xE4292D5AL,
|
||||
0xBA3A117EL, 0x4851927DL, 0x5B016189L, 0xA96AE28AL,
|
||||
0x7DA08661L, 0x8FCB0562L, 0x9C9BF696L, 0x6EF07595L,
|
||||
0x417B1DBCL, 0xB3109EBFL, 0xA0406D4BL, 0x522BEE48L,
|
||||
0x86E18AA3L, 0x748A09A0L, 0x67DAFA54L, 0x95B17957L,
|
||||
0xCBA24573L, 0x39C9C670L, 0x2A993584L, 0xD8F2B687L,
|
||||
0x0C38D26CL, 0xFE53516FL, 0xED03A29BL, 0x1F682198L,
|
||||
0x5125DAD3L, 0xA34E59D0L, 0xB01EAA24L, 0x42752927L,
|
||||
0x96BF4DCCL, 0x64D4CECFL, 0x77843D3BL, 0x85EFBE38L,
|
||||
0xDBFC821CL, 0x2997011FL, 0x3AC7F2EBL, 0xC8AC71E8L,
|
||||
0x1C661503L, 0xEE0D9600L, 0xFD5D65F4L, 0x0F36E6F7L,
|
||||
0x61C69362L, 0x93AD1061L, 0x80FDE395L, 0x72966096L,
|
||||
0xA65C047DL, 0x5437877EL, 0x4767748AL, 0xB50CF789L,
|
||||
0xEB1FCBADL, 0x197448AEL, 0x0A24BB5AL, 0xF84F3859L,
|
||||
0x2C855CB2L, 0xDEEEDFB1L, 0xCDBE2C45L, 0x3FD5AF46L,
|
||||
0x7198540DL, 0x83F3D70EL, 0x90A324FAL, 0x62C8A7F9L,
|
||||
0xB602C312L, 0x44694011L, 0x5739B3E5L, 0xA55230E6L,
|
||||
0xFB410CC2L, 0x092A8FC1L, 0x1A7A7C35L, 0xE811FF36L,
|
||||
0x3CDB9BDDL, 0xCEB018DEL, 0xDDE0EB2AL, 0x2F8B6829L,
|
||||
0x82F63B78L, 0x709DB87BL, 0x63CD4B8FL, 0x91A6C88CL,
|
||||
0x456CAC67L, 0xB7072F64L, 0xA457DC90L, 0x563C5F93L,
|
||||
0x082F63B7L, 0xFA44E0B4L, 0xE9141340L, 0x1B7F9043L,
|
||||
0xCFB5F4A8L, 0x3DDE77ABL, 0x2E8E845FL, 0xDCE5075CL,
|
||||
0x92A8FC17L, 0x60C37F14L, 0x73938CE0L, 0x81F80FE3L,
|
||||
0x55326B08L, 0xA759E80BL, 0xB4091BFFL, 0x466298FCL,
|
||||
0x1871A4D8L, 0xEA1A27DBL, 0xF94AD42FL, 0x0B21572CL,
|
||||
0xDFEB33C7L, 0x2D80B0C4L, 0x3ED04330L, 0xCCBBC033L,
|
||||
0xA24BB5A6L, 0x502036A5L, 0x4370C551L, 0xB11B4652L,
|
||||
0x65D122B9L, 0x97BAA1BAL, 0x84EA524EL, 0x7681D14DL,
|
||||
0x2892ED69L, 0xDAF96E6AL, 0xC9A99D9EL, 0x3BC21E9DL,
|
||||
0xEF087A76L, 0x1D63F975L, 0x0E330A81L, 0xFC588982L,
|
||||
0xB21572C9L, 0x407EF1CAL, 0x532E023EL, 0xA145813DL,
|
||||
0x758FE5D6L, 0x87E466D5L, 0x94B49521L, 0x66DF1622L,
|
||||
0x38CC2A06L, 0xCAA7A905L, 0xD9F75AF1L, 0x2B9CD9F2L,
|
||||
0xFF56BD19L, 0x0D3D3E1AL, 0x1E6DCDEEL, 0xEC064EEDL,
|
||||
0xC38D26C4L, 0x31E6A5C7L, 0x22B65633L, 0xD0DDD530L,
|
||||
0x0417B1DBL, 0xF67C32D8L, 0xE52CC12CL, 0x1747422FL,
|
||||
0x49547E0BL, 0xBB3FFD08L, 0xA86F0EFCL, 0x5A048DFFL,
|
||||
0x8ECEE914L, 0x7CA56A17L, 0x6FF599E3L, 0x9D9E1AE0L,
|
||||
0xD3D3E1ABL, 0x21B862A8L, 0x32E8915CL, 0xC083125FL,
|
||||
0x144976B4L, 0xE622F5B7L, 0xF5720643L, 0x07198540L,
|
||||
0x590AB964L, 0xAB613A67L, 0xB831C993L, 0x4A5A4A90L,
|
||||
0x9E902E7BL, 0x6CFBAD78L, 0x7FAB5E8CL, 0x8DC0DD8FL,
|
||||
0xE330A81AL, 0x115B2B19L, 0x020BD8EDL, 0xF0605BEEL,
|
||||
0x24AA3F05L, 0xD6C1BC06L, 0xC5914FF2L, 0x37FACCF1L,
|
||||
0x69E9F0D5L, 0x9B8273D6L, 0x88D28022L, 0x7AB90321L,
|
||||
0xAE7367CAL, 0x5C18E4C9L, 0x4F48173DL, 0xBD23943EL,
|
||||
0xF36E6F75L, 0x0105EC76L, 0x12551F82L, 0xE03E9C81L,
|
||||
0x34F4F86AL, 0xC69F7B69L, 0xD5CF889DL, 0x27A40B9EL,
|
||||
0x79B737BAL, 0x8BDCB4B9L, 0x988C474DL, 0x6AE7C44EL,
|
||||
0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, 0xAD7D5351L
|
||||
}
|
||||
};
|
||||
|
||||
/* ========================================================================= */
|
||||
#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
|
||||
#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
|
||||
|
||||
/* ========================================================================= */
|
||||
uint32_t crc32c_z(uint32_t crc, const unsigned char* buf, uint32_t len)
|
||||
{
|
||||
if (buf == NULL) return 0UL;
|
||||
|
||||
crc = crc ^ 0xffffffffUL;
|
||||
while (len >= 8) {
|
||||
DO8;
|
||||
len -= 8;
|
||||
}
|
||||
if (len) do {
|
||||
DO1;
|
||||
} while (--len);
|
||||
return crc ^ 0xffffffffUL;
|
||||
}
|
||||
|
||||
// Table-driven software version as a fall-back. This is about 15 times slower than using the hardware instructions. This assumes little-endian integers, as is the case on Intel processors that the assembler code here is for.
|
||||
unsigned int crc32c(unsigned int crci, const void *buf, unsigned int len)
|
||||
/* ========================================================================= */
|
||||
uint32_t crc32c(uint32_t crc, const unsigned char* buf, uint32_t len)
|
||||
{
|
||||
unsigned long long crc;
|
||||
const unsigned char *next = (const unsigned char*)buf;
|
||||
if (crc32c_once_sw == 0) { crc32c_init_sw(); crc32c_once_sw = 1; }
|
||||
crc = crci ^ 0xffffffff;
|
||||
while (len && ((uintptr_t)next & 7) != 0) { crc = (unsigned long long)crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); len--; }
|
||||
while (len >= 8) { crc ^= *(unsigned long long *)next; crc = crc32c_table[7][crc & 0xff] ^ crc32c_table[6][(crc >> 8) & 0xff] ^ crc32c_table[5][(crc >> 16) & 0xff] ^ crc32c_table[4][(crc >> 24) & 0xff] ^ crc32c_table[3][(crc >> 32) & 0xff] ^ crc32c_table[2][(crc >> 40) & 0xff] ^ crc32c_table[1][(crc >> 48) & 0xff] ^ crc32c_table[0][crc >> 56]; next += 8; len -= 8; }
|
||||
while (len) { crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); len--; }
|
||||
return (unsigned int)crc ^ 0xffffffff;
|
||||
return crc32c_z(crc, buf, len);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -267,4 +267,4 @@ unsigned int ILibTURN_GetPendingBytesToSend(ILibTURN_ClientModule turnModule);
|
||||
/*! @} */
|
||||
/*! @} */
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1073,7 +1073,7 @@ ILibWebServer_ServerToken ILibWebServer_CreateEx(void *Chain, int MaxConnections
|
||||
unsigned short ILibWebServer_GetPortNumber(ILibWebServer_ServerToken WebServerToken)
|
||||
{
|
||||
struct ILibWebServer_StateModule *WSM = (struct ILibWebServer_StateModule*) WebServerToken;
|
||||
return ILibAsyncServerSocket_GetPortNumber(WSM->ServerSocket);
|
||||
return (WSM != NULL ? ILibAsyncServerSocket_GetPortNumber(WSM->ServerSocket) : 0);
|
||||
}
|
||||
|
||||
void ILibWebServer_Digest_CalculateNonce(struct ILibWebServer_Session *session, long long expiration, char* buffer)
|
||||
@@ -1447,7 +1447,7 @@ int ILibWebServer_WebSocket_CreateHeader(char* header, unsigned short FLAGS, uns
|
||||
retVal = 10;
|
||||
header1 |= 127;
|
||||
((unsigned short*)header)[0] = htons(header1);
|
||||
((unsigned long long*)(header + 2))[0] = htonl((int)payloadLength);
|
||||
((unsigned long long*)(header + 2))[0] = ILibHTONLL((uint64_t)payloadLength);
|
||||
}
|
||||
|
||||
return retVal;
|
||||
|
||||
@@ -76,7 +76,6 @@ typedef struct ILibWrapper_WebRTC_ConnectionStruct
|
||||
int stunServerListLength;
|
||||
int stunIndex;
|
||||
int isConnected;
|
||||
void *extraMemory;
|
||||
|
||||
int id;
|
||||
char localUsername[9];
|
||||
@@ -85,7 +84,6 @@ typedef struct ILibWrapper_WebRTC_ConnectionStruct
|
||||
char *remoteUsername;
|
||||
char* remotePassword;
|
||||
char *remoteOfferBlock;
|
||||
int remoteOfferBlockLen;
|
||||
|
||||
char* offerBlock;
|
||||
int offerBlockLen;
|
||||
@@ -103,7 +101,7 @@ typedef struct ILibWrapper_WebRTC_ConnectionStruct
|
||||
}ILibWrapper_WebRTC_ConnectionStruct;
|
||||
|
||||
// Prototypes:
|
||||
const int ILibMemory_WebRTC_Connection_CONTAINERSIZE = sizeof(ILibWrapper_WebRTC_ConnectionStruct);
|
||||
|
||||
int ILibWrapper_WebRTC_PerformStun(ILibWrapper_WebRTC_ConnectionStruct *connection);
|
||||
char* ILibWrapper_WebRTC_Connection_GetLocalUsername(ILibWrapper_WebRTC_Connection connection)
|
||||
{
|
||||
@@ -171,9 +169,9 @@ unsigned short ILibWrapper_GetNextStreamId(ILibWrapper_WebRTC_ConnectionStruct *
|
||||
return(id);
|
||||
}
|
||||
|
||||
int ILibWrapper_SdpToBlock(char* sdp, int sdpLen, int *isActive, char **username, char **password, char **block)
|
||||
char* ILibWrapper_SdpToBlock(char* sdp, int sdpLen, int *isActive, char **username, char **password)
|
||||
{
|
||||
struct parser_result *pr;
|
||||
struct parser_result *pr = NULL;
|
||||
struct parser_result_field *f;
|
||||
|
||||
int ptr;
|
||||
@@ -185,10 +183,11 @@ int ILibWrapper_SdpToBlock(char* sdp, int sdpLen, int *isActive, char **username
|
||||
int candidatecount = 0;
|
||||
int BlockFlags = 0;
|
||||
int passwordLen = 0, usernameLen = 0;
|
||||
char *retVal = NULL;
|
||||
*isActive = 0;
|
||||
*username = NULL;
|
||||
*password = NULL;
|
||||
|
||||
|
||||
|
||||
ILibCreateStack(&candidates);
|
||||
|
||||
@@ -263,48 +262,54 @@ int ILibWrapper_SdpToBlock(char* sdp, int sdpLen, int *isActive, char **username
|
||||
|
||||
if (*username == NULL || *password == NULL || dtlshash == NULL)
|
||||
{
|
||||
*block = NULL;
|
||||
return 0;
|
||||
if (pr != NULL) { ILibDestructParserResults(pr); }
|
||||
if (dtlshash != NULL) { free(dtlshash); }
|
||||
while (ILibPeekStack(&candidates) != NULL)
|
||||
{
|
||||
ILibPopStack(&candidates);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
blockLen = 6 + usernameLen+1 + passwordLen+1 + dtlsHashLen + 1 + (candidatecount*6)+1;
|
||||
if((*block = (char*)malloc(blockLen))==NULL){ILIBCRITICALEXIT(254);}
|
||||
|
||||
retVal = (char*)ILibMemory_SmartAllocateEx(blockLen, sizeof(int));
|
||||
ptr = 0;
|
||||
|
||||
((unsigned short*)*block+ptr)[0] = htons(1);
|
||||
((unsigned short*)(retVal+ptr))[0] = htons(1);
|
||||
ptr += 2;
|
||||
|
||||
((unsigned int*)(*block+ptr))[0] = htonl(BlockFlags);
|
||||
((unsigned int*)(retVal + ptr))[0] = htonl(BlockFlags);
|
||||
ptr += 4;
|
||||
|
||||
(*block)[ptr] = (char)usernameLen;
|
||||
(retVal)[ptr] = (char)usernameLen;
|
||||
ptr += 1;
|
||||
memcpy_s(*block + ptr, blockLen - ptr, *username, usernameLen);
|
||||
memcpy_s((retVal + ptr), blockLen - ptr, *username, usernameLen);
|
||||
ptr += usernameLen;
|
||||
|
||||
(*block)[ptr] = (char)passwordLen;
|
||||
(retVal)[ptr] = (char)passwordLen;
|
||||
ptr += 1;
|
||||
memcpy_s(*block + ptr, blockLen - ptr, *password, passwordLen);
|
||||
memcpy_s((retVal + ptr), blockLen - ptr, *password, passwordLen);
|
||||
ptr += passwordLen;
|
||||
|
||||
(*block)[ptr] = (char)dtlsHashLen;
|
||||
(retVal)[ptr] = (char)dtlsHashLen;
|
||||
ptr += 1;
|
||||
memcpy_s(*block + ptr, blockLen - ptr, dtlshash, dtlsHashLen);
|
||||
memcpy_s((retVal + ptr), blockLen - ptr, dtlshash, dtlsHashLen);
|
||||
ptr += dtlsHashLen;
|
||||
|
||||
(*block)[ptr] = (char)candidatecount;
|
||||
(retVal)[ptr] = (char)candidatecount;
|
||||
ptr += 1;
|
||||
while(ILibPeekStack(&candidates)!=NULL)
|
||||
{
|
||||
memcpy_s(*block + ptr, blockLen - ptr, ILibPopStack(&candidates), 6);
|
||||
memcpy_s((retVal + ptr), blockLen - ptr, ILibPopStack(&candidates), 6);
|
||||
ptr += 6;
|
||||
}
|
||||
|
||||
ILibDestructParserResults(pr);
|
||||
free(lines);
|
||||
if(dtlshash!=NULL) {free(dtlshash);}
|
||||
return(ptr);
|
||||
return(retVal);
|
||||
}
|
||||
|
||||
int ILibWrapper_BlockToSDPEx(char* block, int blockLen, char** username, char** password, char **sdp, char* serverReflexiveCandidateAddress, unsigned short serverReflexiveCandidatePort)
|
||||
@@ -527,7 +532,7 @@ void ILibWrapper_WebRTC_Connection_DestroyConnection(ILibWrapper_WebRTC_Connecti
|
||||
{
|
||||
ILibWrapper_WebRTC_ConnectionStruct *obj = (ILibWrapper_WebRTC_ConnectionStruct*)connection;
|
||||
|
||||
if(obj==NULL) {return;}
|
||||
if(obj==NULL || !ILibMemory_CanaryOK(obj)) {return;}
|
||||
|
||||
|
||||
if(ILibIsChainBeingDestroyed(obj->mFactory->ChainLink.ParentChain)==0)
|
||||
@@ -546,7 +551,7 @@ void ILibWrapper_WebRTC_Connection_DestroyConnection(ILibWrapper_WebRTC_Connecti
|
||||
// Clear the ICE State for the local Offer
|
||||
ILibStun_ClearIceState(obj->mFactory->mStunModule, ILibStun_CharToSlot(obj->offerBlock[7]));
|
||||
}
|
||||
if(obj->remoteOfferBlock!=NULL && obj->remoteOfferBlockLen>0)
|
||||
if(obj->remoteOfferBlock!=NULL && ILibMemory_CanaryOK(obj->remoteOfferBlock) && obj->isOfferInitiator)
|
||||
{
|
||||
// Clear the ICE State for the remote Offer
|
||||
ILibStun_ClearIceState(obj->mFactory->mStunModule, ILibStun_CharToSlot(obj->remoteOfferBlock[7]));
|
||||
@@ -575,7 +580,7 @@ void ILibWrapper_WebRTC_Connection_DestroyConnection(ILibWrapper_WebRTC_Connecti
|
||||
|
||||
if(obj->remoteOfferBlock!=NULL)
|
||||
{
|
||||
free(obj->remoteOfferBlock);
|
||||
ILibMemory_Free(obj->remoteOfferBlock);
|
||||
obj->remoteOfferBlock = NULL;
|
||||
}
|
||||
|
||||
@@ -585,7 +590,7 @@ void ILibWrapper_WebRTC_Connection_DestroyConnection(ILibWrapper_WebRTC_Connecti
|
||||
free(obj->stunServerFlags);
|
||||
}
|
||||
|
||||
free(obj);
|
||||
ILibMemory_Free(obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -607,7 +612,7 @@ void ILibWrapper_WebRTC_Connection_Disconnect(ILibWrapper_WebRTC_Connection conn
|
||||
// Clear the ICE State for the local Offer
|
||||
ILibStun_ClearIceState(obj->mFactory->mStunModule, ILibStun_CharToSlot(obj->offerBlock[7]));
|
||||
}
|
||||
if(obj->remoteOfferBlock!=NULL && obj->remoteOfferBlockLen>0)
|
||||
if(obj->remoteOfferBlock!=NULL && ILibMemory_CanaryOK(obj->remoteOfferBlock))
|
||||
{
|
||||
// Clear the ICE State for the remote Offer
|
||||
ILibStun_ClearIceState(obj->mFactory->mStunModule, ILibStun_CharToSlot(obj->remoteOfferBlock[7]));
|
||||
@@ -702,6 +707,7 @@ void ILibWrapper_WebRTC_OnDataSink(void* StunModule, void* module, unsigned shor
|
||||
{
|
||||
ILibWrapper_WebRTC_DataChannel *dc = NULL;
|
||||
ILibWrapper_WebRTC_ConnectionStruct *connection = (ILibWrapper_WebRTC_ConnectionStruct*)ILibWebRTC_GetUserObjectFromDtlsSession(module);
|
||||
if (connection == NULL) { return; }
|
||||
|
||||
UNREFERENCED_PARAMETER(user);
|
||||
UNREFERENCED_PARAMETER(StunModule);
|
||||
@@ -739,7 +745,7 @@ void ILibWrapper_WebRTC_OnDataChannelClosed(void *StunModule, void* WebRTCModule
|
||||
{
|
||||
ILibWrapper_WebRTC_ConnectionStruct *obj = (ILibWrapper_WebRTC_ConnectionStruct*) ILibWebRTC_GetUserObjectFromDtlsSession(WebRTCModule);
|
||||
ILibWrapper_WebRTC_DataChannel* dataChannel = NULL;
|
||||
|
||||
if (obj == NULL) { return; }
|
||||
UNREFERENCED_PARAMETER(StunModule);
|
||||
|
||||
ILibSparseArray_Lock(obj->DataChannels);
|
||||
@@ -753,13 +759,13 @@ int ILibWrapper_WebRTC_OnDataChannel(void *StunModule, void* WebRTCModule, unsig
|
||||
{
|
||||
ILibWrapper_WebRTC_ConnectionStruct *obj = (ILibWrapper_WebRTC_ConnectionStruct*) ILibWebRTC_GetUserObjectFromDtlsSession(WebRTCModule);
|
||||
ILibWrapper_WebRTC_DataChannel* dataChannel = NULL;
|
||||
|
||||
if (obj == NULL) { return(0); }
|
||||
UNREFERENCED_PARAMETER(StunModule);
|
||||
|
||||
ILibSparseArray_Lock(obj->DataChannels);
|
||||
if((dataChannel = (ILibWrapper_WebRTC_DataChannel*)ILibSparseArray_Get(obj->DataChannels, (int)StreamId))==NULL)
|
||||
{
|
||||
dataChannel = (ILibWrapper_WebRTC_DataChannel*)ILibChain_Link_Allocate(sizeof(ILibWrapper_WebRTC_DataChannel), ILibMemory_GetExtraMemorySize(obj->extraMemory));
|
||||
dataChannel = (ILibWrapper_WebRTC_DataChannel*)ILibChain_Link_Allocate(sizeof(ILibWrapper_WebRTC_DataChannel), (int)ILibMemory_ExtraSize(obj));
|
||||
|
||||
dataChannel->parent = obj;
|
||||
dataChannel->streamId = StreamId;
|
||||
@@ -859,8 +865,8 @@ ILibWrapper_WebRTC_ConnectionFactory ILibWrapper_WebRTC_ConnectionFactory_Create
|
||||
retVal->mStunModule = ILibStunClient_Start(chain, localPort, &ILibWrapper_WebRTC_OnStunResult);
|
||||
if (retVal->mStunModule == NULL)
|
||||
{
|
||||
free(retVal);
|
||||
ILibLinkedList_Remove_ByData(ILibChain_GetLinks(chain), retVal);
|
||||
free(retVal);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
@@ -912,28 +918,33 @@ ILibWrapper_WebRTC_Connection ILibWrapper_WebRTC_ConnectionFactory_CreateConnect
|
||||
{
|
||||
// There is no outstanding offer from the remote peer
|
||||
retVal = ILibWrapper_WebRTC_ConnectionFactory_CreateConnection2(factory, OnConnectHandler, OnDataChannelHandler, OnConnectionSendOK, extraMemorySize);
|
||||
retVal->remoteOfferBlockLen = iceOfferBlockLen;
|
||||
if ((retVal->remoteOfferBlock = (char*)malloc(iceOfferBlockLen)) == NULL) { ILIBCRITICALEXIT(254); }
|
||||
retVal->remoteOfferBlock = (char*)ILibMemory_SmartAllocateEx(iceOfferBlockLen, sizeof(int));
|
||||
((int*)ILibMemory_Extra(retVal->remoteOfferBlock))[0] = 0;
|
||||
|
||||
memcpy_s(retVal->remoteOfferBlock, iceOfferBlockLen, iceOfferBlock, iceOfferBlockLen);
|
||||
retVal->offerBlockLen = ILibStun_SetIceOffer(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, iceOfferBlock, iceOfferBlockLen, &(retVal->offerBlock));
|
||||
ILibWrapper_BlockToSDP(retVal->remoteOfferBlock, iceOfferBlockLen, 0, (char**)&(retVal->remoteUsername), (char**)&(retVal->remotePassword), ILibBlockToSDP_CREDENTIALS_ONLY_WITH_ALLOCATE);
|
||||
ILibWrapper_BlockToSDP(retVal->offerBlock, retVal->offerBlockLen, 0, (char**)&(retVal->localUsername), (char**)&(retVal->localPassword), ILibBlockToSDP_CREDENTIALS_ONLY);
|
||||
|
||||
ILibWebRTC_SetUserObject(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, retVal->localUsername, retVal);
|
||||
if (retVal->offerBlock != NULL)
|
||||
{
|
||||
ILibWrapper_BlockToSDP(retVal->remoteOfferBlock, iceOfferBlockLen, 0, (char**)&(retVal->remoteUsername), (char**)&(retVal->remotePassword), ILibBlockToSDP_CREDENTIALS_ONLY_WITH_ALLOCATE);
|
||||
ILibWrapper_BlockToSDP(retVal->offerBlock, retVal->offerBlockLen, 0, (char**)&(retVal->localUsername), (char**)&(retVal->localPassword), ILibBlockToSDP_CREDENTIALS_ONLY);
|
||||
ILibWebRTC_SetUserObject(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, retVal->localUsername, retVal);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Outstanding offer from remote peer was found
|
||||
tmp[0] = (char)ILibStun_SlotToChar(iceSlot);
|
||||
retVal = ILibWebRTC_GetUserObject(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, tmp);
|
||||
if (retVal != NULL)
|
||||
{
|
||||
if (retVal->remoteOfferBlock != NULL) { ILibMemory_Free(retVal->remoteOfferBlock); }
|
||||
if (retVal->offerBlock != NULL) { free(retVal->offerBlock); }
|
||||
|
||||
if (retVal->remoteOfferBlock != NULL) { free(retVal->remoteOfferBlock); }
|
||||
if (retVal->offerBlock != NULL) { free(retVal->offerBlock); }
|
||||
|
||||
retVal->remoteOfferBlockLen = iceOfferBlockLen;
|
||||
if ((retVal->remoteOfferBlock = (char*)malloc(iceOfferBlockLen)) == NULL) { ILIBCRITICALEXIT(254); }
|
||||
memcpy_s(retVal->remoteOfferBlock, iceOfferBlockLen, iceOfferBlock, iceOfferBlockLen);
|
||||
retVal->offerBlockLen = ILibStun_SetIceOffer(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, iceOfferBlock, iceOfferBlockLen, &(retVal->offerBlock));
|
||||
retVal->remoteOfferBlock = (char*)ILibMemory_SmartAllocateEx(iceOfferBlockLen, sizeof(int));
|
||||
((int*)ILibMemory_Extra(retVal->remoteOfferBlock))[0] = 1;
|
||||
memcpy_s(retVal->remoteOfferBlock, iceOfferBlockLen, iceOfferBlock, iceOfferBlockLen);
|
||||
retVal->offerBlockLen = ILibStun_SetIceOffer(((ILibWrapper_WebRTC_ConnectionFactoryStruct*)factory)->mStunModule, iceOfferBlock, iceOfferBlockLen, &(retVal->offerBlock));
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
@@ -945,9 +956,7 @@ ILibWrapper_WebRTC_Connection ILibWrapper_WebRTC_ConnectionFactory_CreateConnect
|
||||
|
||||
ILibWrapper_WebRTC_Connection ILibWrapper_WebRTC_ConnectionFactory_CreateConnection2(ILibWrapper_WebRTC_ConnectionFactory factory, ILibWrapper_WebRTC_Connection_OnConnect OnConnectHandler, ILibWrapper_WebRTC_Connection_OnDataChannel OnDataChannelHandler, ILibWrapper_WebRTC_Connection_OnSendOK OnConnectionSendOK, int extraMemorySize)
|
||||
{
|
||||
void *extraMemory;
|
||||
ILibWrapper_WebRTC_ConnectionStruct *retVal = (ILibWrapper_WebRTC_ConnectionStruct*)ILibMemory_Allocate(sizeof(ILibWrapper_WebRTC_ConnectionStruct), extraMemorySize, NULL, &extraMemory);
|
||||
retVal->extraMemory = extraMemory;
|
||||
ILibWrapper_WebRTC_ConnectionStruct *retVal = (ILibWrapper_WebRTC_ConnectionStruct*)ILibMemory_SmartAllocateEx(sizeof(ILibWrapper_WebRTC_ConnectionStruct), extraMemorySize);
|
||||
|
||||
retVal->OnConnected = OnConnectHandler;
|
||||
retVal->OnDataChannel = OnDataChannelHandler;
|
||||
@@ -978,7 +987,7 @@ void ILibWrapper_WebRTC_Connection_SetStunServers(ILibWrapper_WebRTC_Connection
|
||||
if((obj->stunServerFlags = (char*)malloc(serverLength))==NULL){ILIBCRITICALEXIT(254);}
|
||||
memset(obj->stunServerFlags, 0, serverLength);
|
||||
|
||||
obj->stunServerList = (char**)malloc(serverLength * sizeof(char*));
|
||||
if ((obj->stunServerList = (char**)malloc(serverLength * sizeof(char*))) == NULL) { ILIBCRITICALEXIT(254); }
|
||||
obj->stunServerListLength = serverLength;
|
||||
for(i=0;i<serverLength;++i)
|
||||
{
|
||||
@@ -1073,15 +1082,19 @@ char* ILibWrapper_WebRTC_Connection_SetOffer(ILibWrapper_WebRTC_Connection conne
|
||||
int isActive;
|
||||
char *un,*up;
|
||||
|
||||
if(obj->remoteOfferBlock!=NULL) {free(obj->remoteOfferBlock);}
|
||||
if(obj->remoteOfferBlock!=NULL) {ILibMemory_Free(obj->remoteOfferBlock);}
|
||||
if(obj->offerBlock!=NULL) {free(obj->offerBlock);}
|
||||
|
||||
obj->remoteOfferBlockLen = ILibWrapper_SdpToBlock(offer, offerLen, &isActive, &(obj->remoteUsername), &(obj->remotePassword), &(obj->remoteOfferBlock));
|
||||
char *remoteOfferBlock = ILibWrapper_SdpToBlock(offer, offerLen, &isActive, &(obj->remoteUsername), &(obj->remotePassword));
|
||||
if (remoteOfferBlock == NULL) { return(NULL); }
|
||||
|
||||
if (obj->remoteOfferBlock != NULL) { ILibMemory_Free(obj->remoteOfferBlock); ((int*)ILibMemory_Extra(remoteOfferBlock))[0] = 1; }
|
||||
obj->remoteOfferBlock = remoteOfferBlock;
|
||||
|
||||
offer[offerLen] = 0;
|
||||
ILibRemoteLogging_printf(ILibChainGetLogger(obj->mFactory->ChainLink.ParentChain), ILibRemoteLogging_Modules_WebRTC_STUN_ICE, ILibRemoteLogging_Flags_VerbosityLevel_1, "[ILibWrapperWebRTC] Set ICE/Offer: <br/>%s", offer);
|
||||
|
||||
obj->offerBlockLen = ILibStun_SetIceOffer2(obj->mFactory->mStunModule, obj->remoteOfferBlock, obj->remoteOfferBlockLen, obj->offerBlock != NULL ? obj->localUsername : NULL, obj->offerBlock != NULL ? 8 : 0, obj->offerBlock != NULL ? obj->localPassword : NULL, obj->offerBlock != NULL ? 32 : 0, &obj->offerBlock);
|
||||
obj->offerBlockLen = ILibStun_SetIceOffer2(obj->mFactory->mStunModule, obj->remoteOfferBlock, (int)ILibMemory_Size(obj->remoteOfferBlock), obj->offerBlock != NULL ? obj->localUsername : NULL, obj->offerBlock != NULL ? 8 : 0, obj->offerBlock != NULL ? obj->localPassword : NULL, obj->offerBlock != NULL ? 32 : 0, &obj->offerBlock);
|
||||
if (obj->offerBlockLen == 0) { return(NULL); }
|
||||
|
||||
ILibWrapper_BlockToSDP(obj->offerBlock, obj->offerBlockLen, obj->isOfferInitiator, &un, &up, &sdp);
|
||||
@@ -1142,13 +1155,12 @@ void ILibWrapper_WebRTC_Connection_GetLocalParameters_DTLS(ILibWrapper_WebRTC_Co
|
||||
*hashLen = *hashLen + (*hashLen / 2) + 1;
|
||||
|
||||
*hash = (char*)malloc(*hashLen);
|
||||
if (*hash == NULL) { ILIBCRITICALEXIT(254); }
|
||||
|
||||
util_tohex2(obj->mFactory->tlsServerCertThumbprint, (int)sizeof(obj->mFactory->tlsServerCertThumbprint), *hash);
|
||||
*hashLen = (int)strnlen_s(*hash, *hashLen);
|
||||
}
|
||||
void* ILibWrapper_WebRTC_Connection_GetExtraMemory(ILibWrapper_WebRTC_Connection connection)
|
||||
{
|
||||
return(((ILibWrapper_WebRTC_ConnectionStruct*)connection)->extraMemory);
|
||||
}
|
||||
|
||||
ILibTransport* ILibWrapper_WebRTC_Connection_GetRawTransport(ILibWrapper_WebRTC_Connection connection)
|
||||
{
|
||||
return(((ILibWrapper_WebRTC_ConnectionStruct*)connection)->dtlsSession);
|
||||
@@ -1251,7 +1263,7 @@ void ILibWrapper_WebRTC_DataChannel_Close(ILibWrapper_WebRTC_DataChannel* dataCh
|
||||
}
|
||||
ILibWrapper_WebRTC_DataChannel* ILibWrapper_WebRTC_DataChannel_CreateEx(ILibWrapper_WebRTC_Connection connection, char* channelName, int channelNameLen, unsigned short streamId, ILibWrapper_WebRTC_DataChannel_OnDataChannelAck OnAckHandler)
|
||||
{
|
||||
ILibWrapper_WebRTC_DataChannel *retVal = (ILibWrapper_WebRTC_DataChannel*)ILibChain_Link_Allocate(sizeof(ILibWrapper_WebRTC_DataChannel), ILibMemory_GetExtraMemorySize(((ILibWrapper_WebRTC_ConnectionStruct*)connection)->extraMemory));
|
||||
ILibWrapper_WebRTC_DataChannel *retVal = (ILibWrapper_WebRTC_DataChannel*)ILibChain_Link_Allocate(sizeof(ILibWrapper_WebRTC_DataChannel), (int)ILibMemory_ExtraSize(connection));
|
||||
|
||||
retVal->parent = connection;
|
||||
if ((retVal->channelName = (char*)malloc(channelNameLen + 1)) == NULL) { ILIBCRITICALEXIT(254); }
|
||||
|
||||
@@ -30,7 +30,6 @@ limitations under the License.
|
||||
/*! @{ */
|
||||
|
||||
#define ILibTransports_WebRTC_DataChannel 0x51
|
||||
extern const int ILibMemory_WebRTC_Connection_CONTAINERSIZE;
|
||||
|
||||
/** Factory object that is used to create WebRTC Connections. */
|
||||
typedef void* ILibWrapper_WebRTC_ConnectionFactory;
|
||||
|
||||
@@ -1,310 +0,0 @@
|
||||
/* Functions to compute SHA256 message digest of files or memory blocks.
|
||||
according to the definition of SHA256 in FIPS 180-2.
|
||||
Copyright (C) 2007, 2011 Free Software Foundation, Inc.
|
||||
This file is part of the GNU C Library.
|
||||
The GNU C Library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
The GNU C Library 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
|
||||
Lesser General Public License for more details.
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with the GNU C Library; if not, see
|
||||
<http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Written by Ulrich Drepper <drepper@redhat.com>, 2007. */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <endian.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "sha256.h"
|
||||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
# ifdef _LIBC
|
||||
# include <byteswap.h>
|
||||
# define SWAP(n) bswap_32 (n)
|
||||
# define SWAP64(n) bswap_64 (n)
|
||||
# else
|
||||
# define SWAP(n) \
|
||||
(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
|
||||
# define SWAP64(n) \
|
||||
(((n) << 56) \
|
||||
| (((n) & 0xff00) << 40) \
|
||||
| (((n) & 0xff0000) << 24) \
|
||||
| (((n) & 0xff000000) << 8) \
|
||||
| (((n) >> 8) & 0xff000000) \
|
||||
| (((n) >> 24) & 0xff0000) \
|
||||
| (((n) >> 40) & 0xff00) \
|
||||
| ((n) >> 56))
|
||||
# endif
|
||||
#else
|
||||
# define SWAP(n) (n)
|
||||
# define SWAP64(n) (n)
|
||||
#endif
|
||||
|
||||
|
||||
/* This array contains the bytes used to pad the buffer to the next
|
||||
64-byte boundary. (FIPS 180-2:5.1.1) */
|
||||
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
|
||||
|
||||
|
||||
/* Constants for SHA256 from FIPS 180-2:4.2.2. */
|
||||
static const uint32_t K[64] =
|
||||
{
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
||||
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
||||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
|
||||
/* Process LEN bytes of BUFFER, accumulating context into CTX.
|
||||
It is assumed that LEN % 64 == 0. */
|
||||
static void
|
||||
sha256_process_block(const void *buffer, size_t len, struct sha256_ctx *ctx)
|
||||
{
|
||||
const uint32_t *words = buffer;
|
||||
size_t nwords = len / sizeof(uint32_t);
|
||||
uint32_t a = ctx->H[0];
|
||||
uint32_t b = ctx->H[1];
|
||||
uint32_t c = ctx->H[2];
|
||||
uint32_t d = ctx->H[3];
|
||||
uint32_t e = ctx->H[4];
|
||||
uint32_t f = ctx->H[5];
|
||||
uint32_t g = ctx->H[6];
|
||||
uint32_t h = ctx->H[7];
|
||||
|
||||
/* First increment the byte count. FIPS 180-2 specifies the possible
|
||||
length of the file up to 2^64 bits. Here we only compute the
|
||||
number of bytes. */
|
||||
ctx->total64 += len;
|
||||
|
||||
/* Process all bytes in the buffer with 64 bytes in each round of
|
||||
the loop. */
|
||||
while (nwords > 0)
|
||||
{
|
||||
uint32_t W[64];
|
||||
uint32_t a_save = a;
|
||||
uint32_t b_save = b;
|
||||
uint32_t c_save = c;
|
||||
uint32_t d_save = d;
|
||||
uint32_t e_save = e;
|
||||
uint32_t f_save = f;
|
||||
uint32_t g_save = g;
|
||||
uint32_t h_save = h;
|
||||
|
||||
/* Operators defined in FIPS 180-2:4.1.2. */
|
||||
#define Ch(x, y, z) ((x & y) ^ (~x & z))
|
||||
#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
|
||||
#define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
|
||||
#define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
|
||||
#define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
|
||||
#define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
|
||||
|
||||
/* It is unfortunate that C does not provide an operator for
|
||||
cyclic rotation. Hope the C compiler is smart enough. */
|
||||
#define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
|
||||
|
||||
/* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
|
||||
for (unsigned int t = 0; t < 16; ++t)
|
||||
{
|
||||
W[t] = SWAP(*words);
|
||||
++words;
|
||||
}
|
||||
for (unsigned int t = 16; t < 64; ++t)
|
||||
W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16];
|
||||
|
||||
/* The actual computation according to FIPS 180-2:6.2.2 step 3. */
|
||||
for (unsigned int t = 0; t < 64; ++t)
|
||||
{
|
||||
uint32_t T1 = h + S1(e) + Ch(e, f, g) + K[t] + W[t];
|
||||
uint32_t T2 = S0(a) + Maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + T1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = T1 + T2;
|
||||
}
|
||||
|
||||
/* Add the starting values of the context according to FIPS 180-2:6.2.2
|
||||
step 4. */
|
||||
a += a_save;
|
||||
b += b_save;
|
||||
c += c_save;
|
||||
d += d_save;
|
||||
e += e_save;
|
||||
f += f_save;
|
||||
g += g_save;
|
||||
h += h_save;
|
||||
|
||||
/* Prepare for the next round. */
|
||||
nwords -= 16;
|
||||
}
|
||||
|
||||
/* Put checksum in context given as argument. */
|
||||
ctx->H[0] = a;
|
||||
ctx->H[1] = b;
|
||||
ctx->H[2] = c;
|
||||
ctx->H[3] = d;
|
||||
ctx->H[4] = e;
|
||||
ctx->H[5] = f;
|
||||
ctx->H[6] = g;
|
||||
ctx->H[7] = h;
|
||||
}
|
||||
|
||||
|
||||
/* Initialize structure containing state of computation.
|
||||
(FIPS 180-2:5.3.2) */
|
||||
void
|
||||
__sha256_init_ctx(ctx)
|
||||
struct sha256_ctx *ctx;
|
||||
{
|
||||
ctx->H[0] = 0x6a09e667;
|
||||
ctx->H[1] = 0xbb67ae85;
|
||||
ctx->H[2] = 0x3c6ef372;
|
||||
ctx->H[3] = 0xa54ff53a;
|
||||
ctx->H[4] = 0x510e527f;
|
||||
ctx->H[5] = 0x9b05688c;
|
||||
ctx->H[6] = 0x1f83d9ab;
|
||||
ctx->H[7] = 0x5be0cd19;
|
||||
|
||||
ctx->total64 = 0;
|
||||
ctx->buflen = 0;
|
||||
}
|
||||
|
||||
|
||||
/* Process the remaining bytes in the internal buffer and the usual
|
||||
prolog according to the standard and write the result to RESBUF.
|
||||
IMPORTANT: On some systems it is required that RESBUF is correctly
|
||||
aligned for a 32 bits value. */
|
||||
void *
|
||||
__sha256_finish_ctx(ctx, resbuf)
|
||||
struct sha256_ctx *ctx;
|
||||
void *resbuf;
|
||||
{
|
||||
/* Take yet unprocessed bytes into account. */
|
||||
uint32_t bytes = ctx->buflen;
|
||||
size_t pad;
|
||||
|
||||
/* Now count remaining bytes. */
|
||||
ctx->total64 += bytes;
|
||||
|
||||
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
|
||||
memcpy(&ctx->buffer[bytes], fillbuf, pad);
|
||||
|
||||
/* Put the 64-bit file length in *bits* at the end of the buffer. */
|
||||
#ifdef _STRING_ARCH_unaligned
|
||||
ctx->buffer64[(bytes + pad) / 8] = SWAP64(ctx->total64 << 3);
|
||||
#else
|
||||
ctx->buffer32[(bytes + pad + 4) / 4] = SWAP(ctx->total[TOTAL64_low] << 3);
|
||||
ctx->buffer32[(bytes + pad) / 4] = SWAP((ctx->total[TOTAL64_high] << 3) |
|
||||
(ctx->total[TOTAL64_low] >> 29));
|
||||
#endif
|
||||
|
||||
/* Process last bytes. */
|
||||
sha256_process_block(ctx->buffer, bytes + pad + 8, ctx);
|
||||
|
||||
/* Put result from CTX in first 32 bytes following RESBUF. */
|
||||
for (unsigned int i = 0; i < 8; ++i)
|
||||
((uint32_t *)resbuf)[i] = SWAP(ctx->H[i]);
|
||||
|
||||
return resbuf;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
__sha256_process_bytes(buffer, len, ctx)
|
||||
const void *buffer;
|
||||
size_t len;
|
||||
struct sha256_ctx *ctx;
|
||||
{
|
||||
/* When we already have some bits in our internal buffer concatenate
|
||||
both inputs first. */
|
||||
if (ctx->buflen != 0)
|
||||
{
|
||||
size_t left_over = ctx->buflen;
|
||||
size_t add = 128 - left_over > len ? len : 128 - left_over;
|
||||
|
||||
memcpy(&ctx->buffer[left_over], buffer, add);
|
||||
ctx->buflen += add;
|
||||
|
||||
if (ctx->buflen > 64)
|
||||
{
|
||||
sha256_process_block(ctx->buffer, ctx->buflen & ~63, ctx);
|
||||
|
||||
ctx->buflen &= 63;
|
||||
/* The regions in the following copy operation cannot overlap. */
|
||||
memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
|
||||
ctx->buflen);
|
||||
}
|
||||
|
||||
buffer = (const char *)buffer + add;
|
||||
len -= add;
|
||||
}
|
||||
|
||||
/* Process available complete blocks. */
|
||||
if (len >= 64)
|
||||
{
|
||||
#if !_STRING_ARCH_unaligned
|
||||
/* To check alignment gcc has an appropriate operator. Other
|
||||
compilers don't. */
|
||||
# if __GNUC__ >= 2
|
||||
# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
|
||||
# else
|
||||
# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
|
||||
# endif
|
||||
if (UNALIGNED_P(buffer))
|
||||
while (len > 64)
|
||||
{
|
||||
sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx);
|
||||
buffer = (const char *)buffer + 64;
|
||||
len -= 64;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
sha256_process_block(buffer, len & ~63, ctx);
|
||||
buffer = (const char *)buffer + (len & ~63);
|
||||
len &= 63;
|
||||
}
|
||||
}
|
||||
|
||||
/* Move remaining bytes into internal buffer. */
|
||||
if (len > 0)
|
||||
{
|
||||
size_t left_over = ctx->buflen;
|
||||
|
||||
memcpy(&ctx->buffer[left_over], buffer, len);
|
||||
left_over += len;
|
||||
if (left_over >= 64)
|
||||
{
|
||||
sha256_process_block(ctx->buffer, 64, ctx);
|
||||
left_over -= 64;
|
||||
memcpy(ctx->buffer, &ctx->buffer[64], left_over);
|
||||
}
|
||||
ctx->buflen = left_over;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/* Declaration of functions and data types used for SHA256 sum computing
|
||||
library functions.
|
||||
Copyright (C) 2007, 2011 Free Software Foundation, Inc.
|
||||
This file is part of the GNU C Library.
|
||||
The GNU C Library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
The GNU C Library 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
|
||||
Lesser General Public License for more details.
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with the GNU C Library; if not, see
|
||||
<http://www.gnu.org/licenses/>. */
|
||||
|
||||
#ifndef _SHA256_H
|
||||
#define _SHA256_H 1
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <endian.h>
|
||||
|
||||
|
||||
/* Structure to save state of computation between the single steps. */
|
||||
struct sha256_ctx
|
||||
{
|
||||
uint32_t H[8];
|
||||
|
||||
union
|
||||
{
|
||||
uint64_t total64;
|
||||
#define TOTAL64_low (1 - (BYTE_ORDER == LITTLE_ENDIAN))
|
||||
#define TOTAL64_high (BYTE_ORDER == LITTLE_ENDIAN)
|
||||
uint32_t total[2];
|
||||
};
|
||||
uint32_t buflen;
|
||||
union
|
||||
{
|
||||
char buffer[128];
|
||||
uint32_t buffer32[32];
|
||||
uint64_t buffer64[16];
|
||||
};
|
||||
};
|
||||
|
||||
/* Initialize structure containing state of computation.
|
||||
(FIPS 180-2: 5.3.2) */
|
||||
extern void __sha256_init_ctx(struct sha256_ctx *ctx) __THROW;
|
||||
|
||||
/* Starting with the result of former calls of this function (or the
|
||||
initialization function update the context for the next LEN bytes
|
||||
starting at BUFFER.
|
||||
It is NOT required that LEN is a multiple of 64. */
|
||||
extern void __sha256_process_bytes(const void *buffer, size_t len,
|
||||
struct sha256_ctx *ctx) __THROW;
|
||||
|
||||
/* Process the remaining bytes in the buffer and put result from CTX
|
||||
in first 32 bytes following RESBUF.
|
||||
IMPORTANT: On some systems it is required that RESBUF is correctly
|
||||
aligned for a 32 bits value. */
|
||||
extern void *__sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf)
|
||||
__THROW;
|
||||
|
||||
#endif /* sha256.h */
|
||||
@@ -322,4 +322,4 @@ int main (int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
#endif /* self-test */
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user