Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | #ifndef _RATELIM_INTERNAL_H_ |
michael@0 | 27 | #define _RATELIM_INTERNAL_H_ |
michael@0 | 28 | |
michael@0 | 29 | #ifdef __cplusplus |
michael@0 | 30 | extern "C" { |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | #include "event2/util.h" |
michael@0 | 34 | |
michael@0 | 35 | /** A token bucket is an internal structure that tracks how many bytes we are |
michael@0 | 36 | * currently willing to read or write on a given bufferevent or group of |
michael@0 | 37 | * bufferevents */ |
michael@0 | 38 | struct ev_token_bucket { |
michael@0 | 39 | /** How many bytes are we willing to read or write right now? These |
michael@0 | 40 | * values are signed so that we can do "defecit spending" */ |
michael@0 | 41 | ev_ssize_t read_limit, write_limit; |
michael@0 | 42 | /** When was this bucket last updated? Measured in abstract 'ticks' |
michael@0 | 43 | * relative to the token bucket configuration. */ |
michael@0 | 44 | ev_uint32_t last_updated; |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | /** Configuration info for a token bucket or set of token buckets. */ |
michael@0 | 48 | struct ev_token_bucket_cfg { |
michael@0 | 49 | /** How many bytes are we willing to read on average per tick? */ |
michael@0 | 50 | size_t read_rate; |
michael@0 | 51 | /** How many bytes are we willing to read at most in any one tick? */ |
michael@0 | 52 | size_t read_maximum; |
michael@0 | 53 | /** How many bytes are we willing to write on average per tick? */ |
michael@0 | 54 | size_t write_rate; |
michael@0 | 55 | /** How many bytes are we willing to write at most in any one tick? */ |
michael@0 | 56 | size_t write_maximum; |
michael@0 | 57 | |
michael@0 | 58 | /* How long is a tick? Note that fractions of a millisecond are |
michael@0 | 59 | * ignored. */ |
michael@0 | 60 | struct timeval tick_timeout; |
michael@0 | 61 | |
michael@0 | 62 | /* How long is a tick, in milliseconds? Derived from tick_timeout. */ |
michael@0 | 63 | unsigned msec_per_tick; |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | /** The current tick is 'current_tick': add bytes to 'bucket' as specified in |
michael@0 | 67 | * 'cfg'. */ |
michael@0 | 68 | int ev_token_bucket_update(struct ev_token_bucket *bucket, |
michael@0 | 69 | const struct ev_token_bucket_cfg *cfg, |
michael@0 | 70 | ev_uint32_t current_tick); |
michael@0 | 71 | |
michael@0 | 72 | /** In which tick does 'tv' fall according to 'cfg'? Note that ticks can |
michael@0 | 73 | * overflow easily; your code needs to handle this. */ |
michael@0 | 74 | ev_uint32_t ev_token_bucket_get_tick(const struct timeval *tv, |
michael@0 | 75 | const struct ev_token_bucket_cfg *cfg); |
michael@0 | 76 | |
michael@0 | 77 | /** Adjust 'bucket' to respect 'cfg', and note that it was last updated in |
michael@0 | 78 | * 'current_tick'. If 'reinitialize' is true, we are changing the |
michael@0 | 79 | * configuration of 'bucket'; otherwise, we are setting it up for the first |
michael@0 | 80 | * time. |
michael@0 | 81 | */ |
michael@0 | 82 | int ev_token_bucket_init(struct ev_token_bucket *bucket, |
michael@0 | 83 | const struct ev_token_bucket_cfg *cfg, |
michael@0 | 84 | ev_uint32_t current_tick, |
michael@0 | 85 | int reinitialize); |
michael@0 | 86 | |
michael@0 | 87 | int bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev, |
michael@0 | 88 | int unsuspend); |
michael@0 | 89 | |
michael@0 | 90 | /** Decrease the read limit of 'b' by 'n' bytes */ |
michael@0 | 91 | #define ev_token_bucket_decrement_read(b,n) \ |
michael@0 | 92 | do { \ |
michael@0 | 93 | (b)->read_limit -= (n); \ |
michael@0 | 94 | } while (0) |
michael@0 | 95 | /** Decrease the write limit of 'b' by 'n' bytes */ |
michael@0 | 96 | #define ev_token_bucket_decrement_write(b,n) \ |
michael@0 | 97 | do { \ |
michael@0 | 98 | (b)->write_limit -= (n); \ |
michael@0 | 99 | } while (0) |
michael@0 | 100 | |
michael@0 | 101 | #ifdef __cplusplus |
michael@0 | 102 | } |
michael@0 | 103 | #endif |
michael@0 | 104 | |
michael@0 | 105 | #endif |