-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsfuncs.c
executable file
·478 lines (424 loc) · 14.1 KB
/
rsfuncs.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**************************************************************************************************
* FILENAME: rsfuncs.c
* PURPOSE: RoadShow Support functions
* CREATED: 21-Jun-2004
* MODIFIED: 23-Oct-2004
* AUTHOR: SGL
**************************************************************************************************/
#include "includes.h"
#include "globextvars.h"
#define MILLION 1000000
#define KILO 1000
/**************************************************************************************************
* Static prototypes:
**************************************************************************************************/
STATIC STRPTR TransformUnit(SBQUAD_T *bytes_u,SBQUAD_T *bytes_d,STRPTR buffer,BOOL onlyCalculate);
STATIC STRPTR TransformTime(ULONG seconds, STRPTR buffer);
STATIC STRPTR MakeHumanString(double bytes, STRPTR retbuffer);
// Taken from Olaf Barthels Roadshow-Tools
STATIC ULONG divide_64_by_32(SBQUAD_T * dividend,ULONG divisor,SBQUAD_T * quotient);
STATIC STRPTR convert_quad_to_string(const SBQUAD_T * const number,STRPTR string,LONG max_string_len);
/**************************************************************************************************
* FUNCTION: RetrieveSocketData()
* PURPOSE: Updates internal Memory list with stats from all Interfaces
* INPUT: -
* RETURN: -
* NOTES: Updates internal list and gui at once!
**************************************************************************************************/
void RetrieveSocketData(void)
{
struct MInterfaces *mi;
struct timeval ltv,current,calctv;
struct sockaddr_in sin;
struct sockaddr_in sin_mask;
long state;
LONG bps;
LONG mtu;
SBQUAD_T BytesIn = {0L,0L};
SBQUAD_T BytesOut = {0L,0L};
SBQUAD_T MYNULL = {0L,0L};
char volume[80],ontime[80],buf[80],buf1[30],buf2[30];
STRPTR IStates[] = {"Offline","Online","Inactive","Active",NULL};
STRPTR bindstr;
char buffer[2*LEN_DATSTRING+30];
double Sample,Speed,MaxIn,MaxOut,bin,bout,Stats_CurrSpeed,Stats_SpeedIn,Stats_SpeedOut;
LONG bind_type;
struct DateStamp lease_expire;
time_t currtime;
GetSysTime(¤t);
for(mi=(struct MInterfaces *)InterfaceList->mlh_Head;mi->Node.mln_Succ!=NULL;mi=(struct MInterfaces *)mi->Node.mln_Succ)
{
bind_type = IFABT_Unknown;
mtu = bps = state = 0;
memcpy(&calctv,¤t,sizeof(struct timeval));
memset(&sin,0,sizeof(sin));
memset(&sin_mask,0,sizeof(sin_mask));
memset(&lease_expire,0,sizeof(lease_expire));
if(QueryInterfaceTags(mi->Name,
IFQ_GetBytesIn, &BytesIn,
IFQ_GetBytesOut, &BytesOut,
IFQ_LastStart, <v,
IFQ_State, &state,
IFQ_Address, &sin,
IFQ_NetMask, &sin_mask,
IFQ_MTU, &mtu,
IFQ_BPS, &bps,
IFQ_AddressBindType, &bind_type,
IFQ_AddressLeaseExpires, &lease_expire,
TAG_END) != 0)
{
continue;
}
SubTime(&calctv,<v);
TransformTime(calctv.tv_sec,ontime);
TransformUnit(&BytesIn,&BytesOut,volume,FALSE);
set(mi->TimePtr,MUIA_Text_Contents,ontime);
set(mi->VolumePtr,MUIA_Text_Contents,volume);
set(mi->StatePtr,MUIA_Text_Contents,IStates[state]);
if(bps > MILLION)
{
sprintf(ontime,"%ld MBit/sec",bps/MILLION);
}
else if(bps > KILO)
{
sprintf(ontime,"%ld KBit/sec",bps/KILO);
}
else
{
sprintf(ontime,"%ld Bit/sec",bps);
}
if(bind_type == IFABT_Dynamic)
{
if(lease_expire.ds_Days == 0 && lease_expire.ds_Minute == 0 && lease_expire.ds_Tick == 0)
{
bindstr = "DHCP - Lease never expires";
}
else
{
struct DateTime dat;
char date[LEN_DATSTRING+1];
char time[LEN_DATSTRING+1];
LONG len;
memset(&dat,0,sizeof(dat));
dat.dat_Stamp = lease_expire;
dat.dat_Format = 4; /* FORMAT_DEF */
dat.dat_StrDate = date;
dat.dat_StrTime = time;
DateToStr(&dat);
len = strlen(date);
while(len > 0 && date[len-1] == ' ') date[--len] = '\0';
len = strlen(time);
while(len > 0 && time[len-1] == ' ') time[--len] = '\0';
strcpy(buffer,"DHCP - Lease expires at: ");
strcat(buffer,time);
strcat(buffer," ");
strcat(buffer,date);
bindstr = buffer;
}
}
else
{
bindstr = "Static";
}
strcpy(volume,Inet_NtoA(sin.sin_addr.s_addr));
strcpy(buf,Inet_NtoA(sin_mask.sin_addr.s_addr));
sprintf(mi->NameBubble,"IP-addr: %s/%s\nSpeed: %s\nMTU: %ld Bytes\nBindtype: %s",volume,buf,ontime,mtu,bindstr);
// V0.2: Display traffic broken down to up / down as Bubble, too:
memset(volume,'\0',sizeof(volume));
memset(buf,'\0',sizeof(buf));
TransformUnit(&BytesIn,&MYNULL,volume,FALSE); // Our total bytes downloaded.
TransformUnit(&BytesOut,&MYNULL,buf,FALSE); // Our total bytes uploaded.
sprintf(mi->VolumeBubble,"\33rIN: %s\nOUT: %s",volume,buf);
// Now finally calculate current transfer speed for this interface:
Sample = atof(TransformUnit(&BytesIn,&BytesOut,volume,TRUE)); // Our total bytes, current value.
MaxIn = atof(TransformUnit(&BytesIn,&MYNULL,buf1,TRUE)); // Bytes In, current value
MaxOut = atof(TransformUnit(&BytesOut,&MYNULL,buf2,TRUE)); // Bytes out, current value
bin = MaxIn;
bout = MaxOut;
if(!mi->FirstSample) // Must be first call, remember value and skip calc:
{
mi->FirstSample = Sample;
mi->FirstMaxIn = MaxIn;
mi->FirstMaxOut = MaxOut;
mi->MaxIn = 0.00;
mi->MaxOut = 0.00;
set(mi->SpeedPtr,MUIA_Text_Contents,"0.00 kB/s");
return;
}
// We have a sample and know our Interval, lets try to math a bit :)
mi->CurrentSample = Sample;
mi->CurrentMaxIn = MaxIn;
mi->CurrentMaxOut = MaxOut;
Sample = mi->CurrentSample - mi->FirstSample; // Get Bytes transfered in "INTERVAL" time
MaxIn = mi->CurrentMaxIn - mi->FirstMaxIn;
MaxOut = mi->CurrentMaxOut - mi->FirstMaxOut;
mi->FirstSample = mi->CurrentSample; // Add current sample back for next run
mi->FirstMaxIn = mi->CurrentMaxIn;
mi->FirstMaxOut = mi->CurrentMaxOut;
if(Sample)
{
Speed = (Sample / 1024) / RefreshInterval;
}
else
{
Speed = 0.00;
}
Stats_CurrSpeed = Speed;
sprintf(volume, "%#04.2f kB/s",Speed);
set(mi->SpeedPtr,MUIA_Text_Contents,volume);
if(MaxIn)
{
Speed = (MaxIn / 1024) / RefreshInterval;
}
else
{
Speed = 0.00;
}
Stats_SpeedIn = Speed;
sprintf(mi->CurrentInUnit,"%#04.2f kb/s",Speed);
if(Speed > mi->MaxIn)
{
sprintf(mi->MaxInUnit,"%#04.2f kb/s",Speed);
mi->MaxIn = Speed;
}
else
{
sprintf(mi->MaxInUnit,"%#04.2f kb/s",mi->MaxIn);
}
if(MaxOut)
{
Speed = (MaxOut / 1024) / RefreshInterval;
}
else
{
Speed = 0.00;
}
Stats_SpeedOut = Speed;
sprintf(mi->CurrentOutUnit,"%#04.2f kb/s",Speed);
if(Speed > mi->MaxOut)
{
sprintf(mi->MaxOutUnit,"%#04.2f kb/s",Speed);
mi->MaxOut = Speed;
}
else
{
sprintf(mi->MaxOutUnit,"%#04.2f kb/s",mi->MaxOut);
}
sprintf(mi->SpeedRecord,"\33rCurrent IN: %s\nCurrent OUT: %s\n\nMaximum IN: %s\nMaximum OUT: %s",mi->CurrentInUnit,mi->CurrentOutUnit,mi->MaxInUnit,mi->MaxOutUnit);
/**************************************************************************************************
* Check if we should write down these stats to as Env-variable to ENV:
* Filename format is: NetMon_Stats.%s
* Format of written var is (separated by colons):
* 1. TimeStamp
* 2. Total Bytes in/out
* 3. Total speed in kb/s
* 4. Bytes In
* 5. Speed in (current)
* 6. Bytes Out
* 7. Speed out (current)
**************************************************************************************************/
if(WriteStats == TRUE)
{
char fname[128];
memset(tempbuf1,'\0',TEMPBUFSIZE);
currtime = time(&currtime);
sprintf(tempbuf1,"%lu:%0.f:%#04.2f:%.0f:%#04.2f:%.0f:%#04.2f:%#04.2f:%#04.2f\n",
currtime,
(bin+bout),
Stats_CurrSpeed,
bin,
Stats_SpeedIn,
bout,
Stats_SpeedOut,
mi->MaxIn,
mi->MaxOut
);
sprintf(fname,"NetMon_Stats.%s",mi->Name);
SetVar(fname,tempbuf1,strlen(tempbuf1),GVF_GLOBAL_ONLY);
}
}
}
/**************************************************************************************************
* FUNCTION: TransformTime()
* PURPOSE: Returns given seconds in a Human-readable format: HH:MM.SS
* INPUT: seconds => Seconds to convert
* buffer => Buffer to take string
* RETURN: Converted String
**************************************************************************************************/
STATIC STRPTR TransformTime(ULONG seconds, STRPTR buffer )
{
long hours,minutes, secs, days;
hours = seconds / 3600;
if((seconds / 60) >= 60) minutes = (seconds - ((floor((double)seconds / 3600) * 3600))) / 60;
else minutes = seconds / 60;
secs = seconds % 60;
if(hours < 48)
{
sprintf(buffer,"%02ld:%02ld:%02ld",hours,minutes,secs);
}
else
{
days = hours/24;
sprintf(buffer,"%2ldd %02ldh",days,hours-(days*24));
}
return(buffer);
}
/**************************************************************************************************
* FUNCTION: TransformUnit()
* PURPOSE: Returns given Bytes in a Human-readable format, i.e. 12.4GB
* INPUT: bytes => Bytes to convert (64)
* buffer => Buffer to take string
* onlyCalculate => TRUE = Returns only byte string and check for new max. transfer rate
* FALSE = Returns traffic as human-readable string
* RETURN: Converted String
**************************************************************************************************/
STATIC STRPTR TransformUnit(SBQUAD_T *bytes_u,SBQUAD_T *bytes_d,STRPTR buffer, BOOL onlyCalculate)
{
char bc_u[40],bc_d[40];
double result=0;
memset(bc_u,'\0',sizeof(bc_u));
memset(bc_d,'\0',sizeof(bc_d));
if(bytes_u->sbq_High) // > 4.3GB
{
convert_quad_to_string(bytes_u,bc_u,sizeof(bc_u));
}
else
{
sprintf(bc_u,"%lu",bytes_u->sbq_Low);
}
if(bytes_d->sbq_High) // > 4.3GB
{
convert_quad_to_string(bytes_d,bc_d,sizeof(bc_d));
}
else
{
sprintf(bc_d,"%lu",bytes_d->sbq_Low);
}
result = atof(bc_u) + atof(bc_d);
if(onlyCalculate==TRUE)
{
sprintf(buffer,"%f",result);
return(buffer);
}
MakeHumanString(result,buffer);
}
/**************************************************************************************************
* FUNCTION: MakeHumanString
* PURPOSE: Converts bytes to human readable string including unit (kb, MB,GB etc.)
* INPUT: bytes => Bytes to convert
* retbuffer => Buffer to hold converted string
* RETURN: retbuffer
**************************************************************************************************/
STATIC STRPTR MakeHumanString(double bytes, STRPTR retbuffer)
{
int8 lv = 0;
char *units[] = {"","kB","MB","GB","TB"};
if(bytes >= 1024)
{
while(bytes >= 1024)
{
bytes = bytes / 1024;
lv++;
}
}
else
{
bytes = bytes / 1024;
lv = 1;
}
if(lv > 4) lv = 4;
sprintf(retbuffer,"%#5.2f %s",bytes,units[lv]);
return(retbuffer);
}
/**************************************************************************************************
* FUNCTION: divide_64_by_32()
* PURPOSE: Divide a 64 bit integer by a 32 bit integer, filling in a 64 bit quotient
* and returning a 32 bit remainder.
* INPUT: dividend (64),divisor(32), quotient(64)
* RETURN: Remainder (32)
* NOTES: Taken from "ShowNetStatus.c" written by Olaf Barthel.
**************************************************************************************************/
STATIC ULONG divide_64_by_32(SBQUAD_T * dividend,ULONG divisor,SBQUAD_T * quotient)
{
SBQUAD_T dividend_cdef = (*dividend);
ULONG dividend_ab = 0;
LONG i;
quotient->sbq_High = quotient->sbq_Low = 0;
for(i = 0 ; i < 64 ; i++)
{
/* Shift the quotient left by one bit. */
quotient->sbq_High = (quotient->sbq_High << 1);
if((quotient->sbq_Low & 0x80000000UL) != 0)
quotient->sbq_High |= 1;
quotient->sbq_Low = (quotient->sbq_Low << 1);
/* Shift the dividend left by one bit. We start
* with the most significant 32 bit portion.
*/
dividend_ab = (dividend_ab << 1);
if((dividend_cdef.sbq_High & 0x80000000UL) != 0)
dividend_ab |= 1;
/* Now for the middle 32 bit portion. */
dividend_cdef.sbq_High = (dividend_cdef.sbq_High << 1);
if((dividend_cdef.sbq_Low & 0x80000000UL) != 0)
dividend_cdef.sbq_High |= 1;
/* Finally, the least significant portion. */
dividend_cdef.sbq_Low = (dividend_cdef.sbq_Low << 1);
/* Does the divisor actually divide the dividend? */
if(dividend_ab >= divisor)
{
dividend_ab -= divisor;
/* We could divide the divisor. Keep track of
* this and take care of an overflow condition.
*/
quotient->sbq_Low++;
if(quotient->sbq_Low == 0)
quotient->sbq_High++;
}
}
return(dividend_ab);
}
/**************************************************************************************************
* FUNCTION: convert_quad_to_string()
* PURPOSE: Convert a 64 bit number into a textual representation, using base=10, just like
* sprintf(...,"%lD",...) would do if it could handle multiprecision numbers...
* INPUT: number(64) => SBQUAD_T Number to convert
* string => Target String
* max_string_len => Maximum target string length
* RETURN: Converted number as string
* NOTES: Taken from "ShowNetStatus.c" written by Olaf Barthel.
**************************************************************************************************/
STATIC STRPTR convert_quad_to_string(const SBQUAD_T * const number,STRPTR string,LONG max_string_len)
{
SBQUAD_T q;
STRPTR s;
UBYTE c;
ULONG r;
LONG i,len;
/* Make a local copy of the number. */
q = (*number);
s = string;
len = 0;
/* Build the number string in reverse order, calculating
* the single digits:
*/
while(len < max_string_len)
{
/* Divide by ten and remember the remainder. */
r = divide_64_by_32(&q,10,&q);
(*s++) = '0' + r;
len++;
/* Stop when we hit zero. */
if(q.sbq_High == 0 && q.sbq_Low == 0)
break;
}
/* Don't forget to terminate the string. */
(*s) = '\0';
/* Reverse the string in place. */
for(i = 0 ; i < len/2 ; i++)
{
c = string[len-1-i];
string[len-1-i] = string[i];
string[i] = c;
}
return(string);
}