blob: 10541088379ac75de5efad89a88a060d6a33212e [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 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko.
Denis Vlasenko57a426b2007-04-01 10:59:33 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko57a426b2007-04-01 10:59:33 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Denis Vlasenko4866e902008-03-17 09:17:27 +000011#include "tcpudp_perhost.h"
Denis Vlasenko2856dab2007-04-01 01:18:20 +000012
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//}