blob: d4c0d795fca062b3f7eaad0564b6512dae08a069 [file] [log] [blame]
Denis Vlasenko57a426b2007-04-01 10:59:33 +00001/* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
2 * which are released into public domain by the author.
3 * Homepage: http://smarden.sunsite.dk/ipsvd/
4 *
5 * Copyright (C) 2007 Denis Vlasenko.
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Denis Vlasenko2856dab2007-04-01 01:18:20 +000011#include "ipsvd_perhost.h"
12
13static struct hcc *cc;
14static unsigned cclen;
15
16/* to be optimized */
17
18void ipsvd_perhost_init(unsigned c)
19{
20// free(cc);
21 cc = xzalloc(c * sizeof(*cc));
22 cclen = c;
23}
24
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000025unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp)
Denis Vlasenko2856dab2007-04-01 01:18:20 +000026{
27 unsigned i;
28 unsigned conn = 1;
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000029 int freepos = -1;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000030
31 for (i = 0; i < cclen; ++i) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000032 if (!cc[i].ip) {
33 freepos = i;
Denis Vlasenko4b924f32007-05-30 00:29:55 +000034 continue;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000035 }
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000036 if (strcmp(cc[i].ip, ip) == 0) {
Denis Vlasenko2856dab2007-04-01 01:18:20 +000037 conn++;
38 continue;
39 }
40 }
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000041 if (freepos == -1) return 0;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000042 if (conn <= maxconn) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000043 cc[freepos].ip = ip;
44 *hccpp = &cc[freepos];
Denis Vlasenko2856dab2007-04-01 01:18:20 +000045 }
46 return conn;
47}
48
49void ipsvd_perhost_remove(int pid)
50{
51 unsigned i;
52 for (i = 0; i < cclen; ++i) {
53 if (cc[i].pid == pid) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000054 free(cc[i].ip);
55 cc[i].ip = NULL;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000056 cc[i].pid = 0;
57 return;
58 }
59 }
60}
61
62//void ipsvd_perhost_free(void)
63//{
64// free(cc);
65//}