Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: roaming_common.c,v 1.4 2009/06/21 09:04:03 dtucker Exp $ */ |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 2 | /* |
| 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 | |
| 35 | static u_int64_t write_bytes = 0; |
| 36 | static u_int64_t read_bytes = 0; |
| 37 | |
| 38 | int resume_in_progress = 0; |
| 39 | |
| 40 | u_int64_t |
| 41 | get_recv_bytes(void) |
| 42 | { |
| 43 | return read_bytes; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | add_recv_bytes(u_int64_t num) |
| 48 | { |
| 49 | read_bytes += num; |
| 50 | } |
| 51 | |
| 52 | u_int64_t |
| 53 | get_sent_bytes(void) |
| 54 | { |
| 55 | return write_bytes; |
| 56 | } |
| 57 | |
| 58 | void |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 59 | roam_set_bytes(u_int64_t sent, u_int64_t recvd) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 60 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 61 | read_bytes = recvd; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 62 | write_bytes = sent; |
| 63 | } |
| 64 | |
| 65 | ssize_t |
| 66 | roaming_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 Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 74 | debug3("Wrote %ld bytes for a total of %llu", (long)ret, |
| 75 | (unsigned long long)write_bytes); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | ssize_t |
| 80 | roaming_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 Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 91 | size_t |
| 92 | roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf, |
| 93 | size_t count) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 94 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 95 | size_t ret = atomicio(f, fd, buf, count); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 96 | |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame^] | 97 | if (f == vwrite && ret > 0 && !resume_in_progress) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 98 | write_bytes += ret; |
| 99 | } else if (f == read && ret > 0 && !resume_in_progress) { |
| 100 | read_bytes += ret; |
| 101 | } |
| 102 | return ret; |
| 103 | } |