2007-04-01 10:59:33 +00: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/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Denis Vlasenko.
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2007-04-01 01:18:20 +00:00
|
|
|
#include "ipsvd_perhost.h"
|
|
|
|
|
|
|
|
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 12:37:28 +00:00
|
|
|
unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp)
|
2007-04-01 01:18:20 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
unsigned conn = 1;
|
2007-04-02 12:37:28 +00:00
|
|
|
int freepos = -1;
|
2007-04-01 01:18:20 +00:00
|
|
|
|
|
|
|
for (i = 0; i < cclen; ++i) {
|
2007-04-02 12:37:28 +00:00
|
|
|
if (!cc[i].ip) {
|
|
|
|
freepos = i;
|
2007-05-30 00:29:55 +00:00
|
|
|
continue;
|
2007-04-01 01:18:20 +00:00
|
|
|
}
|
2007-04-02 12:37:28 +00:00
|
|
|
if (strcmp(cc[i].ip, ip) == 0) {
|
2007-04-01 01:18:20 +00:00
|
|
|
conn++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-04-02 12:37:28 +00:00
|
|
|
if (freepos == -1) return 0;
|
2007-04-01 01:18:20 +00:00
|
|
|
if (conn <= maxconn) {
|
2007-04-02 12:37:28 +00:00
|
|
|
cc[freepos].ip = ip;
|
|
|
|
*hccpp = &cc[freepos];
|
2007-04-01 01:18:20 +00: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 12:37:28 +00:00
|
|
|
free(cc[i].ip);
|
|
|
|
cc[i].ip = NULL;
|
2007-04-01 01:18:20 +00:00
|
|
|
cc[i].pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void ipsvd_perhost_free(void)
|
|
|
|
//{
|
|
|
|
// free(cc);
|
|
|
|
//}
|