/*
 * rlogger.c 
 *   - logs a msg to a remote syslog
 *
 * jeff murphy (jcmurphy@smurfland.cit.buffalo.edu)
 * 110894
 */
/*
 * $Log: rlogger.c,v $
 * Revision 1.2  1994/11/10  01:02:40  jcmurphy
 * *** empty log message ***
 *
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "conf.h"

char rcsid[]="$Header: /u/staff/jcmurphy/src/HPHub/RCS/rlogger.c,v 1.2 1994/11/10 01:02:40 jcmurphy Exp jcmurphy $";

/*
 * rLogIt(dest, SYS, id, msg)
 *
 * dest = host name of destination system
 * SYS  = syslog priority and facility OR'd together.
 * id   = ident that is sent in the message
 * mesg = the message itself. 
 *
 * Example:
 * rLogIt("myhost.edu", (LOG_AUTH | LOG_NOTICE), "rlogger", "test message");
 */

int rLogIt(char *dest, int SYS, char *id, char *msg)
{
  int                 s;
  char                buf[256];
  struct sockaddr_in  sin;
  struct servent     *srvc;
  
  if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket(AF_INET, SOCK_DGRAM, 0)");
    exit(1);
  }
  sprintf(buf, "<%d>%s: %s\0", SYS, id, msg);
  
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family      = AF_INET;
  sin.sin_addr.s_addr = inet_addr(dest);

  if(srvc = getservbyname("syslog", 0)) {
    sin.sin_port        = htons(srvc->s_port);
    
    if(connect(s, (struct sockaddr *)(&sin), sizeof(sin)) < 0) {
      perror("connect()");
      exit(1);
    }
    
    send(s, buf, strlen(buf), 0);
    close(s);
  } else {
    perror("getservbyname");
    return 1;
  }

  return 0;
}

