Use 1Mbits/s as beacon sending rate; it seems to fix TX performance issue
[dragonfly.git] / contrib / libevent / buffer.c
1 /*
2  * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef HAVE_VASPRINTF
33 /* If we have vasprintf, we need to define this before we include stdio.h. */
34 #define _GNU_SOURCE
35 #endif
36
37 #include <sys/types.h>
38
39 #ifdef HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42
43 #ifdef HAVE_SYS_IOCTL_H
44 #include <sys/ioctl.h>
45 #endif
46
47 #include <assert.h>
48 #include <errno.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #ifdef HAVE_STDARG_H
53 #include <stdarg.h>
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #include "event.h"
60
61 struct evbuffer *
62 evbuffer_new(void)
63 {
64         struct evbuffer *buffer;
65         
66         buffer = calloc(1, sizeof(struct evbuffer));
67
68         return (buffer);
69 }
70
71 void
72 evbuffer_free(struct evbuffer *buffer)
73 {
74         if (buffer->orig_buffer != NULL)
75                 free(buffer->orig_buffer);
76         free(buffer);
77 }
78
79 /* 
80  * This is a destructive add.  The data from one buffer moves into
81  * the other buffer.
82  */
83
84 #define SWAP(x,y) do { \
85         (x)->buffer = (y)->buffer; \
86         (x)->orig_buffer = (y)->orig_buffer; \
87         (x)->misalign = (y)->misalign; \
88         (x)->totallen = (y)->totallen; \
89         (x)->off = (y)->off; \
90 } while (0)
91
92 int
93 evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
94 {
95         int res;
96
97         /* Short cut for better performance */
98         if (outbuf->off == 0) {
99                 struct evbuffer tmp;
100                 size_t oldoff = inbuf->off;
101
102                 /* Swap them directly */
103                 SWAP(&tmp, outbuf);
104                 SWAP(outbuf, inbuf);
105                 SWAP(inbuf, &tmp);
106
107                 /* 
108                  * Optimization comes with a price; we need to notify the
109                  * buffer if necessary of the changes. oldoff is the amount
110                  * of data that we transfered from inbuf to outbuf
111                  */
112                 if (inbuf->off != oldoff && inbuf->cb != NULL)
113                         (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg);
114                 if (oldoff && outbuf->cb != NULL)
115                         (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
116                 
117                 return (0);
118         }
119
120         res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
121         if (res == 0) {
122                 /* We drain the input buffer on success */
123                 evbuffer_drain(inbuf, inbuf->off);
124         }
125
126         return (res);
127 }
128
129 int
130 evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
131 {
132         char *buffer;
133         size_t space;
134         size_t oldoff = buf->off;
135         int sz;
136         va_list aq;
137
138         /* make sure that at least some space is available */
139         evbuffer_expand(buf, 64);
140         for (;;) {
141                 size_t used = buf->misalign + buf->off;
142                 buffer = (char *)buf->buffer + buf->off;
143                 assert(buf->totallen >= used);
144                 space = buf->totallen - used;
145
146 #ifndef va_copy
147 #define va_copy(dst, src)       memcpy(&(dst), &(src), sizeof(va_list))
148 #endif
149                 va_copy(aq, ap);
150
151 #ifdef WIN32
152                 sz = vsnprintf(buffer, space - 1, fmt, aq);
153                 buffer[space - 1] = '\0';
154 #else
155                 sz = vsnprintf(buffer, space, fmt, aq);
156 #endif
157
158                 va_end(aq);
159
160                 if (sz < 0)
161                         return (-1);
162                 if (sz < space) {
163                         buf->off += sz;
164                         if (buf->cb != NULL)
165                                 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
166                         return (sz);
167                 }
168                 if (evbuffer_expand(buf, sz + 1) == -1)
169                         return (-1);
170
171         }
172         /* NOTREACHED */
173 }
174
175 int
176 evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
177 {
178         int res = -1;
179         va_list ap;
180
181         va_start(ap, fmt);
182         res = evbuffer_add_vprintf(buf, fmt, ap);
183         va_end(ap);
184
185         return (res);
186 }
187
188 /* Reads data from an event buffer and drains the bytes read */
189
190 int
191 evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen)
192 {
193         size_t nread = datlen;
194         if (nread >= buf->off)
195                 nread = buf->off;
196
197         memcpy(data, buf->buffer, nread);
198         evbuffer_drain(buf, nread);
199         
200         return (nread);
201 }
202
203 /*
204  * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
205  * The returned buffer needs to be freed by the called.
206  */
207
208 char *
209 evbuffer_readline(struct evbuffer *buffer)
210 {
211         u_char *data = EVBUFFER_DATA(buffer);
212         size_t len = EVBUFFER_LENGTH(buffer);
213         char *line;
214         unsigned int i;
215
216         for (i = 0; i < len; i++) {
217                 if (data[i] == '\r' || data[i] == '\n')
218                         break;
219         }
220
221         if (i == len)
222                 return (NULL);
223
224         if ((line = malloc(i + 1)) == NULL) {
225                 fprintf(stderr, "%s: out of memory\n", __func__);
226                 evbuffer_drain(buffer, i);
227                 return (NULL);
228         }
229
230         memcpy(line, data, i);
231         line[i] = '\0';
232
233         /*
234          * Some protocols terminate a line with '\r\n', so check for
235          * that, too.
236          */
237         if ( i < len - 1 ) {
238                 char fch = data[i], sch = data[i+1];
239
240                 /* Drain one more character if needed */
241                 if ( (sch == '\r' || sch == '\n') && sch != fch )
242                         i += 1;
243         }
244
245         evbuffer_drain(buffer, i + 1);
246
247         return (line);
248 }
249
250 /* Adds data to an event buffer */
251
252 static void
253 evbuffer_align(struct evbuffer *buf)
254 {
255         memmove(buf->orig_buffer, buf->buffer, buf->off);
256         buf->buffer = buf->orig_buffer;
257         buf->misalign = 0;
258 }
259
260 /* Expands the available space in the event buffer to at least datlen */
261
262 int
263 evbuffer_expand(struct evbuffer *buf, size_t datlen)
264 {
265         size_t need = buf->misalign + buf->off + datlen;
266
267         /* If we can fit all the data, then we don't have to do anything */
268         if (buf->totallen >= need)
269                 return (0);
270
271         /*
272          * If the misalignment fulfills our data needs, we just force an
273          * alignment to happen.  Afterwards, we have enough space.
274          */
275         if (buf->misalign >= datlen) {
276                 evbuffer_align(buf);
277         } else {
278                 void *newbuf;
279                 size_t length = buf->totallen;
280
281                 if (length < 256)
282                         length = 256;
283                 while (length < need)
284                         length <<= 1;
285
286                 if (buf->orig_buffer != buf->buffer)
287                         evbuffer_align(buf);
288                 if ((newbuf = realloc(buf->buffer, length)) == NULL)
289                         return (-1);
290
291                 buf->orig_buffer = buf->buffer = newbuf;
292                 buf->totallen = length;
293         }
294
295         return (0);
296 }
297
298 int
299 evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen)
300 {
301         size_t need = buf->misalign + buf->off + datlen;
302         size_t oldoff = buf->off;
303
304         if (buf->totallen < need) {
305                 if (evbuffer_expand(buf, datlen) == -1)
306                         return (-1);
307         }
308
309         memcpy(buf->buffer + buf->off, data, datlen);
310         buf->off += datlen;
311
312         if (datlen && buf->cb != NULL)
313                 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
314
315         return (0);
316 }
317
318 void
319 evbuffer_drain(struct evbuffer *buf, size_t len)
320 {
321         size_t oldoff = buf->off;
322
323         if (len >= buf->off) {
324                 buf->off = 0;
325                 buf->buffer = buf->orig_buffer;
326                 buf->misalign = 0;
327                 goto done;
328         }
329
330         buf->buffer += len;
331         buf->misalign += len;
332
333         buf->off -= len;
334
335  done:
336         /* Tell someone about changes in this buffer */
337         if (buf->off != oldoff && buf->cb != NULL)
338                 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
339
340 }
341
342 /*
343  * Reads data from a file descriptor into a buffer.
344  */
345
346 #define EVBUFFER_MAX_READ       4096
347
348 int
349 evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
350 {
351         u_char *p;
352         size_t oldoff = buf->off;
353         int n = EVBUFFER_MAX_READ;
354 #ifdef WIN32
355         DWORD dwBytesRead;
356 #endif
357
358 #ifdef FIONREAD
359         if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
360                 n = EVBUFFER_MAX_READ;
361         } else if (n > EVBUFFER_MAX_READ && n > howmuch) {
362                 /*
363                  * It's possible that a lot of data is available for
364                  * reading.  We do not want to exhaust resources
365                  * before the reader has a chance to do something
366                  * about it.  If the reader does not tell us how much
367                  * data we should read, we artifically limit it.
368                  */
369                 if (n > buf->totallen << 2)
370                         n = buf->totallen << 2;
371                 if (n < EVBUFFER_MAX_READ)
372                         n = EVBUFFER_MAX_READ;
373         }
374 #endif  
375         if (howmuch < 0 || howmuch > n)
376                 howmuch = n;
377
378         /* If we don't have FIONREAD, we might waste some space here */
379         if (evbuffer_expand(buf, howmuch) == -1)
380                 return (-1);
381
382         /* We can append new data at this point */
383         p = buf->buffer + buf->off;
384
385 #ifndef WIN32
386         n = read(fd, p, howmuch);
387         if (n == -1)
388                 return (-1);
389         if (n == 0)
390                 return (0);
391 #else
392         n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL);
393         if (n == 0)
394                 return (-1);
395         if (dwBytesRead == 0)
396                 return (0);
397         n = dwBytesRead;
398 #endif
399
400         buf->off += n;
401
402         /* Tell someone about changes in this buffer */
403         if (buf->off != oldoff && buf->cb != NULL)
404                 (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
405
406         return (n);
407 }
408
409 int
410 evbuffer_write(struct evbuffer *buffer, int fd)
411 {
412         int n;
413 #ifdef WIN32
414         DWORD dwBytesWritten;
415 #endif
416
417 #ifndef WIN32
418         n = write(fd, buffer->buffer, buffer->off);
419         if (n == -1)
420                 return (-1);
421         if (n == 0)
422                 return (0);
423 #else
424         n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL);
425         if (n == 0)
426                 return (-1);
427         if (dwBytesWritten == 0)
428                 return (0);
429         n = dwBytesWritten;
430 #endif
431         evbuffer_drain(buffer, n);
432
433         return (n);
434 }
435
436 u_char *
437 evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len)
438 {
439         u_char *search = buffer->buffer, *end = search + buffer->off;
440         u_char *p;
441
442         while (search < end &&
443             (p = memchr(search, *what, end - search)) != NULL) {
444                 if (p + len > end)
445                         break;
446                 if (memcmp(p, what, len) == 0)
447                         return (p);
448                 search = p + 1;
449         }
450
451         return (NULL);
452 }
453
454 void evbuffer_setcb(struct evbuffer *buffer,
455     void (*cb)(struct evbuffer *, size_t, size_t, void *),
456     void *cbarg)
457 {
458         buffer->cb = cb;
459         buffer->cbarg = cbarg;
460 }