summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--roaming.h8
-rw-r--r--roaming_common.c96
3 files changed, 108 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 93f6b8a20..20aa4bd5c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,12 @@
4 packet_bacup_state() and packet_restore_state() will be used to 4 packet_bacup_state() and packet_restore_state() will be used to
5 temporarily save the current state ren resuming a suspended connection. 5 temporarily save the current state ren resuming a suspended connection.
6 ok markus@ 6 ok markus@
7 - andreas@cvs.openbsd.org 2009/06/27 09:32:43
8 [roaming_common.c roaming.h]
9 It may be necessary to retransmit some data when resuming, so add it
10 to a buffer when roaming is enabled.
11 Most of this code was written by Martin Forssen, maf at appgate dot com.
12 ok markus@
7 13
820090622 1420090622
9 - (dtucker) OpenBSD CVS Sync 15 - (dtucker) OpenBSD CVS Sync
diff --git a/roaming.h b/roaming.h
index e99465502..e517161f6 100644
--- a/roaming.h
+++ b/roaming.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: roaming.h,v 1.3 2009/06/21 09:04:03 dtucker Exp $ */ 1/* $OpenBSD: roaming.h,v 1.4 2009/06/27 09:32:43 andreas Exp $ */
2/* 2/*
3 * Copyright (c) 2004-2009 AppGate Network Security AB 3 * Copyright (c) 2004-2009 AppGate Network Security AB
4 * 4 *
@@ -18,15 +18,21 @@
18#ifndef ROAMING_H 18#ifndef ROAMING_H
19#define ROAMING_H 19#define ROAMING_H
20 20
21#define DEFAULT_ROAMBUF 65536
22
21extern int resume_in_progress; 23extern int resume_in_progress;
22 24
25int get_snd_buf_size(void);
26int get_recv_buf_size(void);
23void add_recv_bytes(u_int64_t); 27void add_recv_bytes(u_int64_t);
28void set_out_buffer_size(size_t);
24ssize_t roaming_write(int, const void *, size_t, int *); 29ssize_t roaming_write(int, const void *, size_t, int *);
25ssize_t roaming_read(int, void *, size_t, int *); 30ssize_t roaming_read(int, void *, size_t, int *);
26size_t roaming_atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t); 31size_t roaming_atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
27u_int64_t get_recv_bytes(void); 32u_int64_t get_recv_bytes(void);
28u_int64_t get_sent_bytes(void); 33u_int64_t get_sent_bytes(void);
29void roam_set_bytes(u_int64_t, u_int64_t); 34void roam_set_bytes(u_int64_t, u_int64_t);
35void resend_bytes(int, u_int64_t *);
30int resume_kex(void); 36int resume_kex(void);
31 37
32#endif /* ROAMING */ 38#endif /* ROAMING */
diff --git a/roaming_common.c b/roaming_common.c
index 14dd5808f..73db09d79 100644
--- a/roaming_common.c
+++ b/roaming_common.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: roaming_common.c,v 1.4 2009/06/21 09:04:03 dtucker Exp $ */ 1/* $OpenBSD: roaming_common.c,v 1.5 2009/06/27 09:32:43 andreas Exp $ */
2/* 2/*
3 * Copyright (c) 2004-2009 AppGate Network Security AB 3 * Copyright (c) 2004-2009 AppGate Network Security AB
4 * 4 *
@@ -26,6 +26,7 @@
26#include <inttypes.h> 26#include <inttypes.h>
27#endif 27#endif
28#include <stdarg.h> 28#include <stdarg.h>
29#include <string.h>
29#include <unistd.h> 30#include <unistd.h>
30 31
31#include "atomicio.h" 32#include "atomicio.h"
@@ -36,11 +37,56 @@
36#include "buffer.h" 37#include "buffer.h"
37#include "roaming.h" 38#include "roaming.h"
38 39
40static size_t out_buf_size = 0;
41static char *out_buf = NULL;
42static size_t out_start;
43static size_t out_last;
44
39static u_int64_t write_bytes = 0; 45static u_int64_t write_bytes = 0;
40static u_int64_t read_bytes = 0; 46static u_int64_t read_bytes = 0;
41 47
48int roaming_enabled = 0;
42int resume_in_progress = 0; 49int resume_in_progress = 0;
43 50
51int
52get_snd_buf_size()
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
63int
64get_recv_buf_size()
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
75void
76set_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
44u_int64_t 90u_int64_t
45get_recv_bytes(void) 91get_recv_bytes(void)
46{ 92{
@@ -66,6 +112,28 @@ roam_set_bytes(u_int64_t sent, u_int64_t recvd)
66 write_bytes = sent; 112 write_bytes = sent;
67} 113}
68 114
115static void
116buf_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
69ssize_t 137ssize_t
70roaming_write(int fd, const void *buf, size_t count, int *cont) 138roaming_write(int fd, const void *buf, size_t count, int *cont)
71{ 139{
@@ -74,6 +142,8 @@ roaming_write(int fd, const void *buf, size_t count, int *cont)
74 ret = write(fd, buf, count); 142 ret = write(fd, buf, count);
75 if (ret > 0 && !resume_in_progress) { 143 if (ret > 0 && !resume_in_progress) {
76 write_bytes += ret; 144 write_bytes += ret;
145 if (out_buf_size > 0)
146 buf_append(buf, ret);
77 } 147 }
78 debug3("Wrote %ld bytes for a total of %llu", (long)ret, 148 debug3("Wrote %ld bytes for a total of %llu", (long)ret,
79 (unsigned long long)write_bytes); 149 (unsigned long long)write_bytes);
@@ -105,3 +175,27 @@ roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf,
105 } 175 }
106 return ret; 176 return ret;
107} 177}
178
179void
180resend_bytes(int fd, u_int64_t *offset)
181{
182 size_t available, needed;
183
184 if (out_start < out_last)
185 available = out_last - out_start;
186 else
187 available = out_buf_size;
188 needed = write_bytes - *offset;
189 debug3("resend_bytes: resend %lu bytes from %llu",
190 (unsigned long)needed, (unsigned long long)*offset);
191 if (needed > available)
192 fatal("Needed to resend more data than in the cache");
193 if (out_last < needed) {
194 int chunkend = needed - out_last;
195 atomicio(vwrite, fd, out_buf + out_buf_size - chunkend,
196 chunkend);
197 atomicio(vwrite, fd, out_buf, out_last);
198 } else {
199 atomicio(vwrite, fd, out_buf + (out_last - needed), needed);
200 }
201}