/* Copyright (C) 2000-2006 Boris Wesslowski */ /* $Id: output.c,v 1.67 2006/03/08 19:36:03 bw Exp $ */ #include #include #include #include #include #include #include #include #include #include "output.h" #include "resolve.h" #include "utils.h" #include "whois.h" extern struct options opt; extern struct conn_data *first; void output_timediff(time_t start, time_t end, char *td) { time_t diff; int part; char tmp[4]; diff = end - start; if (diff <= 0) { snprintf(td, 2, "-"); return; } part = diff / 86400; /* days */ snprintf(td, TIMESIZE, "%02d:", part); diff = diff % 86400; part = diff / 3600; /* hours */ snprintf(tmp, 4, "%02d:", part); strncat(td, tmp, 4); diff = diff % 3600; part = diff / 60; /* minutes */ snprintf(tmp, 4, "%02d:", part); strncat(td, tmp, 4); part = diff % 60; /* seconds */ snprintf(tmp, 3, "%02d", part); strncat(td, tmp, 3); } void output_tcp_opts(struct conn_data *input, char *buf) { if ((input->flags & (TCP_ACK|TCP_FIN|TCP_RST|TCP_PSH|TCP_URG)) != 0) { if (input->flags & TCP_SYN) { strcpy(buf, "s"); } else { strcpy (buf, "-"); } if (input->flags & TCP_ACK) { strcat(buf, "a"); } else { strcat (buf, "-"); } if (input->flags & TCP_FIN) { strcat(buf, "f"); } else { strcat (buf, "-"); } if (input->flags & TCP_RST) { strcat(buf, "r"); } else { strcat (buf, "-"); } if (input->flags & TCP_PSH) { strcat(buf, "p"); } else { strcat (buf, "-"); } if (input->flags & TCP_URG) { strcat(buf, "u"); } else { strcat (buf, "-"); } } else { if (input->flags & TCP_SYN) { strcpy(buf, "SYN"); } else { strcpy(buf, "-"); } } } void output_html_entry(struct conn_data *input, FILE *fd) { char *proto = NULL, time[TIMESIZE], buf[HOSTLEN]; if (opt.html == 2) { fprintf(fd, "", opt.html); } else { fprintf(fd, "", opt.html); } fprintf(fd, "%d", input->count); if(opt.stimes) { strftime(time, TIMESIZE, _("%b %d %H:%M:%S"), localtime(&input->start_time)); fprintf(fd, "%s", time); } if(opt.etimes) { fprintf(fd, ""); if(input->end_time != 0) { strftime(time, TIMESIZE, _("%b %d %H:%M:%S"), localtime(&input->end_time)); fprintf(fd, "%s", time); } else { fprintf(fd, "-"); } } if(opt.duration) { output_timediff(input->start_time, input->end_time, time); fprintf(fd, "%s", time); } if(opt.loghost) fprintf(fd, "%s", input->hostname); if(opt.chains) fprintf(fd, "%s", input->chainlabel); if(opt.branches) fprintf(fd, "%s", input->branchname); if(opt.ifs) fprintf(fd, "%s", input->interface); if(opt.proto) { proto = resolve_protocol(input->protocol); fprintf(fd, "%s", proto); } if (opt.datalen) fprintf(fd, "%d", input->datalen); if (opt.src_ip) { fprintf(fd, "%s", inet_ntoa(input->shost)); if(opt.resolve) fprintf(fd, "%s", resolve_hostname(input->shost)); if(opt.whois_lookup) { struct whois_entry *we; we = whois(input->shost); if (we != NULL) { snprintf(buf, HOSTLEN, "%s %s AS%d %s", we->ip_route, we->ip_descr, we->as_number, we->as_descr); } else { snprintf(buf, HOSTLEN, "-"); } fprintf(fd, "%s", buf); } } if (opt.src_port) { fprintf(fd, "%d", input->sport); if (opt.sresolve) fprintf(fd, "%s", resolve_service(input->sport, proto)); } if (opt.dst_ip) { fprintf(fd, "%s", inet_ntoa(input->dhost)); if(opt.resolve) fprintf(fd, "%s", resolve_hostname(input->dhost)); } if (opt.dst_port) { fprintf(fd, "%d", input->dport); if (opt.sresolve) fprintf(fd, "%s", resolve_service(input->dport, proto)); } if(opt.opts) { output_tcp_opts(input, buf); fprintf(fd, "%s", buf); } fprintf(fd, "\n"); } void output_text_entry(struct conn_data *input, FILE *fd) { char *proto, time[TIMESIZE], buf[HOSTLEN]; unsigned char first = 1; if(opt.stimes) { strftime(time, TIMESIZE, _("%b %d %H:%M:%S"), localtime(&input->start_time)); fprintf(fd, "%s", time); first = 0; } if(opt.etimes) { if(!first) fprintf(fd, _(" to ")); if(input->end_time != 0) { strftime(time, TIMESIZE, _("%b %d %H:%M:%S"), localtime(&input->end_time)); fprintf(fd, "%s", time); } else { fprintf(fd, "-"); } first = 0; } if(opt.duration) { if(!first) fprintf(fd, " "); output_timediff(input->start_time, input->end_time, time); fprintf(fd, "%s", time); first = 0; } if(opt.loghost) { if(!first) fprintf(fd, " "); fprintf(fd, "%s", input->hostname); first = 0; } if(opt.chains) { if(!first) fprintf(fd, " "); fprintf(fd, "%s", input->chainlabel); first = 0; } if(opt.branches) { if(!first) fprintf(fd, " "); fprintf(fd, "%s", input->branchname); first = 0; } if(opt.ifs) { if(!first) fprintf(fd, " "); fprintf(fd, "%s", input->interface); first = 0; } if(!first) fprintf(fd, " "); fprintf(fd, "%d", input->count); proto = resolve_protocol(input->protocol); if(opt.proto) fprintf(fd, " %s", proto); if(input->count == 1) { fprintf(fd, _(" packet")); } else { fprintf(fd, _(" packets")); } if (opt.datalen) fprintf(fd, _(" (%d bytes)"), input->datalen); if (opt.src_ip) { fprintf(fd, _(" from %s"), inet_ntoa(input->shost)); if(opt.resolve) fprintf(fd, " (%s)", resolve_hostname(input->shost)); if(opt.whois_lookup) { struct whois_entry *we; we = whois(input->shost); if (we != NULL) { snprintf(buf, HOSTLEN, "%s %s AS%d %s", we->ip_route, we->ip_descr, we->as_number, we->as_descr); } else { snprintf(buf, HOSTLEN, "-"); } fprintf(fd, " [%s]", buf); } } if (opt.src_port) { fprintf(fd, _(" port %d"), input->sport); if (opt.sresolve) fprintf(fd, " (%s)", resolve_service(input->sport, proto)); } if (opt.dst_ip) { fprintf(fd, _(" to %s"), inet_ntoa(input->dhost)); if(opt.resolve) { fprintf(fd, " (%s)", resolve_hostname(input->dhost)); } } if (opt.dst_port) { fprintf(fd, _(" port %d"), input->dport); if (opt.sresolve) fprintf(fd, " (%s)", resolve_service(input->dport, proto)); } if(opt.opts) { output_tcp_opts(input, buf); fprintf(fd, " %s", buf); } fprintf(fd, "\n"); } void output_html_table(FILE *fd) { fprintf(fd, "

