transmission 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ /******************************************************************************
2
+ * $Id: ratecontrol.c 261 2006-05-29 21:27:31Z titer $
3
+ *
4
+ * Copyright (c) 2006 Transmission authors and contributors
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ *****************************************************************************/
24
+
25
+ #include "transmission.h"
26
+
27
+ #define MAX_HISTORY 30
28
+
29
+ typedef struct tr_transfer_s tr_transfer_t;
30
+ struct tr_ratecontrol_s
31
+ {
32
+ tr_lock_t lock;
33
+ int limit;
34
+ tr_transfer_t * first;
35
+ tr_transfer_t * last;
36
+ };
37
+ struct tr_transfer_s
38
+ {
39
+ uint64_t date;
40
+ int size;
41
+ tr_transfer_t * next;
42
+ tr_transfer_t * prev;
43
+ };
44
+
45
+ /***********************************************************************
46
+ * rateForInterval
47
+ ***********************************************************************
48
+ * Returns the transfer rate on the last 'interval' milliseconds
49
+ **********************************************************************/
50
+ static inline float rateForInterval( tr_ratecontrol_t * r, int interval )
51
+ {
52
+ tr_transfer_t * t;
53
+ uint64_t start = tr_date() - interval;
54
+ int total = 0;
55
+
56
+ for( t = r->first; t && t->date > start; t = t->next )
57
+ {
58
+ total += t->size;
59
+ }
60
+
61
+ return ( 1000.0 / 1024.0 ) * total / interval;
62
+ }
63
+
64
+ static inline void cleanOldTransfers( tr_ratecontrol_t * r )
65
+ {
66
+ tr_transfer_t * t, * prev;
67
+ uint64_t old = tr_date() - MAX_HISTORY * 1000;
68
+
69
+ for( t = r->last; t && t->date < old; )
70
+ {
71
+ prev = t->prev;
72
+ if( prev )
73
+ prev->next = NULL;
74
+ else
75
+ r->first = NULL;
76
+ free( t );
77
+ t = prev;
78
+ r->last = prev;
79
+ }
80
+ }
81
+
82
+ tr_ratecontrol_t * tr_rcInit()
83
+ {
84
+ tr_ratecontrol_t * r;
85
+
86
+ r = calloc( sizeof( tr_ratecontrol_t ), 1 );
87
+ r->limit = -1;
88
+ tr_lockInit( &r->lock );
89
+
90
+ return r;
91
+ }
92
+
93
+ void tr_rcSetLimit( tr_ratecontrol_t * r, int limit )
94
+ {
95
+ tr_lockLock( &r->lock );
96
+ r->limit = limit;
97
+ tr_lockUnlock( &r->lock );
98
+ }
99
+
100
+ int tr_rcCanTransfer( tr_ratecontrol_t * r )
101
+ {
102
+ int ret;
103
+
104
+ tr_lockLock( &r->lock );
105
+ ret = ( r->limit < 0 ) || ( rateForInterval( r, 1000 ) < r->limit );
106
+ tr_lockUnlock( &r->lock );
107
+
108
+ return ret;
109
+ }
110
+
111
+ void tr_rcTransferred( tr_ratecontrol_t * r, int size )
112
+ {
113
+ tr_transfer_t * t;
114
+
115
+ if( size < 100 )
116
+ {
117
+ return;
118
+ }
119
+
120
+ tr_lockLock( &r->lock );
121
+ t = malloc( sizeof( tr_transfer_t ) );
122
+
123
+ if( r->first )
124
+ r->first->prev = t;
125
+ if( !r->last )
126
+ r->last = t;
127
+ t->next = r->first;
128
+ t->prev = NULL;
129
+ r->first = t;
130
+
131
+ t->date = tr_date();
132
+ t->size = size;
133
+
134
+ cleanOldTransfers( r );
135
+ tr_lockUnlock( &r->lock );
136
+ }
137
+
138
+ float tr_rcRate( tr_ratecontrol_t * r )
139
+ {
140
+ float ret;
141
+
142
+ tr_lockLock( &r->lock );
143
+ ret = rateForInterval( r, MAX_HISTORY * 1000 );
144
+ tr_lockUnlock( &r->lock );
145
+
146
+ return ret;
147
+ }
148
+
149
+ void tr_rcReset( tr_ratecontrol_t * r )
150
+ {
151
+ tr_transfer_t * t, * next;
152
+
153
+ tr_lockLock( &r->lock );
154
+ for( t = r->first; t; )
155
+ {
156
+ next = t->next;
157
+ free( t );
158
+ t = next;
159
+ }
160
+ r->first = NULL;
161
+ r->last = NULL;
162
+ tr_lockUnlock( &r->lock );
163
+ }
164
+
165
+ void tr_rcClose( tr_ratecontrol_t * r )
166
+ {
167
+ tr_rcReset( r );
168
+ tr_lockClose( &r->lock );
169
+ free( r );
170
+ }
@@ -0,0 +1,33 @@
1
+ /******************************************************************************
2
+ * $Id: ratecontrol.h 261 2006-05-29 21:27:31Z titer $
3
+ *
4
+ * Copyright (c) 2006 Transmission authors and contributors
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ * DEALINGS IN THE SOFTWARE.
23
+ *****************************************************************************/
24
+
25
+ typedef struct tr_ratecontrol_s tr_ratecontrol_t;
26
+
27
+ tr_ratecontrol_t * tr_rcInit();
28
+ void tr_rcSetLimit( tr_ratecontrol_t *, int );
29
+ int tr_rcCanTransfer( tr_ratecontrol_t * );
30
+ void tr_rcTransferred( tr_ratecontrol_t *, int );
31
+ float tr_rcRate( tr_ratecontrol_t * );
32
+ void tr_rcReset( tr_ratecontrol_t * );
33
+ void tr_rcClose( tr_ratecontrol_t * );
@@ -0,0 +1,235 @@
1
+ /*
2
+ sha1.c: Implementation of SHA-1 Secure Hash Algorithm-1
3
+
4
+ Based upon: NIST FIPS180-1 Secure Hash Algorithm-1
5
+ http://www.itl.nist.gov/fipspubs/fip180-1.htm
6
+
7
+ Non-official Japanese Translation by HIRATA Yasuyuki:
8
+ http://yasu.asuka.net/translations/SHA-1.html
9
+
10
+ Copyright (C) 2002 vi@nwr.jp. All rights reserved.
11
+
12
+ This software is provided 'as-is', without any express or implied
13
+ warranty. In no event will the authors be held liable for any damages
14
+ arising from the use of this software.
15
+
16
+ Permission is granted to anyone to use this software for any purpose,
17
+ including commercial applications, and to alter it and redistribute it
18
+ freely, subject to the following restrictions:
19
+
20
+ 1. The origin of this software must not be misrepresented; you must not
21
+ claim that you wrote the original software. If you use this software
22
+ in a product, an acknowledgement in the product documentation would be
23
+ appreciated but is not required.
24
+ 2. Altered source versions must be plainly marked as such, and must not be
25
+ misrepresented as beging the original software.
26
+ 3. This notice may not be removed or altered from any source distribution.
27
+
28
+ Note:
29
+ The copyright notice above is copied from md5.h by L. Peter Deutsch
30
+ <ghost@aladdin.com>. Thank him since I'm not a good speaker of English. :)
31
+ */
32
+ #include <string.h>
33
+ #include "sha1.h"
34
+
35
+ #define INLINE inline
36
+ /*
37
+ * Packing bytes to a word
38
+ *
39
+ * Should not assume p is aligned to word boundary
40
+ */
41
+ static INLINE sha1_word_t packup(sha1_byte_t *p)
42
+ {
43
+ /* Portable, but slow */
44
+ return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3] << 0;
45
+ }
46
+
47
+ /*
48
+ * Unpacking a word to bytes
49
+ *
50
+ * Should not assume p is aligned to word boundary
51
+ */
52
+ static void unpackup(sha1_byte_t *p, sha1_word_t q)
53
+ {
54
+ p[0] = (q >> 24) & 0xff;
55
+ p[1] = (q >> 16) & 0xff;
56
+ p[2] = (q >> 8) & 0xff;
57
+ p[3] = (q >> 0) & 0xff;
58
+ }
59
+
60
+ /*
61
+ * Processing a block
62
+ */
63
+ static inline void sha1_update_now(sha1_state_s *pms, sha1_byte_t *bp)
64
+ {
65
+ sha1_word_t tmp, a, b, c, d, e, w[16+16];
66
+ int i, s;
67
+
68
+ /* pack 64 bytes into 16 words */
69
+ for(i = 0; i < 16; i++) {
70
+ w[i] = packup(bp + i * sizeof(sha1_word_t));
71
+ }
72
+ memcpy(w + 16, w + 0, sizeof(sha1_word_t) * 16);
73
+
74
+ a = pms->sha1_h[0], b = pms->sha1_h[1], c = pms->sha1_h[2], d = pms->sha1_h[3], e = pms->sha1_h[4];
75
+
76
+ #define rot(x,n) (((x) << n) | ((x) >> (32-n)))
77
+ #define f0(b, c, d) ((b&c)|(~b&d))
78
+ #define f1(b, c, d) (b^c^d)
79
+ #define f2(b, c, d) ((b&c)|(b&d)|(c&d))
80
+ #define f3(b, c, d) (b^c^d)
81
+ #define k0 0x5a827999
82
+ #define k1 0x6ed9eba1
83
+ #define k2 0x8f1bbcdc
84
+ #define k3 0xca62c1d6
85
+
86
+ /* t=0-15 */
87
+ s = 0;
88
+ for(i = 0; i < 16; i++) {
89
+ tmp = rot(a, 5) + f0(b, c, d) + e + w[s] + k0;
90
+ e = d; d = c; c = rot(b, 30); b = a; a = tmp;
91
+ s = (s + 1) % 16;
92
+ }
93
+
94
+ /* t=16-19 */
95
+ for(i = 16; i < 20; i++) {
96
+ w[s] = rot(w[s+13] ^ w[s+8] ^ w[s+2] ^ w[s], 1);
97
+ w[s+16] = w[s];
98
+ tmp = rot(a, 5) + f0(b, c, d) + e + w[s] + k0;
99
+ e = d; d = c; c = rot(b, 30); b = a; a = tmp;
100
+ s = (s + 1) % 16;
101
+ }
102
+
103
+ /* t=20-39 */
104
+ for(i = 0; i < 20; i++) {
105
+ w[s] = rot(w[s+13] ^ w[s+8] ^ w[s+2] ^ w[s], 1);
106
+ w[s+16] = w[s];
107
+ tmp = rot(a, 5) + f1(b, c, d) + e + w[s] + k1;
108
+ e = d; d = c; c = rot(b, 30); b = a; a = tmp;
109
+ s = (s + 1) % 16;
110
+ }
111
+
112
+ /* t=40-59 */
113
+ for(i = 0; i < 20; i++) {
114
+ w[s] = rot(w[s+13] ^ w[s+8] ^ w[s+2] ^ w[s], 1);
115
+ w[s+16] = w[s];
116
+ tmp = rot(a, 5) + f2(b, c, d) + e + w[s] + k2;
117
+ e = d; d = c; c = rot(b, 30); b = a; a = tmp;
118
+ s = (s + 1) % 16;
119
+ }
120
+
121
+ /* t=60-79 */
122
+ for(i = 0; i < 20; i++) {
123
+ w[s] = rot(w[s+13] ^ w[s+8] ^ w[s+2] ^ w[s], 1);
124
+ w[s+16] = w[s];
125
+ tmp = rot(a, 5) + f3(b, c, d) + e + w[s] + k3;
126
+ e = d; d = c; c = rot(b, 30); b = a; a = tmp;
127
+ s = (s + 1) % 16;
128
+ }
129
+
130
+ pms->sha1_h[0] += a, pms->sha1_h[1] += b, pms->sha1_h[2] += c, pms->sha1_h[3] += d, pms->sha1_h[4] += e;
131
+ }
132
+
133
+ /*
134
+ * Increment sha1_size1, sha1_size2 field of sha1_state_s
135
+ */
136
+ static INLINE void incr(sha1_state_s *pms, int v)
137
+ {
138
+ sha1_word_t q;
139
+
140
+ q = pms->sha1_size1 + v * BITS;
141
+ if(q < pms->sha1_size1) {
142
+ pms->sha1_size2++;
143
+ }
144
+ pms->sha1_size1 = q;
145
+ }
146
+
147
+ /*
148
+ * Initialize sha1_state_s as FIPS specifies
149
+ */
150
+ void sha1_init(sha1_state_s *pms)
151
+ {
152
+ memset(pms, 0, sizeof(*pms));
153
+ pms->sha1_h[0] = 0x67452301; /* Initialize H[0]-H[4] */
154
+ pms->sha1_h[1] = 0xEFCDAB89;
155
+ pms->sha1_h[2] = 0x98BADCFE;
156
+ pms->sha1_h[3] = 0x10325476;
157
+ pms->sha1_h[4] = 0xC3D2E1F0;
158
+ }
159
+
160
+ /*
161
+ * Fill block and update output when needed
162
+ */
163
+ void sha1_update(sha1_state_s *pms, sha1_byte_t *bufp, int length)
164
+ {
165
+ /* Is the buffer partially filled? */
166
+ if(pms->sha1_count != 0) {
167
+ if(pms->sha1_count + length >= (signed) sizeof(pms->sha1_buf)) { /* buffer is filled enough */
168
+ int fil = sizeof(pms->sha1_buf) - pms->sha1_count; /* length to copy */
169
+
170
+ memcpy(pms->sha1_buf + pms->sha1_count, bufp, fil);
171
+ sha1_update_now(pms, pms->sha1_buf);
172
+ length -= fil;
173
+ bufp += fil;
174
+ pms->sha1_count = 0;
175
+ incr(pms, fil);
176
+ } else {
177
+ memcpy(pms->sha1_buf + pms->sha1_count, bufp, length);
178
+ pms->sha1_count += length;
179
+ incr(pms, length);
180
+ return;
181
+ }
182
+ }
183
+
184
+ /* Loop to update state */
185
+ for(;;) {
186
+ if(length < (signed) sizeof(pms->sha1_buf)) { /* Short to fill up the buffer */
187
+ if(length) {
188
+ memcpy(pms->sha1_buf, bufp, length);
189
+ }
190
+ pms->sha1_count = length;
191
+ incr(pms, length);
192
+ break;
193
+ }
194
+ sha1_update_now(pms, bufp);
195
+ length -= sizeof(pms->sha1_buf);
196
+ bufp += sizeof(pms->sha1_buf);
197
+ incr(pms, sizeof(pms->sha1_buf));
198
+ }
199
+ }
200
+
201
+ void sha1_finish(sha1_state_s *pms, sha1_byte_t output[SHA1_OUTPUT_SIZE])
202
+ {
203
+ int i;
204
+ sha1_byte_t buf[1];
205
+
206
+ /* fill a bit */
207
+ buf[0] = 0x80;
208
+ sha1_update(pms, buf, 1);
209
+
210
+ /* Decrement sha1_size1, sha1_size2 */
211
+ if((pms->sha1_size1 -= BITS) == 0) {
212
+ pms->sha1_size2--;
213
+ }
214
+
215
+ /* fill zeros */
216
+ if(pms->sha1_count > (signed) (sizeof(pms->sha1_buf) - 2 * sizeof(sha1_word_t))) {
217
+ memset(pms->sha1_buf + pms->sha1_count, 0, sizeof(pms->sha1_buf) - pms->sha1_count);
218
+ sha1_update_now(pms, pms->sha1_buf);
219
+ pms->sha1_count = 0;
220
+ }
221
+ memset(pms->sha1_buf + pms->sha1_count, 0,
222
+ sizeof(pms->sha1_buf) - pms->sha1_count - sizeof(sha1_word_t) * 2);
223
+
224
+ /* fill last length */
225
+ unpackup(pms->sha1_buf + sizeof(pms->sha1_buf) - sizeof(sha1_word_t) * 2, pms->sha1_size2);
226
+ unpackup(pms->sha1_buf + sizeof(pms->sha1_buf) - sizeof(sha1_word_t) * 1, pms->sha1_size1);
227
+
228
+ /* final update */
229
+ sha1_update_now(pms, pms->sha1_buf);
230
+
231
+ /* move hash value to output byte array */
232
+ for(i = 0; i < (signed) (sizeof(pms->sha1_h)/sizeof(sha1_word_t)); i++) {
233
+ unpackup(output + i * sizeof(sha1_word_t), pms->sha1_h[i]);
234
+ }
235
+ }
@@ -0,0 +1,68 @@
1
+ /*
2
+ sha1.h: Implementation of SHA-1 Secure Hash Algorithm-1
3
+
4
+ Based upon: NIST FIPS180-1 Secure Hash Algorithm-1
5
+ http://www.itl.nist.gov/fipspubs/fip180-1.htm
6
+
7
+ Non-official Japanese Translation by HIRATA Yasuyuki:
8
+ http://yasu.asuka.net/translations/SHA-1.html
9
+
10
+ Copyright (C) 2002 vi@nwr.jp. All rights reserved.
11
+
12
+ This software is provided 'as-is', without any express or implied
13
+ warranty. In no event will the authors be held liable for any damages
14
+ arising from the use of this software.
15
+
16
+ Permission is granted to anyone to use this software for any purpose,
17
+ including commercial applications, and to alter it and redistribute it
18
+ freely, subject to the following restrictions:
19
+
20
+ 1. The origin of this software must not be misrepresented; you must not
21
+ claim that you wrote the original software. If you use this software
22
+ in a product, an acknowledgement in the product documentation would be
23
+ appreciated but is not required.
24
+ 2. Altered source versions must be plainly marked as such, and must not be
25
+ misrepresented as beging the original software.
26
+ 3. This notice may not be removed or altered from any source distribution.
27
+
28
+ Note:
29
+ The copyright notice above is copied from md5.h by L. Petet Deutsch
30
+ <ghost@aladdin.com>. Thank him since I'm not a good speaker of English. :)
31
+ */
32
+ #ifndef SHA1_H
33
+ #define SHA1_H
34
+
35
+ typedef unsigned int sha1_word_t; /* 32bits unsigned integer */
36
+ typedef unsigned char sha1_byte_t; /* 8bits unsigned integer */
37
+ #define BITS 8
38
+
39
+ /* Define the state of SHA-1 algorithm */
40
+ typedef struct {
41
+ sha1_byte_t sha1_buf[64]; /* 512 bits */
42
+ int sha1_count; /* How many bytes are used */
43
+ sha1_word_t sha1_size1; /* Length counter Lower Word */
44
+ sha1_word_t sha1_size2; /* Length counter Upper Word */
45
+ sha1_word_t sha1_h[5]; /* Hash output */
46
+ } sha1_state_s;
47
+ #define SHA1_OUTPUT_SIZE 20 /* in bytes */
48
+
49
+ /* External Functions */
50
+
51
+ #ifdef __cplusplus
52
+ extern "C" {
53
+ #endif
54
+
55
+ /* Initialize SHA-1 algorithm */
56
+ void sha1_init(sha1_state_s *pms);
57
+
58
+ /* Append a string to SHA-1 algorithm */
59
+ void sha1_update(sha1_state_s *pms, sha1_byte_t *input_buffer, int length);
60
+
61
+ /* Finish the SHA-1 algorithm and return the hash */
62
+ void sha1_finish(sha1_state_s *pms, sha1_byte_t output[SHA1_OUTPUT_SIZE]);
63
+
64
+ #ifdef __cplusplus
65
+ }
66
+ #endif
67
+
68
+ #endif