xref: /freebsd/crypto/openssh/msg.c (revision 545d5eca429a5967b3300cb527d49cae8184e79f)
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"
25545d5ecaSDag-Erling Smørgrav RCSID("$OpenBSD: msg.c,v 1.2 2002/06/19 00:27:55 deraadt 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 
33545d5ecaSDag-Erling Smørgrav void
34545d5ecaSDag-Erling Smørgrav 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 
39545d5ecaSDag-Erling Smørgrav 	debug3("msg_send: type %d", type);
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 */
43545d5ecaSDag-Erling Smørgrav 	if (atomicio(write, fd, buf, sizeof(buf)) != sizeof(buf))
44545d5ecaSDag-Erling Smørgrav 		fatal("msg_send: write");
45545d5ecaSDag-Erling Smørgrav 	if (atomicio(write, fd, buffer_ptr(m), mlen) != mlen)
46545d5ecaSDag-Erling Smørgrav 		fatal("msg_send: write");
47545d5ecaSDag-Erling Smørgrav }
48545d5ecaSDag-Erling Smørgrav 
49545d5ecaSDag-Erling Smørgrav int
50545d5ecaSDag-Erling Smørgrav msg_recv(int fd, Buffer *m)
51545d5ecaSDag-Erling Smørgrav {
52545d5ecaSDag-Erling Smørgrav 	u_char buf[4];
53545d5ecaSDag-Erling Smørgrav 	ssize_t res;
54545d5ecaSDag-Erling Smørgrav 	u_int msg_len;
55545d5ecaSDag-Erling Smørgrav 
56545d5ecaSDag-Erling Smørgrav 	debug3("msg_recv entering");
57545d5ecaSDag-Erling Smørgrav 
58545d5ecaSDag-Erling Smørgrav 	res = atomicio(read, fd, buf, sizeof(buf));
59545d5ecaSDag-Erling Smørgrav 	if (res != sizeof(buf)) {
60545d5ecaSDag-Erling Smørgrav 		if (res == 0)
61545d5ecaSDag-Erling Smørgrav 			return -1;
62545d5ecaSDag-Erling Smørgrav 		fatal("msg_recv: read: header %d", res);
63545d5ecaSDag-Erling Smørgrav 	}
64545d5ecaSDag-Erling Smørgrav 	msg_len = GET_32BIT(buf);
65545d5ecaSDag-Erling Smørgrav 	if (msg_len > 256 * 1024)
66545d5ecaSDag-Erling Smørgrav 		fatal("msg_recv: read: bad msg_len %d", msg_len);
67545d5ecaSDag-Erling Smørgrav 	buffer_clear(m);
68545d5ecaSDag-Erling Smørgrav 	buffer_append_space(m, msg_len);
69545d5ecaSDag-Erling Smørgrav 	res = atomicio(read, fd, buffer_ptr(m), msg_len);
70545d5ecaSDag-Erling Smørgrav 	if (res != msg_len)
71545d5ecaSDag-Erling Smørgrav 		fatal("msg_recv: read: %ld != msg_len", (long)res);
72545d5ecaSDag-Erling Smørgrav 	return 0;
73545d5ecaSDag-Erling Smørgrav }
74