*BSD News Article 70111


Return to BSD News archive

#! rnews 3016 bsd
Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.cs.su.oz.au!inferno.mpx.com.au!msnews.microsoft.com!tank.news.pipex.net!pipex!news.mathworks.com!newsfeed.internetmci.com!tezcat.com!news.ner.bbnplanet.net!news3.near.net!news-server.moat.platsol.com!orchard.la.platsol.com!news
From: "J.T. Anderson" <jta@locus.com>
Newsgroups: comp.unix.bsd,comp.dcom.isdn,comp.protocols.tcp-ip,microsoft.public.win95.networking,comp.os.ms-windows.networking.win95
Subject: Re: syslogd for Win95
Date: Mon, 03 Jun 1996 18:59:08 -0700
Organization: Platinum Solutions, Inc.
Lines: 90
Message-ID: <31B3986C.6E79@locus.com>
References: <31B1A351.7E73@ix.netcom.com>
NNTP-Posting-Host: tank.pipex.net
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 2.0 (Win95; I)
To: "Jeffrey J. Quisenberry" <jeffq@ix.netcom.com>
Xref: euryale.cc.adfa.oz.au comp.unix.bsd:16794 comp.dcom.isdn:34081 comp.protocols.tcp-ip:45191 comp.os.ms-windows.networking.win95:7799

Compile this as a Win32 console application:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

void SockError(char const *fun)
{
    printf("%s: error %d\n", fun, WSAGetLastError());
    WSACleanup();
    exit(1);
}

BOOL WINAPI ControlHandler(DWORD dwSignal)
{
    printf("Control handler %d\n", dwSignal);
    
    switch (dwSignal) {
        case CTRL_C_EVENT:
        case CTRL_BREAK_EVENT:
        case CTRL_CLOSE_EVENT:
        case CTRL_LOGOFF_EVENT:
        case CTRL_SHUTDOWN_EVENT:
            WSACleanup();
            exit(0);
            break;
    }
    
    return FALSE;
}

int main(int ac, char *av[])
{
    WSADATA wsadata;
    struct servent *pServ;
    SOCKADDR_IN sin;
    SOCKET s;
    int err;
    int cnt;
    int fromlen;
    SOCKADDR_IN from;
    char buf[2048];
    char now[128];
    FILE *log = NULL;
    
    err = WSAStartup(MAKEWORD(1,1), &wsadata);
    if (err != 0) {
        printf("WSAStartup: error %d\n", err);
        exit(1);
    }
    
    SetConsoleCtrlHandler(ControlHandler, TRUE);
    
    if (ac == 2) {
        log = fopen(av[1], "w");
    }
    
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    
    pServ = getservbyname("syslog", "udp");
    if (pServ == NULL) SockError("getservbyname");
    sin.sin_port = htons(pServ->s_port);
    
    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s == INVALID_SOCKET) SockError("socket");
    
    if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR)
        SockError("bind");
    
    for (;;) {
        fromlen = sizeof(from);
        cnt = recvfrom(s, buf, sizeof(buf), 0, 
                      (struct sockaddr *)&from, &fromlen);
        buf[cnt] = 0;
        _strtime(now);
        printf("%s %s\n", now, buf);
        if (log) {
            fprintf(log, "%s %s\n", now, buf);
            fflush(log);
        }
    }
    
    closesocket(s);
    WSACleanup();
}

-- 
J.T. Anderson
PLATINUM technology, Los Angeles Laboratory
jtanderson@platinum.com