41ae2420dccbfe8a2e697413dfcdfefeb760c16d
[ircu2.10.12-pk.git] / ircd / msgq.c
1 /*
2  * IRC - Internet Relay Chat, ircd/msgq.c
3  * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "msgq.h"
22 #include "ircd_alloc.h"
23 #include "ircd_defs.h"
24 #include "ircd_snprintf.h"
25 #include "s_debug.h"
26
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <sys/uio.h>    /* struct iovec */
31
32 struct MsgBuf {
33   struct MsgBuf *next;          /* next msg in global queue */
34   struct MsgBuf **prev_p;       /* what points to us in linked list */
35   unsigned int ref;             /* reference count */
36   unsigned int length;          /* length of message */
37   char msg[BUFSIZE + 1];        /* the message */
38 };
39
40 struct Msg {
41   struct Msg *next;             /* next msg */
42   unsigned int sent;            /* bytes in msg that have already been sent */
43   struct MsgBuf *msg;           /* actual message in queue */
44 };
45
46 static struct {
47   struct MsgBuf *msgs;
48   struct MsgBuf *free_mbs;
49   struct Msg *free_msgs;
50 } MQData = { 0, 0, 0 };
51
52 struct MsgCounts msgBufCounts = { 0, 0 };
53 struct MsgCounts msgCounts = { 0, 0 };
54
55 /* XXX HACK HACK HACK XXX */
56 #if 1
57 /* First, force assertion checking */
58 #undef NDEBUG
59 #include <assert.h>
60
61 /* This routine is TEMPORARY and is intended to track down a problem we're
62  * having with apparent buffer overflows in this file.
63  */
64 static void
65 msgq_integrity(void)
66 {
67   struct MsgBuf *mb;
68   struct Msg *msg;
69
70   for (mb = MQData.msgs; mb; mb = mb->next)
71     assert(((unsigned long)mb) != 0x8000a0d);
72   for (mb = MQData.free_mbs; mb; mb = mb->next)
73     assert(((unsigned long)mb) != 0x8000a0d);
74   for (msg = MQData.free_msgs; msg; msg = msg->next)
75     assert(((unsigned long)msg) != 0x8000a0d);
76 }
77 #else
78 #define msgq_integrity()        ((void)0)
79 #endif /* XXX HACK HACK HACK XXX */
80
81 /*
82  * This routine is used to remove a certain amount of data from a given
83  * queue and release the Msg (and MsgBuf) structure if needed
84  */
85 static void
86 msgq_delmsg(struct MsgQ *mq, struct MsgQList *qlist, unsigned int *length_p)
87 {
88   struct Msg *m;
89   unsigned int msglen;
90
91   msgq_integrity();
92
93   assert(0 != mq);
94   assert(0 != qlist);
95   assert(0 != qlist->head);
96   assert(0 != length_p);
97
98   m = qlist->head; /* find the msg we're deleting from */
99
100   msglen = m->msg->length - m->sent; /* calculate how much is left */
101
102   if (*length_p >= msglen) { /* deleted it all? */
103     mq->length -= msglen; /* decrement length */
104     mq->count--; /* decrement the message count */
105     *length_p -= msglen;
106
107     msgq_clean(m->msg); /* free up the struct MsgBuf */
108     m->msg = 0; /* don't let it point anywhere nasty, please */
109
110     if (qlist->head == qlist->tail) /* figure out if we emptied the queue */
111       qlist->head = qlist->tail = 0;
112     else
113       qlist->head = m->next; /* just shift the list down some */
114
115     msgCounts.used--; /* struct Msg is not in use anymore */
116
117     m->next = MQData.free_msgs; /* throw it onto the free list */
118     MQData.free_msgs = m;
119   } else {
120     mq->length -= *length_p; /* decrement queue length */
121     m->sent += *length_p; /* this much of the message has been sent */
122     *length_p = 0; /* we've dealt with it all */
123   }
124
125   msgq_integrity();
126 }
127
128 /*
129  * This just initializes a struct MsgQ.
130  */
131 void
132 msgq_init(struct MsgQ *mq)
133 {
134   msgq_integrity();
135
136   assert(0 != mq);
137
138   mq->length = 0;
139   mq->count = 0;
140   mq->queue.head = 0;
141   mq->queue.tail = 0;
142   mq->prio.head = 0;
143   mq->prio.tail = 0;
144
145   msgq_integrity();
146 }
147
148 /*
149  * This routine is used to delete the specified number of bytes off
150  * of the queue.  We only really need to worry about one struct Msg*,
151  * but this allows us to retain the flexibility to deal with more,
152  * which means we could do something fancy involving writev...
153  */
154 void
155 msgq_delete(struct MsgQ *mq, unsigned int length)
156 {
157   msgq_integrity();
158
159   assert(0 != mq);
160
161   while (length > 0) {
162     if (mq->queue.head && mq->queue.head->sent > 0) /* partial msg on norm q */
163       msgq_delmsg(mq, &mq->queue, &length);
164     else if (mq->prio.head) /* message (partial or complete) on prio queue */
165       msgq_delmsg(mq, &mq->prio, &length);
166     else if (mq->queue.head) /* message on normal queue */
167       msgq_delmsg(mq, &mq->queue, &length);
168     else
169       break;
170   }
171
172   msgq_integrity();
173 }
174
175 /*
176  * This is the more intelligent routine that can fill in an array of
177  * struct iovec's.
178  */
179 int
180 msgq_mapiov(const struct MsgQ *mq, struct iovec *iov, int count,
181             unsigned int *len)
182 {
183   struct Msg *queue;
184   struct Msg *prio;
185   int i = 0;
186
187   msgq_integrity();
188
189   assert(0 != mq);
190   assert(0 != iov);
191   assert(0 != count);
192   assert(0 != len);
193
194   if (mq->length <= 0) { /* no data to map */
195     msgq_integrity();
196     return 0;
197   }
198
199   if (mq->queue.head && mq->queue.head->sent > 0) { /* partial msg on norm q */
200     iov[i].iov_base = mq->queue.head->msg->msg + mq->queue.head->sent;
201     iov[i].iov_len = mq->queue.head->msg->length - mq->queue.head->sent;
202     *len += iov[i].iov_len;
203
204     queue = mq->queue.head->next; /* where we start later... */
205
206     i++; /* filled an iovec... */
207     if (!--count) { /* check for space */
208       msgq_integrity();
209       return i;
210     }
211   } else
212     queue = mq->queue.head; /* start at head of queue */
213
214   if (mq->prio.head && mq->prio.head->sent > 0) { /* partial msg on prio q */
215     iov[i].iov_base = mq->prio.head->msg->msg + mq->prio.head->sent;
216     iov[i].iov_len = mq->prio.head->msg->length - mq->prio.head->sent;
217     *len += iov[i].iov_len;
218
219     prio = mq->prio.head->next; /* where we start later... */
220
221     i++; /* filled an iovec... */
222     if (!--count) { /* check for space */
223       msgq_integrity();
224       return i;
225     }
226   } else
227     prio = mq->prio.head; /* start at head of prio */
228
229   for (; prio; prio = prio->next) { /* go through prio queue */
230     iov[i].iov_base = prio->msg->msg; /* store message */
231     iov[i].iov_len = prio->msg->length;
232     *len += iov[i].iov_len;
233
234     i++; /* filled an iovec... */
235     if (!--count) { /* check for space */
236       msgq_integrity();
237       return i;
238     }
239   }
240
241   for (; queue; queue = queue->next) { /* go through normal queue */
242     iov[i].iov_base = queue->msg->msg;
243     iov[i].iov_len = queue->msg->length;
244     *len += iov[i].iov_len;
245
246     i++; /* filled an iovec... */
247     if (!--count) { /* check for space */
248       msgq_integrity();
249       return i;
250     }
251   }
252
253   msgq_integrity();
254
255   return i;
256 }
257
258 /*
259  * This routine builds a struct MsgBuf with the appropriate contents
260  * and returns it; this saves us from having to worry about the contents
261  * of struct MsgBuf in anything other than this module
262  */
263 struct MsgBuf *
264 msgq_vmake(struct Client *dest, const char *format, va_list vl)
265 {
266   struct MsgBuf *mb;
267
268   msgq_integrity();
269
270   assert(0 != format);
271
272   if (!(mb = MQData.free_mbs)) { /* do I need to allocate one? */
273     mb = (struct MsgBuf *)MyMalloc(sizeof(struct MsgBuf));
274     msgBufCounts.alloc++; /* we allocated another */
275   } else /* shift the free list */
276     MQData.free_mbs = MQData.free_mbs->next;
277
278   msgBufCounts.used++; /* we're using another */
279
280   mb->next = MQData.msgs; /* initialize the msgbuf */
281   mb->prev_p = &MQData.msgs;
282   mb->ref = 1;
283
284   /* fill the buffer */
285   mb->length = ircd_vsnprintf(dest, mb->msg, sizeof(mb->msg) - 2, format, vl);
286
287   if (mb->length > sizeof(mb->msg) - 3)
288     mb->length = sizeof(mb->msg) - 3;
289
290   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
291   mb->msg[mb->length++] = '\n';
292   mb->msg[mb->length] = '\0'; /* not strictly necessary */
293
294   assert(mb->length < sizeof(mb->msg));
295
296   if (MQData.msgs) /* link it into the list */
297     MQData.msgs->prev_p = &mb->next;
298   MQData.msgs = mb;
299
300   msgq_integrity();
301
302   return mb;
303 }
304
305 struct MsgBuf *
306 msgq_make(struct Client *dest, const char *format, ...)
307 {
308   va_list vl;
309   struct MsgBuf *mb;
310
311   va_start(vl, format);
312   mb = msgq_vmake(dest, format, vl);
313   va_end(vl);
314
315   return mb;
316 }
317
318 /*
319  * This routine is used to append a formatted string to a struct MsgBuf.
320  */
321 void
322 msgq_append(struct Client *dest, struct MsgBuf *mb, const char *format, ...)
323 {
324   va_list vl;
325
326   msgq_integrity();
327
328   assert(0 != mb);
329   assert(0 != format);
330
331   assert(2 < mb->length);
332   assert(sizeof(mb->msg) > mb->length);
333
334   mb->length -= 2; /* back up to before \r\n */
335
336   va_start(vl, format); /* append to the buffer */
337   mb->length += ircd_vsnprintf(dest, mb->msg + mb->length,
338                                sizeof(mb->msg) - 2 - mb->length, format, vl);
339   va_end(vl);
340
341   if (mb->length > sizeof(mb->msg) - 3)
342     mb->length = sizeof(mb->msg) - 3;
343
344   mb->msg[mb->length++] = '\r'; /* add \r\n to buffer */
345   mb->msg[mb->length++] = '\n';
346   mb->msg[mb->length] = '\0'; /* not strictly necessary */
347
348   assert(mb->length < sizeof(mb->msg));
349
350   msgq_integrity();
351 }
352
353 /*
354  * This routine is called to decrement the reference count on a
355  * struct MsgBuf and delete it if necessary.
356  */
357 void
358 msgq_clean(struct MsgBuf *mb)
359 {
360   msgq_integrity();
361
362   assert(0 != mb);
363   assert(0 < mb->ref);
364   assert(0 != mb->prev_p);
365
366   if (!--mb->ref) { /* deallocate the message */
367     *mb->prev_p = mb->next; /* clip it out of active MsgBuf's list */
368     if (mb->next)
369       mb->next->prev_p = mb->prev_p;
370
371     mb->next = MQData.free_mbs; /* add it to free list */
372     MQData.free_mbs = mb;
373
374     mb->prev_p = 0;
375
376     msgBufCounts.used--; /* decrement the usage count */
377   }
378
379   msgq_integrity();
380 }
381
382 /*
383  * This routine simply adds a struct Msg to the end of a user's MsgQ.
384  */
385 void
386 msgq_add(struct MsgQ *mq, struct MsgBuf *mb, int prio)
387 {
388   struct MsgQList *qlist;
389   struct Msg *msg;
390
391   msgq_integrity();
392
393   assert(0 != mq);
394   assert(0 != mb);
395   assert(0 < mb->ref);
396
397   Debug((DEBUG_SEND, "Adding buffer %p [%.*s] to %s queue", mb,
398          mb->length - 2, mb->msg, prio ? "priority" : "normal"));
399
400   qlist = prio ? &mq->prio : &mq->queue;
401
402   if (!(msg = MQData.free_msgs)) { /* do I need to allocate one? */
403     msg = (struct Msg *)MyMalloc(sizeof(struct Msg));
404     msgCounts.alloc++; /* we allocated another */
405   } else /* shift the free list */
406     MQData.free_msgs = MQData.free_msgs->next;
407
408   msgCounts.used++; /* we're using another */
409
410   msg->next = 0; /* initialize the msg */
411   msg->sent = 0;
412   msg->msg = mb;
413
414   mb->ref++; /* increment the ref count on the buffer */
415
416   if (!qlist->head) /* queue list was empty; head and tail point to msg */
417     qlist->head = qlist->tail = msg;
418   else {
419     assert(0 != qlist->tail);
420
421     qlist->tail->next = msg; /* queue had something in it; add to end */
422     qlist->tail = msg;
423   }
424
425   mq->length += mb->length; /* update the queue length */
426   mq->count++; /* and the queue count */
427
428   msgq_integrity();
429 }
430
431 /*
432  * This is for reporting memory usage by the msgq system.
433  */
434 void
435 msgq_count_memory(size_t *msg_alloc, size_t *msg_used, size_t *msgbuf_alloc,
436                   size_t *msgbuf_used)
437 {
438   msgq_integrity();
439
440   assert(0 != msg_alloc);
441   assert(0 != msg_used);
442   assert(0 != msgbuf_alloc);
443   assert(0 != msgbuf_used);
444
445   *msg_alloc = msgCounts.alloc * sizeof(struct Msg);
446   *msg_used = msgCounts.used * sizeof(struct Msg);
447   *msgbuf_alloc = msgCounts.alloc * sizeof(struct MsgBuf);
448   *msgbuf_used = msgCounts.used * sizeof(struct MsgBuf);
449
450   msgq_integrity();
451 }
452
453 /*
454  * This routine is used simply to report how much bufferspace is left.
455  */
456 unsigned int
457 msgq_bufleft(struct MsgBuf *mb)
458 {
459   assert(0 != mb);
460
461   return sizeof(mb->msg) - mb->length - 1; /* \r\n counted in mb->length */
462 }