2 * IRC - Internet Relay Chat, common/send.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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 1, or (at your option)
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.
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 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "ircd_features.h"
30 #include "ircd_snprintf.h"
31 #include "ircd_string.h"
49 static int sentalong[MAXCONNECTIONS];
50 static int sentalong_marker;
51 struct SLink *opsarray[32]; /* don't use highest bit unless you change
52 atoi to strtoul in sendto_op_mask() */
53 static struct Connection *send_queues = 0;
58 * An error has been detected. The link *must* be closed,
59 * but *cannot* call ExitClient (m_bye) from here.
60 * Instead, mark it with FLAG_DEADSOCKET. This should
61 * generate ExitClient from the main loop.
63 * If 'notice' is not NULL, it is assumed to be a format
64 * for a message to local opers. It can contain only one
65 * '%s', which will be replaced by the sockhost field of
68 * Also, the notice is skipped for "uninteresting" cases,
69 * like Persons and yet unknown connections...
72 static void dead_link(struct Client *to, char *notice)
74 SetFlag(to, FLAG_DEADSOCKET);
76 * If because of BUFFERPOOL problem then clean dbuf's now so that
77 * notices don't hurt operators below.
79 DBufClear(&(cli_recvQ(to)));
80 MsgQClear(&(cli_sendQ(to)));
81 client_drop_sendq(cli_connect(to));
84 * Keep a copy of the last comment, for later use...
86 ircd_strncpy(cli_info(to), notice, REALLEN);
88 if (!IsUser(to) && !IsUnknown(to) && !HasFlag(to, FLAG_CLOSING))
89 sendto_opmask_butone(0, SNO_OLDSNO, "%s for %s", cli_info(to), cli_name(to));
90 Debug((DEBUG_ERROR, cli_info(to)));
93 static int can_send(struct Client* to)
96 return (IsDead(to) || IsMe(to) || -1 == cli_fd(to)) ? 0 : 1;
99 /* This helper routine kills the connection with the highest sendq, to
100 * try to free up some buffer memory.
103 kill_highest_sendq(int servers_too)
106 unsigned int highest_sendq = 0;
107 struct Client *highest_client = 0;
109 for (i = HighestFd; i >= 0; i--)
111 if (!LocalClientArray[i] || (!servers_too && cli_serv(LocalClientArray[i])))
112 continue; /* skip servers */
114 /* If this sendq is higher than one we last saw, remember it */
115 if (MsgQLength(&(cli_sendQ(LocalClientArray[i]))) > highest_sendq)
117 highest_client = LocalClientArray[i];
118 highest_sendq = MsgQLength(&(cli_sendQ(highest_client)));
123 dead_link(highest_client, "Buffer allocation error");
129 * Used to empty all output buffers for all connections. Should only
130 * be called once per scan of connections. There should be a select in
131 * here perhaps but that means either forcing a timeout or doing a poll.
132 * When flushing, all we do is empty the obuffer array for each local
133 * client and try to send it. if we cant send it, it goes into the sendQ
136 void flush_connections(struct Client* cptr)
142 struct Connection* con;
143 for (con = send_queues; con; con = con_next(con)) {
144 assert(0 < MsgQLength(&(con_sendQ(con))));
145 send_queued(con_client(con));
153 * This function is called from the main select-loop (or whatever)
154 * when there is a chance that some output would be possible. This
155 * attempts to empty the send queue as far as possible...
157 void send_queued(struct Client *to)
160 assert(0 != cli_local(to));
162 if (IsBlocked(to) || !can_send(to))
163 return; /* Don't bother */
165 while (MsgQLength(&(cli_sendQ(to))) > 0) {
168 if ((len = deliver_it(to, &(cli_sendQ(to))))) {
169 msgq_delete(&(cli_sendQ(to)), len);
170 cli_lastsq(to) = MsgQLength(&(cli_sendQ(to))) / 1024;
179 sprintf(tmp,"Write error: %s",(strerror(cli_error(to))) ? (strerror(cli_error(to))) : "Unknown error" );
186 /* Ok, sendq is now empty... */
187 client_drop_sendq(cli_connect(to));
191 void send_buffer(struct Client* to, struct MsgBuf* buf, int prio)
201 * This socket has already been marked as dead
205 if (MsgQLength(&(cli_sendQ(to))) > get_sendq(to)) {
207 sendto_opmask_butone(0, SNO_OLDSNO, "Max SendQ limit exceeded for %C: "
208 "%zu > %zu", to, MsgQLength(&(cli_sendQ(to))),
210 dead_link(to, "Max sendQ exceeded");
214 Debug((DEBUG_SEND, "Sending [%p] to %s", buf, cli_name(to)));
216 msgq_add(&(cli_sendQ(to)), buf, prio);
217 client_add_sendq(cli_connect(to), &send_queues);
221 * Update statistics. The following is slightly incorrect
222 * because it counts messages even if queued, but bytes
223 * only really sent. Queued bytes get updated in SendQueued.
228 * This little bit is to stop the sendQ from growing too large when
229 * there is no need for it to. Thus we call send_queued() every time
230 * 2k has been added to the queue since the last non-fatal write.
231 * Also stops us from deliberately building a large sendQ and then
232 * trying to flood that link with data (possible during the net
233 * relinking done by servers with a large load).
235 if (MsgQLength(&(cli_sendQ(to))) / 1024 > cli_lastsq(to))
240 * Send a msg to all ppl on servers/hosts that match a specified mask
241 * (used for enhanced PRIVMSGs)
243 * addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
246 static int match_it(struct Client *from, struct Client *one, const char *mask, int what)
251 return (match(mask, cli_user(one)->host) == 0 ||
252 (HasHiddenHost(one) && match(mask, cli_user(one)->realhost) == 0));
255 return (match(mask, cli_name(cli_user(one)->server)) == 0);
260 * Send a raw command to a single client; use *ONLY* if you absolutely
261 * must send a command without a prefix.
263 void sendrawto_one(struct Client *to, const char *pattern, ...)
268 va_start(vl, pattern);
269 mb = msgq_vmake(to, pattern, vl);
272 send_buffer(to, mb, 0);
278 * Send a (prefixed) command to a single client; select which of <cmd>
279 * <tok> to use depending on if to is a server or not. <from> is the
280 * originator of the command.
282 void sendcmdto_one(struct Client *from, const char *cmd, const char *tok,
283 struct Client *to, const char *pattern, ...)
290 vd.vd_format = pattern; /* set up the struct VarData for %v */
291 va_start(vd.vd_args, pattern);
293 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
298 send_buffer(to, mb, 0);
304 * Send a (prefixed) command to a single client in the priority queue;
305 * select which of <cmd> <tok> to use depending on if to is a server
306 *or not. <from> is the originator of the command.
308 void sendcmdto_prio_one(struct Client *from, const char *cmd, const char *tok,
309 struct Client *to, const char *pattern, ...)
316 vd.vd_format = pattern; /* set up the struct VarData for %v */
317 va_start(vd.vd_args, pattern);
319 mb = msgq_make(to, "%:#C %s %v", from, IsServer(to) || IsMe(to) ? tok : cmd,
324 send_buffer(to, mb, 1);
330 * Send a (prefixed) command to all servers but one, using tok; cmd
331 * is ignored in this particular function.
333 void sendcmdto_serv_butone(struct Client *from, const char *cmd,
334 const char *tok, struct Client *one,
335 const char *pattern, ...)
341 vd.vd_format = pattern; /* set up the struct VarData for %v */
342 va_start(vd.vd_args, pattern);
345 mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
348 /* send it to our downlinks */
349 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
350 if (one && lp->value.cptr == cli_from(one))
352 send_buffer(lp->value.cptr, mb, 0);
359 * Send a (prefix) command originating from <from> to all channels
360 * <from> is locally on. <from> must be a user. <tok> is ignored in
363 * Update: don't send to 'one', if any. --Vampire
365 /* XXX sentalong_marker used XXX
367 * There is not an easy way to revoke the need for sentalong_marker
368 * from this function. Thoughts and ideas would be welcome... -Kev
370 * One possibility would be to internalize the sentalong array; that
371 * could be prohibitively big, though. We could get around that by
372 * making one that's the number of connected servers or something...
373 * or perhaps by adding a special flag to the servers we've sent a
374 * message to, and then a final loop through the connected servers
375 * to delete the flag. -Kev
377 void sendcmdto_common_channels_butone(struct Client *from, const char *cmd,
378 const char *tok, struct Client *one,
379 const char *pattern, ...)
383 struct Membership *chan;
384 struct Membership *member;
387 assert(0 != cli_from(from));
388 assert(0 != pattern);
389 assert(!IsServer(from) && !IsMe(from));
391 vd.vd_format = pattern; /* set up the struct VarData for %v */
393 va_start(vd.vd_args, pattern);
395 /* build the buffer */
396 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
400 if (-1 < cli_fd(cli_from(from)))
401 sentalong[cli_fd(cli_from(from))] = sentalong_marker;
403 * loop through from's channels, and the members on their channels
405 for (chan = cli_user(from)->channel; chan; chan = chan->next_channel) {
406 if (IsZombie(chan) || IsDelayedJoin(chan))
408 for (member = chan->channel->members; member;
409 member = member->next_member)
410 if (MyConnect(member->user) && -1 < cli_fd(cli_from(member->user)) &&
411 member->user != one &&
412 sentalong[cli_fd(cli_from(member->user))] != sentalong_marker) {
413 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
414 send_buffer(member->user, mb, 0);
418 if (MyConnect(from) && from != one)
419 send_buffer(from, mb, 0);
425 * Send a (prefixed) command to all local users on the channel specified
426 * by <to>; <tok> is ignored by this function
428 * Update: don't send to 'one', if any. --Vampire
429 * Update: use 'skip' like sendcmdto_channel_butone. --Entrope
431 void sendcmdto_channel_butserv_butone(struct Client *from, const char *cmd,
432 const char *tok, struct Channel *to,
433 struct Client *one, unsigned int skip,
434 const char *pattern, ...)
438 struct Membership *member;
440 vd.vd_format = pattern; /* set up the struct VarData for %v */
441 va_start(vd.vd_args, pattern);
443 /* build the buffer */
444 mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
447 /* send the buffer to each local channel member */
448 for (member = to->members; member; member = member->next_member) {
449 if (!MyConnect(member->user)
450 || member->user == one
452 || (skip & SKIP_DEAF && IsDeaf(member->user))
453 || (skip & SKIP_NONOPS && !IsChanOp(member))
454 || (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)))
456 send_buffer(member->user, mb, 0);
463 * Send a (prefixed) command to all servers with users on the channel
464 * specified by <to>; <cmd> and <skip> are ignored by this function.
466 * XXX sentalong_marker used XXX
468 void sendcmdto_channel_servers_butone(struct Client *from, const char *cmd,
469 const char *tok, struct Channel *to,
470 struct Client *one, unsigned int skip,
471 const char *pattern, ...)
474 struct MsgBuf *serv_mb;
475 struct Membership *member;
477 /* build the buffer */
478 vd.vd_format = pattern;
479 va_start(vd.vd_args, pattern);
480 serv_mb = msgq_make(&me, "%:#C %s %v", from, tok, &vd);
483 /* send the buffer to each server */
485 for (member = to->members; member; member = member->next_member) {
486 if (cli_from(member->user) == one
487 || MyConnect(member->user)
489 || cli_fd(cli_from(member->user)) < 0
490 || sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
492 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
493 send_buffer(member->user, serv_mb, 0);
500 * Send a (prefixed) command to all users on this channel, including
501 * remote users; users to skip may be specified by setting appropriate
502 * flags in the <skip> argument. <one> will also be skipped.
504 /* XXX sentalong_marker used XXX
506 * We can drop sentalong_marker from this function by adding a field to
507 * channels and to connections; what we do is make a user's connection
508 * a "member" of the channel by adding it to the new list, and we use
509 * the struct Membership status as a reference count. Then, to implement
510 * this function, we just walk the list of connections. Unfortunately,
511 * this doesn't account for sending only to channel ops, or for not
512 * sending to +d users; we could account for that by splitting those
513 * counts out, but that would imply adding two more fields (at least) to
514 * the struct Membership... -Kev
516 void sendcmdto_channel_butone(struct Client *from, const char *cmd,
517 const char *tok, struct Channel *to,
518 struct Client *one, unsigned int skip,
519 const char *pattern, ...)
521 struct Membership *member;
523 struct MsgBuf *user_mb;
524 struct MsgBuf *serv_mb;
526 vd.vd_format = pattern;
528 /* Build buffer to send to users */
529 va_start(vd.vd_args, pattern);
530 user_mb = msgq_make(0, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? "%:#C %s @%v" : "%:#C %s %v",
531 from, skip & (SKIP_NONOPS | SKIP_NONVOICES) ? MSG_NOTICE : cmd, &vd);
534 /* Build buffer to send to servers */
535 va_start(vd.vd_args, pattern);
536 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
539 /* send buffer along! */
541 for (member = to->members; member; member = member->next_member) {
542 /* skip one, zombies, and deaf users... */
543 if (cli_from(member->user) == one || IsZombie(member) ||
544 (skip & SKIP_DEAF && IsDeaf(member->user)) ||
545 (skip & SKIP_NONOPS && !IsChanOp(member)) ||
546 (skip & SKIP_NONVOICES && !IsChanOp(member) && !HasVoice(member)) ||
547 (skip & SKIP_BURST && IsBurstOrBurstAck(cli_from(member->user))) ||
548 cli_fd(cli_from(member->user)) < 0 ||
549 sentalong[cli_fd(cli_from(member->user))] == sentalong_marker)
551 sentalong[cli_fd(cli_from(member->user))] = sentalong_marker;
553 if (MyConnect(member->user)) /* pick right buffer to send */
554 send_buffer(member->user, user_mb, 0);
556 send_buffer(member->user, serv_mb, 0);
564 * Send a (prefixed) command to all users except <one> that have
567 void sendwallto_group_butone(struct Client *from, int type, struct Client *one,
568 const char *pattern, ...)
578 vd.vd_format = pattern;
580 /* Build buffer to send to users */
581 va_start(vd.vd_args, pattern);
598 mb = msgq_make(0, "%:#C " MSG_WALLOPS " :%s%v", from, prefix,&vd);
601 /* send buffer along! */
602 for (i = 0; i <= HighestFd; i++)
604 if (!(cptr = LocalClientArray[i]) ||
605 (cli_fd(cli_from(cptr)) < 0) ||
606 (type == WALL_DESYNCH && !HasFlag(cptr, FLAG_DEBUG)) ||
607 (type == WALL_WALLOPS &&
608 (!HasFlag(cptr, FLAG_WALLOP) || (feature_bool(FEAT_HIS_WALLOPS) &&
609 !IsAnOper(cptr)))) ||
610 (type == WALL_WALLUSERS && !HasFlag(cptr, FLAG_WALLOP)))
611 continue; /* skip it */
612 send_buffer(cptr, mb, 1);
617 /* Build buffer to send to servers */
618 va_start(vd.vd_args, pattern);
619 mb = msgq_make(&me, "%C %s :%v", from, tok, &vd);
622 /* send buffer along! */
623 for (lp = cli_serv(&me)->down; lp; lp = lp->next) {
624 if (one && lp->value.cptr == cli_from(one))
626 send_buffer(lp->value.cptr, mb, 1);
633 * Send a (prefixed) command to all users who match <to>, under control
636 /* XXX sentalong_marker used XXX
638 * This is also a difficult one to solve. The basic approach would be
639 * to walk the client list of each connected server until we find a
640 * match--but then, we also have to walk the client list of all the
641 * servers behind that one. We could implement this recursively--or we
642 * could add (yet another) field to the connection struct that would be
643 * a linked list of clients introduced through that link, and just walk
644 * that, making this into an iterative implementation. Unfortunately,
645 * we probably would not be able to use tail recursion for the recursive
646 * solution, so a deep network could exhaust our stack space; therefore
647 * I favor the extra linked list, even though that increases the
648 * complexity of the database. -Kev
650 void sendcmdto_match_butone(struct Client *from, const char *cmd,
651 const char *tok, const char *to,
652 struct Client *one, unsigned int who,
653 const char *pattern, ...)
657 struct MsgBuf *user_mb;
658 struct MsgBuf *serv_mb;
660 vd.vd_format = pattern;
662 /* Build buffer to send to users */
663 va_start(vd.vd_args, pattern);
664 user_mb = msgq_make(0, "%:#C %s %v", from, cmd, &vd);
667 /* Build buffer to send to servers */
668 va_start(vd.vd_args, pattern);
669 serv_mb = msgq_make(&me, "%C %s %v", from, tok, &vd);
672 /* send buffer along */
674 for (cptr = GlobalClientList; cptr; cptr = cli_next(cptr)) {
675 if (!IsRegistered(cptr) || cli_from(cptr) == one || IsServer(cptr) ||
676 IsMe(cptr) || !match_it(from, cptr, to, who) || cli_fd(cli_from(cptr)) < 0 ||
677 sentalong[cli_fd(cli_from(cptr))] == sentalong_marker)
678 continue; /* skip it */
679 sentalong[cli_fd(cli_from(cptr))] = sentalong_marker;
681 if (MyConnect(cptr)) /* send right buffer */
682 send_buffer(cptr, user_mb, 0);
684 send_buffer(cptr, serv_mb, 0);
692 * Send a server notice to all users subscribing to the indicated <mask>
695 void sendto_opmask_butone(struct Client *one, unsigned int mask,
696 const char *pattern, ...)
700 va_start(vl, pattern);
701 vsendto_opmask_butone(one, mask, pattern, vl);
706 * Send a server notice to all users subscribing to the indicated <mask>
707 * except for <one> - Ratelimited 1 / 30sec
709 void sendto_opmask_butone_ratelimited(struct Client *one, unsigned int mask,
710 time_t *rate, const char *pattern, ...)
714 if ((CurrentTime - *rate) < 30)
719 va_start(vl, pattern);
720 vsendto_opmask_butone(one, mask, pattern, vl);
727 * Same as above, except called with a variable argument list
729 void vsendto_opmask_butone(struct Client *one, unsigned int mask,
730 const char *pattern, va_list vl)
734 int i = 0; /* so that 1 points to opsarray[0] */
735 struct SLink *opslist;
740 if (!(opslist = opsarray[i]))
744 * build string; I don't want to bother with client nicknames, so I hope
747 vd.vd_format = pattern;
748 va_copy(vd.vd_args, vl);
749 mb = msgq_make(0, ":%s " MSG_NOTICE " * :*** Notice -- %v", cli_name(&me),
752 for (; opslist; opslist = opslist->next)
753 if (opslist->value.cptr != one)
754 send_buffer(opslist->value.cptr, mb, 0);