msg.c revision 272461
1139825Simp/* $OpenBSD: msg.c,v 1.15 2006/08/03 03:34:42 deraadt Exp $ */ 2139740Sphk/* 350974Swpaul * Copyright (c) 2002 Markus Friedl. All rights reserved. 450974Swpaul * 550974Swpaul * Redistribution and use in source and binary forms, with or without 650974Swpaul * modification, are permitted provided that the following conditions 750974Swpaul * are met: 850974Swpaul * 1. Redistributions of source code must retain the above copyright 950974Swpaul * notice, this list of conditions and the following disclaimer. 1050974Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1150974Swpaul * notice, this list of conditions and the following disclaimer in the 1250974Swpaul * documentation and/or other materials provided with the distribution. 1350974Swpaul * 1450974Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1550974Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1650974Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1750974Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1850974Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1950974Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2050974Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2150974Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2250974Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2350974Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2450974Swpaul */ 2550974Swpaul 2650974Swpaul#include "includes.h" 2750974Swpaul 2850974Swpaul#include <sys/types.h> 2950974Swpaul#include <sys/uio.h> 3050974Swpaul 3150974Swpaul#include <errno.h> 3250974Swpaul#include <stdio.h> 3350974Swpaul#include <string.h> 34122678Sobrien#include <unistd.h> 35122678Sobrien#include <stdarg.h> 36122678Sobrien 3750974Swpaul#include "buffer.h" 3850974Swpaul#include "log.h" 3950974Swpaul#include "atomicio.h" 4050974Swpaul#include "msg.h" 4164963Swpaul#include "misc.h" 4264963Swpaul 4364963Swpaulint 4450974Swpaulssh_msg_send(int fd, u_char type, Buffer *m) 4550974Swpaul{ 4650974Swpaul u_char buf[5]; 4750974Swpaul u_int mlen = buffer_len(m); 4850974Swpaul 4950974Swpaul debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff); 5050974Swpaul 5150974Swpaul put_u32(buf, mlen + 1); 5250974Swpaul buf[4] = type; /* 1st byte of payload is mesg-type */ 5350974Swpaul if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) { 5450974Swpaul error("ssh_msg_send: write"); 5550974Swpaul return (-1); 5650974Swpaul } 5750974Swpaul if (atomicio(vwrite, fd, buffer_ptr(m), mlen) != mlen) { 5850974Swpaul error("ssh_msg_send: write"); 5950974Swpaul return (-1); 6050974Swpaul } 6150974Swpaul return (0); 6250974Swpaul} 6350974Swpaul 6450974Swpaulint 6550974Swpaulssh_msg_recv(int fd, Buffer *m) 6650974Swpaul{ 67129876Sphk u_char buf[4]; 6850974Swpaul u_int msg_len; 6987059Sluigi 7050974Swpaul debug3("ssh_msg_recv entering"); 7150974Swpaul 7250974Swpaul if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) { 7350974Swpaul if (errno != EPIPE) 7450974Swpaul error("ssh_msg_recv: read: header"); 7550974Swpaul return (-1); 7687390Sjhay } 7787390Sjhay msg_len = get_u32(buf); 7850974Swpaul if (msg_len > 256 * 1024) { 7950974Swpaul error("ssh_msg_recv: read: bad msg_len %u", msg_len); 8050974Swpaul return (-1); 8150974Swpaul } 8250974Swpaul buffer_clear(m); 8350974Swpaul buffer_append_space(m, msg_len); 8450974Swpaul if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) { 8550974Swpaul error("ssh_msg_recv: read: %s", strerror(errno)); 8650974Swpaul return (-1); 8750974Swpaul } 8850974Swpaul return (0); 8950974Swpaul} 9050974Swpaul