From 46dc7803fffd06178b07d907e5a44042abeee504 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 4 Jul 2005 15:55:34 +0000 Subject: first (nearly) functional version with tcp --- NEWS | 5 ++++- syslogd.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- test.conf | 5 +++-- version.h | 2 +- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index a3d0f98c..df60e07f 100644 --- a/NEWS +++ b/NEWS @@ -4,7 +4,10 @@ Version 0.9.2 (RGer), around 2005-07-05 the needs of the IHE (Integrating the Healthcare Environment) initiative. They use syslog for auditing and may generate messages that large. -- begun a simple TCP syslog receiver +- added a syslog/tcp receiver; we now can receive messages via + plain tcp, but we can still send only via UDP. +- slightly changed some error messages that contained a spurios \n at + the end of the line (which gives empty lines in your log...) --------------------------------------------------------------------------- Version 0.9.1 (RGer) diff --git a/syslogd.c b/syslogd.c index c485c7b7..6e426889 100644 --- a/syslogd.c +++ b/syslogd.c @@ -630,6 +630,8 @@ static int sockTCPLstn = -1; struct TCPSession { int sock; char msg[MAXLINE]; +/* struct sockaddr remHost; maybe not needed...*/ + char *fromHost; } TCPSessions[TCPSESS_MAX]; @@ -749,12 +751,18 @@ void TCPSessAccept(void) { int newConn; int iSess; - newConn = accept(sockTCPLstn, NULL, NULL); + struct sockaddr_in addr; + socklen_t addrlen = sizeof(struct sockaddr); + int lenHostName; + char *fromHost; + char *pBuf; + + newConn = accept(sockTCPLstn, (struct sockaddr*) &addr, &addrlen); if (newConn < 0) { - logerror("tcp accept, ignoring error"); + logerror("tcp accept, ignoring error and connection request"); return; } - /* TODO: must change to nonblocking? */ + /* TODO: must change to nonblocking! */ /* Add to session list */ iSess = TCPSessFindFreeSpot(); @@ -766,6 +774,17 @@ void TCPSessAccept(void) } /* OK, we have a "good" index... */ + /* get the host name */ + fromHost = (char *)cvthname(&addr); + lenHostName = strlen(fromHost) + 1; /* for \0 byte */ + if((pBuf = (char*) malloc(sizeof(char) * lenHostName)) == NULL) { + logerror("couldn't allocate buffer for hostname - ignored"); + TCPSessions[iSess].fromHost = "NO-MEMORY-FOR-HOSTNAME"; + } else { + memcpy(pBuf, fromHost, lenHostName); + TCPSessions[iSess].fromHost = pBuf; + } + TCPSessions[iSess].sock = newConn; TCPSessions[iSess].msg[0] = '\0'; /* init msg buffer! */ } @@ -785,8 +804,35 @@ void TCPSessClose(int iSess) close(TCPSessions[iSess].sock); TCPSessions[iSess].sock = -1; + free(TCPSessions[iSess].fromHost); + TCPSessions[iSess].fromHost = NULL; /* not really needed, but... */ +} + + +/* Processes the data received via a TCP session. If there + * is no other way to handle it, data is discarded. + * Input parameter data is the data received, iLen is its + * len as returned from recv(). iLen must be 1 or more (that + * is errors must be handled by caller!). iTCPSess must be + * the index of the TCP session that received the data. + * rgerhards 2005-07-04 + */ +void TCPSessDataRcvd(int iTCPSess, char *data, int iLen) +{ + assert(data != NULL); + assert(iLen > 0); + assert(iTCPSess >= 0); + assert(iTCPSess < TCPSESS_MAX); + assert(TCPSessions[iTCPSess].sock != -1); + + if(*(data+iLen - 1) == '\n') + *(data+iLen-1) = '\0'; + else + *(data+iLen) = '\0'; + printline(TCPSessions[iTCPSess].fromHost, data, SOURCE_INET); } + #endif /******************************************************************** * ### END OF SYSLOG/TCP CODE ### @@ -2244,15 +2290,16 @@ int main(argc, argv) /* Session closed */ TCPSessClose(iTCPSess); } else if(state == -1) { - /*TODO: good error message */ - logerror("TCP session - session will be closed, error ignored"); - /* fd);*/ + char errmsg[128]; + snprintf(errmsg, sizeof(errmsg)/sizeof(char), + "TCP session %d will be closed, error ignored", + fd); + logerror(errmsg); TCPSessClose(iTCPSess); } else { /* valid data received, process it! */ - dprintf("Data received: '%s'\n", buf); + TCPSessDataRcvd(iTCPSess, buf, state); } - getchar(); } iTCPSess = TCPSessGetNxtSess(iTCPSess); } @@ -2478,7 +2525,7 @@ void printchopped(hname, msg, len, fd, iSource) dprintf("Message length: %d, File descriptor: %d.\n", len, fd); tmpline[0] = '\0'; - if ( parts[fd] != (char *) 0 ) + if ( parts[fd] != (char *) NULL ) { dprintf("Including part from messages.\n"); strcpy(tmpline, parts[fd]); @@ -3246,14 +3293,14 @@ again: /* didn't work out, so disable... */ f->f_type = F_UNUSED; snprintf(errMsg, sizeof(errMsg), - "no longer writing to file %s; grown beyond configured file size of %lld bytes, actual size %lld - configured command did not resolve situation\n", + "no longer writing to file %s; grown beyond configured file size of %lld bytes, actual size %lld - configured command did not resolve situation", f->f_un.f_fname, (long long) f->f_sizeLimit, (long long) actualFileSize); errno = 0; logerror(errMsg); return; } else { snprintf(errMsg, sizeof(errMsg), - "file %s had grown beyond configured file size of %lld bytes, actual size was %lld - configured command resolved situation\n", + "file %s had grown beyond configured file size of %lld bytes, actual size was %lld - configured command resolved situation", f->f_un.f_fname, (long long) f->f_sizeLimit, (long long) actualFileSize); errno = 0; logerror(errMsg); diff --git a/test.conf b/test.conf index 0d3a0a11..3959e886 100644 --- a/test.conf +++ b/test.conf @@ -138,9 +138,10 @@ $template WinSyslogFmt,"%HOSTNAME%,%timegenerated:1:10:date-rfc3339%,%timegenera # by a comma and then the name of a template to use. # This is an example: #authpriv.* /var/log/secure,precise -*.* rger +#*.* rger #*.* *;MySQLInsert -*.* -/home/rger/proj/rsyslog/logfile;WinSyslogFmt +#*.* -/home/rger/proj/rsyslog/logfile;WinSyslogFmt +*.* /home/rger/proj/rsyslog/logfile #*.* /home/rger/proj/rsyslog/logfile;UserMsg #*.* /home/rger/proj/rsyslog/tradfile;TraditionalFormat #*.* @172.19.2.16;RFC3164fmt diff --git a/version.h b/version.h index 95d17d2b..3080db43 100644 --- a/version.h +++ b/version.h @@ -1,2 +1,2 @@ #define VERSION "0.9" -#define PATCHLEVEL "1" +#define PATCHLEVEL "2" -- cgit