Browse Source

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
OPENBSD_3_7
henning 20 years ago
parent
commit
7b3f946188
4 changed files with 26 additions and 64 deletions
  1. +16
    -54
      src/usr.sbin/ntpd/imsg.c
  2. +3
    -3
      src/usr.sbin/ntpd/ntp.c
  3. +2
    -2
      src/usr.sbin/ntpd/ntpd.c
  4. +5
    -5
      src/usr.sbin/ntpd/ntpd.h

+ 16
- 54
src/usr.sbin/ntpd/imsg.c View File

@ -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;


+ 3
- 3
src/usr.sbin/ntpd/ntp.c View File

@ -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);
}

+ 2
- 2
src/usr.sbin/ntpd/ntpd.c View File

@ -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;


+ 5
- 5
src/usr.sbin/ntpd/ntpd.h View File

@ -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 *);


Loading…
Cancel
Save