blob: 14dd5808f6e2447f6e4bdccc9a22e263cfff3956 [file] [log] [blame]
Darren Tuckere6b590e2009-06-21 19:08:48 +10001/* $OpenBSD: roaming_common.c,v 1.4 2009/06/21 09:04:03 dtucker Exp $ */
Darren Tuckerc5564e12009-06-21 18:53:53 +10002/*
3 * Copyright (c) 2004-2009 AppGate Network Security AB
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
Darren Tucker828c96d2009-06-21 22:22:08 +100018#include "includes.h"
19
Darren Tuckerc5564e12009-06-21 18:53:53 +100020#include <sys/types.h>
21#include <sys/socket.h>
22#include <sys/uio.h>
23
24#include <errno.h>
Darren Tucker828c96d2009-06-21 22:22:08 +100025#ifdef HAVE_INTTYPES_H
Darren Tuckerc5564e12009-06-21 18:53:53 +100026#include <inttypes.h>
Darren Tucker828c96d2009-06-21 22:22:08 +100027#endif
Darren Tuckerc5564e12009-06-21 18:53:53 +100028#include <stdarg.h>
29#include <unistd.h>
30
31#include "atomicio.h"
32#include "log.h"
33#include "packet.h"
34#include "xmalloc.h"
35#include "cipher.h"
36#include "buffer.h"
37#include "roaming.h"
38
39static u_int64_t write_bytes = 0;
40static u_int64_t read_bytes = 0;
41
42int resume_in_progress = 0;
43
44u_int64_t
45get_recv_bytes(void)
46{
47 return read_bytes;
48}
49
50void
51add_recv_bytes(u_int64_t num)
52{
53 read_bytes += num;
54}
55
56u_int64_t
57get_sent_bytes(void)
58{
59 return write_bytes;
60}
61
62void
Darren Tuckere6b590e2009-06-21 19:08:48 +100063roam_set_bytes(u_int64_t sent, u_int64_t recvd)
Darren Tuckerc5564e12009-06-21 18:53:53 +100064{
Darren Tuckere6b590e2009-06-21 19:08:48 +100065 read_bytes = recvd;
Darren Tuckerc5564e12009-06-21 18:53:53 +100066 write_bytes = sent;
67}
68
69ssize_t
70roaming_write(int fd, const void *buf, size_t count, int *cont)
71{
72 ssize_t ret;
73
74 ret = write(fd, buf, count);
75 if (ret > 0 && !resume_in_progress) {
76 write_bytes += ret;
77 }
Darren Tuckere6b590e2009-06-21 19:08:48 +100078 debug3("Wrote %ld bytes for a total of %llu", (long)ret,
79 (unsigned long long)write_bytes);
Darren Tuckerc5564e12009-06-21 18:53:53 +100080 return ret;
81}
82
83ssize_t
84roaming_read(int fd, void *buf, size_t count, int *cont)
85{
86 ssize_t ret = read(fd, buf, count);
87 if (ret > 0) {
88 if (!resume_in_progress) {
89 read_bytes += ret;
90 }
91 }
92 return ret;
93}
94
Darren Tuckere6b590e2009-06-21 19:08:48 +100095size_t
96roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf,
97 size_t count)
Darren Tuckerc5564e12009-06-21 18:53:53 +100098{
Darren Tuckere6b590e2009-06-21 19:08:48 +100099 size_t ret = atomicio(f, fd, buf, count);
Darren Tuckerc5564e12009-06-21 18:53:53 +1000100
Darren Tuckere6b590e2009-06-21 19:08:48 +1000101 if (f == vwrite && ret > 0 && !resume_in_progress) {
Darren Tuckerc5564e12009-06-21 18:53:53 +1000102 write_bytes += ret;
103 } else if (f == read && ret > 0 && !resume_in_progress) {
104 read_bytes += ret;
105 }
106 return ret;
107}