summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorPierre-Yves Ritschard <pyr@cvs.openbsd.org>2009-06-06 08:09:44 +0000
committerPierre-Yves Ritschard <pyr@cvs.openbsd.org>2009-06-06 08:09:44 +0000
commit045b3dc7bf2a43657be65eef5452117f1e25c416 (patch)
tree309662f2289ebe383ac921b7dbe85740b581643b /usr.sbin
parentd34138cae376a2b9b00d469124798b9fb695c64a (diff)
make ldpd imsg-in-a-lib ready too.
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/ldpd/buffer.c111
-rw-r--r--usr.sbin/ldpd/control.c45
-rw-r--r--usr.sbin/ldpd/control.h4
-rw-r--r--usr.sbin/ldpd/imsg.c139
-rw-r--r--usr.sbin/ldpd/imsg.h105
-rw-r--r--usr.sbin/ldpd/lde.c97
-rw-r--r--usr.sbin/ldpd/ldpd.c89
-rw-r--r--usr.sbin/ldpd/ldpd.h83
-rw-r--r--usr.sbin/ldpd/ldpe.c74
9 files changed, 494 insertions, 253 deletions
diff --git a/usr.sbin/ldpd/buffer.c b/usr.sbin/ldpd/buffer.c
index 6e00d182ef3..258bfe74912 100644
--- a/usr.sbin/ldpd/buffer.c
+++ b/usr.sbin/ldpd/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.1 2009/06/01 20:59:45 michele Exp $ */
+/* $OpenBSD: buffer.c,v 1.2 2009/06/06 08:09:43 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 "ldpd.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
@@ -157,9 +214,14 @@ msgbuf_write(struct msgbuf *msgbuf)
{
struct iovec iov[IOV_MAX];
struct buf *buf, *next;
- int i = 0;
+ 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/ldpd/control.c b/usr.sbin/ldpd/control.c
index 5da80f884ee..9726038d0a8 100644
--- a/usr.sbin/ldpd/control.c
+++ b/usr.sbin/ldpd/control.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.c,v 1.1 2009/06/01 20:59:45 michele Exp $ */
+/* $OpenBSD: control.c,v 1.2 2009/06/06 08:09:43 pyr 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;
}
@@ -225,13 +226,13 @@ control_dispatch_imsg(int fd, short event, void *bula)
ldpe_fib_update(imsg.hdr.type);
/* FALLTHROUGH */
case IMSG_CTL_RELOAD:
- c->ibuf.pid = imsg.hdr.pid;
+ c->iev.ibuf.pid = imsg.hdr.pid;
ldpe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0);
break;
case IMSG_CTL_KROUTE:
case IMSG_CTL_KROUTE_ADDR:
case IMSG_CTL_IFINFO:
- c->ibuf.pid = imsg.hdr.pid;
+ c->iev.ibuf.pid = imsg.hdr.pid;
ldpe_imsg_compose_parent(imsg.hdr.type,
imsg.hdr.pid, imsg.data,
imsg.hdr.len - IMSG_HEADER_SIZE);
@@ -241,12 +242,12 @@ control_dispatch_imsg(int fd, short event, void *bula)
sizeof(ifidx)) {
memcpy(&ifidx, imsg.data, sizeof(ifidx));
ldpe_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_LIB:
- c->ibuf.pid = imsg.hdr.pid;
+ c->iev.ibuf.pid = imsg.hdr.pid;
ldpe_imsg_compose_lde(imsg.hdr.type, 0, imsg.hdr.pid,
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
break;
@@ -261,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
@@ -272,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/ldpd/control.h b/usr.sbin/ldpd/control.h
index 30de3762269..76a9b41afee 100644
--- a/usr.sbin/ldpd/control.h
+++ b/usr.sbin/ldpd/control.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: control.h,v 1.1 2009/06/01 20:59:45 michele Exp $ */
+/* $OpenBSD: control.h,v 1.2 2009/06/06 08:09:43 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/ldpd/imsg.c b/usr.sbin/ldpd/imsg.c
index 20724baed2e..50006b32fac 100644
--- a/usr.sbin/ldpd/imsg.c
+++ b/usr.sbin/ldpd/imsg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg.c,v 1.1 2009/06/01 20:59:45 michele Exp $ */
+/* $OpenBSD: imsg.c,v 1.2 2009/06/06 08:09:43 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 "ldpd.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,15 +136,40 @@ 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;
@@ -124,9 +177,7 @@ imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
datalen += IMSG_HEADER_SIZE;
if (datalen > MAX_IMSGSIZE) {
- log_warnx("imsg_create: len %u > MAX_IMSGSIZE; "
- "type %u peerid %lu", datalen + IMSG_HEADER_SIZE,
- type, peerid);
+ errno = ERANGE;
return (NULL);
}
@@ -135,7 +186,6 @@ imsg_create(struct imsgbuf *ibuf, enum imsg_type type, u_int32_t peerid,
if ((hdr.pid = pid) == 0)
hdr.pid = ibuf->pid;
if ((wbuf = buf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
- log_warn("imsg_create: buf_open");
return (NULL);
}
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
@@ -149,29 +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;
hdr = (struct imsg_hdr *)msg->buf;
hdr->len = (u_int16_t)msg->wpos;
- 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);
+ buf_close(&ibuf->w, msg);
}
void
@@ -179,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/ldpd/imsg.h b/usr.sbin/ldpd/imsg.h
new file mode 100644
index 00000000000..97134f9c767
--- /dev/null
+++ b/usr.sbin/ldpd/imsg.h
@@ -0,0 +1,105 @@
+/* $OpenBSD: imsg.h,v 1.1 2009/06/06 08:09:43 pyr 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/ldpd/lde.c b/usr.sbin/ldpd/lde.c
index 6bb8ad4fcaf..70388472227 100644
--- a/usr.sbin/ldpd/lde.c
+++ b/usr.sbin/ldpd/lde.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lde.c,v 1.2 2009/06/05 22:34:45 michele Exp $ */
+/* $OpenBSD: lde.c,v 1.3 2009/06/06 08:09:43 pyr Exp $ */
/*
* Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
@@ -54,8 +54,8 @@ void lde_req_list_free(struct lde_nbr *);
void lde_map_list_free(struct lde_nbr *);
struct ldpd_conf *ldeconf = NULL, *nconf = NULL;
-struct imsgbuf *ibuf_ldpe;
-struct imsgbuf *ibuf_main;
+struct imsgev *iev_ldpe;
+struct imsgev *iev_main;
struct lde_nbr *nbrself;
/* ARGSUSED */
@@ -131,22 +131,24 @@ lde(struct ldpd_conf *xconf, int pipe_parent2lde[2], int pipe_ldpe2lde[2],
close(pipe_parent2ldpe[0]);
close(pipe_parent2ldpe[1]);
- if ((ibuf_ldpe = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_main = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_ldpe, pipe_ldpe2lde[1], lde_dispatch_imsg);
- imsg_init(ibuf_main, pipe_parent2lde[1], lde_dispatch_parent);
+ imsg_init(&iev_ldpe->ibuf, pipe_ldpe2lde[1]);
+ iev_ldpe->handler = lde_dispatch_imsg;
+ imsg_init(&iev_main->ibuf, pipe_parent2lde[1]);
+ iev_main->handler = lde_dispatch_parent;
/* setup event handler */
- ibuf_ldpe->events = EV_READ;
- event_set(&ibuf_ldpe->ev, ibuf_ldpe->fd, ibuf_ldpe->events,
- ibuf_ldpe->handler, ibuf_ldpe);
- event_add(&ibuf_ldpe->ev, NULL);
+ iev_ldpe->events = EV_READ;
+ event_set(&iev_ldpe->ev, iev_ldpe->ibuf.fd, iev_ldpe->events,
+ iev_ldpe->handler, iev_ldpe);
+ event_add(&iev_ldpe->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();
@@ -168,10 +170,10 @@ lde_shutdown(void)
lde_nbr_free();
- msgbuf_clear(&ibuf_ldpe->w);
- free(ibuf_ldpe);
- msgbuf_clear(&ibuf_main->w);
- free(ibuf_main);
+ msgbuf_clear(&iev_ldpe->ibuf.w);
+ free(iev_ldpe);
+ msgbuf_clear(&iev_main->ibuf.w);
+ free(iev_main);
free(ldeconf);
log_info("label decision engine exiting");
@@ -182,14 +184,16 @@ int
lde_imsg_compose_ldpe(int type, u_int32_t peerid, pid_t pid, void *data,
u_int16_t datalen)
{
- return (imsg_compose(ibuf_ldpe, type, peerid, pid, data, datalen));
+ return (imsg_compose_event(iev_ldpe, type, peerid, pid,
+ -1, data, datalen));
}
/* ARGSUSED */
void
lde_dispatch_imsg(int fd, short event, void *bula)
{
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
struct lde_nbr rn, *nbr;
struct map map;
@@ -320,8 +324,8 @@ lde_dispatch_imsg(int fd, short event, void *bula)
case IMSG_CTL_SHOW_LIB:
rt_dump(imsg.hdr.pid);
- imsg_compose(ibuf_ldpe, IMSG_CTL_END, 0, imsg.hdr.pid,
- NULL, 0);
+ imsg_compose_event(iev_ldpe, IMSG_CTL_END, 0,
+ imsg.hdr.pid, -1, NULL, 0);
break;
default:
log_debug("lde_dispatch_imsg: unexpected imsg %d",
@@ -331,10 +335,10 @@ lde_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);
}
}
@@ -346,7 +350,8 @@ lde_dispatch_parent(int fd, short event, void *bula)
struct imsg imsg;
struct kroute kr;
struct rroute rr;
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
ssize_t n;
int shut = 0;
@@ -401,8 +406,9 @@ lde_dispatch_parent(int fd, short event, void *bula)
lde_send_change_kroute(rn);
else*/
/* should not happen */
- imsg_compose(ibuf_main, IMSG_KLABEL_DELETE, 0,
- 0, &kr, sizeof(kr));
+ imsg_compose_event(iev_main,
+ IMSG_KLABEL_DELETE, 0,
+ 0, -1, &kr, sizeof(kr));
break;
case IMSG_RECONF_CONF:
if ((nconf = malloc(sizeof(struct ldpd_conf))) ==
@@ -425,10 +431,10 @@ lde_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);
}
}
@@ -452,7 +458,8 @@ lde_send_insert_klabel(struct rt_node *r)
kr.prefixlen = r->prefixlen;
kr.ext_tag = r->ext_tag;
- imsg_compose(ibuf_main, IMSG_KLABEL_INSERT, 0, 0, &kr, sizeof(kr));
+ imsg_compose_event(iev_main, IMSG_KLABEL_INSERT, 0, 0, -1,
+ &kr, sizeof(kr));
}
void
@@ -468,7 +475,8 @@ lde_send_change_klabel(struct rt_node *r)
kr.prefixlen = r->prefixlen;
kr.ext_tag = r->ext_tag;
- imsg_compose(ibuf_main, IMSG_KLABEL_CHANGE, 0, 0, &kr, sizeof(kr));
+ imsg_compose_event(iev_main, IMSG_KLABEL_CHANGE, 0, 0, -1,
+ &kr, sizeof(kr));
}
void
@@ -480,28 +488,35 @@ lde_send_delete_klabel(struct rt_node *r)
kr.prefix.s_addr = r->prefix.s_addr;
kr.prefixlen = r->prefixlen;
- imsg_compose(ibuf_main, IMSG_KLABEL_DELETE, 0, 0, &kr, sizeof(kr));
+ imsg_compose_event(iev_main, IMSG_KLABEL_DELETE, 0, 0, -1,
+ &kr, sizeof(kr));
}
void
lde_send_labelrequest(u_int32_t peerid, struct map *map)
{
- imsg_compose(ibuf_ldpe, IMSG_REQUEST_ADD, peerid, 0, map, sizeof(map));
- imsg_compose(ibuf_ldpe, IMSG_REQUEST_ADD_END, peerid, 0, NULL, 0);
+ imsg_compose_event(iev_ldpe, IMSG_REQUEST_ADD, peerid, 0,
+ -1, map, sizeof(map));
+ imsg_compose_event(iev_ldpe, IMSG_REQUEST_ADD_END, peerid, 0,
+ -1, NULL, 0);
}
void
lde_send_labelmapping(u_int32_t peerid, struct map *map)
{
- imsg_compose(ibuf_ldpe, IMSG_MAPPING_ADD, peerid, 0, map, sizeof(map));
- imsg_compose(ibuf_ldpe, IMSG_MAPPING_ADD_END, peerid, 0, NULL, 0);
+ imsg_compose_event(iev_ldpe, IMSG_MAPPING_ADD, peerid, 0,
+ -1, map, sizeof(map));
+ imsg_compose_event(iev_ldpe, IMSG_MAPPING_ADD_END, peerid, 0,
+ -1, NULL, 0);
}
void
lde_send_labelrelease(u_int32_t peerid, struct map *map)
{
- imsg_compose(ibuf_ldpe, IMSG_RELEASE_ADD, peerid, 0, map, sizeof(map));
- imsg_compose(ibuf_ldpe, IMSG_RELEASE_ADD_END, peerid, 0, NULL, 0);
+ imsg_compose_event(iev_ldpe, IMSG_RELEASE_ADD, peerid, 0,
+ -1, map, sizeof(map));
+ imsg_compose_event(iev_ldpe, IMSG_RELEASE_ADD_END, peerid, 0,
+ -1, NULL, 0);
}
void
@@ -517,8 +532,8 @@ lde_send_notification(u_int32_t peerid, u_int32_t code, u_int32_t msgid,
nm.messageid = ntohl(msgid);
nm.type = type;
- imsg_compose(ibuf_ldpe, IMSG_NOTIFICATION_SEND, peerid, 0, &nm,
- sizeof(nm));
+ imsg_compose_event(iev_ldpe, IMSG_NOTIFICATION_SEND, peerid, 0,
+ -1, &nm, sizeof(nm));
}
LIST_HEAD(lde_nbr_head, lde_nbr);
diff --git a/usr.sbin/ldpd/ldpd.c b/usr.sbin/ldpd/ldpd.c
index 07624cfe084..82526538057 100644
--- a/usr.sbin/ldpd/ldpd.c
+++ b/usr.sbin/ldpd/ldpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldpd.c,v 1.1 2009/06/01 20:59:45 michele Exp $ */
+/* $OpenBSD: ldpd.c,v 1.2 2009/06/06 08:09:43 pyr Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -66,8 +66,8 @@ int pipe_parent2lde[2];
int pipe_ldpe2lde[2];
struct ldpd_conf *ldpd_conf = NULL;
-struct imsgbuf *ibuf_ldpe;
-struct imsgbuf *ibuf_lde;
+struct imsgev *iev_ldpe;
+struct imsgev *iev_lde;
char *conffile;
pid_t ldpe_pid = 0;
@@ -262,22 +262,24 @@ main(int argc, char *argv[])
close(pipe_ldpe2lde[0]);
close(pipe_ldpe2lde[1]);
- if ((ibuf_ldpe = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_lde = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_lde = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_ldpe, pipe_parent2ldpe[0], main_dispatch_ldpe);
- imsg_init(ibuf_lde, pipe_parent2lde[0], main_dispatch_lde);
+ imsg_init(&iev_ldpe->ibuf, pipe_parent2ldpe[0]);
+ iev_ldpe->handler = main_dispatch_ldpe;
+ imsg_init(&iev_lde->ibuf, pipe_parent2lde[0]);
+ iev_lde->handler = main_dispatch_lde;
/* setup event handler */
- ibuf_ldpe->events = EV_READ;
- event_set(&ibuf_ldpe->ev, ibuf_ldpe->fd, ibuf_ldpe->events,
- ibuf_ldpe->handler, ibuf_ldpe);
- event_add(&ibuf_ldpe->ev, NULL);
+ iev_ldpe->events = EV_READ;
+ event_set(&iev_ldpe->ev, iev_ldpe->ibuf.fd, iev_ldpe->events,
+ iev_ldpe->handler, iev_ldpe);
+ event_add(&iev_ldpe->ev, NULL);
- ibuf_lde->events = EV_READ;
- event_set(&ibuf_lde->ev, ibuf_lde->fd, ibuf_lde->events,
- ibuf_lde->handler, ibuf_lde);
- event_add(&ibuf_lde->ev, NULL);
+ iev_lde->events = EV_READ;
+ event_set(&iev_lde->ev, iev_lde->ibuf.fd, iev_lde->events,
+ iev_lde->handler, iev_lde);
+ event_add(&iev_lde->ev, NULL);
if (kr_init(!(ldpd_conf->flags & LDPD_FLAG_NO_LFIB_UPDATE)) == -1)
fatalx("kr_init failed");
@@ -313,10 +315,10 @@ ldpd_shutdown(void)
fatal("wait");
} while (pid != -1 || (pid == -1 && errno == EINTR));
- msgbuf_clear(&ibuf_ldpe->w);
- free(ibuf_ldpe);
- msgbuf_clear(&ibuf_lde->w);
- free(ibuf_lde);
+ msgbuf_clear(&iev_ldpe->ibuf.w);
+ free(iev_ldpe);
+ msgbuf_clear(&iev_lde->ibuf.w);
+ free(iev_lde);
free(ldpd_conf);
close(ldpd_conf->ldp_session_socket);
@@ -350,7 +352,8 @@ check_child(pid_t pid, const char *pname)
void
main_dispatch_ldpe(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;
@@ -408,10 +411,10 @@ main_dispatch_ldpe(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);
}
}
@@ -420,7 +423,8 @@ main_dispatch_ldpe(int fd, short event, void *bula)
void
main_dispatch_lde(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 count, shut = 0;
@@ -467,10 +471,10 @@ main_dispatch_lde(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);
}
}
@@ -478,26 +482,37 @@ main_dispatch_lde(int fd, short event, void *bula)
void
main_imsg_compose_ldpe(int type, pid_t pid, void *data, u_int16_t datalen)
{
- imsg_compose(ibuf_ldpe, type, 0, pid, data, datalen);
+ imsg_compose_event(iev_ldpe, type, 0, pid, -1, data, datalen);
}
void
main_imsg_compose_lde(int type, pid_t pid, void *data, u_int16_t datalen)
{
- imsg_compose(ibuf_lde, type, 0, pid, data, datalen);
+ imsg_compose_event(iev_lde, type, 0, pid, -1, data, datalen);
}
-/* this needs to be added here so that ldpctl 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);
}
/*
@@ -537,9 +552,9 @@ ldp_reload(void)
int
ldp_sendboth(enum imsg_type type, void *buf, u_int16_t len)
{
- if (imsg_compose(ibuf_ldpe, type, 0, 0, buf, len) == -1)
+ if (imsg_compose_event(iev_ldpe, type, 0, 0, -1, buf, len) == -1)
return (-1);
- if (imsg_compose(ibuf_lde, type, 0, 0, buf, len) == -1)
+ if (imsg_compose_event(iev_lde, type, 0, 0, -1, buf, len) == -1)
return (-1);
return (0);
}
diff --git a/usr.sbin/ldpd/ldpd.h b/usr.sbin/ldpd/ldpd.h
index 98a552ab399..052e86bc9b7 100644
--- a/usr.sbin/ldpd/ldpd.h
+++ b/usr.sbin/ldpd/ldpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldpd.h,v 1.2 2009/06/05 22:34:45 michele Exp $ */
+/* $OpenBSD: ldpd.h,v 1.3 2009/06/06 08:09:43 pyr Exp $ */
/*
* Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
@@ -30,6 +30,7 @@
#include <netinet/in.h>
#include <event.h>
+#include "imsg.h"
#include "ldp.h"
#define CONF_FILE "/etc/ldpd.conf"
@@ -41,7 +42,6 @@
#define NBR_IDSELF 1
#define NBR_CNTSTART (NBR_IDSELF + 1)
-#define READ_BUF_SIZE 65535
#define RT_BUF_SIZE 16384
#define MAX_RTSOCK_BUF 128 * 1024
#define LDP_BACKLOG 128
@@ -57,40 +57,12 @@
#define F_DYNAMIC 0x0040
#define F_REDISTRIBUTED 0x0100
-/* 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;
+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 {
@@ -133,18 +105,6 @@ enum imsg_type {
IMSG_RECONF_END
};
-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;
-};
-
/* interface states */
#define IF_STA_NEW 0x00 /* dummy state for reload */
#define IF_STA_DOWN 0x01
@@ -423,35 +383,10 @@ struct ctl_sum_lspace {
u_int32_t num_lsa;
};
-/* 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 *);
-
/* parse.y */
struct ldpd_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 */
-
/* in_cksum.c */
u_int16_t in_cksum(void *, size_t);
@@ -493,6 +428,10 @@ void rtlabel_tag(u_int16_t, u_int32_t);
void main_imsg_compose_ldpe(int, pid_t, void *, u_int16_t);
void main_imsg_compose_lde(int, pid_t, void *, u_int16_t);
void merge_config(struct ldpd_conf *, struct ldpd_conf *);
+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 *);
+
/* printconf.c */
void print_config(struct ldpd_conf *);
diff --git a/usr.sbin/ldpd/ldpe.c b/usr.sbin/ldpd/ldpe.c
index 63398c47cdb..400a6d75a68 100644
--- a/usr.sbin/ldpd/ldpe.c
+++ b/usr.sbin/ldpd/ldpe.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ldpe.c,v 1.2 2009/06/05 22:34:45 michele Exp $ */
+/* $OpenBSD: ldpe.c,v 1.3 2009/06/06 08:09:43 pyr Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -49,8 +49,8 @@ void ldpe_shutdown(void);
void recv_packet(int, short, void *);
struct ldpd_conf *leconf = NULL, *nconf;
-struct imsgbuf *ibuf_main;
-struct imsgbuf *ibuf_lde;
+struct imsgev *iev_main;
+struct imsgev *iev_lde;
int oe_nofib;
/* ARGSUSED */
@@ -179,22 +179,24 @@ ldpe(struct ldpd_conf *xconf, int pipe_parent2ldpe[2], int pipe_ldpe2lde[2],
close(pipe_parent2lde[0]);
close(pipe_parent2lde[1]);
- if ((ibuf_lde = malloc(sizeof(struct imsgbuf))) == NULL ||
- (ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
+ if ((iev_lde = malloc(sizeof(struct imsgev))) == NULL ||
+ (iev_main = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
- imsg_init(ibuf_lde, pipe_ldpe2lde[0], ldpe_dispatch_lde);
- imsg_init(ibuf_main, pipe_parent2ldpe[1], ldpe_dispatch_main);
+ imsg_init(&iev_lde->ibuf, pipe_ldpe2lde[0]);
+ iev_lde->handler = ldpe_dispatch_lde;
+ imsg_init(&iev_main->ibuf, pipe_parent2ldpe[1]);
+ iev_main->handler = ldpe_dispatch_main;
/* setup event handler */
- ibuf_lde->events = EV_READ;
- event_set(&ibuf_lde->ev, ibuf_lde->fd, ibuf_lde->events,
- ibuf_lde->handler, ibuf_lde);
- event_add(&ibuf_lde->ev, NULL);
+ iev_lde->events = EV_READ;
+ event_set(&iev_lde->ev, iev_lde->ibuf.fd, iev_lde->events,
+ iev_lde->handler, iev_lde);
+ event_add(&iev_lde->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(&leconf->disc_ev, leconf->ldp_discovery_socket,
EV_READ|EV_PERSIST, disc_recv_packet, leconf);
@@ -243,12 +245,12 @@ ldpe_shutdown(void)
close(leconf->ldp_discovery_socket);
/* clean up */
- msgbuf_write(&ibuf_lde->w);
- msgbuf_clear(&ibuf_lde->w);
- free(ibuf_lde);
- msgbuf_write(&ibuf_main->w);
- msgbuf_clear(&ibuf_main->w);
- free(ibuf_main);
+ msgbuf_write(&iev_lde->ibuf.w);
+ msgbuf_clear(&iev_lde->ibuf.w);
+ free(iev_lde);
+ msgbuf_write(&iev_main->ibuf.w);
+ msgbuf_clear(&iev_main->ibuf.w);
+ free(iev_main);
free(leconf);
free(pkt_ptr);
@@ -260,14 +262,15 @@ ldpe_shutdown(void)
int
ldpe_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
ldpe_imsg_compose_lde(int type, u_int32_t peerid, pid_t pid,
void *data, u_int16_t datalen)
{
- return (imsg_compose(ibuf_lde, type, peerid, pid, data, datalen));
+ return (imsg_compose_event(iev_lde, type, peerid, pid, -1,
+ data, datalen));
}
/* ARGSUSED */
@@ -275,7 +278,8 @@ void
ldpe_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 iface *iface = NULL;
struct kif *kif;
int n, link_ok, shut = 0;
@@ -351,10 +355,10 @@ ldpe_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);
}
}
@@ -363,7 +367,8 @@ ldpe_dispatch_main(int fd, short event, void *bula)
void
ldpe_dispatch_lde(int fd, short event, void *bula)
{
- struct imsgbuf *ibuf = bula;
+ struct imsgev *iev = bula;
+ struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
struct map map;
struct notify_msg nm;
@@ -485,10 +490,10 @@ ldpe_dispatch_lde(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);
}
}
@@ -517,8 +522,9 @@ ldpe_iface_ctl(struct ctl_conn *c, unsigned int idx)
LIST_FOREACH(iface, &leconf->iface_list, entry) {
if (idx == 0 || idx == iface->ifindex) {
ictl = if_to_ctl(iface);
- imsg_compose(&c->ibuf, IMSG_CTL_SHOW_INTERFACE,
- 0, 0, ictl, sizeof(struct ctl_iface));
+ imsg_compose_event(&c->iev,
+ IMSG_CTL_SHOW_INTERFACE,
+ 0, 0, -1, ictl, sizeof(struct ctl_iface));
}
}
}
@@ -534,12 +540,12 @@ ldpe_nbr_ctl(struct ctl_conn *c)
LIST_FOREACH(nbr, &iface->nbr_list, entry) {
if (iface->self != nbr) {
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);
}