ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/modules/m_stats.c
Revision: 6834
Committed: Tue Nov 24 17:20:25 2015 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 49423 byte(s)
Log Message:
- Move report_dns_servers() from res.c to m_stats.c

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 5346 * 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 4564 * 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 6834 #include "reslib.h"
50 michael 2150 #include "motd.h"
51 michael 4326 #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 4816 const dlink_node *node = NULL;
81 michael 4033 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 4035 case CONF_XLINE:
88 michael 6717 DLINK_FOREACH(node, gecos_items.head)
89 michael 4035 {
90 michael 4816 conf = node->data;
91 michael 4035 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 4035 break;
97 michael 1922
98 michael 6627 case CONF_SHARED:
99     DLINK_FOREACH(node, shared_items.head)
100 michael 4035 {
101 michael 4816 conf = node->data;
102 michael 4035 p = buf;
103 michael 1922
104 michael 4035 *p++ = 'c';
105 michael 1922
106 michael 4035 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 4035 *p = '\0';
113 michael 3285
114 michael 4035 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 4816 DLINK_FOREACH(node, cluster_items.head)
120 michael 4035 {
121 michael 4816 conf = node->data;
122 michael 4035 p = buf;
123 michael 1922
124 michael 4035 *p++ = 'C';
125 michael 1922
126 michael 4035 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 4035 *p = '\0';
133 michael 3285
134 michael 4035 sendto_one_numeric(source_p, &me, RPL_STATSULINE, conf->name, "*", "*", buf);
135     }
136 michael 1922
137 michael 4035 break;
138 michael 1922
139 michael 4035 case CONF_OPER:
140 michael 6627 DLINK_FOREACH(node, operator_items.head)
141 michael 4035 {
142 michael 4816 conf = node->data;
143 michael 1922
144 michael 4035 /* 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 4035 break;
155 michael 1922
156 michael 4035 case CONF_SERVICE:
157 michael 4816 DLINK_FOREACH(node, service_items.head)
158 michael 4035 {
159 michael 4816 conf = node->data;
160 michael 4035 sendto_one_numeric(source_p, &me, RPL_STATSSERVICE, 'S', "*", conf->name, 0, 0);
161     }
162 michael 1922
163 michael 4035 break;
164 michael 1922
165 michael 4035 case CONF_SERVER:
166 michael 4816 DLINK_FOREACH(node, server_items.head)
167 michael 4035 {
168     p = buf;
169 michael 4816 conf = node->data;
170 michael 1922
171 michael 4035 buf[0] = '\0';
172 michael 1922
173 michael 4035 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 4035 "*@127.0.0.1", buf, conf->name, conf->port,
192     conf->class->name);
193     }
194 michael 1922
195 michael 4035 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 4816 const dlink_node *node = NULL;
212 michael 1927
213 michael 4816 DLINK_FOREACH(node, cresv_items.head)
214 michael 1927 {
215 michael 4816 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 4816 DLINK_FOREACH(node, nresv_items.head)
223 michael 1927 {
224 michael 4816 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 4299 * 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 4589 rup = (CurrentTime - me.connection->since) * hzz;
268 adx 30
269     if (rup == 0)
270     rup = 1;
271    
272 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
273 michael 3574 "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 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
278 michael 3574 "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 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
283 michael 3574 "R :Swaps %d Reclaims %d Faults %d",
284     (int)rus.ru_nswap,
285     (int)rus.ru_minflt, (int)rus.ru_majflt);
286 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
287 michael 3574 "R :Block in %d out %d",
288     (int)rus.ru_inblock,
289     (int)rus.ru_oublock);
290 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
291 michael 3574 "R :Msg Rcv %d Send %d",
292     (int)rus.ru_msgrcv,
293     (int)rus.ru_msgsnd);
294 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
295 michael 3574 "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 4330 unsigned int number_ips_stored = 0; /* number of ip addresses hashed */
319 adx 30
320 michael 6718 size_t channel_memory = 0;
321     size_t channel_ban_memory = 0;
322     size_t channel_except_memory = 0;
323     size_t channel_invex_memory = 0;
324 adx 30
325     unsigned int safelist_count = 0;
326 michael 6718 size_t safelist_memory = 0;
327 adx 30
328 michael 6718 size_t wwm = 0; /* whowas array memory used */
329     size_t conf_memory = 0; /* memory used by conf lines */
330     size_t mem_ips_stored = 0; /* memory used by ip address hash */
331 adx 30
332 michael 6718 size_t total_channel_memory = 0;
333     size_t totww = 0;
334 adx 30
335     unsigned int local_client_count = 0;
336     unsigned int remote_client_count = 0;
337    
338 michael 6718 size_t local_client_memory_used = 0;
339     size_t remote_client_memory_used = 0;
340 adx 30
341 michael 6718 size_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 michael 6718 size_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 4033 const struct Client *target_p = gptr->data;
352 adx 30
353     if (MyConnect(target_p))
354     {
355     ++local_client_count;
356 michael 4589 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 5758 channel_memory = dlink_list_length(&channel_list) * sizeof(struct Channel);
373    
374 michael 3945 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 4589 DLINK_FOREACH(dlink, acptr->connection->list_task->show_mask.head)
402 adx 30 safelist_memory += strlen(dlink->data);
403    
404 michael 4589 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 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
428 michael 6781 "z :WATCH headers %u(%zu) entries %d(%u)",
429 michael 3574 watch_list_headers,
430     watch_list_memory, watch_list_entries,
431     watch_list_entries * sizeof(dlink_node) * 2);
432 michael 884
433 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
434 michael 3574 "z :Clients %u(%u)",
435     users_counted,
436     (users_counted * sizeof(struct Client)));
437 adx 30
438 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
439 michael 3574 "z :User aways %u", aways_counted);
440 adx 30
441 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
442 michael 6718 "z :Attached confs %u(%zu)",
443 michael 3574 local_client_conf_count,
444 michael 6718 local_client_conf_count * sizeof(dlink_node));
445 adx 30
446 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
447 michael 6718 "z :Resv channels %u(%zu) nicks %u(%zu)",
448 michael 3574 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 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
454 michael 6718 "z :Classes %u(%zu)",
455     class_count, class_count * sizeof(struct ClassItem));
456 adx 30
457 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
458 michael 6718 "z :Channels %u(%zu) Topics %u(%u)",
459 michael 3945 dlink_list_length(&channel_list),
460 michael 3574 channel_memory, topic_count, topic_count *
461     (TOPICLEN + 1 + USERHOST_REPLYLEN));
462 adx 30
463 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
464 michael 6718 "z :Bans %u(%zu)",
465 michael 3574 channel_bans, channel_ban_memory);
466 adx 30
467 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
468 michael 6718 "z :Exceptions %u(%zu)",
469 michael 3574 channel_except, channel_except_memory);
470 adx 30
471 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
472 michael 6718 "z :Invex %u(%zu)",
473 michael 3574 channel_invex, channel_invex_memory);
474 adx 30
475 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
476 michael 6718 "z :Channel members %u(%zu) invites %u(%zu)",
477 michael 3574 channel_members,
478 michael 6718 channel_members * sizeof(struct Membership),
479     channel_invites,
480     channel_invites * sizeof(dlink_node) * 2);
481 adx 30
482     total_channel_memory = channel_memory + channel_ban_memory +
483 michael 1017 channel_members * sizeof(struct Membership) +
484 michael 6718 (channel_invites * sizeof(dlink_node) * 2);
485 adx 30
486 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
487 michael 6718 "z :Safelist %u(%zu)",
488 michael 3574 safelist_count, safelist_memory);
489 adx 30
490 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
491 michael 6718 "z :Whowas users %u(%zu)",
492     wwu, wwu * sizeof(struct Client));
493 adx 30
494 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
495 michael 6718 "z :Whowas array %u(%zu)",
496 michael 3574 NICKNAMEHISTORYLENGTH, wwm);
497 adx 30
498     totww = wwu * sizeof(struct Client) + wwm;
499    
500 michael 2150 motd_memory_count(source_p);
501 michael 4326 ipcache_get_stats(&number_ips_stored, &mem_ips_stored);
502 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
503 michael 6718 "z :iphash %u(%zu)",
504 michael 3574 number_ips_stored, mem_ips_stored);
505 adx 30
506     total_memory = totww + total_channel_memory + conf_memory + class_count *
507     sizeof(struct ClassItem);
508 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
509 michael 6718 "z :Total: whowas %zu channel %zu conf %zu",
510 michael 3574 totww, total_channel_memory, conf_memory);
511 adx 30
512 michael 4577 local_client_memory_used = local_client_count*(sizeof(struct Client) + sizeof(struct Connection));
513 adx 30 total_memory += local_client_memory_used;
514 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
515 michael 6718 "z :Local client Memory in use: %u(%zu)",
516 michael 3574 local_client_count, local_client_memory_used);
517 adx 30
518     remote_client_memory_used = remote_client_count * sizeof(struct Client);
519     total_memory += remote_client_memory_used;
520 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
521 michael 6718 "z :Remote client Memory in use: %u(%zu)",
522 michael 3574 remote_client_count, remote_client_memory_used);
523 adx 30
524 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
525 michael 6718 "z :TOTAL: %zu",
526 michael 3574 me.name, RPL_STATSDEBUG, source_p->name,
527     total_memory);
528 adx 30 }
529    
530     static void
531 michael 4478 stats_dns_servers(struct Client *source_p, int parc, char *parv[])
532 adx 30 {
533 michael 6834 char ipaddr[HOSTIPLEN + 1] = "";
534    
535     for (unsigned int i = 0; i < irc_nscount; ++i)
536     {
537     getnameinfo((const struct sockaddr *)&(irc_nsaddr_list[i]),
538     irc_nsaddr_list[i].ss_len, ipaddr,
539     sizeof(ipaddr), NULL, 0, NI_NUMERICHOST);
540     sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr);
541     }
542 adx 30 }
543    
544     static void
545 michael 1457 stats_connect(struct Client *source_p, int parc, char *parv[])
546 adx 30 {
547 michael 1632 report_confitem_types(source_p, CONF_SERVER);
548 adx 30 }
549    
550     /* stats_deny()
551     *
552     * input - client to report to
553     * output - none
554     * side effects - client is given dline list.
555     */
556     static void
557 michael 1457 stats_deny(struct Client *source_p, int parc, char *parv[])
558 adx 30 {
559 michael 4033 const struct MaskItem *conf = NULL;
560 michael 4816 const dlink_node *node = NULL;
561 adx 30
562 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
563 adx 30 {
564 michael 4816 DLINK_FOREACH(node, atable[i].head)
565 adx 30 {
566 michael 4816 const struct AddressRec *arec = node->data;
567 michael 1367
568 michael 1632 if (arec->type != CONF_DLINE)
569     continue;
570 adx 30
571 michael 1632 conf = arec->conf;
572 adx 30
573 michael 5541 /* Don't report a temporary dline as permanent dline */
574 michael 1649 if (conf->until)
575 michael 1632 continue;
576 adx 30
577 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'D', conf->host, conf->reason);
578 adx 30 }
579     }
580     }
581    
582     /* stats_tdeny()
583     *
584     * input - client to report to
585     * output - none
586     * side effects - client is given dline list.
587     */
588     static void
589 michael 1457 stats_tdeny(struct Client *source_p, int parc, char *parv[])
590 adx 30 {
591 michael 4033 const struct MaskItem *conf = NULL;
592 michael 4816 const dlink_node *node = NULL;
593 adx 30
594 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
595 adx 30 {
596 michael 4816 DLINK_FOREACH(node, atable[i].head)
597 adx 30 {
598 michael 4816 const struct AddressRec *arec = node->data;
599 michael 1367
600 michael 1632 if (arec->type != CONF_DLINE)
601     continue;
602 adx 30
603 michael 1632 conf = arec->conf;
604 adx 30
605 michael 5541 /* Don't report a permanent dline as temporary dline */
606 michael 1649 if (!conf->until)
607 michael 1632 continue;
608 adx 30
609 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'd', conf->host, conf->reason);
610 adx 30 }
611     }
612     }
613    
614     /* stats_exempt()
615     *
616     * input - client to report to
617     * output - none
618     * side effects - client is given list of exempt blocks
619     */
620     static void
621 michael 1457 stats_exempt(struct Client *source_p, int parc, char *parv[])
622 adx 30 {
623 michael 4035 const struct MaskItem *conf = NULL;
624 michael 4816 const dlink_node *node = NULL;
625 adx 30
626 michael 4341 if (ConfigGeneral.stats_e_disabled)
627 michael 584 {
628 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
629 michael 584 return;
630     }
631    
632 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
633 adx 30 {
634 michael 4816 DLINK_FOREACH(node, atable[i].head)
635 adx 30 {
636 michael 4816 const struct AddressRec *arec = node->data;
637 michael 1367
638 michael 1632 if (arec->type != CONF_EXEMPT)
639     continue;
640 adx 30
641 michael 1632 conf = arec->conf;
642 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSDLINE, 'e', conf->host, "");
643 adx 30 }
644     }
645     }
646    
647     static void
648 michael 1457 stats_events(struct Client *source_p, int parc, char *parv[])
649 adx 30 {
650 michael 6305 const dlink_node *node;
651    
652     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
653     "E :Operation Next Execution");
654     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
655     "E :---------------------------------------------");
656    
657     DLINK_FOREACH(node, event_get_list()->head)
658     {
659     const struct event *ev = node->data;
660    
661     sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
662     "E :%-30s %-4d seconds",
663     ev->name,
664     (int)(ev->next - CurrentTime));
665     }
666 adx 30 }
667    
668     static void
669 michael 1457 stats_hubleaf(struct Client *source_p, int parc, char *parv[])
670 adx 30 {
671 michael 4816 const dlink_node *node = NULL, *dptr = NULL;
672 michael 2118
673 michael 4816 DLINK_FOREACH(node, server_items.head)
674 michael 2118 {
675 michael 4816 const struct MaskItem *conf = node->data;
676 michael 2118
677     DLINK_FOREACH(dptr, conf->hub_list.head)
678 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSHLINE, 'H', dptr->data, conf->name, 0, "*");
679 michael 2118 }
680    
681 michael 4816 DLINK_FOREACH(node, server_items.head)
682 michael 2118 {
683 michael 4816 const struct MaskItem *conf = node->data;
684 michael 2118
685     DLINK_FOREACH(dptr, conf->leaf_list.head)
686 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLLINE, 'L', dptr->data, conf->name, 0, "*");
687 michael 2118 }
688 adx 30 }
689    
690 michael 1372 /*
691     * show_iline_prefix()
692     *
693     * inputs - pointer to struct Client requesting output
694 michael 2820 * - pointer to struct MaskItem
695 michael 1372 * - name to which iline prefix will be prefixed to
696     * output - pointer to static string with prefixes listed in ascii form
697     * side effects - NONE
698     */
699     static const char *
700 michael 6507 show_iline_prefix(const struct Client *source_p, const struct MaskItem *conf)
701 michael 1372 {
702 michael 5986 static char prefix_of_host[USERLEN + 16];
703 michael 1372 char *prefix_ptr = prefix_of_host;
704    
705 michael 1715 if (IsConfWebIRC(conf))
706     *prefix_ptr++ = '<';
707 michael 1632 if (IsNoTilde(conf))
708 michael 1372 *prefix_ptr++ = '-';
709 michael 1632 if (IsNeedIdentd(conf))
710 michael 1372 *prefix_ptr++ = '+';
711 michael 1632 if (!IsNeedPassword(conf))
712 michael 1372 *prefix_ptr++ = '&';
713 michael 1632 if (IsConfExemptResv(conf))
714 michael 1372 *prefix_ptr++ = '$';
715 michael 1632 if (IsConfDoSpoofIp(conf))
716 michael 1372 *prefix_ptr++ = '=';
717 michael 6507 if (HasUMode(source_p, UMODE_OPER))
718     {
719     if (IsConfExemptKline(conf))
720     *prefix_ptr++ = '^';
721     if (IsConfExemptXline(conf))
722     *prefix_ptr++ = '!';
723     if (IsConfExemptLimits(conf))
724     *prefix_ptr++ = '>';
725     }
726    
727 michael 1632 if (IsConfCanFlood(conf))
728 michael 1372 *prefix_ptr++ = '|';
729    
730 michael 1632 strlcpy(prefix_ptr, conf->user, USERLEN+1);
731 michael 1372
732     return prefix_of_host;
733     }
734    
735 adx 30 static void
736 michael 3156 report_auth(struct Client *source_p, int parc, char *parv[])
737 michael 1372 {
738 michael 4033 const struct MaskItem *conf = NULL;
739 michael 4816 const dlink_node *node = NULL;
740 michael 1372
741 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
742 michael 1372 {
743 michael 4816 DLINK_FOREACH(node, atable[i].head)
744 michael 1372 {
745 michael 4816 const struct AddressRec *arec = node->data;
746 michael 1372
747 michael 1632 if (arec->type != CONF_CLIENT)
748     continue;
749 michael 1372
750 michael 1632 conf = arec->conf;
751 michael 1372
752 michael 6507 if (!HasUMode(source_p, UMODE_OPER) && IsConfDoSpoofIp(conf))
753 michael 1632 continue;
754 michael 1372
755 michael 4968 sendto_one_numeric(source_p, &me, RPL_STATSILINE, 'I',
756     conf->name == NULL ? "*" : conf->name,
757     show_iline_prefix(source_p, conf),
758     conf->host, conf->port,
759     conf->class->name);
760 michael 1372 }
761     }
762     }
763    
764     static void
765 michael 1457 stats_auth(struct Client *source_p, int parc, char *parv[])
766 adx 30 {
767     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
768 michael 4341 if (ConfigGeneral.stats_i_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
769 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
770 adx 30
771 michael 4035 /* If unopered, only return matching auth blocks */
772 michael 4341 else if (ConfigGeneral.stats_i_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
773 adx 30 {
774 michael 4035 const struct MaskItem *conf = NULL;
775 adx 30
776     if (MyConnect(source_p))
777 michael 1632 conf = find_conf_by_address(source_p->host,
778 michael 4589 &source_p->connection->ip, CONF_CLIENT,
779     source_p->connection->aftype,
780 michael 2820 source_p->username,
781 michael 4589 source_p->connection->password, 1);
782 adx 30 else
783 michael 4035 conf = find_conf_by_address(source_p->host, NULL, CONF_CLIENT, 0,
784     source_p->username, NULL, 1);
785 adx 30
786 michael 1632 if (conf == NULL)
787 adx 30 return;
788    
789 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSILINE,
790     'I', "*", show_iline_prefix(source_p, conf),
791     conf->host, conf->port,
792 michael 4036 conf->class->name);
793 adx 30 }
794 michael 4035 else /* They are opered, or allowed to see all auth blocks */
795 michael 1457 report_auth(source_p, 0, NULL);
796 adx 30 }
797    
798 michael 1372 /* report_Klines()
799     * Inputs: Client to report to,
800     * type(==0 for perm, !=0 for temporary)
801 michael 2820 * mask
802 michael 1372 * Output: None
803 michael 3156 * Side effects: Reports configured K(or k)-lines to source_p.
804 michael 1372 */
805 adx 30 static void
806 michael 3156 report_Klines(struct Client *source_p, int tkline)
807 michael 1372 {
808 michael 4033 const struct MaskItem *conf = NULL;
809 michael 4816 const dlink_node *node = NULL;
810 michael 1921 char c = '\0';
811 michael 1372
812     if (tkline)
813 michael 1921 c = 'k';
814 michael 1372 else
815 michael 1921 c = 'K';
816 michael 1372
817 michael 3246 for (unsigned int i = 0; i < ATABLE_SIZE; ++i)
818 michael 1372 {
819 michael 4816 DLINK_FOREACH(node, atable[i].head)
820 michael 1372 {
821 michael 4816 const struct AddressRec *arec = node->data;
822 michael 1372
823 michael 1632 if (arec->type != CONF_KLINE)
824     continue;
825 michael 1628
826 michael 1632 conf = arec->conf;
827 michael 1372
828 michael 1650 if ((!tkline && conf->until) ||
829     (tkline && !conf->until))
830 michael 1632 continue;
831    
832 michael 4569 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, c, conf->host, conf->user,
833     conf->reason);
834 michael 1372 }
835     }
836     }
837    
838     static void
839 michael 1457 stats_tklines(struct Client *source_p, int parc, char *parv[])
840 adx 30 {
841     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
842 michael 4341 if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
843 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
844 adx 30
845 michael 4035 /* If unopered, only return matching klines */
846 michael 4341 else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
847 adx 30 {
848 michael 4033 const struct MaskItem *conf = NULL;
849 adx 30
850     if (MyConnect(source_p))
851 michael 1632 conf = find_conf_by_address(source_p->host,
852 michael 4589 &source_p->connection->ip, CONF_KLINE,
853     source_p->connection->aftype,
854 michael 2820 source_p->username, NULL, 1);
855 adx 30 else
856 michael 4035 conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0,
857     source_p->username, NULL, 1);
858 adx 30
859 michael 1632 if (!conf)
860 adx 30 return;
861    
862 michael 5541 /* Don't report a permanent kline as temporary kline */
863 michael 1649 if (!conf->until)
864 adx 30 return;
865    
866 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'k',
867     conf->host, conf->user, conf->reason);
868 adx 30 }
869 michael 4035 else /* They are opered, or allowed to see all klines */
870 adx 30 report_Klines(source_p, 1);
871     }
872    
873     static void
874 michael 1457 stats_klines(struct Client *source_p, int parc, char *parv[])
875 adx 30 {
876     /* Oper only, if unopered, return ERR_NOPRIVILEGES */
877 michael 4341 if (ConfigGeneral.stats_k_oper_only == 2 && !HasUMode(source_p, UMODE_OPER))
878 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
879 adx 30
880 michael 4035 /* If unopered, only return matching klines */
881 michael 4341 else if (ConfigGeneral.stats_k_oper_only == 1 && !HasUMode(source_p, UMODE_OPER))
882 adx 30 {
883 michael 4033 const struct MaskItem *conf = NULL;
884 adx 30
885 michael 4035 /* Search for a kline */
886 michael 560 if (MyConnect(source_p))
887 michael 1632 conf = find_conf_by_address(source_p->host,
888 michael 4589 &source_p->connection->ip, CONF_KLINE,
889     source_p->connection->aftype,
890 michael 2820 source_p->username, NULL, 0);
891 adx 30 else
892 michael 4035 conf = find_conf_by_address(source_p->host, NULL, CONF_KLINE, 0,
893     source_p->username, NULL, 0);
894 adx 30
895 michael 1632 if (!conf)
896 adx 30 return;
897    
898 michael 5541 /* Don't report a temporary kline as permanent kline */
899 michael 1649 if (conf->until)
900 adx 30 return;
901 michael 2820
902 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSKLINE, 'K',
903     conf->host, conf->user, conf->reason);
904 adx 30 }
905 michael 4035 else /* They are opered, or allowed to see all klines */
906 adx 30 report_Klines(source_p, 0);
907     }
908    
909     static void
910 michael 1457 stats_messages(struct Client *source_p, int parc, char *parv[])
911 adx 30 {
912 michael 5027 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_m_oper_only)
913     sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
914     else
915     report_messages(source_p);
916 adx 30 }
917    
918     static void
919 michael 1457 stats_oper(struct Client *source_p, int parc, char *parv[])
920 adx 30 {
921 michael 4341 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_o_oper_only)
922 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
923 adx 30 else
924 michael 1632 report_confitem_types(source_p, CONF_OPER);
925 adx 30 }
926    
927     /* stats_operedup()
928     *
929     * input - client pointer
930     * output - none
931     * side effects - client is shown a list of active opers
932     */
933     static void
934 michael 1457 stats_operedup(struct Client *source_p, int parc, char *parv[])
935 adx 30 {
936 michael 4816 const dlink_node *node = NULL;
937 michael 3512 unsigned int opercount = 0;
938 michael 3517 char buf[IRCD_BUFSIZE] = "";
939 adx 30
940 michael 4816 DLINK_FOREACH(node, oper_list.head)
941 adx 30 {
942 michael 4816 const struct Client *target_p = node->data;
943 adx 30
944 michael 1294 if (HasUMode(target_p, UMODE_HIDDEN) && !HasUMode(source_p, UMODE_OPER))
945 adx 30 continue;
946    
947 michael 3517 if (HasUMode(source_p, UMODE_OPER) || !HasUMode(target_p, UMODE_HIDEIDLE))
948 michael 5543 snprintf(buf, sizeof(buf), "%u", client_get_idle_time(source_p, target_p));
949 michael 3517 else
950     strlcpy(buf, "n/a", sizeof(buf));
951    
952 michael 1219 if (MyClient(source_p) && HasUMode(source_p, UMODE_OPER))
953 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
954 michael 3574 "p :[%c][%s] %s (%s@%s) Idle: %s",
955     HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O',
956 michael 4589 oper_privs_as_string(target_p->connection->operflags),
957 michael 3574 target_p->name, target_p->username, target_p->host, buf);
958 adx 30 else
959 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
960 michael 3574 "p :[%c] %s (%s@%s) Idle: %s",
961     HasUMode(target_p, UMODE_ADMIN) ? 'A' : 'O',
962     target_p->name, target_p->username, target_p->host, buf);
963 michael 3512 ++opercount;
964 adx 30 }
965    
966 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
967 michael 3574 "p :%u OPER(s)", opercount);
968 adx 30 }
969    
970 michael 6365 /* show_ports()
971     *
972     * inputs - pointer to client to show ports to
973     * output - none
974     * side effects - send port listing to a client
975     */
976 adx 30 static void
977 michael 6365 show_ports(struct Client *source_p)
978     {
979     char buf[IRCD_BUFSIZE] = "";
980     char *p = NULL;
981     const dlink_node *node = NULL;
982    
983     DLINK_FOREACH(node, listener_get_list()->head)
984     {
985     const struct Listener *listener = node->data;
986     p = buf;
987    
988     if (listener->flags & LISTENER_HIDDEN)
989     {
990     if (!HasUMode(source_p, UMODE_ADMIN))
991     continue;
992     *p++ = 'H';
993     }
994    
995     if (listener->flags & LISTENER_SERVER)
996     *p++ = 'S';
997     if (listener->flags & LISTENER_SSL)
998     *p++ = 's';
999     *p = '\0';
1000    
1001    
1002     if (HasUMode(source_p, UMODE_ADMIN) &&
1003     (MyClient(source_p) || !ConfigServerHide.hide_server_ips))
1004     sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port,
1005     listener->name,
1006     listener->ref_count, buf,
1007     listener->active ? "active" : "disabled");
1008     else
1009     sendto_one_numeric(source_p, &me, RPL_STATSPLINE, 'P', listener->port,
1010     me.name, listener->ref_count, buf,
1011     listener->active ? "active" : "disabled");
1012     }
1013     }
1014    
1015     static void
1016 michael 1457 stats_ports(struct Client *source_p, int parc, char *parv[])
1017 adx 30 {
1018 michael 4341 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_P_oper_only)
1019 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1020 adx 30 else
1021     show_ports(source_p);
1022     }
1023    
1024     static void
1025 michael 1457 stats_resv(struct Client *source_p, int parc, char *parv[])
1026 adx 30 {
1027     report_resv(source_p);
1028     }
1029    
1030     static void
1031 michael 1457 stats_service(struct Client *source_p, int parc, char *parv[])
1032 adx 30 {
1033 michael 1632 report_confitem_types(source_p, CONF_SERVICE);
1034 michael 1175 }
1035    
1036     static void
1037 michael 1457 stats_tstats(struct Client *source_p, int parc, char *parv[])
1038 adx 30 {
1039 michael 4816 const dlink_node *node = NULL;
1040 michael 896 struct ServerStatistics tmp;
1041 michael 1842 struct ServerStatistics *sp = &tmp;
1042 michael 896
1043     memcpy(sp, &ServerStats, sizeof(struct ServerStatistics));
1044    
1045     /*
1046     * must use the += operator. is_sv is not the number of currently
1047     * active server connections. Note the incrementation in
1048     * s_bsd.c:close_connection.
1049     */
1050 michael 4212 sp->is_sv += dlink_list_length(&local_server_list);
1051 michael 896
1052 michael 4816 DLINK_FOREACH(node, local_server_list.head)
1053 michael 896 {
1054 michael 4816 const struct Client *target_p = node->data;
1055 michael 896
1056 michael 4589 sp->is_sbs += target_p->connection->send.bytes;
1057     sp->is_sbr += target_p->connection->recv.bytes;
1058     sp->is_sti += CurrentTime - target_p->connection->firsttime;
1059 michael 896 }
1060    
1061     sp->is_cl += dlink_list_length(&local_client_list);
1062    
1063 michael 4816 DLINK_FOREACH(node, local_client_list.head)
1064 michael 896 {
1065 michael 4816 const struct Client *target_p = node->data;
1066 michael 896
1067 michael 4589 sp->is_cbs += target_p->connection->send.bytes;
1068     sp->is_cbr += target_p->connection->recv.bytes;
1069     sp->is_cti += CurrentTime - target_p->connection->firsttime;
1070 michael 896 }
1071    
1072     sp->is_ni += dlink_list_length(&unknown_list);
1073    
1074 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1075 michael 4039 "t :accepts %u refused %u",
1076 michael 3574 sp->is_ac, sp->is_ref);
1077 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1078 michael 4039 "t :unknown commands %u prefixes %u",
1079 michael 3574 sp->is_unco, sp->is_unpf);
1080 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1081 michael 4039 "t :nick collisions %u unknown closes %u",
1082 michael 3574 sp->is_kill, sp->is_ni);
1083 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1084 michael 4039 "t :wrong direction %u empty %u",
1085 michael 3574 sp->is_wrdi, sp->is_empt);
1086 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1087 michael 4039 "t :numerics seen %u",
1088 michael 3574 sp->is_num);
1089 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1090 michael 4039 "t :auth successes %u fails %u",
1091 michael 3574 sp->is_asuc, sp->is_abad);
1092 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1093 michael 4039 "t :Client Server");
1094 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1095 michael 4039 "t :connected %u %u",
1096 michael 3574 (unsigned int)sp->is_cl,
1097     (unsigned int)sp->is_sv);
1098 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1099 michael 6781 "t :bytes sent %ju %ju",
1100 michael 3574 sp->is_cbs, sp->is_sbs);
1101 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1102 michael 6781 "t :bytes recv %ju %ju",
1103 michael 3574 sp->is_cbr, sp->is_sbr);
1104 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1105 michael 4039 "t :time connected %u %u",
1106 michael 3574 (unsigned int)sp->is_cti,
1107     (unsigned int)sp->is_sti);
1108 adx 30 }
1109    
1110     static void
1111 michael 1457 stats_uptime(struct Client *source_p, int parc, char *parv[])
1112 adx 30 {
1113 michael 4341 if (!HasUMode(source_p, UMODE_OPER) && ConfigGeneral.stats_u_oper_only)
1114 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1115 michael 2269 else
1116     {
1117 michael 6570 sendto_one_numeric(source_p, &me, RPL_STATSUPTIME,
1118     time_dissect(CurrentTime - me.connection->since));
1119 michael 2269 if (!ConfigServerHide.disable_remote_commands || HasUMode(source_p, UMODE_OPER))
1120 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSCONN, Count.max_loc_con,
1121     Count.max_loc_cli, Count.totalrestartcount);
1122 michael 2269 }
1123 adx 30 }
1124    
1125     static void
1126 michael 1457 stats_shared(struct Client *source_p, int parc, char *parv[])
1127 adx 30 {
1128 michael 6627 report_confitem_types(source_p, CONF_SHARED);
1129 adx 30 }
1130    
1131     /* stats_servers()
1132     *
1133     * input - client pointer
1134     * output - none
1135     * side effects - client is shown lists of who connected servers
1136     */
1137     static void
1138 michael 1457 stats_servers(struct Client *source_p, int parc, char *parv[])
1139 adx 30 {
1140 michael 4816 const dlink_node *node = NULL;
1141 adx 30
1142 michael 4816 DLINK_FOREACH(node, local_server_list.head)
1143 adx 30 {
1144 michael 4816 const struct Client *target_p = node->data;
1145 adx 30
1146 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1147 michael 3574 "v :%s (%s!%s@%s) Idle: %d",
1148     target_p->name,
1149     (target_p->serv->by[0] ? target_p->serv->by : "Remote."),
1150 michael 4589 "*", "*", (int)(CurrentTime - target_p->connection->lasttime));
1151 adx 30 }
1152    
1153 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1154 michael 4212 "v :%u Server(s)",
1155     dlink_list_length(&local_server_list));
1156 adx 30 }
1157    
1158     static void
1159 michael 1457 stats_gecos(struct Client *source_p, int parc, char *parv[])
1160 adx 30 {
1161 michael 1632 report_confitem_types(source_p, CONF_XLINE);
1162 adx 30 }
1163    
1164     static void
1165 michael 1457 stats_class(struct Client *source_p, int parc, char *parv[])
1166 adx 30 {
1167 michael 4816 const dlink_node *node = NULL;
1168 michael 2116
1169 michael 4816 DLINK_FOREACH(node, class_get_list()->head)
1170 michael 2116 {
1171 michael 4816 const struct ClassItem *class = node->data;
1172 michael 2116
1173 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSYLINE, 'Y',
1174     class->name, class->ping_freq,
1175     class->con_freq,
1176     class->max_total, class->max_sendq,
1177     class->max_recvq,
1178     class->ref_count,
1179     class->number_per_cidr, class->cidr_bitlen_ipv4,
1180     class->number_per_cidr, class->cidr_bitlen_ipv6,
1181     class->active ? "active" : "disabled");
1182 michael 2116 }
1183 adx 30 }
1184    
1185     static void
1186 michael 1457 stats_servlinks(struct Client *source_p, int parc, char *parv[])
1187 adx 30 {
1188 michael 6781 uintmax_t sendB = 0, recvB = 0;
1189 adx 30 time_t uptime = 0;
1190 michael 4816 dlink_node *node = NULL;
1191 adx 30
1192 michael 1219 if (ConfigServerHide.flatten_links && !HasUMode(source_p, UMODE_OPER))
1193 adx 30 {
1194 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1195 adx 30 return;
1196     }
1197    
1198 michael 4816 DLINK_FOREACH(node, local_server_list.head)
1199 adx 30 {
1200 michael 4816 struct Client *target_p = node->data;
1201 adx 30
1202 michael 1851 if (HasFlag(target_p, FLAGS_SERVICE) && ConfigServerHide.hide_services)
1203     if (!HasUMode(source_p, UMODE_OPER))
1204     continue;
1205    
1206 michael 4589 sendB += target_p->connection->send.bytes;
1207     recvB += target_p->connection->recv.bytes;
1208 adx 30
1209 michael 6781 /* ":%s 211 %s %s %u %u %ju %u %ju :%u %u %s" */
1210 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1211 michael 1219 get_client_name(target_p, HasUMode(source_p, UMODE_ADMIN) ? SHOW_IP : MASK_IP),
1212 michael 4589 dbuf_length(&target_p->connection->buf_sendq),
1213     target_p->connection->send.messages,
1214     target_p->connection->send.bytes >> 10,
1215     target_p->connection->recv.messages,
1216     target_p->connection->recv.bytes >> 10,
1217 michael 5530 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1218     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0,
1219 michael 1219 HasUMode(source_p, UMODE_OPER) ? show_capabilities(target_p) : "TS");
1220 adx 30 }
1221    
1222     sendB >>= 10;
1223     recvB >>= 10;
1224    
1225 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :%u total server(s)",
1226 michael 4212 dlink_list_length(&local_server_list));
1227 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Sent total: %7.2f %s",
1228 michael 3574 _GMKv(sendB), _GMKs(sendB));
1229 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT, "? :Recv total: %7.2f %s",
1230 michael 3574 _GMKv(recvB), _GMKs(recvB));
1231 adx 30
1232 michael 4589 uptime = (CurrentTime - me.connection->since);
1233 adx 30
1234 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1235 michael 3574 "? :Server send: %7.2f %s (%4.1f K/s)",
1236 michael 4589 _GMKv((me.connection->send.bytes>>10)),
1237     _GMKs((me.connection->send.bytes>>10)),
1238     (float)((float)((me.connection->send.bytes) >> 10) /
1239 michael 3574 (float)uptime));
1240 michael 5582 sendto_one_numeric(source_p, &me, RPL_STATSDEBUG | SND_EXPLICIT,
1241 michael 3574 "? :Server recv: %7.2f %s (%4.1f K/s)",
1242 michael 4589 _GMKv((me.connection->recv.bytes>>10)),
1243     _GMKs((me.connection->recv.bytes>>10)),
1244     (float)((float)((me.connection->recv.bytes) >> 10) /
1245 michael 3574 (float)uptime));
1246 adx 30 }
1247    
1248 michael 1457 /* parse_stats_args()
1249     *
1250     * inputs - arg count
1251     * - args
1252     * - doall flag
1253     * - wild card or not
1254     * output - pointer to name to use
1255     * side effects -
1256     * common parse routine for m_stats args
1257 michael 2820 *
1258 michael 1457 */
1259 michael 4754 static const char *
1260 michael 3114 parse_stats_args(struct Client *source_p, int parc, char *parv[], int *doall, int *wilds)
1261 adx 30 {
1262 michael 1457 if (parc > 2)
1263 adx 30 {
1264 michael 4754 const char *name = parv[2];
1265 adx 30
1266 michael 3114 if (!irccmp(name, ID_or_name(&me, source_p)))
1267 michael 1457 *doall = 2;
1268 michael 3114 else if (!match(name, ID_or_name(&me, source_p)))
1269 michael 1457 *doall = 1;
1270    
1271     *wilds = has_wildcards(name);
1272    
1273     return name;
1274 adx 30 }
1275    
1276 michael 1457 return NULL;
1277 adx 30 }
1278    
1279     static void
1280 michael 4754 stats_L_list(struct Client *source_p, const char *name, int doall, int wilds,
1281     dlink_list *list, const char statchar)
1282 adx 30 {
1283 michael 4816 dlink_node *node = NULL;
1284 adx 30
1285     /*
1286 michael 4035 * Send info about connections which match, or all if the
1287     * mask matches from. Only restrictions are on those who
1288 adx 30 * are invisible not being visible to 'foreigners' who use
1289     * a wild card based search to list it.
1290     */
1291 michael 4816 DLINK_FOREACH(node, list->head)
1292 adx 30 {
1293 michael 4816 struct Client *target_p = node->data;
1294 adx 30
1295 michael 1219 if (HasUMode(target_p, UMODE_INVISIBLE) && (doall || wilds) &&
1296 michael 1457 !(MyConnect(source_p) && HasUMode(source_p, UMODE_OPER)) &&
1297     !HasUMode(target_p, UMODE_OPER) && (target_p != source_p))
1298 adx 30 continue;
1299 michael 4035
1300 michael 1652 if (!doall && wilds && match(name, target_p->name))
1301 adx 30 continue;
1302 michael 4035
1303 adx 30 if (!(doall || wilds) && irccmp(name, target_p->name))
1304     continue;
1305    
1306 michael 4035 /*
1307     * This basically shows ips for our opers if it's not a server/admin, or
1308 michael 4299 * it's one of our admins.
1309 michael 4035 */
1310 michael 3654 if (MyClient(source_p) && HasUMode(source_p, UMODE_OPER) &&
1311     (HasUMode(source_p, UMODE_ADMIN) ||
1312     (!IsServer(target_p) && !HasUMode(target_p, UMODE_ADMIN) &&
1313     !IsHandshake(target_p) && !IsConnecting(target_p))))
1314 adx 30 {
1315 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1316 adx 30 (IsUpper(statchar)) ?
1317     get_client_name(target_p, SHOW_IP) :
1318     get_client_name(target_p, HIDE_IP),
1319 michael 4589 dbuf_length(&target_p->connection->buf_sendq),
1320     target_p->connection->send.messages,
1321     target_p->connection->send.bytes>>10,
1322     target_p->connection->recv.messages,
1323     target_p->connection->recv.bytes>>10,
1324 michael 5530 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1325     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since) : 0,
1326 adx 30 IsServer(target_p) ? show_capabilities(target_p) : "-");
1327     }
1328     else
1329     {
1330 michael 4968 /* If it's a server, mask the real IP */
1331     if (IsServer(target_p) || IsHandshake(target_p) || IsConnecting(target_p))
1332 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1333 michael 1457 get_client_name(target_p, MASK_IP),
1334 michael 4589 dbuf_length(&target_p->connection->buf_sendq),
1335     target_p->connection->send.messages,
1336     target_p->connection->send.bytes>>10,
1337     target_p->connection->recv.messages,
1338     target_p->connection->recv.bytes>>10,
1339 michael 5530 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1340     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0,
1341 michael 1457 IsServer(target_p) ? show_capabilities(target_p) : "-");
1342 adx 30 else /* show the real IP */
1343 michael 3109 sendto_one_numeric(source_p, &me, RPL_STATSLINKINFO,
1344 michael 1457 (IsUpper(statchar)) ?
1345     get_client_name(target_p, SHOW_IP) :
1346     get_client_name(target_p, HIDE_IP),
1347 michael 4589 dbuf_length(&target_p->connection->buf_sendq),
1348     target_p->connection->send.messages,
1349     target_p->connection->send.bytes>>10,
1350     target_p->connection->recv.messages,
1351     target_p->connection->recv.bytes>>10,
1352 michael 5530 (unsigned int)(CurrentTime - target_p->connection->firsttime),
1353     (CurrentTime > target_p->connection->since) ? (unsigned int)(CurrentTime - target_p->connection->since):0,
1354 michael 1457 IsServer(target_p) ? show_capabilities(target_p) : "-");
1355 adx 30 }
1356     }
1357     }
1358    
1359 michael 1457 /*
1360     * stats_L
1361 adx 30 *
1362 michael 1457 * inputs - pointer to client to report to
1363     * - doall flag
1364     * - wild card or not
1365     * output - NONE
1366     * side effects -
1367     */
1368     static void
1369 michael 4754 stats_L(struct Client *source_p, const char *name, int doall,
1370     int wilds, const char statchar)
1371 michael 1457 {
1372     stats_L_list(source_p, name, doall, wilds, &unknown_list, statchar);
1373     stats_L_list(source_p, name, doall, wilds, &local_client_list, statchar);
1374 michael 4212 stats_L_list(source_p, name, doall, wilds, &local_server_list, statchar);
1375 michael 1457 }
1376    
1377     static void
1378     stats_ltrace(struct Client *source_p, int parc, char *parv[])
1379     {
1380     int doall = 0;
1381     int wilds = 0;
1382 michael 4754 const char *name = NULL;
1383 michael 1457
1384 michael 3246 if ((name = parse_stats_args(source_p, parc, parv, &doall, &wilds)))
1385 michael 1457 {
1386 michael 4754 const char statchar = *parv[1];
1387 michael 1457 stats_L(source_p, name, doall, wilds, statchar);
1388     }
1389     else
1390 michael 3109 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "STATS");
1391 michael 1457 }
1392    
1393 michael 6551 struct StatsStruct
1394 michael 1457 {
1395     const unsigned char letter;
1396 michael 4478 void (*handler)(struct Client *, int, char *[]);
1397 michael 6551 const unsigned int required_modes;
1398 michael 1457 };
1399    
1400 michael 6551 static const struct StatsStruct *stats_map[256];
1401     static const struct StatsStruct stats_tab[] =
1402     {
1403     { 'a', stats_dns_servers, UMODE_ADMIN },
1404     { 'A', stats_dns_servers, UMODE_ADMIN },
1405     { 'c', stats_connect, UMODE_OPER },
1406     { 'C', stats_connect, UMODE_OPER },
1407     { 'd', stats_tdeny, UMODE_OPER },
1408     { 'D', stats_deny, UMODE_OPER },
1409     { 'e', stats_exempt, UMODE_OPER },
1410     { 'E', stats_events, UMODE_ADMIN },
1411     { 'f', fd_dump, UMODE_ADMIN },
1412     { 'F', fd_dump, UMODE_ADMIN },
1413     { 'h', stats_hubleaf, UMODE_OPER },
1414     { 'H', stats_hubleaf, UMODE_OPER },
1415     { 'i', stats_auth, 0 },
1416     { 'I', stats_auth, 0 },
1417     { 'k', stats_tklines, 0 },
1418     { 'K', stats_klines, 0 },
1419     { 'l', stats_ltrace, UMODE_OPER },
1420     { 'L', stats_ltrace, UMODE_OPER },
1421     { 'm', stats_messages, 0 },
1422     { 'M', stats_messages, 0 },
1423     { 'o', stats_oper, 0 },
1424     { 'O', stats_oper, 0 },
1425     { 'p', stats_operedup, 0 },
1426     { 'P', stats_ports, 0 },
1427     { 'q', stats_resv, UMODE_OPER },
1428     { 'Q', stats_resv, UMODE_OPER },
1429     { 'r', stats_usage, UMODE_OPER },
1430     { 'R', stats_usage, UMODE_OPER },
1431     { 's', stats_service, UMODE_OPER },
1432     { 'S', stats_service, UMODE_OPER },
1433     { 't', stats_tstats, UMODE_OPER },
1434     { 'T', motd_report, UMODE_OPER },
1435     { 'u', stats_uptime, 0 },
1436     { 'U', stats_shared, UMODE_OPER },
1437     { 'v', stats_servers, UMODE_OPER },
1438     { 'x', stats_gecos, UMODE_OPER },
1439     { 'X', stats_gecos, UMODE_OPER },
1440     { 'y', stats_class, UMODE_OPER },
1441     { 'Y', stats_class, UMODE_OPER },
1442     { 'z', stats_memory, UMODE_OPER },
1443     { '?', stats_servlinks, 0 },
1444     { '\0', NULL, 0 }
1445     };
1446    
1447 michael 1457 static void
1448     do_stats(struct Client *source_p, int parc, char *parv[])
1449     {
1450 michael 6551 const unsigned char statchar = *parv[1];
1451     const struct StatsStruct *tab;
1452 michael 1457
1453     if (statchar == '\0')
1454     {
1455 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, '*');
1456 michael 1457 return;
1457     }
1458    
1459 michael 6551 if ((tab = stats_map[statchar]))
1460 michael 1457 {
1461 michael 6551 if (!tab->required_modes || HasUMode(source_p, tab->required_modes))
1462     tab->handler(source_p, parc, parv);
1463     else
1464     sendto_one_numeric(source_p, &me, ERR_NOPRIVILEGES);
1465 michael 1457
1466 michael 6551 sendto_realops_flags(UMODE_SPY, L_ALL, SEND_NOTICE,
1467     "STATS %c requested by %s (%s@%s) [%s]",
1468     statchar, source_p->name, source_p->username,
1469     source_p->host, source_p->servptr->name);
1470 michael 1457 }
1471    
1472 michael 3109 sendto_one_numeric(source_p, &me, RPL_ENDOFSTATS, statchar);
1473 michael 1457 }
1474    
1475     /*
1476     * m_stats()
1477 michael 3096 * parv[0] = command
1478 michael 1457 * parv[1] = stat letter/command
1479     * parv[2] = (if present) server/mask in stats L
1480 adx 30 */
1481 michael 2820 static int
1482 michael 3156 m_stats(struct Client *source_p, int parc, char *parv[])
1483 adx 30 {
1484 michael 1457 static time_t last_used = 0;
1485 adx 30
1486 michael 4299 /* Check the user is actually allowed to do /stats, and isn't flooding */
1487 michael 4341 if ((last_used + ConfigGeneral.pace_wait) > CurrentTime)
1488 michael 1457 {
1489 michael 4758 sendto_one_numeric(source_p, &me, RPL_LOAD2HI, "STATS");
1490 michael 2820 return 0;
1491 michael 1457 }
1492 adx 30
1493 michael 1457 last_used = CurrentTime;
1494 adx 30
1495 michael 2341 /* Is the stats meant for us? */
1496     if (!ConfigServerHide.disable_remote_commands)
1497 michael 4035 if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
1498 michael 2820 return 0;
1499 michael 2341
1500 michael 1457 do_stats(source_p, parc, parv);
1501 michael 2820 return 0;
1502 michael 1457 }
1503    
1504     /*
1505 michael 3219 * ms_stats()
1506 michael 3096 * parv[0] = command
1507 michael 1457 * parv[1] = stat letter/command
1508     * parv[2] = (if present) server/mask in stats L, or target
1509     */
1510 michael 2820 static int
1511 michael 3219 ms_stats(struct Client *source_p, int parc, char *parv[])
1512 michael 1457 {
1513 michael 4035 if (hunt_server(source_p, ":%s STATS %s :%s", 2, parc, parv) != HUNTED_ISME)
1514 michael 2820 return 0;
1515 michael 1457
1516     do_stats(source_p, parc, parv);
1517 michael 2820 return 0;
1518 adx 30 }
1519 michael 1230
1520 michael 6551 static void
1521     stats_init(void)
1522     {
1523     for (const struct StatsStruct *tab = stats_tab; tab->letter; ++tab)
1524     stats_map[tab->letter] = tab;
1525     }
1526    
1527 michael 2820 static struct Message stats_msgtab =
1528     {
1529 michael 5880 .cmd = "STATS",
1530     .args_min = 2,
1531     .args_max = MAXPARA,
1532     .handlers[UNREGISTERED_HANDLER] = m_unregistered,
1533     .handlers[CLIENT_HANDLER] = m_stats,
1534     .handlers[SERVER_HANDLER] = ms_stats,
1535     .handlers[ENCAP_HANDLER] = m_ignore,
1536     .handlers[OPER_HANDLER] = ms_stats
1537 michael 1230 };
1538    
1539     static void
1540     module_init(void)
1541     {
1542 michael 6551 stats_init();
1543 michael 1230 mod_add_cmd(&stats_msgtab);
1544     }
1545    
1546     static void
1547     module_exit(void)
1548     {
1549     mod_del_cmd(&stats_msgtab);
1550     }
1551    
1552 michael 2820 struct module module_entry =
1553     {
1554 michael 1230 .version = "$Revision$",
1555     .modinit = module_init,
1556     .modexit = module_exit,
1557     };

Properties

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