sysannoy2

This is basically identical to the other version of sysannoy, at least conceptually speaking.  The major difference is that this version was written as an experiment with remote system logging.  It's also a cheesy demonstration of networking using sockets and UDP.  But, hey, it works, and it actually proved very useful once when trying to diagnose problems with a centralized system logging server that wasn't accepting log messages.


"sysannoy2.c" (click here to download)

#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>

#define MSGLENTGH       256

int main(int argc, char *argv[])
{
        int                     socketfd, counter = 1;
        char                    *message;
        const time_t            sectime = time(NULL);
        size_t                  length = 0;
        struct sockaddr_in      packet;
        struct hostent          *remotehost;
        struct servent          *service;

        if (argc < 2)
        {
                printf("Usage: %s <loghost> <message to send>\n", argv[0]);
                exit(1);
        }

        while (counter < argc)
        {
                length += (strlen(argv[counter++]) + 2);
        }

        length += 30; /* Add 30 (29 for time string, 1 for the null) */

        if ((message = (char *)malloc(length)) == NULL)
        {
                perror("Malloc failed");
                exit(1);
        }

        cftime(message, "<13>%b %d %T sysannoy:", &sectime);

        counter = 2;
        while (counter < argc)
        {
                strcat(message, " ");
                strcat(message, argv[counter++]);
        }

        bzero(&packet, sizeof(struct sockaddr_in));

        if ((remotehost = gethostbyname(argv[1])) == NULL)
        {
                perror("Host name lookup failed");
                exit(1);
        }

        if ((service = getservbyname("syslog", "udp")) == NULL)
        {
                perror("Unable to obtain syslog info");
                exit(1);
        }

        packet.sin_family = AF_INET;
        packet.sin_port = service->s_port;
        bcopy(remotehost->h_addr, &packet.sin_addr, remotehost->h_length);

        if ((socketfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
                perror("Unable to open socket");
                exit(1);
        }

        if ((sendto(socketfd, message, (length - 1), 0, (struct sockaddr *) &packet, sizeof(packet))) < 0)
        {
                perror("Unable to send message");
                exit(1);
        }

        printf("Sufficiently annoyed.\n");
}


Back to the Bad Idea page