blob: 065542520c618ef02a7fa26f25391d40ea69c098 [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
18#include <sys/types.h>
19#include <sys/socket.h>
20#include <sys/uio.h>
21
22#include <errno.h>
23#include <inttypes.h>
24#include <stdarg.h>
25#include <unistd.h>
26
27#include "atomicio.h"
28#include "log.h"
29#include "packet.h"
30#include "xmalloc.h"
31#include "cipher.h"
32#include "buffer.h"
33#include "roaming.h"
34
35static u_int64_t write_bytes = 0;
36static u_int64_t read_bytes = 0;
37
38int resume_in_progress = 0;
39
40u_int64_t
41get_recv_bytes(void)
42{
43 return read_bytes;
44}
45
46void
47add_recv_bytes(u_int64_t num)
48{
49 read_bytes += num;
50}
51
52u_int64_t
53get_sent_bytes(void)
54{
55 return write_bytes;
56}
57
58void
Darren Tuckere6b590e2009-06-21 19:08:48 +100059roam_set_bytes(u_int64_t sent, u_int64_t recvd)
Darren Tuckerc5564e12009-06-21 18:53:53 +100060{
Darren Tuckere6b590e2009-06-21 19:08:48 +100061 read_bytes = recvd;
Darren Tuckerc5564e12009-06-21 18:53:53 +100062 write_bytes = sent;
63}
64
65ssize_t
66roaming_write(int fd, const void *buf, size_t count, int *cont)
67{
68 ssize_t ret;
69
70 ret = write(fd, buf, count);
71 if (ret > 0 && !resume_in_progress) {
72 write_bytes += ret;
73 }
Darren Tuckere6b590e2009-06-21 19:08:48 +100074 debug3("Wrote %ld bytes for a total of %llu", (long)ret,
75 (unsigned long long)write_bytes);
Darren Tuckerc5564e12009-06-21 18:53:53 +100076 return ret;
77}
78
79ssize_t
80roaming_read(int fd, void *buf, size_t count, int *cont)
81{
82 ssize_t ret = read(fd, buf, count);
83 if (ret > 0) {
84 if (!resume_in_progress) {
85 read_bytes += ret;
86 }
87 }
88 return ret;
89}
90
Darren Tuckere6b590e2009-06-21 19:08:48 +100091size_t
92roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf,
93 size_t count)
Darren Tuckerc5564e12009-06-21 18:53:53 +100094{
Darren Tuckere6b590e2009-06-21 19:08:48 +100095 size_t ret = atomicio(f, fd, buf, count);
Darren Tuckerc5564e12009-06-21 18:53:53 +100096
Darren Tuckere6b590e2009-06-21 19:08:48 +100097 if (f == vwrite && ret > 0 && !resume_in_progress) {
Darren Tuckerc5564e12009-06-21 18:53:53 +100098 write_bytes += ret;
99 } else if (f == read && ret > 0 && !resume_in_progress) {
100 read_bytes += ret;
101 }
102 return ret;
103}