xref: /freebsd/crypto/openssh/msg.c (revision 043840df5be0cf8490b48a08fe6d9c316f473f58)
1545d5ecaSDag-Erling Smørgrav /*
2545d5ecaSDag-Erling Smørgrav  * Copyright (c) 2002 Markus Friedl.  All rights reserved.
3545d5ecaSDag-Erling Smørgrav  *
4545d5ecaSDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
5545d5ecaSDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
6545d5ecaSDag-Erling Smørgrav  * are met:
7545d5ecaSDag-Erling Smørgrav  * 1. Redistributions of source code must retain the above copyright
8545d5ecaSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer.
9545d5ecaSDag-Erling Smørgrav  * 2. Redistributions in binary form must reproduce the above copyright
10545d5ecaSDag-Erling Smørgrav  *    notice, this list of conditions and the following disclaimer in the
11545d5ecaSDag-Erling Smørgrav  *    documentation and/or other materials provided with the distribution.
12545d5ecaSDag-Erling Smørgrav  *
13545d5ecaSDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14545d5ecaSDag-Erling Smørgrav  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15545d5ecaSDag-Erling Smørgrav  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16545d5ecaSDag-Erling Smørgrav  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17545d5ecaSDag-Erling Smørgrav  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18545d5ecaSDag-Erling Smørgrav  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19545d5ecaSDag-Erling Smørgrav  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20545d5ecaSDag-Erling Smørgrav  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21545d5ecaSDag-Erling Smørgrav  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22545d5ecaSDag-Erling Smørgrav  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23545d5ecaSDag-Erling Smørgrav  */
24545d5ecaSDag-Erling Smørgrav #include "includes.h"
25043840dfSDag-Erling Smørgrav RCSID("$OpenBSD: msg.c,v 1.8 2005/05/24 17:32:43 avsm Exp $");
26545d5ecaSDag-Erling Smørgrav 
27545d5ecaSDag-Erling Smørgrav #include "buffer.h"
28545d5ecaSDag-Erling Smørgrav #include "getput.h"
29545d5ecaSDag-Erling Smørgrav #include "log.h"
30545d5ecaSDag-Erling Smørgrav #include "atomicio.h"
31545d5ecaSDag-Erling Smørgrav #include "msg.h"
32545d5ecaSDag-Erling Smørgrav 
33efcad6b7SDag-Erling Smørgrav int
344b17dab0SDag-Erling Smørgrav ssh_msg_send(int fd, u_char type, Buffer *m)
35545d5ecaSDag-Erling Smørgrav {
36545d5ecaSDag-Erling Smørgrav 	u_char buf[5];
37545d5ecaSDag-Erling Smørgrav 	u_int mlen = buffer_len(m);
38545d5ecaSDag-Erling Smørgrav 
394b17dab0SDag-Erling Smørgrav 	debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff);
40545d5ecaSDag-Erling Smørgrav 
41545d5ecaSDag-Erling Smørgrav 	PUT_32BIT(buf, mlen + 1);
42545d5ecaSDag-Erling Smørgrav 	buf[4] = type;		/* 1st byte of payload is mesg-type */
43efcad6b7SDag-Erling Smørgrav 	if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) {
44efcad6b7SDag-Erling Smørgrav 		error("ssh_msg_send: write");
45efcad6b7SDag-Erling Smørgrav 		return (-1);
46efcad6b7SDag-Erling Smørgrav 	}
47efcad6b7SDag-Erling Smørgrav 	if (atomicio(vwrite, fd, buffer_ptr(m), mlen) != mlen) {
48efcad6b7SDag-Erling Smørgrav 		error("ssh_msg_send: write");
49efcad6b7SDag-Erling Smørgrav 		return (-1);
50efcad6b7SDag-Erling Smørgrav 	}
51efcad6b7SDag-Erling Smørgrav 	return (0);
52545d5ecaSDag-Erling Smørgrav }
53545d5ecaSDag-Erling Smørgrav 
54545d5ecaSDag-Erling Smørgrav int
554b17dab0SDag-Erling Smørgrav ssh_msg_recv(int fd, Buffer *m)
56545d5ecaSDag-Erling Smørgrav {
57545d5ecaSDag-Erling Smørgrav 	u_char buf[4];
58545d5ecaSDag-Erling Smørgrav 	u_int msg_len;
59545d5ecaSDag-Erling Smørgrav 
604b17dab0SDag-Erling Smørgrav 	debug3("ssh_msg_recv entering");
61545d5ecaSDag-Erling Smørgrav 
62043840dfSDag-Erling Smørgrav 	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
63043840dfSDag-Erling Smørgrav 		if (errno != EPIPE)
64043840dfSDag-Erling Smørgrav 			error("ssh_msg_recv: read: header");
65efcad6b7SDag-Erling Smørgrav 		return (-1);
66545d5ecaSDag-Erling Smørgrav 	}
67545d5ecaSDag-Erling Smørgrav 	msg_len = GET_32BIT(buf);
68efcad6b7SDag-Erling Smørgrav 	if (msg_len > 256 * 1024) {
69efcad6b7SDag-Erling Smørgrav 		error("ssh_msg_recv: read: bad msg_len %u", msg_len);
70efcad6b7SDag-Erling Smørgrav 		return (-1);
71efcad6b7SDag-Erling Smørgrav 	}
72545d5ecaSDag-Erling Smørgrav 	buffer_clear(m);
73545d5ecaSDag-Erling Smørgrav 	buffer_append_space(m, msg_len);
74043840dfSDag-Erling Smørgrav 	if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
75043840dfSDag-Erling Smørgrav 		error("ssh_msg_recv: read: %s", strerror(errno));
76efcad6b7SDag-Erling Smørgrav 		return (-1);
77efcad6b7SDag-Erling Smørgrav 	}
78efcad6b7SDag-Erling Smørgrav 	return (0);
79545d5ecaSDag-Erling Smørgrav }
80