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 | |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame^] | 18 | #include "includes.h" |
| 19 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/uio.h> |
| 23 | |
| 24 | #include <errno.h> |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame^] | 25 | #ifdef HAVE_INTTYPES_H |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 26 | #include <inttypes.h> |
Darren Tucker | 828c96d | 2009-06-21 22:22:08 +1000 | [diff] [blame^] | 27 | #endif |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 28 | #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 | |
| 39 | static u_int64_t write_bytes = 0; |
| 40 | static u_int64_t read_bytes = 0; |
| 41 | |
| 42 | int resume_in_progress = 0; |
| 43 | |
| 44 | u_int64_t |
| 45 | get_recv_bytes(void) |
| 46 | { |
| 47 | return read_bytes; |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | add_recv_bytes(u_int64_t num) |
| 52 | { |
| 53 | read_bytes += num; |
| 54 | } |
| 55 | |
| 56 | u_int64_t |
| 57 | get_sent_bytes(void) |
| 58 | { |
| 59 | return write_bytes; |
| 60 | } |
| 61 | |
| 62 | void |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 63 | roam_set_bytes(u_int64_t sent, u_int64_t recvd) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 64 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 65 | read_bytes = recvd; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 66 | write_bytes = sent; |
| 67 | } |
| 68 | |
| 69 | ssize_t |
| 70 | roaming_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 Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 78 | debug3("Wrote %ld bytes for a total of %llu", (long)ret, |
| 79 | (unsigned long long)write_bytes); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | ssize_t |
| 84 | roaming_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 Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 95 | size_t |
| 96 | roaming_atomicio(ssize_t(*f)(int, void*, size_t), int fd, void *buf, |
| 97 | size_t count) |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 98 | { |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 99 | size_t ret = atomicio(f, fd, buf, count); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 100 | |
Darren Tucker | e6b590e | 2009-06-21 19:08:48 +1000 | [diff] [blame] | 101 | if (f == vwrite && ret > 0 && !resume_in_progress) { |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 102 | write_bytes += ret; |
| 103 | } else if (f == read && ret > 0 && !resume_in_progress) { |
| 104 | read_bytes += ret; |
| 105 | } |
| 106 | return ret; |
| 107 | } |