2
* Copyright (C) 1998-2004 Luca Deri <deri@ntop.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
/* ****************************************** */
26
char* formatKBytes(float numKBytes, char *outStr, int outStrLen) {
27
if(numKBytes < 0) return(""); /* It shouldn't happen */
29
if(numKBytes < 1024) {
30
if(snprintf(outStr, outStrLen, "%.1f%sKB", numKBytes, myGlobals.separator) < 0)
33
float tmpKBytes = numKBytes/1024;
35
if(tmpKBytes < 1024) {
36
if(snprintf(outStr, outStrLen, "%.1f%sMB", tmpKBytes, myGlobals.separator) < 0)
39
float tmpGBytes = tmpKBytes/1024;
41
if(tmpGBytes < 1024) {
42
if(snprintf(outStr, outStrLen, "%.1f%sGB", tmpGBytes, myGlobals.separator) < 0)
45
if(snprintf(outStr, outStrLen, "%.1f%sTB", ((float)(tmpGBytes)/1024), myGlobals.separator) < 0)
54
/* ******************************* */
56
char* formatBytes(Counter numBytes, short encodeString, char* outStr, int outStrLen) {
60
locSeparator = myGlobals.separator;
65
return("0"); /* return(" "); */
66
} else if(numBytes < 1024) {
67
if(snprintf(outStr, outStrLen, "%lu", (unsigned long)numBytes) < 0)
69
} else if (numBytes < 1048576) {
70
if(snprintf(outStr, outStrLen, "%.1f%sKB",
71
((float)(numBytes)/1024), locSeparator) < 0)
74
float tmpMBytes = ((float)numBytes)/1048576;
76
if(tmpMBytes < 1024) {
77
if(snprintf(outStr, outStrLen, "%.1f%sMB",
78
tmpMBytes, locSeparator) < 0)
83
if(tmpMBytes < 1024) {
84
if(snprintf(outStr, outStrLen, "%.1f%sGB", tmpMBytes, locSeparator) < 0)
87
if(snprintf(outStr, outStrLen, "%.1f%sTB",
88
((float)(tmpMBytes)/1024), locSeparator) < 0)
97
/* ******************************* */
99
char* formatAdapterSpeed(Counter numBits, char* outStr, int outStrLen) {
101
return("0"); /* return(" "); */
102
} else if(numBits < 1000) {
103
if(snprintf(outStr, outStrLen, "%lu", (unsigned long)numBits) < 0)
105
} else if(numBits < 1000000) {
106
if(snprintf(outStr, outStrLen, "%.1f Kb", (float)(numBits)/1000) < 0)
109
float tmpMBytes = ((float)numBits)/1000000;
111
if(tmpMBytes < 1000) {
112
if(snprintf(outStr, outStrLen, "%.1f Mb", tmpMBytes) < 0)
117
if(tmpMBytes < 1000) {
118
if(snprintf(outStr, outStrLen, "%.1f Gb", tmpMBytes) < 0)
121
if(snprintf(outStr, outStrLen, "%.1f Tb", ((float)(tmpMBytes)/1000)) < 0)
130
/* ******************************* */
132
char* formatSeconds(unsigned long sec, char* outStr, int outStrLen) {
133
unsigned int hour=0, min=0, days=0;
150
if(min > 0) sec -= min*60;
153
if(snprintf(outStr, outStrLen, "%u day%s %u:%02u:%02lu", days, (days>1)?"s":"", hour, min, sec) < 0)
155
} else if(hour > 0) {
156
if(snprintf(outStr, outStrLen, "%u:%02u:%02lu", hour, min, sec) < 0)
159
if(snprintf(outStr, outStrLen, "%u:%02lu", min, sec) < 0)
162
if(snprintf(outStr, outStrLen, "%lu sec", sec) < 0)
169
/* ******************************* */
171
char* formatMicroSeconds(unsigned long microsec,
172
char* outStr, int outStrLen) {
173
float f = ((float)microsec)/1000;
176
if(snprintf(outStr, outStrLen, "%.1f ms", f) < 0)
179
if(snprintf(outStr, outStrLen, "%.1f sec", (f/1000)) < 0)
185
/* ******************************* */
187
char* formatThroughput(float numBytes /* <=== Bytes/second */, u_char htmlFormat,
188
char* outStr, int outStrLen) {
190
int divider = 1000; /* As SNMP does instead of using 1024 ntop divides for 1000 */
194
separator = myGlobals.separator;
198
if(numBytes < 0) numBytes = 0; /* Sanity check */
199
numBits = numBytes*8;
202
numBits = 0; /* Avoid very small decimal values */
204
if (numBits < divider) {
205
if(snprintf(outStr, outStrLen, "%.1f%sbps", numBits, separator) < 0)
207
} else if (numBits < (divider*divider)) {
208
if(snprintf(outStr, outStrLen, "%.1f%sKbps", ((float)(numBits)/divider), separator) < 0)
211
if(snprintf(outStr, outStrLen, "%.1f%sMbps", ((float)(numBits)/1048576), separator) < 0)
216
traceEvent(CONST_TRACE_INFO, "%.2f = %s", numBytes, outStr);
222
/* ******************************* */
224
char* formatLatency(struct timeval tv, u_short sessionState, char* outStr, int outStrLen) {
225
if(((tv.tv_sec == 0) && (tv.tv_usec == 0))
226
|| (sessionState < FLAG_STATE_ACTIVE)
228
Andreas Pfaller <apfaller@yahoo.com.au>
231
Latency not computed (the session was initiated
236
if(snprintf(outStr, outStrLen, "%.1f ms",
237
(float)(tv.tv_sec*1000+(float)tv.tv_usec/1000)) < 0)
243
/* ******************************* */
245
char* formatTimeStamp(unsigned int ndays,
247
unsigned int nminutes, char* outStr, int outStrLen) {
250
/* printf("%u - %u - %u\n", ndays, nhours, nminutes); */
257
theTime = myGlobals.actTime-(ndays*86400)-(nhours*3600)-(nminutes*60);
258
strncpy(outStr, ctime(&theTime), outStrLen);
259
outStr[outStrLen-1] = '\0'; /* Remove trailer '\n' */
264
/* ************************ */
266
char* formatPkts(Counter pktNr, char* outStr, int outStrLen) {
268
if(snprintf(outStr, outStrLen, "%lu", (unsigned long)pktNr) < 0)
270
} else if(pktNr < 1000000) {
271
if(snprintf(outStr, outStrLen, "%lu,%03lu",
272
(unsigned long)(pktNr/1000),
273
((unsigned long)pktNr)%1000) < 0)
275
} else if(pktNr < 1000000000) {
276
unsigned long a, b, c;
277
a = (unsigned long)(pktNr/1000000);
278
b = (unsigned long)((pktNr-a*1000000)/1000);
279
c = ((unsigned long)pktNr)%1000;
280
if(snprintf(outStr, outStrLen, "%lu,%03lu,%03lu", a, b, c) < 0)
283
unsigned long a, b, c, a1, a2;
284
a = (unsigned long)(pktNr/1000000);
285
a1 = (unsigned long)(a/1000);
287
b = (unsigned long)((pktNr-a*1000000)/1000);
288
c = ((unsigned long)pktNr)%1000;
289
if(snprintf(outStr, outStrLen, "%lu,%03lu,%03lu,%03lu", a1, a2, b, c) < 0)
296
/* ************************************ */
298
char* formatTime(time_t *theTime, char* outStr, int outStrLen) {
302
locTime = localtime_r(theTime, &myLocTime);
303
strftime(outStr, outStrLen, CONST_LOCALE_TIMESPEC, locTime);