diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2004-09-15 19:21:26 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2004-09-15 19:21:26 +0000 |
commit | 078fd8953f1ccce51c0026d3be83e90002feeb5c (patch) | |
tree | 6b611c60bb46d4bd21902e3ebd7ccce87d0486b8 | |
parent | cd4ff5e43c31c11a0c2f117b61c9560e773ac5d1 (diff) |
imsg framework cleanup:
-kill the _pid flavors of imsg_create and imsg_compose, and just add pid as
argument to those
-use imsg_create in imsg_compose instead of duplicating code
-check for datalen overflow
-rw-r--r-- | usr.sbin/ntpd/imsg.c | 70 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntp.c | 6 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.c | 4 | ||||
-rw-r--r-- | usr.sbin/ntpd/ntpd.h | 10 |
4 files changed, 26 insertions, 64 deletions
diff --git a/usr.sbin/ntpd/imsg.c b/usr.sbin/ntpd/imsg.c index 8dfaa893dc4..ef3fd58e906 100644 --- a/usr.sbin/ntpd/imsg.c +++ b/usr.sbin/ntpd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.1 2004/05/31 13:46:16 henning Exp $ */ +/* $OpenBSD: imsg.c,v 1.2 2004/09/15 19:21:25 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -25,11 +25,6 @@ #include "ntpd.h" -int imsg_compose_core(struct imsgbuf *, int, u_int32_t, void *, - u_int16_t, pid_t); -struct buf *imsg_create_core(struct imsgbuf *, int, u_int32_t, u_int16_t, - pid_t); - void imsg_init(struct imsgbuf *ibuf, int fd) { @@ -97,47 +92,19 @@ imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) } int -imsg_compose(struct imsgbuf *ibuf, int type, u_int32_t peerid, void *data, - u_int16_t dlen) -{ - return (imsg_compose_core(ibuf, type, peerid, data, dlen, ibuf->pid)); -} - -int -imsg_compose_pid(struct imsgbuf *ibuf, int type, pid_t pid, void *data, - u_int16_t datalen) -{ - return (imsg_compose_core(ibuf, type, 0, data, datalen, pid)); -} - -int -imsg_compose_core(struct imsgbuf *ibuf, int type, u_int32_t peerid, void *data, - u_int16_t datalen, pid_t pid) +imsg_compose(struct imsgbuf *ibuf, int type, u_int32_t peerid, pid_t pid, + void *data, u_int16_t datalen) { struct buf *wbuf; - struct imsg_hdr hdr; int n; - hdr.len = datalen + IMSG_HEADER_SIZE; - hdr.type = type; - hdr.peerid = peerid; - hdr.pid = pid; - wbuf = buf_open(hdr.len); - if (wbuf == NULL) { - log_warn("imsg_compose: buf_open"); + if ((wbuf = imsg_create(ibuf, type, peerid, datalen, pid)) == NULL) return (-1); - } - if (buf_add(wbuf, &hdr, sizeof(hdr)) == -1) { - log_warnx("imsg_compose: buf_add error"); - buf_free(wbuf); + + if (imsg_add(wbuf, data, datalen) == -1) { + free(wbuf); return (-1); } - if (datalen) - if (buf_add(wbuf, data, datalen) == -1) { - log_warnx("imsg_compose: buf_add error"); - buf_free(wbuf); - return (-1); - } if ((n = buf_close(&ibuf->w, wbuf)) < 0) { log_warnx("imsg_compose: buf_add error"); @@ -148,24 +115,19 @@ imsg_compose_core(struct imsgbuf *ibuf, int type, u_int32_t peerid, void *data, } struct buf * -imsg_create(struct imsgbuf *ibuf, int type, u_int32_t peerid, u_int16_t dlen) -{ - return (imsg_create_core(ibuf, type, peerid, dlen, ibuf->pid)); -} - -struct buf * -imsg_create_pid(struct imsgbuf *ibuf, int type, pid_t pid, u_int16_t datalen) -{ - return (imsg_create_core(ibuf, type, 0, datalen, pid)); -} - -struct buf * -imsg_create_core(struct imsgbuf *ibuf, int type, u_int32_t peerid, - u_int16_t datalen, pid_t pid) +imsg_create(struct imsgbuf *ibuf, int 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); + return (NULL); + } + hdr.len = datalen + IMSG_HEADER_SIZE; hdr.type = type; hdr.peerid = peerid; diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c index 27318e09828..47828af9155 100644 --- a/usr.sbin/ntpd/ntp.c +++ b/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.30 2004/09/15 19:14:11 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.31 2004/09/15 19:21:25 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -354,7 +354,7 @@ ntp_adjtime(void) if (offset_cnt > 0) { offset_median /= offset_cnt; - imsg_compose(ibuf_main, IMSG_ADJTIME, 0, + imsg_compose(ibuf_main, IMSG_ADJTIME, 0, 0, &offset_median, sizeof(offset_median)); conf->status.reftime = gettime(); @@ -371,5 +371,5 @@ ntp_host_dns(char *name, u_int32_t peerid) u_int16_t dlen; dlen = strlen(name) + 1; - imsg_compose(ibuf_main, IMSG_HOST_DNS, peerid, name, dlen); + imsg_compose(ibuf_main, IMSG_HOST_DNS, peerid, 0, name, dlen); } diff --git a/usr.sbin/ntpd/ntpd.c b/usr.sbin/ntpd/ntpd.c index deb51799c1d..c69c19c5b9d 100644 --- a/usr.sbin/ntpd/ntpd.c +++ b/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.16 2004/09/15 19:14:11 henning Exp $ */ +/* $OpenBSD: ntpd.c,v 1.17 2004/09/15 19:21:25 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -250,7 +250,7 @@ dispatch_imsg(void) fatal("invalid IMSG_HOST_DNS received"); if ((cnt = host_dns(name, &hn)) > 0) { buf = imsg_create(ibuf, IMSG_HOST_DNS, - imsg.hdr.peerid, + imsg.hdr.peerid, 0, cnt * sizeof(struct sockaddr_storage)); if (buf == NULL) break; diff --git a/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h index a7192506c32..1d0c1577807 100644 --- a/usr.sbin/ntpd/ntpd.h +++ b/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.34 2004/09/15 00:18:12 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.35 2004/09/15 19:21:25 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -191,10 +191,10 @@ int msgbuf_write(struct msgbuf *); void imsg_init(struct imsgbuf *, int); int imsg_read(struct imsgbuf *); int imsg_get(struct imsgbuf *, struct imsg *); -int imsg_compose(struct imsgbuf *, int, u_int32_t, void *, u_int16_t); -int imsg_compose_pid(struct imsgbuf *, int, pid_t, void *, u_int16_t); -struct buf *imsg_create(struct imsgbuf *, int, u_int32_t, u_int16_t); -struct buf *imsg_create_pid(struct imsgbuf *, int, pid_t, u_int16_t); +int imsg_compose(struct imsgbuf *, int, u_int32_t, pid_t, void *, + u_int16_t); +struct buf *imsg_create(struct imsgbuf *, int, 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 *); |