2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
47 unsigned int len, hash;
57 double level, last, etime, wtime;
58 typedbuf(struct waiting) brim;
68 double size, rate, retain, warnrate;
72 static struct bucket *sbuckets[1 << SBUCKETS];
73 static struct bucket **buckets = sbuckets;
74 static int hashlen = SBUCKETS, nbuckets = 0;
75 static typedbuf(struct btime) timeheap;
76 static int child, reload;
78 static const struct config defcfg = {
79 .size = 100, .rate = 10, .warnrate = 60,
80 .retain = 10, .brimsize = 10,
82 static struct config cf;
84 static double rtime(void)
87 static struct timespec or;
90 clock_gettime(CLOCK_MONOTONIC, &ts);
95 return((ts.tv_sec - or.tv_sec) + ((ts.tv_nsec - or.tv_nsec) / 1000000000.0));
98 static struct source reqsource(struct hthead *req)
106 ret = (struct source){};
107 if((sa = getheader(req, "X-Ash-Address")) != NULL) {
108 if(inet_pton(AF_INET, sa, &a4) == 1) {
110 memcpy(ret.data, &a4, ret.len = sizeof(a4));
111 } else if(inet_pton(AF_INET6, sa, &a6) == 1) {
113 memcpy(ret.data, &a6, ret.len = sizeof(a6));
116 for(i = 0, ret.hash = ret.type; i < ret.len; i++)
117 ret.hash = (ret.hash * 31) + ret.data[i];
121 static int srccmp(const struct source *a, const struct source *b)
125 if((c = a->len - b->len) != 0)
127 if((c = a->type - b->type) != 0)
129 return(memcmp(a->data, b->data, a->len));
132 static const char *formatsrc(const struct source *src)
134 static char buf[128];
140 memcpy(&a4, src->data, sizeof(a4));
141 if(!inet_ntop(AF_INET, &a4, buf, sizeof(buf)))
142 return("<invalid ipv4>");
145 memcpy(&a6, src->data, sizeof(a6));
146 if(!inet_ntop(AF_INET6, &a6, buf, sizeof(buf)))
147 return("<invalid ipv6>");
150 return("<invalid source record>");
154 static void rehash(int nlen)
156 unsigned int i, o, n, m, pl, nl;
157 struct bucket **new, **old;
160 if(nlen <= SBUCKETS) {
164 new = smalloc(sizeof(*new) * (1 << nlen));
168 memset(new, 0, sizeof(*new) * (1 << nlen));
170 pl = 1 << hashlen; nl = 1 << nlen; m = nl - 1;
171 for(i = 0; i < pl; i++) {
174 for(o = old[i]->id.hash & m, n = 0; n < nl; o = (o + 1) & m, n++) {
187 static struct bucket *hashget(const struct source *src)
189 unsigned int i, n, N, m;
192 m = (N = (1 << hashlen)) - 1;
193 for(i = src->hash & m, n = 0; n < N; i = (i + 1) & m, n++) {
195 if(bk && !srccmp(&bk->id, src))
198 for(i = src->hash & m; buckets[i]; i = (i + 1) & m);
199 buckets[i] = bk = szmalloc(sizeof(*bk));
200 memcpy(&bk->id, src, sizeof(*src));
201 bk->last = bk->etime = now;
204 if(++nbuckets > (1 << (hashlen - 1)))
209 static void hashdel(struct bucket *bk)
211 unsigned int i, o, p, n, N, m;
214 m = (N = (1 << hashlen)) - 1;
215 for(i = bk->id.hash & m, n = 0; n < N; i = (i + 1) & m, n++) {
216 assert((sb = buckets[i]) != NULL);
217 if(!srccmp(&sb->id, &bk->id))
222 for(o = (i + 1) & m; buckets[o] != NULL; o = (o + 1) & m) {
224 p = (sb->id.hash - i) & m;
225 if((p == 0) || (p > ((o - i) & m))) {
231 if(--nbuckets <= (1 << (hashlen - 3)))
235 static void thraise(struct btime bt, int n)
241 if(timeheap.b[p].tm <= bt.tm)
243 (timeheap.b[n] = timeheap.b[p]).bk->thpos = n;
246 (timeheap.b[n] = bt).bk->thpos = n;
249 static void thlower(struct btime bt, int n)
254 c2 = (c1 = (n << 1) + 1) + 1;
257 c = ((c2 < timeheap.d) && (timeheap.b[c2].tm < timeheap.b[c1].tm)) ? c2 : c1;
258 if(timeheap.b[c].tm > bt.tm)
260 (timeheap.b[n] = timeheap.b[c]).bk->thpos = n;
263 (timeheap.b[n] = bt).bk->thpos = n;
266 static void thadjust(struct btime bt, int n)
268 if((n > 0) && (timeheap.b[(n - 1) >> 1].tm > bt.tm))
274 static void freebucket(struct bucket *bk)
280 if((n = bk->thpos) >= 0) {
281 r = timeheap.b[--timeheap.d];
285 for(i = 0; i < bk->brim.d; i++) {
286 freehthead(bk->brim.b[i].req);
287 close(bk->brim.b[i].fd);
293 static void updbtime(struct bucket *bk)
297 tm = (bk->level == 0) ? (bk->etime + cf.retain) : (bk->last + (bk->level / cf.rate) + cf.retain);
298 if((bk->blocked > 0) && ((tm2 = bk->wtime + cf.warnrate) > tm))
301 if((bk->brim.d > 0) && ((tm2 = bk->last + ((bk->level - cf.size) / cf.rate)) < tm))
303 if((bk->blocked > 0) && ((tm2 = bk->wtime + cf.warnrate) < tm))
307 sizebuf(timeheap, ++timeheap.d);
308 thraise((struct btime){bk, tm}, timeheap.d - 1);
310 thadjust((struct btime){bk, tm}, bk->thpos);
314 static void tickbucket(struct bucket *bk)
318 delta = now - bk->last;
321 if((bk->level -= delta * cf.rate) < 0) {
323 bk->etime = now + (bk->level / cf.rate);
326 while((bk->brim.d > 0) && (bk->level < cf.size)) {
327 if(sendreq(child, bk->brim.b[0].req, bk->brim.b[0].fd)) {
328 flog(LOG_ERR, "ratequeue: could not pass request to child: %s", strerror(errno));
331 freehthead(bk->brim.b[0].req);
332 close(bk->brim.b[0].fd);
336 if((bk->blocked > 0) && (now - bk->wtime >= cf.warnrate)) {
337 flog(LOG_NOTICE, "ratequeue: blocked %i requests from %s", bk->blocked, formatsrc(&bk->id));
343 static void checkbtime(struct bucket *bk)
346 if((bk->level == 0) && (now >= bk->etime + cf.retain) && (bk->blocked <= 0)) {
353 static void serve(struct hthead *req, int fd)
359 src = reqsource(req);
362 if(bk->level < cf.size) {
364 if(sendreq(child, req, fd)) {
365 flog(LOG_ERR, "ratequeue: could not pass request to child: %s", strerror(errno));
370 } else if(bk->brim.d < cf.brimsize) {
371 bufadd(bk->brim, ((struct waiting){.req = req, .fd = fd}));
373 if(bk->blocked < 0) {
374 flog(LOG_NOTICE, "ratequeue: blocking requests from %s", formatsrc(&bk->id));
378 simpleerror(fd, 429, "Too many requests", "Your client is being throttled for issuing too frequent requests.");
386 static int parseint(const char *str, int *dst)
391 buf = strtol(str, &p, 0);
398 static int parsefloat(const char *str, double *dst)
403 buf = strtod(str, &p);
410 static int readconf(char *path, struct config *buf)
416 if((fp = fopen(path, "r")) == NULL) {
417 flog(LOG_ERR, "ratequeue: %s: %s", path, strerror(errno));
421 s = mkcfparser(fp, path);
425 if(!strcmp(s->argv[0], "eof")) {
427 } else if(!strcmp(s->argv[0], "size")) {
428 if((s->argc < 2) || parsefloat(s->argv[1], &buf->size)) {
429 flog(LOG_ERR, "%s:%i: missing or invalid `size' argument");
432 } else if(!strcmp(s->argv[0], "rate")) {
433 if((s->argc < 2) || parsefloat(s->argv[1], &buf->rate)) {
434 flog(LOG_ERR, "%s:%i: missing or invalid `rate' argument");
437 } else if(!strcmp(s->argv[0], "brim")) {
438 if((s->argc < 2) || parseint(s->argv[1], &buf->brimsize)) {
439 flog(LOG_ERR, "%s:%i: missing or invalid `brim' argument");
443 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
453 static void huphandler(int sig)
458 static void usage(FILE *out)
460 fprintf(out, "usage: ratequeue [-h] [-s BUCKET-SIZE] [-r RATE] [-b BRIM-SIZE] PROGRAM [ARGS...]\n");
463 int main(int argc, char **argv)
475 while((c = getopt(argc, argv, "+hc:s:r:b:")) >= 0) {
484 parsefloat(optarg, &cf.size);
487 parsefloat(optarg, &cf.rate);
490 parseint(optarg, &cf.brimsize);
494 if(argc - optind < 1) {
499 if(readconf(cfname, &cfbuf))
503 if((child = stdmkchild(argv + optind, NULL, NULL)) < 0) {
504 flog(LOG_ERR, "ratequeue: could not fork child: %s", strerror(errno));
507 sigaction(SIGHUP, &(struct sigaction){.sa_handler = huphandler}, NULL);
511 if(!readconf(cfname, &cfbuf))
517 pfd = (struct pollfd){.fd = 0, .events = POLLIN};
518 timeout = (timeheap.d > 0) ? timeheap.b[0].tm : -1;
519 if((rv = poll(&pfd, 1, (timeout < 0) ? -1 : (int)((timeout + 0.1 - now) * 1000))) < 0) {
521 flog(LOG_ERR, "ratequeue: error in poll: %s", strerror(errno));
526 if((fd = recvreq(0, &req)) < 0) {
530 flog(LOG_ERR, "recvreq: %s", strerror(errno));
535 while((timeheap.d > 0) && ((now = rtime()) >= timeheap.b[0].tm))
536 checkbtime(timeheap.b[0].bk);