ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/src/send.c
Revision: 430
Committed: Sat Feb 11 12:52:34 2006 UTC (19 years, 6 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/src/send.c
File size: 34416 byte(s)
Log Message:
- Fixed same problem for SSL_write.  Some interesting reference
  about the problem we experienced can be found at
  http://www.mail-archive.com/openssl-dev@openssl.org/msg13939.html


File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * send.c: Functions for sending messages.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "tools.h"
27 #include "send.h"
28 #include "channel.h"
29 #include "client.h"
30 #include "common.h"
31 #include "dbuf.h"
32 #include "irc_string.h"
33 #include "ircd.h"
34 #include "handlers.h"
35 #include "numeric.h"
36 #include "fdlist.h"
37 #include "s_bsd.h"
38 #include "s_serv.h"
39 #include "sprintf_irc.h"
40 #include "s_conf.h"
41 #include "list.h"
42 #include "s_log.h"
43 #include "memory.h"
44 #include "hook.h"
45 #include "irc_getnameinfo.h"
46 #include "packet.h"
47
48 #define LOG_BUFSIZE 2048
49
50 struct Callback *iosend_cb = NULL;
51 struct Callback *iosendctrl_cb = NULL;
52
53 static void send_message(struct Client *, char *, int);
54 static void send_message_remote(struct Client *, struct Client *, char *, int);
55
56 static unsigned long current_serial = 0L;
57
58 /* send_format()
59 *
60 * inputs - buffer to format into
61 * - size of the buffer
62 * - format pattern to use
63 * - var args
64 * output - number of bytes formatted output
65 * side effects - modifies sendbuf
66 */
67 static inline int
68 send_format(char *lsendbuf, int bufsize, const char *pattern, va_list args)
69 {
70 int len;
71
72 /*
73 * from rfc1459
74 *
75 * IRC messages are always lines of characters terminated with a CR-LF
76 * (Carriage Return - Line Feed) pair, and these messages shall not
77 * exceed 512 characters in length, counting all characters
78 * including the trailing CR-LF.
79 * Thus, there are 510 characters maximum allowed
80 * for the command and its parameters. There is no provision for
81 * continuation message lines. See section 7 for more details about
82 * current implementations.
83 */
84 len = vsnprintf(lsendbuf, bufsize - 1, pattern, args);
85 if (len > bufsize - 2)
86 len = bufsize - 2; /* required by some versions of vsnprintf */
87
88 lsendbuf[len++] = '\r';
89 lsendbuf[len++] = '\n';
90 return (len);
91 }
92
93 /*
94 * iosend_default - append a packet to the client's sendq.
95 */
96 void *
97 iosend_default(va_list args)
98 {
99 struct Client *to = va_arg(args, struct Client *);
100 int length = va_arg(args, int);
101 char *buf = va_arg(args, char *);
102
103 dbuf_put(&to->localClient->buf_sendq, buf, length);
104 return NULL;
105 }
106
107 /*
108 ** send_message
109 ** Internal utility which appends given buffer to the sockets
110 ** sendq.
111 */
112 static void
113 send_message(struct Client *to, char *buf, int len)
114 {
115 #ifdef INVARIANTS
116 if (IsMe(to))
117 {
118 sendto_realops_flags(UMODE_ALL, L_ALL,
119 "Trying to send message to myself!");
120 return;
121 }
122 #endif
123
124 if (dbuf_length(&to->localClient->buf_sendq) + len > get_sendq(to))
125 {
126 if (IsServer(to))
127 sendto_realops_flags(UMODE_ALL, L_ALL,
128 "Max SendQ limit exceeded for %s: %lu > %lu",
129 get_client_name(to, HIDE_IP),
130 (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + len),
131 get_sendq(to));
132 if (IsClient(to))
133 SetSendQExceeded(to);
134 dead_link_on_write(to, 0);
135 return;
136 }
137
138 execute_callback(iosend_cb, to, len, buf);
139
140 /*
141 ** Update statistics. The following is slightly incorrect
142 ** because it counts messages even if queued, but bytes
143 ** only really sent. Queued bytes get updated in SendQueued.
144 */
145 ++to->localClient->send.messages;
146 ++me.localClient->send.messages;
147
148 if (dbuf_length(&to->localClient->buf_sendq) >
149 (IsServer(to) ? (unsigned int) 1024 : (unsigned int) 4096))
150 send_queued_write(to);
151 }
152
153 /* send_message_remote()
154 *
155 * inputs - pointer to client from message is being sent
156 * - pointer to client to send to
157 * - pointer to preformatted buffer
158 * - length of input buffer
159 * output - none
160 * side effects - Despite the function name, this only sends to directly
161 * connected clients.
162 *
163 */
164 static void
165 send_message_remote(struct Client *to, struct Client *from,
166 char *buf, int len)
167 {
168 if (!MyConnect(to))
169 {
170 sendto_realops_flags(UMODE_ALL, L_ALL,
171 "server send message to %s [%s] dropped from %s(Not local server)",
172 to->name, to->from->name, from->name);
173 return;
174 }
175
176 if (ServerInfo.hub && IsCapable(to, CAP_LL))
177 {
178 if (((from->lazyLinkClientExists &
179 to->localClient->serverMask) == 0))
180 client_burst_if_needed(to, from);
181 }
182
183 /* Optimize by checking if (from && to) before everything */
184 /* we set to->from up there.. */
185
186 if (!MyClient(from) && IsClient(to) && (to == from->from))
187 {
188 if (IsServer(from))
189 {
190 sendto_realops_flags(UMODE_ALL, L_ALL,
191 "Send message to %s [%s] dropped from %s(Fake Dir)",
192 to->name, to->from->name, from->name);
193 return;
194 }
195
196 sendto_realops_flags(UMODE_ALL, L_ALL,
197 "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
198 to->name, to->username, to->host,
199 from->name, from->username, from->host,
200 to->from->name);
201
202 sendto_server(NULL, to, NULL, CAP_TS6, NOCAPS, NOFLAGS,
203 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
204 me.id, to->name, me.name, to->name,
205 to->username, to->host, to->from->name);
206 sendto_server(NULL, to, NULL, NOCAPS, CAP_TS6, NOFLAGS,
207 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
208 me.name, to->name, me.name, to->name,
209 to->username, to->host, to->from->name);
210
211 SetKilled(to);
212
213 if (IsClient(from))
214 sendto_one(from, form_str(ERR_GHOSTEDCLIENT),
215 me.name, from->name, to->name, to->username,
216 to->host, to->from);
217
218 exit_client(to, &me, "Ghosted client");
219
220 return;
221 }
222
223 send_message(to, buf, len);
224 }
225
226 /*
227 ** sendq_unblocked
228 ** Called when a socket is ready for writing.
229 */
230 void
231 sendq_unblocked(fde_t *fd, struct Client *client_p)
232 {
233 ClearSendqBlocked(client_p);
234 /* let send_queued_write be executed by send_queued_all */
235
236 #ifdef HAVE_LIBCRYPTO
237 if (fd->flags.pending_read)
238 {
239 fd->flags.pending_read = 0;
240 read_packet(fd, client_p);
241 }
242 #endif
243 }
244
245 /*
246 ** slinkq_unblocked
247 ** Called when a server control socket is ready for writing.
248 */
249 static void
250 slinkq_unblocked(fde_t *fd, struct Client *client_p)
251 {
252 ClearSlinkqBlocked(client_p);
253 send_queued_slink_write(client_p);
254 }
255
256 /*
257 ** send_queued_write
258 ** This is called when there is a chance that some output would
259 ** be possible. This attempts to empty the send queue as far as
260 ** possible, and then if any data is left, a write is rescheduled.
261 */
262 void
263 send_queued_write(struct Client *to)
264 {
265 int retlen;
266 struct dbuf_block *first;
267
268 /*
269 ** Once socket is marked dead, we cannot start writing to it,
270 ** even if the error is removed...
271 */
272 if (IsDead(to) || IsSendqBlocked(to))
273 return; /* no use calling send() now */
274
275 /* Next, lets try to write some data */
276
277 if (dbuf_length(&to->localClient->buf_sendq))
278 {
279 do {
280 first = to->localClient->buf_sendq.blocks.head->data;
281
282 #ifdef HAVE_LIBCRYPTO
283 if (to->localClient->fd.ssl)
284 {
285 retlen = SSL_write(to->localClient->fd.ssl, first->data, first->size);
286
287 /* translate openssl error codes, sigh */
288 if (retlen < 0)
289 switch (SSL_get_error(to->localClient->fd.ssl, retlen))
290 {
291 case SSL_ERROR_WANT_READ:
292 return; /* retry later, don't register for write events */
293
294 case SSL_ERROR_WANT_WRITE:
295 errno = EWOULDBLOCK;
296 case SSL_ERROR_SYSCALL:
297 break;
298 case SSL_ERROR_SSL:
299 if (errno == EAGAIN)
300 break;
301 default:
302 retlen = errno = 0; /* either an SSL-specific error or EOF */
303 }
304 }
305 else
306 #endif
307 retlen = send(to->localClient->fd.fd, first->data, first->size, 0);
308
309 if (retlen <= 0)
310 {
311 #ifdef _WIN32
312 errno = WSAGetLastError();
313 #endif
314 break;
315 }
316
317 dbuf_delete(&to->localClient->buf_sendq, retlen);
318
319 /* We have some data written .. update counters */
320 to->localClient->send.bytes += retlen;
321 me.localClient->send.bytes += retlen;
322 } while (dbuf_length(&to->localClient->buf_sendq));
323
324 if ((retlen < 0) && (ignoreErrno(errno)))
325 {
326 /* we have a non-fatal error, reschedule a write */
327 SetSendqBlocked(to);
328 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
329 (PF *)sendq_unblocked, (void *)to, 0);
330 }
331 else if (retlen <= 0)
332 {
333 dead_link_on_write(to, errno);
334 return;
335 }
336 }
337 }
338
339 /*
340 ** send_queued_slink_write
341 ** This is called when there is a chance the some output would
342 ** be possible. This attempts to empty the send queue as far as
343 ** possible, and then if any data is left, a write is rescheduled.
344 */
345 void
346 send_queued_slink_write(struct Client *to)
347 {
348 int retlen;
349
350 /*
351 ** Once socket is marked dead, we cannot start writing to it,
352 ** even if the error is removed...
353 */
354 if (IsDead(to) || IsSlinkqBlocked(to))
355 return; /* no use calling send() now */
356
357 /* Next, lets try to write some data */
358 if (to->localClient->slinkq != NULL)
359 {
360 retlen = send(to->localClient->ctrlfd.fd,
361 to->localClient->slinkq + to->localClient->slinkq_ofs,
362 to->localClient->slinkq_len, 0);
363 if (retlen < 0)
364 {
365 /* If we have a fatal error */
366 if (!ignoreErrno(errno))
367 {
368 dead_link_on_write(to, errno);
369 return;
370 }
371 }
372 else if (retlen == 0)
373 {
374 /* 0 bytes is an EOF .. */
375 dead_link_on_write(to, 0);
376 return;
377 }
378 else
379 {
380 execute_callback(iosendctrl_cb, to, retlen,
381 to->localClient->slinkq + to->localClient->slinkq_ofs);
382 to->localClient->slinkq_len -= retlen;
383
384 assert(to->localClient->slinkq_len >= 0);
385 if (to->localClient->slinkq_len)
386 to->localClient->slinkq_ofs += retlen;
387 else
388 {
389 to->localClient->slinkq_ofs = 0;
390 MyFree(to->localClient->slinkq);
391 to->localClient->slinkq = NULL;
392 }
393 }
394
395 /* Finally, if we have any more data, reschedule a write */
396 if (to->localClient->slinkq_len)
397 {
398 SetSlinkqBlocked(to);
399 comm_setselect(&to->localClient->ctrlfd, COMM_SELECT_WRITE,
400 (PF *)slinkq_unblocked, (void *)to, 0);
401 }
402 }
403 }
404
405 /* send_queued_all()
406 *
407 * input - NONE
408 * output - NONE
409 * side effects - try to flush sendq of each client
410 */
411 void
412 send_queued_all(void)
413 {
414 dlink_node *ptr;
415
416 /* Servers are processed first, mainly because this can generate
417 * a notice to opers, which is to be delivered by this function.
418 */
419 DLINK_FOREACH(ptr, serv_list.head)
420 send_queued_write((struct Client *) ptr->data);
421
422 DLINK_FOREACH(ptr, unknown_list.head)
423 send_queued_write((struct Client *) ptr->data);
424
425 DLINK_FOREACH(ptr, local_client_list.head)
426 send_queued_write((struct Client *) ptr->data);
427
428 /* NOTE: This can still put clients on aborted_list; unfortunately,
429 * exit_aborted_clients takes precedence over send_queued_all,
430 * because exiting clients can generate server/oper traffic.
431 * The easiest approach is dealing with aborted clients in the next I/O lap.
432 * -adx
433 */
434 }
435
436 /* sendto_one()
437 *
438 * inputs - pointer to destination client
439 * - var args message
440 * output - NONE
441 * side effects - send message to single client
442 */
443 void
444 sendto_one(struct Client *to, const char *pattern, ...)
445 {
446 va_list args;
447 char buffer[IRCD_BUFSIZE];
448 int len;
449
450 if (to->from != NULL)
451 to = to->from;
452 if (IsDead(to))
453 return; /* This socket has already been marked as dead */
454
455 va_start(args, pattern);
456 len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
457 va_end(args);
458
459 send_message(to, buffer, len);
460 }
461
462 /* sendto_channel_butone()
463 *
464 * inputs - pointer to client(server) to NOT send message to
465 * - pointer to client that is sending this message
466 * - pointer to channel being sent to
467 * - vargs message
468 * output - NONE
469 * side effects - message as given is sent to given channel members.
470 *
471 * WARNING - +D clients are ignored
472 */
473 void
474 sendto_channel_butone(struct Client *one, struct Client *from,
475 struct Channel *chptr, const char *command,
476 const char *pattern, ...)
477 {
478 va_list alocal, aremote, auid;
479 char local_buf[IRCD_BUFSIZE];
480 char remote_buf[IRCD_BUFSIZE];
481 char uid_buf[IRCD_BUFSIZE];
482 int local_len, remote_len, uid_len;
483 dlink_node *ptr;
484 dlink_node *ptr_next;
485 struct Client *target_p;
486
487 if (IsServer(from))
488 local_len = ircsprintf(local_buf, ":%s %s %s ",
489 from->name, command, chptr->chname);
490 else
491 local_len = ircsprintf(local_buf, ":%s!%s@%s %s %s ",
492 from->name, from->username, from->host,
493 command, chptr->chname);
494 remote_len = ircsprintf(remote_buf, ":%s %s %s ",
495 from->name, command, chptr->chname);
496 uid_len = ircsprintf(uid_buf, ":%s %s %s ",
497 ID(from), command, chptr->chname);
498
499 va_start(alocal, pattern);
500 va_start(aremote, pattern);
501 va_start(auid, pattern);
502 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
503 pattern, alocal);
504 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
505 pattern, aremote);
506 uid_len += send_format(&uid_buf[uid_len], IRCD_BUFSIZE - uid_len, pattern,
507 auid);
508 va_end(auid);
509 va_end(aremote);
510 va_end(alocal);
511
512 ++current_serial;
513
514 DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
515 {
516 target_p = ((struct Membership *)ptr->data)->client_p;
517 assert(target_p != NULL);
518
519 if (IsDefunct(target_p) || IsDeaf(target_p) || target_p->from == one)
520 continue;
521
522 if (MyClient(target_p))
523 {
524 if (target_p->serial != current_serial)
525 {
526 send_message(target_p, local_buf, local_len);
527 target_p->serial = current_serial;
528 }
529 }
530 else
531 {
532 /* Now check whether a message has been sent to this
533 * remote link already
534 */
535 if (target_p->from->serial != current_serial)
536 {
537 if (IsCapable(target_p->from, CAP_TS6))
538 send_message_remote(target_p->from, from, uid_buf, uid_len);
539 else
540 send_message_remote(target_p->from, from, remote_buf, remote_len);
541 target_p->from->serial = current_serial;
542 }
543 }
544 }
545 }
546
547 /* sendto_server()
548 *
549 * inputs - pointer to client to NOT send to
550 * - pointer to source client required by LL (if any)
551 * - pointer to channel required by LL (if any)
552 * - caps or'd together which must ALL be present
553 * - caps or'd together which must ALL NOT be present
554 * - LL flags: LL_ICLIENT | LL_ICHAN
555 * - printf style format string
556 * - args to format string
557 * output - NONE
558 * side effects - Send a message to all connected servers, except the
559 * client 'one' (if non-NULL), as long as the servers
560 * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
561 * If the server is a lazylink client, then it must know
562 * about source_p if non-NULL (unless LL_ICLIENT is specified,
563 * when source_p will be introduced where required) and
564 * chptr if non-NULL (unless LL_ICHANNEL is specified, when
565 * chptr will be introduced where required).
566 * Note: nothing will be introduced to a LazyLeaf unless
567 * the message is actually sent.
568 *
569 * This function was written in an attempt to merge together the other
570 * billion sendto_*serv*() functions, which sprung up with capabs,
571 * lazylinks, uids, etc.
572 * -davidt
573 */
574 void
575 sendto_server(struct Client *one, struct Client *source_p,
576 struct Channel *chptr, unsigned long caps,
577 unsigned long nocaps, unsigned long llflags,
578 const char *format, ...)
579 {
580 va_list args;
581 struct Client *client_p;
582 dlink_node *ptr;
583 char buffer[IRCD_BUFSIZE];
584 int len;
585
586 if (chptr != NULL)
587 {
588 if (chptr->chname[0] != '#')
589 return;
590 }
591
592 va_start(args, format);
593 len = send_format(buffer, IRCD_BUFSIZE, format, args);
594 va_end(args);
595
596 DLINK_FOREACH(ptr, serv_list.head)
597 {
598 client_p = ptr->data;
599
600 /* If dead already skip */
601 if (IsDead(client_p))
602 continue;
603 /* check against 'one' */
604 if (one != NULL && (client_p == one->from))
605 continue;
606 /* check we have required capabs */
607 if ((client_p->localClient->caps & caps) != caps)
608 continue;
609 /* check we don't have any forbidden capabs */
610 if ((client_p->localClient->caps & nocaps) != 0)
611 continue;
612
613 if (ServerInfo.hub && IsCapable(client_p, CAP_LL))
614 {
615 /* check LL channel */
616 if (chptr != NULL &&
617 ((chptr->lazyLinkChannelExists &
618 client_p->localClient->serverMask) == 0))
619 {
620 /* Only introduce the channel if we really will send this message */
621 if (!(llflags & LL_ICLIENT) && source_p &&
622 ((source_p->lazyLinkClientExists &
623 client_p->localClient->serverMask) == 0))
624 continue; /* we can't introduce the unknown source_p, skip */
625
626 if (llflags & LL_ICHAN)
627 burst_channel(client_p, chptr);
628 else
629 continue; /* we can't introduce the unknown chptr, skip */
630 }
631 /* check LL client */
632 if (source_p &&
633 ((source_p->lazyLinkClientExists &
634 client_p->localClient->serverMask) == 0))
635 {
636 if (llflags & LL_ICLIENT)
637 client_burst_if_needed(client_p,source_p);
638 else
639 continue; /* we can't introduce the unknown source_p, skip */
640 }
641 }
642 send_message(client_p, buffer, len);
643 }
644 }
645
646 /* sendto_common_channels_local()
647 *
648 * inputs - pointer to client
649 * - pattern to send
650 * output - NONE
651 * side effects - Sends a message to all people on local server who are
652 * in same channel with user.
653 * used by m_nick.c and exit_one_client.
654 */
655 void
656 sendto_common_channels_local(struct Client *user, int touser,
657 const char *pattern, ...)
658 {
659 va_list args;
660 dlink_node *uptr;
661 dlink_node *cptr;
662 struct Channel *chptr;
663 struct Membership *ms;
664 struct Client *target_p;
665 char buffer[IRCD_BUFSIZE];
666 int len;
667
668 va_start(args, pattern);
669 len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
670 va_end(args);
671
672 ++current_serial;
673
674 DLINK_FOREACH(cptr, user->channel.head)
675 {
676 chptr = ((struct Membership *) cptr->data)->chptr;
677 assert(chptr != NULL);
678
679 DLINK_FOREACH(uptr, chptr->locmembers.head)
680 {
681 ms = uptr->data;
682 target_p = ms->client_p;
683 assert(target_p != NULL);
684
685 if (target_p == user || IsDefunct(target_p) ||
686 target_p->serial == current_serial)
687 continue;
688
689 target_p->serial = current_serial;
690 send_message(target_p, buffer, len);
691 }
692 }
693
694 if (touser && MyConnect(user) && !IsDead(user) &&
695 user->serial != current_serial)
696 send_message(user, buffer, len);
697 }
698
699 /* sendto_channel_local()
700 *
701 * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
702 * - whether to ignore +D clients (YES/NO)
703 * - pointer to channel to send to
704 * - var args pattern
705 * output - NONE
706 * side effects - Send a message to all members of a channel that are
707 * locally connected to this server.
708 */
709 void
710 sendto_channel_local(int type, int nodeaf, struct Channel *chptr,
711 const char *pattern, ...)
712 {
713 va_list args;
714 char buffer[IRCD_BUFSIZE];
715 int len;
716 dlink_node *ptr;
717 struct Membership *ms;
718 struct Client *target_p;
719
720 va_start(args, pattern);
721 len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
722 va_end(args);
723
724 DLINK_FOREACH(ptr, chptr->locmembers.head)
725 {
726 ms = ptr->data;
727 target_p = ms->client_p;
728
729 if (type != 0 && (ms->flags & type) == 0)
730 continue;
731
732 if (IsDefunct(target_p) || (nodeaf && IsDeaf(target_p)))
733 continue;
734
735 send_message(target_p, buffer, len);
736 }
737 }
738
739 /* sendto_channel_local_butone()
740 *
741 * inputs - pointer to client to NOT send message to
742 * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
743 * - pointer to channel to send to
744 * - var args pattern
745 * output - NONE
746 * side effects - Send a message to all members of a channel that are
747 * locally connected to this server except one.
748 *
749 * WARNING - +D clients are omitted
750 */
751 void
752 sendto_channel_local_butone(struct Client *one, int type,
753 struct Channel *chptr, const char *pattern, ...)
754 {
755 va_list args;
756 char buffer[IRCD_BUFSIZE];
757 int len;
758 struct Client *target_p;
759 struct Membership *ms;
760 dlink_node *ptr;
761
762 va_start(args, pattern);
763 len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
764 va_end(args);
765
766 DLINK_FOREACH(ptr, chptr->locmembers.head)
767 {
768 ms = ptr->data;
769 target_p = ms->client_p;
770
771 if (type != 0 && (ms->flags & type) == 0)
772 continue;
773
774 if (target_p == one || IsDefunct(target_p) || IsDeaf(target_p))
775 continue;
776 send_message(target_p, buffer, len);
777 }
778 }
779
780
781 /* sendto_channel_remote()
782 *
783 * inputs - Client not to send towards
784 * - Client from whom message is from
785 * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
786 * - pointer to channel to send to
787 * - var args pattern
788 * output - NONE
789 * side effects - Send a message to all members of a channel that are
790 * remote to this server.
791 */
792 void
793 sendto_channel_remote(struct Client *one, struct Client *from, int type, int caps,
794 int nocaps, struct Channel *chptr, const char *pattern, ...)
795 {
796 va_list args;
797 char buffer[IRCD_BUFSIZE];
798 int len;
799 dlink_node *ptr;
800 struct Client *target_p;
801 struct Membership *ms;
802
803 va_start(args, pattern);
804 len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
805 va_end(args);
806
807 ++current_serial;
808
809 DLINK_FOREACH(ptr, chptr->members.head)
810 {
811 ms = ptr->data;
812 target_p = ms->client_p;
813
814 if (type != 0 && (ms->flags & type) == 0)
815 continue;
816
817 if (MyConnect(target_p))
818 continue;
819 target_p = target_p->from;
820
821 if (target_p == one->from ||
822 ((target_p->from->localClient->caps & caps) != caps) ||
823 ((target_p->from->localClient->caps & nocaps) != 0))
824 continue;
825 if (target_p->from->serial != current_serial)
826 {
827 send_message(target_p, buffer, len);
828 target_p->from->serial = current_serial;
829 }
830 }
831 }
832
833 /*
834 ** match_it() and sendto_match_butone() ARE only used
835 ** to send a msg to all ppl on servers/hosts that match a specified mask
836 ** (used for enhanced PRIVMSGs) for opers
837 **
838 ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
839 **
840 */
841
842 /* match_it()
843 *
844 * inputs - client pointer to match on
845 * - actual mask to match
846 * - what to match on, HOST or SERVER
847 * output - 1 or 0 if match or not
848 * side effects - NONE
849 */
850 static int
851 match_it(const struct Client *one, const char *mask, int what)
852 {
853 if (what == MATCH_HOST)
854 return(match(mask, one->host));
855
856 return(match(mask, one->servptr->name));
857 }
858
859 /* sendto_match_butone()
860 *
861 * Send to all clients which match the mask in a way defined on 'what';
862 * either by user hostname or user servername.
863 *
864 * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
865 */
866 void
867 sendto_match_butone(struct Client *one, struct Client *from, char *mask,
868 int what, const char *pattern, ...)
869 {
870 va_list alocal, aremote;
871 struct Client *client_p;
872 dlink_node *ptr, *ptr_next;
873 char local_buf[IRCD_BUFSIZE], remote_buf[IRCD_BUFSIZE];
874 int local_len = ircsprintf(local_buf, ":%s!%s@%s ", from->name,
875 from->username, from->host);
876 int remote_len = ircsprintf(remote_buf, ":%s ", from->name);
877
878 va_start(alocal, pattern);
879 va_start(aremote, pattern);
880 local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
881 pattern, alocal);
882 remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
883 pattern, aremote);
884 va_end(aremote);
885 va_end(alocal);
886
887 /* scan the local clients */
888 DLINK_FOREACH(ptr, local_client_list.head)
889 {
890 client_p = ptr->data;
891
892 if (client_p != one && !IsDefunct(client_p) &&
893 match_it(client_p, mask, what))
894 send_message(client_p, local_buf, local_len);
895 }
896
897 /* Now scan servers */
898 DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
899 {
900 client_p = ptr->data;
901
902 /*
903 * The old code looped through every client on the
904 * network for each server to check if the
905 * server (client_p) has at least 1 client matching
906 * the mask, using something like:
907 *
908 * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
909 * if (IsRegisteredUser(target_p) &&
910 * match_it(target_p, mask, what) &&
911 * (target_p->from == client_p))
912 * vsendto_prefix_one(client_p, from, pattern, args);
913 *
914 * That way, we wouldn't send the message to
915 * a server who didn't have a matching client.
916 * However, on a network such as EFNet, that
917 * code would have looped through about 50
918 * servers, and in each loop, loop through
919 * about 50k clients as well, calling match()
920 * in each nested loop. That is a very bad
921 * thing cpu wise - just send the message
922 * to every connected server and let that
923 * server deal with it.
924 * -wnder
925 */
926 if (client_p != one && !IsDefunct(client_p))
927 send_message_remote(client_p, from, remote_buf, remote_len);
928 }
929 }
930
931 /* sendto_match_servs()
932 *
933 * inputs - source client
934 * - mask to send to
935 * - capab needed
936 * - data
937 * outputs - none
938 * side effects - data sent to servers matching with capab
939 */
940 void
941 sendto_match_servs(struct Client *source_p, const char *mask, int cap,
942 const char *pattern, ...)
943 {
944 va_list args;
945 struct Client *target_p;
946 dlink_node *ptr;
947 char buffer[IRCD_BUFSIZE];
948 int found = 0;
949
950 va_start(args, pattern);
951 vsnprintf(buffer, sizeof(buffer), pattern, args);
952 va_end(args);
953
954 current_serial++;
955
956 DLINK_FOREACH(ptr, global_serv_list.head)
957 {
958 target_p = ptr->data;
959
960 /* Do not attempt to send to ourselves, or the source */
961 if (IsMe(target_p) || target_p->from == source_p->from)
962 continue;
963
964 if (target_p->from->serial == current_serial)
965 continue;
966
967 if (match(mask, target_p->name))
968 {
969 /*
970 * if we set the serial here, then we'll never do a
971 * match() again, if !IsCapable()
972 */
973 target_p->from->serial = current_serial;
974 found++;
975
976 if (!IsCapable(target_p->from, cap))
977 continue;
978
979 sendto_anywhere(target_p, source_p, "%s", buffer);
980 }
981 }
982 }
983
984 /* sendto_anywhere()
985 *
986 * inputs - pointer to dest client
987 * - pointer to from client
988 * - varags
989 * output - NONE
990 * side effects - less efficient than sendto_remote and sendto_one
991 * but useful when one does not know where target "lives"
992 */
993 void
994 sendto_anywhere(struct Client *to, struct Client *from,
995 const char *pattern, ...)
996 {
997 va_list args;
998 char buffer[IRCD_BUFSIZE];
999 int len;
1000 struct Client *send_to = (to->from != NULL ? to->from : to);
1001
1002 if (IsDead(send_to))
1003 return;
1004
1005 if (MyClient(to))
1006 {
1007 if (IsServer(from))
1008 {
1009 if (IsCapable(to, CAP_TS6) && HasID(from))
1010 len = ircsprintf(buffer, ":%s ", from->id);
1011 else
1012 len = ircsprintf(buffer, ":%s ", from->name);
1013 }
1014 else
1015 len = ircsprintf(buffer, ":%s!%s@%s ",
1016 from->name, from->username, from->host);
1017 }
1018 else len = ircsprintf(buffer, ":%s ", ID_or_name(from, send_to));
1019
1020 va_start(args, pattern);
1021 len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1022 va_end(args);
1023
1024 if(MyClient(to))
1025 send_message(send_to, buffer, len);
1026 else
1027 send_message_remote(send_to, from, buffer, len);
1028 }
1029
1030 /* sendto_realops_flags()
1031 *
1032 * inputs - flag types of messages to show to real opers
1033 * - flag indicating opers/admins
1034 * - var args input message
1035 * output - NONE
1036 * side effects - Send to *local* ops only but NOT +s nonopers.
1037 */
1038 void
1039 sendto_realops_flags(unsigned int flags, int level, const char *pattern, ...)
1040 {
1041 struct Client *client_p;
1042 char nbuf[IRCD_BUFSIZE];
1043 dlink_node *ptr;
1044 va_list args;
1045
1046 va_start(args, pattern);
1047 vsnprintf(nbuf, IRCD_BUFSIZE, pattern, args);
1048 va_end(args);
1049
1050 DLINK_FOREACH(ptr, oper_list.head)
1051 {
1052 client_p = ptr->data;
1053 assert(client_p->umodes & UMODE_OPER);
1054
1055 /* If we're sending it to opers and theyre an admin, skip.
1056 * If we're sending it to admins, and theyre not, skip.
1057 */
1058 if (((level == L_ADMIN) && !IsAdmin(client_p)) ||
1059 ((level == L_OPER) && IsAdmin(client_p)))
1060 continue;
1061
1062 if (client_p->umodes & flags)
1063 sendto_one(client_p, ":%s NOTICE %s :*** Notice -- %s",
1064 me.name, client_p->name, nbuf);
1065 }
1066 }
1067
1068 /* sendto_wallops_flags()
1069 *
1070 * inputs - flag types of messages to show to real opers
1071 * - client sending request
1072 * - var args input message
1073 * output - NONE
1074 * side effects - Send a wallops to local opers
1075 */
1076 void
1077 sendto_wallops_flags(unsigned int flags, struct Client *source_p,
1078 const char *pattern, ...)
1079 {
1080 struct Client *client_p;
1081 dlink_node *ptr;
1082 va_list args;
1083 char buffer[IRCD_BUFSIZE];
1084 int len;
1085
1086 if (IsClient(source_p))
1087 len = ircsprintf(buffer, ":%s!%s@%s WALLOPS :",
1088 source_p->name, source_p->username, source_p->host);
1089 else
1090 len = ircsprintf(buffer, ":%s WALLOPS :", source_p->name);
1091
1092 va_start(args, pattern);
1093 len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1094 va_end(args);
1095
1096 DLINK_FOREACH(ptr, oper_list.head)
1097 {
1098 client_p = ptr->data;
1099 assert(client_p->umodes & UMODE_OPER);
1100
1101 if ((client_p->umodes & flags) && !IsDefunct(client_p))
1102 send_message(client_p, buffer, len);
1103 }
1104 }
1105
1106 /* ts_warn()
1107 *
1108 * inputs - var args message
1109 * output - NONE
1110 * side effects - Call sendto_realops_flags, with some flood checking
1111 * (at most 5 warnings every 5 seconds)
1112 */
1113 void
1114 ts_warn(const char *pattern, ...)
1115 {
1116 va_list args;
1117 char buffer[LOG_BUFSIZE];
1118 static time_t last = 0;
1119 static int warnings = 0;
1120
1121 /*
1122 ** if we're running with TS_WARNINGS enabled and someone does
1123 ** something silly like (remotely) connecting a nonTS server,
1124 ** we'll get a ton of warnings, so we make sure we don't send
1125 ** more than 5 every 5 seconds. -orabidoo
1126 */
1127
1128 if (CurrentTime - last < 5)
1129 {
1130 if (++warnings > 5)
1131 return;
1132 }
1133 else
1134 {
1135 last = CurrentTime;
1136 warnings = 0;
1137 }
1138
1139 va_start(args, pattern);
1140 vsprintf_irc(buffer, pattern, args);
1141 va_end(args);
1142
1143 sendto_realops_flags(UMODE_ALL, L_ALL, "%s", buffer);
1144 ilog(L_CRIT, "%s", buffer);
1145 }
1146
1147 /* kill_client()
1148 *
1149 * inputs - client to send kill towards
1150 * - pointer to client to kill
1151 * - reason for kill
1152 * output - NONE
1153 * side effects - NONE
1154 */
1155 void
1156 kill_client(struct Client *client_p, struct Client *diedie,
1157 const char *pattern, ...)
1158 {
1159 va_list args;
1160 char buffer[IRCD_BUFSIZE];
1161 int len;
1162
1163 if (client_p->from != NULL)
1164 client_p = client_p->from;
1165 if (IsDead(client_p))
1166 return;
1167
1168 len = ircsprintf(buffer, ":%s KILL %s :", ID_or_name(&me, client_p->from),
1169 ID_or_name(diedie, client_p));
1170
1171 va_start(args, pattern);
1172 len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1173 va_end(args);
1174
1175 send_message(client_p, buffer, len);
1176 }
1177
1178 /* kill_client_ll_serv_butone()
1179 *
1180 * inputs - pointer to client to not send to
1181 * - pointer to client to kill
1182 * output - NONE
1183 * side effects - Send a KILL for the given client
1184 * message to all connected servers
1185 * except the client 'one'. Also deal with
1186 * client being unknown to leaf, as in lazylink...
1187 */
1188 void
1189 kill_client_ll_serv_butone(struct Client *one, struct Client *source_p,
1190 const char *pattern, ...)
1191 {
1192 va_list args;
1193 int have_uid = 0;
1194 struct Client *client_p;
1195 dlink_node *ptr;
1196 char buf_uid[IRCD_BUFSIZE], buf_nick[IRCD_BUFSIZE];
1197 int len_uid = 0, len_nick;
1198
1199 if (HasID(source_p) && (me.id[0] != '\0'))
1200 {
1201 have_uid = 1;
1202 va_start(args, pattern);
1203 len_uid = ircsprintf(buf_uid, ":%s KILL %s :", me.id, ID(source_p));
1204 len_uid += send_format(&buf_uid[len_uid], IRCD_BUFSIZE - len_uid, pattern,
1205 args);
1206 va_end(args);
1207 }
1208
1209 va_start(args, pattern);
1210 len_nick = ircsprintf(buf_nick, ":%s KILL %s :", me.name, source_p->name);
1211 len_nick += send_format(&buf_nick[len_nick], IRCD_BUFSIZE - len_nick, pattern,
1212 args);
1213 va_end(args);
1214
1215 DLINK_FOREACH(ptr, serv_list.head)
1216 {
1217 client_p = ptr->data;
1218
1219 if (one != NULL && (client_p == one->from))
1220 continue;
1221 if (IsDefunct(client_p))
1222 continue;
1223
1224 /* XXX perhaps IsCapable should test for localClient itself ? -db */
1225 if (client_p->localClient == NULL || !IsCapable(client_p, CAP_LL) ||
1226 !ServerInfo.hub ||
1227 (source_p->lazyLinkClientExists & client_p->localClient->serverMask))
1228 {
1229 if (have_uid && IsCapable(client_p, CAP_TS6))
1230 send_message(client_p, buf_uid, len_uid);
1231 else
1232 send_message(client_p, buf_nick, len_nick);
1233 }
1234 }
1235 }

Properties

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