/* 

passlogd - passive syslog capture daemon
copyright (c) 2005 - christian void <cvoid@morphine.com>

file: util.c

history: 

07jun01 cvoid: added sanitize_buffer().
04jun01 cvoid: added reverse_lookup().
03jun01 cvoid: created.

*/

#include "passlog.h"
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>

extern int debug;

/* INIT_BUF */
void init_buf(char x[], int size) 
{
  register int n;

  for(n = 0; n < size; n++)
    {
      x[n] = '\0';
    } 
}

/* FIND_HEADER_LENGTH */
int find_header_length(int d)
{
  switch(d)
    {
    case DLT_NULL:
      return(4);		/* loopback header 4 bytes */
      break;
    case DLT_EN10MB:
      return(14);		/* ethernet 10mb header 14 bytes */
      break;
    case DLT_FDDI:
      return(21);		/* fddi header 21 bytes */
      break;
    case DLT_PPP:
      return(0);		/* ppp header 0 or 24 bytes? */
      break;
    default:
      return(14);		/* ethernet 10mb header 14 bytes */
      break;
    }
}

int reverse_lookup(char * addrstr)
{
  /* do reverse lookup */
  struct hostent	*hostname, *hostaddr;

  /* resolve the src and destination addresses */
  hostaddr = gethostbyname(addrstr);
  hostname = gethostbyaddr(hostaddr->h_addr, 4, AF_INET);
  
  if(!hostname){
    return 1;
  }
  else {
    strncpy(addrstr, hostname->h_name, strlen(hostname->h_name) + 1);
    /* plus one to get the null byte */ 
    return 0;
  }
}

void sanitize_buffer(char * buffer, int buflen)
{
  /* sanitize the buffer - replace any bytes in the buffer */
  /* that are not printable, NULL or LF with space (0x20) */
  /* this probably isn't neccessary, done as a result of an */
  /* error in the parser -- left here for the hell of it */
  /* even though it is expensive */

  int c = 0; 

  for(c = 0; c <= buflen; c++){

    if ((!isprint(buffer[c])) && (buffer[c] != '\0') && (buffer[c] != '\n')){
      if(debug)
	printf("tweaked byte %d\n", c);
      buffer[c]=0x20;
    }
  }
}


syntax highlighted by Code2HTML, v. 0.9.1