-
Notifications
You must be signed in to change notification settings - Fork 1
/
netio.c
216 lines (195 loc) · 6.8 KB
/
netio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* ryshttpd -- simple filesharing http server.
*
* ryshttpd is copyrighted:
* Copyright (C) 2018 Andrey Rys. All rights reserved.
*
* ryshttpd is licensed to you under the terms of std. MIT/X11 license:
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "httpd.h"
void io_socket_timeout(int fd, unsigned long rcvtimeo, unsigned long sndtimeo)
{
struct timeval tv;
unsigned long long t;
if (rcvtimeo > 0) {
t = rcvtimeo * 1000000ULL;
useconds_to_timeval(t, &tv);
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)) == -1) xerror("setting receive timeout");
}
if (sndtimeo > 0) {
t = sndtimeo * 1000000ULL;
useconds_to_timeval(t, &tv);
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(tv)) == -1) xerror("setting send timeout");
}
}
static void timespec_diff(const struct timespec *start, const struct timespec *stop, struct timespec *result)
{
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000UL;
}
else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
}
static void timespec_add(struct timespec *to, const struct timespec *add)
{
to->tv_sec += add->tv_sec;
to->tv_nsec += add->tv_nsec;
if (to->tv_nsec >= 1000000000UL) {
to->tv_nsec -= 1000000000UL;
to->tv_sec++;
}
}
/*
* Rate limiting is implemented as a simple userspace sleeping.
* The total limit is determined by a series of sent chunks
* with a series of added sleeps over them if user was downloading
* a chunk faster than a single time chunk permitted.
* Because client here is a simple forked process, it is permitted to sleep.
*/
static void ratelim_calculate(struct rate_limit *rl)
{
if (rl->calculated == YES) return;
if (rl->total == NOFSIZE) {
rl->chunk = 0;
rl->calculated = YES;
return;
}
if (rl->total < (rh_fsize)rh_rdwr_bufsize) rl->total = (rh_fsize)rh_rdwr_bufsize;
rl->nr_chk = RATELIM_START_CHUNKS;
rl->chunk = rl->total / rl->nr_chk;
while (rl->chunk >= (rh_fsize)rh_rdwr_bufsize) {
rl->nr_chk *= 2;
rl->chunk = rl->total / rl->nr_chk;
}
rl->calculated = YES;
}
static void ratelim_snapshot_time(struct timespec *tp)
{
clockid_t clkid;
#ifdef CLOCK_MONOTONIC_RAW
clkid = CLOCK_MONOTONIC_RAW;
#else
clkid = CLOCK_MONOTONIC;
#endif
rh_memzero(tp, sizeof(struct timespec));
if (clock_gettime(clkid, tp) == -1) xerror("clock_gettime");
}
static size_t io_ratelimit_data(rh_yesno write, struct client_info *clinfo, void *data, size_t szdata, rh_yesno noretry, rh_yesno nosleep)
{
size_t xszdata = szdata;
size_t done = 0, t;
struct rate_limit *rl;
struct timespec tps, tpd, tdiff;
rh_yesno do_stop = NO;
rh_yesno single = YES;
rl = write ? &clinfo->ralimitdown : &clinfo->ralimitup;
if (nosleep == NO) ratelim_calculate(rl);
if (rl->chunk > 0 && xszdata >= rl->chunk) {
do {
/* Single chunk or multiple (heavy transfer?) */
single = NO;
/* Take start time */
ratelim_snapshot_time(&tps);
/* Do send/recv a chunk */
t = write ?
io_write_data(clinfo->clfd,
data+(szdata-xszdata), rl->chunk, noretry, NULL)
: io_read_data(clinfo->clfd,
data+(szdata-xszdata), rl->chunk, noretry, NULL);
if (t == NOSIZE) return NOSIZE;
if (t < rl->chunk) do_stop = YES;
/* Record sent/recv'd amount */
done += t;
/* Take push/receive time */
ratelim_snapshot_time(&tpd);
/* Measure network times */
timespec_diff(&tps, &tpd, &tdiff);
/* If chunk push/receive was faster, then force sleep the remaining */
if (tdiff.tv_sec == 0
&& tdiff.tv_nsec < RATELIM_TIME_CHUNK(rl->nr_chk)) {
tps.tv_sec = 0;
tps.tv_nsec = RATELIM_TIME_CHUNK(rl->nr_chk) - tdiff.tv_nsec;
nanosleep(&tps, NULL);
}
if (do_stop) goto _done;
} while ((xszdata -= rl->chunk) >= rl->chunk);
}
if (xszdata) {
if (rl->chunk > 0) ratelim_snapshot_time(&tps);
t = write ?
io_write_data(clinfo->clfd,
data+(szdata-xszdata), xszdata, noretry, NULL)
: io_read_data(clinfo->clfd,
data+(szdata-xszdata), xszdata, noretry, NULL);
if (t == NOSIZE) return NOSIZE;
done += t;
if (rl->chunk > 0) {
ratelim_snapshot_time(&tpd);
timespec_diff(&tps, &tpd, &tdiff);
/* If single chunk, then accumulate it's time and size over calls */
if (single == YES) {
/* Save size */
rl->done += t;
/* Accumulate time */
timespec_add(&rl->doneacc, &tdiff);
/* If chunk size exceeded... */
if (rl->done >= rl->chunk) {
/* ... then measure time difference, and sleep remaining */
if (rl->doneacc.tv_sec == 0
&& tdiff.tv_nsec < RATELIM_TIME_CHUNK(rl->nr_chk)) {
tps.tv_sec = 0;
tps.tv_nsec = RATELIM_TIME_CHUNK(rl->nr_chk) - tdiff.tv_nsec;
nanosleep(&tps, NULL);
}
/* But if limit is not hit, still, reset the counters */
rl->done = 0;
rh_memzero(&rl->doneacc, sizeof(struct timespec));
}
}
else {
if (tdiff.tv_sec == 0
&& tdiff.tv_nsec < RATELIM_TIME_CHUNK_REM(rl->nr_chk, rl->chunk, xszdata)) {
tps.tv_sec = 0;
tps.tv_nsec = RATELIM_TIME_CHUNK_REM(rl->nr_chk, rl->chunk, xszdata) - tdiff.tv_nsec;
nanosleep(&tps, NULL);
}
}
}
}
_done: return done;
}
/*
* noretry: if returned less than requested, then return the number, otherwise try to get anything
* nosleep: do not apply the rate limiting mechanism because we're sending headers etc.
*/
size_t io_recv_data(struct client_info *clinfo, void *data, size_t szdata, rh_yesno noretry, rh_yesno nosleep)
{
return io_ratelimit_data(NO, clinfo, data, szdata, noretry, nosleep);
}
size_t io_send_data(struct client_info *clinfo, const void *data, size_t szdata, rh_yesno noretry, rh_yesno nosleep)
{
/* yeah, const void * -> void * looks ugly, but saves exec size */
return io_ratelimit_data(YES, clinfo, (void *)data, szdata, noretry, nosleep);
}