diff options
author | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2011-05-02 22:32:30 +0000 |
---|---|---|
committer | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2011-05-02 22:32:30 +0000 |
commit | b390a0870d68a4915f148287ebbfd493d445a155 (patch) | |
tree | f03783b47c80ec49f54b771b3851ac16615b5947 /lib | |
parent | eec210b2e4d847c224112c6cd053f26f9f3139da (diff) |
Add missing byter order conversions in message headers. Fixes the case
when the server and the client are not of the same endianness.
Found by naddy.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libsndio/aucat.c | 24 | ||||
-rw-r--r-- | lib/libsndio/sio_aucat.c | 64 |
2 files changed, 47 insertions, 41 deletions
diff --git a/lib/libsndio/aucat.c b/lib/libsndio/aucat.c index ba8016b43a1..d28ad40e795 100644 --- a/lib/libsndio/aucat.c +++ b/lib/libsndio/aucat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aucat.c,v 1.47 2011/05/02 22:24:23 ratchov Exp $ */ +/* $OpenBSD: aucat.c,v 1.48 2011/05/02 22:32:29 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -69,8 +69,8 @@ aucat_rmsg(struct aucat *hdl, int *eof) } hdl->rtodo -= n; } - if (hdl->rmsg.cmd == AMSG_DATA) { - hdl->rtodo = hdl->rmsg.u.data.size; + if (ntohl(hdl->rmsg.cmd) == AMSG_DATA) { + hdl->rtodo = ntohl(hdl->rmsg.u.data.size); hdl->rstate = RSTATE_DATA; } else { hdl->rtodo = sizeof(struct amsg); @@ -109,8 +109,8 @@ aucat_wmsg(struct aucat *hdl, int *eof) } hdl->wtodo -= n; } - if (hdl->wmsg.cmd == AMSG_DATA) { - hdl->wtodo = hdl->wmsg.u.data.size; + if (ntohl(hdl->wmsg.cmd) == AMSG_DATA) { + hdl->wtodo = ntohl(hdl->wmsg.u.data.size); hdl->wstate = WSTATE_DATA; } else { hdl->wtodo = 0xdeadbeef; @@ -165,8 +165,8 @@ aucat_wdata(struct aucat *hdl, const void *buf, size_t len, unsigned wbpf, int * len -= len % wbpf; if (len == 0) len = wbpf; - hdl->wmsg.cmd = AMSG_DATA; - hdl->wmsg.u.data.size = len; + hdl->wmsg.cmd = htonl(AMSG_DATA); + hdl->wmsg.u.data.size = htonl(len); hdl->wtodo = sizeof(struct amsg); hdl->wstate = WSTATE_MSG; /* FALLTHROUGH */ @@ -423,16 +423,16 @@ aucat_open(struct aucat *hdl, const char *str, unsigned mode, int isaudio) * say hello to server */ AMSG_INIT(&hdl->wmsg); - hdl->wmsg.cmd = AMSG_AUTH; + hdl->wmsg.cmd = htonl(AMSG_AUTH); if (!aucat_mkcookie(hdl->wmsg.u.auth.cookie)) goto bad_connect; hdl->wtodo = sizeof(struct amsg); if (!aucat_wmsg(hdl, &eof)) goto bad_connect; AMSG_INIT(&hdl->wmsg); - hdl->wmsg.cmd = AMSG_HELLO; + hdl->wmsg.cmd = htonl(AMSG_HELLO); hdl->wmsg.u.hello.version = AMSG_VERSION; - hdl->wmsg.u.hello.mode = mode; + hdl->wmsg.u.hello.mode = htons(mode); strlcpy(hdl->wmsg.u.hello.who, __progname, sizeof(hdl->wmsg.u.hello.who)); strlcpy(hdl->wmsg.u.hello.opt, opt, @@ -445,7 +445,7 @@ aucat_open(struct aucat *hdl, const char *str, unsigned mode, int isaudio) DPRINTF("aucat_init: mode refused\n"); goto bad_connect; } - if (hdl->rmsg.cmd != AMSG_ACK) { + if (ntohl(hdl->rmsg.cmd) != AMSG_ACK) { DPRINTF("aucat_init: protocol err\n"); goto bad_connect; } @@ -463,7 +463,7 @@ aucat_close(struct aucat *hdl, int eof) if (!eof) { AMSG_INIT(&hdl->wmsg); - hdl->wmsg.cmd = AMSG_BYE; + hdl->wmsg.cmd = htonl(AMSG_BYE); hdl->wtodo = sizeof(struct amsg); if (!aucat_wmsg(hdl, &eof)) goto bad_close; diff --git a/lib/libsndio/sio_aucat.c b/lib/libsndio/sio_aucat.c index abdce5efb64..8186298d967 100644 --- a/lib/libsndio/sio_aucat.c +++ b/lib/libsndio/sio_aucat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sio_aucat.c,v 1.5 2011/04/18 23:57:35 ratchov Exp $ */ +/* $OpenBSD: sio_aucat.c,v 1.6 2011/05/02 22:32:29 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -80,36 +80,42 @@ static struct sio_ops sio_aucat_ops = { static int sio_aucat_runmsg(struct sio_aucat_hdl *hdl) { + int delta; + unsigned size, ctl; + if (!aucat_rmsg(&hdl->aucat, &hdl->sio.eof)) return 0; - switch (hdl->aucat.rmsg.cmd) { + switch (ntohl(hdl->aucat.rmsg.cmd)) { case AMSG_DATA: - if (hdl->aucat.rmsg.u.data.size == 0 || - hdl->aucat.rmsg.u.data.size % hdl->rbpf) { + size = ntohl(hdl->aucat.rmsg.u.data.size); + if (size == 0 || size % hdl->rbpf) { DPRINTF("sio_aucat_runmsg: bad data message\n"); hdl->sio.eof = 1; return 0; } return 1; case AMSG_POS: - hdl->maxwrite += hdl->aucat.rmsg.u.ts.delta * (int)hdl->wbpf; + delta = ntohl(hdl->aucat.rmsg.u.ts.delta); + hdl->maxwrite += delta * (int)hdl->wbpf; DPRINTF("aucat: pos = %d, maxwrite = %d\n", - hdl->aucat.rmsg.u.ts.delta, hdl->maxwrite); - hdl->delta = hdl->aucat.rmsg.u.ts.delta; + delta, hdl->maxwrite); + hdl->delta = delta; break; case AMSG_MOVE: - hdl->maxwrite += hdl->aucat.rmsg.u.ts.delta * hdl->wbpf; - hdl->delta += hdl->aucat.rmsg.u.ts.delta; + delta = ntohl(hdl->aucat.rmsg.u.ts.delta); + hdl->maxwrite += delta * hdl->wbpf; + hdl->delta += delta; DPRINTF("aucat: move = %d, delta = %d, maxwrite = %d\n", - hdl->aucat.rmsg.u.ts.delta, hdl->delta, hdl->maxwrite); + delta, hdl->delta, hdl->maxwrite); if (hdl->delta >= 0) { sio_onmove_cb(&hdl->sio, hdl->delta); hdl->delta = 0; } break; case AMSG_SETVOL: - hdl->curvol = hdl->reqvol = hdl->aucat.rmsg.u.vol.ctl; - sio_onvol_cb(&hdl->sio, hdl->curvol); + ctl = ntohl(hdl->aucat.rmsg.u.vol.ctl); + hdl->curvol = hdl->reqvol = ctl; + sio_onvol_cb(&hdl->sio, ctl); break; case AMSG_STOP: hdl->pstate = PSTATE_INIT; @@ -130,8 +136,8 @@ sio_aucat_buildmsg(struct sio_aucat_hdl *hdl) if (hdl->curvol != hdl->reqvol) { hdl->aucat.wstate = WSTATE_MSG; hdl->aucat.wtodo = sizeof(struct amsg); - hdl->aucat.wmsg.cmd = AMSG_SETVOL; - hdl->aucat.wmsg.u.vol.ctl = hdl->reqvol; + hdl->aucat.wmsg.cmd = htonl(AMSG_SETVOL); + hdl->aucat.wmsg.u.vol.ctl = htonl(hdl->reqvol); hdl->curvol = hdl->reqvol; return aucat_wmsg(&hdl->aucat, &hdl->sio.eof); } @@ -186,7 +192,7 @@ sio_aucat_start(struct sio_hdl *sh) DPRINTF("aucat: start, maxwrite = %d\n", hdl->maxwrite); AMSG_INIT(&hdl->aucat.wmsg); - hdl->aucat.wmsg.cmd = AMSG_START; + hdl->aucat.wmsg.cmd = htonl(AMSG_START); hdl->aucat.wtodo = sizeof(struct amsg); if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof)) return 0; @@ -231,7 +237,7 @@ sio_aucat_stop(struct sio_hdl *sh) * send stop message */ AMSG_INIT(&hdl->aucat.wmsg); - hdl->aucat.wmsg.cmd = AMSG_STOP; + hdl->aucat.wmsg.cmd = htonl(AMSG_STOP); hdl->aucat.wtodo = sizeof(struct amsg); if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof)) return 0; @@ -260,19 +266,19 @@ sio_aucat_setpar(struct sio_hdl *sh, struct sio_par *par) struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh; AMSG_INIT(&hdl->aucat.wmsg); - hdl->aucat.wmsg.cmd = AMSG_SETPAR; + hdl->aucat.wmsg.cmd = htonl(AMSG_SETPAR); hdl->aucat.wmsg.u.par.bits = par->bits; hdl->aucat.wmsg.u.par.bps = par->bps; hdl->aucat.wmsg.u.par.sig = par->sig; hdl->aucat.wmsg.u.par.le = par->le; hdl->aucat.wmsg.u.par.msb = par->msb; - hdl->aucat.wmsg.u.par.rate = par->rate; - hdl->aucat.wmsg.u.par.appbufsz = par->appbufsz; + hdl->aucat.wmsg.u.par.rate = htonl(par->rate); + hdl->aucat.wmsg.u.par.appbufsz = htonl(par->appbufsz); hdl->aucat.wmsg.u.par.xrun = par->xrun; if (hdl->sio.mode & SIO_REC) - hdl->aucat.wmsg.u.par.rchan = par->rchan; + hdl->aucat.wmsg.u.par.rchan = htons(par->rchan); if (hdl->sio.mode & SIO_PLAY) - hdl->aucat.wmsg.u.par.pchan = par->pchan; + hdl->aucat.wmsg.u.par.pchan = htons(par->pchan); hdl->aucat.wtodo = sizeof(struct amsg); if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof)) return 0; @@ -285,14 +291,14 @@ sio_aucat_getpar(struct sio_hdl *sh, struct sio_par *par) struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh; AMSG_INIT(&hdl->aucat.wmsg); - hdl->aucat.wmsg.cmd = AMSG_GETPAR; + hdl->aucat.wmsg.cmd = htonl(AMSG_GETPAR); hdl->aucat.wtodo = sizeof(struct amsg); if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof)) return 0; hdl->aucat.rtodo = sizeof(struct amsg); if (!aucat_rmsg(&hdl->aucat, &hdl->sio.eof)) return 0; - if (hdl->aucat.rmsg.cmd != AMSG_GETPAR) { + if (ntohl(hdl->aucat.rmsg.cmd) != AMSG_GETPAR) { DPRINTF("sio_aucat_getpar: protocol err\n"); hdl->sio.eof = 1; return 0; @@ -302,15 +308,15 @@ sio_aucat_getpar(struct sio_hdl *sh, struct sio_par *par) par->sig = hdl->aucat.rmsg.u.par.sig; par->le = hdl->aucat.rmsg.u.par.le; par->msb = hdl->aucat.rmsg.u.par.msb; - par->rate = hdl->aucat.rmsg.u.par.rate; - par->bufsz = hdl->aucat.rmsg.u.par.bufsz; - par->appbufsz = hdl->aucat.rmsg.u.par.appbufsz; + par->rate = ntohl(hdl->aucat.rmsg.u.par.rate); + par->bufsz = ntohl(hdl->aucat.rmsg.u.par.bufsz); + par->appbufsz = ntohl(hdl->aucat.rmsg.u.par.appbufsz); par->xrun = hdl->aucat.rmsg.u.par.xrun; - par->round = hdl->aucat.rmsg.u.par.round; + par->round = ntohl(hdl->aucat.rmsg.u.par.round); if (hdl->sio.mode & SIO_PLAY) - par->pchan = hdl->aucat.rmsg.u.par.pchan; + par->pchan = ntohs(hdl->aucat.rmsg.u.par.pchan); if (hdl->sio.mode & SIO_REC) - par->rchan = hdl->aucat.rmsg.u.par.rchan; + par->rchan = ntohs(hdl->aucat.rmsg.u.par.rchan); return 1; } |