1 /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
2  * which are released into public domain by the author.
3  * Homepage: http://smarden.sunsite.dk/ipsvd/
4  *
5  * Copyright (C) 2007 Denys Vlasenko.
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 
10 #include "libbb.h"
11 #include "tcpudp_perhost.h"
12 
ipsvd_perhost_init(unsigned c)13 struct hcc* FAST_FUNC ipsvd_perhost_init(unsigned c)
14 {
15 //	free(cc);
16 	struct hcc *cc = xzalloc((c + 1) * sizeof(*cc));
17 	cc[c].pid = -1; /* "end" marker */
18 	return cc;
19 }
20 
ipsvd_perhost_add(struct hcc * cc,char * ip,unsigned maxconn,struct hcc ** hccpp)21 unsigned 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 
ipsvd_perhost_remove(struct hcc * cc,int pid)45 void 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 //void ipsvd_perhost_free(struct hcc *cc)
59 //{
60 //	free(cc);
61 //}
62