/* * logtool - a logfile parsing/monitoring/manipulation utility * * Copyright (C) Y2K (2000) A.L.Lambert * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Yee ole includes (I put this all in one file for my sanity) */ #include "includes.h" #ifndef NO_RESOLVER /* var's for gethostbyaddr() stuff */ extern int h_errno; struct hostent *h; int lt_gdbm_query(char *ip_addr); int lt_gdbm_write(char *ip_addr, char *h_name); /* define our database stuff */ int lt_db_gethost(char *ip_addr) { short int ret = FALSE; #ifndef WE_USE_GDBM return FALSE; #endif /* we wrap this in case someday we need more complicated things to happen here */ ret = lt_gdbm_query(ip_addr); return ret; /* tell calling function if we populated lt_host or not */ return FALSE; } void lt_db_savehost(char *ip_addr, char *h_name) { char line[LSIZE]; #ifndef WE_USE_GDBM return; #endif /* we wrap this in case someday we need more complicated things to happen here */ /* we need a timestamp at the end of ip_addr */ sprintf(line, "%s %li", h_name, time(NULL)); lt_gdbm_write(ip_addr, line); return; } #endif /* no resolver */ /* a wrapper for gethostbyaddr() for my own sanity */ char *get_host(char *ip_addr) { #ifdef NO_RESOLVER return FALSE; #else short int ret = FALSE; in_addr_t ad; /* our network byte order storage spot */ if(cf.resolv == FALSE) return NULL; strcpy(lt_host, ""); /* get rid of any old data */ h = NULL; /* null out our global variable */ /* see if we can get a hostname out of the database */ ret = lt_db_gethost(ip_addr); if(ret == TRUE) { if(strcmp(lt_host, "!") != 0) { return(lt_host); } else { return NULL; } } /* if we made it here, it wasn't in the database, so move along to doing a host lookup */ ad = inet_addr(ip_addr); /* crunch input into network byte order */ h = gethostbyaddr((char *) &ad, sizeof(ad), AF_INET); if(h == NULL) { lt_db_savehost(ip_addr, "!"); return NULL; } /* see what we got and return appropriately */ if(h->h_name != NULL) { strcpy(lt_host, h->h_name); lt_db_savehost(ip_addr, lt_host); return (lt_host); } /* if we made it this far and didn't return, things are bad; so return now */ return NULL; #endif }