\n"); fprintf(fd, "\n"); fprintf(fd, ""); if(opt.stimes) fprintf(fd, _("")); if(opt.etimes) fprintf(fd, _("")); if(opt.duration) fprintf(fd, _("")); if(opt.loghost) fprintf(fd, _("")); if(opt.chains) fprintf(fd, _("")); if(opt.branches) fprintf(fd, _("")); if(opt.ifs) fprintf(fd, _("")); if(opt.proto) fprintf(fd, _("")); if(opt.datalen) fprintf(fd, _("")); if(opt.src_ip) { fprintf(fd, _("")); if(opt.resolve) fprintf(fd, _("")); if(opt.whois_lookup) fprintf(fd, _("")); } if (opt.src_port) { fprintf(fd, _("")); if (opt.sresolve) fprintf(fd, _("")); } if(opt.dst_ip) { fprintf(fd, _("")); if(opt.resolve) fprintf(fd, _("")); } if (opt.dst_port) { fprintf(fd, _("")); if (opt.sresolve) fprintf(fd, _("")); } if (opt.opts) fprintf(fd, _("")); fprintf(fd, "\n"); } void output_html_header(int fd) { char nows[TIMESIZE]; time_t now; fdprintf(fd, "\n"); now = time(NULL); strftime(nows, TIMESIZE, _("%b %d %H:%M:%S"), localtime(&now)); fdprintf(fd, "\n\n%s - %s\n", opt.title, nows); fdprintf(fd, "\n"); fdprintf(fd, "\n"); fdprintf(fd, "\n"); if((opt.mode == REALTIME_RESPONSE) && (opt.refresh > 0)) { fdprintf(fd, "\n", opt.refresh); } if (opt.stylesheet[0] != '\0') { if ((opt.mode != REALTIME_RESPONSE) || (strncmp(opt.stylesheet, "http", 4) == 0)) { fdprintf(fd, "\n", opt.stylesheet); } else { char buf[BUFSIZE]; FILE *cssfd; int retval; cssfd = fopen(opt.stylesheet, "r"); if (cssfd == NULL) { syslog(LOG_NOTICE, "fopen %s: %s", opt.stylesheet, strerror(errno)); } else { fdprintf(fd, "\n"); retval = fclose(cssfd); if (retval == EOF) syslog(LOG_NOTICE, "fclose %s: %s", opt.stylesheet, strerror(errno)); } } } else { fdprintf(fd, "\n"); } fdprintf(fd, "\n\n"); fdprintf(fd, "

