msg.c 2.8 KB

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