diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2005-02-01 21:25:19 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2005-02-01 21:25:19 +0000 |
commit | 11750cd51d0158acbff6eca77fe6350328f2f744 (patch) | |
tree | 644f3c9052d67220d38f6cdf5f9481fc3a7031b7 /usr.sbin | |
parent | fbfc7897c23382d51bf9270c5986ea78fc80b41e (diff) |
Introduce dynmaic buffers. Dynamic buffers are realloced() until max is
hit. This makes it possible to alloc a buffer based on the max packet size
and filling it up slowly till the packet is finished or *sigh* an overflow
is detected. While doing that switch most sizes from ssize_t to the unsigned
size_t as this makes more sense. The ssize_t -> size_t change is mostly from
henning@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/ospfd/buffer.c | 56 | ||||
-rw-r--r-- | usr.sbin/ospfd/ospfd.h | 16 |
2 files changed, 56 insertions, 16 deletions
diff --git a/usr.sbin/ospfd/buffer.c b/usr.sbin/ospfd/buffer.c index f8fa58797d6..42d160499ed 100644 --- a/usr.sbin/ospfd/buffer.c +++ b/usr.sbin/ospfd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.1 2005/01/28 14:05:40 claudio Exp $ */ +/* $OpenBSD: buffer.c,v 1.2 2005/02/01 21:25:18 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -29,11 +29,12 @@ #include "ospfd.h" +int buf_realloc(struct buf *, size_t); void buf_enqueue(struct msgbuf *, struct buf *); void buf_dequeue(struct msgbuf *, struct buf *); struct buf * -buf_open(ssize_t len) +buf_open(size_t len) { struct buf *buf; @@ -43,17 +44,53 @@ buf_open(ssize_t len) free(buf); return (NULL); } - buf->size = len; + buf->size = buf->max = len; buf->fd = -1; return (buf); } +struct buf * +buf_dynamic(size_t len, size_t max) +{ + struct buf *buf; + + if (max < len) + return (NULL); + + if ((buf = buf_open(len)) == NULL) + return (NULL); + + if (max > 0) + buf->max = max; + + return (buf); +} + int -buf_add(struct buf *buf, void *data, ssize_t len) +buf_realloc(struct buf *buf, size_t len) { - if (buf->wpos + len > buf->size) + u_char *b; + + /* on static buffers max is eq size and so the following fails */ + if (buf->wpos + len > buf->max) + return (-1); + + b = realloc(buf->buf, buf->wpos + len); + if (b == NULL) return (-1); + buf->buf = b; + buf->size = buf->wpos + len; + + return (0); +} + +int +buf_add(struct buf *buf, void *data, size_t len) +{ + if (buf->wpos + len > buf->size) + if (buf_realloc(buf, len) == -1) + return (-1); memcpy(buf->buf + buf->wpos, data, len); buf->wpos += len; @@ -61,12 +98,13 @@ buf_add(struct buf *buf, void *data, ssize_t len) } void * -buf_reserve(struct buf *buf, ssize_t len) +buf_reserve(struct buf *buf, size_t len) { void *b; if (buf->wpos + len > buf->size) - return (NULL); + if (buf_realloc(buf, len) == -1) + return (NULL); b = buf->buf + buf->wpos; buf->wpos += len; @@ -98,7 +136,7 @@ buf_write(int sock, struct buf *buf) return (-2); } - if (n < buf->size - buf->rpos) { /* not all data written yet */ + if (buf->rpos + n < buf->size) { /* not all data written yet */ buf->rpos += n; return (0); } else @@ -186,7 +224,7 @@ msgbuf_write(struct msgbuf *msgbuf) for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; buf = next) { next = TAILQ_NEXT(buf, entry); - if (n >= buf->size - buf->rpos) { + if (buf->rpos + n >= buf->size) { n -= buf->size - buf->rpos; buf_dequeue(msgbuf, buf); } else { diff --git a/usr.sbin/ospfd/ospfd.h b/usr.sbin/ospfd/ospfd.h index 43ee8980c8c..9c284c02533 100644 --- a/usr.sbin/ospfd/ospfd.h +++ b/usr.sbin/ospfd/ospfd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ospfd.h,v 1.2 2005/01/28 17:53:33 norby Exp $ */ +/* $OpenBSD: ospfd.h,v 1.3 2005/02/01 21:25:18 claudio Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -55,9 +55,10 @@ struct buf { TAILQ_ENTRY(buf) entry; u_char *buf; - ssize_t size; - ssize_t wpos; - ssize_t rpos; + size_t size; + size_t max; + size_t wpos; + size_t rpos; int fd; }; @@ -362,9 +363,10 @@ int area_del(struct area *); struct area *area_find(struct ospfd_conf *, struct in_addr); /* buffer.c */ -struct buf *buf_open(ssize_t); -int buf_add(struct buf *, void *, ssize_t); -void *buf_reserve(struct buf *, ssize_t); +struct buf *buf_open(size_t); +struct buf *buf_dynamic(size_t, size_t); +int buf_add(struct buf *, void *, size_t); +void *buf_reserve(struct buf *, size_t); int buf_close(struct msgbuf *, struct buf *); int buf_write(int, struct buf *); void buf_free(struct buf *); |