-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn2pingdb.c
More file actions
87 lines (77 loc) · 2.49 KB
/
n2pingdb.c
File metadata and controls
87 lines (77 loc) · 2.49 KB
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
#include "n2ping.h"
#include "n2malloc.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* ------------------------------------------------------------------------- *\
* FUNCTION read_ping_data (address) *
* --------------------------------- *
* Utility function for the main process that reads the stored ping log for *
* the provided address from /var/state/n2/ping. *
\* ------------------------------------------------------------------------- */
ping_log *read_ping_data (unsigned long addr)
{
char fname[256];
ping_log *res;
FILE *F;
sprintf (fname, "/var/state/n2/ping/%d.%d.%d.%d",
(addr & 0xff000000) >> 24,
(addr & 0x00ff0000) >> 16,
(addr & 0x0000ff00) >> 8,
(addr & 0x000000ff));
res = NULL;
F = fopen (fname, "r");
if (F)
{
res = (ping_log *) pool_alloc (sizeof (ping_log));
fread (res, sizeof (ping_log), 1, F);
fclose (F);
}
return res;
}
/* ------------------------------------------------------------------------- *\
* FUNCTION calc_ping10 (pinglog) *
* ------------------------------ *
* Calculates a pingtime average for a backlog of packets from the current *
* position of the provided ping_log structure. *
\* ------------------------------------------------------------------------- */
unsigned short calc_ping10 (ping_log *r)
{
int crsr;
int count;
int total;
int done = 0;
total = 0;
crsr = r->pos & 0xff;
for (count=0; count<10; ++count)
{
crsr = (crsr-1) & 0xff;
if (r->times[crsr])
{
total += r->times[crsr];
++done;
}
}
if (!done) return 0;
return (total/done);
}
/* ------------------------------------------------------------------------- *\
* FUNCTION calc_loss (pinglog) *
* ---------------------------- *
* Calculates the packet loss from the provided ping_log, measured for a *
* number of packets backwards from the current position. *
\* ------------------------------------------------------------------------- */
unsigned short calc_loss (ping_log *r)
{
int crsr;
int count;
unsigned short total;
total = 0;
crsr = r->pos & 0xff;
for (count=0; count<20; ++count)
{
crsr = (crsr-1) & 0xff;
if (r->times[crsr] == 0) total += (10000/20);
}
return (total);
}