msg.c revision 1.12 1 1.1 christos /* $NetBSD: msg.c,v 1.12 2024/07/08 22:33:44 christos Exp $ */
2 1.12 christos /* $OpenBSD: msg.c,v 1.21 2024/05/17 00:30:24 djm Exp $ */
3 1.7 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 2002 Markus Friedl. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 christos */
27 1.1 christos
28 1.2 christos #include "includes.h"
29 1.8 pgoyette __RCSID("$NetBSD: msg.c,v 1.12 2024/07/08 22:33:44 christos Exp $");
30 1.1 christos #include <sys/types.h>
31 1.1 christos #include <sys/uio.h>
32 1.1 christos
33 1.1 christos #include <errno.h>
34 1.1 christos #include <stdio.h>
35 1.1 christos #include <string.h>
36 1.1 christos #include <unistd.h>
37 1.1 christos #include <stdarg.h>
38 1.2 christos #include <time.h>
39 1.1 christos
40 1.4 christos #include "sshbuf.h"
41 1.4 christos #include "ssherr.h"
42 1.1 christos #include "log.h"
43 1.1 christos #include "atomicio.h"
44 1.1 christos #include "msg.h"
45 1.1 christos #include "misc.h"
46 1.1 christos
47 1.1 christos int
48 1.4 christos ssh_msg_send(int fd, u_char type, struct sshbuf *m)
49 1.1 christos {
50 1.1 christos u_char buf[5];
51 1.4 christos u_int mlen = sshbuf_len(m);
52 1.1 christos
53 1.12 christos debug3_f("type %u len %zu", (unsigned int)type & 0xff, sshbuf_len(m));
54 1.1 christos
55 1.1 christos put_u32(buf, mlen + 1);
56 1.1 christos buf[4] = type; /* 1st byte of payload is mesg-type */
57 1.1 christos if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) {
58 1.11 christos error_f("write: %s", strerror(errno));
59 1.1 christos return (-1);
60 1.1 christos }
61 1.7 christos if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) {
62 1.11 christos error_f("write: %s", strerror(errno));
63 1.1 christos return (-1);
64 1.1 christos }
65 1.12 christos debug3_f("done");
66 1.1 christos return (0);
67 1.1 christos }
68 1.1 christos
69 1.1 christos int
70 1.4 christos ssh_msg_recv(int fd, struct sshbuf *m)
71 1.1 christos {
72 1.4 christos u_char buf[4], *p;
73 1.1 christos u_int msg_len;
74 1.4 christos int r;
75 1.1 christos
76 1.1 christos debug3("ssh_msg_recv entering");
77 1.1 christos
78 1.1 christos if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
79 1.1 christos if (errno != EPIPE)
80 1.11 christos error_f("read header: %s", strerror(errno));
81 1.1 christos return (-1);
82 1.1 christos }
83 1.1 christos msg_len = get_u32(buf);
84 1.10 christos if (msg_len > sshbuf_max_size(m)) {
85 1.11 christos error_f("read: bad msg_len %u", msg_len);
86 1.1 christos return (-1);
87 1.1 christos }
88 1.4 christos sshbuf_reset(m);
89 1.4 christos if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
90 1.11 christos error_fr(r, "reserve");
91 1.4 christos return -1;
92 1.4 christos }
93 1.4 christos if (atomicio(read, fd, p, msg_len) != msg_len) {
94 1.11 christos error_f("read: %s", strerror(errno));
95 1.1 christos return (-1);
96 1.1 christos }
97 1.1 christos return (0);
98 1.1 christos }
99