/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * FILE: sendlog.c * USAGE: sendlog (ipaddr) (message) (severity) (priority) * * A trivial C-language source code program, useful for sending syslog * messages to a syslog receiver. This is similar to the source code for * the sendlog.exe program, included with the standard CorreLog Syslog * Windows Tool Set (CorreLog WTS). The program can be easily ported to * other platform operating systems * * The user must specify an IP address as the first argument, followed * by the message content. The severity and facility arguments follow, * and are integer numbers, where the "severity" ranges from 0=emergency to * 7=debug, and the "facility" ranges from 0=kernel to 23=local7. * * EXAMPLE: sendlog.exe 127.0.0.1 "Test message" 6 23 * * The above example sends "Test Message" to the local host, with "info" * severity and the "local7" facility. * * This code has been placed in the public domain by: * CorreLog, Inc. * http://www.CorreLog.com * * Rights to use and modify this program for any reason and purpose is * hereby granted to any and all parties. CorreLog, Inc. accepts * no liabilities and makes no claims about any use of this program, * or any adaptation of this program, whatsoever. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifdef WIN32 #include #include #endif #include #include #include #define SYSLOG_PORT 514 #define UDP_PROTOCOL 17 int sendlog (char *host, char *msg, int severity, int facility); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ main(argc, argv) int argc; char *argv[]; { /* A wrapper for the "sendlog()" function. */ if (argc != 5) { printf("USAGE: sendlog (ipaddr) \"(msg)\" (sev) (facil)\n"); exit(1); } /* Send the message. */ if (sendlog(argv[1], argv[2], atoi(argv[3]), atoi(argv[4])) != 0) { printf("Function sendlog() failed.\n"); exit(1); } return(0); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ int sendlog (ipaddr, msg, severity, facility) char *ipaddr; char *msg; int severity; int facility; { int sockd; int priority; int status; char main_message[1050]; struct sockaddr_in destination; unsigned long dest_addr; # ifdef WIN32 /* This is required only on Windows systems. */ WSADATA winsock_data; WSAStartup(MAKEWORD(1,1), &winsock_data); # endif /* Create the socket. */ if ((sockd = socket(AF_INET, SOCK_DGRAM, UDP_PROTOCOL)) <= 0) { printf("ERROR: Cannot make socket.\n"); return(1); } /* Check and translate the IP address argument. */ if ((dest_addr = inet_addr(ipaddr)) == INADDR_NONE) { printf("ERROR: Invalid IP address specified.\n"); return(1); } /* Compose the remote destination address info. */ destination.sin_family = (short) PF_INET; destination.sin_port = (u_short) htons ( (u_short) SYSLOG_PORT); memcpy(&destination.sin_addr, &dest_addr, sizeof(long)); /* Compose the message to send. */ facility = facility << 3; priority = facility | severity; sprintf(main_message, "<%d>%s", priority, msg); /* Send the message here. */ status = sendto ( sockd, main_message, strlen(main_message) + 1, 0, (struct sockaddr*) &destination, sizeof(destination) ); /* Close the socket. */ # ifdef WIN32 closesocket(sockd); # else close(sockd); # endif /* Check the return status. */ if (status != strlen(main_message) + 1) { printf("ERROR: Cannot send message. Status: %x\n", status); return(1); } /* We are finished. */ return(0); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* End of file. */