2 * IRC - Internet Relay Chat, ircd/m_stats.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
84 * No need to include handlers.h here the signatures must match
85 * and we don't need to force a rebuild of all the handlers everytime
86 * we add a new one to the list. --Bleep
100 #include "ircd_alloc.h"
101 #include "ircd_chattr.h"
102 #include "ircd_reply.h"
103 #include "ircd_string.h"
105 #include "listener.h"
109 #include "numnicks.h"
110 #include "opercmds.h"
119 #include "userload.h"
127 * m_stats - generic message handler
129 * parv[0] = sender prefix
130 * parv[1] = statistics selector (defaults to Message frequency)
131 * parv[2] = target server (current server defaulted, if omitted)
132 * And 'stats l' and 'stats' L:
133 * parv[3] = server mask ("*" defaulted, if omitted)
135 * parv[3] = port mask (returns p-lines when its port is matched by this)
136 * Or for stats k,K,i and I:
137 * parv[3] = [user@]host.name (returns which K/I-lines match this)
138 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
139 * (defaults to old reply if ommitted, when local or Oper)
140 * A remote mask (something containing wildcards) is only
141 * allowed for IRC Operators.
143 * parv[3] = time param
144 * parv[4] = time param
145 * (see report_memleak_stats() in runmalloc.c for details)
147 * This function is getting really ugly. -Ghostwolf
149 int m_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
151 struct Message *mptr;
152 struct Client *acptr;
153 struct ConfItem *aconf;
154 char stat = parc > 1 ? parv[1][0] : '\0';
155 const char **infotext = statsinfo;
158 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
166 int doall = 0, wilds = 0;
168 if (parc > 3 && *parv[3])
172 wilds = (*name == '*' || *name == '?');
173 for (p = name + 1; *p; ++p)
174 if ((*p == '*' || *p == '?') && p[-1] != '\\')
183 * Send info about connections which match, or all if the
184 * mask matches me.name. Only restrictions are on those who
185 * are invisible not being visible to 'foreigners' who use
186 * a wild card based search to list it.
188 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
189 "SendM SendKBytes RcveM RcveKBytes :Open since");
190 for (i = 0; i <= HighestFd; i++)
192 if (!(acptr = LocalClientArray[i]))
194 /* Don't return clients when this is a request for `all' */
195 if (doall && IsUser(acptr))
197 /* Don't show invisible people to non opers unless they know the nick */
198 if (IsInvisible(acptr) && (doall || wilds) && !IsAnOper(acptr) && (acptr != sptr))
200 /* Only show the ones that match the given mask - if any */
201 if (!doall && wilds && match(name, acptr->name))
203 /* Skip all that do not match the specific query */
204 if (!(doall || wilds) && 0 != ircd_strcmp(name, acptr->name))
206 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
207 "%s %u %u %u %u %u :%Tu", (*acptr->name) ? acptr->name : "<unregistered>",
208 (int)DBufLength(&acptr->sendQ), (int)acptr->sendM,
209 (int)acptr->sendK, (int)acptr->receiveM,
210 (int)acptr->receiveK, CurrentTime - acptr->firsttime);
216 report_configured_links(sptr, CONF_SERVER);
219 case 'g': /* send glines */
224 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
229 case 'k': /* display CONF_IPKILL as well
233 char *user, *host, *p;
234 int conf_status = (stat == 'k' || stat == 'K') ? CONF_KLINE : CONF_CLIENT;
237 report_configured_links(sptr, conf_status);
242 for (p = parv[3]; *p; p++)
250 if (*p == '?' || *p == '*')
259 if (conf_status == CONF_CLIENT)
261 user = 0; /* Not used, but to avoid compiler warning. */
267 if ((host = strchr(parv[3], '@')))
278 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
280 if ((aconf->status & conf_status))
282 if (conf_status == CONF_KLINE)
284 if ((!wilds && ((user || aconf->host[1]) &&
285 !match(aconf->host, host) &&
286 (!user || !match(aconf->name, user)))) ||
287 (wilds && !mmatch(host, aconf->host) &&
288 (!user || !mmatch(user, aconf->name))))
290 send_reply(sptr, RPL_STATSKLINE,
291 (aconf->status & CONF_KILL) ? 'K' : 'k', aconf->host,
292 aconf->passwd, aconf->name, aconf->port,
293 get_conf_class(aconf));
298 else if (conf_status == CONF_CLIENT)
300 if ((!wilds && (!match(aconf->host, host) ||
301 !match(aconf->name, host))) ||
302 (wilds && (!mmatch(host, aconf->host) ||
303 !mmatch(host, aconf->name))))
305 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
306 aconf->port, get_conf_class(aconf));
317 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
318 fda_get_block_count());
323 sendto_one(sptr, rpl_str(RPL_STATMEMTOT), /* XXX DEAD */
324 me.name, parv[0], get_mem_size(), get_alloc_cnt());
327 report_memleak_stats(sptr, parc, parv);
329 #if !defined(MEMSIZESTATS) && !defined(MEMLEAKSTATS)
330 sendto_one(sptr, ":%s NOTICE %s :stats M : Memory allocation monitoring " /* XXX DEAD */
331 "is not enabled on this server", me.name, parv[0]);
336 for (mptr = msgtab; mptr->cmd; mptr++)
338 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
343 report_configured_links(sptr, CONF_OPS);
348 * show listener ports
349 * show hidden ports to opers, if there are more than 3 parameters,
350 * interpret the fourth parameter as the port number.
352 show_ports(sptr, 0, (parc > 3) ? atoi(parv[3]) : 0, 100);
357 send_usage(sptr, parv[0]);
361 report_crule_list(sptr, CRULE_ALL);
364 report_crule_list(sptr, CRULE_MASK);
367 tstats(sptr, parv[0]);
370 report_motd_list(sptr);
373 report_configured_links(sptr, CONF_UWORLD);
379 nowr = CurrentTime - me.since;
380 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
381 (nowr / 60) % 60, nowr % 60);
382 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
387 const struct ConnectionClass* cl = get_class_list();
389 for ( ; cl; cl = cl->next) {
391 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
402 class_send_meminfo(sptr);
403 send_listinfo(sptr, parv[0]);
408 report_classes(sptr);
412 count_memory(sptr, parv[0]);
417 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
420 send_reply(sptr, RPL_ENDOFSTATS, stat);
425 * ms_stats - server message handler
427 * parv[0] = sender prefix
428 * parv[1] = statistics selector (defaults to Message frequency)
429 * parv[2] = target server (current server defaulted, if omitted)
430 * And 'stats l' and 'stats' L:
431 * parv[3] = server mask ("*" defaulted, if omitted)
433 * parv[3] = port mask (returns p-lines when its port is matched by this)
434 * Or for stats k,K,i and I:
435 * parv[3] = [user@]host.name (returns which K/I-lines match this)
436 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
437 * (defaults to old reply if ommitted, when local or Oper)
438 * A remote mask (something containing wildcards) is only
439 * allowed for IRC Operators.
441 * parv[3] = time param
442 * parv[4] = time param
443 * (see report_memleak_stats() in runmalloc.c for details)
445 * This function is getting really ugly. -Ghostwolf
447 int ms_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
449 struct Message *mptr;
450 struct Client *acptr;
451 struct ConfItem *aconf;
452 char stat = parc > 1 ? parv[1][0] : '\0';
455 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
463 int doall = 0, wilds = 0;
465 if (parc > 3 && *parv[3])
469 wilds = (*name == '*' || *name == '?');
470 for (p = name + 1; *p; ++p)
471 if ((*p == '*' || *p == '?') && p[-1] != '\\')
480 * Send info about connections which match, or all if the
481 * mask matches me.name. Only restrictions are on those who
482 * are invisible not being visible to 'foreigners' who use
483 * a wild card based search to list it.
485 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
486 "SendM SendKBytes RcveM RcveKBytes :Open since");
487 for (i = 0; i <= HighestFd; i++)
489 if (!(acptr = LocalClientArray[i]))
491 /* Don't return clients when this is a request for `all' */
492 if (doall && IsUser(acptr))
494 /* Don't show invisible people to unauthorized people when using
495 * wildcards -- Is this still needed now /stats is oper only ?
496 * Not here, because ms_stats is specifically a remote command,
497 * thus the check was removed. -Ghostwolf */
498 /* Only show the ones that match the given mask - if any */
499 if (!doall && wilds && match(name, acptr->name))
501 /* Skip all that do not match the specific query */
502 if (!(doall || wilds) && 0 != ircd_strcmp(name, acptr->name))
504 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
505 "%s %u %u %u %u %u :%Tu", acptr->name,
506 (int)DBufLength(&acptr->sendQ), (int)acptr->sendM,
507 (int)acptr->sendK, (int)acptr->receiveM,
508 (int)acptr->receiveK, CurrentTime - acptr->firsttime);
514 report_configured_links(sptr, CONF_SERVER);
517 case 'g': /* send glines */
522 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
527 case 'k': /* display CONF_IPKILL as well
531 char *user, *host, *p;
532 int conf_status = (stat == 'k' || stat == 'K') ? CONF_KLINE : CONF_CLIENT;
533 if (IsOper(sptr) && parc < 4)
535 report_configured_links(sptr, conf_status);
538 if (parc < 4 || *parv[3] == '\0')
539 return need_more_params(sptr,
540 (conf_status & CONF_KLINE) ? "STATS K" : "STATS I");
543 for (p = parv[3]; *p; p++)
551 if (*p == '?' || *p == '*')
565 if (conf_status == CONF_CLIENT)
567 user = 0; /* Not used, but to avoid compiler warning. */
573 if ((host = strchr(parv[3], '@')))
584 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
586 if ((aconf->status & conf_status))
588 if (conf_status == CONF_KLINE)
590 if ((!wilds && ((user || aconf->host[1]) &&
591 !match(aconf->host, host) &&
592 (!user || !match(aconf->name, user)))) ||
593 (wilds && !mmatch(host, aconf->host) &&
594 (!user || !mmatch(user, aconf->name))))
596 send_reply(sptr, RPL_STATSKLINE,
597 (aconf->status & CONF_KILL) ? 'K' : 'k', aconf->host,
598 aconf->passwd, aconf->name, aconf->port,
599 get_conf_class(aconf));
604 else if (conf_status == CONF_CLIENT)
606 if ((!wilds && (!match(aconf->host, host) ||
607 !match(aconf->name, host))) ||
608 (wilds && (!mmatch(host, aconf->host) ||
609 !mmatch(host, aconf->name))))
611 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
612 aconf->port, get_conf_class(aconf));
623 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
624 fda_get_block_count());
629 sendto_one(sptr, rpl_str(RPL_STATMEMTOT), /* XXX DEAD */
630 me.name, parv[0], get_mem_size(), get_alloc_cnt());
633 report_memleak_stats(sptr, parc, parv);
635 #if !defined(MEMSIZESTATS) && !defined(MEMLEAKSTATS)
636 sendto_one(sptr, ":%s NOTICE %s :stats M : Memory allocation monitoring " /* XXX DEAD */
637 "is not enabled on this server", me.name, parv[0]);
642 for (mptr = msgtab; mptr->cmd; mptr++)
644 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
649 report_configured_links(sptr, CONF_OPS);
654 * show listener ports
655 * show hidden ports to opers, if there are more than 3 parameters,
656 * interpret the fourth parameter as the port number, limit non-local
657 * or non-oper results to 8 ports.
659 show_ports(sptr, IsOper(sptr), (parc > 3) ? atoi(parv[3]) : 0, IsOper(sptr) ? 100 : 8);
664 send_usage(sptr, parv[0]);
668 report_crule_list(sptr, CRULE_ALL);
671 report_crule_list(sptr, CRULE_MASK);
674 tstats(sptr, parv[0]);
677 report_motd_list(sptr);
680 report_configured_links(sptr, CONF_UWORLD);
686 nowr = CurrentTime - me.since;
687 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
688 (nowr / 60) % 60, nowr % 60);
689 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
694 const struct ConnectionClass* cl = get_class_list();
696 for ( ; cl; cl = cl->next) {
698 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
710 class_send_meminfo(sptr);
711 send_listinfo(sptr, parv[0]);
716 report_classes(sptr);
720 count_memory(sptr, parv[0]);
726 send_reply(sptr, RPL_ENDOFSTATS, stat);
731 * mo_stats - oper message handler
733 * parv[0] = sender prefix
734 * parv[1] = statistics selector (defaults to Message frequency)
735 * parv[2] = target server (current server defaulted, if omitted)
736 * And 'stats l' and 'stats' L:
737 * parv[3] = server mask ("*" defaulted, if omitted)
739 * parv[3] = port mask (returns p-lines when its port is matched by this)
740 * Or for stats k,K,i and I:
741 * parv[3] = [user@]host.name (returns which K/I-lines match this)
742 * or [user@]host.mask (returns which K/I-lines are mmatched by this)
743 * (defaults to old reply if ommitted, when local or Oper)
744 * A remote mask (something containing wildcards) is only
745 * allowed for IRC Operators.
747 * parv[3] = time param
748 * parv[4] = time param
749 * (see report_memleak_stats() in runmalloc.c for details)
751 * This function is getting really ugly. -Ghostwolf
753 int mo_stats(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
755 struct Message *mptr;
756 struct Client *acptr;
757 struct ConfItem *aconf;
758 char stat = parc > 1 ? parv[1][0] : '\0';
759 const char **infotext = statsinfo;
762 if (hunt_stats(cptr, sptr, parc, parv, stat) != HUNTED_ISME)
770 int doall = 0, wilds = 0;
772 if (parc > 3 && *parv[3])
776 wilds = (*name == '*' || *name == '?');
777 for (p = name + 1; *p; ++p)
778 if ((*p == '*' || *p == '?') && p[-1] != '\\')
787 * Send info about connections which match, or all if the
788 * mask matches me.name. Only restrictions are on those who
789 * are invisible not being visible to 'foreigners' who use
790 * a wild card based search to list it.
792 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO, "Connection SendQ "
793 "SendM SendKBytes RcveM RcveKBytes :Open since");
794 for (i = 0; i <= HighestFd; i++)
796 if (!(acptr = LocalClientArray[i]))
798 /* Don't return clients when this is a request for `all' */
799 if (doall && IsUser(acptr))
801 /* Only show the ones that match the given mask - if any */
802 if (!doall && wilds && match(name, acptr->name))
804 /* Skip all that do not match the specific query */
805 if (!(doall || wilds) && 0 != ircd_strcmp(name, acptr->name))
807 send_reply(sptr, SND_EXPLICIT | RPL_STATSLINKINFO,
808 "%s %u %u %u %u %u :%Tu", acptr->name,
809 (int)DBufLength(&acptr->sendQ), (int)acptr->sendM,
810 (int)acptr->sendK, (int)acptr->receiveM,
811 (int)acptr->receiveK, CurrentTime - acptr->firsttime);
817 report_configured_links(sptr, CONF_SERVER);
820 case 'g': /* send glines */
825 report_configured_links(sptr, CONF_HUB | CONF_LEAF);
830 case 'k': /* display CONF_IPKILL as well
834 char *user, *host, *p;
835 int conf_status = (stat == 'k' || stat == 'K') ? CONF_KLINE : CONF_CLIENT;
838 report_configured_links(sptr, conf_status);
843 for (p = parv[3]; *p; p++)
851 if (*p == '?' || *p == '*')
860 if (conf_status == CONF_CLIENT)
862 user = 0; /* Not used, but to avoid compiler warning. */
868 if ((host = strchr(parv[3], '@')))
879 for (aconf = GlobalConfList; aconf; aconf = aconf->next)
881 if ((aconf->status & conf_status))
883 if (conf_status == CONF_KLINE)
885 if ((!wilds && ((user || aconf->host[1]) &&
886 !match(aconf->host, host) &&
887 (!user || !match(aconf->name, user)))) ||
888 (wilds && !mmatch(host, aconf->host) &&
889 (!user || !mmatch(user, aconf->name))))
891 send_reply(sptr, RPL_STATSKLINE,
892 (aconf->status & CONF_KILL) ? 'K' : 'k', aconf->host,
893 aconf->passwd, aconf->name, aconf->port,
894 get_conf_class(aconf));
899 else if (conf_status == CONF_CLIENT)
901 if ((!wilds && (!match(aconf->host, host) ||
902 !match(aconf->name, host))) ||
903 (wilds && (!mmatch(host, aconf->host) ||
904 !mmatch(host, aconf->name))))
906 send_reply(sptr, RPL_STATSILINE, 'I', aconf->host, aconf->name,
907 aconf->port, get_conf_class(aconf));
918 send_reply(sptr, RPL_STATMEMTOT, fda_get_byte_count(),
919 fda_get_block_count());
924 sendto_one(sptr, rpl_str(RPL_STATMEMTOT), /* XXX DEAD */
925 me.name, parv[0], get_mem_size(), get_alloc_cnt());
928 report_memleak_stats(sptr, parc, parv);
930 #if !defined(MEMSIZESTATS) && !defined(MEMLEAKSTATS)
931 sendto_one(sptr, ":%s NOTICE %s :stats M : Memory allocation monitoring " /* XXX DEAD */
932 "is not enabled on this server", me.name, parv[0]);
937 for (mptr = msgtab; mptr->cmd; mptr++)
939 send_reply(sptr, RPL_STATSCOMMANDS, mptr->cmd, mptr->count,
944 report_configured_links(sptr, CONF_OPS);
949 * show listener ports
950 * show hidden ports to opers, if there are more than 3 parameters,
951 * interpret the fourth parameter as the port number, limit non-local
952 * or non-oper results to 8 ports.
954 show_ports(sptr, 1, (parc > 3) ? atoi(parv[3]) : 0, 100);
959 send_usage(sptr, parv[0]);
963 report_crule_list(sptr, CRULE_ALL);
966 report_crule_list(sptr, CRULE_MASK);
969 tstats(sptr, parv[0]);
972 report_motd_list(sptr);
975 report_configured_links(sptr, CONF_UWORLD);
981 nowr = CurrentTime - me.since;
982 send_reply(sptr, RPL_STATSUPTIME, nowr / 86400, (nowr / 3600) % 24,
983 (nowr / 60) % 60, nowr % 60);
984 send_reply(sptr, RPL_STATSCONN, max_connection_count, max_client_count);
989 const struct ConnectionClass* cl = get_class_list();
991 for ( ; cl; cl = cl->next) {
993 send_reply(sptr, RPL_TRACECLASS, ConClass(cl), Links(cl));
1004 class_send_meminfo(sptr);
1005 send_listinfo(sptr, parv[0]);
1010 report_classes(sptr);
1014 count_memory(sptr, parv[0]);
1019 sendcmdto_one(&me, CMD_NOTICE, sptr, "%C :%s", sptr, *infotext++);
1022 send_reply(sptr, RPL_ENDOFSTATS, stat);