2007-04-01 18:59:33 +08:00
|
|
|
/* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
|
|
|
|
* which are released into public domain by the author.
|
|
|
|
* Homepage: http://smarden.sunsite.dk/ipsvd/
|
|
|
|
*
|
2008-03-02 20:53:15 +08:00
|
|
|
* Copyright (C) 2007 Denys Vlasenko.
|
2007-04-01 18:59:33 +08:00
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 03:00:18 +08:00
|
|
|
#include "libbb.h"
|
2008-03-17 17:17:27 +08:00
|
|
|
#include "tcpudp_perhost.h"
|
2007-04-01 09:18:20 +08:00
|
|
|
|
|
|
|
static struct hcc *cc;
|
|
|
|
static unsigned cclen;
|
|
|
|
|
|
|
|
/* to be optimized */
|
|
|
|
|
|
|
|
void ipsvd_perhost_init(unsigned c)
|
|
|
|
{
|
|
|
|
// free(cc);
|
|
|
|
cc = xzalloc(c * sizeof(*cc));
|
|
|
|
cclen = c;
|
|
|
|
}
|
|
|
|
|
2007-04-02 20:37:28 +08:00
|
|
|
unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp)
|
2007-04-01 09:18:20 +08:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
unsigned conn = 1;
|
2007-04-02 20:37:28 +08:00
|
|
|
int freepos = -1;
|
2007-04-01 09:18:20 +08:00
|
|
|
|
|
|
|
for (i = 0; i < cclen; ++i) {
|
2007-04-02 20:37:28 +08:00
|
|
|
if (!cc[i].ip) {
|
|
|
|
freepos = i;
|
2007-05-30 08:29:55 +08:00
|
|
|
continue;
|
2007-04-01 09:18:20 +08:00
|
|
|
}
|
2007-04-02 20:37:28 +08:00
|
|
|
if (strcmp(cc[i].ip, ip) == 0) {
|
2007-04-01 09:18:20 +08:00
|
|
|
conn++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-04-02 20:37:28 +08:00
|
|
|
if (freepos == -1) return 0;
|
2007-04-01 09:18:20 +08:00
|
|
|
if (conn <= maxconn) {
|
2007-04-02 20:37:28 +08:00
|
|
|
cc[freepos].ip = ip;
|
|
|
|
*hccpp = &cc[freepos];
|
2007-04-01 09:18:20 +08:00
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipsvd_perhost_remove(int pid)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < cclen; ++i) {
|
|
|
|
if (cc[i].pid == pid) {
|
2007-04-02 20:37:28 +08:00
|
|
|
free(cc[i].ip);
|
|
|
|
cc[i].ip = NULL;
|
2007-04-01 09:18:20 +08:00
|
|
|
cc[i].pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void ipsvd_perhost_free(void)
|
|
|
|
//{
|
|
|
|
// free(cc);
|
|
|
|
//}
|