2015-11-04 20:11:19 +03:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* WinPR Logger
|
|
|
|
*
|
|
|
|
* Copyright © 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright © 2015 David FORT <contact@hardening-consulting.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/environment.h>
|
2015-11-10 18:29:09 +03:00
|
|
|
#include <winpr/winsock.h>
|
2015-11-04 20:11:19 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
#include "wlog.h"
|
2015-11-04 20:11:19 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
struct _wLogUdpAppender
|
|
|
|
{
|
|
|
|
WLOG_APPENDER_COMMON();
|
2018-08-22 14:16:58 +03:00
|
|
|
char* host;
|
2015-11-09 19:56:44 +03:00
|
|
|
struct sockaddr targetAddr;
|
2019-11-06 17:24:51 +03:00
|
|
|
int targetAddrLen;
|
2015-11-09 19:56:44 +03:00
|
|
|
SOCKET sock;
|
|
|
|
};
|
|
|
|
typedef struct _wLogUdpAppender wLogUdpAppender;
|
|
|
|
|
|
|
|
static BOOL WLog_UdpAppender_Open(wLog* log, wLogAppender* appender)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
2018-08-22 14:16:58 +03:00
|
|
|
wLogUdpAppender* udpAppender;
|
2015-11-04 20:11:19 +03:00
|
|
|
char addressString[256];
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo* result;
|
|
|
|
int status, addrLen;
|
2018-08-22 14:16:58 +03:00
|
|
|
char* colonPos;
|
2015-11-04 20:11:19 +03:00
|
|
|
|
2015-11-07 01:17:11 +03:00
|
|
|
if (!appender)
|
2015-11-04 20:11:19 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
udpAppender = (wLogUdpAppender*)appender;
|
|
|
|
|
|
|
|
if (udpAppender->targetAddrLen) /* already opened */
|
2015-11-04 20:11:19 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
colonPos = strchr(udpAppender->host, ':');
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (!colonPos)
|
|
|
|
return FALSE;
|
2018-08-22 14:16:58 +03:00
|
|
|
|
|
|
|
addrLen = (int)(colonPos - udpAppender->host);
|
2015-11-09 19:56:44 +03:00
|
|
|
memcpy(addressString, udpAppender->host, addrLen);
|
2015-11-04 20:11:19 +03:00
|
|
|
addressString[addrLen] = '\0';
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
2018-08-22 14:16:58 +03:00
|
|
|
status = getaddrinfo(addressString, colonPos + 1, &hints, &result);
|
2015-11-04 20:11:19 +03:00
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (result->ai_addrlen > sizeof(udpAppender->targetAddr))
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
memcpy(&udpAppender->targetAddr, result->ai_addr, result->ai_addrlen);
|
2019-11-06 17:24:51 +03:00
|
|
|
udpAppender->targetAddrLen = (int)result->ai_addrlen;
|
2018-08-21 11:27:56 +03:00
|
|
|
freeaddrinfo(result);
|
2015-11-04 20:11:19 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_UdpAppender_Close(wLog* log, wLogAppender* appender)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_UdpAppender_WriteMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
char prefix[WLOG_MAX_PREFIX_SIZE];
|
2018-08-22 14:16:58 +03:00
|
|
|
wLogUdpAppender* udpAppender;
|
2015-11-04 20:11:19 +03:00
|
|
|
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
udpAppender = (wLogUdpAppender*)appender;
|
2015-11-04 20:11:19 +03:00
|
|
|
message->PrefixString = prefix;
|
|
|
|
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
|
2019-10-29 13:25:44 +03:00
|
|
|
_sendto(udpAppender->sock, message->PrefixString, (int)strnlen(message->PrefixString, INT_MAX),
|
2018-08-22 14:16:58 +03:00
|
|
|
0, &udpAppender->targetAddr, udpAppender->targetAddrLen);
|
2019-11-06 17:24:51 +03:00
|
|
|
_sendto(udpAppender->sock, message->TextString, (int)strnlen(message->TextString, INT_MAX), 0,
|
|
|
|
&udpAppender->targetAddr, udpAppender->targetAddrLen);
|
2015-11-09 19:56:44 +03:00
|
|
|
_sendto(udpAppender->sock, "\n", 1, 0, &udpAppender->targetAddr, udpAppender->targetAddrLen);
|
2015-11-04 20:11:19 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
static BOOL WLog_UdpAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
|
2019-11-06 17:24:51 +03:00
|
|
|
wLogMessage* message)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
static BOOL WLog_UdpAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
|
2019-11-06 17:24:51 +03:00
|
|
|
wLogMessage* message)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
if (!log || !appender || !message)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
static BOOL WLog_UdpAppender_Set(wLogAppender* appender, const char* setting, void* value)
|
2015-11-09 19:56:44 +03:00
|
|
|
{
|
2019-10-29 13:25:44 +03:00
|
|
|
const char target[] = "target";
|
2018-08-22 14:16:58 +03:00
|
|
|
wLogUdpAppender* udpAppender = (wLogUdpAppender*)appender;
|
2015-11-09 19:56:44 +03:00
|
|
|
|
2019-10-29 13:25:44 +03:00
|
|
|
/* Just check the value string is not empty */
|
|
|
|
if (!value || (strnlen(value, 2) == 0))
|
2015-11-09 19:56:44 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2019-10-29 13:25:44 +03:00
|
|
|
if (strncmp(target, setting, sizeof(target)))
|
2015-11-09 19:56:44 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
udpAppender->targetAddrLen = 0;
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (udpAppender->host)
|
|
|
|
free(udpAppender->host);
|
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
udpAppender->host = _strdup((const char*)value);
|
2015-11-09 19:56:44 +03:00
|
|
|
return (udpAppender->host != NULL) && WLog_UdpAppender_Open(NULL, appender);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WLog_UdpAppender_Free(wLogAppender* appender)
|
|
|
|
{
|
|
|
|
wLogUdpAppender* udpAppender;
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (appender)
|
|
|
|
{
|
2018-08-22 14:16:58 +03:00
|
|
|
udpAppender = (wLogUdpAppender*)appender;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (udpAppender->sock != INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
closesocket(udpAppender->sock);
|
|
|
|
udpAppender->sock = INVALID_SOCKET;
|
|
|
|
}
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
free(udpAppender->host);
|
|
|
|
free(udpAppender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wLogAppender* WLog_UdpAppender_New(wLog* log)
|
2015-11-04 20:11:19 +03:00
|
|
|
{
|
|
|
|
wLogUdpAppender* appender;
|
|
|
|
DWORD nSize;
|
|
|
|
LPCSTR name;
|
2019-11-06 17:24:51 +03:00
|
|
|
appender = (wLogUdpAppender*)calloc(1, sizeof(wLogUdpAppender));
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (!appender)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
appender->Type = WLOG_APPENDER_UDP;
|
2015-11-09 19:56:44 +03:00
|
|
|
appender->Open = WLog_UdpAppender_Open;
|
|
|
|
appender->Close = WLog_UdpAppender_Close;
|
|
|
|
appender->WriteMessage = WLog_UdpAppender_WriteMessage;
|
|
|
|
appender->WriteDataMessage = WLog_UdpAppender_WriteDataMessage;
|
|
|
|
appender->WriteImageMessage = WLog_UdpAppender_WriteImageMessage;
|
|
|
|
appender->Free = WLog_UdpAppender_Free;
|
|
|
|
appender->Set = WLog_UdpAppender_Set;
|
2015-11-04 20:11:19 +03:00
|
|
|
appender->sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (appender->sock == INVALID_SOCKET)
|
|
|
|
goto error_sock;
|
|
|
|
|
|
|
|
name = "WLOG_UDP_TARGET";
|
|
|
|
nSize = GetEnvironmentVariableA(name, NULL, 0);
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (nSize)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
appender->host = (LPSTR)malloc(nSize);
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (!appender->host)
|
2017-03-03 14:37:27 +03:00
|
|
|
goto error_open;
|
2015-11-04 20:11:19 +03:00
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
if (GetEnvironmentVariableA(name, appender->host, nSize) != nSize - 1)
|
|
|
|
goto error_open;
|
2015-11-04 20:11:19 +03:00
|
|
|
|
2018-08-22 14:16:58 +03:00
|
|
|
if (!WLog_UdpAppender_Open(log, (wLogAppender*)appender))
|
2015-11-04 20:11:19 +03:00
|
|
|
goto error_open;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appender->host = _strdup("127.0.0.1:20000");
|
2018-08-22 14:16:58 +03:00
|
|
|
|
2015-11-04 20:11:19 +03:00
|
|
|
if (!appender->host)
|
2017-03-03 14:37:27 +03:00
|
|
|
goto error_open;
|
2015-11-04 20:11:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
return (wLogAppender*)appender;
|
2015-11-04 20:11:19 +03:00
|
|
|
error_open:
|
|
|
|
free(appender->host);
|
|
|
|
closesocket(appender->sock);
|
|
|
|
error_sock:
|
|
|
|
free(appender);
|
|
|
|
return NULL;
|
|
|
|
}
|