comparison dcclib/print_info.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.56 $Revision$
38 */
39
40 #include "dcc_clnt.h"
41 #include "dcc_xhdr.h"
42
43
44 static void
45 print_srvr(const DCC_SRVR_CLASS *class,
46 const DCC_SRVR_ADDR *ap,
47 u_char print_name,
48 u_char have_rtt_adj)
49 {
50 char srvr_nm[DCC_MAXDOMAINLEN];
51 char a1[DCC_SU2STR_SIZE+1+5];
52 DCC_SOCKU su;
53 int addr_len, name_len, i, j;
54
55 printf("# %1s", class->srvr_inx == ap - class->addrs ? "*" : "");
56 if (print_name) {
57 dcc_mk_su(&su, ap->ip.family, &ap->ip.u, ap->ip.port);
58 dcc_su2name(srvr_nm, sizeof(srvr_nm), &su);
59 } else {
60 srvr_nm[0] = '\0';
61 }
62 addr_len = dcc_ap2str_opt(a1, sizeof(a1),
63 class, ap - class->addrs, '-');
64 name_len = strlen(srvr_nm);
65 i = 22 - (name_len-25);
66 if (i < 1)
67 i = 1;
68 else if (i > 22)
69 i = 22;
70 j = 25 - (addr_len-22);
71 if (j < 1)
72 j = 1;
73 else if (j > 25)
74 j = 25;
75 printf("%-*s %-*s", i, a1, j, srvr_nm);
76 if (ap->srvr_id != DCC_ID_INVALID) {
77 i = 16 - ((addr_len+name_len) - (22+25));
78 if (i < 1)
79 i = 1;
80 else if (i > 16)
81 i = 16;
82 printf(" %*s ID %d", i, ap->brand, ap->srvr_id);
83 if (ap->srvr_pkt_vers != DCC_PKT_VERSION)
84 printf("\n# protocol version %d",
85 ap->srvr_pkt_vers);
86 }
87 putchar('\n');
88
89 if (ap->rtt >= DCC_RTT_BAD) {
90 fputs("# not answering\n", stdout);
91 return;
92 }
93 if (ap->total_xmits == 0) {
94 printf("# %22s", "");
95 } else {
96 printf("# %3.0f%% of %2d requests ok",
97 (ap->total_resps*100.0)/ap->total_xmits,
98 ap->total_xmits);
99 }
100 if (have_rtt_adj) {
101 if (ap->srvr_pkt_vers < DCC_PKT_VERSION
102 #ifdef DCC_PKT_VERSION7
103 /* version 7 and 8 are the same for the free code */
104 && ap->srvr_pkt_vers+1 != DCC_PKT_VERSION
105 #endif
106 && ap->srvr_id != DCC_ID_INVALID) {
107 i = printf("%8.2f%+d+%d ms RTT",
108 ap->rtt/1000.0,
109 class->nms[ap->nam_inx].rtt_adj/1000,
110 DCC_RTT_VERS_ADJ/1000);
111 } else {
112 i = printf("%8.2f%+d ms RTT",
113 ap->rtt/1000.0,
114 class->nms[ap->nam_inx].rtt_adj/1000);
115 }
116 } else {
117 i = printf("%8.2f ms RTT",
118 ap->rtt/1000.0);
119 }
120 i = (i >= 22) ? 1 : (22-i);
121 printf(" %*s %4d ms queue wait\n",
122 i, "", ap->srvr_wait/1000);
123 }
124
125
126
127 /* dump the /var/dcc/map file */
128 void
129 dcc_print_info(const char *map_nm, /* or null for temporary file */
130 const DCC_CLNT_INFO *info,
131 u_char quiet,
132 u_char grey,
133 u_char srcbad,
134 u_char names,
135 u_char show_passwd)
136 {
137 #define dcc_clnt_info ??? error using dcc_clnt_info
138 DCC_PATH path;
139 char date_buf[40];
140 int port;
141 NAM_INX nam_inx;
142 time_t now;
143 const DCC_SRVR_CLASS *class;
144 const DCC_SRVR_ADDR *ap,*ap_prev, *ap_next;
145 u_char printed_addr[DCC_MAX_SRVR_ADDRS];
146 char sustr[DCC_SU2STR_SIZE];
147 u_char have_rtt_adj;
148 u_char need_blank_line;
149 int i;
150
151 now = time(0);
152 class = grey ? &info->grey : &info->dcc;
153 if (map_nm && !quiet) {
154 printf("# %-s %s%s\n",
155 dcc_time2str(date_buf, sizeof(date_buf), "%x %X %Z",
156 now),
157 grey ? "greylist " : "",
158 fnm2abs_err(path, map_nm));
159 fputs("# ", stdout);
160 if (class->resolve > now || dcc_clnt_debug)
161 printf("Re-resolve names after %s ",
162 dcc_time2str(date_buf, sizeof(date_buf), "%X",
163 class->resolve));
164
165 if (class->measure > now || dcc_clnt_debug)
166 printf("Check RTTs after %s",
167 dcc_time2str(date_buf, sizeof(date_buf), "%X",
168 class->measure));
169
170 putchar('\n');
171
172 fputs("# ", stdout);
173 i = 0;
174 for (ap = class->addrs; ap <= LAST(class->addrs); ++ap) {
175 if (ap->rtt != DCC_RTT_BAD
176 && ap->ip.family != AF_UNSPEC)
177 ++i;
178 }
179 if (i > 1 || dcc_clnt_debug)
180 printf("%6.2f ms threshold, %4.2f ms average ",
181 class->thold_rtt/1000.0,
182 class->avg_thold_rtt/1000.0);
183 printf("%d total, %d working servers\n",
184 class->num_srvrs, i);
185 if (class->fail_exp != 0) {
186 int fail_time = class->fail_time - now;
187 if (fail_time > 0
188 && fail_time <= DCC_MAX_FAIL_SECS) {
189 printf("# skipping asking %s server"
190 " %d seconds more\n",
191 grey ? "greylist" : "DCC", fail_time);
192 }
193 }
194
195 i = now/DCCPROC_COST - info->dccproc_last/DCCPROC_COST;
196 if (i > DCCPROC_MAX_CREDITS*2)
197 i = DCCPROC_MAX_CREDITS*2;
198 else if (i < 0)
199 i = 0;
200 i += info->dccproc_c;
201 if (i > DCCPROC_MAX_CREDITS)
202 i = DCCPROC_MAX_CREDITS;
203 else if (i < -DCCPROC_MAX_CREDITS)
204 i = -DCCPROC_MAX_CREDITS;
205 if (!grey && (i < DCCPROC_MAX_CREDITS
206 || !DCC_IS_TIME(now, info->dccproc_dccifd_try,
207 DCCPROC_TRY_DCCIFD))) {
208 printf("# %d dccproc balance since %s",
209 i, dcc_time2str(date_buf, sizeof(date_buf), "%X",
210 info->dccproc_last));
211 if (!DCC_IS_TIME(now, info->dccproc_dccifd_try,
212 DCCPROC_TRY_DCCIFD))
213 printf("; do not try to start dccifd until %s",
214 dcc_time2str(date_buf, sizeof(date_buf),
215 "%X",
216 info->dccproc_dccifd_try));
217 putchar('\n');
218 }
219 }
220 if (map_nm && !grey) {
221 fputs((info->flags & DCC_INFO_FG_IPV6)
222 ? DCC_INFO_USE_IPV6 : DCC_INFO_USE_IPV4, stdout);
223 if (info->flags & DCC_INFO_FG_SOCKS)
224 fputs(" "DCC_INFO_USE_SOCKS, stdout);
225 if (info->src.family != AF_UNSPEC) {
226 printf(" "DCC_INFO_USE_SRC"%s%s",
227 dcc_ip2str(sustr, sizeof(sustr),
228 &info->src),
229 srcbad ? " "DCC_INFO_USE_SRCBAD : "");
230 }
231 putchar('\n');
232 }
233
234 have_rtt_adj = 0;
235 for (nam_inx = 0; nam_inx < DIM(class->nms); ++nam_inx) {
236 if (class->nms[nam_inx].hostname[0] == '\0')
237 continue;
238 if (class->nms[nam_inx].rtt_adj != 0) {
239 have_rtt_adj = 1;
240 break;
241 }
242 }
243 if (!have_rtt_adj) {
244 for (ap = class->addrs; ap<=LAST(class->addrs); ++ap) {
245 if (ap->srvr_pkt_vers < DCC_PKT_VERSION
246 #ifdef DCC_PKT_VERSION7
247 /* version 7 and 8 are the same for the free code */
248 && ap->srvr_pkt_vers+1 != DCC_PKT_VERSION
249 #endif
250 && ap->srvr_id != DCC_ID_INVALID) {
251 have_rtt_adj = 1;
252 break;
253 }
254 }
255 }
256
257 memset(printed_addr, 0, sizeof(printed_addr));
258
259 /* convert each non-null hostname */
260 need_blank_line = 1;
261 for (nam_inx = 0; nam_inx < DIM(class->nms); ++nam_inx) {
262 if (class->nms[nam_inx].hostname[0] == '\0')
263 continue;
264
265 /* First display the main line for a host */
266 if (class->nms[nam_inx].defined == 0)
267 need_blank_line = 1;
268 if (!need_blank_line && nam_inx != 0) {
269 for (ap = class->addrs; ap<=LAST(class->addrs); ++ap) {
270 if (ap->nam_inx == nam_inx) {
271 need_blank_line = 1;
272 break;
273 }
274 }
275 }
276 if (need_blank_line) {
277 need_blank_line = 0;
278 if (!quiet)
279 putchar('\n');
280 }
281 i = printf("%s", class->nms[nam_inx].hostname);
282 i = (i >= 26) ? 1 : (26-i);
283 port = class->nms[nam_inx].port;
284 if (port == DCC_GREY2PORT(grey))
285 printf(",%-*s", i, "- ");
286 else
287 printf(",%-*d", i, ntohs(port));
288
289 if (grey)
290 fputs(" Greylist", stdout);
291
292 if (have_rtt_adj) {
293 i = printf(" RTT%+d ms",
294 class->nms[nam_inx].rtt_adj/1000);
295 i = (i >= 12) ? 1 : (12-i);
296 printf("%*s", i, "");
297 }
298
299 /* Suppress the password if it does not exist or is secret */
300 if (class->nms[nam_inx].clnt_id == DCC_ID_ANON) {
301 fputs(" "DCC_XHDR_ID_ANON"\n", stdout);
302 } else {
303 printf(" %5d "DCC_PASSWD_PAT"\n",
304 class->nms[nam_inx].clnt_id,
305 show_passwd ? class->nms[nam_inx].passwd : "");
306 }
307
308 if (class->nms[nam_inx].defined == 0) {
309 need_blank_line = 1;
310 if (!quiet)
311 fputs("# UNDEFINED\n", stdout);
312 }
313
314 /* display operating information for each IP address
315 * kludge sort the IP addresses */
316 for (ap_prev = 0, i = 0;
317 i < DCC_MAX_SRVR_ADDRS;
318 ap_prev = ap_next, ++i) {
319 ap_next = 0;
320 for (ap = class->addrs; ap<=LAST(class->addrs); ++ap) {
321 if (ap->nam_inx != nam_inx
322 || ap->ip.family == AF_UNSPEC)
323 continue;
324 /* find smallest IP address not yet printed */
325 if (printed_addr[ap - class->addrs])
326 continue;
327 if (ap_next
328 && ap->ip.family >= ap_next->ip.family
329 && 0 <= memcmp(&ap->ip.u, &ap_next->ip.u,
330 sizeof(ap->ip.u))
331 && ap->ip.port >= ap_next->ip.port)
332 continue;
333 if (!ap_prev
334 || ap->ip.family >= ap_prev->ip.family
335 || 0 <= memcmp(&ap->ip.u, &ap_prev->ip.u,
336 sizeof(ap->ip.u))
337 || ap->ip.port >= ap_prev->ip.port)
338 ap_next = ap;
339 }
340 if (!ap_next)
341 break;
342 if (!quiet)
343 print_srvr(class, ap_next, names, have_rtt_adj);
344 printed_addr[ap_next - class->addrs] = 1;
345 need_blank_line = 1;
346
347 }
348 }
349
350 for (ap = class->addrs, i = 0; ap <= LAST(class->addrs); ++ap, ++i) {
351 if (ap->ip.family == 0)
352 continue;
353 if (printed_addr[i])
354 continue;
355 printf("\n# stray address entry #%d, nam_inx %d:\n",
356 i, ap->nam_inx);
357 print_srvr(class, ap, names, have_rtt_adj);
358 }
359
360 #undef dcc_clnt_info
361 }