-
Notifications
You must be signed in to change notification settings - Fork 2
/
cliprint.c
317 lines (267 loc) · 7.85 KB
/
cliprint.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
/*
* Copyright (c) 2010-2011, Red Hat, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND RED HAT, INC. DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RED HAT, INC. BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Author: Jan Friesse <[email protected]>
*/
#include <sys/types.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include "cliprint.h"
#include "logging.h"
#include "omping.h"
/*
* Print status of client with host_name (maximum length of host_name_len). transport_method is
* transport method to be used, mcast_addr is current multicast address to be used by client.
* remote_addr is address of client and state is current state of client.
*/
void
cliprint_client_state(const char *host_name, int host_name_len,
enum sf_transport_method transport_method, const struct sockaddr_storage *mcast_addr,
const struct sockaddr_storage *remote_addr, enum rh_client_state state,
enum rh_client_stop_reason stop_reason)
{
char mcast_addr_str[INET6_ADDRSTRLEN];
char rh_addr_str[INET6_ADDRSTRLEN];
printf("%-*s : ", host_name_len, host_name);
switch (state) {
case RH_CS_INITIAL:
printf("waiting for response msg");
break;
case RH_CS_QUERY:
memset(mcast_addr_str, 0, sizeof(mcast_addr_str));
memset(rh_addr_str, 0, sizeof(rh_addr_str));
if (mcast_addr != NULL) {
af_sa_to_str(AF_CAST_SA(mcast_addr), mcast_addr_str);
}
if (remote_addr != NULL) {
af_sa_to_str(AF_CAST_SA(remote_addr), rh_addr_str);
}
switch (transport_method) {
case SF_TM_ASM:
printf("joined (S,G) = (*, %s), pinging", mcast_addr_str);
break;
case SF_TM_SSM:
printf("joined (S,G) = (%s, %s), pinging", rh_addr_str, mcast_addr_str);
break;
case SF_TM_IPBC:
printf("joined (S,G) = (*, %s), pinging", mcast_addr_str);
break;
}
break;
case RH_CS_STOP:
switch (stop_reason) {
case RH_CSR_NONE:
DEBUG_PRINTF("internal program error.");
errx(1, "Internal program error");
break;
case RH_CSR_SERVER:
printf("server told us to stop");
break;
case RH_CSR_SEND_MAXIMUM:
printf("maximum number of query messages exhausted");
break;
case RH_CSR_TO_SEND_EXHAUSTED:
printf("given amount of query messages was sent");
break;
case RH_CSR_REMOTE_VERSION_RECEIVED:
printf("remote version received");
break;
}
break;
}
printf("\n");
}
/*
* Print final remote versions. remote_hosts is list with all remote hosts and host_name_len is
* maximal length of host name in list.
*/
void
cliprint_final_remote_version(const struct rh_list *remote_hosts, int host_name_len)
{
struct rh_item *rh_item;
struct rh_item_ci *ci;
size_t i;
unsigned char ch;
printf("\n");
TAILQ_FOREACH(rh_item, remote_hosts, entries) {
ci = &rh_item->client_info;
printf("%-*s : ", host_name_len, rh_item->addr->host_name);
if (ci->server_info_len == 0) {
printf("response message not received\n");
} else {
for (i = 0; i < ci->server_info_len; i++) {
ch = ci->server_info[i];
if (ch >= ' ' && ch < 0x7f && ch != '\\') {
fputc(ch, stdout);
} else {
if (ch == '\\') {
printf("\\\\");
} else {
printf("\\x%02X", ch);
}
}
}
printf("\n");
}
}
}
/*
* Print final statistics. remote_hosts is list with all remote hosts and host_name_len is maximal
* length of host name in list. transport_method is transport method (SF_TM_ASM/SSM/IPBC) from
* omping instance.
*/
void
cliprint_final_stats(const struct rh_list *remote_hosts, int host_name_len,
enum sf_transport_method transport_method)
{
const char *cast_str;
struct rh_item *rh_item;
struct rh_item_ci *ci;
enum sf_cast_type cast_type;
double avg_rtt;
int i;
int loss;
int loss_adj;
uint64_t received;
uint64_t sent;
printf("\n");
loss_adj = 0;
TAILQ_FOREACH(rh_item, remote_hosts, entries) {
for (i = 0; i < 2; i++) {
if (i == 0) {
cast_type = SF_CT_UNI;
} else {
switch (transport_method) {
case SF_TM_ASM:
case SF_TM_SSM:
cast_type = SF_CT_MULTI;
break;
case SF_TM_IPBC:
cast_type = SF_CT_BROAD;
break;
default:
DEBUG_PRINTF("Internal error - unknown transport method");
errx(1, "Internal error - unknown transport method");
/* NOTREACHED */
}
}
cast_str = sf_cast_type_to_str(cast_type);
ci = &rh_item->client_info;
received = ci->no_received[i];
sent = ci->no_sent;
printf("%-*s : ", host_name_len, rh_item->addr->host_name);
if (received == 0 && i == 0) {
printf("response message never received\n");
break;
}
if (i != 0) {
loss_adj = util_packet_loss_percent(sent - ci->first_mcast_seq + 1,
received);
}
loss = util_packet_loss_percent(sent, received);
if (received == 0) {
avg_rtt = 0;
} else {
avg_rtt = ci->avg_rtt[i] / UTIL_NSINMS;
}
printf("%5scast, ", cast_str);
printf("xmt/rcv/%%loss = ");
printf("%"PRIu64"/%"PRIu64, sent, received);
if (ci->no_dups[i] > 0) {
printf("+%"PRIu64, ci->no_dups[i]);
}
printf("/%d%%", loss);
if (i != 0 && ci->first_mcast_seq > 1) {
printf(" (seq>=%"PRIu32" %d%%)", ci->first_mcast_seq, loss_adj);
}
printf(", min/avg/max/std-dev = ");
printf("%.3f/%.3f/%.3f/%.3f", ci->rtt_min[i] / UTIL_NSINMS, avg_rtt,
ci->rtt_max[i] / UTIL_NSINMS,
util_ov_std_dev(ci->m2_rtt[i], ci->no_received[i]) / UTIL_NSINMS);
printf("\n");
}
}
}
/*
* Display newline
*/
void
cliprint_nl(void)
{
printf("\n");
}
/*
* Print packet statistics. host_name is remote host name with maximal host_name_len length. seq is
* sequence number of packet, is_dup is boolean with information if packet is duplicate or not,
* msg_len is length of message, dist_set is boolean variable with information if dist is set or
* not. dist is distance of packet (how TTL was changed). rtt_set is boolean variable with
* information if rtt (current round trip time) and avg_rtt (average round trip time) is set and
* computed or not. loss is number of lost packets. cast_type is type of packet received
* (unicast/multicast/broadcast). cont_stat is boolean variable saying, if to display
* continuous statistic or not.
*/
void
cliprint_packet_stats(const char *host_name, int host_name_len, uint32_t seq, int is_dup,
size_t msg_len, int dist_set, uint8_t dist, int rtt_set, double rtt, double avg_rtt, int loss,
enum sf_cast_type cast_type, int cont_stat)
{
const char *cast_str;
cast_str = sf_cast_type_to_str(cast_type);
printf("%-*s : ", host_name_len, host_name);
printf("%5scast, ", cast_str);
printf("seq=%"PRIu32, seq);
if (is_dup) {
printf(" (dup)");
}
printf(", ");
printf("size=%zu bytes", msg_len);
if (dist_set) {
printf(", dist=%"PRIu8, dist);
}
if (rtt_set) {
printf(", time=%.3fms", rtt);
}
if (cont_stat) {
printf(" (");
if (rtt_set) {
printf("%.3f avg, ", avg_rtt);
}
printf("%d%% loss)", loss);
}
printf("\n");
}
/*
* Display application ussage
*/
void
cliprint_usage(void)
{
printf("usage: %s [-46CDEFqVv] [-c count] [-i interval] [-M transport_method]\n",
PROGRAM_NAME);
printf("%14s[-m mcast_addr] [-O op_mode] [-p port] [-R rcvbuf] [-r rate_limit]\n", "");
printf("%14s[-S sndbuf] [-T timeout] [-t ttl] [-w wait_time] remote_addr...\n", "");
}
/*
* Show application version
*/
void
cliprint_version(void)
{
printf("%s version %s\n", PROGRAM_NAME, PROGRAM_VERSION);
}