diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2009-06-06 08:20:56 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2009-06-06 08:20:56 +0000 |
commit | 9a677f2d953a22199af1bac140a115167517c88b (patch) | |
tree | 4f7c8493b33675a4a8a261c6a5ffc53366b458aa /usr.sbin | |
parent | 045b3dc7bf2a43657be65eef5452117f1e25c416 (diff) |
make ripctl/ripd imsg-in-a-lib ready too.
ok pyr@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/ripctl/ripctl.c | 30 | ||||
-rw-r--r-- | usr.sbin/ripd/buffer.c | 109 | ||||
-rw-r--r-- | usr.sbin/ripd/control.c | 44 | ||||
-rw-r--r-- | usr.sbin/ripd/control.h | 4 | ||||
-rw-r--r-- | usr.sbin/ripd/imsg.c | 148 | ||||
-rw-r--r-- | usr.sbin/ripd/imsg.h | 105 | ||||
-rw-r--r-- | usr.sbin/ripd/rde.c | 73 | ||||
-rw-r--r-- | usr.sbin/ripd/ripd.c | 92 | ||||
-rw-r--r-- | usr.sbin/ripd/ripd.h | 90 | ||||
-rw-r--r-- | usr.sbin/ripd/ripe.c | 73 |
10 files changed, 499 insertions, 269 deletions
diff --git a/usr.sbin/ripctl/ripctl.c b/usr.sbin/ripctl/ripctl.c index 52e55d66db0..0471dd66f88 100644 --- a/usr.sbin/ripctl/ripctl.c +++ b/usr.sbin/ripctl/ripctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ripctl.c,v 1.6 2008/12/31 14:10:20 sobrado Exp $ +/* $OpenBSD: ripctl.c,v 1.7 2009/06/06 08:20:55 eric Exp $ * * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it> * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> @@ -62,12 +62,6 @@ usage(void) exit(1); } -/* dummy function to allow ripctl to run without libevent */ -void -imsg_event_add(struct imsgbuf *i) -{ -} - int main(int argc, char *argv[]) { @@ -95,7 +89,7 @@ main(int argc, char *argv[]) if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) err(1, NULL); - imsg_init(ibuf, ctl_sock, NULL); + imsg_init(ibuf, ctl_sock); done = 0; /* process user request */ @@ -113,51 +107,51 @@ main(int argc, char *argv[]) if (ifidx == 0) errx(1, "no such interface %s", res->ifname); } - imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, + imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, -1, &ifidx, sizeof(ifidx)); break; case SHOW_NBR: printf("%-15s %-15s %-15s %-9s %-10s\n", "ID", "State", "Address", "Iface", "Uptime"); - 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\n", "Destination", "Nexthop", "Cost"); - 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_FIB: if (!res->addr.s_addr) - imsg_compose(ibuf, IMSG_CTL_KROUTE, 0, 0, + imsg_compose(ibuf, IMSG_CTL_KROUTE, 0, 0, -1, &res->flags, sizeof(res->flags)); else - imsg_compose(ibuf, IMSG_CTL_KROUTE_ADDR, 0, 0, + imsg_compose(ibuf, IMSG_CTL_KROUTE_ADDR, 0, 0, -1, &res->addr, sizeof(res->addr)); show_fib_head(); break; case SHOW_FIB_IFACE: if (*res->ifname) - imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, + imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, res->ifname, sizeof(res->ifname)); else - imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, NULL, 0); + imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, NULL, 0); show_interface_head(); break; case FIB: errx(1, "fib couple|decouple"); break; case FIB_COUPLE: - imsg_compose(ibuf, IMSG_CTL_FIB_COUPLE, 0, 0, NULL, 0); + imsg_compose(ibuf, IMSG_CTL_FIB_COUPLE, 0, 0, -1, NULL, 0); printf("couple request sent.\n"); done = 1; break; case FIB_DECOUPLE: - imsg_compose(ibuf, IMSG_CTL_FIB_DECOUPLE, 0, 0, NULL, 0); + imsg_compose(ibuf, IMSG_CTL_FIB_DECOUPLE, 0, 0, -1, NULL, 0); printf("decouple request sent.\n"); done = 1; 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/ripd/buffer.c b/usr.sbin/ripd/buffer.c index 5ee360faa74..3bb7cb992a8 100644 --- a/usr.sbin/ripd/buffer.c +++ b/usr.sbin/ripd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.2 2007/10/24 20:18:07 claudio Exp $ */ +/* $OpenBSD: buffer.c,v 1.3 2009/06/06 08:20:55 eric 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 "ripd.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/ripd/control.c b/usr.sbin/ripd/control.c index cd1ddd0e415..917ecb5dfaf 100644 --- a/usr.sbin/ripd/control.c +++ b/usr.sbin/ripd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.9 2009/05/31 20:30:15 jacekm Exp $ */ +/* $OpenBSD: control.c,v 1.10 2009/06/06 08:20:55 eric Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -132,11 +132,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); } @@ -146,7 +147,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 */ @@ -158,7 +159,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 */ @@ -175,11 +176,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); } @@ -198,20 +199,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; } @@ -226,11 +227,12 @@ control_dispatch_imsg(int fd, short event, void *bula) memcpy(&ifidx, imsg.data, sizeof(ifidx)); ripe_iface_ctl(c, ifidx); - imsg_compose(&c->ibuf, IMSG_CTL_END, 0, 0, NULL, 0); + imsg_compose(&c->iev.ibuf, IMSG_CTL_END, 0, 0, + -1, NULL, 0); break; case IMSG_CTL_SHOW_RIB: - c->ibuf.pid = imsg.hdr.pid; + c->iev.ibuf.pid = imsg.hdr.pid; ripe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); @@ -241,7 +243,7 @@ control_dispatch_imsg(int fd, short event, void *bula) case IMSG_CTL_KROUTE_ADDR: case IMSG_CTL_KROUTE: case IMSG_CTL_IFINFO: - c->ibuf.pid = imsg.hdr.pid; + c->iev.ibuf.pid = imsg.hdr.pid; ripe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); @@ -249,7 +251,7 @@ control_dispatch_imsg(int fd, short event, void *bula) case IMSG_CTL_FIB_COUPLE: case IMSG_CTL_FIB_DECOUPLE: case IMSG_CTL_RELOAD: - c->ibuf.pid = imsg.hdr.pid; + c->iev.ibuf.pid = imsg.hdr.pid; ripe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0); break; default: @@ -260,7 +262,7 @@ control_dispatch_imsg(int fd, short event, void *bula) imsg_free(&imsg); } - imsg_event_add(&c->ibuf); + imsg_event_add(&c->iev); } int @@ -271,8 +273,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/ripd/control.h b/usr.sbin/ripd/control.h index 6d372673006..23240d367ae 100644 --- a/usr.sbin/ripd/control.h +++ b/usr.sbin/ripd/control.h @@ -1,4 +1,4 @@ -/* $OpenBSD: control.h,v 1.1 2006/10/18 16:11:58 norby Exp $ */ +/* $OpenBSD: control.h,v 1.2 2009/06/06 08:20:55 eric 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/ripd/imsg.c b/usr.sbin/ripd/imsg.c index 28a9cb17ba7..1488ac4796b 100644 --- a/usr.sbin/ripd/imsg.c +++ b/usr.sbin/ripd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.2 2007/03/19 10:10:29 henning Exp $ */ +/* $OpenBSD: imsg.c,v 1.3 2009/06/06 08:20:55 eric 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 "ripd.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/ripd/imsg.h b/usr.sbin/ripd/imsg.h new file mode 100644 index 00000000000..857675180e2 --- /dev/null +++ b/usr.sbin/ripd/imsg.h @@ -0,0 +1,105 @@ +/* $OpenBSD: imsg.h,v 1.1 2009/06/06 08:20:55 eric Exp $ */ + +/* + * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org> + * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org> + * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/tree.h> + +#define READ_BUF_SIZE 65535 +#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr) +#define MAX_IMSGSIZE 16384 + +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 { + TAILQ_HEAD(, buf) bufs; + u_int32_t queued; + int fd; +}; + +struct buf_read { + u_char buf[READ_BUF_SIZE]; + u_char *rptr; + size_t wpos; +}; + +struct imsg_fd { + TAILQ_ENTRY(imsg_fd) entry; + int fd; +}; + +struct imsgbuf { + TAILQ_HEAD(, imsg_fd) fds; + struct buf_read r; + struct msgbuf w; + int fd; + pid_t pid; +}; + +struct imsg_hdr { + u_int16_t type; + u_int16_t len; + u_int32_t peerid; + pid_t pid; +}; + +struct imsg { + struct imsg_hdr hdr; + void *data; +}; + + +/* buffer.c */ +struct buf *buf_open(size_t); +struct buf *buf_dynamic(size_t, size_t); +int buf_add(struct buf *, const void *, size_t); +void *buf_reserve(struct buf *, size_t); +void *buf_seek(struct buf *, size_t, size_t); +size_t buf_size(struct buf *); +size_t buf_left(struct buf *); +void buf_close(struct msgbuf *, struct buf *); +int buf_write(struct msgbuf *); +void buf_free(struct buf *); +void msgbuf_init(struct msgbuf *); +void msgbuf_clear(struct msgbuf *); +int msgbuf_write(struct msgbuf *); + +/* imsg.c */ +void imsg_init(struct imsgbuf *, int); +ssize_t imsg_read(struct imsgbuf *); +ssize_t imsg_get(struct imsgbuf *, struct imsg *); +int imsg_compose(struct imsgbuf *, u_int16_t, u_int32_t, pid_t, + int, void *, u_int16_t); +int imsg_composev(struct imsgbuf *, u_int16_t, u_int32_t, pid_t, + int, const struct iovec *, int); +struct buf *imsg_create(struct imsgbuf *, u_int16_t, u_int32_t, pid_t, + u_int16_t); +int imsg_add(struct buf *, void *, u_int16_t); +void imsg_close(struct imsgbuf *, struct buf *); +void imsg_free(struct imsg *); +int imsg_get_fd(struct imsgbuf *); +int imsg_flush(struct imsgbuf *); +void imsg_clear(struct imsgbuf *); diff --git a/usr.sbin/ripd/rde.c b/usr.sbin/ripd/rde.c index f00e135e3f4..53dc1858406 100644 --- a/usr.sbin/ripd/rde.c +++ b/usr.sbin/ripd/rde.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rde.c,v 1.11 2009/05/31 20:30:15 jacekm Exp $ */ +/* $OpenBSD: rde.c,v 1.12 2009/06/06 08:20:55 eric Exp $ */ /* * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it> @@ -40,8 +40,8 @@ #include "rde.h" struct ripd_conf *rdeconf = NULL; -struct imsgbuf *ibuf_ripe; -struct imsgbuf *ibuf_main; +struct imsgev *iev_ripe; +struct imsgev *iev_main; void rde_sig_handler(int, short, void *); void rde_shutdown(void); @@ -123,22 +123,24 @@ rde(struct ripd_conf *xconf, int pipe_parent2rde[2], int pipe_ripe2rde[2], close(pipe_parent2ripe[0]); close(pipe_parent2ripe[1]); - if ((ibuf_ripe = malloc(sizeof(struct imsgbuf))) == NULL || - (ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL) + if ((iev_ripe = malloc(sizeof(struct imsgev))) == NULL || + (iev_main = malloc(sizeof(struct imsgev))) == NULL) fatal(NULL); - imsg_init(ibuf_ripe, pipe_ripe2rde[1], rde_dispatch_imsg); - imsg_init(ibuf_main, pipe_parent2rde[1], rde_dispatch_parent); + imsg_init(&iev_ripe->ibuf, pipe_ripe2rde[1]); + iev_ripe->handler = rde_dispatch_imsg; + imsg_init(&iev_main->ibuf, pipe_parent2rde[1]); + iev_main->handler = rde_dispatch_parent; /* setup event handler */ - ibuf_ripe->events = EV_READ; - event_set(&ibuf_ripe->ev, ibuf_ripe->fd, ibuf_ripe->events, - ibuf_ripe->handler, ibuf_ripe); - event_add(&ibuf_ripe->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_ripe->events = EV_READ; + event_set(&iev_ripe->ev, iev_ripe->ibuf.fd, iev_ripe->events, + iev_ripe->handler, iev_ripe); + event_add(&iev_ripe->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(); /* remove unneeded config stuff */ @@ -160,10 +162,10 @@ rde_shutdown(void) { rt_clear(); - msgbuf_clear(&ibuf_ripe->w); - free(ibuf_ripe); - msgbuf_clear(&ibuf_main->w); - free(ibuf_main); + msgbuf_clear(&iev_ripe->ibuf.w); + free(iev_ripe); + msgbuf_clear(&iev_main->ibuf.w); + free(iev_main); free(rdeconf); log_info("route decision engine exiting"); @@ -174,14 +176,16 @@ int rde_imsg_compose_ripe(int type, u_int32_t peerid, pid_t pid, void *data, u_int16_t datalen) { - return (imsg_compose(ibuf_ripe, type, peerid, pid, data, datalen)); + return (imsg_compose_event(iev_ripe, type, peerid, pid, -1, + data, datalen)); } /* ARGSUSED */ void rde_dispatch_imsg(int fd, short event, void *bula) { - struct imsgbuf *ibuf = bula; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; struct rip_route rr; struct imsg imsg; ssize_t n; @@ -250,8 +254,8 @@ rde_dispatch_imsg(int fd, short event, void *bula) case IMSG_CTL_SHOW_RIB: rt_dump(imsg.hdr.pid); - imsg_compose(ibuf_ripe, IMSG_CTL_END, 0, imsg.hdr.pid, - NULL, 0); + imsg_compose_event(iev_ripe, IMSG_CTL_END, 0, + imsg.hdr.pid, -1, NULL, 0); break; default: @@ -262,10 +266,10 @@ rde_dispatch_imsg(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -277,7 +281,8 @@ rde_dispatch_parent(int fd, short event, void *bula) struct imsg imsg; struct rt_node *rt; struct kroute kr; - struct imsgbuf *ibuf = bula; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; ssize_t n; int shut = 0; @@ -333,8 +338,8 @@ rde_dispatch_parent(int fd, short event, void *bula) rde_send_change_kroute(rt); else /* should not happen */ - imsg_compose(ibuf_main, IMSG_KROUTE_DELETE, 0, - 0, &kr, sizeof(kr)); + imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, + 0, 0, -1, &kr, sizeof(kr)); break; default: @@ -345,10 +350,10 @@ rde_dispatch_parent(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -366,7 +371,8 @@ rde_send_change_kroute(struct rt_node *r) kr.flags = r->flags; kr.ifindex = r->ifindex; - imsg_compose(ibuf_main, IMSG_KROUTE_CHANGE, 0, 0, &kr, sizeof(kr)); + imsg_compose_event(iev_main, IMSG_KROUTE_CHANGE, 0, 0, -1, + &kr, sizeof(kr)); } void @@ -382,7 +388,8 @@ rde_send_delete_kroute(struct rt_node *r) kr.flags = r->flags; kr.ifindex = r->ifindex; - imsg_compose(ibuf_main, IMSG_KROUTE_DELETE, 0, 0, &kr, sizeof(kr)); + imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 0, 0, -1, + &kr, sizeof(kr)); } int diff --git a/usr.sbin/ripd/ripd.c b/usr.sbin/ripd/ripd.c index 57167cce94d..721c8c8fd24 100644 --- a/usr.sbin/ripd/ripd.c +++ b/usr.sbin/ripd/ripd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ripd.c,v 1.15 2009/05/31 20:30:15 jacekm Exp $ */ +/* $OpenBSD: ripd.c,v 1.16 2009/06/06 08:20:55 eric Exp $ */ /* * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it> @@ -60,8 +60,8 @@ int pipe_parent2rde[2]; int pipe_ripe2rde[2]; struct ripd_conf *conf = NULL; -struct imsgbuf *ibuf_ripe; -struct imsgbuf *ibuf_rde; +struct imsgev *iev_ripe; +struct imsgev *iev_rde; pid_t ripe_pid = 0; pid_t rde_pid = 0; @@ -246,22 +246,24 @@ main(int argc, char *argv[]) close(pipe_ripe2rde[0]); close(pipe_ripe2rde[1]); - if ((ibuf_ripe = malloc(sizeof(struct imsgbuf))) == NULL || - (ibuf_rde = malloc(sizeof(struct imsgbuf))) == NULL) + if ((iev_ripe = malloc(sizeof(struct imsgev))) == NULL || + (iev_rde = malloc(sizeof(struct imsgev))) == NULL) fatal(NULL); - imsg_init(ibuf_ripe, pipe_parent2ripe[0], main_dispatch_ripe); - imsg_init(ibuf_rde, pipe_parent2rde[0], main_dispatch_rde); + imsg_init(&iev_ripe->ibuf, pipe_parent2ripe[0]); + iev_ripe->handler = main_dispatch_ripe; + imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]); + iev_rde->handler = main_dispatch_rde; /* setup event handler */ - ibuf_ripe->events = EV_READ; - event_set(&ibuf_ripe->ev, ibuf_ripe->fd, ibuf_ripe->events, - ibuf_ripe->handler, ibuf_ripe); - event_add(&ibuf_ripe->ev, NULL); + iev_ripe->events = EV_READ; + event_set(&iev_ripe->ev, iev_ripe->ibuf.fd, iev_ripe->events, + iev_ripe->handler, iev_ripe); + event_add(&iev_ripe->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 (kr_init(!(conf->flags & RIPD_FLAG_NO_FIB_UPDATE)) == -1) fatalx("kr_init failed"); @@ -299,10 +301,10 @@ ripd_shutdown(void) fatal("wait"); } while (pid != -1 || (pid == -1 && errno == EINTR)); - msgbuf_clear(&ibuf_ripe->w); - free(ibuf_ripe); - msgbuf_clear(&ibuf_rde->w); - free(ibuf_rde); + msgbuf_clear(&iev_ripe->ibuf.w); + free(iev_ripe); + msgbuf_clear(&iev_rde->ibuf.w); + free(iev_rde); free(conf); log_info("terminating"); @@ -334,7 +336,8 @@ check_child(pid_t pid, const char *pname) void main_dispatch_ripe(int fd, short event, void *bula) { - struct imsgbuf *ibuf = bula; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; struct imsg imsg; struct demote_msg dmsg; ssize_t n; @@ -394,10 +397,10 @@ main_dispatch_ripe(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -406,7 +409,8 @@ main_dispatch_ripe(int fd, short event, void *bula) void main_dispatch_rde(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; int shut = 0; @@ -448,10 +452,10 @@ main_dispatch_rde(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -459,13 +463,13 @@ main_dispatch_rde(int fd, short event, void *bula) void main_imsg_compose_ripe(int type, pid_t pid, void *data, u_int16_t datalen) { - imsg_compose(ibuf_ripe, type, 0, pid, data, datalen); + imsg_compose_event(iev_ripe, 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); } int @@ -536,15 +540,31 @@ rip_redistribute(struct kroute *kr) return (0); } -/* this needs to be added here so that ripctl 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; - - event_del(&ibuf->ev); - event_set(&ibuf->ev, ibuf->fd, ibuf->events, ibuf->handler, ibuf); - event_add(&ibuf->ev, NULL); + if (iev->handler == NULL) { + imsg_flush(&iev->ibuf); + return; + } + + iev->events = EV_READ; + if (iev->ibuf.w.queued) + iev->events |= EV_WRITE; + + 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/ripd/ripd.h b/usr.sbin/ripd/ripd.h index 718e6a5d81e..36210d79788 100644 --- a/usr.sbin/ripd/ripd.h +++ b/usr.sbin/ripd/ripd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ripd.h,v 1.13 2009/06/02 21:43:36 claudio Exp $ */ +/* $OpenBSD: ripd.h,v 1.14 2009/06/06 08:20:55 eric Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -28,6 +28,8 @@ #include <netinet/in.h> #include <event.h> +#include "imsg.h" + #define CONF_FILE "/etc/ripd.conf" #define RIPD_SOCKET "/var/run/ripd.sock" #define RIPD_USER "_ripd" @@ -41,7 +43,6 @@ #define NBR_TIMEOUT 180 -#define READ_BUF_SIZE 65535 #define RT_BUF_SIZE 16384 #define MAX_RTSOCK_BUF 128 * 1024 @@ -65,42 +66,6 @@ #define OPT_SPLIT_POISONED 0x02 #define OPT_TRIGGERED_UPDATES 0x04 -/* buffer */ -struct buf { - TAILQ_ENTRY(buf) entry; - u_char *buf; - size_t size; - size_t max; - size_t wpos; - size_t rpos; -}; - -struct msgbuf { - TAILQ_HEAD(, buf) bufs; - u_int32_t queued; - int fd; -}; - -#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; - void (*handler)(int, short, void *); - int fd; - pid_t pid; - short events; -}; - enum imsg_type { IMSG_NONE, IMSG_CTL_END, @@ -133,21 +98,12 @@ enum imsg_type { IMSG_DEMOTE }; -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; +struct imsgev { + struct imsgbuf ibuf; + void (*handler)(int, short, void *); + struct event ev; + void *data; + short events; }; /* interface states */ @@ -374,42 +330,20 @@ u_int8_t mask2prefixlen(in_addr_t); void main_imsg_compose_ripe(int, pid_t, void *, u_int16_t); void main_imsg_compose_rde(int, pid_t, void *, u_int16_t); int rip_redistribute(struct kroute *); +void imsg_event_add(struct imsgev *); +int imsg_compose_event(struct imsgev *, u_int16_t, u_int32_t, + pid_t, int, void *, u_int16_t); /* parse.y */ struct ripd_conf *parse_config(char *, int); int cmdline_symset(char *); -/* 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 *); - /* carp.c */ int carp_demote_init(char *, int); void carp_demote_shutdown(void); int carp_demote_get(char *); int carp_demote_set(char *, int); -/* 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 *); - /* printconf.c */ void print_config(struct ripd_conf *); diff --git a/usr.sbin/ripd/ripe.c b/usr.sbin/ripd/ripe.c index 1094c50cc97..2d6f4f584c3 100644 --- a/usr.sbin/ripd/ripe.c +++ b/usr.sbin/ripd/ripe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ripe.c,v 1.11 2009/05/31 20:30:15 jacekm Exp $ */ +/* $OpenBSD: ripe.c,v 1.12 2009/06/06 08:20:55 eric Exp $ */ /* * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it> @@ -47,8 +47,8 @@ void ripe_sig_handler(int, short, void *); void ripe_shutdown(void); struct ripd_conf *oeconf = NULL; -struct imsgbuf *ibuf_main; -struct imsgbuf *ibuf_rde; +struct imsgev *iev_main; +struct imsgev *iev_rde; /* ARGSUSED */ void @@ -151,22 +151,24 @@ ripe(struct ripd_conf *xconf, int pipe_parent2ripe[2], int pipe_ripe2rde[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_ripe2rde[0], ripe_dispatch_rde); - imsg_init(ibuf_main, pipe_parent2ripe[1], ripe_dispatch_main); + imsg_init(&iev_rde->ibuf, pipe_ripe2rde[0]); + iev_rde->handler = ripe_dispatch_rde; + imsg_init(&iev_main->ibuf, pipe_parent2ripe[1]); + iev_main->handler = ripe_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(&oeconf->ev, oeconf->rip_socket, EV_READ|EV_PERSIST, recv_packet, oeconf); @@ -208,14 +210,15 @@ ripe(struct ripd_conf *xconf, int pipe_parent2ripe[2], int pipe_ripe2rde[2], int ripe_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 ripe_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)); } /* ARGSUSED */ @@ -223,7 +226,8 @@ void ripe_dispatch_main(int fd, short event, void *bula) { struct imsg imsg; - struct imsgbuf *ibuf = bula; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; struct kif *kif; struct iface *iface; ssize_t n; @@ -288,10 +292,10 @@ ripe_dispatch_main(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -302,7 +306,8 @@ ripe_dispatch_rde(int fd, short event, void *bula) { struct rip_route *rr; struct imsg imsg; - struct imsgbuf *ibuf = bula; + struct imsgev *iev = bula; + struct imsgbuf *ibuf = &iev->ibuf; struct iface *iface; struct nbr *nbr; ssize_t n; @@ -434,10 +439,10 @@ ripe_dispatch_rde(int fd, short event, void *bula) imsg_free(&imsg); } if (!shut) - imsg_event_add(ibuf); + imsg_event_add(iev); else { /* this pipe is dead, so remove the event handler */ - event_del(&ibuf->ev); + event_del(&iev->ev); event_loopexit(NULL); } } @@ -461,12 +466,12 @@ ripe_shutdown(void) close(oeconf->rip_socket); /* 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(oeconf); free(pkt_ptr); @@ -483,8 +488,8 @@ ripe_iface_ctl(struct ctl_conn *c, unsigned int idx) LIST_FOREACH(iface, &oeconf->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)); } } } @@ -499,12 +504,12 @@ ripe_nbr_ctl(struct ctl_conn *c) LIST_FOREACH(iface, &oeconf->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, + 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); } void |