summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpyr <pyr@openbsd.org>2009-06-06 07:52:04 +0000
committerpyr <pyr@openbsd.org>2009-06-06 07:52:04 +0000
commit14d72cd08621b4f31d3b17f7ca52f714e50a9b98 (patch)
treed0686bded63a2a5b33a2f83cd64f1dc19f2b4579
parentsync ospfctl/ospfd with the common imsg code, making it lib ready as well. (diff)
downloadwireguard-openbsd-14d72cd08621b4f31d3b17f7ca52f714e50a9b98.tar.xz
wireguard-openbsd-14d72cd08621b4f31d3b17f7ca52f714e50a9b98.zip
make dvmrpd imsg-in-a-lib ready as well.
``put it in'' claudio@, ok eric@
-rw-r--r--usr.sbin/dvmrpctl/dvmrpctl.c23
-rw-r--r--usr.sbin/dvmrpd/buffer.c109
-rw-r--r--usr.sbin/dvmrpd/control.c45
-rw-r--r--usr.sbin/dvmrpd/control.h4
-rw-r--r--usr.sbin/dvmrpd/dvmrpd.c81
-rw-r--r--usr.sbin/dvmrpd/dvmrpd.h88
-rw-r--r--usr.sbin/dvmrpd/dvmrpe.c74
-rw-r--r--usr.sbin/dvmrpd/group.c6
-rw-r--r--usr.sbin/dvmrpd/imsg.c148
-rw-r--r--usr.sbin/dvmrpd/rde.c60
10 files changed, 379 insertions, 259 deletions
diff --git a/usr.sbin/dvmrpctl/dvmrpctl.c b/usr.sbin/dvmrpctl/dvmrpctl.c
index ae1a0ab13aa..6cac076e5a3 100644
--- a/usr.sbin/dvmrpctl/dvmrpctl.c
+++ b/usr.sbin/dvmrpctl/dvmrpctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dvmrpctl.c,v 1.6 2009/01/24 16:23:52 michele Exp $ */
+/* $OpenBSD: dvmrpctl.c,v 1.7 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -71,11 +71,6 @@ usage(void)
exit(1);
}
-void
-imsg_event_add(struct imsgbuf *i)
-{
-}
-
int
main(int argc, char *argv[])
{
@@ -103,7 +98,7 @@ main(int argc, char *argv[])
if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
- imsg_init(ibuf, ctl_sock, NULL);
+ imsg_init(ibuf, ctl_sock);
done = 0;
/* process user request */
@@ -113,7 +108,7 @@ main(int argc, char *argv[])
/* NOTREACHED */
case SHOW:
case SHOW_SUM:
- imsg_compose(ibuf, IMSG_CTL_SHOW_SUM, 0, 0, NULL, 0);
+ imsg_compose(ibuf, IMSG_CTL_SHOW_SUM, 0, 0, -1, NULL, 0);
break;
case SHOW_IFACE:
printf("%-11s %-18s %-10s %-10s %-10s %-8s %s\n",
@@ -126,7 +121,7 @@ main(int argc, char *argv[])
if (ifidx == 0)
errx(1, "no such interface %s", res->ifname);
}
- imsg_compose(ibuf, IMSG_CTL_SHOW_IFACE, 0, 0, &ifidx,
+ imsg_compose(ibuf, IMSG_CTL_SHOW_IFACE, 0, 0, -1, &ifidx,
sizeof(ifidx));
break;
case SHOW_IGMP:
@@ -135,7 +130,7 @@ main(int argc, char *argv[])
if (ifidx == 0)
errx(1, "no such interface %s", res->ifname);
}
- imsg_compose(ibuf, IMSG_CTL_SHOW_IGMP, 0, 0, &ifidx,
+ imsg_compose(ibuf, IMSG_CTL_SHOW_IGMP, 0, 0, -1, &ifidx,
sizeof(ifidx));
break;
case SHOW_NBR:
@@ -143,14 +138,14 @@ main(int argc, char *argv[])
"DeadTime", "Address", "Interface", "Uptime");
/* FALLTHROUGH */
case SHOW_NBR_DTAIL:
- imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, NULL, 0);
+ imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, -1, NULL, 0);
break;
case SHOW_RIB:
printf("%-20s %-17s %-7s %-10s %-s\n", "Destination", "Nexthop",
"Cost", "Uptime", "Expire");
/* FALLTHROUGH */
case SHOW_RIB_DTAIL:
- imsg_compose(ibuf, IMSG_CTL_SHOW_RIB, 0, 0, NULL, 0);
+ imsg_compose(ibuf, IMSG_CTL_SHOW_RIB, 0, 0, -1, NULL, 0);
break;
case SHOW_MFC:
printf("%-16s %-16s %-9s %-9s %-4s %-10s %-10s\n", "Group",
@@ -158,10 +153,10 @@ main(int argc, char *argv[])
"Expire");
/* FALLTHROUGH */
case SHOW_MFC_DTAIL:
- imsg_compose(ibuf, IMSG_CTL_SHOW_MFC, 0, 0, NULL, 0);
+ imsg_compose(ibuf, IMSG_CTL_SHOW_MFC, 0, 0, -1, NULL, 0);
break;
case RELOAD:
- imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, NULL, 0);
+ imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0);
printf("reload request sent.\n");
done = 1;
break;
diff --git a/usr.sbin/dvmrpd/buffer.c b/usr.sbin/dvmrpd/buffer.c
index 055fa2d1b25..665e041d9bb 100644
--- a/usr.sbin/dvmrpd/buffer.c
+++ b/usr.sbin/dvmrpd/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.2 2008/10/03 15:20:29 eric Exp $ */
+/* $OpenBSD: buffer.c,v 1.3 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -16,17 +16,17 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include "dvmrpd.h"
+#include "imsg.h"
int buf_realloc(struct buf *, size_t);
void buf_enqueue(struct msgbuf *, struct buf *);
@@ -44,6 +44,7 @@ buf_open(size_t len)
return (NULL);
}
buf->size = buf->max = len;
+ buf->fd = -1;
return (buf);
}
@@ -86,7 +87,7 @@ buf_realloc(struct buf *buf, size_t len)
}
int
-buf_add(struct buf *buf, void *data, size_t len)
+buf_add(struct buf *buf, const void *data, size_t len)
{
if (buf->wpos + len > buf->size)
if (buf_realloc(buf, len) == -1)
@@ -121,11 +122,67 @@ buf_seek(struct buf *buf, size_t pos, size_t len)
return (buf->buf + pos);
}
-int
+size_t
+buf_size(struct buf *buf)
+{
+ return (buf->wpos);
+}
+
+size_t
+buf_left(struct buf *buf)
+{
+ return (buf->max - buf->wpos);
+}
+
+void
buf_close(struct msgbuf *msgbuf, struct buf *buf)
{
buf_enqueue(msgbuf, buf);
- return (1);
+}
+
+int
+buf_write(struct msgbuf *msgbuf)
+{
+ struct iovec iov[IOV_MAX];
+ struct buf *buf, *next;
+ unsigned int i = 0;
+ ssize_t n;
+
+ bzero(&iov, sizeof(iov));
+ TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
+ if (i >= IOV_MAX)
+ break;
+ iov[i].iov_base = buf->buf + buf->rpos;
+ iov[i].iov_len = buf->size - buf->rpos;
+ i++;
+ }
+
+ if ((n = writev(msgbuf->fd, iov, i)) == -1) {
+ if (errno == EAGAIN || errno == ENOBUFS ||
+ errno == EINTR) /* try later */
+ return (0);
+ else
+ return (-1);
+ }
+
+ if (n == 0) { /* connection closed */
+ errno = 0;
+ return (-2);
+ }
+
+ for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
+ buf = next) {
+ next = TAILQ_NEXT(buf, entry);
+ if (buf->rpos + n >= buf->size) {
+ n -= buf->size - buf->rpos;
+ buf_dequeue(msgbuf, buf);
+ } else {
+ buf->rpos += n;
+ n = 0;
+ }
+ }
+
+ return (0);
}
void
@@ -160,6 +217,11 @@ msgbuf_write(struct msgbuf *msgbuf)
unsigned int i = 0;
ssize_t n;
struct msghdr msg;
+ struct cmsghdr *cmsg;
+ union {
+ struct cmsghdr hdr;
+ char buf[CMSG_SPACE(sizeof(int))];
+ } cmsgbuf;
bzero(&iov, sizeof(iov));
bzero(&msg, sizeof(msg));
@@ -167,13 +229,25 @@ msgbuf_write(struct msgbuf *msgbuf)
if (i >= IOV_MAX)
break;
iov[i].iov_base = buf->buf + buf->rpos;
- iov[i].iov_len = buf->size - buf->rpos;
+ iov[i].iov_len = buf->wpos - buf->rpos;
i++;
+ if (buf->fd != -1)
+ break;
}
msg.msg_iov = iov;
msg.msg_iovlen = i;
+ if (buf != NULL && buf->fd != -1) {
+ msg.msg_control = (caddr_t)&cmsgbuf.buf;
+ msg.msg_controllen = sizeof(cmsgbuf.buf);
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ *(int *)CMSG_DATA(cmsg) = buf->fd;
+ }
+
if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
if (errno == EAGAIN || errno == ENOBUFS ||
errno == EINTR) /* try later */
@@ -187,11 +261,20 @@ msgbuf_write(struct msgbuf *msgbuf)
return (-2);
}
+ /*
+ * assumption: fd got sent if sendmsg sent anything
+ * this works because fds are passed one at a time
+ */
+ if (buf != NULL && buf->fd != -1) {
+ close(buf->fd);
+ buf->fd = -1;
+ }
+
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
buf = next) {
next = TAILQ_NEXT(buf, entry);
- if (buf->rpos + n >= buf->size) {
- n -= buf->size - buf->rpos;
+ if (buf->rpos + n >= buf->wpos) {
+ n -= buf->wpos - buf->rpos;
buf_dequeue(msgbuf, buf);
} else {
buf->rpos += n;
@@ -213,6 +296,10 @@ void
buf_dequeue(struct msgbuf *msgbuf, struct buf *buf)
{
TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
+
+ if (buf->fd != -1)
+ close(buf->fd);
+
msgbuf->queued--;
buf_free(buf);
}
diff --git a/usr.sbin/dvmrpd/control.c b/usr.sbin/dvmrpd/control.c
index f416ce0222d..a99e46be4b4 100644
--- a/usr.sbin/dvmrpd/control.c
+++ b/usr.sbin/dvmrpd/control.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.c,v 1.7 2009/05/31 20:31:35 jacekm Exp $ */
+/* $OpenBSD: control.c,v 1.8 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -133,11 +133,12 @@ control_accept(int listenfd, short event, void *bula)
return;
}
- imsg_init(&c->ibuf, connfd, control_dispatch_imsg);
- c->ibuf.events = EV_READ;
- event_set(&c->ibuf.ev, c->ibuf.fd, c->ibuf.events,
- c->ibuf.handler, &c->ibuf);
- event_add(&c->ibuf.ev, NULL);
+ imsg_init(&c->iev.ibuf, connfd);
+ c->iev.handler = control_dispatch_imsg;
+ c->iev.events = EV_READ;
+ event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
+ c->iev.handler, &c->iev);
+ event_add(&c->iev.ev, NULL);
TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
}
@@ -147,7 +148,7 @@ control_connbyfd(int fd)
{
struct ctl_conn *c;
- for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.fd != fd;
+ for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.fd != fd;
c = TAILQ_NEXT(c, entry))
; /* nothing */
@@ -159,7 +160,7 @@ control_connbypid(pid_t pid)
{
struct ctl_conn *c;
- for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.pid != pid;
+ for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.pid != pid;
c = TAILQ_NEXT(c, entry))
; /* nothing */
@@ -176,11 +177,11 @@ control_close(int fd)
return;
}
- msgbuf_clear(&c->ibuf.w);
+ msgbuf_clear(&c->iev.ibuf.w);
TAILQ_REMOVE(&ctl_conns, c, entry);
- event_del(&c->ibuf.ev);
- close(c->ibuf.fd);
+ event_del(&c->iev.ev);
+ close(c->iev.ibuf.fd);
free(c);
}
@@ -199,20 +200,20 @@ control_dispatch_imsg(int fd, short event, void *bula)
}
if (event & EV_READ) {
- if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) {
+ if ((n = imsg_read(&c->iev.ibuf)) == -1 || n == 0) {
control_close(fd);
return;
}
}
if (event & EV_WRITE) {
- if (msgbuf_write(&c->ibuf.w) == -1) {
+ if (msgbuf_write(&c->iev.ibuf.w) == -1) {
control_close(fd);
return;
}
}
for (;;) {
- if ((n = imsg_get(&c->ibuf, &imsg)) == -1) {
+ if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
control_close(fd);
return;
}
@@ -231,8 +232,8 @@ control_dispatch_imsg(int fd, short event, void *bula)
sizeof(ifidx)) {
memcpy(&ifidx, imsg.data, sizeof(ifidx));
dvmrpe_iface_ctl(c, ifidx);
- imsg_compose(&c->ibuf, IMSG_CTL_END, 0,
- 0, NULL, 0);
+ imsg_compose_event(&c->iev, IMSG_CTL_END, 0,
+ 0, -1, NULL, 0);
}
break;
case IMSG_CTL_SHOW_IGMP:
@@ -240,8 +241,8 @@ control_dispatch_imsg(int fd, short event, void *bula)
sizeof(ifidx)) {
memcpy(&ifidx, imsg.data, sizeof(ifidx));
dvmrpe_iface_igmp_ctl(c, ifidx);
- imsg_compose(&c->ibuf, IMSG_CTL_END, 0,
- 0, NULL, 0);
+ imsg_compose_event(&c->iev, IMSG_CTL_END, 0,
+ 0, -1, NULL, 0);
}
break;
case IMSG_CTL_SHOW_NBR:
@@ -250,7 +251,7 @@ control_dispatch_imsg(int fd, short event, void *bula)
case IMSG_CTL_SHOW_RIB:
case IMSG_CTL_SHOW_MFC:
case IMSG_CTL_SHOW_SUM:
- c->ibuf.pid = imsg.hdr.pid;
+ c->iev.ibuf.pid = imsg.hdr.pid;
dvmrpe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
break;
@@ -262,7 +263,7 @@ control_dispatch_imsg(int fd, short event, void *bula)
imsg_free(&imsg);
}
- imsg_event_add(&c->ibuf);
+ imsg_event_add(&c->iev);
}
int
@@ -273,8 +274,8 @@ control_imsg_relay(struct imsg *imsg)
if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
return (0);
- return (imsg_compose(&c->ibuf, imsg->hdr.type, 0, imsg->hdr.pid,
- imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
+ return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
+ -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
}
void
diff --git a/usr.sbin/dvmrpd/control.h b/usr.sbin/dvmrpd/control.h
index d7518e5275a..7840bf9b1a6 100644
--- a/usr.sbin/dvmrpd/control.h
+++ b/usr.sbin/dvmrpd/control.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.h,v 1.1 2006/06/01 14:12:20 norby Exp $ */
+/* $OpenBSD: control.h,v 1.2 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -35,7 +35,7 @@ enum blockmodes {
struct ctl_conn {
TAILQ_ENTRY(ctl_conn) entry;
- struct imsgbuf ibuf;
+ struct imsgev iev;
};
int control_init(void);
diff --git a/usr.sbin/dvmrpd/dvmrpd.c b/usr.sbin/dvmrpd/dvmrpd.c
index 9362b27b1a0..901f0e6462b 100644
--- a/usr.sbin/dvmrpd/dvmrpd.c
+++ b/usr.sbin/dvmrpd/dvmrpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dvmrpd.c,v 1.9 2009/06/01 23:22:58 henning Exp $ */
+/* $OpenBSD: dvmrpd.c,v 1.10 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -64,8 +64,8 @@ int pipe_parent2rde[2];
int pipe_dvmrpe2rde[2];
struct dvmrpd_conf *conf = NULL;
-struct imsgbuf *ibuf_dvmrpe;
-struct imsgbuf *ibuf_rde;
+struct imsgev *iev_dvmrpe;
+struct imsgev *iev_rde;
pid_t dvmrpe_pid;
pid_t rde_pid;
@@ -254,22 +254,24 @@ main(int argc, char *argv[])
close(pipe_dvmrpe2rde[0]);
close(pipe_dvmrpe2rde[1]);
- if ((ibuf_dvmrpe = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_dvmrpe = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_rde = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_dvmrpe, pipe_parent2dvmrpe[0], main_dispatch_dvmrpe);
- imsg_init(ibuf_rde, pipe_parent2rde[0], main_dispatch_rde);
+ imsg_init(&iev_dvmrpe->ibuf, pipe_parent2dvmrpe[0]);
+ imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]);
+ iev_dvmrpe->handler = main_dispatch_dvmrpe;
+ iev_rde->handler = main_dispatch_rde;
/* setup event handler */
- ibuf_dvmrpe->events = EV_READ;
- event_set(&ibuf_dvmrpe->ev, ibuf_dvmrpe->fd, ibuf_dvmrpe->events,
- ibuf_dvmrpe->handler, ibuf_dvmrpe);
- event_add(&ibuf_dvmrpe->ev, NULL);
+ iev_dvmrpe->events = EV_READ;
+ event_set(&iev_dvmrpe->ev, iev_dvmrpe->ibuf.fd, iev_dvmrpe->events,
+ iev_dvmrpe->handler, iev_dvmrpe);
+ event_add(&iev_dvmrpe->ev, NULL);
- ibuf_rde->events = EV_READ;
- event_set(&ibuf_rde->ev, ibuf_rde->fd, ibuf_rde->events,
- ibuf_rde->handler, ibuf_rde);
- event_add(&ibuf_rde->ev, NULL);
+ iev_rde->events = EV_READ;
+ event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
+ iev_rde->handler, iev_rde);
+ event_add(&iev_rde->ev, NULL);
if (kmr_init(!(conf->flags & DVMRPD_FLAG_NO_FIB_UPDATE)) == -1)
dvmrpd_shutdown();
@@ -315,10 +317,10 @@ dvmrpd_shutdown(void)
fatal("wait");
} while (pid != -1 || (pid == -1 && errno == EINTR));
- msgbuf_clear(&ibuf_dvmrpe->w);
- free(ibuf_dvmrpe);
- msgbuf_clear(&ibuf_rde->w);
- free(ibuf_rde);
+ msgbuf_clear(&iev_dvmrpe->ibuf.w);
+ free(iev_dvmrpe);
+ msgbuf_clear(&iev_rde->ibuf.w);
+ free(iev_rde);
log_info("terminating");
exit(0);
@@ -348,7 +350,8 @@ check_child(pid_t pid, const char *pname)
void
main_dispatch_dvmrpe(int fd, short event, void *bula)
{
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
ssize_t n;
@@ -388,14 +391,15 @@ main_dispatch_dvmrpe(int fd, short event, void *bula)
}
imsg_free(&imsg);
}
- imsg_event_add(ibuf);
+ imsg_event_add(iev);
}
void
main_dispatch_rde(int fd, short event, void *bula)
{
struct mfc mfc;
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
ssize_t n;
@@ -441,30 +445,41 @@ main_dispatch_rde(int fd, short event, void *bula)
}
imsg_free(&imsg);
}
- imsg_event_add(ibuf);
+ imsg_event_add(iev);
}
void
main_imsg_compose_dvmrpe(int type, pid_t pid, void *data, u_int16_t datalen)
{
- imsg_compose(ibuf_dvmrpe, type, 0, pid, data, datalen);
+ imsg_compose_event(iev_dvmrpe, type, 0, pid, -1, data, datalen);
}
void
main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
{
- imsg_compose(ibuf_rde, type, 0, pid, data, datalen);
+ imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen);
}
-/* this needs to be added here so that dvmrpctl can be used without libevent */
void
-imsg_event_add(struct imsgbuf *ibuf)
+imsg_event_add(struct imsgev *iev)
{
- ibuf->events = EV_READ;
- if (ibuf->w.queued)
- ibuf->events |= EV_WRITE;
+ iev->events = EV_READ;
+ if (iev->ibuf.w.queued)
+ iev->events |= EV_WRITE;
- event_del(&ibuf->ev);
- event_set(&ibuf->ev, ibuf->fd, ibuf->events, ibuf->handler, ibuf);
- event_add(&ibuf->ev, NULL);
+ event_del(&iev->ev);
+ event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
+ event_add(&iev->ev, NULL);
+}
+
+int
+imsg_compose_event(struct imsgev *iev, u_int16_t type,
+ u_int32_t peerid, pid_t pid, int fd, void *data, u_int16_t datalen)
+{
+ int ret;
+
+ if ((ret = imsg_compose(&iev->ibuf, type, peerid,
+ pid, fd, data, datalen)) != -1)
+ imsg_event_add(iev);
+ return (ret);
}
diff --git a/usr.sbin/dvmrpd/dvmrpd.h b/usr.sbin/dvmrpd/dvmrpd.h
index fd41c286f3a..7067c88e61d 100644
--- a/usr.sbin/dvmrpd/dvmrpd.h
+++ b/usr.sbin/dvmrpd/dvmrpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: dvmrpd.h,v 1.17 2009/04/16 20:11:12 michele Exp $ */
+/* $OpenBSD: dvmrpd.h,v 1.18 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2004, 2005, 2006 Esben Norby <norby@openbsd.org>
@@ -28,6 +28,8 @@
#include <netinet/in.h>
#include <event.h>
+#include "imsg.h"
+
#define CONF_FILE "/etc/dvmrpd.conf"
#define DVMRPD_SOCKET "/var/run/dvmrpd.sock"
#define DVMRPD_USER "_dvmrpd"
@@ -37,7 +39,6 @@
#define NBR_IDSELF 1
#define NBR_CNTSTART (NBR_IDSELF + 1)
-#define READ_BUF_SIZE 65535
#define RT_BUF_SIZE 16384
#define DVMRPD_FLAG_NO_FIB_UPDATE 0x0001
@@ -51,41 +52,12 @@
#define MAXVIFS 32 /* XXX */
-/* buffer */
-struct buf {
- TAILQ_ENTRY(buf) entry;
- u_char *buf;
- size_t size;
- size_t max;
- size_t wpos;
- size_t rpos;
- int fd;
-};
-
-struct msgbuf {
- u_int32_t queued;
- int fd;
- TAILQ_HEAD(, buf) bufs;
-};
-
-#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
-#define MAX_IMSGSIZE 8192
-
-struct buf_read {
- u_char buf[READ_BUF_SIZE];
- u_char *rptr;
- size_t wpos;
-};
-
-struct imsgbuf {
- TAILQ_HEAD(, imsg_fd) fds;
- struct buf_read r;
- struct msgbuf w;
- struct event ev;
+struct imsgev {
+ struct imsgbuf ibuf;
void (*handler)(int, short, void *);
- int fd;
- pid_t pid;
- short events;
+ struct event ev;
+ void *data;
+ short events;
};
enum imsg_type {
@@ -118,23 +90,6 @@ enum imsg_type {
IMSG_FLASH_UPDATE_DS
};
-struct imsg_hdr {
- enum imsg_type type;
- u_int16_t len;
- u_int32_t peerid;
- pid_t pid;
-};
-
-struct imsg {
- struct imsg_hdr hdr;
- void *data;
-};
-
-struct imsg_fd {
- TAILQ_ENTRY(imsg_fd) entry;
- int fd;
-};
-
/* interface states */
#define IF_STA_DOWN 0x01
#define IF_STA_QUERIER 0x02
@@ -403,18 +358,6 @@ struct ctl_sum {
u_int32_t hold_time;
};
-/* buffer.c */
-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);
-void *buf_seek(struct buf *, size_t, size_t);
-int buf_close(struct msgbuf *, struct buf *);
-void buf_free(struct buf *);
-void msgbuf_init(struct msgbuf *);
-void msgbuf_clear(struct msgbuf *);
-int msgbuf_write(struct msgbuf *);
-
/* dvmrpd.c */
void main_imsg_compose_dvmrpe(int, pid_t, void *, u_int16_t);
@@ -422,18 +365,9 @@ void main_imsg_compose_dvmrpe(int, pid_t, void *, u_int16_t);
struct dvmrpd_conf *parse_config(char *, int);
int cmdline_symset(char *);
-/* imsg.c */
-void imsg_init(struct imsgbuf *, int, void (*)(int, short, void *));
-ssize_t imsg_read(struct imsgbuf *);
-ssize_t imsg_get(struct imsgbuf *, struct imsg *);
-int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
- void *, u_int16_t);
-struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
- u_int16_t);
-int imsg_add(struct buf *, void *, u_int16_t);
-int imsg_close(struct imsgbuf *, struct buf *);
-void imsg_free(struct imsg *);
-void imsg_event_add(struct imsgbuf *); /* needs to be provided externally */
+int imsg_compose_event(struct imsgev *, u_int16_t, u_int32_t, pid_t,
+ int, void *, u_int16_t);
+void imsg_event_add(struct imsgev *);
/* in_cksum.c */
u_int16_t in_cksum(void *, size_t);
diff --git a/usr.sbin/dvmrpd/dvmrpe.c b/usr.sbin/dvmrpd/dvmrpe.c
index 00ad87565aa..16bb4fb634f 100644
--- a/usr.sbin/dvmrpd/dvmrpe.c
+++ b/usr.sbin/dvmrpd/dvmrpe.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dvmrpe.c,v 1.6 2009/05/31 20:31:35 jacekm Exp $ */
+/* $OpenBSD: dvmrpe.c,v 1.7 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -47,8 +47,8 @@ void dvmrpe_shutdown(void);
volatile sig_atomic_t dvmrpe_quit = 0;
struct dvmrpd_conf *deconf = NULL;
-struct imsgbuf *ibuf_main;
-struct imsgbuf *ibuf_rde;
+struct imsgev *iev_main;
+struct imsgev *iev_rde;
struct ctl_conn *ctl_conn;
void
@@ -140,22 +140,25 @@ dvmrpe(struct dvmrpd_conf *xconf, int pipe_parent2dvmrpe[2],
close(pipe_parent2rde[0]);
close(pipe_parent2rde[1]);
- if ((ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_rde = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_main = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_rde, pipe_dvmrpe2rde[0], dvmrpe_dispatch_rde);
- imsg_init(ibuf_main, pipe_parent2dvmrpe[1], dvmrpe_dispatch_main);
+ imsg_init(&iev_rde->ibuf, pipe_dvmrpe2rde[0]);
+ iev_rde->handler = dvmrpe_dispatch_rde;
+
+ imsg_init(&iev_main->ibuf, pipe_parent2dvmrpe[1]);
+ iev_main->handler = dvmrpe_dispatch_main;
/* setup event handler */
- ibuf_rde->events = EV_READ;
- event_set(&ibuf_rde->ev, ibuf_rde->fd, ibuf_rde->events,
- ibuf_rde->handler, ibuf_rde);
- event_add(&ibuf_rde->ev, NULL);
+ iev_rde->events = EV_READ;
+ event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
+ iev_rde->handler, iev_rde);
+ event_add(&iev_rde->ev, NULL);
- ibuf_main->events = EV_READ;
- event_set(&ibuf_main->ev, ibuf_main->fd, ibuf_main->events,
- ibuf_main->handler, ibuf_main);
- event_add(&ibuf_main->ev, NULL);
+ iev_main->events = EV_READ;
+ event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
+ iev_main->handler, iev_main);
+ event_add(&iev_main->ev, NULL);
event_set(&deconf->ev, deconf->dvmrp_socket, EV_READ|EV_PERSIST,
recv_packet, deconf);
@@ -201,12 +204,12 @@ dvmrpe_shutdown(void)
}
/* clean up */
- msgbuf_write(&ibuf_rde->w);
- msgbuf_clear(&ibuf_rde->w);
- free(ibuf_rde);
- msgbuf_write(&ibuf_main->w);
- msgbuf_clear(&ibuf_main->w);
- free(ibuf_main);
+ msgbuf_write(&iev_rde->ibuf.w);
+ msgbuf_clear(&iev_rde->ibuf.w);
+ free(iev_rde);
+ msgbuf_write(&iev_main->ibuf.w);
+ msgbuf_clear(&iev_main->ibuf.w);
+ free(iev_main);
free(pkt_ptr);
log_info("dvmrp engine exiting");
@@ -216,21 +219,23 @@ dvmrpe_shutdown(void)
int
dvmrpe_imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen)
{
- return (imsg_compose(ibuf_main, type, 0, pid, data, datalen));
+ return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
}
int
dvmrpe_imsg_compose_rde(int type, u_int32_t peerid, pid_t pid,
void *data, u_int16_t datalen)
{
- return (imsg_compose(ibuf_rde, type, peerid, pid, data, datalen));
+ return (imsg_compose_event(iev_rde, type, peerid, pid,
+ -1, data, datalen));
}
void
dvmrpe_dispatch_main(int fd, short event, void *bula)
{
struct imsg imsg;
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
ssize_t n;
if (event & EV_READ) {
@@ -258,13 +263,14 @@ dvmrpe_dispatch_main(int fd, short event, void *bula)
}
imsg_free(&imsg);
}
- imsg_event_add(ibuf);
+ imsg_event_add(iev);
}
void
dvmrpe_dispatch_rde(int fd, short event, void *bula)
{
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
struct nbr *nbr;
struct prune p;
@@ -403,7 +409,7 @@ dvmrpe_dispatch_rde(int fd, short event, void *bula)
}
imsg_free(&imsg);
}
- imsg_event_add(ibuf);
+ imsg_event_add(iev);
}
void
@@ -415,8 +421,8 @@ dvmrpe_iface_ctl(struct ctl_conn *c, unsigned int idx)
LIST_FOREACH(iface, &deconf->iface_list, entry)
if (idx == 0 || idx == iface->ifindex) {
ictl = if_to_ctl(iface);
- imsg_compose(&c->ibuf, IMSG_CTL_SHOW_IFACE,
- 0, 0, ictl, sizeof(struct ctl_iface));
+ imsg_compose_event(&c->iev, IMSG_CTL_SHOW_IFACE,
+ 0, 0, -1, ictl, sizeof(struct ctl_iface));
}
}
@@ -429,8 +435,8 @@ dvmrpe_iface_igmp_ctl(struct ctl_conn *c, unsigned int idx)
LIST_FOREACH(iface, &deconf->iface_list, entry)
if (idx == 0 || idx == iface->ifindex) {
ictl = if_to_ctl(iface);
- imsg_compose(&c->ibuf, IMSG_CTL_SHOW_IFACE,
- 0, 0, ictl, sizeof(struct ctl_iface));
+ imsg_compose_event(&c->iev, IMSG_CTL_SHOW_IFACE,
+ 0, 0, -1, ictl, sizeof(struct ctl_iface));
group_list_dump(iface, c);
}
@@ -446,9 +452,9 @@ dvmrpe_nbr_ctl(struct ctl_conn *c)
LIST_FOREACH(iface, &deconf->iface_list, entry)
LIST_FOREACH(nbr, &iface->nbr_list, entry) {
nctl = nbr_to_ctl(nbr);
- imsg_compose(&c->ibuf, IMSG_CTL_SHOW_NBR, 0, 0, nctl,
- sizeof(struct ctl_nbr));
+ imsg_compose_event(&c->iev, IMSG_CTL_SHOW_NBR,
+ 0, 0, -1, nctl, sizeof(struct ctl_nbr));
}
- imsg_compose(&c->ibuf, IMSG_CTL_END, 0, 0, NULL, 0);
+ imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
}
diff --git a/usr.sbin/dvmrpd/group.c b/usr.sbin/dvmrpd/group.c
index 8fe979090db..c8c51fd0e79 100644
--- a/usr.sbin/dvmrpd/group.c
+++ b/usr.sbin/dvmrpd/group.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: group.c,v 1.2 2009/03/06 18:39:13 michele Exp $ */
+/* $OpenBSD: group.c,v 1.3 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2006 Esben Norby <norby@openbsd.org>
@@ -453,8 +453,8 @@ group_list_dump(struct iface *iface, struct ctl_conn *c)
TAILQ_FOREACH(ge, &iface->group_list, entry) {
gctl = group_to_ctl(ge);
- imsg_compose(&c->ibuf, IMSG_CTL_SHOW_IGMP, 0, 0, gctl,
- sizeof(struct ctl_group));
+ imsg_compose_event(&c->iev, IMSG_CTL_SHOW_IGMP, 0, 0,
+ -1, gctl, sizeof(struct ctl_group));
}
}
diff --git a/usr.sbin/dvmrpd/imsg.c b/usr.sbin/dvmrpd/imsg.c
index f086fa677f9..c223a42d22a 100644
--- a/usr.sbin/dvmrpd/imsg.c
+++ b/usr.sbin/dvmrpd/imsg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg.c,v 1.2 2007/03/19 10:13:20 henning Exp $ */
+/* $OpenBSD: imsg.c,v 1.3 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -16,7 +16,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
@@ -24,30 +26,44 @@
#include <string.h>
#include <unistd.h>
-#include "dvmrpd.h"
-#include "log.h"
+#include "imsg.h"
void
-imsg_init(struct imsgbuf *ibuf, int fd, void (*handler)(int, short, void *))
+imsg_init(struct imsgbuf *ibuf, int fd)
{
msgbuf_init(&ibuf->w);
bzero(&ibuf->r, sizeof(ibuf->r));
ibuf->fd = fd;
ibuf->w.fd = fd;
ibuf->pid = getpid();
- ibuf->handler = handler;
TAILQ_INIT(&ibuf->fds);
}
ssize_t
imsg_read(struct imsgbuf *ibuf)
{
+ struct msghdr msg;
+ struct cmsghdr *cmsg;
+ union {
+ struct cmsghdr hdr;
+ char buf[CMSG_SPACE(sizeof(int) * 16)];
+ } cmsgbuf;
+ struct iovec iov;
ssize_t n;
+ int fd;
+ struct imsg_fd *ifd;
- if ((n = recv(ibuf->fd, ibuf->r.buf + ibuf->r.wpos,
- sizeof(ibuf->r.buf) - ibuf->r.wpos, 0)) == -1) {
+ bzero(&msg, sizeof(msg));
+
+ iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
+ iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = &cmsgbuf.buf;
+ msg.msg_controllen = sizeof(cmsgbuf.buf);
+
+ if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
if (errno != EINTR && errno != EAGAIN) {
- log_warn("imsg_read: pipe read error");
return (-1);
}
return (-2);
@@ -55,6 +71,21 @@ imsg_read(struct imsgbuf *ibuf)
ibuf->r.wpos += n;
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+ cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_RIGHTS) {
+ fd = (*(int *)CMSG_DATA(cmsg));
+ if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL) {
+ /* XXX: this return can leak */
+ return (-1);
+ }
+ ifd->fd = fd;
+ TAILQ_INSERT_TAIL(&ibuf->fds, ifd, entry);
+ }
+ /* we do not handle other ctl data level */
+ }
+
return (n);
}
@@ -71,8 +102,7 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
if (imsg->hdr.len < IMSG_HEADER_SIZE ||
imsg->hdr.len > MAX_IMSGSIZE) {
- log_warnx("imsg_get: imsg hdr len %u out of bounds, type=%u",
- imsg->hdr.len, imsg->hdr.type);
+ errno = ERANGE;
return (-1);
}
if (imsg->hdr.len > av)
@@ -80,7 +110,6 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
if ((imsg->data = malloc(datalen)) == NULL) {
- log_warn("imsg_get");
return (-1);
}
memcpy(imsg->data, ibuf->r.rptr, datalen);
@@ -96,11 +125,10 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
}
int
-imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
- pid_t pid, void *data, u_int16_t datalen)
+imsg_compose(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
+ pid_t pid, int fd, void *data, u_int16_t datalen)
{
struct buf *wbuf;
- int n;
if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
return (-1);
@@ -108,34 +136,56 @@ imsg_compose(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
if (imsg_add(wbuf, data, datalen) == -1)
return (-1);
- if ((n = imsg_close(ibuf, wbuf)) < 0)
+ wbuf->fd = fd;
+
+ imsg_close(ibuf, wbuf);
+
+ return (1);
+}
+
+int
+imsg_composev(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
+ pid_t pid, int fd, const struct iovec *iov, int iovcnt)
+{
+ struct buf *wbuf;
+ int i, datalen = 0;
+
+ for (i = 0; i < iovcnt; i++)
+ datalen += iov[i].iov_len;
+
+ if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
return (-1);
- return (n);
+ for (i = 0; i < iovcnt; i++)
+ if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
+ return (-1);
+
+ wbuf->fd = fd;
+
+ imsg_close(ibuf, wbuf);
+
+ return (1);
}
/* ARGSUSED */
struct buf *
-imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
+imsg_create(struct imsgbuf *ibuf, u_int16_t type, u_int32_t peerid,
pid_t pid, u_int16_t datalen)
{
struct buf *wbuf;
struct imsg_hdr hdr;
- if (datalen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
- log_warnx("imsg_create: len %u > MAX_IMSGSIZE; "
- "type %u peerid %lu", datalen + IMSG_HEADER_SIZE,
- type, peerid);
+ datalen += IMSG_HEADER_SIZE;
+ if (datalen > MAX_IMSGSIZE) {
+ errno = ERANGE;
return (NULL);
}
- hdr.len = (u_int16_t)(datalen + IMSG_HEADER_SIZE);
hdr.type = type;
hdr.peerid = peerid;
if ((hdr.pid = pid) == 0)
hdr.pid = ibuf->pid;
- if ((wbuf = buf_open(hdr.len)) == NULL) {
- log_warn("imsg_create: buf_open");
+ if ((wbuf = buf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
return (NULL);
}
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
@@ -149,26 +199,20 @@ imsg_add(struct buf *msg, void *data, u_int16_t datalen)
{
if (datalen)
if (buf_add(msg, data, datalen) == -1) {
- log_warnx("imsg_add: buf_add error");
buf_free(msg);
return (-1);
}
return (datalen);
}
-int
+void
imsg_close(struct imsgbuf *ibuf, struct buf *msg)
{
- int n;
+ struct imsg_hdr *hdr;
- if ((n = buf_close(&ibuf->w, msg)) < 0) {
- log_warnx("imsg_close: buf_close error");
- buf_free(msg);
- return (-1);
- }
- imsg_event_add(ibuf);
-
- return (n);
+ hdr = (struct imsg_hdr *)msg->buf;
+ hdr->len = (u_int16_t)msg->wpos;
+ buf_close(&ibuf->w, msg);
}
void
@@ -176,3 +220,35 @@ imsg_free(struct imsg *imsg)
{
free(imsg->data);
}
+
+int
+imsg_get_fd(struct imsgbuf *ibuf)
+{
+ int fd;
+ struct imsg_fd *ifd;
+
+ if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
+ return (-1);
+
+ fd = ifd->fd;
+ TAILQ_REMOVE(&ibuf->fds, ifd, entry);
+ free(ifd);
+
+ return (fd);
+}
+
+int
+imsg_flush(struct imsgbuf *ibuf)
+{
+ while (ibuf->w.queued)
+ if (msgbuf_write(&ibuf->w) < 0)
+ return (-1);
+ return (0);
+}
+
+void
+imsg_clear(struct imsgbuf *ibuf)
+{
+ while (ibuf->w.queued)
+ msgbuf_clear(&ibuf->w);
+}
diff --git a/usr.sbin/dvmrpd/rde.c b/usr.sbin/dvmrpd/rde.c
index 954187a3dd5..97000ff082a 100644
--- a/usr.sbin/dvmrpd/rde.c
+++ b/usr.sbin/dvmrpd/rde.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rde.c,v 1.19 2009/06/01 23:22:58 henning Exp $ */
+/* $OpenBSD: rde.c,v 1.20 2009/06/06 07:52:04 pyr Exp $ */
/*
* Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
@@ -48,8 +48,8 @@ int rde_select_ds_ifs(struct mfc *, struct iface *);
volatile sig_atomic_t rde_quit = 0;
struct dvmrpd_conf *rdeconf = NULL;
struct rde_nbr *nbrself;
-struct imsgbuf *ibuf_dvmrpe;
-struct imsgbuf *ibuf_main;
+struct imsgev *iev_dvmrpe;
+struct imsgev *iev_main;
void
rde_sig_handler(int sig, short event, void *arg)
@@ -119,22 +119,26 @@ rde(struct dvmrpd_conf *xconf, int pipe_parent2rde[2], int pipe_dvmrpe2rde[2],
close(pipe_parent2dvmrpe[0]);
close(pipe_parent2dvmrpe[1]);
- if ((ibuf_dvmrpe = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_dvmrpe = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_main = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_dvmrpe, pipe_dvmrpe2rde[1], rde_dispatch_imsg);
- imsg_init(ibuf_main, pipe_parent2rde[1], rde_dispatch_imsg);
+
+ imsg_init(&iev_dvmrpe->ibuf, pipe_dvmrpe2rde[1]);
+ iev_dvmrpe->handler = rde_dispatch_imsg;
+
+ imsg_init(&iev_main->ibuf, pipe_parent2rde[1]);
+ iev_main->handler = rde_dispatch_imsg;
/* setup event handler */
- ibuf_dvmrpe->events = EV_READ;
- event_set(&ibuf_dvmrpe->ev, ibuf_dvmrpe->fd, ibuf_dvmrpe->events,
- ibuf_dvmrpe->handler, ibuf_dvmrpe);
- event_add(&ibuf_dvmrpe->ev, NULL);
+ iev_dvmrpe->events = EV_READ;
+ event_set(&iev_dvmrpe->ev, iev_dvmrpe->ibuf.fd, iev_dvmrpe->events,
+ iev_dvmrpe->handler, iev_dvmrpe);
+ event_add(&iev_dvmrpe->ev, NULL);
- ibuf_main->events = EV_READ;
- event_set(&ibuf_main->ev, ibuf_main->fd, ibuf_main->events,
- ibuf_main->handler, ibuf_main);
- event_add(&ibuf_main->ev, NULL);
+ iev_main->events = EV_READ;
+ event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
+ iev_main->handler, iev_main);
+ event_add(&iev_main->ev, NULL);
rt_init();
mfc_init();
@@ -159,10 +163,10 @@ rde_shutdown(void)
if_del(iface);
}
- msgbuf_clear(&ibuf_dvmrpe->w);
- free(ibuf_dvmrpe);
- msgbuf_clear(&ibuf_main->w);
- free(ibuf_main);
+ msgbuf_clear(&iev_dvmrpe->ibuf.w);
+ free(iev_dvmrpe);
+ msgbuf_clear(&iev_main->ibuf.w);
+ free(iev_main);
free(rdeconf);
log_info("route decision engine exiting");
@@ -173,14 +177,15 @@ rde_shutdown(void)
int
rde_imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen)
{
- return (imsg_compose(ibuf_main, type, 0, pid, data, datalen));
+ return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
}
int
rde_imsg_compose_dvmrpe(int type, u_int32_t peerid, pid_t pid, void *data,
u_int16_t datalen)
{
- return (imsg_compose(ibuf_dvmrpe, type, peerid, pid, data, datalen));
+ return (imsg_compose_event(iev_dvmrpe, type, peerid, pid, -1,
+ data, datalen));
}
void
@@ -188,7 +193,8 @@ rde_dispatch_imsg(int fd, short event, void *bula)
{
struct mfc mfc;
struct prune p;
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
struct route_report rr;
struct nbr_msg nm;
@@ -216,13 +222,13 @@ rde_dispatch_imsg(int fd, short event, void *bula)
switch (imsg.hdr.type) {
case IMSG_CTL_SHOW_RIB:
rt_dump(imsg.hdr.pid);
- imsg_compose(ibuf_dvmrpe, IMSG_CTL_END, 0, imsg.hdr.pid,
- NULL, 0);
+ imsg_compose_event(iev_dvmrpe, IMSG_CTL_END, 0,
+ imsg.hdr.pid, -1, NULL, 0);
break;
case IMSG_CTL_SHOW_MFC:
mfc_dump(imsg.hdr.pid);
- imsg_compose(ibuf_dvmrpe, IMSG_CTL_END, 0, imsg.hdr.pid,
- NULL, 0);
+ imsg_compose_event(iev_dvmrpe, IMSG_CTL_END, 0,
+ imsg.hdr.pid, -1, NULL, 0);
break;
case IMSG_ROUTE_REPORT:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rr))
@@ -313,7 +319,7 @@ rde_dispatch_imsg(int fd, short event, void *bula)
}
imsg_free(&imsg);
}
- imsg_event_add(ibuf);
+ imsg_event_add(iev);
}
int