2007-04-01 16:29:33 +05:30
|
|
|
/* 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 18:23:15 +05:30
|
|
|
* Copyright (C) 2007 Denys Vlasenko.
|
2007-04-01 16:29:33 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2008-03-17 14:47:27 +05:30
|
|
|
#include "tcpudp_perhost.h"
|
2007-04-01 06:48:20 +05:30
|
|
|
|
|
|
|
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 18:07:28 +05:30
|
|
|
unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp)
|
2007-04-01 06:48:20 +05:30
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
unsigned conn = 1;
|
2007-04-02 18:07:28 +05:30
|
|
|
int freepos = -1;
|
2007-04-01 06:48:20 +05:30
|
|
|
|
|
|
|
for (i = 0; i < cclen; ++i) {
|
2007-04-02 18:07:28 +05:30
|
|
|
if (!cc[i].ip) {
|
|
|
|
freepos = i;
|
2007-05-30 05:59:55 +05:30
|
|
|
continue;
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
2007-04-02 18:07:28 +05:30
|
|
|
if (strcmp(cc[i].ip, ip) == 0) {
|
2007-04-01 06:48:20 +05:30
|
|
|
conn++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2007-04-02 18:07:28 +05:30
|
|
|
if (freepos == -1) return 0;
|
2007-04-01 06:48:20 +05:30
|
|
|
if (conn <= maxconn) {
|
2007-04-02 18:07:28 +05:30
|
|
|
cc[freepos].ip = ip;
|
|
|
|
*hccpp = &cc[freepos];
|
2007-04-01 06:48:20 +05:30
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipsvd_perhost_remove(int pid)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < cclen; ++i) {
|
|
|
|
if (cc[i].pid == pid) {
|
2007-04-02 18:07:28 +05:30
|
|
|
free(cc[i].ip);
|
|
|
|
cc[i].ip = NULL;
|
2007-04-01 06:48:20 +05:30
|
|
|
cc[i].pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//void ipsvd_perhost_free(void)
|
|
|
|
//{
|
|
|
|
// free(cc);
|
|
|
|
//}
|