%s

\n", opt.title); } void output_html_footer(int fd) { fdprintf(fd, "

%s %s © %s

\n", PACKAGE, VERSION, COPYRIGHT); fdprintf(fd, "\n\n"); } void output_raw_data(struct conn_data *input) { struct conn_data *this; this = first; while (this != NULL) { #ifndef __OpenBSD__ #ifndef __FreeBSD__ printf("%d;%ld;%ld;" "%s;%s;%s;" "%s;%d;" "%u;%d;" "%u;%d;" "%d\n", input->count, input->start_time, input->end_time, input->hostname, input->chainlabel, input->branchname, input->interface, input->protocol, ntohl(input->shost.s_addr), input->sport, ntohl(input->dhost.s_addr), input->dport, input->flags); #else printf("%d;%ld;%ld;" "%s;%s;%s;" "%s;%d;" "%ld;%d;" "%ld;%d;" "%d\n", input->count, input->start_time, input->end_time, input->hostname, input->chainlabel, input->branchname, input->interface, input->protocol, ntohl(input->shost.s_addr), input->sport, ntohl(input->dhost.s_addr), input->dport, input->flags); #endif #else printf("%d;%d;%d;" "%s;%s;%s;" "%s;%d;" "%u;%d;" "%u;%d;" "%d\n", input->count, input->start_time, input->end_time, input->hostname, input->chainlabel, input->branchname, input->interface, input->protocol, ntohl(input->shost.s_addr), input->sport, ntohl(input->dhost.s_addr), input->dport, input->flags); #endif this = this->next; } }
#startendintervalloghostchaintargetinterfaceprotobytessourcehostnamewhois informationportservicedestinationhostnameportserviceopts