summaryrefslogtreecommitdiff
path: root/sftp-client.c
diff options
context:
space:
mode:
Diffstat (limited to 'sftp-client.c')
-rw-r--r--sftp-client.c54
1 files changed, 27 insertions, 27 deletions
diff --git a/sftp-client.c b/sftp-client.c
index e0d3ad568..3fac22bee 100644
--- a/sftp-client.c
+++ b/sftp-client.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2001,2002 Damien Miller. All rights reserved. 2 * Copyright (c) 2001-2003 Damien Miller. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
@@ -28,7 +28,7 @@
28/* XXX: copy between two remote sites */ 28/* XXX: copy between two remote sites */
29 29
30#include "includes.h" 30#include "includes.h"
31RCSID("$OpenBSD: sftp-client.c,v 1.39 2003/01/10 08:19:07 fgsch Exp $"); 31RCSID("$OpenBSD: sftp-client.c,v 1.40 2003/01/10 08:48:15 djm Exp $");
32 32
33#include "openbsd-compat/sys-queue.h" 33#include "openbsd-compat/sys-queue.h"
34 34
@@ -49,6 +49,9 @@ extern int showprogress;
49/* Minimum amount of data to read at at time */ 49/* Minimum amount of data to read at at time */
50#define MIN_READ_SIZE 512 50#define MIN_READ_SIZE 512
51 51
52/* Maximum packet size */
53#define MAX_MSG_LENGTH (256 * 1024)
54
52struct sftp_conn { 55struct sftp_conn {
53 int fd_in; 56 int fd_in;
54 int fd_out; 57 int fd_out;
@@ -61,48 +64,45 @@ struct sftp_conn {
61static void 64static void
62send_msg(int fd, Buffer *m) 65send_msg(int fd, Buffer *m)
63{ 66{
64 int mlen = buffer_len(m); 67 u_char mlen[4];
65 int len; 68
66 Buffer oqueue; 69 if (buffer_len(m) > MAX_MSG_LENGTH)
70 fatal("Outbound message too long %u", buffer_len(m));
67 71
68 buffer_init(&oqueue); 72 /* Send length first */
69 buffer_put_int(&oqueue, mlen); 73 PUT_32BIT(mlen, buffer_len(m));
70 buffer_append(&oqueue, buffer_ptr(m), mlen); 74 if (atomicio(write, fd, mlen, sizeof(mlen)) <= 0)
71 buffer_consume(m, mlen); 75 fatal("Couldn't send packet: %s", strerror(errno));
72 76
73 len = atomicio(write, fd, buffer_ptr(&oqueue), buffer_len(&oqueue)); 77 if (atomicio(write, fd, buffer_ptr(m), buffer_len(m)) <= 0)
74 if (len <= 0)
75 fatal("Couldn't send packet: %s", strerror(errno)); 78 fatal("Couldn't send packet: %s", strerror(errno));
76 79
77 buffer_free(&oqueue); 80 buffer_clear(m);
78} 81}
79 82
80static void 83static void
81get_msg(int fd, Buffer *m) 84get_msg(int fd, Buffer *m)
82{ 85{
83 u_int len, msg_len; 86 ssize_t len;
84 unsigned char buf[4096]; 87 u_int msg_len;
85 88
86 len = atomicio(read, fd, buf, 4); 89 buffer_append_space(m, 4);
90 len = atomicio(read, fd, buffer_ptr(m), 4);
87 if (len == 0) 91 if (len == 0)
88 fatal("Connection closed"); 92 fatal("Connection closed");
89 else if (len == -1) 93 else if (len == -1)
90 fatal("Couldn't read packet: %s", strerror(errno)); 94 fatal("Couldn't read packet: %s", strerror(errno));
91 95
92 msg_len = GET_32BIT(buf); 96 msg_len = buffer_get_int(m);
93 if (msg_len > 256 * 1024) 97 if (msg_len > MAX_MSG_LENGTH)
94 fatal("Received message too long %u", msg_len); 98 fatal("Received message too long %u", msg_len);
95 99
96 while (msg_len) { 100 buffer_append_space(m, msg_len);
97 len = atomicio(read, fd, buf, MIN(msg_len, sizeof(buf))); 101 len = atomicio(read, fd, buffer_ptr(m), msg_len);
98 if (len == 0) 102 if (len == 0)
99 fatal("Connection closed"); 103 fatal("Connection closed");
100 else if (len == -1) 104 else if (len == -1)
101 fatal("Couldn't read packet: %s", strerror(errno)); 105 fatal("Read packet: %s", strerror(errno));
102
103 msg_len -= len;
104 buffer_append(m, buf, len);
105 }
106} 106}
107 107
108static void 108static void