1 /* $OpenBSD: roaming_common.c,v 1.5 2009/06/27 09:32:43 andreas Exp $ */
2 /*
3 * Copyright © 2013
4 * Thorsten “mirabilos” Glaser <tg@mirbsd.org>
5 * Copyright (c) 2004-2009 AppGate Network Security AB
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "atomicio.h"
31 #include "log.h"
32 #include "packet.h"
33 #include "xmalloc.h"
34 #include "cipher.h"
35 #include "buffer.h"
36 #include "roaming.h"
37
38 __RCSID("$MirOS: src/usr.bin/ssh/roaming_common.c,v 1.3 2013/10/31 20:07:12 tg Exp $");
39
40 static size_t out_buf_size = 0;
41 static char *out_buf = NULL;
42 static size_t out_start;
43 static size_t out_last;
44
45 static u_int64_t write_bytes = 0;
46 static u_int64_t read_bytes = 0;
47
48 int roaming_enabled = 0;
49 int resume_in_progress = 0;
50
51 int
get_snd_buf_size(void)52 get_snd_buf_size(void)
53 {
54 int fd = packet_get_connection_out();
55 int optval, optvallen;
56
57 optvallen = sizeof(optval);
58 if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optvallen) != 0)
59 optval = DEFAULT_ROAMBUF;
60 return optval;
61 }
62
63 int
get_recv_buf_size(void)64 get_recv_buf_size(void)
65 {
66 int fd = packet_get_connection_in();
67 int optval, optvallen;
68
69 optvallen = sizeof(optval);
70 if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optval, &optvallen) != 0)
71 optval = DEFAULT_ROAMBUF;
72 return optval;
73 }
74
75 void
set_out_buffer_size(size_t size)76 set_out_buffer_size(size_t size)
77 {
78 /*
79 * The buffer size can only be set once and the buffer will live
80 * as long as the session lives.
81 */
82 if (out_buf == NULL) {
83 out_buf_size = size;
84 out_buf = xmalloc(size);
85 out_start = 0;
86 out_last = 0;
87 }
88 }
89
90 u_int64_t
get_recv_bytes(void)91 get_recv_bytes(void)
92 {
93 return read_bytes;
94 }
95
96 void
add_recv_bytes(u_int64_t num)97 add_recv_bytes(u_int64_t num)
98 {
99 read_bytes += num;
100 }
101
102 u_int64_t
get_sent_bytes(void)103 get_sent_bytes(void)
104 {
105 return write_bytes;
106 }
107
108 void
roam_set_bytes(u_int64_t sent,u_int64_t recvd)109 roam_set_bytes(u_int64_t sent, u_int64_t recvd)
110 {
111 read_bytes = recvd;
112 write_bytes = sent;
113 }
114
115 static void
buf_append(const char * buf,size_t count)116 buf_append(const char *buf, size_t count)
117 {
118 if (count > out_buf_size) {
119 buf += count - out_buf_size;
120 count = out_buf_size;
121 }
122 if (count < out_buf_size - out_last) {
123 memcpy(out_buf + out_last, buf, count);
124 if (out_start > out_last)
125 out_start += count;
126 out_last += count;
127 } else {
128 /* data will wrap */
129 size_t chunk = out_buf_size - out_last;
130 memcpy(out_buf + out_last, buf, chunk);
131 memcpy(out_buf, buf + chunk, count - chunk);
132 out_last = count - chunk;
133 out_start = out_last + 1;
134 }
135 }
136
137 ssize_t
roaming_write(int fd,const void * buf,size_t count,int * cont)138 roaming_write(int fd, const void *buf, size_t count,
139 int *cont __attribute__((__unused__)))
140 {
141 ssize_t ret;
142
143 ret = write(fd, buf, count);
144 if (ret > 0 && !resume_in_progress) {
145 write_bytes += ret;
146 if (out_buf_size > 0)
147 buf_append(buf, ret);
148 }
149 debug3("Wrote %ld bytes for a total of %llu", (long)ret,
150 (unsigned long long)write_bytes);
151 return ret;
152 }
153
154 ssize_t
roaming_read(int fd,void * buf,size_t count,int * cont)155 roaming_read(int fd, void *buf, size_t count,
156 int *cont __attribute__((__unused__)))
157 {
158 ssize_t ret = read(fd, buf, count);
159 if (ret > 0) {
160 if (!resume_in_progress) {
161 read_bytes += ret;
162 }
163 }
164 return ret;
165 }
166
167 size_t
roaming_atomicio(ssize_t (* f)(int,void *,size_t),int fd,void * buf,size_t count)168 roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf,
169 size_t count)
170 {
171 size_t ret = atomicio(f, fd, buf, count);
172
173 if (f == vwrite && ret > 0 && !resume_in_progress) {
174 write_bytes += ret;
175 } else if (f == read && ret > 0 && !resume_in_progress) {
176 read_bytes += ret;
177 }
178 return ret;
179 }
180
181 void
resend_bytes(int fd,u_int64_t * offset)182 resend_bytes(int fd, u_int64_t *offset)
183 {
184 size_t available, needed;
185
186 if (out_start < out_last)
187 available = out_last - out_start;
188 else
189 available = out_buf_size;
190 needed = write_bytes - *offset;
191 debug3("resend_bytes: resend %lu bytes from %llu",
192 (unsigned long)needed, (unsigned long long)*offset);
193 if (needed > available)
194 fatal("Needed to resend more data than in the cache");
195 if (out_last < needed) {
196 int chunkend = needed - out_last;
197 atomicio(vwrite, fd, out_buf + out_buf_size - chunkend,
198 chunkend);
199 atomicio(vwrite, fd, out_buf, out_last);
200 } else {
201 atomicio(vwrite, fd, out_buf + (out_last - needed), needed);
202 }
203 }
204