1*190cef3dSDag-Erling Smørgrav /* $OpenBSD: msg.c,v 1.17 2018/07/09 21:59:10 markus Exp $ */ 2545d5ecaSDag-Erling Smørgrav /* 3545d5ecaSDag-Erling Smørgrav * Copyright (c) 2002 Markus Friedl. All rights reserved. 4545d5ecaSDag-Erling Smørgrav * 5545d5ecaSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 6545d5ecaSDag-Erling Smørgrav * modification, are permitted provided that the following conditions 7545d5ecaSDag-Erling Smørgrav * are met: 8545d5ecaSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 9545d5ecaSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 10545d5ecaSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 11545d5ecaSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 12545d5ecaSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 13545d5ecaSDag-Erling Smørgrav * 14545d5ecaSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15545d5ecaSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16545d5ecaSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17545d5ecaSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18545d5ecaSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19545d5ecaSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20545d5ecaSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21545d5ecaSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22545d5ecaSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23545d5ecaSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24545d5ecaSDag-Erling Smørgrav */ 25761efaa7SDag-Erling Smørgrav 26545d5ecaSDag-Erling Smørgrav #include "includes.h" 27761efaa7SDag-Erling Smørgrav 28761efaa7SDag-Erling Smørgrav #include <sys/types.h> 29761efaa7SDag-Erling Smørgrav #include <sys/uio.h> 30761efaa7SDag-Erling Smørgrav 31761efaa7SDag-Erling Smørgrav #include <errno.h> 32761efaa7SDag-Erling Smørgrav #include <stdio.h> 33761efaa7SDag-Erling Smørgrav #include <string.h> 34761efaa7SDag-Erling Smørgrav #include <unistd.h> 35761efaa7SDag-Erling Smørgrav #include <stdarg.h> 36545d5ecaSDag-Erling Smørgrav 37bc5531deSDag-Erling Smørgrav #include "sshbuf.h" 38bc5531deSDag-Erling Smørgrav #include "ssherr.h" 39545d5ecaSDag-Erling Smørgrav #include "log.h" 40545d5ecaSDag-Erling Smørgrav #include "atomicio.h" 41545d5ecaSDag-Erling Smørgrav #include "msg.h" 42761efaa7SDag-Erling Smørgrav #include "misc.h" 43545d5ecaSDag-Erling Smørgrav 44efcad6b7SDag-Erling Smørgrav int 45bc5531deSDag-Erling Smørgrav ssh_msg_send(int fd, u_char type, struct sshbuf *m) 46545d5ecaSDag-Erling Smørgrav { 47545d5ecaSDag-Erling Smørgrav u_char buf[5]; 48bc5531deSDag-Erling Smørgrav u_int mlen = sshbuf_len(m); 49545d5ecaSDag-Erling Smørgrav 504b17dab0SDag-Erling Smørgrav debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff); 51545d5ecaSDag-Erling Smørgrav 52761efaa7SDag-Erling Smørgrav put_u32(buf, mlen + 1); 53545d5ecaSDag-Erling Smørgrav buf[4] = type; /* 1st byte of payload is mesg-type */ 54efcad6b7SDag-Erling Smørgrav if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) { 55efcad6b7SDag-Erling Smørgrav error("ssh_msg_send: write"); 56efcad6b7SDag-Erling Smørgrav return (-1); 57efcad6b7SDag-Erling Smørgrav } 58*190cef3dSDag-Erling Smørgrav if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) { 59efcad6b7SDag-Erling Smørgrav error("ssh_msg_send: write"); 60efcad6b7SDag-Erling Smørgrav return (-1); 61efcad6b7SDag-Erling Smørgrav } 62efcad6b7SDag-Erling Smørgrav return (0); 63545d5ecaSDag-Erling Smørgrav } 64545d5ecaSDag-Erling Smørgrav 65545d5ecaSDag-Erling Smørgrav int 66bc5531deSDag-Erling Smørgrav ssh_msg_recv(int fd, struct sshbuf *m) 67545d5ecaSDag-Erling Smørgrav { 68bc5531deSDag-Erling Smørgrav u_char buf[4], *p; 69545d5ecaSDag-Erling Smørgrav u_int msg_len; 70bc5531deSDag-Erling Smørgrav int r; 71545d5ecaSDag-Erling Smørgrav 724b17dab0SDag-Erling Smørgrav debug3("ssh_msg_recv entering"); 73545d5ecaSDag-Erling Smørgrav 74043840dfSDag-Erling Smørgrav if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) { 75043840dfSDag-Erling Smørgrav if (errno != EPIPE) 76043840dfSDag-Erling Smørgrav error("ssh_msg_recv: read: header"); 77efcad6b7SDag-Erling Smørgrav return (-1); 78545d5ecaSDag-Erling Smørgrav } 79761efaa7SDag-Erling Smørgrav msg_len = get_u32(buf); 80efcad6b7SDag-Erling Smørgrav if (msg_len > 256 * 1024) { 81efcad6b7SDag-Erling Smørgrav error("ssh_msg_recv: read: bad msg_len %u", msg_len); 82efcad6b7SDag-Erling Smørgrav return (-1); 83efcad6b7SDag-Erling Smørgrav } 84bc5531deSDag-Erling Smørgrav sshbuf_reset(m); 85bc5531deSDag-Erling Smørgrav if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) { 86bc5531deSDag-Erling Smørgrav error("%s: buffer error: %s", __func__, ssh_err(r)); 87bc5531deSDag-Erling Smørgrav return -1; 88bc5531deSDag-Erling Smørgrav } 89bc5531deSDag-Erling Smørgrav if (atomicio(read, fd, p, msg_len) != msg_len) { 90043840dfSDag-Erling Smørgrav error("ssh_msg_recv: read: %s", strerror(errno)); 91efcad6b7SDag-Erling Smørgrav return (-1); 92efcad6b7SDag-Erling Smørgrav } 93efcad6b7SDag-Erling Smørgrav return (0); 94545d5ecaSDag-Erling Smørgrav } 95