-
Notifications
You must be signed in to change notification settings - Fork 1
/
study.c
131 lines (124 loc) · 3.18 KB
/
study.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
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <poll.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
double packets;
double ttotal;
double t[16][256];
double tsq[16][256];
long long tnum[16][256];
double u[16][256];
double udev[16][256];
char n[16];
void tally(double timing)
{
int j;
int b;
for (j = 0;j < 16;++j) {
b = 255 & (int) n[j];
++packets;
ttotal += timing;
t[j][b] += timing;
tsq[j][b] += timing * timing;
tnum[j][b] += 1;
}
}
int s;
int size;
void studyinput(FILE *fp)
{
int j;
char packet[2048];
char response[40];
struct pollfd p;
for (;;) {
if (size < 16) continue;
if (size > sizeof packet) continue;
/* a mediocre PRNG is sufficient here */
for (j = 0;j < size;++j) packet[j] = random();
for (j = 0;j < 16;++j) n[j] = packet[j];
send(s,packet,size,0);
p.fd = s;
p.events = POLLIN;
if (poll(&p,1,100) <= 0) continue;
while (p.revents & POLLIN) {
if (recv(s,response,sizeof response,0) == sizeof response) {
if (!memcmp(packet,response,16)) {
unsigned int timing;
timing = *(unsigned int *) (response + 36);
timing -= *(unsigned int *) (response + 32);
/*if (timing < 1400) { [> clip tail to reduce noise <]*/
fprintf(fp, "%u\n", timing);
/*fflush(fp);*/
tally(timing);
return;
/*}*/
}
}
if (poll(&p,1,0) <= 0) break;
}
}
}
void printpatterns(void)
{
int j;
int b;
double taverage;
taverage = ttotal / packets;
for (j = 0;j < 16;++j)
for (b = 0;b < 256;++b) {
u[j][b] = t[j][b] / tnum[j][b];
udev[j][b] = tsq[j][b] / tnum[j][b];
udev[j][b] -= u[j][b] * u[j][b];
udev[j][b] = sqrt(udev[j][b]);
}
for (j = 0;j < 16;++j) {
for (b = 0;b < 256;++b) {
printf("%2d %4d %3d %lld %.3f %.3f %.3f %.3f\n"
,j
,size
,b
,tnum[j][b]
,u[j][b]
,udev[j][b]
,u[j][b] - taverage
,udev[j][b] / sqrt(tnum[j][b])
);
}
}
fflush(stdout);
}
int timetoprint(long long inputs)
{
if (inputs < 10000) return 0;
if (!(inputs & (inputs - 1))) return 1;
return 0;
}
int main(int argc,char **argv)
{
struct sockaddr_in server;
long long inputs = 0;
if (!argv[1]) return 100;
if (!inet_aton(argv[1],&server.sin_addr)) return 100;
server.sin_family = AF_INET;
server.sin_port = htons(10000);
if (!argv[2]) return 100;
size = atoi(argv[2]);
while ((s = socket(AF_INET,SOCK_DGRAM,0)) == -1) sleep(1);
while (connect(s,(struct sockaddr *) &server,sizeof server
) == -1) sleep(1);
FILE *fp = fopen("/data/local/tmp/bern-timings.txt", "w+");
for (;;) {
studyinput(fp);
++inputs;
if (timetoprint(inputs)) printpatterns();
}
}