1
2
3
4
5
6
7
8
9
10#include "libbb.h"
11#include "tcpudp_perhost.h"
12
13struct hcc* FAST_FUNC ipsvd_perhost_init(unsigned c)
14{
15
16 struct hcc *cc = xzalloc((c + 1) * sizeof(*cc));
17 cc[c].pid = -1;
18 return cc;
19}
20
21unsigned FAST_FUNC ipsvd_perhost_add(struct hcc *cc, char *ip, unsigned maxconn, struct hcc **hccpp)
22{
23 unsigned i;
24 unsigned conn = 1;
25 int freepos = -1;
26
27 for (i = 0; cc[i].pid >= 0; ++i) {
28 if (!cc[i].ip) {
29 freepos = i;
30 continue;
31 }
32 if (strcmp(cc[i].ip, ip) == 0) {
33 conn++;
34 continue;
35 }
36 }
37 if (freepos == -1) return 0;
38 if (conn <= maxconn) {
39 cc[freepos].ip = ip;
40 *hccpp = &cc[freepos];
41 }
42 return conn;
43}
44
45void FAST_FUNC ipsvd_perhost_remove(struct hcc *cc, int pid)
46{
47 unsigned i;
48 for (i = 0; cc[i].pid >= 0; ++i) {
49 if (cc[i].pid == pid) {
50 free(cc[i].ip);
51 cc[i].ip = NULL;
52 cc[i].pid = 0;
53 return;
54 }
55 }
56}
57
58
59
60
61
62