"Fossies" - the Fresh Open Source Software Archive

Member "arpwatch-NG1.7/arpsnmp.c" (5 Nov 2006, 4584 Bytes) of archive /linux/misc/old/arpwatch-NG1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "arpsnmp.c": 2.1a15_vs_1.7.

    1 /*
    2  * Copyright (c) 1996, 1997, 1999, 2004
    3  *  The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that: (1) source code distributions
    7  * retain the above copyright notice and this paragraph in its entirety, (2)
    8  * distributions including binary code include the above copyright notice and
    9  * this paragraph in its entirety in the documentation or other materials
   10  * provided with the distribution, and (3) all advertising materials mentioning
   11  * features or use of this software display the following acknowledgement:
   12  * ``This product includes software developed by the University of California,
   13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
   14  * the University nor the names of its contributors may be used to endorse
   15  * or promote products derived from this software without specific prior
   16  * written permission.
   17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
   18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
   19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   20  */
   21 
   22 /*
   23  arpwatch NG has been forked by and is copyrighted by freek
   24  numerous changes have been made - please consult the CHANGES file in
   25  this distribution
   26 
   27  arpwatch NG is distributed under the GPL
   28  */
   29 
   30 #include <sys/param.h>
   31 #include <sys/types.h>      /* concession to AIX */
   32 #include <sys/file.h>
   33 
   34 #include <ctype.h>
   35 #ifdef HAVE_MEMORY_H
   36 #include <memory.h>
   37 #endif
   38 #ifdef HAVE_FCNTL_H
   39 #include <fcntl.h>
   40 #endif
   41 #include <stdio.h>
   42 #include <stdlib.h>
   43 #include <string.h>
   44 #include <syslog.h>
   45 #include <time.h>
   46 #include <unistd.h>
   47 
   48 #include "gnuc.h"
   49 #ifdef HAVE_OS_PROTO_H
   50 #include "os-proto.h"
   51 #endif
   52 
   53 #include "arpwatch.h"
   54 #include "db.h"
   55 #include "ec.h"
   56 #include "file.h"
   57 #include "machdep.h"
   58 #include "util.h"
   59 
   60 /* Forwards */
   61 int main(int, char **);
   62 int readsnmp(char *);
   63 int snmp_add(u_int32_t, u_char *, time_t, char *);
   64 __dead void usage(void) __attribute__ ((volatile));
   65 
   66 char *prog;
   67 
   68 extern int optind;
   69 extern int opterr;
   70 extern char *optarg;
   71 
   72 int main(int argc, char **argv)
   73 {
   74     char *cp;
   75     int op, i;
   76     char errbuf[256];
   77 
   78     if((cp = strrchr(argv[0], '/')) != NULL)
   79         prog = cp + 1;
   80     else
   81         prog = argv[0];
   82 
   83     if(abort_on_misalignment(errbuf) < 0) {
   84         fprintf(stderr, "%s: %s\n", prog, errbuf);
   85         exit(1);
   86     }
   87 
   88     opterr = 0;
   89         while((op = getopt(argc, argv, "df:s:t:")) != EOF)
   90         switch (op) {
   91 
   92         case 'd':
   93             ++debug;
   94             break;
   95 
   96         case 'f':
   97             arpfile = optarg;
   98             break;
   99 
  100                 case 's':
  101                         sendmail=optarg;
  102                         break;
  103 
  104                 case 't':
  105             mailto=optarg;
  106             break;
  107 
  108         default:
  109             usage();
  110         }
  111 
  112     if(optind == argc)
  113         usage();
  114 
  115     openlog(prog, 0, LOG_DAEMON);
  116 
  117     /*
  118      Read in databases
  119      XXX todo: file locking
  120      
  121      not getting both of arp.dat and ethercodes.dat is considered fatal
  122      */
  123     initializing = 1;
  124     if(readdata() > 0) {
  125         fprintf(stderr, "%s: could not open %s or %s\n", prog, arpfile, ethercodes);
  126         exit(1);
  127     }
  128     sorteinfo();
  129 
  130         if(debug > 2) {
  131         debugdump();
  132         exit(0);
  133     }
  134 
  135         initializing = 0;
  136 
  137     /* Suck files in then exit */
  138     for(i = optind; i < argc; ++i)
  139         readsnmp(argv[i]);
  140     if(!dump())
  141         exit(1);
  142     exit(0);
  143 }
  144 
  145 static time_t now;
  146 
  147 int snmp_add(u_int32_t a, u_char * e, time_t t, char *h)
  148 {
  149     /* Watch for ethernet broadcast */
  150     if(MEMCMP(e, zero, 6) == 0 || MEMCMP(e, allones, 6) == 0) {
  151         dosyslog(LOG_INFO, "ethernet broadcast", a, e, NULL);
  152         return (1);
  153     }
  154 
  155     /* Watch for some ip broadcast addresses */
  156     if(a == 0 || a == 1) {
  157         dosyslog(LOG_INFO, "ip broadcast", a, e, NULL);
  158         return (1);
  159     }
  160 
  161     /* Use current time (although it would be nice to subtract idle time) */
  162     return (ent_add(a, e, now, h));
  163 }
  164 
  165 /* Process an snmp file */
  166 int readsnmp(char *file)
  167 {
  168     FILE *f;
  169 
  170     if(debug > 2)
  171         fprintf(stderr, "%s: reading %s\n", prog, file);
  172     if((f = fopen(file, "r")) == NULL) {
  173         syslog(LOG_ERR, "fopen(%s): %m", file);
  174         return (0);
  175     }
  176     now = time(NULL);
  177     if(!file_loop(f, snmp_add, file)) {
  178         fclose(f);
  179         return (0);
  180     }
  181     fclose(f);
  182     return (1);
  183 }
  184 
  185 __dead void usage(void)
  186 {
  187     extern const char *version;
  188 
  189     printf("%s version %s - released under GPL\n", prog, version);
  190     printf("    -d                debug mode\n" \
  191            "    -f data_file      use data_file instead of arp.dat\n" \
  192            "    -s prog           use prog instead of sendmail to send mail\n" \
  193            "    -t mail_to        send mail reports To:\n" \
  194            "    file              data file generated by snmpwalk et al\n"
  195           );
  196     exit(1);
  197 }