File: write-report.c

package info (click to toggle)
traffic-vis 0.34-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 356 kB
  • ctags: 436
  • sloc: ansic: 4,029; makefile: 144; perl: 82; sh: 25
file content (135 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
	**
	** write-report.c
	**
	** Routines to write a report in the same format as those generated 
	** by traffic-collect 
	**
	** Copyright 1998-1999 Damien Miller <dmiller@ilogic.com.au>
	**
	** This software is licensed under the terms of the GNU General 
	** Public License (GPL). Please see the file COPYING for details.
	** 
	** $Id: write-report.c,v 1.2 1999/02/04 10:22:42 dmiller Exp $
	**
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "report.h"
#include "write-report.h"

#include "util.h"

static char rcsid[] = "$Id: write-report.c,v 1.2 1999/02/04 10:22:42 dmiller Exp $";

/* Prototypes */
int write_hosts(host_t *h, FILE *out);
int write_peers(peer_t *p, FILE *out);

int write_report(report_t *r, const char *report_file)
{
	time_t	now;
	FILE		*out;
	
	if (report_file == NULL)
	{
		out = stdout;
	} else
	{
		/* Open report file */
		out = fopen(report_file, "w");
		if (out == NULL)
		{
			fprintf(stderr, "Couldn't open report file '%s' for writing.\n", report_file);
			return(0);
		}
	}
		
	now = time(NULL);
	
	fprintf(out, "# Report created at: %s", ctime(&(r->summary_finish)));
	fprintf(out, "# Report last processed at: %s\n", ctime(&now));
	
	fprintf(out, "START_TIME\t%lu\n", r->summary_start);
	fprintf(out, "FINISH_TIME\t%lu\n", r->summary_finish);
	fprintf(out, "TOTAL_HOSTS\t%u\n", r->active_hosts);
	fprintf(out, "TOTAL_BYTES\t%llu\n", r->total_bytes);
	fprintf(out, "TOTAL_PACKETS\t%llu\n", r->total_packets);
	fprintf(out, "TOTAL_CONREQS\t%llu\n", r->total_connections);
	fprintf(out, "\n");

	if (!write_hosts(r->hosts, out))
		return(0);

	if (report_file != NULL)
		fclose(out);

	return(1);	
}

int write_hosts(host_t *h, FILE *out)
{
	struct in_addr i;

	while (h != NULL)
	{
		i.s_addr = h->ip_addr;
		fprintf(out, "NEW_HOST\n");
		fprintf(out, "\tHOST_ADDR\t%s\n", inet_ntoa(i));
		fprintf(out, "\tHOST_BYTES_SENT\t%llu\n", h->bytes_sent);
		fprintf(out, "\tHOST_BYTES_RECEIVED\t%llu\n", h->bytes_received);
		if (h->hostname != NULL)
			fprintf(out, "\tHOST_NAME\t%s\n", h->hostname);
		fprintf(out, "\tHOST_PACKETS_SENT\t%llu\n", h->packets_sent);
		fprintf(out, "\tHOST_PACKETS_RECEIVED\t%llu\n", h->packets_received);
		fprintf(out, "\tHOST_CONREQS_SENT\t%llu\n", h->connections_sent);
		fprintf(out, "\tHOST_CONREQS_RECEIVED\t%llu\n", h->connections_received);
		fprintf(out, "\tHOST_FIRST_TRAFFIC_TIME\t%lu\n", h->first_seen);
		fprintf(out, "\tHOST_LAST_TRAFFIC_TIME\t%lu\n", h->last_seen);

		if (!write_peers(h->peers, out))
			return(0);

		fprintf(out, "END_HOST\n");
		fprintf(out, "\n");
		
		h = h->next;
	}
	
	return(1);	
}

int write_peers(peer_t *p, FILE *out)
{
	struct 			in_addr i;
	char				host1_addr[20];
	char				host2_addr[20];

	while (p != NULL)
	{
		i.s_addr = p->src_addr;
		snprintf(host1_addr, sizeof(host1_addr), "%s", inet_ntoa(i));
		i.s_addr = p->dst_addr;
		snprintf(host2_addr, sizeof(host2_addr), "%s", inet_ntoa(i));
		
		fprintf(out, "\tNEW_PEER\n");
		fprintf(out, "\t\tPEER_SRC_ADDR\t%s\n", host1_addr);
		fprintf(out, "\t\tPEER_DST_ADDR\t%s\n", host2_addr);
		fprintf(out, "\t\tPEER_BYTES\t%llu\n", p->bytes_sent);
		fprintf(out, "\t\tPEER_PACKETS\t%llu\n", p->packets_sent);
		fprintf(out, "\t\tPEER_CONREQS\t%llu\n", p->connections_sent);
		fprintf(out, "\tEND_PEER\n");
		
		p = p->next;
	}
	
	return(1);
}