tcpsvd: new applet
[oweals/busybox.git] / ipsvd / ipsvd_perhost.c
1 #include "busybox.h"
2 #include "ipsvd_perhost.h"
3
4 static struct hcc *cc;
5 static unsigned cclen;
6
7 /* to be optimized */
8
9 void ipsvd_perhost_init(unsigned c)
10 {
11 //      free(cc);
12         cc = xzalloc(c * sizeof(*cc));
13         cclen = c;
14 }
15
16 unsigned ipsvd_perhost_add(const char *ip, unsigned maxconn, struct hcc **hccpp)
17 {
18         unsigned i;
19         unsigned conn = 1;
20         int p = -1;
21
22         for (i = 0; i < cclen; ++i) {
23                 if (cc[i].ip[0] == 0) {
24                         if (p == -1) p = i;
25                         continue;
26                 }
27                 if (strncmp(cc[i].ip, ip, sizeof(cc[i].ip)) == 0) {
28                         conn++;
29                         continue;
30                 }
31         }
32         if (p == -1) return 0;
33         if (conn <= maxconn) {
34                 strcpy(cc[p].ip, ip);
35                 *hccpp = &cc[p];
36         }
37         return conn;
38 }
39
40 void ipsvd_perhost_remove(int pid)
41 {
42         unsigned i;
43         for (i = 0; i < cclen; ++i) {
44                 if (cc[i].pid == pid) {
45                         cc[i].ip[0] = 0;
46                         cc[i].pid = 0;
47                         return;
48                 }
49         }
50 }
51
52 //void ipsvd_perhost_free(void)
53 //{
54 //      free(cc);
55 //}