Add support for Winsock library and logging to the windows event log
This commit is contained in:
@@ -19,11 +19,19 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#ifndef WIN32
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
#else
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
int report_level = RPT_INFO;
|
int report_level = RPT_INFO;
|
||||||
int report_dest = RPT_DEST_STORE;
|
int report_dest = RPT_DEST_STORE;
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
HANDLE event_log_handle = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
#define MAX_STORED_MSGS 200
|
#define MAX_STORED_MSGS 200
|
||||||
|
|
||||||
static char *stored_msgs[MAX_STORED_MSGS];
|
static char *stored_msgs[MAX_STORED_MSGS];
|
||||||
@@ -55,7 +63,43 @@ void report( const int level, const char *format, .../*args*/ )
|
|||||||
fprintf( stderr, "\n" );
|
fprintf( stderr, "\n" );
|
||||||
break;
|
break;
|
||||||
case RPT_DEST_SYSLOG:
|
case RPT_DEST_SYSLOG:
|
||||||
|
{
|
||||||
|
#ifndef WIN32
|
||||||
vsyslog( LOG_USER|(level+2), format, ap );
|
vsyslog( LOG_USER|(level+2), format, ap );
|
||||||
|
#else
|
||||||
|
LPTSTR tmpArray[1];
|
||||||
|
WORD eventType;
|
||||||
|
switch (level)
|
||||||
|
{
|
||||||
|
case RPT_CRIT:
|
||||||
|
case RPT_ERR:
|
||||||
|
eventType = EVENTLOG_ERROR_TYPE;
|
||||||
|
break;
|
||||||
|
case RPT_WARNING:
|
||||||
|
eventType = EVENTLOG_WARNING_TYPE;
|
||||||
|
break;
|
||||||
|
case RPT_NOTICE:
|
||||||
|
case RPT_INFO:
|
||||||
|
case RPT_DEBUG:
|
||||||
|
default:
|
||||||
|
eventType = EVENTLOG_INFORMATION_TYPE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
vsnprintf( buf, sizeof(buf), format, ap );
|
||||||
|
tmpArray[0] = buf;
|
||||||
|
ReportEvent(event_log_handle,
|
||||||
|
eventType,
|
||||||
|
0, /* category */
|
||||||
|
0, /* event ID */
|
||||||
|
NULL, /* user ID */
|
||||||
|
1, /* number of strings */
|
||||||
|
0, /* binary data size */
|
||||||
|
(LPCTSTR*) tmpArray, /* error string */
|
||||||
|
NULL /* binary data */
|
||||||
|
);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case RPT_DEST_STORE:
|
case RPT_DEST_STORE:
|
||||||
vsnprintf( buf, sizeof(buf), format, ap );
|
vsnprintf( buf, sizeof(buf), format, ap );
|
||||||
@@ -76,10 +120,27 @@ int set_reporting( char *application_name, int new_level, int new_dest )
|
|||||||
}
|
}
|
||||||
|
|
||||||
if( report_dest != RPT_DEST_SYSLOG && new_dest == RPT_DEST_SYSLOG ) {
|
if( report_dest != RPT_DEST_SYSLOG && new_dest == RPT_DEST_SYSLOG ) {
|
||||||
|
#ifdef WIN32
|
||||||
|
if (event_log_handle)
|
||||||
|
{
|
||||||
|
CloseEventLog(event_log_handle);
|
||||||
|
} else {
|
||||||
|
event_log_handle = RegisterEventSource(NULL, "LcdProc");
|
||||||
|
}
|
||||||
|
#else
|
||||||
openlog( application_name, 0, LOG_USER );
|
openlog( application_name, 0, LOG_USER );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else if( report_dest == RPT_DEST_SYSLOG && new_dest != RPT_DEST_SYSLOG ) {
|
else if( report_dest == RPT_DEST_SYSLOG && new_dest != RPT_DEST_SYSLOG ) {
|
||||||
|
#ifdef WIN32
|
||||||
|
if (event_log_handle)
|
||||||
|
{
|
||||||
|
CloseEventLog(event_log_handle);
|
||||||
|
event_log_handle = NULL;
|
||||||
|
}
|
||||||
|
#else
|
||||||
closelog();
|
closelog();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
report_level = new_level;
|
report_level = new_level;
|
||||||
|
|||||||
@@ -61,7 +61,6 @@
|
|||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <syslog.h>
|
|
||||||
|
|
||||||
|
|
||||||
/* Reporting levels */
|
/* Reporting levels */
|
||||||
|
|||||||
+86
-10
@@ -1,15 +1,21 @@
|
|||||||
|
#include "config.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#ifndef WINSOCK2
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#else
|
||||||
|
#include <winsock2.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "report.h"
|
#include "report.h"
|
||||||
@@ -55,7 +61,11 @@ sock_connect (char *host, unsigned short int port)
|
|||||||
|
|
||||||
report (RPT_DEBUG, "sock_connect: Creating socket");
|
report (RPT_DEBUG, "sock_connect: Creating socket");
|
||||||
sock = socket (PF_INET, SOCK_STREAM, 0);
|
sock = socket (PF_INET, SOCK_STREAM, 0);
|
||||||
|
#ifdef WINSOCK2
|
||||||
|
if (sock == INVALID_SOCKET) {
|
||||||
|
#else
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
|
#endif
|
||||||
report (RPT_ERR, "sock_connect: Error creating socket");
|
report (RPT_ERR, "sock_connect: Error creating socket");
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
@@ -65,13 +75,27 @@ sock_connect (char *host, unsigned short int port)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
err = connect (sock, (struct sockaddr *) &servername, sizeof (servername));
|
err = connect (sock, (struct sockaddr *) &servername, sizeof (servername));
|
||||||
|
#ifdef WINSOCK2
|
||||||
|
if (err == INVALID_SOCKET) {
|
||||||
|
#else
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
|
#endif
|
||||||
report (RPT_ERR, "sock_connect: connect failed");
|
report (RPT_ERR, "sock_connect: connect failed");
|
||||||
shutdown (sock, 2);
|
shutdown (sock, 2);
|
||||||
return 0; // Normal exit if server doesn't exist...
|
return 0; // Normal exit if server doesn't exist...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef WINSOCK2
|
||||||
fcntl (sock, F_SETFL, O_NONBLOCK);
|
fcntl (sock, F_SETFL, O_NONBLOCK);
|
||||||
|
#else
|
||||||
|
{
|
||||||
|
unsigned long tmp = 1;
|
||||||
|
if (ioctlsocket(sock, FIONBIO, &tmp) == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
report(RPT_ERR, "sock_connect: Error setting socket to non-blocking");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
@@ -102,7 +126,11 @@ sock_send_string (int fd, char *string)
|
|||||||
while (offset != len) {
|
while (offset != len) {
|
||||||
// write isn't guaranteed to send the entire string at once,
|
// write isn't guaranteed to send the entire string at once,
|
||||||
// so we have to sent it in a loop like this
|
// so we have to sent it in a loop like this
|
||||||
|
#ifndef WINSOCK2
|
||||||
int sent = write (fd, string + offset, len - offset);
|
int sent = write (fd, string + offset, len - offset);
|
||||||
|
#else
|
||||||
|
int sent = send(fd, string + offset, len - offset, 0);
|
||||||
|
#endif
|
||||||
if (sent == -1) {
|
if (sent == -1) {
|
||||||
if (errno != EAGAIN) {
|
if (errno != EAGAIN) {
|
||||||
report (RPT_ERR, "sock_send_string: socket write error");
|
report (RPT_ERR, "sock_send_string: socket write error");
|
||||||
@@ -128,7 +156,7 @@ int
|
|||||||
sock_recv_string (int fd, char *dest, size_t maxlen)
|
sock_recv_string (int fd, char *dest, size_t maxlen)
|
||||||
{
|
{
|
||||||
char *ptr = dest;
|
char *ptr = dest;
|
||||||
int recv = 0;
|
int recvBytes = 0;
|
||||||
|
|
||||||
if (!dest)
|
if (!dest)
|
||||||
return -1;
|
return -1;
|
||||||
@@ -136,7 +164,11 @@ sock_recv_string (int fd, char *dest, size_t maxlen)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
#ifndef WINSOCK2
|
||||||
int err = read (fd, ptr, 1);
|
int err = read (fd, ptr, 1);
|
||||||
|
#else
|
||||||
|
int err = recv(fd, ptr, 1, 0);
|
||||||
|
#endif
|
||||||
if (err == -1) {
|
if (err == -1) {
|
||||||
if (errno == EAGAIN) {
|
if (errno == EAGAIN) {
|
||||||
if (recv) {
|
if (recv) {
|
||||||
@@ -150,28 +182,28 @@ sock_recv_string (int fd, char *dest, size_t maxlen)
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
} else if (err == 0) {
|
} else if (err == 0) {
|
||||||
return recv;
|
return recvBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
recv++;
|
recvBytes++;
|
||||||
|
|
||||||
if (recv == maxlen || *ptr == 0 || *ptr == 10) {
|
if (recvBytes == maxlen || *ptr == 0 || *ptr == 10) {
|
||||||
*ptr = 0;
|
*ptr = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr++;
|
ptr++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recv == 1 && dest[0] == 0) {
|
if (recvBytes == 1 && dest[0] == 0) {
|
||||||
// Don't return a null string
|
// Don't return a null string
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recv < maxlen - 1) {
|
if (recvBytes < maxlen - 1) {
|
||||||
dest[recv] = 0;
|
dest[recvBytes] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return recv;
|
return recvBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send/receive raw data
|
// Send/receive raw data
|
||||||
@@ -186,7 +218,11 @@ sock_send (int fd, void *src, size_t size)
|
|||||||
while (offset != size) {
|
while (offset != size) {
|
||||||
// write isn't guaranteed to send the entire string at once,
|
// write isn't guaranteed to send the entire string at once,
|
||||||
// so we have to sent it in a loop like this
|
// so we have to sent it in a loop like this
|
||||||
int sent = write (fd, ((char *) src) + offset, size - offset);
|
#ifndef WINSOCK2
|
||||||
|
int sent = write (fd, ((char *) src) + offset, size - offset);
|
||||||
|
#else
|
||||||
|
int sent = send(fd, ((char *) src) + offset, size - offset, 0);
|
||||||
|
#endif
|
||||||
if (sent == -1) {
|
if (sent == -1) {
|
||||||
if (errno != EAGAIN) {
|
if (errno != EAGAIN) {
|
||||||
report (RPT_ERR, "sock_send: socket write error");
|
report (RPT_ERR, "sock_send: socket write error");
|
||||||
@@ -216,7 +252,11 @@ sock_recv (int fd, void *dest, size_t maxlen)
|
|||||||
if (maxlen <= 0)
|
if (maxlen <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
#ifndef WINSOCK2
|
||||||
err = read (fd, dest, maxlen);
|
err = read (fd, dest, maxlen);
|
||||||
|
#else
|
||||||
|
err = recv(fd, dest, maxlen, 0);
|
||||||
|
#endif
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
//report (RPT_DEBUG,"sock_recv: socket read error");
|
//report (RPT_DEBUG,"sock_recv: socket read error");
|
||||||
//shutdown(fd, 2);
|
//shutdown(fd, 2);
|
||||||
@@ -226,3 +266,39 @@ sock_recv (int fd, void *dest, size_t maxlen)
|
|||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
static char retString[256];
|
||||||
|
|
||||||
|
char*
|
||||||
|
sock_geterror(void)
|
||||||
|
{
|
||||||
|
#ifndef WINSOCK2
|
||||||
|
return strerror(errno);
|
||||||
|
#else
|
||||||
|
long err;
|
||||||
|
char* tmp;
|
||||||
|
|
||||||
|
err = WSAGetLastError();
|
||||||
|
|
||||||
|
sprintf(retString, "Error code %ld: ", err);
|
||||||
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
err,
|
||||||
|
0, /* Default language */
|
||||||
|
(LPTSTR) &tmp,
|
||||||
|
0,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
/* append the message text after the error code and ensure a terminating
|
||||||
|
character ends the string */
|
||||||
|
strncpy(retString + strlen(retString), tmp,
|
||||||
|
sizeof(retString) - strlen(retString) - 1);
|
||||||
|
retString[sizeof(retString) - 1] = '\0';
|
||||||
|
|
||||||
|
return retString;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ int sock_recv_string (int fd, char *dest, size_t maxlen);
|
|||||||
int sock_send (int fd, void *src, size_t size);
|
int sock_send (int fd, void *src, size_t size);
|
||||||
int sock_recv (int fd, void *dest, size_t maxlen);
|
int sock_recv (int fd, void *dest, size_t maxlen);
|
||||||
|
|
||||||
|
/* Return error message string for the socket function */
|
||||||
|
char *sock_geterror(void);
|
||||||
|
|
||||||
// Er, ignore the rest of this file. I'll clean it up sometime...
|
// Er, ignore the rest of this file. I'll clean it up sometime...
|
||||||
|
|
||||||
/*****************************************************************
|
/*****************************************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user