[Autosec-devel] sonar/plugins error.c,1.3,1.4 inp_rand.c,1.10,1.11 log_file.c,1.26,1.27 log_file.h,1
Brought to you by:
red0x
Update of /cvsroot/autosec/sonar/plugins In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24331/plugins Modified Files: error.c inp_rand.c log_file.c log_file.h log_xml.c log_xml.h network_icmp.c network_icmp.h rfc793.c rfc793.h Log Message: Massive code 'indent' Index: rfc793.h =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/rfc793.h,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** rfc793.h 31 Mar 2004 11:53:20 -0000 1.29 --- rfc793.h 13 Apr 2004 03:42:07 -0000 1.30 *************** *** 35,43 **** #ifndef ICMP_DEST_UNREACH ! #define ICMP_DEST_UNREACH 3 /* Destination Unreachable */ #endif #ifndef ICMP_PORT_UNREACH ! #define ICMP_PORT_UNREACH 3 /* Port Unreachable */ #endif --- 35,43 ---- #ifndef ICMP_DEST_UNREACH ! #define ICMP_DEST_UNREACH 3 /* Destination Unreachable */ #endif #ifndef ICMP_PORT_UNREACH ! #define ICMP_PORT_UNREACH 3 /* Port Unreachable */ #endif *************** *** 120,141 **** struct tcp_timestamp { ! uint8_t kind; /* TCPOPT_TIMESTAMP */ ! uint8_t len; /* 10 */ ! uint32_t tsval; /* my timestamp */ ! uint32_t tsecho; /* server's timestamp */ }; struct recv_t { ! /// socket to listen on (must already be open) int socket, errs; ! /// number of hosts and ports we pinged uint32_t hosts; ! uint32_t ports; ! /// set the number of responses uint32_t responses; ! /// start time clock_t start; ! /// max wait time (timeout function) clock_t max; }; --- 120,141 ---- struct tcp_timestamp { ! uint8_t kind; /* TCPOPT_TIMESTAMP */ ! uint8_t len; /* 10 */ ! uint32_t tsval; /* my timestamp */ ! uint32_t tsecho; /* server's timestamp */ }; struct recv_t { ! // / socket to listen on (must already be open) int socket, errs; ! // / number of hosts and ports we pinged uint32_t hosts; ! uint32_t ports; ! // / set the number of responses uint32_t responses; ! // / start time clock_t start; ! // / max wait time (timeout function) clock_t max; }; *************** *** 155,157 **** ! #endif //_RFC793_H_ --- 155,157 ---- ! #endif // _RFC793_H_ Index: inp_rand.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/inp_rand.c,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** inp_rand.c 8 Apr 2004 19:11:02 -0000 1.10 --- inp_rand.c 13 Apr 2004 03:42:07 -0000 1.11 *************** *** 24,29 **** static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; ! /// Plugin's plugin_t structure, built by plugin_init static plugin_t pthis; --- 24,29 ---- static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; ! /// Plugin's plugin_t structure, built by plugin_init static plugin_t pthis; *************** *** 36,40 **** static char my_hostname[BUF_SIZE]; static unsigned int num_hosts; ! static int plugin_destroy(void); static int plugin_open(void *c_data, int i_data); --- 36,40 ---- static char my_hostname[BUF_SIZE]; static unsigned int num_hosts; ! static int plugin_destroy(void); static int plugin_open(void *c_data, int i_data); *************** *** 45,222 **** static void plugin_status(void); static unsigned int get_random(void); ! ! void *plugin_init(void *in_data) { ! assert(in_data != NULL); ! //useless reference, to avoid a warning ! if(rcsid); ! ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! { ! fprintf(stderr, "Plugin API must be 6 or higher, update sonar or downgrade this plugin\n"); ! } ! pthis.type = INPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.selected = false; ! pthis.version = 6; /* set API version 5 */ ! pthis.plugin_char = 'R'; ! pthis.shortname = "random"; ! pthis.longname = "Random Input Plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = &plugin_next; ! // this is not needed for net plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! ! srand((unsigned int ) time(NULL) ^ (unsigned int) &mthis); ! ! memset(my_hostname, 0, BUF_SIZE); ! /* default to one host */ ! num_hosts = 1; ! return &pthis; ! } ! static int plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! ! memset(my_hostname, 0, 16); ! return my_errno; } ! static int plugin_open(void *c_data, int i_data) { ! /* return the buffer size */ ! if(c_data) ! { #ifdef DEBUG ! printf("opts: \'%s\'\n", c_data); #endif ! num_hosts = atoi(c_data); ! if(num_hosts <= 0) ! num_hosts = 1; ! } ! pthis.ready = true; ! return BUF_SIZE -1; } ! static int plugin_close(void) { ! memset(my_hostname, 0, BUF_SIZE); ! return PLUGIN_OK; } ! static int plugin_next(char *hostname) { ! static unsigned int returned = 0; ! static char *myname; ! char myport[PORT_SIZE]; ! unsigned long int addr, len; ! unsigned int port; ! struct in_addr ia; ! ! assert(hostname != NULL); ! ! if(returned >= num_hosts) ! return PLUGIN_ERROR; ! returned++; ! ! memset(myport, 0, PORT_SIZE); ! addr = (unsigned long int) get_random(); ! port = get_random() % 65535; ! snprintf(myport, PORT_SIZE, "%u", port); ! ! ia.s_addr = addr; ! myname = inet_ntoa(ia); ! ! len = strlen(myname); ! memcpy(hostname, myname, len); ! strncat(hostname, ":", 1); ! strncat(hostname, myport, strlen(myport)); ! if(mthis->opts.iVerbose > 0) ! mthis->message("Scanning %s\n", hostname); ! ! return PLUGIN_OK; } ! static void plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("random input loaded::(%d)\n", num_hosts); } ! static unsigned int get_random(void) { ! FILE *fp; ! unsigned int ret; ! if((fp = fopen("/dev/urandom", "r")) != NULL) ! { ! if((fread(&ret, sizeof(unsigned int), 1, fp)) == 1) ! { ! fclose(fp); ! return ret; ! } ! } ! if((fp = fopen("/dev/random", "r")) != NULL) ! { ! if(fread(&ret, sizeof(unsigned int), 1, fp) == 1) ! { ! fclose(fp); ! return ret; ! } ! } ! srand((unsigned int) clock()); ! ret = 0x1008 + (unsigned int) (0xfffe * rand() / (RAND_MAX + 1.0)); ! return ret; } ! static void plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n Random input plugin\n"); ! mthis->output( ! " Usage: -iR,[number]\n"); ! mthis->output( ! "\t number: number of hostnames to return\n"); } ! static int plugin_last_error(void) { ! return my_errno; } --- 45,230 ---- static void plugin_status(void); static unsigned int get_random(void); ! ! void * ! plugin_init(void *in_data) { ! assert(in_data != NULL); ! // useless reference, to avoid a warning ! if(rcsid) ; ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! { ! fprintf(stderr, ! "Plugin API must be 6 or higher, update sonar or downgrade this plugin\n"); ! } ! pthis.type = INPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.selected = false; ! pthis.version = 6; /* set API version 5 */ ! pthis.plugin_char = 'R'; ! pthis.shortname = "random"; ! pthis.longname = "Random Input Plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = &plugin_next; ! // this is not needed for net plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! srand((unsigned int) time(NULL) ^ (unsigned int) &mthis); ! ! memset(my_hostname, 0, BUF_SIZE); ! /* default to one host */ ! num_hosts = 1; ! ! return &pthis; ! } ! ! static int ! plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! ! memset(my_hostname, 0, 16); ! return my_errno; } ! static int ! plugin_open(void *c_data, int i_data) { ! /* return the buffer size */ ! if(c_data) ! { #ifdef DEBUG ! printf("opts: \'%s\'\n", c_data); #endif ! num_hosts = atoi(c_data); ! if(num_hosts <= 0) ! num_hosts = 1; ! } ! pthis.ready = true; ! return BUF_SIZE - 1; } ! static int ! plugin_close(void) { ! memset(my_hostname, 0, BUF_SIZE); ! return PLUGIN_OK; } ! static int ! plugin_next(char *hostname) { ! static unsigned int returned = 0; ! static char *myname; ! char myport[PORT_SIZE]; ! unsigned long int addr, len; ! unsigned int port; ! struct in_addr ia; ! ! assert(hostname != NULL); ! ! if(returned >= num_hosts) ! return PLUGIN_ERROR; ! returned++; ! ! memset(myport, 0, PORT_SIZE); ! addr = (unsigned long int) get_random(); ! port = get_random() % 65535; ! snprintf(myport, PORT_SIZE, "%u", port); ! ! ia.s_addr = addr; ! myname = inet_ntoa(ia); ! ! len = strlen(myname); ! memcpy(hostname, myname, len); ! strncat(hostname, ":", 1); ! strncat(hostname, myport, strlen(myport)); ! if(mthis->opts.iVerbose > 0) ! mthis->message("Scanning %s\n", hostname); ! ! return PLUGIN_OK; } ! static void ! plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("random input loaded::(%d)\n", num_hosts); } ! static unsigned int ! get_random(void) { ! FILE *fp; ! unsigned int ret; ! if((fp = fopen("/dev/urandom", "r")) != NULL) ! { ! if((fread(&ret, sizeof(unsigned int), 1, fp)) == 1) ! { ! fclose(fp); ! return ret; ! } ! } ! if((fp = fopen("/dev/random", "r")) != NULL) ! { ! if(fread(&ret, sizeof(unsigned int), 1, fp) == 1) ! { ! fclose(fp); ! return ret; ! } ! } ! srand((unsigned int) clock()); ! ret = 0x1008 + (unsigned int) (0xfffe * rand() / (RAND_MAX + 1.0)); ! return ret; } ! static void ! plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n Random input plugin\n"); ! mthis->output(" Usage: -iR,[number]\n"); ! mthis->output("\t number: number of hostnames to return\n"); } ! static int ! plugin_last_error(void) { ! return my_errno; } Index: network_icmp.h =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/network_icmp.h,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** network_icmp.h 30 Mar 2004 22:04:24 -0000 1.33 --- network_icmp.h 13 Apr 2004 03:42:07 -0000 1.34 *************** *** 82,105 **** struct recv_t { ! /// socket to listen on (must already be open) ! int socket; ! /// number of hosts we pinged ! unsigned int hosts; ! /// set the number of responses ! unsigned int responses; ! /// start time ! unsigned int start; ! /// max wait time (timeout function) ! unsigned int max; }; typedef struct icmp_options_s { ! uint8_t ttl; ! uint8_t debug; ! uint32_t size; ! }icmp_options_t; void *plugin_init(void *in_data); ! #endif //_ICMP_PLUG_H_ --- 82,105 ---- struct recv_t { ! // / socket to listen on (must already be open) ! int socket; ! // / number of hosts we pinged ! unsigned int hosts; ! // / set the number of responses ! unsigned int responses; ! // / start time ! unsigned int start; ! // / max wait time (timeout function) ! unsigned int max; }; typedef struct icmp_options_s { ! uint8_t ttl; ! uint8_t debug; ! uint32_t size; ! } icmp_options_t; void *plugin_init(void *in_data); ! #endif // _ICMP_PLUG_H_ Index: log_xml.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/log_xml.c,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** log_xml.c 8 Apr 2004 23:28:16 -0000 1.11 --- log_xml.c 13 Apr 2004 03:42:07 -0000 1.12 *************** *** 25,29 **** static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; /// Plugin's plugin_t structure, built by plugin_init --- 25,29 ---- static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; /// Plugin's plugin_t structure, built by plugin_init *************** *** 43,47 **** static int plugin_close(void); static int plugin_output(char *msg, va_list * ap); ! static int plugin_results(target_t *result, unsigned int num); static void plugin_usage(void); static void plugin_status(void); --- 43,47 ---- static int plugin_close(void); static int plugin_output(char *msg, va_list * ap); ! static int plugin_results(target_t * result, unsigned int num); static void plugin_usage(void); static void plugin_status(void); *************** *** 56,374 **** */ ! void *plugin_init(void *in_data) { ! assert(in_data != NULL); ! //useless reference, to avoid a warning ! if(rcsid); ! ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! err_quit("Plugin API must be 6 or higher, update sonar"); ! pthis.type = OUTPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.version = 6; /* set API version 6 */ ! pthis.plugin_char = 'X'; ! pthis.shortname = "xml"; ! pthis.longname = "XML Log File output plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = NULL; ! // this is not needed for I/O plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = &plugin_output; ! pthis.plugin_results = &plugin_results; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! my_file = NULL; ! my_fp = NULL; ! return &pthis; } ! static int plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! my_file = NULL; ! my_fp = NULL; ! return PLUGIN_UNLOADED; } ! static int plugin_open(void *c_data, int i_data) { ! char *file_opts; // pointer to additional opts after filename ! char *append_or_overwrite = NULL; // indicates append or overwrite option ! int filename_len = i_data; // set length assuming no further options - - pthis.selected = true; // we were selected, whether the right arguments are present or not ! if (!c_data || i_data == 0) ! return PLUGIN_NOFILE; ! file_opts = strchr(c_data, ','); ! if (file_opts != NULL) ! { ! *file_opts++ = 0; // NULL delimit previous string, and advance ! // ! // if there was a comma, then don't include the comma and the ! // characters after it as part of the filename ! // ! filename_len = strlen(c_data); ! // ! // if an 'a' is found, then set the appropriate char option for fopen ! // other options may be filtered out with further if statements ! // ! if (*file_opts == 'a') ! append_or_overwrite = strdup("a"); ! else ! append_or_overwrite = strdup("w+"); ! } ! else ! append_or_overwrite = strdup("w+"); ! // check to see if my_file points to prev allocated data? ! my_file = strndup(c_data, filename_len); #ifdef DEBUG ! printf("fopen(\"%s\", \"%s\");\n", my_file, append_or_overwrite); #endif ! my_fp = fopen(my_file, append_or_overwrite); ! free(append_or_overwrite); ! if(my_fp == NULL) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("Could not open file"); ! pthis.ready = false; ! return PLUGIN_NOFILE; ! } ! pthis.ready = true; ! return PLUGIN_OK; } ! static int plugin_close(void) { ! fprintf(my_fp, "</sonar-scan>\n"); ! fclose(my_fp); ! free(my_file); ! return PLUGIN_OK; } ! static int plugin_output(char *msg, va_list * ap) { ! struct tm *tm; ! char buf[60]; ! time_t now; ! int tries = 0; ! ! /* sanity checks */ ! if(!pthis.ready) ! { ! *(pthis.err) = PLUGIN_NOPEN; ! err_msg("output plugin not ready"); ! return PLUGIN_NOPEN; ! } ! while(!my_fp && tries < 3) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("output plugin not ready, retrying"); ! my_fp = fopen(my_file, "a"); /* reopen it instead of dying */ ! /*return PLUGIN_NOFILE;*/ ! tries++; ! } ! if(!my_fp) ! return PLUGIN_NOFILE; /* we tried */ ! if(!header) ! { ! now = time(NULL); ! if (!(tm = localtime(&now))) ! err_sys("could not get localtime\n"); ! if (strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M %Z", tm) <= 0) ! err_sys("Could not format localtime\n"); ! fprintf(my_fp, "<?xml version=\"1.0\" ?>\n"); ! fprintf(my_fp, "<!-- %s %s initiated at %s as %s -->\n", PACKAGE, VERSION, buf, mthis->opts.sCommandLine); ! fprintf(my_fp, "<sonar-scan>\n"); ! header++; ! } ! fprintf(my_fp, "<!-- "); ! vfprintf(my_fp, msg, *ap); ! fprintf(my_fp, " -->\n"); ! return PLUGIN_OK; } ! static int null_result(target_t *result) { ! /* as of right now, there is no way to report dead hosts */ ! fprintf(my_fp, ! "\t<deadhost hostname=\"unreported-in-this-version\" />\n"); ! return PLUGIN_OK; } ! static int plugin_results(target_t *result, unsigned int num_hosts) { ! struct sockaddr_in *sa = NULL; ! struct sockaddr_in6 *sa6 = NULL; ! char ip6sa_format[] = "%x%x%x%x:%x%x%x%x:%x%x%x%x:%x%x%x%x\0"; ! char buf[256]; ! /*struct sockaddr_un *un;*/ ! ! int i, j; ! ! for(j = 0; j< num_hosts; j++) ! { ! assert(result != NULL); ! if(result->addrinfo == NULL) ! { ! null_result(result); ! continue; ! } ! ! memset(buf, 0, 256); ! fprintf(my_fp, ! "\t<host hostname=\"%s\" rtt=\"%0.2f\"" ! "ttl=\"%d\" mask=\"%d\"", ! result->addrinfo->ai_canonname, ! result->stats.rtt_avg, result->stats.ttl, ! result->netmask); ! switch(result->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in*) ! result->addrinfo->ai_addr; ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! inet_ntoa(sa->sin_addr), "ipv4"); ! break; ! case PF_INET6: ! sa6 = (struct sockaddr_in6 *) ! result->addrinfo->ai_addr; ! snprintf(buf, 255, ip6sa_format, ! sa6->sin6_addr.s6_addr[0], ! sa6->sin6_addr.s6_addr[1], ! sa6->sin6_addr.s6_addr[2], ! sa6->sin6_addr.s6_addr[3], ! sa6->sin6_addr.s6_addr[4], ! sa6->sin6_addr.s6_addr[5], ! sa6->sin6_addr.s6_addr[6], ! sa6->sin6_addr.s6_addr[7], ! sa6->sin6_addr.s6_addr[8], ! sa6->sin6_addr.s6_addr[9], ! sa6->sin6_addr.s6_addr[10], ! sa6->sin6_addr.s6_addr[11], ! sa6->sin6_addr.s6_addr[12], ! sa6->sin6_addr.s6_addr[13], ! sa6->sin6_addr.s6_addr[14], ! sa6->sin6_addr.s6_addr[15]); ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! buf, "ipv6"); ! break; ! /*case PF_UNIX: ! un = (struct sockaddr_un*) ! result->ainfo->ai_addr; ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! un->sun_path, "unix"); ! break;*/ ! default: ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! "unknown-type", "unknown"); ! }; ! ! /* print the port info */ ! for(i =0; i < 65536; i++) ! { ! switch(result->ports[i]) ! { ! case PORT_OPENED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"open\"/>\n", i); ! break; ! case PORT_CLOSED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"closed\"/>\n", i); ! break; ! case PORT_FILTERED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"filtered\"/>\n", i); ! break; ! case PORT_UNFILTERED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"unfiltered\"/>\n", i); ! break; ! } ! } ! fprintf(my_fp, ! "\t\t<stats sent=\"%d\" recvd=\"%d\" dropped=\"%d\" ", ! result->stats.packets_sent, result->stats.packets_recvd, ! result->stats.packets_sent ! - result->stats.packets_recvd); ! fprintf(my_fp, ! " rtt_min=\"%.2f\" rtt_max=\"%.2f\" rtt_avg=\"%.2f\" " ! "/>\n", result->stats.rtt_min, result->stats.rtt_max, ! result->stats.rtt_avg); ! fprintf(my_fp, "\t</host>\n"); ! fflush(my_fp); ! result = result->next; ! } ! return PLUGIN_OK; } ! static void plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n XML output plugin\n"); ! mthis->output(" Usage: -oX,<filename>,[o/a]\n"); ! mthis->output(" [o/a]: either 'o' or 'a', for overwrite, or append\n"); ! mthis->output(" overwrite is default\n"); ! /*exit(-1);*/ } ! static void plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("standard XML output loaded::(%s)\n", ! my_file); } ! static int plugin_last_error(void) { ! return my_errno; } --- 56,374 ---- */ ! void * ! plugin_init(void *in_data) { ! assert(in_data != NULL); ! // useless reference, to avoid a warning ! if(rcsid) ; ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! err_quit("Plugin API must be 6 or higher, update sonar"); ! pthis.type = OUTPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.version = 6; /* set API version 6 */ ! pthis.plugin_char = 'X'; ! pthis.shortname = "xml"; ! pthis.longname = "XML Log File output plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = NULL; ! // this is not needed for I/O plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = &plugin_output; ! pthis.plugin_results = &plugin_results; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! my_file = NULL; ! my_fp = NULL; ! ! return &pthis; } ! static int ! plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! my_file = NULL; ! my_fp = NULL; ! return PLUGIN_UNLOADED; } ! static int ! plugin_open(void *c_data, int i_data) { ! char *file_opts; // pointer to additional opts after filename ! char *append_or_overwrite = NULL; // indicates append or overwrite ! // option ! int filename_len = i_data; // set length assuming no further options ! pthis.selected = true; // we were selected, whether the right ! // arguments are present or not ! if(!c_data || i_data == 0) ! return PLUGIN_NOFILE; ! file_opts = strchr(c_data, ','); ! if(file_opts != NULL) ! { ! *file_opts++ = 0; // NULL delimit previous string, and advance ! // ! // if there was a comma, then don't include the comma and the ! // characters after it as part of the filename ! // ! filename_len = strlen(c_data); ! // ! // if an 'a' is found, then set the appropriate char option for fopen ! // other options may be filtered out with further if statements ! // ! if(*file_opts == 'a') ! append_or_overwrite = strdup("a"); ! else ! append_or_overwrite = strdup("w+"); ! } ! else ! append_or_overwrite = strdup("w+"); ! ! // check to see if my_file points to prev allocated data? ! my_file = strndup(c_data, filename_len); #ifdef DEBUG ! printf("fopen(\"%s\", \"%s\");\n", my_file, append_or_overwrite); #endif ! my_fp = fopen(my_file, append_or_overwrite); ! free(append_or_overwrite); ! if(my_fp == NULL) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("Could not open file"); ! pthis.ready = false; ! return PLUGIN_NOFILE; ! } ! pthis.ready = true; ! return PLUGIN_OK; } ! static int ! plugin_close(void) { ! fprintf(my_fp, "</sonar-scan>\n"); ! fclose(my_fp); ! free(my_file); ! return PLUGIN_OK; } ! static int ! plugin_output(char *msg, va_list * ap) { ! struct tm *tm; ! char buf[60]; ! time_t now; ! int tries = 0; ! ! /* sanity checks */ ! if(!pthis.ready) ! { ! *(pthis.err) = PLUGIN_NOPEN; ! err_msg("output plugin not ready"); ! return PLUGIN_NOPEN; ! } ! while(!my_fp && tries < 3) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("output plugin not ready, retrying"); ! my_fp = fopen(my_file, "a"); /* reopen it instead of dying */ ! /* return PLUGIN_NOFILE; */ ! tries++; ! } ! if(!my_fp) ! return PLUGIN_NOFILE; /* we tried */ ! if(!header) ! { ! now = time(NULL); ! if(!(tm = localtime(&now))) ! err_sys("could not get localtime\n"); ! if(strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M %Z", tm) <= 0) ! err_sys("Could not format localtime\n"); ! fprintf(my_fp, "<?xml version=\"1.0\" ?>\n"); ! fprintf(my_fp, "<!-- %s %s initiated at %s as %s -->\n", PACKAGE, ! VERSION, buf, mthis->opts.sCommandLine); ! fprintf(my_fp, "<sonar-scan>\n"); ! header++; ! } ! fprintf(my_fp, "<!-- "); ! vfprintf(my_fp, msg, *ap); ! fprintf(my_fp, " -->\n"); ! return PLUGIN_OK; } ! static int ! null_result(target_t * result) { ! /* as of right now, there is no way to report dead hosts */ ! fprintf(my_fp, ! "\t<deadhost hostname=\"unreported-in-this-version\" />\n"); ! return PLUGIN_OK; } ! static int ! plugin_results(target_t * result, unsigned int num_hosts) { ! struct sockaddr_in *sa = NULL; ! struct sockaddr_in6 *sa6 = NULL; ! char ip6sa_format[] = "%x%x%x%x:%x%x%x%x:%x%x%x%x:%x%x%x%x\0"; ! char buf[256]; ! /* struct sockaddr_un *un; */ ! ! int i, j; ! ! for(j = 0; j < num_hosts; j++) ! { ! assert(result != NULL); ! if(result->addrinfo == NULL) ! { ! null_result(result); ! continue; ! } ! ! memset(buf, 0, 256); ! fprintf(my_fp, ! "\t<host hostname=\"%s\" rtt=\"%0.2f\"" ! "ttl=\"%d\" mask=\"%d\"", ! result->addrinfo->ai_canonname, ! result->stats.rtt_avg, result->stats.ttl, result->netmask); ! switch (result->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in *) result->addrinfo->ai_addr; ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! inet_ntoa(sa->sin_addr), "ipv4"); ! break; ! case PF_INET6: ! sa6 = (struct sockaddr_in6 *) result->addrinfo->ai_addr; ! snprintf(buf, 255, ip6sa_format, ! sa6->sin6_addr.s6_addr[0], ! sa6->sin6_addr.s6_addr[1], ! sa6->sin6_addr.s6_addr[2], ! sa6->sin6_addr.s6_addr[3], ! sa6->sin6_addr.s6_addr[4], ! sa6->sin6_addr.s6_addr[5], ! sa6->sin6_addr.s6_addr[6], ! sa6->sin6_addr.s6_addr[7], ! sa6->sin6_addr.s6_addr[8], ! sa6->sin6_addr.s6_addr[9], ! sa6->sin6_addr.s6_addr[10], ! sa6->sin6_addr.s6_addr[11], ! sa6->sin6_addr.s6_addr[12], ! sa6->sin6_addr.s6_addr[13], ! sa6->sin6_addr.s6_addr[14], sa6->sin6_addr.s6_addr[15]); ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", buf, "ipv6"); ! break; ! /* case PF_UNIX: un = (struct sockaddr_un*) ! * result->ainfo->ai_addr; fprintf(my_fp, " address=\"%s\" ! * addr-type=\"%s\">\n", un->sun_path, "unix"); break; */ ! default: ! fprintf(my_fp, ! " address=\"%s\" addr-type=\"%s\">\n", ! "unknown-type", "unknown"); ! }; ! ! /* print the port info */ ! for(i = 0; i < 65536; i++) ! { ! switch (result->ports[i]) ! { ! case PORT_OPENED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " "state=\"open\"/>\n", i); ! break; ! case PORT_CLOSED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " "state=\"closed\"/>\n", i); ! break; ! case PORT_FILTERED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"filtered\"/>\n", i); ! break; ! case PORT_UNFILTERED: ! fprintf(my_fp, ! "\t\t<port number=\"%d\" " ! "state=\"unfiltered\"/>\n", i); ! break; ! } ! } ! fprintf(my_fp, ! "\t\t<stats sent=\"%d\" recvd=\"%d\" dropped=\"%d\" ", ! result->stats.packets_sent, result->stats.packets_recvd, ! result->stats.packets_sent - result->stats.packets_recvd); ! fprintf(my_fp, ! " rtt_min=\"%.2f\" rtt_max=\"%.2f\" rtt_avg=\"%.2f\" " ! "/>\n", result->stats.rtt_min, result->stats.rtt_max, ! result->stats.rtt_avg); ! fprintf(my_fp, "\t</host>\n"); ! fflush(my_fp); ! result = result->next; ! } ! return PLUGIN_OK; } ! static void ! plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n XML output plugin\n"); ! mthis->output(" Usage: -oX,<filename>,[o/a]\n"); ! mthis->output(" [o/a]: either 'o' or 'a', for overwrite, or append\n"); ! mthis->output(" overwrite is default\n"); ! /* exit(-1); */ } ! static void ! plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("standard XML output loaded::(%s)\n", my_file); } ! static int ! plugin_last_error(void) { ! return my_errno; } Index: log_file.h =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/log_file.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** log_file.h 4 Nov 2003 23:47:17 -0000 1.5 --- log_file.h 13 Apr 2004 03:42:07 -0000 1.6 *************** *** 45,47 **** ! #endif //_BARE_H_ --- 45,47 ---- ! #endif // _BARE_H_ Index: network_icmp.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/network_icmp.c,v retrieving revision 1.44 retrieving revision 1.45 diff -C2 -d -r1.44 -r1.45 *** network_icmp.c 8 Apr 2004 21:09:07 -0000 1.44 --- network_icmp.c 13 Apr 2004 03:42:07 -0000 1.45 *************** *** 32,36 **** #endif ! static const char rcsid[] = "\100$ autosec/sonar: $Id$"; static unsigned char done = 0; --- 32,37 ---- #endif ! static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; [...2118 lines suppressed...] ! case SIGSEGV: ! err_msg("oops! ACK scan seg faulted.\n"); ! abort(); ! break; ! } ! /* just close the thread and be done with it */ ! pthread_exit(NULL); } /* out = out - in */ ! static void ! tvsub(struct timeval *out, struct timeval *in) { ! if((out->tv_usec -= in->tv_usec) < 0) ! { ! out->tv_sec--; ! out->tv_usec += 1000000; ! } ! out->tv_sec -= in->tv_sec; } Index: rfc793.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/rfc793.c,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** rfc793.c 12 Apr 2004 23:46:31 -0000 1.47 --- rfc793.c 13 Apr 2004 03:42:07 -0000 1.48 *************** *** 32,36 **** static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; static int my_socket, err_socket; --- 32,36 ---- static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; [...1494 lines suppressed...] ! return "host precedence violation"; ! case ICMP_UNREACH_PRECEDENCE_CUTOFF: ! return "precedence cutoff"; } return "unknown"; } ! static RETSIGTYPE ! sig_handler(int i) { ! switch (i) { ! case SIGSEGV: err_msg("oops! ACK scan seg faulted.\n"); abort(); break; ! } ! /* just close the thread and be done with it */ pthread_exit(NULL); } Index: log_file.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/log_file.c,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** log_file.c 8 Apr 2004 23:28:17 -0000 1.26 --- log_file.c 13 Apr 2004 03:42:07 -0000 1.27 *************** *** 40,44 **** static int plugin_close(void); static int plugin_output(char *msg, va_list * ap); ! static int plugin_results(target_t *result, unsigned int num); static void plugin_usage(void); static void plugin_status(void); --- 40,44 ---- static int plugin_close(void); static int plugin_output(char *msg, va_list * ap); ! static int plugin_results(target_t * result, unsigned int num); static void plugin_usage(void); static void plugin_status(void); *************** *** 46,50 **** static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; /** @fn plugin_init(void *in_data) --- 46,50 ---- static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; /** @fn plugin_init(void *in_data) *************** *** 55,355 **** */ ! void *plugin_init(void *in_data) { ! { ! assert(in_data != NULL); ! } ! //useless reference, to avoid a warning ! if(rcsid); ! ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! err_quit("Plugin API must be 6 or higher, update sonar"); ! pthis.type = OUTPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.version = 6; /* set API version 6 */ ! pthis.selected = false; ! pthis.plugin_char = 'L'; ! pthis.shortname = "log_file"; ! pthis.longname = "Log File output plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = NULL; ! // this is not needed for I/O plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = &plugin_output; ! pthis.plugin_results = &plugin_results; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! my_file = NULL; ! my_fp = NULL; ! return &pthis; } ! static int plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! my_file = NULL; ! my_fp = NULL; ! return PLUGIN_UNLOADED; } ! static int plugin_open(void *c_data, int i_data) { ! char *file_opts; // pointer to additional opts after filename ! char *append_or_overwrite = NULL; // indicates append or overwrite option ! int filename_len = i_data; // set length assuming no further options - - pthis.selected = true; // we were selected, whether the right arguments are present or not ! if (!c_data || i_data == 0) ! return PLUGIN_NOFILE; ! file_opts = strchr(c_data, ','); ! if (file_opts != NULL) ! { ! *file_opts++ = 0; // NULL delimit previous string, and advance ! // ! // if there was a comma, then don't include the comma and the ! // characters after it as part of the filename ! // ! filename_len = strlen(c_data); ! // ! // if an 'a' is found, then set the appropriate char option for fopen ! // other options may be filtered out with further if statements ! // ! if (*file_opts == 'a') ! append_or_overwrite = strdup("a"); ! else ! append_or_overwrite = strdup("w+"); ! } ! else ! append_or_overwrite = strdup("w+"); ! // check to see if my_file points to prev allocated data? ! my_file = strndup(c_data, filename_len); #ifdef DEBUG ! printf("fopen(\"%s\", \"%s\");\n", my_file, append_or_overwrite); #endif ! my_fp = fopen(my_file, append_or_overwrite); ! free(append_or_overwrite); ! if(!my_fp) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("Could not open file"); ! pthis.ready = false; ! return PLUGIN_NOFILE; ! } ! pthis.ready = true; ! return PLUGIN_OK; } ! static int plugin_close(void) { ! fclose(my_fp); ! free(my_file); ! return PLUGIN_OK; } ! static int plugin_output(char *msg, va_list * ap) { ! struct tm *gmt; ! time_t ltime; ! char *mtime; ! int tries = 0; ! //sanity checks ! if(!pthis.ready) ! { ! *(pthis.err) = PLUGIN_NOPEN; ! err_msg("output plugin not ready"); ! return PLUGIN_NOPEN; ! } ! while(!my_fp && tries < 3) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("output plugin not ready, retrying"); ! my_fp = fopen(my_file, "a"); /* reopen it instead of dying */ ! /*return PLUGIN_NOFILE;*/ ! tries++; ! } ! if(!my_fp) ! return PLUGIN_NOFILE; /* we tried */ ! time(<ime); ! gmt = localtime(<ime); ! mtime = asctime(gmt); ! mtime[strlen(mtime) - 1] = '\0'; ! fprintf(my_fp, "[%s] ", mtime); ! ! vfprintf(my_fp, msg, *ap); ! fflush(my_fp); ! return PLUGIN_OK; } ! static int null_result(target_t *result) { ! /* there is no way to report the hostname if we didn't get any responses :( */ ! fprintf(my_fp, "Got no responses\n"); ! ! return PLUGIN_OK; } ! static int plugin_results(target_t *result, unsigned int num) { ! struct sockaddr_in *sa = NULL; ! struct sockaddr_in6 *sa6 = NULL; ! char ip6sa_format[] = "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x\0"; ! char buf[256]; ! int i, j; ! ! for(j = 0; j < num; j++) ! { ! assert(result != NULL); ! if(result->addrinfo == NULL) ! { ! null_result(result); ! continue; ! } ! ! memset(buf, 0, 256); ! fprintf(my_fp, "Host %s[", result->addrinfo->ai_canonname); ! switch(result->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in*) ! result->addrinfo->ai_addr; ! fprintf(my_fp, "%s] results:", ! inet_ntoa(sa->sin_addr)); ! break; ! case PF_INET6: ! sa6 = (struct sockaddr_in6 *) ! result->addrinfo->ai_addr; ! snprintf(buf, 255, ip6sa_format, ! sa6->sin6_addr.s6_addr[0], ! sa6->sin6_addr.s6_addr[1], ! sa6->sin6_addr.s6_addr[2], ! sa6->sin6_addr.s6_addr[3], ! sa6->sin6_addr.s6_addr[4], ! sa6->sin6_addr.s6_addr[5], ! sa6->sin6_addr.s6_addr[6], ! sa6->sin6_addr.s6_addr[7], ! sa6->sin6_addr.s6_addr[8], ! sa6->sin6_addr.s6_addr[9], ! sa6->sin6_addr.s6_addr[10], ! sa6->sin6_addr.s6_addr[11], ! sa6->sin6_addr.s6_addr[12], ! sa6->sin6_addr.s6_addr[13], ! sa6->sin6_addr.s6_addr[14], ! sa6->sin6_addr.s6_addr[15]); ! fprintf(my_fp, "%s] results:", buf); ! break; ! default: ! fprintf(my_fp, "unknown] results:"); ! }; ! fprintf(my_fp, " rtt=%0.2f ttl=%d mask=%d\n", ! result->stats.rtt_min, result->stats.ttl, ! result->netmask); ! /* print the port info */ ! for(i =0; i < 65536; i++) ! { ! switch(result->ports[i]) ! { ! case PORT_OPENED: ! fprintf(my_fp, ! " port %d state: " ! "open range: %0.2f ttl: %u\n", ! i, result->stats.rtt_avg, ! result->stats.ttl); ! break; ! case PORT_CLOSED: ! fprintf(my_fp, ! " port %d state: closed " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, ! result->stats.ttl); ! break; ! case PORT_FILTERED: ! fprintf(my_fp, ! " port %d state: filtered " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, ! result->stats.ttl); ! break; ! case PORT_UNFILTERED: ! fprintf(my_fp, ! " port %d state: unfiltered " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, ! result->stats.ttl); ! break; ! } ! } ! fprintf(my_fp, " stats: sent: %d, recvd: %d, dropped: %d\n", ! result->stats.packets_sent, ! result->stats.packets_recvd, ! result->stats.packets_sent - ! result->stats.packets_recvd); ! fprintf(my_fp, ! " rtt (min/max/avg): %.2f/%.2f/%.2f ms\n", ! result->stats.rtt_min, result->stats.rtt_max, ! result->stats.rtt_avg); ! result = result->next; ! } ! return PLUGIN_OK; } ! static void plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n Logfile output plugin\n"); ! mthis->output(" Usage: -oL,<filename>,[o/a]\n"); ! mthis->output(" [o/a]: either 'o' or 'a', for overwrite, or append\n"); ! mthis->output(" overwrite is default\n"); ! /*exit(-1);*/ } ! static void plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("standard logfile output loaded::(%s)\n", ! my_file); } ! static int plugin_last_error(void) { ! return my_errno; } --- 55,357 ---- */ ! void * ! plugin_init(void *in_data) { ! { ! assert(in_data != NULL); ! } ! // useless reference, to avoid a warning ! if(rcsid) ; ! // this is assumed ! mthis = (sonar_t *) in_data; ! if(mthis->api < 6) ! err_quit("Plugin API must be 6 or higher, update sonar"); ! pthis.type = OUTPUT_PLUGIN; ! pthis.err = &my_errno; ! pthis.ready = false; ! pthis.version = 6; /* set API version 6 */ ! pthis.selected = false; ! pthis.plugin_char = 'L'; ! pthis.shortname = "log_file"; ! pthis.longname = "Log File output plugin"; ! pthis.plugin_init = &plugin_init; ! pthis.plugin_destroy = &plugin_destroy; ! pthis.plugin_open = &plugin_open; ! pthis.plugin_close = &plugin_close; ! pthis.plugin_next = NULL; ! // this is not needed for I/O plugins ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = &plugin_output; ! pthis.plugin_results = &plugin_results; ! pthis.plugin_usage = &plugin_usage; ! pthis.plugin_status = &plugin_status; ! pthis.plugin_last_error = &plugin_last_error; ! my_errno = PLUGIN_LOADED; ! my_file = NULL; ! my_fp = NULL; ! ! return &pthis; } ! static int ! plugin_destroy(void) { ! mthis = NULL; ! pthis.type = 0; ! pthis.err = NULL; ! pthis.ready = false; ! pthis.plugin_init = NULL; ! pthis.plugin_destroy = NULL; ! pthis.plugin_open = NULL; ! pthis.plugin_close = NULL; ! pthis.plugin_next = NULL; ! pthis.plugin_sweep = NULL; ! pthis.plugin_output = NULL; ! pthis.plugin_results = NULL; ! pthis.plugin_usage = NULL; ! pthis.plugin_status = NULL; ! pthis.plugin_last_error = NULL; ! my_errno = PLUGIN_UNLOADED; ! my_file = NULL; ! my_fp = NULL; ! return PLUGIN_UNLOADED; } ! static int ! plugin_open(void *c_data, int i_data) { ! char *file_opts; // pointer to additional opts after filename ! char *append_or_overwrite = NULL; // indicates append or overwrite ! // option ! int filename_len = i_data; // set length assuming no further options ! pthis.selected = true; // we were selected, whether the right ! // arguments are present or not ! if(!c_data || i_data == 0) ! return PLUGIN_NOFILE; ! file_opts = strchr(c_data, ','); ! if(file_opts != NULL) ! { ! *file_opts++ = 0; // NULL delimit previous string, and advance ! // ! // if there was a comma, then don't include the comma and the ! // characters after it as part of the filename ! // ! filename_len = strlen(c_data); ! // ! // if an 'a' is found, then set the appropriate char option for fopen ! // other options may be filtered out with further if statements ! // ! if(*file_opts == 'a') ! append_or_overwrite = strdup("a"); ! else ! append_or_overwrite = strdup("w+"); ! } ! else ! append_or_overwrite = strdup("w+"); ! ! // check to see if my_file points to prev allocated data? ! my_file = strndup(c_data, filename_len); #ifdef DEBUG ! printf("fopen(\"%s\", \"%s\");\n", my_file, append_or_overwrite); #endif ! my_fp = fopen(my_file, append_or_overwrite); ! free(append_or_overwrite); ! if(!my_fp) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("Could not open file"); ! pthis.ready = false; ! return PLUGIN_NOFILE; ! } ! pthis.ready = true; ! return PLUGIN_OK; } ! static int ! plugin_close(void) { ! fclose(my_fp); ! free(my_file); ! return PLUGIN_OK; } ! static int ! plugin_output(char *msg, va_list * ap) { ! struct tm *gmt; ! time_t ltime; ! char *mtime; ! int tries = 0; ! // sanity checks ! if(!pthis.ready) ! { ! *(pthis.err) = PLUGIN_NOPEN; ! err_msg("output plugin not ready"); ! return PLUGIN_NOPEN; ! } ! while(!my_fp && tries < 3) ! { ! *(pthis.err) = PLUGIN_NOFILE; ! err_msg("output plugin not ready, retrying"); ! my_fp = fopen(my_file, "a"); /* reopen it instead of dying */ ! /* return PLUGIN_NOFILE; */ ! tries++; ! } ! if(!my_fp) ! return PLUGIN_NOFILE; /* we tried */ ! time(<ime); ! gmt = localtime(<ime); ! mtime = asctime(gmt); ! mtime[strlen(mtime) - 1] = '\0'; ! fprintf(my_fp, "[%s] ", mtime); ! ! vfprintf(my_fp, msg, *ap); ! fflush(my_fp); ! return PLUGIN_OK; } ! static int ! null_result(target_t * result) { ! /* there is no way to report the hostname if we didn't get any responses ! * :( */ ! fprintf(my_fp, "Got no responses\n"); ! ! return PLUGIN_OK; } ! static int ! plugin_results(target_t * result, unsigned int num) { ! struct sockaddr_in *sa = NULL; ! struct sockaddr_in6 *sa6 = NULL; ! char ip6sa_format[] = "%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x\0"; ! char buf[256]; ! int i, j; ! ! for(j = 0; j < num; j++) ! { ! assert(result != NULL); ! if(result->addrinfo == NULL) ! { ! null_result(result); ! continue; ! } ! ! memset(buf, 0, 256); ! fprintf(my_fp, "Host %s[", result->addrinfo->ai_canonname); ! switch (result->addrinfo->ai_family) ! { ! case PF_INET: ! sa = (struct sockaddr_in *) result->addrinfo->ai_addr; ! fprintf(my_fp, "%s] results:", inet_ntoa(sa->sin_addr)); ! break; ! case PF_INET6: ! sa6 = (struct sockaddr_in6 *) result->addrinfo->ai_addr; ! snprintf(buf, 255, ip6sa_format, ! sa6->sin6_addr.s6_addr[0], ! sa6->sin6_addr.s6_addr[1], ! sa6->sin6_addr.s6_addr[2], ! sa6->sin6_addr.s6_addr[3], ! sa6->sin6_addr.s6_addr[4], ! sa6->sin6_addr.s6_addr[5], ! sa6->sin6_addr.s6_addr[6], ! sa6->sin6_addr.s6_addr[7], ! sa6->sin6_addr.s6_addr[8], ! sa6->sin6_addr.s6_addr[9], ! sa6->sin6_addr.s6_addr[10], ! sa6->sin6_addr.s6_addr[11], ! sa6->sin6_addr.s6_addr[12], ! sa6->sin6_addr.s6_addr[13], ! sa6->sin6_addr.s6_addr[14], sa6->sin6_addr.s6_addr[15]); ! fprintf(my_fp, "%s] results:", buf); ! break; ! default: ! fprintf(my_fp, "unknown] results:"); ! }; ! fprintf(my_fp, " rtt=%0.2f ttl=%d mask=%d\n", ! result->stats.rtt_min, result->stats.ttl, result->netmask); ! /* print the port info */ ! for(i = 0; i < 65536; i++) ! { ! switch (result->ports[i]) ! { ! case PORT_OPENED: ! fprintf(my_fp, ! " port %d state: " ! "open range: %0.2f ttl: %u\n", ! i, result->stats.rtt_avg, result->stats.ttl); ! break; ! case PORT_CLOSED: ! fprintf(my_fp, ! " port %d state: closed " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, result->stats.ttl); ! break; ! case PORT_FILTERED: ! fprintf(my_fp, ! " port %d state: filtered " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, result->stats.ttl); ! break; ! case PORT_UNFILTERED: ! fprintf(my_fp, ! " port %d state: unfiltered " ! "range: %0.2f ttl: %u\n", i, ! result->stats.rtt_avg, result->stats.ttl); ! break; ! } ! } ! fprintf(my_fp, " stats: sent: %d, recvd: %d, dropped: %d\n", ! result->stats.packets_sent, ! result->stats.packets_recvd, ! result->stats.packets_sent - result->stats.packets_recvd); ! fprintf(my_fp, ! " rtt (min/max/avg): %.2f/%.2f/%.2f ms\n", ! result->stats.rtt_min, result->stats.rtt_max, ! result->stats.rtt_avg); ! result = result->next; ! } ! return PLUGIN_OK; } ! static void ! plugin_usage(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! mthis->output("\n Logfile output plugin\n"); ! mthis->output(" Usage: -oL,<filename>,[o/a]\n"); ! mthis->output(" [o/a]: either 'o' or 'a', for overwrite, or append\n"); ! mthis->output(" overwrite is default\n"); ! /* exit(-1); */ } ! static void ! plugin_status(void) { ! assert(mthis != NULL); ! assert(mthis->output != NULL); ! if(pthis.ready && pthis.selected) ! mthis->output("standard logfile output loaded::(%s)\n", my_file); } ! static int ! plugin_last_error(void) { ! return my_errno; } Index: error.c =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/error.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** error.c 28 Feb 2004 08:00:55 -0000 1.3 --- error.c 13 Apr 2004 03:42:07 -0000 1.4 *************** *** 45,49 **** static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; //extern int daemon_proc; /* set nonzero by daemon_init() */ --- 45,49 ---- static const char rcsid[] = ! "\100$ autosec/sonar: $Id$"; //extern int daemon_proc; /* set nonzero by daemon_init() */ *************** *** 56,67 **** * Print a message and return. */ ! void err_ret(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_INFO, fmt, ap); ! va_end(ap); ! return; } --- 56,68 ---- * Print a message and return. */ ! void ! err_ret(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_INFO, fmt, ap); ! va_end(ap); ! return; } *************** *** 69,80 **** * Print a message and terminate. */ ! void err_sys(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_ERR, fmt, ap); ! va_end(ap); ! exit(1); } --- 70,82 ---- * Print a message and terminate. */ ! void ! err_sys(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_ERR, fmt, ap); ! va_end(ap); ! exit(1); } *************** *** 82,94 **** * Print a message, dump core, and terminate. */ ! void err_dump(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_ERR, fmt, ap); ! va_end(ap); ! abort(); /* dump core and terminate */ ! exit(1); /* shouldn't get here */ } --- 84,97 ---- * Print a message, dump core, and terminate. */ ! void ! err_dump(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(1, LOG_ERR, fmt, ap); ! va_end(ap); ! abort(); /* dump core and terminate */ ! exit(1); /* shouldn't get here */ } *************** *** 96,107 **** * Print a message and return. */ ! void err_msg(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(0, LOG_INFO, fmt, ap); ! va_end(ap); ! return; } --- 99,111 ---- * Print a message and return. */ ! void ! err_msg(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(0, LOG_INFO, fmt, ap); ! va_end(ap); ! return; } *************** *** 109,120 **** * Print a message and terminate. */ ! void err_quit(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(0, LOG_ERR, fmt, ap); ! va_end(ap); ! exit(1); } --- 113,125 ---- * Print a message and terminate. */ ! void ! err_quit(const char *fmt, ...) { ! va_list ap; ! va_start(ap, fmt); ! err_doit(0, LOG_ERR, fmt, ap); ! va_end(ap); ! exit(1); } *************** *** 122,154 **** * Caller specifies "errnoflag" and "level". */ ! static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { ! int errno_save, n; ! char buf[MAXLINE]; ! errno_save = errno; /* value caller might want printed */ ! ! #ifdef HAVE_VSNPRINTF ! vsnprintf(buf, sizeof(buf), fmt, ap); /* this is safe */ ! #else ! vsprintf(buf, fmt, ap); /* this is not safe */ ! #endif ! ! n = strlen(buf); ! if(errnoflag) ! snprintf(buf + n, sizeof(buf) - n, ": %s", ! strerror(errno_save)); ! strcat(buf, "\n"); ! if(mthis.sCon.bFork) ! { ! syslog(level, buf); ! } ! else ! { ! fflush(stdout); /* in case stdout and stderr are the same */ ! fputs(buf, stderr); ! fflush(stderr); ! } ! return; } --- 127,159 ---- * Caller specifies "errnoflag" and "level". */ ! static void ! err_doit(int errnoflag, int level, const char *fmt, va_list ap) { ! int errno_save, n; ! char buf[MAXLINE]; ! errno_save = errno; /* value caller might want printed */ ! #ifdef HAVE_VSNPRINTF ! vsnprintf(buf, sizeof(buf), fmt, ap); /* this is safe */ ! #else ! vsprintf(buf, fmt, ap); /* this is not safe */ ! #endif ! ! n = strlen(buf); ! if(errnoflag) ! snprintf(buf + n, sizeof(buf) - n, ": %s", strerror(errno_save)); ! strcat... [truncated message content] |