X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/8afe456df0b0ccd989631f6f6e35f61e9abedea0..f8f89bae5e11c9c8e1895f0b62b0bdf80a5539fc:/msg.c diff --git a/msg.c b/msg.c index a159aae8..cd5f98c4 100644 --- a/msg.c +++ b/msg.c @@ -1,3 +1,4 @@ +/* $OpenBSD: msg.c,v 1.15 2006/08/03 03:34:42 deraadt Exp $ */ /* * Copyright (c) 2002 Markus Friedl. All rights reserved. * @@ -21,53 +22,68 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + #include "includes.h" -RCSID("$OpenBSD: msg.c,v 1.1 2002/05/23 19:24:30 markus Exp $"); + +#include +#include + +#include +#include +#include +#include +#include #include "buffer.h" -#include "getput.h" #include "log.h" #include "atomicio.h" #include "msg.h" +#include "misc.h" -void -msg_send(int fd, u_char type, Buffer *m) +int +ssh_msg_send(int fd, u_char type, Buffer *m) { u_char buf[5]; u_int mlen = buffer_len(m); - debug3("msg_send: type %d", type); + debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff); - PUT_32BIT(buf, mlen + 1); - buf[4] = type; /* 1st byte of payload is mesg-type */ - if (atomicio(write, fd, buf, sizeof(buf)) != sizeof(buf)) - fatal("msg_send: write"); - if (atomicio(write, fd, buffer_ptr(m), mlen) != mlen) - fatal("msg_send: write"); + put_u32(buf, mlen + 1); + buf[4] = type; /* 1st byte of payload is mesg-type */ + if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) { + error("ssh_msg_send: write"); + return (-1); + } + if (atomicio(vwrite, fd, buffer_ptr(m), mlen) != mlen) { + error("ssh_msg_send: write"); + return (-1); + } + return (0); } int -msg_recv(int fd, Buffer *m) +ssh_msg_recv(int fd, Buffer *m) { u_char buf[4]; - ssize_t res; u_int msg_len; - debug3("msg_recv entering"); + debug3("ssh_msg_recv entering"); - res = atomicio(read, fd, buf, sizeof(buf)); - if (res != sizeof(buf)) { - if (res == 0) - return -1; - fatal("msg_recv: read: header %d", res); + if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) { + if (errno != EPIPE) + error("ssh_msg_recv: read: header"); + return (-1); + } + msg_len = get_u32(buf); + if (msg_len > 256 * 1024) { + error("ssh_msg_recv: read: bad msg_len %u", msg_len); + return (-1); } - msg_len = GET_32BIT(buf); - if (msg_len > 256 * 1024) - fatal("msg_recv: read: bad msg_len %d", msg_len); buffer_clear(m); buffer_append_space(m, msg_len); - res = atomicio(read, fd, buffer_ptr(m), msg_len); - if (res != msg_len) - fatal("msg_recv: read: %ld != msg_len", (long)res); - return 0; + if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) { + error("ssh_msg_recv: read: %s", strerror(errno)); + return (-1); + } + return (0); }