annotate ping.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
1 /* $Id: ping.c,v 1.1.1.1 1997/12/06 05:41:29 darius Exp $ */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
2
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
3 /*
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
4 * ping.c
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
5 *
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
6 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
7
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
8 #include "copyright2.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
9 #include <stdio.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
10 #include <sys/types.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
11 #include <sys/time.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
12 #include <netinet/in.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
13 #include <math.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
14 #include <errno.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
15 #include "Wlib.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
16 #include "defs.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
17 #include "struct.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
18 #include "data.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
19 #include "packets.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
20 #include "proto.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
21
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
22 /* These are used only in pingstats.c */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
23
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
24 int ping_iloss_sc = 0; /* inc % loss 0--100, server to client */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
25 int ping_iloss_cs = 0; /* inc % loss 0--100, client to server */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
26 int ping_tloss_sc = 0; /* total % loss 0--100, server to client */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
27 int ping_tloss_cs = 0; /* total % loss 0--100, client to server */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
28 int ping_lag = 0; /* delay in ms of last ping */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
29 int ping_av = 0; /* rt time */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
30 int ping_sd = 0; /* std deviation */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
31
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
32 static int sum, n;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
33 static int M, var;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
34 static double s2;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
35
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
36 void sendServerPingResponse P((int number));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
37 void calc_lag P((void));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
38
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
39 void
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
40 handlePing(packet) /* SP_PING */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
41 struct ping_spacket *packet;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
42 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
43 ping = 1; /* we got a ping */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
44
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
45 /*
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
46 printf("ping received at %d (lag: %d)\n", msetime(), (int)packet->lag);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
47 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
48 sendServerPingResponse((int) packet->number);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
49 ping_lag = ntohs(packet->lag);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
50 ping_iloss_sc = (int) packet->iloss_sc;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
51 ping_iloss_cs = (int) packet->iloss_cs;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
52 ping_tloss_sc = (int) packet->tloss_sc;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
53 ping_tloss_cs = (int) packet->tloss_cs;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
54
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
55 calc_lag();
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
56
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
57 if (W_IsMapped(pStats)) /* pstat window */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
58 updatePStats();
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
59 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
60
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
61 void
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
62 startPing()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
63 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
64 static
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
65 struct ping_cpacket packet;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
66 extern int serverDead;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
67
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
68 packet.type = CP_PING_RESPONSE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
69 packet.pingme = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
70
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
71 if (gwrite(sock, (char *) &packet, sizeof(struct ping_cpacket)) !=
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
72 sizeof(struct ping_cpacket)) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
73 printf("gwrite failed.\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
74 serverDead = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
75 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
76 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
77
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
78 void
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
79 stopPing()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
80 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
81 static
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
82 struct ping_cpacket packet;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
83 extern int serverDead;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
84
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
85 ping = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
86 packet.type = CP_PING_RESPONSE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
87 packet.pingme = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
88
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
89 if (gwrite(sock, (char *) &packet, sizeof(struct ping_cpacket)) !=
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
90 sizeof(struct ping_cpacket)) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
91 printf("gwrite failed.\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
92 serverDead = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
93 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
94 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
95
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
96 void
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
97 sendServerPingResponse(number) /* CP_PING_RESPONSE */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
98 int number;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
99 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
100 struct ping_cpacket packet;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
101 int s;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
102 extern int serverDead;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
103
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
104 if (udpSock >= 0) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
105 s = udpSock;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
106 packets_sent++;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
107 } else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
108 s = sock;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
109
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
110 packet.type = CP_PING_RESPONSE;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
111 packet.pingme = (char) ping;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
112 packet.number = (unsigned char) number;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
113 /* count this one */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
114 packet.cp_sent = htonl(packets_sent);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
115 packet.cp_recv = htonl(packets_received);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
116
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
117 /*
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
118 printf("ping response sent at %d\n", msetime());
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
119 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
120
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
121 if (gwrite(s, (char *) &packet, sizeof(struct ping_cpacket)) !=
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
122 sizeof(struct ping_cpacket)) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
123 printf("gwrite failed.\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
124 serverDead = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
125 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
126 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
127
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
128 void
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
129 calc_lag()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
130 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
131 /* probably ghostbusted */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
132 /* without this things can get really bad */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
133 if (ping_lag > 2000 || ping_lag == 0)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
134 return;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
135
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
136 n++;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
137 sum += ping_lag;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
138 s2 += (ping_lag * ping_lag);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
139 if (n == 1)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
140 return;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
141
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
142 M = sum / n;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
143 var = (s2 - M * sum) / (n - 1);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
144
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
145 ping_av = M;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
146 ping_sd = (int) sqrt((double) var);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
147 }