1294332Sdes/* $OpenBSD: msg.c,v 1.16 2015/01/15 09:40:00 djm Exp $ */
298675Sdes/*
398675Sdes * Copyright (c) 2002 Markus Friedl.  All rights reserved.
498675Sdes *
598675Sdes * Redistribution and use in source and binary forms, with or without
698675Sdes * modification, are permitted provided that the following conditions
798675Sdes * are met:
898675Sdes * 1. Redistributions of source code must retain the above copyright
998675Sdes *    notice, this list of conditions and the following disclaimer.
1098675Sdes * 2. Redistributions in binary form must reproduce the above copyright
1198675Sdes *    notice, this list of conditions and the following disclaimer in the
1298675Sdes *    documentation and/or other materials provided with the distribution.
1398675Sdes *
1498675Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1598675Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1698675Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1798675Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1898675Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1998675Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2098675Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2198675Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2298675Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2398675Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2498675Sdes */
25162852Sdes
2698675Sdes#include "includes.h"
2798675Sdes
28162852Sdes#include <sys/types.h>
29162852Sdes#include <sys/uio.h>
30162852Sdes
31162852Sdes#include <errno.h>
32162852Sdes#include <stdio.h>
33162852Sdes#include <string.h>
34162852Sdes#include <unistd.h>
35162852Sdes#include <stdarg.h>
36162852Sdes
37294332Sdes#include "sshbuf.h"
38294332Sdes#include "ssherr.h"
3998675Sdes#include "log.h"
4098675Sdes#include "atomicio.h"
4198675Sdes#include "msg.h"
42162852Sdes#include "misc.h"
4398675Sdes
44126274Sdesint
45294332Sdesssh_msg_send(int fd, u_char type, struct sshbuf *m)
4698675Sdes{
4798675Sdes	u_char buf[5];
48294332Sdes	u_int mlen = sshbuf_len(m);
4998675Sdes
50106121Sdes	debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff);
5198675Sdes
52162852Sdes	put_u32(buf, mlen + 1);
5398675Sdes	buf[4] = type;		/* 1st byte of payload is mesg-type */
54126274Sdes	if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) {
55126274Sdes		error("ssh_msg_send: write");
56126274Sdes		return (-1);
57126274Sdes	}
58294332Sdes	if (atomicio(vwrite, fd, (u_char *)sshbuf_ptr(m), mlen) != mlen) {
59126274Sdes		error("ssh_msg_send: write");
60126274Sdes		return (-1);
61126274Sdes	}
62126274Sdes	return (0);
6398675Sdes}
6498675Sdes
6598675Sdesint
66294332Sdesssh_msg_recv(int fd, struct sshbuf *m)
6798675Sdes{
68294332Sdes	u_char buf[4], *p;
6998675Sdes	u_int msg_len;
70294332Sdes	int r;
7198675Sdes
72106121Sdes	debug3("ssh_msg_recv entering");
7398675Sdes
74149749Sdes	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
75149749Sdes		if (errno != EPIPE)
76149749Sdes			error("ssh_msg_recv: read: header");
77126274Sdes		return (-1);
7898675Sdes	}
79162852Sdes	msg_len = get_u32(buf);
80126274Sdes	if (msg_len > 256 * 1024) {
81126274Sdes		error("ssh_msg_recv: read: bad msg_len %u", msg_len);
82126274Sdes		return (-1);
83126274Sdes	}
84294332Sdes	sshbuf_reset(m);
85294332Sdes	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
86294332Sdes		error("%s: buffer error: %s", __func__, ssh_err(r));
87294332Sdes		return -1;
88294332Sdes	}
89294332Sdes	if (atomicio(read, fd, p, msg_len) != msg_len) {
90149749Sdes		error("ssh_msg_recv: read: %s", strerror(errno));
91126274Sdes		return (-1);
92126274Sdes	}
93126274Sdes	return (0);
9498675Sdes}
95