ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_stats.c
Revision: 6716
Committed: Fri Oct 30 17:37:27 2015 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 49259 byte(s)
Log Message:
- Rename 'xconf_item' list to 'gecos_items'

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

Properties

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