21 #include <winpr/config.h>
23 #include <winpr/crt.h>
24 #include <winpr/environment.h>
25 #include <winpr/winsock.h>
31 WLOG_APPENDER_COMMON();
33 struct sockaddr targetAddr;
38 static BOOL WLog_UdpAppender_Open(wLog* log, wLogAppender* appender)
40 wLogUdpAppender* udpAppender = NULL;
41 char addressString[256] = { 0 };
42 struct addrinfo hints = { 0 };
43 struct addrinfo* result = { 0 };
46 char* colonPos = NULL;
51 udpAppender = (wLogUdpAppender*)appender;
53 if (udpAppender->targetAddrLen)
56 colonPos = strchr(udpAppender->host,
':');
61 addrLen = (colonPos - udpAppender->host);
62 memcpy(addressString, udpAppender->host, addrLen);
63 addressString[addrLen] =
'\0';
64 hints.ai_family = AF_INET;
65 hints.ai_socktype = SOCK_DGRAM;
66 status = getaddrinfo(addressString, colonPos + 1, &hints, &result);
71 if (result->ai_addrlen >
sizeof(udpAppender->targetAddr))
77 memcpy(&udpAppender->targetAddr, result->ai_addr, result->ai_addrlen);
78 udpAppender->targetAddrLen = (int)result->ai_addrlen;
83 static BOOL WLog_UdpAppender_Close(wLog* log, wLogAppender* appender)
85 if (!log || !appender)
91 static BOOL WLog_UdpAppender_WriteMessage(wLog* log, wLogAppender* appender,
wLogMessage* message)
93 char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
94 wLogUdpAppender* udpAppender = NULL;
96 if (!log || !appender || !message)
99 udpAppender = (wLogUdpAppender*)appender;
100 message->PrefixString = prefix;
101 WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
102 (void)_sendto(udpAppender->sock, message->PrefixString,
103 (
int)strnlen(message->PrefixString, INT_MAX), 0, &udpAppender->targetAddr,
104 udpAppender->targetAddrLen);
105 (void)_sendto(udpAppender->sock, message->TextString,
106 (
int)strnlen(message->TextString, INT_MAX), 0, &udpAppender->targetAddr,
107 udpAppender->targetAddrLen);
108 (void)_sendto(udpAppender->sock,
"\n", 1, 0, &udpAppender->targetAddr,
109 udpAppender->targetAddrLen);
113 static BOOL WLog_UdpAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
116 if (!log || !appender || !message)
122 static BOOL WLog_UdpAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
125 if (!log || !appender || !message)
131 static BOOL WLog_UdpAppender_Set(wLogAppender* appender,
const char* setting,
void* value)
133 const char target[] =
"target";
134 wLogUdpAppender* udpAppender = (wLogUdpAppender*)appender;
137 if (!value || (strnlen(value, 2) == 0))
140 if (strncmp(target, setting,
sizeof(target)) != 0)
143 udpAppender->targetAddrLen = 0;
145 if (udpAppender->host)
146 free(udpAppender->host);
148 udpAppender->host = _strdup((
const char*)value);
149 return (udpAppender->host != NULL) && WLog_UdpAppender_Open(NULL, appender);
152 static void WLog_UdpAppender_Free(wLogAppender* appender)
154 wLogUdpAppender* udpAppender = NULL;
158 udpAppender = (wLogUdpAppender*)appender;
160 if (udpAppender->sock != INVALID_SOCKET)
162 closesocket(udpAppender->sock);
163 udpAppender->sock = INVALID_SOCKET;
166 free(udpAppender->host);
171 wLogAppender* WLog_UdpAppender_New(wLog* log)
173 wLogUdpAppender* appender = NULL;
176 appender = (wLogUdpAppender*)calloc(1,
sizeof(wLogUdpAppender));
181 appender->Type = WLOG_APPENDER_UDP;
182 appender->Open = WLog_UdpAppender_Open;
183 appender->Close = WLog_UdpAppender_Close;
184 appender->WriteMessage = WLog_UdpAppender_WriteMessage;
185 appender->WriteDataMessage = WLog_UdpAppender_WriteDataMessage;
186 appender->WriteImageMessage = WLog_UdpAppender_WriteImageMessage;
187 appender->Free = WLog_UdpAppender_Free;
188 appender->Set = WLog_UdpAppender_Set;
189 appender->sock = _socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
191 if (appender->sock == INVALID_SOCKET)
194 name =
"WLOG_UDP_TARGET";
195 nSize = GetEnvironmentVariableA(name, NULL, 0);
199 appender->host = (LPSTR)malloc(nSize);
204 if (GetEnvironmentVariableA(name, appender->host, nSize) != nSize - 1)
207 if (!WLog_UdpAppender_Open(log, (wLogAppender*)appender))
212 appender->host = _strdup(
"127.0.0.1:20000");
218 return (wLogAppender*)appender;
220 free(appender->host);
221 closesocket(appender->sock);