Home | History | Annotate | Line # | Download | only in dist
      1   1.1  christos /*	$NetBSD: msg.c,v 1.13 2026/04/08 18:58:41 christos Exp $	*/
      2  1.13  christos /* $OpenBSD: msg.c,v 1.22 2026/02/14 00:18:34 jsg 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.13 2026/04/08 18:58:41 christos Exp $");
     30   1.1  christos #include <sys/types.h>
     31   1.1  christos 
     32   1.1  christos #include <errno.h>
     33   1.1  christos #include <string.h>
     34   1.1  christos #include <unistd.h>
     35   1.1  christos #include <stdarg.h>
     36   1.2  christos #include <time.h>
     37   1.1  christos 
     38   1.4  christos #include "sshbuf.h"
     39   1.1  christos #include "log.h"
     40   1.1  christos #include "atomicio.h"
     41   1.1  christos #include "msg.h"
     42   1.1  christos #include "misc.h"
     43   1.1  christos 
     44   1.1  christos int
     45   1.4  christos ssh_msg_send(int fd, u_char type, struct sshbuf *m)
     46   1.1  christos {
     47   1.1  christos 	u_char buf[5];
     48   1.4  christos 	u_int mlen = sshbuf_len(m);
     49   1.1  christos 
     50  1.12  christos 	debug3_f("type %u len %zu", (unsigned int)type & 0xff, sshbuf_len(m));
     51   1.1  christos 
     52   1.1  christos 	put_u32(buf, mlen + 1);
     53   1.1  christos 	buf[4] = type;		/* 1st byte of payload is mesg-type */
     54   1.1  christos 	if (atomicio(vwrite, fd, buf, sizeof(buf)) != sizeof(buf)) {
     55  1.11  christos 		error_f("write: %s", strerror(errno));
     56   1.1  christos 		return (-1);
     57   1.1  christos 	}
     58   1.7  christos 	if (atomicio(vwrite, fd, sshbuf_mutable_ptr(m), mlen) != mlen) {
     59  1.11  christos 		error_f("write: %s", strerror(errno));
     60   1.1  christos 		return (-1);
     61   1.1  christos 	}
     62  1.12  christos 	debug3_f("done");
     63   1.1  christos 	return (0);
     64   1.1  christos }
     65   1.1  christos 
     66   1.1  christos int
     67   1.4  christos ssh_msg_recv(int fd, struct sshbuf *m)
     68   1.1  christos {
     69   1.4  christos 	u_char buf[4], *p;
     70   1.1  christos 	u_int msg_len;
     71   1.4  christos 	int r;
     72   1.1  christos 
     73   1.1  christos 	debug3("ssh_msg_recv entering");
     74   1.1  christos 
     75   1.1  christos 	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
     76   1.1  christos 		if (errno != EPIPE)
     77  1.11  christos 			error_f("read header: %s", strerror(errno));
     78   1.1  christos 		return (-1);
     79   1.1  christos 	}
     80   1.1  christos 	msg_len = get_u32(buf);
     81  1.10  christos 	if (msg_len > sshbuf_max_size(m)) {
     82  1.11  christos 		error_f("read: bad msg_len %u", msg_len);
     83   1.1  christos 		return (-1);
     84   1.1  christos 	}
     85   1.4  christos 	sshbuf_reset(m);
     86   1.4  christos 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
     87  1.11  christos 		error_fr(r, "reserve");
     88   1.4  christos 		return -1;
     89   1.4  christos 	}
     90   1.4  christos 	if (atomicio(read, fd, p, msg_len) != msg_len) {
     91  1.11  christos 		error_f("read: %s", strerror(errno));
     92   1.1  christos 		return (-1);
     93   1.1  christos 	}
     94   1.1  christos 	return (0);
     95   1.1  christos }
     96