ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_stats.c
Revision: 7456
Committed: Sat Mar 12 12:47:27 2016 UTC (10 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 46517 byte(s)
Log Message:
- m_stats.c:stats_memory(): remove extraneous whowas_count_memory() call

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2820 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 1997-2016 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2820 /*! \file m_stats.c
23     * \brief Includes required functions for processing the STATS command.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 2820 #include "list.h"
29     #include "client.h"
30     #include "irc_string.h"
31     #include "ircd.h"
32     #include "listener.h"
33 michael 1632 #include "conf.h"
34 michael 1644 #include "conf_class.h"
35 michael 7209 #include "conf_cluster.h"
36 michael 7304 #include "conf_gecos.h"
37 michael 7234 #include "conf_resv.h"
38 michael 7248 #include "conf_service.h"
39 michael 7209 #include "conf_shared.h"
40 adx 30 #include "hostmask.h"
41 michael 2820 #include "numeric.h"
42     #include "send.h"
43     #include "fdlist.h"
44 michael 3347 #include "misc.h"
45     #include "server.h"
46 michael 2820 #include "event.h"
47 adx 30 #include "parse.h"
48     #include "modules.h"
49     #include "whowas.h"
50 michael 884 #include "watch.h"
51 michael 6836 #include "reslib.h"
52 michael 2150 #include "motd.h"
53 michael 4325 #include "ipcache.h"
54 adx 30
55    
56 michael 7209 static void
57     report_shared(struct Client *source_p)
58 michael 1922 {
59 michael 7218 static const struct shared_types
60 michael 7209 {
61     unsigned int type;
62     unsigned char letter;
63     } flag_table[] = {
64     { SHARED_KLINE, 'K' },
65     { SHARED_UNKLINE, 'U' },
66     { SHARED_XLINE, 'X' },
67     { SHARED_UNXLINE, 'Y' },
68     { SHARED_RESV, 'Q' },
69     { SHARED_UNRESV, 'R' },
70     { SHARED_LOCOPS, 'L' },
71     { SHARED_DLINE, 'D' },
72     { SHARED_UNDLINE, 'E' },
73     { 0, '\0' }
74     };
75 michael 1922
76 michael 7209 dlink_node *node;
77 michael 7425 char buf[sizeof(flag_table) / sizeof(struct shared_types) + 1]; /* +1 for 'c' */
78 michael 7209
79     DLINK_FOREACH(node, shared_get_list()->head)
80     {
81 michael 7218 const struct SharedItem *shared = node->data;
82 michael 7209 char *p = buf;
83    
84     *p++ = 'c';
85    
86 michael 7218 for (const struct shared_types *tab = flag_table; tab->type; ++tab)
87     if (tab->type & shared->type)
88     *p++ = tab->letter;
89 michael 7209 else
90 michael 7218 *p++ = ToLower(tab->letter);
91 michael 7209
92     *p = '\0';
93    
94 michael 7218 sendto_one_numeric(source_p, &me, RPL_STATSULINE, shared->server,
95     shared->user, shared->host, buf);
96 michael 7209 }
97     }
98    
99     static void
100     report_cluster(struct Client *source_p)
101     {
102 michael 7218 static const struct cluster_types
103 michael 7209 {
104     unsigned int type;
105     unsigned char letter;
106     } flag_table[] = {
107     { CLUSTER_KLINE, 'K' },
108     { CLUSTER_UNKLINE, 'U' },
109     { CLUSTER_XLINE, 'X' },
110     { CLUSTER_UNXLINE, 'Y' },
111     { CLUSTER_RESV, 'Q' },
112     { CLUSTER_UNRESV, 'R' },
113     { CLUSTER_LOCOPS, 'L' },
114     { CLUSTER_DLINE, 'D' },
115     { CLUSTER_UNDLINE, 'E' },
116     { 0, '\0' }
117     };
118    
119     dlink_node *node;
120 michael 7425 char buf[sizeof(flag_table) / sizeof(struct cluster_types) + 1]; /* +1 for 'C' */
121 michael 7209
122     DLINK_FOREACH(node, cluster_get_list()->head)
123     {
124 michael 7218 const struct ClusterItem *cluster = node->data;
125 michael 7209 char *p = buf;
126    
127     *p++ = 'C';
128    
129 michael 7218 for (const struct cluster_types *tab = flag_table; tab->type; ++tab)
130     if (tab->type & cluster->type)
131     *p++ = tab->letter;
132 michael 7209 else
133 michael 7218 *p++ = ToLower(tab->letter);
134 michael 7209
135     *p = '\0';
136    
137 michael 7218 sendto_one_numeric(source_p, &me, RPL_STATSULINE, cluster->server,
138 michael 7209 "*", "*", buf);
139     }
140     }
141    
142 michael 7248 static void
143 michael 7397 stats_service(struct Client *source_p, int parc, char *parv[])
144 michael 7248 {
145     dlink_node *node;
146    
147     DLINK_FOREACH(node, service_get_list()->head)
148     {
149     const struct ServiceItem *service = node->data;
150     sendto_one_numeric(source_p, &me, RPL_STATSSERVICE, 'S', "*", service->name, 0, 0);
151     }
152     }
153    
154 michael 7304 static void
155 michael 7397 stats_gecos(struct Client *source_p, int parc, char *parv[])
156 michael 7304 {
157     dlink_node *node;
158    
159     DLINK_FOREACH(node, gecos_get_list()->head)
160     {
161     const struct GecosItem *gecos = node->data;
162     sendto_one_numeric(source_p, &me, RPL_STATSXLINE,
163 michael 7313 gecos->expire ? 'x' : 'X',
164 michael 7304 gecos->mask, gecos->reason);
165     }
166     }
167    
168 adx 30 /*
169 michael 1922 * inputs - pointer to client requesting confitem report
170     * - ConfType to report
171     * output - none
172     * side effects -
173     */
174     static void
175 michael 7405 stats_operator(struct Client *source_p, int parc, char *parv[])
176 michael 1922 {
177 michael 7405 dlink_node *node;
178 michael 1922
179 michael 7405 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_o_oper_only)
180 michael 1922 {
181 michael 7405 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
182     return;
183     }
184 michael 1922
185 michael 7405 DLINK_FOREACH(node, operator_items.head)
186     {
187     const struct MaskItem *conf = node->data;
188 michael 1922
189 michael 7405 /* Don't allow non opers to see oper privs */
190     if (HasUMode(source_p, UMODE_OPER))
191     sendto_one_numeric(source_p, &me, RPL_STATSOLINE, 'O', conf->user, conf->host,
192     conf->name, oper_privs_as_string(conf->port),
193     conf->class->name);
194     else
195     sendto_one_numeric(source_p, &me, RPL_STATSOLINE, 'O', conf->user, conf->host,
196     conf->name, "0", conf->class->name);
197     }
198     }
199 michael 1922
200 michael 7405 static void
201     stats_connect(struct Client *source_p, int parc, char *parv[])
202     {
203     dlink_node *node;
204 michael 1922
205 michael 7405 DLINK_FOREACH(node, connect_items.head)
206     {
207     char buf[8];
208     char *p = buf;
209     const struct MaskItem *conf = node->data;
210 michael 1922
211 michael 7405 if (IsConfAllowAutoConn(conf))
212     *p++ = 'A';
213     if (IsConfSSL(conf))
214     *p++ = 'S';
215     if (p == buf)
216     *p++ = '*';
217 michael 4034
218 michael 7405 *p = '\0';
219 michael 4034
220 michael 7405 /*
221     * Allow admins to see actual ips unless hide_server_ips is enabled
222     */
223     if (!ConfigServerHide.hide_server_ips && HasUMode(source_p, UMODE_ADMIN))
224     sendto_one_numeric(source_p, &me, RPL_STATSCLINE, 'C', conf->host,
225     buf, conf->name, conf->port,
226     conf->class->name);
227     else
228     sendto_one_numeric(source_p, &me, RPL_STATSCLINE, 'C',
229     "*@127.0.0.1", buf, conf->name, conf->port,
230     conf->class->name);
231 michael 1922 }
232     }
233    
234 michael 1927 /* report_resv()
235     *
236     * inputs - pointer to client pointer to report to.
237     * output - NONE
238     * side effects - report all resvs to client.
239     */
240     static void
241 michael 7397 stats_resv(struct Client *source_p, int parc, char *parv[])
242 michael 1927 {
243 michael 4815 const dlink_node *node = NULL;
244 michael 1927
245 michael 7282 DLINK_FOREACH(node, resv_chan_get_list()->head)
246 michael 1927 {
247 michael 7282 const struct ResvItem *resv = node->data;
248 michael 3109
249     sendto_one_numeric(source_p, &me, RPL_STATSQLINE,
250 michael 7282 resv->expire ? 'q' : 'Q',
251     resv->mask, resv->reason);
252 michael 1927 }
253    
254 michael 7282 DLINK_FOREACH(node, resv_nick_get_list()->head)
255 michael 1927 {
256 michael 7282 const struct ResvItem *resv = node->data;
257 michael 3109
258     sendto_one_numeric(source_p, &me, RPL_STATSQLINE,
259 michael 7282 resv->expire ? 'q' : 'Q',
260     resv->mask, resv->reason);
261 michael 1927 }
262     }
263    
264 adx 30 static void
265 michael 1457 stats_memory(struct Client *source_p, int parc, char *parv[])
266 adx 30 {
267 michael 7433 const dlink_node *node, *node2;
268 adx 30
269 michael 948 unsigned int local_client_conf_count = 0; /* local client conf links */
270     unsigned int users_counted = 0; /* user structs */
271 adx 30
272 michael 1017 unsigned int channel_members = 0;
273 michael 948 unsigned int channel_invites = 0;
274     unsigned int channel_bans = 0;
275     unsigned int channel_except = 0;
276     unsigned int channel_invex = 0;
277 adx 30
278 michael 948 unsigned int wwu = 0; /* whowas users */
279     unsigned int class_count = 0; /* classes */
280     unsigned int aways_counted = 0;
281 michael 4329 unsigned int number_ips_stored = 0; /* number of ip addresses hashed */
282 adx 30
283 michael 6719 size_t channel_memory = 0;
284     size_t channel_ban_memory = 0;
285     size_t channel_except_memory = 0;
286     size_t channel_invex_memory = 0;
287 adx 30
288     unsigned int safelist_count = 0;
289 michael 6719 size_t safelist_memory = 0;
290 adx 30
291 michael 6719 size_t wwm = 0; /* whowas array memory used */
292     size_t conf_memory = 0; /* memory used by conf lines */
293     size_t mem_ips_stored = 0; /* memory used by ip address hash */
294 adx 30
295 michael 6719 size_t total_channel_memory = 0;
296 adx 30
297     unsigned int local_client_count = 0;
298     unsigned int remote_client_count = 0;
299    
300 michael 6719 size_t local_client_memory_used = 0;
301     size_t remote_client_memory_used = 0;
302 adx 30
303 michael 6719 size_t total_memory = 0;
304 michael 6932 unsigned int channel_topics = 0;
305 adx 30
306 michael 1017 unsigned int watch_list_headers = 0; /* watchlist headers */
307     unsigned int watch_list_entries = 0; /* watchlist entries */
308 michael 6719 size_t watch_list_memory = 0; /* watchlist memory used */
309 michael 884
310 michael 7349 unsigned int listener_count = 0;
311     size_t listener_memory = 0;
312 adx 30
313 michael 7349
314 michael 7433 DLINK_FOREACH(node, global_client_list.head)
315 adx 30 {
316 michael 7433 const struct Client *target_p = node->data;
317 adx 30
318     if (MyConnect(target_p))
319     {
320     ++local_client_count;
321 michael 4588 local_client_conf_count += dlink_list_length(&target_p->connection->confs);
322     watch_list_entries += dlink_list_length(&target_p->connection->watches);
323 adx 30 }
324     else
325     ++remote_client_count;
326    
327     if (IsClient(target_p))
328     {
329     ++users_counted;
330    
331 michael 1483 if (target_p->away[0])
332 adx 30 ++aways_counted;
333     }
334     }
335    
336     /* Count up all channels, ban lists, except lists, Invex lists */
337 michael 5757 channel_memory = dlink_list_length(&channel_list) * sizeof(struct Channel);
338    
339 michael 7433 DLINK_FOREACH(node, channel_list.head)
340 adx 30 {
341 michael 7433 const struct Channel *chptr = node->data;
342 adx 30
343 michael 7433 if (chptr->topic[0])
344     ++channel_topics;
345    
346 michael 1017 channel_members += dlink_list_length(&chptr->members);
347 adx 30 channel_invites += dlink_list_length(&chptr->invites);
348    
349 michael 1017 channel_bans += dlink_list_length(&chptr->banlist);
350     channel_ban_memory += dlink_list_length(&chptr->banlist) * sizeof(struct Ban);
351    
352     channel_except += dlink_list_length(&chptr->exceptlist);
353     channel_except_memory += dlink_list_length(&chptr->exceptlist) * sizeof(struct Ban);
354    
355     channel_invex += dlink_list_length(&chptr->invexlist);
356     channel_invex_memory += dlink_list_length(&chptr->invexlist) * sizeof(struct Ban);
357 adx 30 }
358    
359     if ((safelist_count = dlink_list_length(&listing_client_list)))
360     {
361     safelist_memory = safelist_count * sizeof(struct ListTask);
362 michael 7433
363     DLINK_FOREACH(node, listing_client_list.head)
364 adx 30 {
365 michael 7433 const struct Client *acptr = node->data;
366 adx 30
367 michael 7433 DLINK_FOREACH(node2, acptr->connection->list_task->show_mask.head)
368     safelist_memory += strlen(node2->data);
369 adx 30
370 michael 7433 DLINK_FOREACH(node2, acptr->connection->list_task->hide_mask.head)
371     safelist_memory += strlen(node2->data);
372 adx 30 }
373     }
374    
375     /* count up all classes */
376 michael 1644 class_count = dlink_list_length(class_get_list());
377 adx 30
378 michael 1017 watch_count_memory(&watch_list_headers, &watch_list_memory);
379 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
380 michael 7355 "z :WATCH headers %u(%zu) entries %u(%u)",
381 michael 3573 watch_list_headers,
382     watch_list_memory, watch_list_entries,
383     watch_list_entries * sizeof(dlink_node) * 2);
384 michael 884
385 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
386 michael 3573 "z :Clients %u(%u)",
387     users_counted,
388     (users_counted * sizeof(struct Client)));
389 adx 30
390 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
391 michael 3573 "z :User aways %u", aways_counted);
392 adx 30
393 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
394 michael 6719 "z :Attached confs %u(%zu)",
395 michael 3573 local_client_conf_count,
396 michael 6719 local_client_conf_count * sizeof(dlink_node));
397 adx 30
398 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
399 michael 6719 "z :Resv channels %u(%zu) nicks %u(%zu)",
400 michael 7282 dlink_list_length(resv_chan_get_list()),
401     dlink_list_length(resv_chan_get_list()) * sizeof(struct ResvItem),
402     dlink_list_length(resv_nick_get_list()),
403     dlink_list_length(resv_nick_get_list()) * sizeof(struct ResvItem));
404 adx 30
405 michael 7349 listener_count_memory(&listener_count, &listener_memory);
406 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
407 michael 7355 "z :Listeners allocated %u(%zu)",
408 michael 7349 listener_count, listener_memory);
409    
410     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
411 michael 6719 "z :Classes %u(%zu)",
412     class_count, class_count * sizeof(struct ClassItem));
413 adx 30
414 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
415 michael 6932 "z :Channels %u(%zu) Topics %u",
416 michael 3944 dlink_list_length(&channel_list),
417 michael 6932 channel_memory, channel_topics);
418 adx 30
419 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
420 michael 6719 "z :Bans %u(%zu)",
421 michael 3573 channel_bans, channel_ban_memory);
422 adx 30
423 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
424 michael 6719 "z :Exceptions %u(%zu)",
425 michael 3573 channel_except, channel_except_memory);
426 adx 30
427 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
428 michael 6719 "z :Invex %u(%zu)",
429 michael 3573 channel_invex, channel_invex_memory);
430 adx 30
431 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
432 michael 6719 "z :Channel members %u(%zu) invites %u(%zu)",
433 michael 3573 channel_members,
434 michael 6719 channel_members * sizeof(struct Membership),
435     channel_invites,
436     channel_invites * sizeof(dlink_node) * 2);
437 adx 30
438     total_channel_memory = channel_memory + channel_ban_memory +
439 michael 1017 channel_members * sizeof(struct Membership) +
440 michael 6719 (channel_invites * sizeof(dlink_node) * 2);
441 adx 30
442 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
443 michael 6719 "z :Safelist %u(%zu)",
444 michael 3573 safelist_count, safelist_memory);
445 adx 30
446 michael 7437 whowas_count_memory(&wwu, &wwm);
447 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
448 michael 7437 "z :Whowas users %u(%zu)", wwu, wwm);
449 adx 30
450 michael 2150 motd_memory_count(source_p);
451 michael 4325 ipcache_get_stats(&number_ips_stored, &mem_ips_stored);
452 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
453 michael 6719 "z :iphash %u(%zu)",
454 michael 3573 number_ips_stored, mem_ips_stored);
455 adx 30
456 michael 7437 total_memory = total_channel_memory + conf_memory + class_count *
457 adx 30 sizeof(struct ClassItem);
458 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
459 michael 7437 "z :Total: channel %zu conf %zu",
460     total_channel_memory, conf_memory);
461 adx 30
462 michael 4576 local_client_memory_used = local_client_count*(sizeof(struct Client) + sizeof(struct Connection));
463 adx 30 total_memory += local_client_memory_used;
464 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
465 michael 6719 "z :Local client Memory in use: %u(%zu)",
466 michael 3573 local_client_count, local_client_memory_used);
467 adx 30
468     remote_client_memory_used = remote_client_count * sizeof(struct Client);
469     total_memory += remote_client_memory_used;
470 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
471 michael 6719 "z :Remote client Memory in use: %u(%zu)",
472 michael 3573 remote_client_count, remote_client_memory_used);
473 adx 30
474 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
475 michael 6719 "z :TOTAL: %zu",
476 michael 3573 me.name, RPL_STATSDEBUG, source_p->name,
477     total_memory);
478 adx 30 }
479    
480     static void
481 michael 4479 stats_dns_servers(struct Client *source_p, int parc, char *parv[])
482 adx 30 {
483 michael 6836 char ipaddr[HOSTIPLEN + 1] = "";
484    
485     for (unsigned int i = 0; i < irc_nscount; ++i)
486     {
487     getnameinfo((const struct sockaddr *)&(irc_nsaddr_list[i]),
488     irc_nsaddr_list[i].ss_len, ipaddr,
489     sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
490     sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
491 michael 6990 }
492 adx 30 }
493    
494     /* stats_deny()
495     *
496     * input - client to report to
497     * output - none
498     * side effects - client is given dline list.
499     */
500     static void
501 michael 1457 stats_deny(struct Client *source_p, int parc, char *parv[])
502 adx 30 {
503 michael 4032 const struct MaskItem *conf = NULL;
504 michael 4815 const dlink_node *node = NULL;
505 adx 30
506 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
507 adx 30 {
508 michael 4815 DLINK_FOREACH(node, atable[i].head)
509 adx 30 {
510 michael 4815 const struct AddressRec *arec = node->data;
511 michael 1367
512 michael 1632 if (arec->type != CONF_DLINE)
513     continue;
514 adx 30
515 michael 1632 conf = arec->conf;
516 adx 30
517 michael 5542 /* Don't report a temporary dline as permanent dline */
518 michael 1649 if (conf->until)
519 michael 1632 continue;
520 adx 30
521 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'D', conf->host, conf->reason);
522 adx 30 }
523     }
524     }
525    
526     /* stats_tdeny()
527     *
528     * input - client to report to
529     * output - none
530     * side effects - client is given dline list.
531     */
532     static void
533 michael 1457 stats_tdeny(struct Client *source_p, int parc, char *parv[])
534 adx 30 {
535 michael 4032 const struct MaskItem *conf = NULL;
536 michael 4815 const dlink_node *node = NULL;
537 adx 30
538 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
539 adx 30 {
540 michael 4815 DLINK_FOREACH(node, atable[i].head)
541 adx 30 {
542 michael 4815 const struct AddressRec *arec = node->data;
543 michael 1367
544 michael 1632 if (arec->type != CONF_DLINE)
545     continue;
546 adx 30
547 michael 1632 conf = arec->conf;
548 adx 30
549 michael 5542 /* Don't report a permanent dline as temporary dline */
550 michael 1649 if (!conf->until)
551 michael 1632 continue;
552 adx 30
553 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'd', conf->host, conf->reason);
554 adx 30 }
555     }
556     }
557    
558     /* stats_exempt()
559     *
560     * input - client to report to
561     * output - none
562     * side effects - client is given list of exempt blocks
563     */
564     static void
565 michael 1457 stats_exempt(struct Client *source_p, int parc, char *parv[])
566 adx 30 {
567 michael 4034 const struct MaskItem *conf = NULL;
568 michael 4815 const dlink_node *node = NULL;
569 adx 30
570 michael 4340 if (ConfigGeneral.stats_e_disabled)
571 michael 584 {
572 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
573 michael 584 return;
574     }
575    
576 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
577 adx 30 {
578 michael 4815 DLINK_FOREACH(node, atable[i].head)
579 adx 30 {
580 michael 4815 const struct AddressRec *arec = node->data;
581 michael 1367
582 michael 1632 if (arec->type != CONF_EXEMPT)
583     continue;
584 adx 30
585 michael 1632 conf = arec->conf;
586 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'e', conf->host, "");
587 adx 30 }
588     }
589     }
590    
591     static void
592 michael 1457 stats_events(struct Client *source_p, int parc, char *parv[])
593 adx 30 {
594 michael 6306 const dlink_node *node;
595    
596     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
597     "E :Operation Next Execution");
598     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
599     "E :---------------------------------------------");
600    
601     DLINK_FOREACH(node, event_get_list()->head)
602     {
603     const struct event *ev = node->data;
604    
605     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
606     "E :%-30s %-4d seconds",
607     ev->name,
608     (int)(ev->next - CurrentTime));
609     }
610 adx 30 }
611    
612     static void
613 michael 1457 stats_hubleaf(struct Client *source_p, int parc, char *parv[])
614 adx 30 {
615 michael 4815 const dlink_node *node = NULL, *dptr = NULL;
616 michael 2118
617 michael 7401 DLINK_FOREACH(node, connect_items.head)
618 michael 2118 {
619 michael 4815 const struct MaskItem *conf = node->data;
620 michael 2118
621     DLINK_FOREACH(dptr, conf->hub_list.head)
622 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSHLINE, 'H', dptr->data, conf->name, 0, "*");
623 michael 2118 }
624    
625 michael 7401 DLINK_FOREACH(node, connect_items.head)
626 michael 2118 {
627 michael 4815 const struct MaskItem *conf = node->data;
628 michael 2118
629     DLINK_FOREACH(dptr, conf->leaf_list.head)
630 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLLINE, 'L', dptr->data, conf->name, 0, "*");
631 michael 2118 }
632 adx 30 }
633    
634 michael 1372 /*
635     * show_iline_prefix()
636     *
637     * inputs - pointer to struct Client requesting output
638 michael 2820 * - pointer to struct MaskItem
639 michael 1372 * - name to which iline prefix will be prefixed to
640     * output - pointer to static string with prefixes listed in ascii form
641     * side effects - NONE
642     */
643     static const char *
644 michael 6506 show_iline_prefix(const struct Client *source_p, const struct MaskItem *conf)
645 michael 1372 {
646 michael 5985 static char prefix_of_host[USERLEN + 16];
647 michael 1372 char *prefix_ptr = prefix_of_host;
648    
649 michael 1715 if (IsConfWebIRC(conf))
650     *prefix_ptr++ = '<';
651 michael 1632 if (IsNoTilde(conf))
652 michael 1372 *prefix_ptr++ = '-';
653 michael 1632 if (IsNeedIdentd(conf))
654 michael 1372 *prefix_ptr++ = '+';
655 michael 1632 if (!IsNeedPassword(conf))
656 michael 1372 *prefix_ptr++ = '&';
657 michael 1632 if (IsConfExemptResv(conf))
658 michael 1372 *prefix_ptr++ = '$';
659 michael 1632 if (IsConfDoSpoofIp(conf))
660 michael 1372 *prefix_ptr++ = '=';
661 michael 6506 if (HasUMode(source_p, UMODE_OPER))
662     {
663     if (IsConfExemptKline(conf))
664     *prefix_ptr++ = '^';
665     if (IsConfExemptXline(conf))
666     *prefix_ptr++ = '!';
667     if (IsConfExemptLimits(conf))
668     *prefix_ptr++ = '>';
669     }
670    
671 michael 1632 if (IsConfCanFlood(conf))
672 michael 1372 *prefix_ptr++ = '|';
673    
674 michael 1632 strlcpy(prefix_ptr, conf->user, USERLEN+1);
675 michael 1372
676     return prefix_of_host;
677     }
678    
679 adx 30 static void
680 michael 3156 report_auth(struct Client *source_p, int parc, char *parv[])
681 michael 1372 {
682 michael 4032 const struct MaskItem *conf = NULL;
683 michael 4815 const dlink_node *node = NULL;
684 michael 1372
685 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
686 michael 1372 {
687 michael 4815 DLINK_FOREACH(node, atable[i].head)
688 michael 1372 {
689 michael 4815 const struct AddressRec *arec = node->data;
690 michael 1372
691 michael 1632 if (arec->type != CONF_CLIENT)
692     continue;
693 michael 1372
694 michael 1632 conf = arec->conf;
695 michael 1372
696 michael 6506 if (!HasUMode(source_p, UMODE_OPER) && IsConfDoSpoofIp(conf))
697 michael 1632 continue;
698 michael 1372
699 michael 4967 sendto_one_numeric(source_p, &me, RPL_STATSILINE, 'I',
700     conf->name == NULL ? "*" : conf->name,
701     show_iline_prefix(source_p, conf),
702     conf->host, conf->port,
703     conf->class->name);
704 michael 1372 }
705     }
706     }
707    
708     static void
709 michael 1457 stats_auth(struct Client *source_p, int parc, char *parv[])
710 adx 30 {
711     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
712 michael 4340 if (ConfigGeneral.stats_i_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
713 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
714 adx 30
715 michael 4034 /* If unopered, only return matching auth blocks */
716 michael 4340 else if (ConfigGeneral.stats_i_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
717 adx 30 {
718 michael 4034 const struct MaskItem *conf = NULL;
719 adx 30
720     if (MyConnect(source_p))
721 michael 1632 conf = find_conf_by_address(source_p->host,
722 michael 4588 &source_p->connection->ip, CONF_CLIENT,
723     source_p->connection->aftype,
724 michael 2820 source_p->username,
725 michael 4588 source_p->connection->password, 1);
726 adx 30 else
727 michael 4034 conf = find_conf_by_address(source_p->host, NULL, CONF_CLIENT, 0,
728     source_p->username, NULL, 1);
729 adx 30
730 michael 1632 if (conf == NULL)
731 adx 30 return;
732    
733 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSILINE,
734     'I', "*", show_iline_prefix(source_p, conf),
735     conf->host, conf->port,
736 michael 4037 conf->class->name);
737 adx 30 }
738 michael 4034 else /* They are opered, or allowed to see all auth blocks */
739 michael 1457 report_auth(source_p, 0, NULL);
740 adx 30 }
741    
742 michael 1372 /* report_Klines()
743     * Inputs: Client to report to,
744     * type(==0 for perm, !=0 for temporary)
745 michael 2820 * mask
746 michael 1372 * Output: None
747 michael 3156 * Side effects: Reports configured K(or k)-lines to source_p.
748 michael 1372 */
749 adx 30 static void
750 michael 3156 report_Klines(struct Client *source_p, int tkline)
751 michael 1372 {
752 michael 4032 const struct MaskItem *conf = NULL;
753 michael 4815 const dlink_node *node = NULL;
754 michael 1921 char c = '\0';
755 michael 1372
756     if (tkline)
757 michael 1921 c = 'k';
758 michael 1372 else
759 michael 1921 c = 'K';
760 michael 1372
761 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
762 michael 1372 {
763 michael 4815 DLINK_FOREACH(node, atable[i].head)
764 michael 1372 {
765 michael 4815 const struct AddressRec *arec = node->data;
766 michael 1372
767 michael 1632 if (arec->type != CONF_KLINE)
768     continue;
769 michael 1628
770 michael 1632 conf = arec->conf;
771 michael 1372
772 michael 1650 if ((!tkline && conf->until) ||
773     (tkline && !conf->until))
774 michael 1632 continue;
775    
776 michael 4570 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, c, conf->host, conf->user,
777     conf->reason);
778 michael 1372 }
779     }
780     }
781    
782     static void
783 michael 1457 stats_tklines(struct Client *source_p, int parc, char *parv[])
784 adx 30 {
785     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
786 michael 4340 if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
787 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
788 adx 30
789 michael 4034 /* If unopered, only return matching klines */
790 michael 4340 else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
791 adx 30 {
792 michael 4032 const struct MaskItem *conf = NULL;
793 adx 30
794     if (MyConnect(source_p))
795 michael 1632 conf = find_conf_by_address(source_p->host,
796 michael 4588 &source_p->connection->ip, CONF_KLINE,
797     source_p->connection->aftype,
798 michael 2820 source_p->username, NULL, 1);
799 adx 30 else
800 michael 4034 conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0,
801     source_p->username, NULL, 1);
802 adx 30
803 michael 1632 if (!conf)
804 adx 30 return;
805    
806 michael 5542 /* Don't report a permanent kline as temporary kline */
807 michael 1649 if (!conf->until)
808 adx 30 return;
809    
810 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'k',
811     conf->host, conf->user, conf->reason);
812 adx 30 }
813 michael 4034 else /* They are opered, or allowed to see all klines */
814 adx 30 report_Klines(source_p, 1);
815     }
816    
817     static void
818 michael 1457 stats_klines(struct Client *source_p, int parc, char *parv[])
819 adx 30 {
820     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
821 michael 4340 if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
822 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
823 adx 30
824 michael 4034 /* If unopered, only return matching klines */
825 michael 4340 else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
826 adx 30 {
827 michael 4032 const struct MaskItem *conf = NULL;
828 adx 30
829 michael 4034 /* Search for a kline */
830 michael 560 if (MyConnect(source_p))
831 michael 1632 conf = find_conf_by_address(source_p->host,
832 michael 4588 &source_p->connection->ip, CONF_KLINE,
833     source_p->connection->aftype,
834 michael 2820 source_p->username, NULL, 0);
835 adx 30 else
836 michael 4034 conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0,
837     source_p->username, NULL, 0);
838 adx 30
839 michael 1632 if (!conf)
840 adx 30 return;
841    
842 michael 5542 /* Don't report a temporary kline as permanent kline */
843 michael 1649 if (conf->until)
844 adx 30 return;
845 michael 2820
846 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'K',
847     conf->host, conf->user, conf->reason);
848 adx 30 }
849 michael 4034 else /* They are opered, or allowed to see all klines */
850 adx 30 report_Klines(source_p, 0);
851     }
852    
853     static void
854 michael 1457 stats_messages(struct Client *source_p, int parc, char *parv[])
855 adx 30 {
856 michael 5026 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_m_oper_only)
857     sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
858     else
859     report_messages(source_p);
860 adx 30 }
861    
862     /* stats_operedup()
863     *
864     * input - client pointer
865     * output - none
866     * side effects - client is shown a list of active opers
867     */
868     static void
869 michael 1457 stats_operedup(struct Client *source_p, int parc, char *parv[])
870 adx 30 {
871 michael 4815 const dlink_node *node = NULL;
872 michael 3511 unsigned int opercount = 0;
873 michael 3516 char buf[IRCD_BUFSIZE] = "";
874 adx 30
875 michael 4815 DLINK_FOREACH(node, oper_list.head)
876 adx 30 {
877 michael 4815 const struct Client *target_p = node->data;
878 adx 30
879 michael 1294 if (HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER))
880 adx 30 continue;
881    
882 michael 3516 if (HasUMode(source_p, UMODE_OPER) || !HasUMode(target_p, UMODE_HIDEIDLE))
883 michael 6846 snprintf(buf, sizeof(buf), "%s", time_dissect(client_get_idle_time(source_p, target_p)));
884 michael 3516 else
885     strlcpy(buf, "n/a", sizeof(buf));
886    
887 michael 7051 if (MyConnect(source_p) && HasUMode(source_p, UMODE_OPER))
888 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
889 michael 3573 "p :[%c][%s] %s (%s@%s) Idle: %s",
890     HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O',
891 michael 4588 oper_privs_as_string(target_p->connection->operflags),
892 michael 3573 target_p->name, target_p->username, target_p->host, buf);
893 adx 30 else
894 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
895 michael 3573 "p :[%c] %s (%s@%s) Idle: %s",
896     HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O',
897     target_p->name, target_p->username, target_p->host, buf);
898 michael 3511 ++opercount;
899 adx 30 }
900    
901 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
902 michael 3573 "p :%u OPER(s)", opercount);
903 adx 30 }
904    
905 michael 6366 /* show_ports()
906     *
907     * inputs - pointer to client to show ports to
908     * output - none
909     * side effects - send port listing to a client
910     */
911 adx 30 static void
912 michael 6366 show_ports(struct Client *source_p)
913     {
914     char buf[IRCD_BUFSIZE] = "";
915     char *p = NULL;
916     const dlink_node *node = NULL;
917    
918     DLINK_FOREACH(node, listener_get_list()->head)
919     {
920     const struct Listener *listener = node->data;
921     p = buf;
922    
923     if (listener->flags & LISTENER_HIDDEN)
924     {
925     if (!HasUMode(source_p, UMODE_ADMIN))
926     continue;
927     *p++ = 'H';
928     }
929    
930     if (listener->flags & LISTENER_SERVER)
931     *p++ = 'S';
932     if (listener->flags & LISTENER_SSL)
933     *p++ = 's';
934     *p = '\0';
935    
936    
937     if (HasUMode(source_p, UMODE_ADMIN) &&
938 michael 7051 (MyConnect(source_p) || !ConfigServerHide.hide_server_ips))
939 michael 6366 sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port,
940     listener->name,
941     listener->ref_count, buf,
942     listener->active ? "active" : "disabled");
943     else
944     sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port,
945     me.name, listener->ref_count, buf,
946     listener->active ? "active" : "disabled");
947     }
948     }
949    
950     static void
951 michael 1457 stats_ports(struct Client *source_p, int parc, char *parv[])
952 adx 30 {
953 michael 4340 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_P_oper_only)
954 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
955 adx 30 else
956     show_ports(source_p);
957     }
958    
959     static void
960 michael 1457 stats_tstats(struct Client *source_p, int parc, char *parv[])
961 adx 30 {
962 michael 4815 const dlink_node *node = NULL;
963 michael 896 struct ServerStatistics tmp;
964 michael 1842 struct ServerStatistics *sp = &tmp;
965 michael 896
966     memcpy(sp, &ServerStats, sizeof(struct ServerStatistics));
967    
968     /*
969     * must use the += operator. is_sv is not the number of currently
970     * active server connections. Note the incrementation in
971     * s_bsd.c:close_connection.
972     */
973 michael 4213 sp->is_sv += dlink_list_length(&local_server_list);
974 michael 896
975 michael 4815 DLINK_FOREACH(node, local_server_list.head)
976 michael 896 {
977 michael 4815 const struct Client *target_p = node->data;
978 michael 896
979 michael 4588 sp->is_sbs += target_p->connection->send.bytes;
980     sp->is_sbr += target_p->connection->recv.bytes;
981     sp->is_sti += CurrentTime - target_p->connection->firsttime;
982 michael 896 }
983    
984     sp->is_cl += dlink_list_length(&local_client_list);
985    
986 michael 4815 DLINK_FOREACH(node, local_client_list.head)
987 michael 896 {
988 michael 4815 const struct Client *target_p = node->data;
989 michael 896
990 michael 4588 sp->is_cbs += target_p->connection->send.bytes;
991     sp->is_cbr += target_p->connection->recv.bytes;
992     sp->is_cti += CurrentTime - target_p->connection->firsttime;
993 michael 896 }
994    
995     sp->is_ni += dlink_list_length(&unknown_list);
996    
997 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
998 michael 4040 "t :accepts %u refused %u",
999 michael 3573 sp->is_ac, sp->is_ref);
1000 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1001 michael 4040 "t :unknown commands %u prefixes %u",
1002 michael 3573 sp->is_unco, sp->is_unpf);
1003 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1004 michael 4040 "t :nick collisions %u unknown closes %u",
1005 michael 3573 sp->is_kill, sp->is_ni);
1006 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1007 michael 4040 "t :wrong direction %u empty %u",
1008 michael 3573 sp->is_wrdi, sp->is_empt);
1009 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1010 michael 4040 "t :numerics seen %u",
1011 michael 3573 sp->is_num);
1012 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1013 michael 4040 "t :auth successes %u fails %u",
1014 michael 3573 sp->is_asuc, sp->is_abad);
1015 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1016 michael 4040 "t :Client Server");
1017 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1018 michael 4040 "t :connected %u %u",
1019 michael 7330 sp->is_cl, sp->is_sv);
1020 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1021 michael 6782 "t :bytes sent %ju %ju",
1022 michael 3573 sp->is_cbs, sp->is_sbs);
1023 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1024 michael 6782 "t :bytes recv %ju %ju",
1025 michael 3573 sp->is_cbr, sp->is_sbr);
1026 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1027 michael 7330 "t :time connected %ju %ju",
1028     sp->is_cti, sp->is_sti);
1029 adx 30 }
1030    
1031     static void
1032 michael 1457 stats_uptime(struct Client *source_p, int parc, char *parv[])
1033 adx 30 {
1034 michael 4340 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_u_oper_only)
1035 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1036 michael 2269 else
1037     {
1038 michael 6569 sendto_one_numeric(source_p, &me, RPL_STATSUPTIME,
1039     time_dissect(CurrentTime - me.connection->since));
1040 michael 2269 if (!ConfigServerHide.disable_remote_commands || HasUMode(source_p, UMODE_OPER))
1041 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSCONN, Count.max_loc_con,
1042     Count.max_loc_cli, Count.totalrestartcount);
1043 michael 2269 }
1044 adx 30 }
1045    
1046     static void
1047 michael 1457 stats_shared(struct Client *source_p, int parc, char *parv[])
1048 adx 30 {
1049 michael 7209 report_shared(source_p);
1050     report_cluster(source_p);
1051 adx 30 }
1052    
1053     /* stats_servers()
1054     *
1055     * input - client pointer
1056     * output - none
1057     * side effects - client is shown lists of who connected servers
1058     */
1059     static void
1060 michael 1457 stats_servers(struct Client *source_p, int parc, char *parv[])
1061 adx 30 {
1062 michael 4815 const dlink_node *node = NULL;
1063 adx 30
1064 michael 4815 DLINK_FOREACH(node, local_server_list.head)
1065 adx 30 {
1066 michael 4815 const struct Client *target_p = node->data;
1067 adx 30
1068 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1069 michael 6846 "v :%s (%s!%s@%s) Idle: %s",
1070 michael 3573 target_p->name,
1071     (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1072 michael 6846 "*", "*", time_dissect(CurrentTime - target_p->connection->lasttime));
1073 adx 30 }
1074    
1075 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1076 michael 4213 "v :%u Server(s)",
1077     dlink_list_length(&local_server_list));
1078 adx 30 }
1079    
1080     static void
1081 michael 1457 stats_class(struct Client *source_p, int parc, char *parv[])
1082 adx 30 {
1083 michael 4815 const dlink_node *node = NULL;
1084 michael 2116
1085 michael 4815 DLINK_FOREACH(node, class_get_list()->head)
1086 michael 2116 {
1087 michael 4815 const struct ClassItem *class = node->data;
1088 michael 2116
1089 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSYLINE, 'Y',
1090     class->name, class->ping_freq,
1091     class->con_freq,
1092     class->max_total, class->max_sendq,
1093     class->max_recvq,
1094     class->ref_count,
1095     class->number_per_cidr, class->cidr_bitlen_ipv4,
1096     class->number_per_cidr, class->cidr_bitlen_ipv6,
1097     class->active ? "active" : "disabled");
1098 michael 2116 }
1099 adx 30 }
1100    
1101     static void
1102 michael 1457 stats_servlinks(struct Client *source_p, int parc, char *parv[])
1103 adx 30 {
1104 michael 6782 uintmax_t sendB = 0, recvB = 0;
1105 michael 7330 uintmax_t uptime = 0;
1106 michael 4815 dlink_node *node = NULL;
1107 adx 30
1108 michael 1219 if (ConfigServerHide.flatten_links && !HasUMode(source_p, UMODE_OPER))
1109 adx 30 {
1110 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1111 adx 30 return;
1112     }
1113    
1114 michael 4815 DLINK_FOREACH(node, local_server_list.head)
1115 adx 30 {
1116 michael 4815 struct Client *target_p = node->data;
1117 adx 30
1118 michael 1851 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
1119     if (!HasUMode(source_p, UMODE_OPER))
1120     continue;
1121    
1122 michael 4588 sendB += target_p->connection->send.bytes;
1123     recvB += target_p->connection->recv.bytes;
1124 adx 30
1125 michael 6782 /* ":%s 211 %s %s %u %u %ju %u %ju :%u %u %s" */
1126 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1127 michael 1219 get_client_name(target_p, HasUMode(source_p, UMODE_ADMIN) ? SHOW_IP : MASK_IP),
1128 michael 4588 dbuf_length(&target_p->connection->buf_sendq),
1129     target_p->connection->send.messages,
1130     target_p->connection->send.bytes >> 10,
1131     target_p->connection->recv.messages,
1132     target_p->connection->recv.bytes >> 10,
1133 michael 5531 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1134     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0,
1135 michael 1219 HasUMode(source_p, UMODE_OPER) ? show_capabilities(target_p) : "TS");
1136 adx 30 }
1137    
1138     sendB >>= 10;
1139     recvB >>= 10;
1140    
1141 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :%u total server(s)",
1142 michael 4213 dlink_list_length(&local_server_list));
1143 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Sent total: %7.2f %s",
1144 michael 3573 _GMKv(sendB), _GMKs(sendB));
1145 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Recv total: %7.2f %s",
1146 michael 3573 _GMKv(recvB), _GMKs(recvB));
1147 adx 30
1148 michael 4588 uptime = (CurrentTime - me.connection->since);
1149 adx 30
1150 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1151 michael 3573 "? :Server send: %7.2f %s (%4.1f K/s)",
1152 michael 7382 _GMKv((me.connection->send.bytes >> 10)),
1153     _GMKs((me.connection->send.bytes >> 10)),
1154 michael 4588 (float)((float)((me.connection->send.bytes) >> 10) /
1155 michael 3573 (float)uptime));
1156 michael 5583 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1157 michael 3573 "? :Server recv: %7.2f %s (%4.1f K/s)",
1158 michael 7382 _GMKv((me.connection->recv.bytes >> 10)),
1159     _GMKs((me.connection->recv.bytes >> 10)),
1160 michael 4588 (float)((float)((me.connection->recv.bytes) >> 10) /
1161 michael 3573 (float)uptime));
1162 adx 30 }
1163    
1164 michael 1457 /* parse_stats_args()
1165     *
1166     * inputs - arg count
1167     * - args
1168     * - doall flag
1169     * - wild card or not
1170     * output - pointer to name to use
1171     * side effects -
1172     * common parse routine for m_stats args
1173 michael 2820 *
1174 michael 1457 */
1175 michael 4755 static const char *
1176 michael 3114 parse_stats_args(struct Client *source_p, int parc, char *parv[], int *doall, int *wilds)
1177 adx 30 {
1178 michael 1457 if (parc > 2)
1179 adx 30 {
1180 michael 4755 const char *name = parv[2];
1181 adx 30
1182 michael 3114 if (!irccmp(name, ID_or_name(&me, source_p)))
1183 michael 1457 *doall = 2;
1184 michael 3114 else if (!match(name, ID_or_name(&me, source_p)))
1185 michael 1457 *doall = 1;
1186    
1187     *wilds = has_wildcards(name);
1188    
1189     return name;
1190 adx 30 }
1191    
1192 michael 1457 return NULL;
1193 adx 30 }
1194    
1195     static void
1196 michael 4755 stats_L_list(struct Client *source_p, const char *name, int doall, int wilds,
1197     dlink_list *list, const char statchar)
1198 adx 30 {
1199 michael 4815 dlink_node *node = NULL;
1200 adx 30
1201     /*
1202 michael 4034 * Send info about connections which match, or all if the
1203     * mask matches from. Only restrictions are on those who
1204 adx 30 * are invisible not being visible to 'foreigners' who use
1205     * a wild card based search to list it.
1206     */
1207 michael 4815 DLINK_FOREACH(node, list->head)
1208 adx 30 {
1209 michael 4815 struct Client *target_p = node->data;
1210 adx 30
1211 michael 1219 if (HasUMode(target_p, UMODE_INVISIBLE) && (doall || wilds) &&
1212 michael 1457 !(MyConnect(source_p) && HasUMode(source_p, UMODE_OPER)) &&
1213     !HasUMode(target_p, UMODE_OPER) && (target_p != source_p))
1214 adx 30 continue;
1215 michael 4034
1216 michael 1652 if (!doall && wilds && match(name, target_p->name))
1217 adx 30 continue;
1218 michael 4034
1219 adx 30 if (!(doall || wilds) && irccmp(name, target_p->name))
1220     continue;
1221    
1222 michael 4034 /*
1223     * This basically shows ips for our opers if it's not a server/admin, or
1224 michael 4298 * it's one of our admins.
1225 michael 4034 */
1226 michael 7051 if (MyConnect(source_p) && HasUMode(source_p, UMODE_OPER) &&
1227 michael 3655 (HasUMode(source_p, UMODE_ADMIN) ||
1228     (!IsServer(target_p) && !HasUMode(target_p, UMODE_ADMIN) &&
1229     !IsHandshake(target_p) && !IsConnecting(target_p))))
1230 adx 30 {
1231 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1232 adx 30 (IsUpper(statchar)) ?
1233     get_client_name(target_p, SHOW_IP) :
1234     get_client_name(target_p, HIDE_IP),
1235 michael 4588 dbuf_length(&target_p->connection->buf_sendq),
1236     target_p->connection->send.messages,
1237 michael 7382 target_p->connection->send.bytes >> 10,
1238 michael 4588 target_p->connection->recv.messages,
1239 michael 7382 target_p->connection->recv.bytes >> 10,
1240 michael 5531 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1241     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0,
1242 adx 30 IsServer(target_p) ? show_capabilities(target_p) : "-");
1243     }
1244     else
1245     {
1246 michael 4967 /* If it's a server, mask the real IP */
1247     if (IsServer(target_p) || IsHandshake(target_p) || IsConnecting(target_p))
1248 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1249 michael 1457 get_client_name(target_p, MASK_IP),
1250 michael 4588 dbuf_length(&target_p->connection->buf_sendq),
1251     target_p->connection->send.messages,
1252 michael 7382 target_p->connection->send.bytes >> 10,
1253 michael 4588 target_p->connection->recv.messages,
1254 michael 7382 target_p->connection->recv.bytes >> 10,
1255 michael 5531 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1256     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0,
1257 michael 1457 IsServer(target_p) ? show_capabilities(target_p) : "-");
1258 adx 30 else /* show the real IP */
1259 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1260 michael 1457 (IsUpper(statchar)) ?
1261     get_client_name(target_p, SHOW_IP) :
1262     get_client_name(target_p, HIDE_IP),
1263 michael 4588 dbuf_length(&target_p->connection->buf_sendq),
1264     target_p->connection->send.messages,
1265 michael 7382 target_p->connection->send.bytes >> 10,
1266 michael 4588 target_p->connection->recv.messages,
1267 michael 7382 target_p->connection->recv.bytes >> 10,
1268 michael 5531 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1269     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0,
1270 michael 1457 IsServer(target_p) ? show_capabilities(target_p) : "-");
1271 adx 30 }
1272     }
1273     }
1274    
1275 michael 1457 /*
1276     * stats_L
1277 adx 30 *
1278 michael 1457 * inputs - pointer to client to report to
1279     * - doall flag
1280     * - wild card or not
1281     * output - NONE
1282     * side effects -
1283     */
1284     static void
1285 michael 4755 stats_L(struct Client *source_p, const char *name, int doall,
1286     int wilds, const char statchar)
1287 michael 1457 {
1288     stats_L_list(source_p, name, doall, wilds, &unknown_list, statchar);
1289     stats_L_list(source_p, name, doall, wilds, &local_client_list, statchar);
1290 michael 4213 stats_L_list(source_p, name, doall, wilds, &local_server_list, statchar);
1291 michael 1457 }
1292    
1293     static void
1294     stats_ltrace(struct Client *source_p, int parc, char *parv[])
1295     {
1296     int doall = 0;
1297     int wilds = 0;
1298 michael 4755 const char *name = NULL;
1299 michael 1457
1300 michael 3246 if ((name = parse_stats_args(source_p, parc, parv, &doall, &wilds)))
1301 michael 1457 {
1302 michael 4755 const char statchar = *parv[1];
1303 michael 1457 stats_L(source_p, name, doall, wilds, statchar);
1304     }
1305     else
1306 michael 3109 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "STATS");
1307 michael 1457 }
1308    
1309 michael 6552 struct StatsStruct
1310 michael 1457 {
1311     const unsigned char letter;
1312 michael 4479 void (*handler)(struct Client *, int, char *[]);
1313 michael 6552 const unsigned int required_modes;
1314 michael 1457 };
1315    
1316 michael 6552 static const struct StatsStruct *stats_map[256];
1317     static const struct StatsStruct stats_tab[] =
1318     {
1319     { 'a', stats_dns_servers, UMODE_ADMIN },
1320     { 'A', stats_dns_servers, UMODE_ADMIN },
1321     { 'c', stats_connect, UMODE_OPER },
1322     { 'C', stats_connect, UMODE_OPER },
1323     { 'd', stats_tdeny, UMODE_OPER },
1324     { 'D', stats_deny, UMODE_OPER },
1325     { 'e', stats_exempt, UMODE_OPER },
1326     { 'E', stats_events, UMODE_ADMIN },
1327     { 'f', fd_dump, UMODE_ADMIN },
1328     { 'F', fd_dump, UMODE_ADMIN },
1329     { 'h', stats_hubleaf, UMODE_OPER },
1330     { 'H', stats_hubleaf, UMODE_OPER },
1331     { 'i', stats_auth, 0 },
1332     { 'I', stats_auth, 0 },
1333     { 'k', stats_tklines, 0 },
1334     { 'K', stats_klines, 0 },
1335     { 'l', stats_ltrace, UMODE_OPER },
1336     { 'L', stats_ltrace, UMODE_OPER },
1337     { 'm', stats_messages, 0 },
1338     { 'M', stats_messages, 0 },
1339 michael 7405 { 'o', stats_operator, 0 },
1340     { 'O', stats_operator, 0 },
1341 michael 6552 { 'p', stats_operedup, 0 },
1342     { 'P', stats_ports, 0 },
1343     { 'q', stats_resv, UMODE_OPER },
1344     { 'Q', stats_resv, UMODE_OPER },
1345     { 's', stats_service, UMODE_OPER },
1346     { 'S', stats_service, UMODE_OPER },
1347     { 't', stats_tstats, UMODE_OPER },
1348     { 'T', motd_report, UMODE_OPER },
1349     { 'u', stats_uptime, 0 },
1350     { 'U', stats_shared, UMODE_OPER },
1351     { 'v', stats_servers, UMODE_OPER },
1352     { 'x', stats_gecos, UMODE_OPER },
1353     { 'X', stats_gecos, UMODE_OPER },
1354     { 'y', stats_class, UMODE_OPER },
1355     { 'Y', stats_class, UMODE_OPER },
1356     { 'z', stats_memory, UMODE_OPER },
1357     { '?', stats_servlinks, 0 },
1358     { '\0', NULL, 0 }
1359     };
1360    
1361 michael 1457 static void
1362     do_stats(struct Client *source_p, int parc, char *parv[])
1363     {
1364 michael 6552 const unsigned char statchar = *parv[1];
1365     const struct StatsStruct *tab;
1366 michael 1457
1367     if (statchar == '\0')
1368     {
1369 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, '*');
1370 michael 1457 return;
1371     }
1372    
1373 michael 6552 if ((tab = stats_map[statchar]))
1374 michael 1457 {
1375 michael 6552 if (!tab->required_modes || HasUMode(source_p, tab->required_modes))
1376     tab->handler(source_p, parc, parv);
1377     else
1378     sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1379 michael 1457
1380 michael 6552 sendto_realops_flags(UMODE_SPY, L_ALL, SEND_NOTICE,
1381     "STATS %c requested by %s (%s@%s) [%s]",
1382     statchar, source_p->name, source_p->username,
1383     source_p->host, source_p->servptr->name);
1384 michael 1457 }
1385    
1386 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, statchar);
1387 michael 1457 }
1388    
1389     /*
1390     * m_stats()
1391 michael 3096 * parv[0] = command
1392 michael 1457 * parv[1] = stat letter/command
1393     * parv[2] = (if present) server/mask in stats L
1394 adx 30 */
1395 michael 2820 static int
1396 michael 3156 m_stats(struct Client *source_p, int parc, char *parv[])
1397 adx 30 {
1398 michael 7330 static uintmax_t last_used = 0;
1399 adx 30
1400 michael 4298 /* Check the user is actually allowed to do /stats, and isn't flooding */
1401 michael 4340 if ((last_used + ConfigGeneral.pace_wait) > CurrentTime)
1402 michael 1457 {
1403 michael 4759 sendto_one_numeric(source_p, &me, RPL_LOAD2HI, "STATS");
1404 michael 2820 return 0;
1405 michael 1457 }
1406 adx 30
1407 michael 1457 last_used = CurrentTime;
1408 adx 30
1409 michael 2341 /* Is the stats meant for us? */
1410     if (!ConfigServerHide.disable_remote_commands)
1411 michael 4034 if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
1412 michael 2820 return 0;
1413 michael 2341
1414 michael 1457 do_stats(source_p, parc, parv);
1415 michael 2820 return 0;
1416 michael 1457 }
1417    
1418     /*
1419 michael 3219 * ms_stats()
1420 michael 3096 * parv[0] = command
1421 michael 1457 * parv[1] = stat letter/command
1422     * parv[2] = (if present) server/mask in stats L, or target
1423     */
1424 michael 2820 static int
1425 michael 3219 ms_stats(struct Client *source_p, int parc, char *parv[])
1426 michael 1457 {
1427 michael 4034 if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
1428 michael 2820 return 0;
1429 michael 1457
1430     do_stats(source_p, parc, parv);
1431 michael 2820 return 0;
1432 adx 30 }
1433 michael 1230
1434 michael 6552 static void
1435     stats_init(void)
1436     {
1437     for (const struct StatsStruct *tab = stats_tab; tab->letter; ++tab)
1438     stats_map[tab->letter] = tab;
1439     }
1440    
1441 michael 2820 static struct Message stats_msgtab =
1442     {
1443 michael 5881 .cmd = "STATS",
1444     .args_min = 2,
1445     .args_max = MAXPARA,
1446     .handlers[UNREGISTERED_HANDLER] = m_unregistered,
1447     .handlers[CLIENT_HANDLER] = m_stats,
1448     .handlers[SERVER_HANDLER] = ms_stats,
1449     .handlers[ENCAP_HANDLER] = m_ignore,
1450     .handlers[OPER_HANDLER] = ms_stats
1451 michael 1230 };
1452    
1453     static void
1454     module_init(void)
1455     {
1456 michael 6552 stats_init();
1457 michael 1230 mod_add_cmd(&stats_msgtab);
1458     }
1459    
1460     static void
1461     module_exit(void)
1462     {
1463     mod_del_cmd(&stats_msgtab);
1464     }
1465    
1466 michael 2820 struct module module_entry =
1467     {
1468 michael 1230 .version = "$Revision$",
1469     .modinit = module_init,
1470     .modexit = module_exit,
1471     };

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision