ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
Revision: 3241
Committed: Sun Mar 30 16:45:31 2014 UTC (12 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 26666 byte(s)
Log Message:
- Incorporate Adam's writev() patch

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file send.c
23     * \brief Functions for sending messages.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 1011 #include "list.h"
29 adx 30 #include "send.h"
30     #include "channel.h"
31     #include "client.h"
32     #include "dbuf.h"
33     #include "irc_string.h"
34     #include "ircd.h"
35     #include "numeric.h"
36     #include "fdlist.h"
37     #include "s_bsd.h"
38     #include "s_serv.h"
39 michael 1309 #include "conf.h"
40     #include "log.h"
41 adx 30 #include "memory.h"
42     #include "packet.h"
43    
44 michael 3241 #ifndef IOV_MAX
45     #define IOV_MAX 64
46     #endif
47 adx 30
48 michael 1078 static unsigned int current_serial = 0;
49 adx 30
50    
51     /* send_format()
52     *
53 michael 3120 * inputs
54     * - buffer
55 adx 30 * - format pattern to use
56     * - var args
57     * output - number of bytes formatted output
58     * side effects - modifies sendbuf
59     */
60 michael 3107 static void
61     send_format(struct dbuf_block *buffer, const char *pattern, va_list args)
62 adx 30 {
63     /*
64     * from rfc1459
65     *
66     * IRC messages are always lines of characters terminated with a CR-LF
67     * (Carriage Return - Line Feed) pair, and these messages shall not
68 michael 2638 * exceed 512 characters in length, counting all characters
69 adx 30 * including the trailing CR-LF.
70     * Thus, there are 510 characters maximum allowed
71     * for the command and its parameters. There is no provision for
72     * continuation message lines. See section 7 for more details about
73     * current implementations.
74     */
75 michael 3107 dbuf_put_args(buffer, pattern, args);
76 adx 30
77 michael 3107 if (buffer->size > sizeof(buffer->data) - 2)
78     buffer->size = sizeof(buffer->data) - 2;
79    
80     buffer->data[buffer->size++] = '\r';
81     buffer->data[buffer->size++] = '\n';
82 adx 30 }
83    
84     /*
85     ** send_message
86     ** Internal utility which appends given buffer to the sockets
87     ** sendq.
88     */
89     static void
90 michael 3107 send_message(struct Client *to, struct dbuf_block *buf)
91 adx 30 {
92 michael 439 assert(!IsMe(to));
93 michael 438 assert(to != &me);
94 adx 30
95 michael 3107 if (dbuf_length(&to->localClient->buf_sendq) + buf->size > get_sendq(&to->localClient->confs))
96 adx 30 {
97     if (IsServer(to))
98 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
99 michael 1761 "Max SendQ limit exceeded for %s: %lu > %u",
100 adx 30 get_client_name(to, HIDE_IP),
101 michael 3107 (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + buf->size),
102 michael 1632 get_sendq(&to->localClient->confs));
103 adx 30 if (IsClient(to))
104     SetSendQExceeded(to);
105 michael 3215
106 adx 30 dead_link_on_write(to, 0);
107     return;
108     }
109 michael 2638
110 michael 3107 dbuf_add(&to->localClient->buf_sendq, buf);
111 adx 30
112     /*
113 michael 3215 * Update statistics. The following is slightly incorrect because
114     * it counts messages even if queued, but bytes only really sent.
115     * Queued bytes get updated in send_queued_write().
116 adx 30 */
117     ++to->localClient->send.messages;
118     ++me.localClient->send.messages;
119    
120 michael 2881 send_queued_write(to);
121 adx 30 }
122    
123     /* send_message_remote()
124     *
125     * inputs - pointer to client from message is being sent
126     * - pointer to client to send to
127 michael 3120 * - pointer to buffer
128 adx 30 * output - none
129     * side effects - Despite the function name, this only sends to directly
130     * connected clients.
131 michael 2916 *
132 adx 30 */
133     static void
134 michael 3107 send_message_remote(struct Client *to, struct Client *from, struct dbuf_block *buf)
135 adx 30 {
136 michael 3112 to = to->from;
137 michael 2793
138 adx 30 if (!MyConnect(to))
139     {
140 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
141 michael 2638 "Server send message to %s [%s] dropped from %s(Not local server)",
142     to->name, to->from->name, from->name);
143 adx 30 return;
144     }
145    
146     /* Optimize by checking if (from && to) before everything */
147     /* we set to->from up there.. */
148    
149     if (!MyClient(from) && IsClient(to) && (to == from->from))
150     {
151     if (IsServer(from))
152     {
153 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
154 adx 30 "Send message to %s [%s] dropped from %s(Fake Dir)",
155     to->name, to->from->name, from->name);
156     return;
157     }
158    
159 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
160 adx 30 "Ghosted: %s[%s@%s] from %s[%s@%s] (%s)",
161     to->name, to->username, to->host,
162     from->name, from->username, from->host,
163     to->from->name);
164    
165 michael 3135 sendto_server(NULL, NOCAPS, NOCAPS,
166 adx 30 ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
167 michael 3174 me.id, to->id, me.name, to->name,
168 adx 30 to->username, to->host, to->from->name);
169    
170 michael 1219 AddFlag(to, FLAGS_KILLED);
171 adx 30
172     if (IsClient(from))
173 michael 3109 sendto_one_numeric(from, &me, ERR_GHOSTEDCLIENT, to->name,
174     to->username, to->host, to->from);
175 adx 30
176 michael 3171 exit_client(to, "Ghosted client");
177 adx 30 return;
178 michael 2638 }
179 adx 30
180 michael 3107 send_message(to, buf);
181 adx 30 }
182    
183     /*
184     ** sendq_unblocked
185     ** Called when a socket is ready for writing.
186     */
187     void
188     sendq_unblocked(fde_t *fd, struct Client *client_p)
189     {
190 michael 2881 assert(fd == &client_p->localClient->fd);
191     send_queued_write(client_p);
192 adx 30 }
193    
194 michael 3241 #ifdef HAVE_LIBCRYPTO
195     static int
196     send_SSL_writev(SSL *ssl, const struct iovec *vec, int iovcnt)
197     {
198     int total_written = 0;
199    
200     for (int i = 0; i < iovcnt; ++i)
201     {
202     const struct iovec *iov = &vec[i];
203     int ret = 0;
204    
205     /* Translate openssl error codes, sigh */
206     if ((ret = SSL_write(ssl, iov->iov_base, iov->iov_len)) <= 0)
207     {
208     switch (SSL_get_error(ssl, i))
209     {
210     case SSL_ERROR_WANT_READ:
211     return -1;
212     case SSL_ERROR_WANT_WRITE:
213     errno = EWOULDBLOCK;
214     case SSL_ERROR_SYSCALL:
215     break;
216     case SSL_ERROR_SSL:
217     if (errno == EAGAIN)
218     break;
219     default:
220     errno = 0; /* Either an SSL-specific error or EOF */
221     }
222    
223     return 0;
224     }
225    
226     total_written += ret;
227     }
228    
229     return total_written;
230     }
231     #endif
232    
233 adx 30 /*
234     ** send_queued_write
235     ** This is called when there is a chance that some output would
236     ** be possible. This attempts to empty the send queue as far as
237     ** possible, and then if any data is left, a write is rescheduled.
238     */
239     void
240     send_queued_write(struct Client *to)
241     {
242 michael 3241 int retlen = 0, i = 0;
243     dlink_node *ptr = NULL;
244     struct iovec vec[IOV_MAX];
245 adx 30
246     /*
247 michael 3241 * Once socket is marked dead, we cannot start writing to it,
248     * even if the error is removed...
249 adx 30 */
250 michael 2881 if (IsDead(to))
251 michael 3241 return; /* No use calling send() now */
252 adx 30
253 michael 3241 if (HasFlag(to, FLAGS_CORK))
254     return;
255    
256     /* Nothing to do? */
257     if (!dbuf_length(&to->localClient->buf_sendq))
258     return;
259    
260     /* Build iovec */
261     DLINK_FOREACH(ptr, to->localClient->buf_sendq.blocks.head)
262 adx 30 {
263 michael 3241 struct dbuf_block *block = ptr->data;
264     struct iovec *iov;
265     int offset;
266 adx 30
267 michael 3241 if (i >= sizeof(vec) / sizeof(struct iovec))
268     break;
269    
270     iov = &vec[i];
271     offset = !i ? to->localClient->buf_sendq.pos : 0;
272    
273     iov->iov_base = block->data + offset;
274     iov->iov_len = block->size - offset;
275    
276     ++i;
277     }
278    
279     /* Next, lets try to write some data */
280 adx 30 #ifdef HAVE_LIBCRYPTO
281 michael 3241 if (to->localClient->fd.ssl)
282     {
283     if ((retlen = send_SSL_writev(to->localClient->fd.ssl, vec, i)) == -1)
284     return;
285     }
286     else
287 adx 30 #endif
288 michael 3241 retlen = writev(to->localClient->fd.fd, vec, i);
289 adx 30
290 michael 3241 if (retlen > 0)
291     {
292     dbuf_delete(&to->localClient->buf_sendq, retlen);
293 adx 30
294 michael 3241 /* We have some data written .. update counters */
295     to->localClient->send.bytes += retlen;
296     me.localClient->send.bytes += retlen;
297 adx 30
298 michael 3241 if (dbuf_length(&to->localClient->buf_sendq))
299 michael 3113 comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
300     (PF *)sendq_unblocked, to, 0);
301 adx 30 }
302 michael 3241 else if (retlen < 0 && ignoreErrno(errno))
303     /* we have a non-fatal error, reschedule a write */
304     comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
305     (PF *)sendq_unblocked, to, 0);
306     else if (retlen <= 0)
307     dead_link_on_write(to, errno);
308 adx 30 }
309    
310     /* send_queued_all()
311     *
312     * input - NONE
313     * output - NONE
314     * side effects - try to flush sendq of each client
315     */
316     void
317     send_queued_all(void)
318     {
319     dlink_node *ptr;
320    
321     /* Servers are processed first, mainly because this can generate
322     * a notice to opers, which is to be delivered by this function.
323     */
324     DLINK_FOREACH(ptr, serv_list.head)
325 michael 2638 send_queued_write(ptr->data);
326 adx 30
327     DLINK_FOREACH(ptr, unknown_list.head)
328 michael 2638 send_queued_write(ptr->data);
329 adx 30
330     DLINK_FOREACH(ptr, local_client_list.head)
331 michael 2638 send_queued_write(ptr->data);
332 adx 30
333     /* NOTE: This can still put clients on aborted_list; unfortunately,
334     * exit_aborted_clients takes precedence over send_queued_all,
335     * because exiting clients can generate server/oper traffic.
336     * The easiest approach is dealing with aborted clients in the next I/O lap.
337     * -adx
338     */
339     }
340    
341     /* sendto_one()
342     *
343     * inputs - pointer to destination client
344     * - var args message
345     * output - NONE
346     * side effects - send message to single client
347     */
348     void
349     sendto_one(struct Client *to, const char *pattern, ...)
350     {
351     va_list args;
352 michael 3189 struct dbuf_block *buffer = NULL;
353 adx 30
354 michael 3189 if (IsDead(to->from))
355 michael 3107 return; /* This socket has already been marked as dead */
356 adx 30
357 michael 3107 buffer = dbuf_alloc();
358    
359 adx 30 va_start(args, pattern);
360 michael 3107 send_format(buffer, pattern, args);
361 adx 30 va_end(args);
362    
363 michael 3189 send_message(to->from, buffer);
364 michael 3107
365     dbuf_ref_free(buffer);
366 adx 30 }
367    
368 michael 3109 void
369     sendto_one_numeric(struct Client *to, struct Client *from, enum irc_numerics numeric, ...)
370     {
371 michael 3189 struct dbuf_block *buffer = NULL;
372     const char *dest = NULL;
373 michael 3109 va_list args;
374    
375 michael 3189 if (IsDead(to->from))
376 michael 3109 return;
377    
378     dest = ID_or_name(to, to);
379     if (EmptyString(dest))
380     dest = "*";
381    
382     buffer = dbuf_alloc();
383    
384 michael 3113 dbuf_put_fmt(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric, dest);
385 michael 3109
386     va_start(args, numeric);
387     send_format(buffer, numeric_form(numeric), args);
388     va_end(args);
389    
390 michael 3189 send_message(to->from, buffer);
391 michael 3109
392     dbuf_ref_free(buffer);
393     }
394    
395 michael 3110 void
396     sendto_one_notice(struct Client *to, struct Client *from, const char *pattern, ...)
397     {
398 michael 3189 struct dbuf_block *buffer = NULL;
399     const char *dest = NULL;
400 michael 3110 va_list args;
401    
402 michael 3189 if (IsDead(to->from))
403 michael 3110 return;
404    
405     dest = ID_or_name(to, to);
406     if (EmptyString(dest))
407     dest = "*";
408    
409     buffer = dbuf_alloc();
410    
411 michael 3113 dbuf_put_fmt(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest);
412 michael 3110
413     va_start(args, pattern);
414     send_format(buffer, pattern, args);
415     va_end(args);
416    
417 michael 3189 send_message(to->from, buffer);
418 michael 3110
419     dbuf_ref_free(buffer);
420     }
421    
422 adx 30 /* sendto_channel_butone()
423     *
424     * inputs - pointer to client(server) to NOT send message to
425     * - pointer to client that is sending this message
426     * - pointer to channel being sent to
427     * - vargs message
428     * output - NONE
429     * side effects - message as given is sent to given channel members.
430     *
431     * WARNING - +D clients are ignored
432     */
433     void
434     sendto_channel_butone(struct Client *one, struct Client *from,
435 michael 1479 struct Channel *chptr, unsigned int type,
436 adx 30 const char *pattern, ...)
437     {
438 michael 3136 va_list alocal, aremote;
439     struct dbuf_block *local_buf, *remote_buf;
440 michael 1078 dlink_node *ptr = NULL, *ptr_next = NULL;
441 adx 30
442 michael 3136 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
443 michael 3107
444 adx 30 if (IsServer(from))
445 michael 3113 dbuf_put_fmt(local_buf, ":%s ", from->name);
446 adx 30 else
447 michael 3113 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
448 adx 30
449 michael 3186 dbuf_put_fmt(remote_buf, ":%s ", from->id);
450 michael 3107
451 adx 285 va_start(alocal, pattern);
452     va_start(aremote, pattern);
453 michael 3107 send_format(local_buf, pattern, alocal);
454     send_format(remote_buf, pattern, aremote);
455 michael 3136
456 adx 285 va_end(aremote);
457     va_end(alocal);
458 adx 30
459     ++current_serial;
460    
461     DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->members.head)
462     {
463 michael 1479 struct Membership *ms = ptr->data;
464     struct Client *target_p = ms->client_p;
465 adx 30
466 michael 1078 assert(IsClient(target_p));
467    
468 michael 3164 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) ||
469     (one && target_p->from == one->from))
470 adx 30 continue;
471    
472 michael 1479 if (type != 0 && (ms->flags & type) == 0)
473     continue;
474    
475 michael 1078 if (MyConnect(target_p))
476 michael 3103 send_message(target_p, local_buf);
477     else if (target_p->from->localClient->serial != current_serial)
478     send_message_remote(target_p->from, from, remote_buf);
479     target_p->from->localClient->serial = current_serial;
480 adx 30 }
481 michael 3107
482     dbuf_ref_free(local_buf);
483     dbuf_ref_free(remote_buf);
484 adx 30 }
485    
486     /* sendto_server()
487 michael 2916 *
488 adx 30 * inputs - pointer to client to NOT send to
489 db 939 * - pointer to channel
490 adx 30 * - caps or'd together which must ALL be present
491     * - caps or'd together which must ALL NOT be present
492     * - printf style format string
493     * - args to format string
494     * output - NONE
495     * side effects - Send a message to all connected servers, except the
496     * client 'one' (if non-NULL), as long as the servers
497     * support ALL capabs in 'caps', and NO capabs in 'nocaps'.
498 michael 2916 *
499 adx 30 * This function was written in an attempt to merge together the other
500     * billion sendto_*serv*() functions, which sprung up with capabs,
501     * lazylinks, uids, etc.
502     * -davidt
503     */
504 michael 2638 void
505 michael 1474 sendto_server(struct Client *one,
506 michael 1015 const unsigned int caps,
507     const unsigned int nocaps,
508 adx 30 const char *format, ...)
509     {
510     va_list args;
511 michael 885 dlink_node *ptr = NULL;
512 michael 3107 struct dbuf_block *buffer;
513 adx 30
514 michael 3107 buffer = dbuf_alloc();
515    
516 adx 30 va_start(args, format);
517 michael 3107 send_format(buffer, format, args);
518 adx 30 va_end(args);
519    
520     DLINK_FOREACH(ptr, serv_list.head)
521     {
522 michael 885 struct Client *client_p = ptr->data;
523 adx 30
524     /* If dead already skip */
525     if (IsDead(client_p))
526     continue;
527     /* check against 'one' */
528     if (one != NULL && (client_p == one->from))
529     continue;
530     /* check we have required capabs */
531     if ((client_p->localClient->caps & caps) != caps)
532     continue;
533     /* check we don't have any forbidden capabs */
534     if ((client_p->localClient->caps & nocaps) != 0)
535     continue;
536    
537 michael 3107 send_message(client_p, buffer);
538 adx 30 }
539 michael 3107
540     dbuf_ref_free(buffer);
541 adx 30 }
542    
543     /* sendto_common_channels_local()
544     *
545     * inputs - pointer to client
546     * - pattern to send
547     * output - NONE
548     * side effects - Sends a message to all people on local server who are
549 michael 2916 * in same channel with user.
550 adx 30 * used by m_nick.c and exit_one_client.
551     */
552     void
553 michael 1734 sendto_common_channels_local(struct Client *user, int touser, unsigned int cap,
554 adx 30 const char *pattern, ...)
555     {
556     va_list args;
557     dlink_node *uptr;
558     dlink_node *cptr;
559     struct Channel *chptr;
560     struct Membership *ms;
561     struct Client *target_p;
562 michael 3107 struct dbuf_block *buffer;
563 adx 30
564 michael 3107 buffer = dbuf_alloc();
565    
566 adx 30 va_start(args, pattern);
567 michael 3107 send_format(buffer, pattern, args);
568 adx 30 va_end(args);
569    
570     ++current_serial;
571    
572     DLINK_FOREACH(cptr, user->channel.head)
573     {
574 michael 2638 chptr = ((struct Membership *)cptr->data)->chptr;
575 adx 30 assert(chptr != NULL);
576    
577 michael 572 DLINK_FOREACH(uptr, chptr->members.head)
578 adx 30 {
579     ms = uptr->data;
580     target_p = ms->client_p;
581     assert(target_p != NULL);
582    
583 michael 572 if (!MyConnect(target_p) || target_p == user || IsDefunct(target_p) ||
584 michael 1078 target_p->localClient->serial == current_serial)
585 adx 30 continue;
586    
587 michael 1734 if (HasCap(target_p, cap) != cap)
588     continue;
589    
590 michael 1078 target_p->localClient->serial = current_serial;
591 michael 3107 send_message(target_p, buffer);
592 adx 30 }
593     }
594    
595     if (touser && MyConnect(user) && !IsDead(user) &&
596 michael 1078 user->localClient->serial != current_serial)
597 michael 1844 if (HasCap(user, cap) == cap)
598 michael 3107 send_message(user, buffer);
599    
600     dbuf_ref_free(buffer);
601 adx 30 }
602    
603     /* sendto_channel_local()
604     *
605     * inputs - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
606     * - whether to ignore +D clients (YES/NO)
607     * - pointer to channel to send to
608     * - var args pattern
609     * output - NONE
610     * side effects - Send a message to all members of a channel that are
611     * locally connected to this server.
612     */
613     void
614 michael 2543 sendto_channel_local(unsigned int type, int nodeaf, struct Channel *chptr,
615 adx 30 const char *pattern, ...)
616     {
617     va_list args;
618 michael 2638 dlink_node *ptr = NULL;
619 michael 3107 struct dbuf_block *buffer;
620 adx 30
621 michael 3107 buffer = dbuf_alloc();
622    
623 adx 30 va_start(args, pattern);
624 michael 3107 send_format(buffer, pattern, args);
625 adx 30 va_end(args);
626    
627 michael 572 DLINK_FOREACH(ptr, chptr->members.head)
628 adx 30 {
629 michael 2638 struct Membership *ms = ptr->data;
630     struct Client *target_p = ms->client_p;
631 adx 30
632     if (type != 0 && (ms->flags & type) == 0)
633     continue;
634    
635 michael 572 if (!MyConnect(target_p) || IsDefunct(target_p) ||
636 michael 1219 (nodeaf && HasUMode(target_p, UMODE_DEAF)))
637 adx 30 continue;
638    
639 michael 3107 send_message(target_p, buffer);
640 adx 30 }
641 michael 3107
642     dbuf_ref_free(buffer);
643 adx 30 }
644    
645     /* sendto_channel_local_butone()
646     *
647     * inputs - pointer to client to NOT send message to
648     * - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
649     * - pointer to channel to send to
650     * - var args pattern
651     * output - NONE
652     * side effects - Send a message to all members of a channel that are
653     * locally connected to this server except one.
654     *
655     * WARNING - +D clients are omitted
656     */
657 michael 2638 void
658 michael 2543 sendto_channel_local_butone(struct Client *one, unsigned int type, unsigned int cap,
659 michael 2638 struct Channel *chptr, const char *pattern, ...)
660 adx 30 {
661     va_list args;
662 michael 2638 dlink_node *ptr = NULL;
663 michael 3107 struct dbuf_block *buffer;
664 adx 30
665 michael 3107 buffer = dbuf_alloc();
666    
667 michael 2638 va_start(args, pattern);
668 michael 3107 send_format(buffer, pattern, args);
669 adx 30 va_end(args);
670    
671 michael 2638 DLINK_FOREACH(ptr, chptr->members.head)
672     {
673     struct Membership *ms = ptr->data;
674     struct Client *target_p = ms->client_p;
675 adx 30
676     if (type != 0 && (ms->flags & type) == 0)
677     continue;
678    
679 michael 3170 if (!MyConnect(target_p) || (one && target_p == one->from))
680 adx 30 continue;
681 michael 1734
682 michael 3170 if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
683     continue;
684    
685 michael 1734 if (HasCap(target_p, cap) != cap)
686 michael 2638 continue;
687    
688 michael 3107 send_message(target_p, buffer);
689 adx 30 }
690 michael 3107
691     dbuf_ref_free(buffer);
692 adx 30 }
693    
694     /*
695     ** match_it() and sendto_match_butone() ARE only used
696     ** to send a msg to all ppl on servers/hosts that match a specified mask
697     ** (used for enhanced PRIVMSGs) for opers
698     **
699     ** addition -- Armin, 8jun90 (gruner@informatik.tu-muenchen.de)
700     **
701     */
702    
703     /* match_it()
704     *
705     * inputs - client pointer to match on
706     * - actual mask to match
707     * - what to match on, HOST or SERVER
708     * output - 1 or 0 if match or not
709     * side effects - NONE
710     */
711     static int
712 michael 2867 match_it(const struct Client *one, const char *mask, unsigned int what)
713 adx 30 {
714     if (what == MATCH_HOST)
715 michael 1652 return !match(mask, one->host);
716 adx 30
717 michael 1652 return !match(mask, one->servptr->name);
718 adx 30 }
719    
720     /* sendto_match_butone()
721     *
722     * Send to all clients which match the mask in a way defined on 'what';
723     * either by user hostname or user servername.
724     *
725     * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
726     */
727     void
728 michael 3170 sendto_match_butone(struct Client *one, struct Client *from, const char *mask,
729 adx 30 int what, const char *pattern, ...)
730     {
731 adx 285 va_list alocal, aremote;
732 michael 3107 dlink_node *ptr = NULL, *ptr_next = NULL;
733     struct dbuf_block *local_buf, *remote_buf;
734 adx 30
735 michael 3107 local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
736    
737 michael 3113 dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
738 michael 3186 dbuf_put_fmt(remote_buf, ":%s ", from->id);
739 michael 3107
740 adx 285 va_start(alocal, pattern);
741     va_start(aremote, pattern);
742 michael 3107 send_format(local_buf, pattern, alocal);
743     send_format(remote_buf, pattern, aremote);
744 adx 285 va_end(aremote);
745     va_end(alocal);
746 adx 30
747     /* scan the local clients */
748     DLINK_FOREACH(ptr, local_client_list.head)
749     {
750 michael 3107 struct Client *client_p = ptr->data;
751 adx 30
752 michael 3174 if ((!one || client_p != one->from) && !IsDefunct(client_p) &&
753 adx 30 match_it(client_p, mask, what))
754 michael 3107 send_message(client_p, local_buf);
755 adx 30 }
756    
757     /* Now scan servers */
758     DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
759     {
760 michael 3107 struct Client *client_p = ptr->data;
761 adx 30
762     /*
763     * The old code looped through every client on the
764     * network for each server to check if the
765     * server (client_p) has at least 1 client matching
766     * the mask, using something like:
767     *
768     * for (target_p = GlobalClientList; target_p; target_p = target_p->next)
769     * if (IsRegisteredUser(target_p) &&
770     * match_it(target_p, mask, what) &&
771     * (target_p->from == client_p))
772     * vsendto_prefix_one(client_p, from, pattern, args);
773     *
774     * That way, we wouldn't send the message to
775     * a server who didn't have a matching client.
776     * However, on a network such as EFNet, that
777     * code would have looped through about 50
778     * servers, and in each loop, loop through
779     * about 50k clients as well, calling match()
780     * in each nested loop. That is a very bad
781     * thing cpu wise - just send the message
782     * to every connected server and let that
783     * server deal with it.
784     * -wnder
785     */
786 michael 3170 if ((!one || client_p != one->from) && !IsDefunct(client_p))
787 michael 3107 send_message_remote(client_p, from, remote_buf);
788 adx 30 }
789 michael 3107
790     dbuf_ref_free(local_buf);
791     dbuf_ref_free(remote_buf);
792 adx 30 }
793    
794     /* sendto_match_servs()
795     *
796     * inputs - source client
797     * - mask to send to
798     * - capab needed
799     * - data
800     * outputs - none
801     * side effects - data sent to servers matching with capab
802     */
803     void
804 michael 2543 sendto_match_servs(struct Client *source_p, const char *mask, unsigned int cap,
805 adx 30 const char *pattern, ...)
806     {
807     va_list args;
808 michael 2638 dlink_node *ptr = NULL;
809 michael 3135 struct dbuf_block *buff_suid;
810 adx 30
811 michael 3135 buff_suid = dbuf_alloc();
812 michael 3107
813 adx 30 va_start(args, pattern);
814 michael 3186 dbuf_put_fmt(buff_suid, ":%s ", source_p->id);
815 michael 3107 dbuf_put_args(buff_suid, pattern, args);
816 adx 30 va_end(args);
817    
818 michael 948 ++current_serial;
819 adx 30
820     DLINK_FOREACH(ptr, global_serv_list.head)
821     {
822 michael 2638 struct Client *target_p = ptr->data;
823 adx 30
824     /* Do not attempt to send to ourselves, or the source */
825     if (IsMe(target_p) || target_p->from == source_p->from)
826     continue;
827    
828 michael 1078 if (target_p->from->localClient->serial == current_serial)
829 adx 30 continue;
830    
831 michael 1652 if (!match(mask, target_p->name))
832 adx 30 {
833     /*
834     * if we set the serial here, then we'll never do a
835     * match() again, if !IsCapable()
836     */
837 michael 1078 target_p->from->localClient->serial = current_serial;
838 adx 30
839     if (!IsCapable(target_p->from, cap))
840     continue;
841    
842 michael 3135 send_message_remote(target_p->from, source_p, buff_suid);
843 adx 30 }
844     }
845 michael 3107
846     dbuf_ref_free(buff_suid);
847 adx 30 }
848    
849     /* sendto_anywhere()
850     *
851     * inputs - pointer to dest client
852     * - pointer to from client
853     * - varags
854     * output - NONE
855     * side effects - less efficient than sendto_remote and sendto_one
856     * but useful when one does not know where target "lives"
857     */
858     void
859     sendto_anywhere(struct Client *to, struct Client *from,
860 michael 2793 const char *command,
861 adx 30 const char *pattern, ...)
862     {
863     va_list args;
864 michael 3189 struct dbuf_block *buffer = NULL;
865 adx 30
866 michael 2793 if (IsDead(to->from))
867 adx 30 return;
868    
869 michael 3107 buffer = dbuf_alloc();
870    
871 michael 3189 if (MyClient(to) && IsClient(from))
872     dbuf_put_fmt(buffer, ":%s!%s@%s %s %s ", from->name, from->username,
873     from->host, command, to->name);
874 michael 2634 else
875 michael 3189 dbuf_put_fmt(buffer, ":%s %s %s ", ID_or_name(from, to),
876     command, ID_or_name(to, to));
877 adx 30
878     va_start(args, pattern);
879 michael 3107 send_format(buffer, pattern, args);
880 adx 30 va_end(args);
881    
882 michael 2518 if (MyClient(to))
883 michael 3107 send_message(to, buffer);
884 adx 30 else
885 michael 3107 send_message_remote(to, from, buffer);
886    
887     dbuf_ref_free(buffer);
888 adx 30 }
889    
890     /* sendto_realops_flags()
891     *
892     * inputs - flag types of messages to show to real opers
893     * - flag indicating opers/admins
894     * - var args input message
895     * output - NONE
896     * side effects - Send to *local* ops only but NOT +s nonopers.
897     */
898     void
899 michael 1618 sendto_realops_flags(unsigned int flags, int level, int type, const char *pattern, ...)
900 adx 30 {
901 michael 1618 const char *ntype = NULL;
902 michael 1078 dlink_node *ptr = NULL;
903 michael 3215 char nbuf[IRCD_BUFSIZE] = "";
904 adx 30 va_list args;
905    
906     va_start(args, pattern);
907 michael 1618 vsnprintf(nbuf, sizeof(nbuf), pattern, args);
908 adx 30 va_end(args);
909    
910 michael 1618 switch (type)
911 adx 30 {
912 michael 1618 case SEND_NOTICE:
913     ntype = "Notice";
914     break;
915     case SEND_GLOBAL:
916     ntype = "Global";
917     break;
918     case SEND_LOCOPS:
919     ntype = "LocOps";
920     break;
921     default:
922     assert(0);
923 adx 30 }
924    
925 michael 1206 DLINK_FOREACH(ptr, oper_list.head)
926     {
927     struct Client *client_p = ptr->data;
928 michael 1618 assert(HasUMode(client_p, UMODE_OPER));
929 michael 1206
930 michael 1618 /*
931     * If we're sending it to opers and they're an admin, skip.
932     * If we're sending it to admins, and they're not, skip.
933 michael 1206 */
934 michael 1219 if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
935 michael 2638 ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
936 michael 1206 continue;
937    
938 michael 1219 if (HasUMode(client_p, flags))
939 michael 1618 sendto_one(client_p, ":%s NOTICE %s :*** %s -- %s",
940     me.name, client_p->name, ntype, nbuf);
941 michael 1206 }
942     }
943    
944 michael 3215 /* ts_warn()
945     *
946     * inputs - var args message
947     * output - NONE
948     * side effects - Call sendto_realops_flags, with some flood checking
949     * (at most 5 warnings every 5 seconds)
950     */
951     void
952     sendto_realops_flags_ratelimited(const char *pattern, ...)
953     {
954     va_list args;
955     char buffer[IRCD_BUFSIZE] = "";
956     static time_t last = 0;
957     static int warnings = 0;
958    
959     /*
960     ** if we're running with TS_WARNINGS enabled and someone does
961     ** something silly like (remotely) connecting a nonTS server,
962     ** we'll get a ton of warnings, so we make sure we don't send
963     ** more than 5 every 5 seconds. -orabidoo
964     */
965    
966     if (CurrentTime - last < 5)
967     {
968     if (++warnings > 5)
969     return;
970     }
971     else
972     {
973     last = CurrentTime;
974     warnings = 0;
975     }
976    
977     va_start(args, pattern);
978     vsnprintf(buffer, sizeof(buffer), pattern, args);
979     va_end(args);
980    
981     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, "%s", buffer);
982     ilog(LOG_TYPE_IRCD, "%s", buffer);
983     }
984    
985 adx 30 /* sendto_wallops_flags()
986     *
987     * inputs - flag types of messages to show to real opers
988     * - client sending request
989     * - var args input message
990     * output - NONE
991     * side effects - Send a wallops to local opers
992     */
993     void
994     sendto_wallops_flags(unsigned int flags, struct Client *source_p,
995     const char *pattern, ...)
996     {
997 michael 1078 dlink_node *ptr = NULL;
998 adx 30 va_list args;
999 michael 3107 struct dbuf_block *buffer;
1000 adx 30
1001 michael 3107 buffer = dbuf_alloc();
1002    
1003 adx 30 if (IsClient(source_p))
1004 michael 3113 dbuf_put_fmt(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host);
1005 adx 30 else
1006 michael 3113 dbuf_put_fmt(buffer, ":%s WALLOPS :", source_p->name);
1007 adx 30
1008     va_start(args, pattern);
1009 michael 3107 send_format(buffer, pattern, args);
1010 adx 30 va_end(args);
1011    
1012     DLINK_FOREACH(ptr, oper_list.head)
1013     {
1014 michael 1078 struct Client *client_p = ptr->data;
1015 adx 30 assert(client_p->umodes & UMODE_OPER);
1016    
1017 michael 1219 if (HasUMode(client_p, flags) && !IsDefunct(client_p))
1018 michael 3107 send_message(client_p, buffer);
1019 adx 30 }
1020 michael 3107
1021     dbuf_ref_free(buffer);
1022 adx 30 }

Properties

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