msg.c revision 124208
13417Scsgr/* 23784Sphk * Copyright (c) 2002 Markus Friedl. All rights reserved. 33417Scsgr * 43417Scsgr * Redistribution and use in source and binary forms, with or without 593149Sphk * modification, are permitted provided that the following conditions 63417Scsgr * are met: 73417Scsgr * 1. Redistributions of source code must retain the above copyright 83417Scsgr * notice, this list of conditions and the following disclaimer. 93417Scsgr * 2. Redistributions in binary form must reproduce the above copyright 103417Scsgr * notice, this list of conditions and the following disclaimer in the 11116182Sobrien * documentation and/or other materials provided with the distribution. 12116182Sobrien * 13116182Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 143417Scsgr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 153784Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1655206Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 173417Scsgr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1841057Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 197840Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 203417Scsgr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 213417Scsgr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2255206Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2330354Sphk */ 2441057Speter#include "includes.h" 2530309SphkRCSID("$OpenBSD: msg.c,v 1.6 2003/06/28 16:23:06 deraadt Exp $"); 263784Sphk 273784Sphk#include "buffer.h" 283784Sphk#include "getput.h" 293784Sphk#include "log.h" 303784Sphk#include "atomicio.h" 313417Scsgr#include "msg.h" 3255206Speter 333784Sphkvoid 347840Sphkssh_msg_send(int fd, u_char type, Buffer *m) 353784Sphk{ 3655206Speter u_char buf[5]; 373417Scsgr u_int mlen = buffer_len(m); 387840Sphk 397840Sphk debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff); 407840Sphk 417840Sphk PUT_32BIT(buf, mlen + 1); 423417Scsgr buf[4] = type; /* 1st byte of payload is mesg-type */ 433784Sphk if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) 443784Sphk fatal("ssh_msg_send: write"); 453784Sphk if (atomicio(vwrite, fd, buffer_ptr(m), mlen) != mlen) 463784Sphk fatal("ssh_msg_send: write"); 473417Scsgr} 483417Scsgr 493417Scsgrint 503417Scsgrssh_msg_recv(int fd, Buffer *m) 5155206Speter{ 5241057Speter u_char buf[4]; 5341057Speter ssize_t res; 5441057Speter u_int msg_len; 5541057Speter 567840Sphk debug3("ssh_msg_recv entering"); 577840Sphk 583784Sphk res = atomicio(read, fd, buf, sizeof(buf)); 593784Sphk if (res != sizeof(buf)) { 603784Sphk if (res == 0) 613784Sphk return -1; 623417Scsgr fatal("ssh_msg_recv: read: header %ld", (long)res); 633417Scsgr } 643417Scsgr msg_len = GET_32BIT(buf); 653417Scsgr if (msg_len > 256 * 1024) 663417Scsgr fatal("ssh_msg_recv: read: bad msg_len %u", msg_len); 673417Scsgr buffer_clear(m); 683417Scsgr buffer_append_space(m, msg_len); 693417Scsgr res = atomicio(read, fd, buffer_ptr(m), msg_len); 703417Scsgr if (res != msg_len) 713417Scsgr fatal("ssh_msg_recv: read: %ld != msg_len", (long)res); 723417Scsgr return 0; 733417Scsgr} 743417Scsgr