This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdataFormat.c
297 lines (241 loc) · 8.77 KB
/
dataFormat.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* http://www.ntop.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
B * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ntop.h"
/* ****************************************** */
char* formatKBytes(float numKBytes, char *outStr, int outStrLen) {
if(numKBytes < 0) return(""); /* It shouldn't happen */
if(numKBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sKBytes", numKBytes, myGlobals.separator);
} else {
float tmpKBytes = numKBytes/1024;
if(tmpKBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sMBytes", tmpKBytes, myGlobals.separator);
} else {
float tmpGBytes = tmpKBytes/1024;
if(tmpGBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sGBytes", tmpGBytes, myGlobals.separator);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sTBytes", ((float)(tmpGBytes)/1024), myGlobals.separator);
}
}
}
return(outStr);
}
/* ******************************* */
char* formatBytes(Counter numBytes, short encodeString, char* outStr, int outStrLen) {
char* locSeparator;
if(encodeString)
locSeparator = myGlobals.separator;
else
locSeparator = " ";
if(numBytes == 0) {
return("0"); /* return(" "); */
} else if(numBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu", (unsigned long)numBytes);
} else if (numBytes < 1048576) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sKBytes", ((float)(numBytes)/1024), locSeparator);
} else {
float tmpMBytes = ((float)numBytes)/1048576;
if(tmpMBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sMBytes", tmpMBytes, locSeparator);
} else {
tmpMBytes /= 1024;
if(tmpMBytes < 1024) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sGBytes", tmpMBytes, locSeparator);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sTBytes", ((float)(tmpMBytes)/1024), locSeparator);
}
}
}
return(outStr);
}
/* ******************************* */
char* formatAdapterSpeed(Counter numBits, char* outStr, int outStrLen) {
if(numBits == 0) {
return("0"); /* return(" "); */
} else if(numBits < 1000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu", (unsigned long)numBits);
} else if(numBits < 1000000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f Kbit/s", (float)(numBits)/1000);
} else {
float tmpMBytes = ((float)numBits)/1000000;
if(tmpMBytes < 1000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f Mbit/s", tmpMBytes);
} else {
tmpMBytes /= 1000;
if(tmpMBytes < 1000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f Gbit/s", tmpMBytes);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f Tbit/s", ((float)(tmpMBytes)/1000));
}
}
}
return(outStr);
}
/* ******************************* */
char* formatSeconds(unsigned long sec, char* outStr, int outStrLen) {
u_int hour = 0, min = 0, days = 0;
if(sec >= 3600) {
hour = (u_int)(sec / 3600);
if(hour > 0) {
if(hour >= 24) {
days = (hour / 24);
hour = hour % 24;
sec -= days*86400;
}
sec -= hour*3600;
} else
hour = 0;
}
min = (u_int)(sec / 60);
if(min > 0) sec -= min*60;
if(days > 0) {
char yearStr[32];
if(days > 365) {
safe_snprintf(__FILE__, __LINE__, yearStr, sizeof(yearStr), "%d years, ", days/365);
days %= 365;
} else
yearStr[0] = '\0';
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%s%u day%s %u:%02u:%02lu",
yearStr, days, (days>1)?"s":"", hour, min, sec);
} else if(hour > 0) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%u:%02u:%02lu", hour, min, sec);
} else if(min > 0) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%u:%02lu", min, sec);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu sec", sec);
}
return(outStr);
}
/* ******************************* */
char* formatMicroSeconds(unsigned long microsec,
char* outStr, int outStrLen) {
float f = ((float)microsec)/1000;
if(f < 1000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f ms", f);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f sec", (f/1000));
}
return(outStr);
}
/* ******************************* */
char* formatThroughput(float numBytes /* <=== Bytes/second */, u_char htmlFormat,
char* outStr, int outStrLen) {
float numBits;
int divider = 1000; /* As SNMP does instead of using 1024 ntop divides for 1000 */
char *separator;
if(htmlFormat)
separator = myGlobals.separator;
else
separator = " ";
if(numBytes < 0) numBytes = 0; /* Sanity check */
numBits = numBytes*8;
if (numBits < divider) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sbit/s", numBits, separator);
} else if (numBits < (divider*divider)) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sKbit/s", ((float)(numBits)/divider), separator);
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.1f%sMbit/s", ((float)(numBits)/1048576), separator);
}
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "%.2f = %s", numBytes, outStr);
#endif
return(outStr);
}
/* ******************************* */
char* formatLatency(struct timeval tv, u_short sessionState, char* outStr, int outStrLen) {
if(((tv.tv_sec == 0) && (tv.tv_usec == 0))
|| (sessionState < FLAG_STATE_ACTIVE)
/* Patch courtesy of
Andreas Pfaller <[email protected]>
*/) {
/*
Latency not computed (the session was initiated
before ntop started
*/
return(" ");
} else {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%.02f ms",
((float)tv.tv_sec*1000+(float)tv.tv_usec/1000));
return(outStr);
}
}
/* ******************************* */
char* formatTimeStamp(unsigned int ndays,
unsigned int nhours,
unsigned int nminutes, char* outStr, int outStrLen) {
time_t theTime;
/* printf("%u - %u - %u\n", ndays, nhours, nminutes); */
if((ndays == 0)
&& (nhours == 0)
&& (nminutes == 0)) {
if (myGlobals.pcap_file_list != NULL) {
theTime = myGlobals.lastPktTime.tv_sec;
}
else {
return("now");
}
} else {
if (myGlobals.pcap_file_list != NULL) {
theTime = myGlobals.lastPktTime.tv_sec-(ndays*86400)-(nhours*3600)-(nminutes*60);
} else {
theTime = myGlobals.actTime-(ndays*86400)-(nhours*3600)-(nminutes*60);
}
}
strncpy(outStr, ctime(&theTime), outStrLen);
outStr[outStrLen-1] = '\0'; /* Remove trailer '\n' */
return(outStr);
}
/* ************************ */
char* formatPkts(Counter pktNr, char* outStr, int outStrLen) {
if(pktNr < 1000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu", (unsigned long)pktNr);
} else if(pktNr < 1000000) {
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu,%03lu",
(unsigned long)(pktNr/1000),
((unsigned long)pktNr)%1000);
} else if(pktNr < 1000000000) {
unsigned long a, b, c;
a = (unsigned long)(pktNr/1000000);
b = (unsigned long)((pktNr-a*1000000)/1000);
c = ((unsigned long)pktNr)%1000;
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu,%03lu,%03lu", a, b, c);
} else {
unsigned long a, b, c;
unsigned long e, f;
e = pktNr/1000000000;
f = pktNr - 1000000000*e;
a = (unsigned long)(f/1000000);
b = (unsigned long)((f-a*1000000)/1000);
c = ((unsigned long)f)%1000;
safe_snprintf(__FILE__, __LINE__, outStr, outStrLen, "%lu,%03lu,%03lu,%03lu", e, a, b, c);
}
return(outStr);
}
/* ************************************ */
char* _formatTime(time_t *theTime, char* outStr, int outStrLen, char* file, int line) {
struct tm *locTime;
struct tm myLocTime;
locTime = localtime_r(theTime, &myLocTime);
if(strftime(outStr, outStrLen, CONST_LOCALE_TIMESPEC, locTime) == 0)
traceEvent(CONST_TRACE_ERROR, "Buffer too short @ %s:%d for formatTime() [%s]",
file, line, outStr);
return(outStr);
}