Home | History | Annotate | Line # | Download | only in dist
msg.c revision 1.7
      1  1.1  christos /*	$NetBSD: msg.c,v 1.7 2018/08/26 07:46:36 christos Exp $	*/
      2  1.7  christos /* $OpenBSD: msg.c,v 1.17 2018/07/09 21:59:10 markus 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.2  christos __RCSID("$NetBSD: msg.c,v 1.7 2018/08/26 07:46:36 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.1  christos 	debug3("ssh_msg_send: type %u", (unsigned int)type & 0xff);
     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.1  christos 		error("ssh_msg_send: write");
     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.1  christos 		error("ssh_msg_send: write");
     63  1.1  christos 		return (-1);
     64  1.1  christos 	}
     65  1.1  christos 	return (0);
     66  1.1  christos }
     67  1.1  christos 
     68  1.1  christos int
     69  1.4  christos ssh_msg_recv(int fd, struct sshbuf *m)
     70  1.1  christos {
     71  1.4  christos 	u_char buf[4], *p;
     72  1.1  christos 	u_int msg_len;
     73  1.4  christos 	int r;
     74  1.1  christos 
     75  1.1  christos 	debug3("ssh_msg_recv entering");
     76  1.1  christos 
     77  1.1  christos 	if (atomicio(read, fd, buf, sizeof(buf)) != sizeof(buf)) {
     78  1.1  christos 		if (errno != EPIPE)
     79  1.1  christos 			error("ssh_msg_recv: read: header");
     80  1.1  christos 		return (-1);
     81  1.1  christos 	}
     82  1.1  christos 	msg_len = get_u32(buf);
     83  1.1  christos 	if (msg_len > 256 * 1024) {
     84  1.1  christos 		error("ssh_msg_recv: read: bad msg_len %u", msg_len);
     85  1.1  christos 		return (-1);
     86  1.1  christos 	}
     87  1.4  christos 	sshbuf_reset(m);
     88  1.4  christos 	if ((r = sshbuf_reserve(m, msg_len, &p)) != 0) {
     89  1.4  christos 		error("%s: buffer error: %s", __func__, ssh_err(r));
     90  1.4  christos 		return -1;
     91  1.4  christos 	}
     92  1.4  christos 	if (atomicio(read, fd, p, msg_len) != msg_len) {
     93  1.1  christos 		error("ssh_msg_recv: read: %s", strerror(errno));
     94  1.1  christos 		return (-1);
     95  1.1  christos 	}
     96  1.1  christos 	return (0);
     97  1.1  christos }
     98